|
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 <event.h> |
|
28 |
|
29 #ifdef _EVENT___func__ |
|
30 #define __func__ _EVENT___func__ |
|
31 #endif |
|
32 |
|
33 int called = 0; |
|
34 |
|
35 static void |
|
36 signal_cb(evutil_socket_t fd, short event, void *arg) |
|
37 { |
|
38 struct event *signal = arg; |
|
39 |
|
40 printf("%s: got signal %d\n", __func__, EVENT_SIGNAL(signal)); |
|
41 |
|
42 if (called >= 2) |
|
43 event_del(signal); |
|
44 |
|
45 called++; |
|
46 } |
|
47 |
|
48 int |
|
49 main(int argc, char **argv) |
|
50 { |
|
51 struct event signal_int; |
|
52 struct event_base* base; |
|
53 #ifdef WIN32 |
|
54 WORD wVersionRequested; |
|
55 WSADATA wsaData; |
|
56 |
|
57 wVersionRequested = MAKEWORD(2, 2); |
|
58 |
|
59 (void) WSAStartup(wVersionRequested, &wsaData); |
|
60 #endif |
|
61 |
|
62 /* Initalize the event library */ |
|
63 base = event_base_new(); |
|
64 |
|
65 /* Initalize one event */ |
|
66 event_assign(&signal_int, base, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb, |
|
67 &signal_int); |
|
68 |
|
69 event_add(&signal_int, NULL); |
|
70 |
|
71 event_base_dispatch(base); |
|
72 event_base_free(base); |
|
73 |
|
74 return (0); |
|
75 } |
|
76 |