xref: /freebsd/contrib/ntp/sntp/libevent/sample/signal-test.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*
2  * Compile with:
3  * cc -I/usr/local/include -o signal-test \
4  *   signal-test.c -L/usr/local/lib -levent
5  */
6 
7 #include <sys/types.h>
8 
9 #include <event2/event-config.h>
10 
11 #include <sys/stat.h>
12 #ifndef _WIN32
13 #include <sys/queue.h>
14 #include <unistd.h>
15 #include <sys/time.h>
16 #else
17 #include <winsock2.h>
18 #include <windows.h>
19 #endif
20 #include <signal.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 
27 #include <event2/event.h>
28 
29 int called = 0;
30 
31 static void
32 signal_cb(evutil_socket_t fd, short event, void *arg)
33 {
34 	struct event *signal = arg;
35 
36 	printf("signal_cb: got signal %d\n", event_get_signal(signal));
37 
38 	if (called >= 2)
39 		event_del(signal);
40 
41 	called++;
42 }
43 
44 int
45 main(int argc, char **argv)
46 {
47 	struct event *signal_int = NULL;
48 	struct event_base* base;
49 	int ret = 0;
50 #ifdef _WIN32
51 	WORD wVersionRequested;
52 	WSADATA wsaData;
53 
54 	wVersionRequested = MAKEWORD(2, 2);
55 
56 	(void) WSAStartup(wVersionRequested, &wsaData);
57 #endif
58 
59 	/* Initialize the event library */
60 	base = event_base_new();
61 	if (!base) {
62 		ret = 1;
63 		goto out;
64 	}
65 
66 	/* Initialize one event */
67 	signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());
68 	if (!signal_int) {
69 		ret = 2;
70 		goto out;
71 	}
72 	event_add(signal_int, NULL);
73 
74 	event_base_dispatch(base);
75 
76 out:
77 	if (signal_int)
78 		event_free(signal_int);
79 	if (base)
80 		event_base_free(base);
81 	return ret;
82 }
83 
84