2002-04-10 00:31:31 +00:00
|
|
|
/*
|
|
|
|
* Compile with:
|
2009-01-15 06:15:24 +00:00
|
|
|
* cc -I/usr/local/include -o signal-test \
|
|
|
|
* signal-test.c -L/usr/local/lib -levent
|
2002-04-10 00:31:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2004-05-24 00:19:52 +00:00
|
|
|
|
2010-07-07 16:45:03 -04:00
|
|
|
#include <event2/event-config.h>
|
2007-03-01 06:25:18 +00:00
|
|
|
|
2002-04-10 00:31:31 +00:00
|
|
|
#include <sys/stat.h>
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifndef _WIN32
|
2002-04-10 00:31:31 +00:00
|
|
|
#include <sys/queue.h>
|
2003-09-25 03:29:37 +00:00
|
|
|
#include <unistd.h>
|
2002-04-10 00:31:31 +00:00
|
|
|
#include <sys/time.h>
|
2003-09-25 03:29:37 +00:00
|
|
|
#else
|
2009-07-28 19:41:39 +00:00
|
|
|
#include <winsock2.h>
|
2003-09-25 03:29:37 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2002-04-10 00:31:31 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-03-12 20:54:32 +02:00
|
|
|
#include <event2/event.h>
|
2002-04-10 00:31:31 +00:00
|
|
|
|
|
|
|
int called = 0;
|
|
|
|
|
2007-11-26 19:18:49 +00:00
|
|
|
static void
|
2010-03-05 12:47:46 -05:00
|
|
|
signal_cb(evutil_socket_t fd, short event, void *arg)
|
2002-04-10 00:31:31 +00:00
|
|
|
{
|
|
|
|
struct event *signal = arg;
|
|
|
|
|
2016-12-06 12:08:32 +03:00
|
|
|
printf("signal_cb: got signal %d\n", event_get_signal(signal));
|
2002-04-10 00:31:31 +00:00
|
|
|
|
|
|
|
if (called >= 2)
|
|
|
|
event_del(signal);
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2002-04-10 00:31:31 +00:00
|
|
|
called++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-02-19 03:39:50 -05:00
|
|
|
main(int argc, char **argv)
|
2002-04-10 00:31:31 +00:00
|
|
|
{
|
2019-08-21 01:00:07 +02:00
|
|
|
struct event *signal_int = NULL;
|
2009-12-08 17:02:24 -05:00
|
|
|
struct event_base* base;
|
2019-08-08 21:28:20 +08:00
|
|
|
int ret = 0;
|
2011-05-25 19:50:56 -04:00
|
|
|
#ifdef _WIN32
|
2009-05-22 19:11:59 +00:00
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
|
2012-11-01 17:38:34 -04:00
|
|
|
(void) WSAStartup(wVersionRequested, &wsaData);
|
2009-05-22 19:11:59 +00:00
|
|
|
#endif
|
2009-01-27 21:10:31 +00:00
|
|
|
|
2019-08-06 18:19:15 +08:00
|
|
|
/* Initialize the event library */
|
2009-12-08 17:02:24 -05:00
|
|
|
base = event_base_new();
|
2019-08-08 21:28:20 +08:00
|
|
|
if (!base) {
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-04-10 00:31:31 +00:00
|
|
|
|
2019-08-06 18:19:15 +08:00
|
|
|
/* Initialize one event */
|
2012-03-12 20:54:32 +02:00
|
|
|
signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());
|
2019-08-08 21:28:20 +08:00
|
|
|
if (!signal_int) {
|
|
|
|
ret = 2;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-03-12 20:54:32 +02:00
|
|
|
event_add(signal_int, NULL);
|
2002-04-10 00:31:31 +00:00
|
|
|
|
2009-12-08 17:02:24 -05:00
|
|
|
event_base_dispatch(base);
|
2002-04-10 00:31:31 +00:00
|
|
|
|
2019-08-08 21:28:20 +08:00
|
|
|
out:
|
|
|
|
if (signal_int)
|
|
|
|
event_free(signal_int);
|
|
|
|
if (base)
|
|
|
|
event_base_free(base);
|
|
|
|
return ret;
|
2002-04-10 00:31:31 +00:00
|
|
|
}
|
|
|
|
|