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