This source file includes following definitions.
- fpm_event_kqueue_module
- fpm_event_kqueue_init
- fpm_event_kqueue_clean
- fpm_event_kqueue_wait
- fpm_event_kqueue_add
- fpm_event_kqueue_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "../fpm_config.h"
22 #include "../fpm_events.h"
23 #include "../fpm.h"
24 #include "../zlog.h"
25
26 #if HAVE_KQUEUE
27
28 #include <sys/types.h>
29 #include <sys/event.h>
30 #include <sys/time.h>
31
32 #include <errno.h>
33
34 static int fpm_event_kqueue_init(int max);
35 static int fpm_event_kqueue_clean();
36 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
37 static int fpm_event_kqueue_add(struct fpm_event_s *ev);
38 static int fpm_event_kqueue_remove(struct fpm_event_s *ev);
39
40 static struct fpm_event_module_s kqueue_module = {
41 .name = "kqueue",
42 .support_edge_trigger = 1,
43 .init = fpm_event_kqueue_init,
44 .clean = fpm_event_kqueue_clean,
45 .wait = fpm_event_kqueue_wait,
46 .add = fpm_event_kqueue_add,
47 .remove = fpm_event_kqueue_remove,
48 };
49
50 static struct kevent *kevents = NULL;
51 static int nkevents = 0;
52 static int kfd = 0;
53
54 #endif
55
56
57
58
59 struct fpm_event_module_s *fpm_event_kqueue_module()
60 {
61 #if HAVE_KQUEUE
62 return &kqueue_module;
63 #else
64 return NULL;
65 #endif
66 }
67
68
69 #if HAVE_KQUEUE
70
71
72
73
74 static int fpm_event_kqueue_init(int max)
75 {
76 if (max < 1) {
77 return 0;
78 }
79
80 kfd = kqueue();
81 if (kfd < 0) {
82 zlog(ZLOG_ERROR, "kqueue: unable to initialize");
83 return -1;
84 }
85
86 kevents = malloc(sizeof(struct kevent) * max);
87 if (!kevents) {
88 zlog(ZLOG_ERROR, "epoll: unable to allocate %d events", max);
89 return -1;
90 }
91
92 memset(kevents, 0, sizeof(struct kevent) * max);
93
94 nkevents = max;
95
96 return 0;
97 }
98
99
100
101
102
103 static int fpm_event_kqueue_clean()
104 {
105 if (kevents) {
106 free(kevents);
107 kevents = NULL;
108 }
109
110 nkevents = 0;
111
112 return 0;
113 }
114
115
116
117
118
119 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout)
120 {
121 struct timespec t;
122 int ret, i;
123
124
125 memset(kevents, 0, sizeof(struct kevent) * nkevents);
126
127
128 t.tv_sec = timeout / 1000;
129 t.tv_nsec = (timeout % 1000) * 1000 * 1000;
130
131
132 ret = kevent(kfd, NULL, 0, kevents, nkevents, &t);
133 if (ret == -1) {
134
135
136 if (errno != EINTR) {
137 zlog(ZLOG_WARNING, "epoll_wait() returns %d", errno);
138 return -1;
139 }
140 }
141
142
143 for (i = 0; i < ret; i++) {
144 if (kevents[i].udata) {
145 struct fpm_event_s *ev = (struct fpm_event_s *)kevents[i].udata;
146 fpm_event_fire(ev);
147
148 if (fpm_globals.parent_pid != getpid()) {
149 return -2;
150 }
151 }
152 }
153
154 return ret;
155 }
156
157
158
159
160
161 static int fpm_event_kqueue_add(struct fpm_event_s *ev)
162 {
163 struct kevent k;
164 int flags = EV_ADD;
165
166 if (ev->flags & FPM_EV_EDGE) {
167 flags = flags | EV_CLEAR;
168 }
169
170 EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
171
172 if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
173 zlog(ZLOG_ERROR, "kevent: unable to add event");
174 return -1;
175 }
176
177
178 ev->index = ev->fd;
179 return 0;
180 }
181
182
183
184
185
186 static int fpm_event_kqueue_remove(struct fpm_event_s *ev)
187 {
188 struct kevent k;
189 int flags = EV_DELETE;
190
191 if (ev->flags & FPM_EV_EDGE) {
192 flags = flags | EV_CLEAR;
193 }
194
195 EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
196
197 if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
198 zlog(ZLOG_ERROR, "kevent: unable to add event");
199 return -1;
200 }
201
202
203 ev->index = -1;
204 return 0;
205 }
206
207
208 #endif