watcher: unregister a watcher FD if its thread gets cancelled
[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 * Cleanup function if callback gets cancelled
111 */
112 static void unregister(notify_data_t *data)
113 {
114 /* if a thread processing a callback gets cancelled, we mark the entry
115 * as cancelled, like the callback would return FALSE. This is required
116 * to not queue this watcher again if all threads have been gone. */
117 data->keep = FALSE;
118 }
119
120 /**
121 * Execute callback of registered FD, asynchronous
122 */
123 static job_requeue_t notify_async(notify_data_t *data)
124 {
125 thread_cleanup_push((void*)unregister, data);
126 data->keep = data->cb(data->data, data->fd, data->event);
127 thread_cleanup_pop(FALSE);
128 return JOB_REQUEUE_NONE;
129 }
130
131 /**
132 * Clean up notification data, reactivate FD
133 */
134 static void notify_end(notify_data_t *data)
135 {
136 private_watcher_t *this = data->this;
137 enumerator_t *enumerator;
138 entry_t *entry;
139
140 /* reactivate the disabled entry */
141 this->mutex->lock(this->mutex);
142 enumerator = this->fds->create_enumerator(this->fds);
143 while (enumerator->enumerate(enumerator, &entry))
144 {
145 if (entry->fd == data->fd)
146 {
147 if (!data->keep)
148 {
149 entry->events &= ~data->event;
150 if (!entry->events)
151 {
152 this->fds->remove_at(this->fds, enumerator);
153 free(entry);
154 break;
155 }
156 }
157 entry->active = TRUE;
158 break;
159 }
160 }
161 enumerator->destroy(enumerator);
162
163 update(this);
164 this->condvar->broadcast(this->condvar);
165 this->mutex->unlock(this->mutex);
166
167 free(data);
168 }
169
170 /**
171 * Execute the callback for a registered FD
172 */
173 static bool notify(private_watcher_t *this, entry_t *entry,
174 watcher_event_t event)
175 {
176 notify_data_t *data;
177
178 /* get a copy of entry for async job, but with specific event */
179 INIT(data,
180 .fd = entry->fd,
181 .event = event,
182 .cb = entry->cb,
183 .data = entry->data,
184 .keep = TRUE,
185 .this = this,
186 );
187
188 /* deactivate entry, so we can select() other FDs even if the async
189 * processing did not handle the event yet */
190 entry->active = FALSE;
191
192 lib->processor->queue_job(lib->processor,
193 (job_t*)callback_job_create_with_prio((void*)notify_async, data,
194 (void*)notify_end, (callback_job_cancel_t)return_false,
195 JOB_PRIO_CRITICAL));
196 return TRUE;
197 }
198
199 /**
200 * Thread cancellation function for watcher thread
201 */
202 static void activate_all(private_watcher_t *this)
203 {
204 enumerator_t *enumerator;
205 entry_t *entry;
206
207 /* When the watcher thread gets cancelled, we have to reactivate any entry
208 * and signal threads in remove() to go on. */
209
210 this->mutex->lock(this->mutex);
211 enumerator = this->fds->create_enumerator(this->fds);
212 while (enumerator->enumerate(enumerator, &entry))
213 {
214 entry->active = TRUE;
215 }
216 enumerator->destroy(enumerator);
217 this->condvar->broadcast(this->condvar);
218 this->mutex->unlock(this->mutex);
219 }
220
221 /**
222 * Dispatching function
223 */
224 static job_requeue_t watch(private_watcher_t *this)
225 {
226 enumerator_t *enumerator;
227 entry_t *entry;
228 fd_set rd, wr, ex;
229 int maxfd = 0, res;
230
231 FD_ZERO(&rd);
232 FD_ZERO(&wr);
233 FD_ZERO(&ex);
234
235 this->mutex->lock(this->mutex);
236 if (this->fds->get_count(this->fds) == 0)
237 {
238 this->mutex->unlock(this->mutex);
239 return JOB_REQUEUE_NONE;
240 }
241
242 if (this->notify[0] != -1)
243 {
244 FD_SET(this->notify[0], &rd);
245 maxfd = this->notify[0];
246 }
247
248 enumerator = this->fds->create_enumerator(this->fds);
249 while (enumerator->enumerate(enumerator, &entry))
250 {
251 if (entry->active)
252 {
253 if (entry->events & WATCHER_READ)
254 {
255 FD_SET(entry->fd, &rd);
256 }
257 if (entry->events & WATCHER_WRITE)
258 {
259 FD_SET(entry->fd, &wr);
260 }
261 if (entry->events & WATCHER_EXCEPT)
262 {
263 FD_SET(entry->fd, &ex);
264 }
265 maxfd = max(maxfd, entry->fd);
266 }
267 }
268 enumerator->destroy(enumerator);
269 this->mutex->unlock(this->mutex);
270
271 while (TRUE)
272 {
273 char buf[1];
274 bool old, notified = FALSE;
275
276 thread_cleanup_push((void*)activate_all, this);
277 old = thread_cancelability(TRUE);
278 res = select(maxfd + 1, &rd, &wr, &ex, NULL);
279 thread_cancelability(old);
280 thread_cleanup_pop(FALSE);
281 if (res > 0)
282 {
283 if (this->notify[0] != -1 && FD_ISSET(this->notify[0], &rd))
284 {
285 ignore_result(read(this->notify[0], buf, sizeof(buf)));
286 return JOB_REQUEUE_DIRECT;
287 }
288
289 this->mutex->lock(this->mutex);
290 enumerator = this->fds->create_enumerator(this->fds);
291 while (enumerator->enumerate(enumerator, &entry))
292 {
293 if (FD_ISSET(entry->fd, &rd))
294 {
295 notified = notify(this, entry, WATCHER_READ);
296 break;
297 }
298 if (FD_ISSET(entry->fd, &wr))
299 {
300 notified = notify(this, entry, WATCHER_WRITE);
301 break;
302 }
303 if (FD_ISSET(entry->fd, &ex))
304 {
305 notified = notify(this, entry, WATCHER_EXCEPT);
306 break;
307 }
308 }
309 enumerator->destroy(enumerator);
310 this->mutex->unlock(this->mutex);
311
312 if (notified)
313 {
314 /* we temporarily disable a notified FD, rebuild FDSET */
315 return JOB_REQUEUE_DIRECT;
316 }
317 }
318 }
319 }
320
321 METHOD(watcher_t, add, void,
322 private_watcher_t *this, int fd, watcher_event_t events,
323 watcher_cb_t cb, void *data)
324 {
325 entry_t *entry;
326
327 INIT(entry,
328 .fd = fd,
329 .events = events,
330 .cb = cb,
331 .data = data,
332 .active = TRUE,
333 );
334
335 this->mutex->lock(this->mutex);
336 this->fds->insert_last(this->fds, entry);
337 if (this->fds->get_count(this->fds) == 1)
338 {
339 lib->processor->queue_job(lib->processor,
340 (job_t*)callback_job_create_with_prio((void*)watch, this,
341 NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
342 }
343 else
344 {
345 update(this);
346 }
347 this->mutex->unlock(this->mutex);
348 }
349
350 METHOD(watcher_t, remove_, void,
351 private_watcher_t *this, int fd)
352 {
353 enumerator_t *enumerator;
354 entry_t *entry;
355
356 this->mutex->lock(this->mutex);
357 while (TRUE)
358 {
359 bool is_in_callback = FALSE;
360
361 enumerator = this->fds->create_enumerator(this->fds);
362 while (enumerator->enumerate(enumerator, &entry))
363 {
364 if (entry->fd == fd)
365 {
366 if (entry->active)
367 {
368 this->fds->remove_at(this->fds, enumerator);
369 free(entry);
370 }
371 else
372 {
373 is_in_callback = TRUE;
374 break;
375 }
376 }
377 }
378 enumerator->destroy(enumerator);
379 if (!is_in_callback)
380 {
381 break;
382 }
383 this->condvar->wait(this->condvar, this->mutex);
384 }
385
386 update(this);
387 this->mutex->unlock(this->mutex);
388 }
389
390 METHOD(watcher_t, destroy, void,
391 private_watcher_t *this)
392 {
393 this->mutex->destroy(this->mutex);
394 this->condvar->destroy(this->condvar);
395 this->fds->destroy(this->fds);
396 if (this->notify[0] != -1)
397 {
398 close(this->notify[0]);
399 }
400 if (this->notify[1] != -1)
401 {
402 close(this->notify[1]);
403 }
404 free(this);
405 }
406
407 /**
408 * See header
409 */
410 watcher_t *watcher_create()
411 {
412 private_watcher_t *this;
413
414 INIT(this,
415 .public = {
416 .add = _add,
417 .remove = _remove_,
418 .destroy = _destroy,
419 },
420 .fds = linked_list_create(),
421 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
422 .condvar = condvar_create(CONDVAR_TYPE_DEFAULT),
423 .notify[0] = -1,
424 .notify[1] = -1,
425 );
426
427 if (pipe(this->notify) != 0)
428 {
429 DBG1(DBG_LIB, "creating watcher notify pipe failed: %s",
430 strerror(errno));
431 }
432 return &this->public;
433 }