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