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 #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_get_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 signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg()); 67 68 event_add(signal_int, NULL); 69 70 event_base_dispatch(base); 71 event_base_free(base); 72 73 return (0); 74 } 75 76