7ccac72bc17c321ccdb79e7fee19970acb036236
[strongswan.git] / src / libstrongswan / processing / watcher.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "watcher.h"
17
18 #include <library.h>
19 #include <threading/thread.h>
20 #include <threading/mutex.h>
21 #include <threading/condvar.h>
22 #include <collections/linked_list.h>
23 #include <processing/jobs/callback_job.h>
24
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/select.h>
28
29 typedef struct private_watcher_t private_watcher_t;
30
31 /**
32 * Private data of an watcher_t object.
33 */
34 struct private_watcher_t {
35
36 /**
37 * Public watcher_t interface.
38 */
39 watcher_t public;
40
41 /**
42 * List of registered FDs, as entry_t
43 */
44 linked_list_t *fds;
45
46 /**
47 * Lock to access FD list
48 */
49 mutex_t *mutex;
50
51 /**
52 * Condvar to signal completion of callback
53 */
54 condvar_t *condvar;
55
56 /**
57 * Notification pipe to signal watcher thread
58 */
59 int notify[2];
60 };
61
62 /**
63 * Entry for a registered file descriptor
64 */
65 typedef struct {
66 /** file descriptor */
67 int fd;
68 /** events to watch */
69 watcher_event_t events;
70 /** registered callback function */
71 watcher_cb_t cb;
72 /** user data to pass to callback */
73 void *data;
74 /** callback currently active? */
75 bool active;
76 } entry_t;
77
78 /**
79 * Data we pass on for an async notification
80 */
81 typedef struct {
82 /** file descriptor */
83 int fd;
84 /** event type */
85 watcher_event_t event;
86 /** registered callback function */
87 watcher_cb_t cb;
88 /** user data to pass to callback */
89 void *data;
90 /** keep registered? */
91 bool keep;
92 /** reference to watcher */
93 private_watcher_t *this;
94 } notify_data_t;
95
96 /**
97 * Notify watcher thread about changes
98 */
99 static void update(private_watcher_t *this)
100 {
101 char buf[1] = { 'u' };
102
103 if (this->notify[1] != -1)
104 {
105 ignore_result(write(this->notify[1], buf, sizeof(buf)));
106 }
107 }
108
109 /**
110 * Execute callback of registered FD, asynchronous
111 */
112 static job_requeue_t notify_async(notify_data_t *data)
113 {
114 data->keep = data->cb(data->data, data->fd, data->event);
115 return JOB_REQUEUE_NONE;
116 }
117
118 /**
119 * Clean up notification data, reactivate FD
120 */
121 static void notify_end(notify_data_t *data)
122 {
123 private_watcher_t *this = data->this;
124 enumerator_t *enumerator;
125 entry_t *entry;
126
127 /* reactivate the disabled entry */
128 this->mutex->lock(this->mutex);
129 enumerator = this->fds->create_enumerator(this->fds);
130 while (enumerator->enumerate(enumerator, &entry))
131 {
132 if (entry->fd == data->fd)
133 {
134 if (!data->keep)
135 {
136 entry->events &= ~data->event;
137 if (!entry->events)
138 {
139 this->fds->remove_at(this->fds, enumerator);
140 free(entry);
141 break;
142 }
143 }
144 entry->active = TRUE;
145 break;
146 }
147 }
148 enumerator->destroy(enumerator);
149
150 update(this);
151 this->condvar->broadcast(this->condvar);
152 this->mutex->unlock(this->mutex);
153
154 free(data);
155 }
156
157 /**
158 * Execute the callback for a registered FD
159 */
160 static bool notify(private_watcher_t *this, entry_t *entry,
161 watcher_event_t event)
162 {
163 notify_data_t *data;
164
165 /* get a copy of entry for async job, but with specific event */
166 INIT(data,
167 .fd = entry->fd,
168 .event = event,
169 .cb = entry->cb,
170 .data = entry->data,
171 .keep = TRUE,
172 .this = this,
173 );
174
175 /* deactivate entry, so we can select() other FDs even if the async
176 * processing did not handle the event yet */
177 entry->active = FALSE;
178
179 lib->processor->queue_job(lib->processor,
180 (job_t*)callback_job_create_with_prio((void*)notify_async, data,
181 (void*)notify_end, (callback_job_cancel_t)return_false,
182 JOB_PRIO_CRITICAL));
183 return TRUE;
184 }
185
186 /**
187 * Dispatching function
188 */
189 static job_requeue_t watch(private_watcher_t *this)
190 {
191 enumerator_t *enumerator;
192 entry_t *entry;
193 fd_set rd, wr, ex;
194 int maxfd = 0, res;
195
196 FD_ZERO(&rd);
197 FD_ZERO(&wr);
198 FD_ZERO(&ex);
199
200 this->mutex->lock(this->mutex);
201 if (this->fds->get_count(this->fds) == 0)
202 {
203 this->mutex->unlock(this->mutex);
204 return JOB_REQUEUE_NONE;
205 }
206
207 if (this->notify[0] != -1)
208 {
209 FD_SET(this->notify[0], &rd);
210 maxfd = this->notify[0];
211 }
212
213 enumerator = this->fds->create_enumerator(this->fds);
214 while (enumerator->enumerate(enumerator, &entry))
215 {
216 if (entry->active)
217 {
218 if (entry->events & WATCHER_READ)
219 {
220 FD_SET(entry->fd, &rd);
221 }
222 if (entry->events & WATCHER_WRITE)
223 {
224 FD_SET(entry->fd, &wr);
225 }
226 if (entry->events & WATCHER_EXCEPT)
227 {
228 FD_SET(entry->fd, &ex);
229 }
230 maxfd = max(maxfd, entry->fd);
231 }
232 }
233 enumerator->destroy(enumerator);
234 this->mutex->unlock(this->mutex);
235
236 while (TRUE)
237 {
238 char buf[1];
239 bool old, notified = FALSE;
240
241 old = thread_cancelability(TRUE);
242 res = select(maxfd + 1, &rd, &wr, &ex, NULL);
243 thread_cancelability(old);
244 if (res > 0)
245 {
246 if (this->notify[0] != -1 && FD_ISSET(this->notify[0], &rd))
247 {
248 ignore_result(read(this->notify[0], buf, sizeof(buf)));
249 return JOB_REQUEUE_DIRECT;
250 }
251
252 this->mutex->lock(this->mutex);
253 enumerator = this->fds->create_enumerator(this->fds);
254 while (enumerator->enumerate(enumerator, &entry))
255 {
256 if (FD_ISSET(entry->fd, &rd))
257 {
258 notified = notify(this, entry, WATCHER_READ);
259 break;
260 }
261 if (FD_ISSET(entry->fd, &wr))
262 {
263 notified = notify(this, entry, WATCHER_WRITE);
264 break;
265 }
266 if (FD_ISSET(entry->fd, &ex))
267 {
268 notified = notify(this, entry, WATCHER_EXCEPT);
269 break;
270 }
271 }
272 enumerator->destroy(enumerator);
273 this->mutex->unlock(this->mutex);
274
275 if (notified)
276 {
277 /* we temporarily disable a notified FD, rebuild FDSET */
278 return JOB_REQUEUE_DIRECT;
279 }
280 }
281 }
282 }
283
284 METHOD(watcher_t, add, void,
285 private_watcher_t *this, int fd, watcher_event_t events,
286 watcher_cb_t cb, void *data)
287 {
288 entry_t *entry;
289
290 INIT(entry,
291 .fd = fd,
292 .events = events,
293 .cb = cb,
294 .data = data,
295 .active = TRUE,
296 );
297
298 this->mutex->lock(this->mutex);
299 this->fds->insert_last(this->fds, entry);
300 if (this->fds->get_count(this->fds) == 1)
301 {
302 lib->processor->queue_job(lib->processor,
303 (job_t*)callback_job_create_with_prio((void*)watch, this,
304 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
305 }
306 else
307 {
308 update(this);
309 }
310 this->mutex->unlock(this->mutex);
311 }
312
313 METHOD(watcher_t, remove_, void,
314 private_watcher_t *this, int fd)
315 {
316 enumerator_t *enumerator;
317 entry_t *entry;
318
319 this->mutex->lock(this->mutex);
320 while (TRUE)
321 {
322 bool is_in_callback = FALSE;
323
324 enumerator = this->fds->create_enumerator(this->fds);
325 while (enumerator->enumerate(enumerator, &entry))
326 {
327 if (entry->fd == fd)
328 {
329 if (entry->active)
330 {
331 this->fds->remove_at(this->fds, enumerator);
332 free(entry);
333 }
334 else
335 {
336 is_in_callback = TRUE;
337 break;
338 }
339 }
340 }
341 enumerator->destroy(enumerator);
342 if (!is_in_callback)
343 {
344 break;
345 }
346 this->condvar->wait(this->condvar, this->mutex);
347 }
348
349 update(this);
350 this->mutex->unlock(this->mutex);
351 }
352
353 METHOD(watcher_t, destroy, void,
354 private_watcher_t *this)
355 {
356 this->mutex->destroy(this->mutex);
357 this->condvar->destroy(this->condvar);
358 this->fds->destroy(this->fds);
359 if (this->notify[0] != -1)
360 {
361 close(this->notify[0]);
362 }
363 if (this->notify[1] != -1)
364 {
365 close(this->notify[1]);
366 }
367 free(this);
368 }
369
370 /**
371 * See header
372 */
373 watcher_t *watcher_create()
374 {
375 private_watcher_t *this;
376
377 INIT(this,
378 .public = {
379 .add = _add,
380 .remove = _remove_,
381 .destroy = _destroy,
382 },
383 .fds = linked_list_create(),
384 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
385 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
386 .notify[0] = -1,
387 .notify[1] = -1,
388 );
389
390 if (pipe(this->notify) != 0)
391 {
392 DBG1(DBG_LIB, "creating watcher notify pipe failed: %s",
393 strerror(errno));
394 }
395 return &this->public;
396 }