This source file includes following definitions.
- cbfunc
- die
- t1func
- main
1
2 #include <event2/event.h>
3 #include <event2/event_struct.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <signal.h>
8 #include <stdbool.h>
9
10 #define SIGPRI 0
11 #define TERMPRI 1
12
13 static struct event_base *base;
14 static bool run=true;
15 static int loops=0;
16
17 static void
18 cbfunc(evutil_socket_t fd, short what, void *arg)
19 {
20 fprintf(stderr, "CAUGHT SIGNAL\n");
21 fflush(stderr);
22 #if 0
23 event_base_loopbreak(base);
24 #endif
25 run = false;
26 }
27
28 static void
29 die(const char *msg)
30 {
31 fprintf(stderr, "%s\n", msg);
32 fflush(stderr);
33 exit(1);
34 }
35
36 static void
37 t1func(evutil_socket_t fd, short what, void *arg)
38 {
39 struct event *t1 = (struct event*)arg;
40 struct event *t2;
41
42 fprintf(stderr, "CAUGHT EVENT\n");
43 fflush(stderr);
44 event_del(t1);
45 free(t1);
46 loops++;
47 if (loops < 10) {
48 t2 = (struct event*)malloc(sizeof(struct event));
49 if (event_assign(t2, base, -1, EV_WRITE, t1func, t2) < 0) {
50 die("event_assign_term");
51 }
52 if (event_priority_set(t2, TERMPRI) < 0) {
53 die("event_priority_set_term");
54 }
55 fprintf(stderr, "EVENT %d DEFINED\n", loops);
56 fflush(stderr);
57 event_active(t2, EV_WRITE, 1);
58 fprintf(stderr, "EVENT %d ACTIVATED\n", loops);
59 fflush(stderr);
60 }
61 }
62
63 int
64 main(int argc, char **argv)
65 {
66 struct event ev;
67 struct event *t1;
68
69 event_enable_debug_mode();
70
71 fprintf(stderr, "Libevent %s\n", event_get_version());
72 fflush(stderr);
73
74 if (!(base = event_base_new()))
75 die("event_base_new");
76 if (event_base_priority_init(base, 8) < 0)
77 die("event_base_priority_init");
78 if (event_assign(&ev, base, SIGTERM, EV_SIGNAL|EV_PERSIST, cbfunc, NULL)<0)
79 die("event_assign");
80 if (event_priority_set(&ev, SIGPRI) < 0)
81 die("event_priority_set");
82 if (event_add(&ev, NULL) < 0)
83 die("event_add");
84 fprintf(stderr, "SIGNAL EVENT DEFINED\n");
85 fflush(stderr);
86
87 t1 = (struct event*)malloc(sizeof(struct event));
88 if (event_assign(t1, base, -1, EV_WRITE, t1func, t1) < 0) {
89 die("event_assign_term");
90 }
91 if (event_priority_set(t1, TERMPRI) < 0) {
92 die("event_priority_set_term");
93 }
94 event_active(t1, EV_WRITE, 1);
95 fprintf(stderr, "FIRST TERMINATION EVENT DEFINED\n");
96 fflush(stderr);
97
98
99
100 while (run) {
101 event_base_loop(base, EVLOOP_ONCE);
102 }
103
104 fprintf(stderr, "EXITED LOOP - FREEING BASE\n");
105 fflush(stderr);
106 event_base_free(base);
107 return 0;
108 }