Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
964e027d | 3 | * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
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 | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
768d7355 RK |
20 | /** @file lib/event.c |
21 | * @brief DisOrder event loop | |
22 | */ | |
460b9539 | 23 | |
24 | #include <config.h> | |
25 | ||
26 | #include <unistd.h> | |
27 | #include <fcntl.h> | |
28 | #include <sys/time.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/resource.h> | |
31 | #include <sys/wait.h> | |
46bd8db4 | 32 | #include <sys/stat.h> |
460b9539 | 33 | #include <unistd.h> |
34 | #include <assert.h> | |
35 | #include <signal.h> | |
36 | #include <errno.h> | |
37 | #include <string.h> | |
38 | #include <limits.h> | |
39 | #include <sys/socket.h> | |
40 | #include <netinet/in.h> | |
41 | #include <sys/un.h> | |
42 | #include <stdio.h> | |
43 | #include "event.h" | |
44 | #include "mem.h" | |
45 | #include "log.h" | |
46 | #include "syscalls.h" | |
47 | #include "printf.h" | |
48 | #include "sink.h" | |
768d7355 | 49 | #include "vector.h" |
460b9539 | 50 | |
768d7355 | 51 | /** @brief A timeout */ |
460b9539 | 52 | struct timeout { |
53 | struct timeout *next; | |
54 | struct timeval when; | |
55 | ev_timeout_callback *callback; | |
56 | void *u; | |
57 | int resolve; | |
58 | }; | |
59 | ||
768d7355 | 60 | /** @brief A file descriptor in one mode */ |
460b9539 | 61 | struct fd { |
62 | int fd; | |
63 | ev_fd_callback *callback; | |
64 | void *u; | |
e8c92ba7 | 65 | const char *what; |
460b9539 | 66 | }; |
67 | ||
768d7355 | 68 | /** @brief All the file descriptors in a given mode */ |
460b9539 | 69 | struct fdmode { |
768d7355 | 70 | /** @brief Mask of active file descriptors passed to @c select() */ |
460b9539 | 71 | fd_set enabled; |
768d7355 RK |
72 | |
73 | /** @brief File descriptor mask returned from @c select() */ | |
460b9539 | 74 | fd_set tripped; |
768d7355 RK |
75 | |
76 | /** @brief Number of file descriptors in @p fds */ | |
77 | int nfds; | |
78 | ||
79 | /** @brief Number of slots in @p fds */ | |
80 | int fdslots; | |
81 | ||
82 | /** @brief Array of all active file descriptors */ | |
460b9539 | 83 | struct fd *fds; |
768d7355 RK |
84 | |
85 | /** @brief Highest-numbered file descriptor or 0 */ | |
460b9539 | 86 | int maxfd; |
87 | }; | |
88 | ||
768d7355 | 89 | /** @brief A signal handler */ |
460b9539 | 90 | struct signal { |
91 | struct sigaction oldsa; | |
92 | ev_signal_callback *callback; | |
93 | void *u; | |
94 | }; | |
95 | ||
768d7355 | 96 | /** @brief A child process */ |
460b9539 | 97 | struct child { |
98 | pid_t pid; | |
99 | int options; | |
100 | ev_child_callback *callback; | |
101 | void *u; | |
102 | }; | |
103 | ||
768d7355 | 104 | /** @brief An event loop */ |
460b9539 | 105 | struct ev_source { |
768d7355 | 106 | /** @brief File descriptors, per mode */ |
460b9539 | 107 | struct fdmode mode[ev_nmodes]; |
768d7355 RK |
108 | |
109 | /** @brief Sorted linked list of timeouts | |
110 | * | |
111 | * We could use @ref HEAP_TYPE now, but there aren't many timeouts. | |
112 | */ | |
460b9539 | 113 | struct timeout *timeouts; |
768d7355 RK |
114 | |
115 | /** @brief Array of handled signals */ | |
460b9539 | 116 | struct signal signals[NSIG]; |
768d7355 RK |
117 | |
118 | /** @brief Mask of handled signals */ | |
460b9539 | 119 | sigset_t sigmask; |
768d7355 RK |
120 | |
121 | /** @brief Escape early from handling of @c select() results | |
122 | * | |
123 | * This is set if any of the file descriptor arrays are invalidated, since | |
124 | * it's then not safe for processing of them to continue. | |
125 | */ | |
460b9539 | 126 | int escape; |
768d7355 RK |
127 | |
128 | /** @brief Signal handling pipe | |
129 | * | |
130 | * The signal handle writes signal numbers down this pipe. | |
131 | */ | |
460b9539 | 132 | int sigpipe[2]; |
768d7355 RK |
133 | |
134 | /** @brief Number of child processes in @p children */ | |
135 | int nchildren; | |
136 | ||
137 | /** @brief Number of slots in @p children */ | |
138 | int nchildslots; | |
139 | ||
140 | /** @brief Array of child processes */ | |
460b9539 | 141 | struct child *children; |
142 | }; | |
143 | ||
768d7355 | 144 | /** @brief Names of file descriptor modes */ |
460b9539 | 145 | static const char *modenames[] = { "read", "write", "except" }; |
146 | ||
147 | /* utilities ******************************************************************/ | |
148 | ||
768d7355 RK |
149 | /** @brief Great-than comparison for timevals |
150 | * | |
151 | * Ought to be in @file lib/timeval.h | |
152 | */ | |
460b9539 | 153 | static inline int gt(const struct timeval *a, const struct timeval *b) { |
154 | if(a->tv_sec > b->tv_sec) | |
155 | return 1; | |
156 | if(a->tv_sec == b->tv_sec | |
157 | && a->tv_usec > b->tv_usec) | |
158 | return 1; | |
159 | return 0; | |
160 | } | |
161 | ||
768d7355 RK |
162 | /** @brief Greater-than-or-equal comparison for timevals |
163 | * | |
3149c1e2 | 164 | * Ought to be in @ref lib/timeval.h |
768d7355 | 165 | */ |
460b9539 | 166 | static inline int ge(const struct timeval *a, const struct timeval *b) { |
167 | return !gt(b, a); | |
168 | } | |
169 | ||
170 | /* creation *******************************************************************/ | |
171 | ||
768d7355 | 172 | /** @brief Create a new event loop */ |
460b9539 | 173 | ev_source *ev_new(void) { |
174 | ev_source *ev = xmalloc(sizeof *ev); | |
175 | int n; | |
176 | ||
177 | memset(ev, 0, sizeof *ev); | |
178 | for(n = 0; n < ev_nmodes; ++n) | |
179 | FD_ZERO(&ev->mode[n].enabled); | |
180 | ev->sigpipe[0] = ev->sigpipe[1] = -1; | |
181 | sigemptyset(&ev->sigmask); | |
182 | return ev; | |
183 | } | |
184 | ||
185 | /* event loop *****************************************************************/ | |
186 | ||
768d7355 RK |
187 | /** @brief Run the event loop |
188 | * @return -1 on error, non-0 if any callback returned non-0 | |
189 | */ | |
460b9539 | 190 | int ev_run(ev_source *ev) { |
191 | for(;;) { | |
192 | struct timeval now; | |
193 | struct timeval delta; | |
194 | int n, mode; | |
195 | int ret; | |
196 | int maxfd; | |
197 | struct timeout *t, **tt; | |
e8c92ba7 | 198 | struct stat sb; |
460b9539 | 199 | |
200 | xgettimeofday(&now, 0); | |
201 | /* Handle timeouts. We don't want to handle any timeouts that are added | |
202 | * while we're handling them (otherwise we'd have to break out of infinite | |
203 | * loops, preferrably without starving better-behaved subsystems). Hence | |
204 | * the slightly complicated two-phase approach here. */ | |
205 | for(t = ev->timeouts; | |
206 | t && ge(&now, &t->when); | |
207 | t = t->next) { | |
208 | t->resolve = 1; | |
209 | D(("calling timeout for %ld.%ld callback %p %p", | |
210 | (long)t->when.tv_sec, (long)t->when.tv_usec, | |
211 | (void *)t->callback, t->u)); | |
212 | ret = t->callback(ev, &now, t->u); | |
213 | if(ret) | |
214 | return ret; | |
215 | } | |
216 | tt = &ev->timeouts; | |
217 | while((t = *tt)) { | |
218 | if(t->resolve) | |
219 | *tt = t->next; | |
220 | else | |
221 | tt = &t->next; | |
222 | } | |
223 | maxfd = 0; | |
224 | for(mode = 0; mode < ev_nmodes; ++mode) { | |
225 | ev->mode[mode].tripped = ev->mode[mode].enabled; | |
226 | if(ev->mode[mode].maxfd > maxfd) | |
227 | maxfd = ev->mode[mode].maxfd; | |
228 | } | |
229 | xsigprocmask(SIG_UNBLOCK, &ev->sigmask, 0); | |
230 | do { | |
231 | if(ev->timeouts) { | |
232 | xgettimeofday(&now, 0); | |
233 | delta.tv_sec = ev->timeouts->when.tv_sec - now.tv_sec; | |
234 | delta.tv_usec = ev->timeouts->when.tv_usec - now.tv_usec; | |
235 | if(delta.tv_usec < 0) { | |
236 | delta.tv_usec += 1000000; | |
237 | --delta.tv_sec; | |
238 | } | |
239 | if(delta.tv_sec < 0) | |
240 | delta.tv_sec = delta.tv_usec = 0; | |
241 | n = select(maxfd + 1, | |
242 | &ev->mode[ev_read].tripped, | |
243 | &ev->mode[ev_write].tripped, | |
244 | &ev->mode[ev_except].tripped, | |
245 | &delta); | |
246 | } else { | |
247 | n = select(maxfd + 1, | |
248 | &ev->mode[ev_read].tripped, | |
249 | &ev->mode[ev_write].tripped, | |
250 | &ev->mode[ev_except].tripped, | |
251 | 0); | |
252 | } | |
253 | } while(n < 0 && errno == EINTR); | |
254 | xsigprocmask(SIG_BLOCK, &ev->sigmask, 0); | |
255 | if(n < 0) { | |
256 | error(errno, "error calling select"); | |
e8c92ba7 RK |
257 | if(errno == EBADF) { |
258 | /* If there's a bad FD in the mix then check them all and log what we | |
259 | * find, to ease debugging */ | |
260 | for(mode = 0; mode < ev_nmodes; ++mode) { | |
261 | for(n = 0; n < ev->mode[mode].nfds; ++n) { | |
262 | const int fd = ev->mode[mode].fds[n].fd; | |
263 | ||
264 | if(FD_ISSET(fd, &ev->mode[mode].enabled) | |
265 | && fstat(fd, &sb) < 0) | |
34a3e246 RK |
266 | error(errno, "mode %s fstat %d (%s)", |
267 | modenames[mode], fd, ev->mode[mode].fds[n].what); | |
e8c92ba7 | 268 | } |
7958ad2f | 269 | for(n = 0; n <= maxfd; ++n) |
34a3e246 RK |
270 | if(FD_ISSET(n, &ev->mode[mode].enabled) |
271 | && fstat(n, &sb) < 0) | |
272 | error(errno, "mode %s fstat %d", modenames[mode], n); | |
e8c92ba7 RK |
273 | } |
274 | } | |
460b9539 | 275 | return -1; |
276 | } | |
277 | if(n > 0) { | |
278 | /* if anything deranges the meaning of an fd, or re-orders the | |
279 | * fds[] tables, we'd better give up; such operations will | |
280 | * therefore set @escape@. */ | |
281 | ev->escape = 0; | |
282 | for(mode = 0; mode < ev_nmodes && !ev->escape; ++mode) | |
283 | for(n = 0; n < ev->mode[mode].nfds && !ev->escape; ++n) { | |
284 | int fd = ev->mode[mode].fds[n].fd; | |
285 | if(FD_ISSET(fd, &ev->mode[mode].tripped)) { | |
286 | D(("calling %s fd %d callback %p %p", modenames[mode], fd, | |
287 | (void *)ev->mode[mode].fds[n].callback, | |
288 | ev->mode[mode].fds[n].u)); | |
289 | ret = ev->mode[mode].fds[n].callback(ev, fd, | |
290 | ev->mode[mode].fds[n].u); | |
291 | if(ret) | |
292 | return ret; | |
293 | } | |
294 | } | |
295 | } | |
296 | /* we'll pick up timeouts back round the loop */ | |
297 | } | |
298 | } | |
299 | ||
300 | /* file descriptors ***********************************************************/ | |
301 | ||
768d7355 RK |
302 | /** @brief Register a file descriptor |
303 | * @param ev Event loop | |
304 | * @param mode @c ev_read or @c ev_write | |
305 | * @param fd File descriptor | |
306 | * @param callback Called when @p is readable/writable | |
307 | * @param u Passed to @p callback | |
308 | * @param what Text description | |
309 | * @return 0 on success, non-0 on error | |
310 | * | |
311 | * Sets @ref ev_source::escape, so no further processing of file descriptors | |
312 | * will occur this time round the event loop. | |
313 | */ | |
460b9539 | 314 | int ev_fd(ev_source *ev, |
315 | ev_fdmode mode, | |
316 | int fd, | |
317 | ev_fd_callback *callback, | |
e8c92ba7 RK |
318 | void *u, |
319 | const char *what) { | |
460b9539 | 320 | int n; |
321 | ||
322 | D(("registering %s fd %d callback %p %p", modenames[mode], fd, | |
323 | (void *)callback, u)); | |
324 | assert(mode < ev_nmodes); | |
325 | if(ev->mode[mode].nfds >= ev->mode[mode].fdslots) { | |
326 | ev->mode[mode].fdslots = (ev->mode[mode].fdslots | |
327 | ? 2 * ev->mode[mode].fdslots : 16); | |
328 | D(("expanding %s fd table to %d entries", modenames[mode], | |
329 | ev->mode[mode].fdslots)); | |
330 | ev->mode[mode].fds = xrealloc(ev->mode[mode].fds, | |
331 | ev->mode[mode].fdslots * sizeof (struct fd)); | |
332 | } | |
333 | n = ev->mode[mode].nfds++; | |
334 | FD_SET(fd, &ev->mode[mode].enabled); | |
335 | ev->mode[mode].fds[n].fd = fd; | |
336 | ev->mode[mode].fds[n].callback = callback; | |
337 | ev->mode[mode].fds[n].u = u; | |
e8c92ba7 | 338 | ev->mode[mode].fds[n].what = what; |
460b9539 | 339 | if(fd > ev->mode[mode].maxfd) |
340 | ev->mode[mode].maxfd = fd; | |
341 | ev->escape = 1; | |
342 | return 0; | |
343 | } | |
344 | ||
768d7355 RK |
345 | /** @brief Cancel a file descriptor |
346 | * @param ev Event loop | |
347 | * @param mode @c ev_read or @c ev_write | |
348 | * @param fd File descriptor | |
349 | * @return 0 on success, non-0 on error | |
350 | * | |
351 | * Sets @ref ev_source::escape, so no further processing of file descriptors | |
352 | * will occur this time round the event loop. | |
353 | */ | |
460b9539 | 354 | int ev_fd_cancel(ev_source *ev, ev_fdmode mode, int fd) { |
355 | int n; | |
356 | int maxfd; | |
357 | ||
358 | D(("cancelling mode %s fd %d", modenames[mode], fd)); | |
359 | /* find the right struct fd */ | |
360 | for(n = 0; n < ev->mode[mode].nfds && fd != ev->mode[mode].fds[n].fd; ++n) | |
361 | ; | |
362 | assert(n < ev->mode[mode].nfds); | |
363 | /* swap in the last fd and reduce the count */ | |
364 | if(n != ev->mode[mode].nfds - 1) | |
365 | ev->mode[mode].fds[n] = ev->mode[mode].fds[ev->mode[mode].nfds - 1]; | |
366 | --ev->mode[mode].nfds; | |
367 | /* if that was the biggest fd, find the new biggest one */ | |
368 | if(fd == ev->mode[mode].maxfd) { | |
369 | maxfd = 0; | |
370 | for(n = 0; n < ev->mode[mode].nfds; ++n) | |
371 | if(ev->mode[mode].fds[n].fd > maxfd) | |
372 | maxfd = ev->mode[mode].fds[n].fd; | |
373 | ev->mode[mode].maxfd = maxfd; | |
374 | } | |
375 | /* don't tell select about this fd any more */ | |
376 | FD_CLR(fd, &ev->mode[mode].enabled); | |
377 | ev->escape = 1; | |
378 | return 0; | |
379 | } | |
380 | ||
768d7355 RK |
381 | /** @brief Re-enable a file descriptor |
382 | * @param ev Event loop | |
383 | * @param mode @c ev_read or @c ev_write | |
384 | * @param fd File descriptor | |
385 | * @return 0 on success, non-0 on error | |
386 | * | |
387 | * It is harmless if @p fd is currently disabled, but it must not have been | |
388 | * cancelled. | |
389 | */ | |
460b9539 | 390 | int ev_fd_enable(ev_source *ev, ev_fdmode mode, int fd) { |
38b8221f | 391 | assert(fd >= 0); |
460b9539 | 392 | D(("enabling mode %s fd %d", modenames[mode], fd)); |
393 | FD_SET(fd, &ev->mode[mode].enabled); | |
394 | return 0; | |
395 | } | |
396 | ||
768d7355 RK |
397 | /** @brief Temporarily disable a file descriptor |
398 | * @param ev Event loop | |
399 | * @param mode @c ev_read or @c ev_write | |
400 | * @param fd File descriptor | |
401 | * @return 0 on success, non-0 on error | |
402 | * | |
403 | * Re-enable with ev_fd_enable(). It is harmless if @p fd is already disabled, | |
404 | * but it must not have been cancelled. | |
405 | */ | |
460b9539 | 406 | int ev_fd_disable(ev_source *ev, ev_fdmode mode, int fd) { |
407 | D(("disabling mode %s fd %d", modenames[mode], fd)); | |
408 | FD_CLR(fd, &ev->mode[mode].enabled); | |
409 | FD_CLR(fd, &ev->mode[mode].tripped); | |
75d64210 RK |
410 | /* Suppress any pending callbacks */ |
411 | ev->escape = 1; | |
460b9539 | 412 | return 0; |
413 | } | |
414 | ||
768d7355 RK |
415 | /** @brief Log a report of file descriptor state */ |
416 | void ev_report(ev_source *ev) { | |
417 | int n, fd; | |
418 | ev_fdmode mode; | |
419 | struct dynstr d[1]; | |
420 | char b[4096]; | |
421 | ||
0fa83caa RK |
422 | if(!debugging) |
423 | return; | |
768d7355 RK |
424 | dynstr_init(d); |
425 | for(mode = 0; mode < ev_nmodes; ++mode) { | |
0fa83caa | 426 | D(("mode %s maxfd %d", modenames[mode], ev->mode[mode].maxfd)); |
768d7355 RK |
427 | for(n = 0; n < ev->mode[mode].nfds; ++n) { |
428 | fd = ev->mode[mode].fds[n].fd; | |
0fa83caa RK |
429 | D(("fd %s %d%s%s (%s)", modenames[mode], fd, |
430 | FD_ISSET(fd, &ev->mode[mode].enabled) ? " enabled" : "", | |
431 | FD_ISSET(fd, &ev->mode[mode].tripped) ? " tripped" : "", | |
432 | ev->mode[mode].fds[n].what)); | |
768d7355 RK |
433 | } |
434 | d->nvec = 0; | |
435 | for(fd = 0; fd <= ev->mode[mode].maxfd; ++fd) { | |
436 | if(!FD_ISSET(fd, &ev->mode[mode].enabled)) | |
437 | continue; | |
438 | for(n = 0; n < ev->mode[mode].nfds; ++n) { | |
439 | if(ev->mode[mode].fds[n].fd == fd) | |
440 | break; | |
441 | } | |
442 | if(n < ev->mode[mode].nfds) | |
34a3e246 | 443 | snprintf(b, sizeof b, "%d(%s)", fd, ev->mode[mode].fds[n].what); |
768d7355 | 444 | else |
34a3e246 | 445 | snprintf(b, sizeof b, "%d", fd); |
768d7355 RK |
446 | dynstr_append(d, ' '); |
447 | dynstr_append_string(d, b); | |
448 | } | |
449 | dynstr_terminate(d); | |
0fa83caa | 450 | D(("%s enabled:%s", modenames[mode], d->vec)); |
768d7355 RK |
451 | } |
452 | } | |
453 | ||
460b9539 | 454 | /* timeouts *******************************************************************/ |
455 | ||
768d7355 RK |
456 | /** @brief Register a timeout |
457 | * @param ev Event source | |
3149c1e2 | 458 | * @param handlep Where to store timeout handle, or @c NULL |
768d7355 RK |
459 | * @param when Earliest time to call @p callback, or @c NULL |
460 | * @param callback Function to call at or after @p when | |
461 | * @param u Passed to @p callback | |
462 | * @return 0 on success, non-0 on error | |
463 | * | |
464 | * If @p when is a null pointer then a time of 0 is assumed. The effect is to | |
465 | * call the timeout handler from ev_run() next time around the event loop. | |
466 | * This is used internally to schedule various operations if it is not | |
467 | * convenient to call them from the current place in the call stack, or | |
468 | * externally to ensure that other clients of the event loop get a look in when | |
469 | * performing some lengthy operation. | |
470 | */ | |
460b9539 | 471 | int ev_timeout(ev_source *ev, |
472 | ev_timeout_handle *handlep, | |
473 | const struct timeval *when, | |
474 | ev_timeout_callback *callback, | |
475 | void *u) { | |
476 | struct timeout *t, *p, **pp; | |
477 | ||
478 | D(("registering timeout at %ld.%ld callback %p %p", | |
479 | when ? (long)when->tv_sec : 0, when ? (long)when->tv_usec : 0, | |
480 | (void *)callback, u)); | |
481 | t = xmalloc(sizeof *t); | |
482 | if(when) | |
483 | t->when = *when; | |
484 | t->callback = callback; | |
485 | t->u = u; | |
486 | pp = &ev->timeouts; | |
487 | while((p = *pp) && gt(&t->when, &p->when)) | |
488 | pp = &p->next; | |
489 | t->next = p; | |
490 | *pp = t; | |
491 | if(handlep) | |
492 | *handlep = t; | |
493 | return 0; | |
494 | } | |
495 | ||
768d7355 RK |
496 | /** @brief Cancel a timeout |
497 | * @param ev Event loop | |
cb9a695c | 498 | * @param handle Handle returned from ev_timeout(), or 0 |
768d7355 | 499 | * @return 0 on success, non-0 on error |
cb9a695c RK |
500 | * |
501 | * If @p handle is 0 then this is a no-op. | |
768d7355 | 502 | */ |
460b9539 | 503 | int ev_timeout_cancel(ev_source *ev, |
504 | ev_timeout_handle handle) { | |
505 | struct timeout *t = handle, *p, **pp; | |
506 | ||
cb9a695c RK |
507 | if(!t) |
508 | return 0; | |
460b9539 | 509 | for(pp = &ev->timeouts; (p = *pp) && p != t; pp = &p->next) |
510 | ; | |
511 | if(p) { | |
512 | *pp = p->next; | |
513 | return 0; | |
514 | } else | |
515 | return -1; | |
516 | } | |
517 | ||
518 | /* signals ********************************************************************/ | |
519 | ||
768d7355 RK |
520 | /** @brief Mapping of signals to pipe write ends |
521 | * | |
522 | * The pipes are per-event loop, it's possible in theory for there to be | |
523 | * multiple event loops (e.g. in different threads), although in fact DisOrder | |
524 | * does not do this. | |
525 | */ | |
460b9539 | 526 | static int sigfd[NSIG]; |
527 | ||
768d7355 RK |
528 | /** @brief The signal handler |
529 | * @param s Signal number | |
530 | * | |
531 | * Writes to @c sigfd[s]. | |
532 | */ | |
460b9539 | 533 | static void sighandler(int s) { |
534 | unsigned char sc = s; | |
535 | static const char errmsg[] = "error writing to signal pipe"; | |
536 | ||
537 | /* probably the reader has stopped listening for some reason */ | |
538 | if(write(sigfd[s], &sc, 1) < 0) { | |
539 | write(2, errmsg, sizeof errmsg - 1); | |
540 | abort(); | |
541 | } | |
542 | } | |
543 | ||
768d7355 | 544 | /** @brief Read callback for signals */ |
460b9539 | 545 | static int signal_read(ev_source *ev, |
546 | int attribute((unused)) fd, | |
547 | void attribute((unused)) *u) { | |
548 | unsigned char s; | |
549 | int n; | |
550 | int ret; | |
551 | ||
552 | if((n = read(ev->sigpipe[0], &s, 1)) == 1) | |
553 | if((ret = ev->signals[s].callback(ev, s, ev->signals[s].u))) | |
554 | return ret; | |
555 | assert(n != 0); | |
556 | if(n < 0 && (errno != EINTR && errno != EAGAIN)) { | |
557 | error(errno, "error reading from signal pipe %d", ev->sigpipe[0]); | |
558 | return -1; | |
559 | } | |
560 | return 0; | |
561 | } | |
562 | ||
768d7355 | 563 | /** @brief Close the signal pipe */ |
460b9539 | 564 | static void close_sigpipe(ev_source *ev) { |
565 | int save_errno = errno; | |
566 | ||
567 | xclose(ev->sigpipe[0]); | |
568 | xclose(ev->sigpipe[1]); | |
569 | ev->sigpipe[0] = ev->sigpipe[1] = -1; | |
570 | errno = save_errno; | |
571 | } | |
572 | ||
768d7355 RK |
573 | /** @brief Register a signal handler |
574 | * @param ev Event loop | |
575 | * @param sig Signal to handle | |
576 | * @param callback Called when signal is delivered | |
577 | * @param u Passed to @p callback | |
578 | * @return 0 on success, non-0 on error | |
579 | * | |
580 | * Note that @p callback is called from inside ev_run(), not from inside the | |
581 | * signal handler, so the usual restrictions on signal handlers do not apply. | |
582 | */ | |
460b9539 | 583 | int ev_signal(ev_source *ev, |
584 | int sig, | |
585 | ev_signal_callback *callback, | |
586 | void *u) { | |
587 | int n; | |
588 | struct sigaction sa; | |
589 | ||
590 | D(("registering signal %d handler callback %p %p", sig, (void *)callback, u)); | |
591 | assert(sig > 0); | |
592 | assert(sig < NSIG); | |
593 | assert(sig <= UCHAR_MAX); | |
594 | if(ev->sigpipe[0] == -1) { | |
595 | D(("creating signal pipe")); | |
596 | xpipe(ev->sigpipe); | |
597 | D(("signal pipe is %d, %d", ev->sigpipe[0], ev->sigpipe[1])); | |
598 | for(n = 0; n < 2; ++n) { | |
599 | nonblock(ev->sigpipe[n]); | |
600 | cloexec(ev->sigpipe[n]); | |
601 | } | |
e8c92ba7 | 602 | if(ev_fd(ev, ev_read, ev->sigpipe[0], signal_read, 0, "sigpipe read")) { |
460b9539 | 603 | close_sigpipe(ev); |
604 | return -1; | |
605 | } | |
606 | } | |
607 | sigaddset(&ev->sigmask, sig); | |
608 | xsigprocmask(SIG_BLOCK, &ev->sigmask, 0); | |
609 | sigfd[sig] = ev->sigpipe[1]; | |
610 | ev->signals[sig].callback = callback; | |
611 | ev->signals[sig].u = u; | |
612 | sa.sa_handler = sighandler; | |
613 | sigfillset(&sa.sa_mask); | |
614 | sa.sa_flags = SA_RESTART; | |
615 | xsigaction(sig, &sa, &ev->signals[sig].oldsa); | |
616 | ev->escape = 1; | |
617 | return 0; | |
618 | } | |
619 | ||
768d7355 RK |
620 | /** @brief Cancel a signal handler |
621 | * @param ev Event loop | |
622 | * @param sig Signal to cancel | |
623 | * @return 0 on success, non-0 on error | |
624 | */ | |
460b9539 | 625 | int ev_signal_cancel(ev_source *ev, |
626 | int sig) { | |
627 | sigset_t ss; | |
628 | ||
629 | xsigaction(sig, &ev->signals[sig].oldsa, 0); | |
630 | ev->signals[sig].callback = 0; | |
631 | ev->escape = 1; | |
632 | sigdelset(&ev->sigmask, sig); | |
633 | sigemptyset(&ss); | |
634 | sigaddset(&ss, sig); | |
635 | xsigprocmask(SIG_UNBLOCK, &ss, 0); | |
636 | return 0; | |
637 | } | |
638 | ||
768d7355 RK |
639 | /** @brief Clean up signal handling |
640 | * @param ev Event loop | |
641 | * | |
642 | * This function can be called from inside a fork. It restores signal | |
643 | * handlers, unblocks the signals, and closes the signal pipe for @p ev. | |
644 | */ | |
460b9539 | 645 | void ev_signal_atfork(ev_source *ev) { |
646 | int sig; | |
647 | ||
648 | if(ev->sigpipe[0] != -1) { | |
649 | /* revert any handled signals to their original state */ | |
650 | for(sig = 1; sig < NSIG; ++sig) { | |
651 | if(ev->signals[sig].callback != 0) | |
652 | xsigaction(sig, &ev->signals[sig].oldsa, 0); | |
653 | } | |
654 | /* and then unblock them */ | |
655 | xsigprocmask(SIG_UNBLOCK, &ev->sigmask, 0); | |
656 | /* don't want a copy of the signal pipe open inside the fork */ | |
657 | xclose(ev->sigpipe[0]); | |
658 | xclose(ev->sigpipe[1]); | |
659 | } | |
660 | } | |
661 | ||
662 | /* child processes ************************************************************/ | |
663 | ||
768d7355 | 664 | /** @brief Called on SIGCHLD */ |
460b9539 | 665 | static int sigchld_callback(ev_source *ev, |
666 | int attribute((unused)) sig, | |
667 | void attribute((unused)) *u) { | |
668 | struct rusage ru; | |
669 | pid_t r; | |
670 | int status, n, ret, revisit; | |
671 | ||
672 | do { | |
673 | revisit = 0; | |
674 | for(n = 0; n < ev->nchildren; ++n) { | |
675 | r = wait4(ev->children[n].pid, | |
676 | &status, | |
677 | ev->children[n].options | WNOHANG, | |
678 | &ru); | |
679 | if(r > 0) { | |
680 | ev_child_callback *c = ev->children[n].callback; | |
681 | void *cu = ev->children[n].u; | |
682 | ||
683 | if(WIFEXITED(status) || WIFSIGNALED(status)) | |
684 | ev_child_cancel(ev, r); | |
685 | revisit = 1; | |
686 | if((ret = c(ev, r, status, &ru, cu))) | |
687 | return ret; | |
688 | } else if(r < 0) { | |
689 | /* We should "never" get an ECHILD but it can in fact happen. For | |
690 | * instance on Linux 2.4.31, and probably other versions, if someone | |
691 | * straces a child process and then a different child process | |
692 | * terminates, when we wait4() the trace process we will get ECHILD | |
693 | * because it has been reparented to strace. Obviously this is a | |
694 | * hopeless design flaw in the tracing infrastructure, but we don't | |
695 | * want the disorder server to bomb out because of it. So we just log | |
696 | * the problem and ignore it. | |
697 | */ | |
698 | error(errno, "error calling wait4 for PID %lu (broken ptrace?)", | |
699 | (unsigned long)ev->children[n].pid); | |
700 | if(errno != ECHILD) | |
701 | return -1; | |
702 | } | |
703 | } | |
704 | } while(revisit); | |
705 | return 0; | |
706 | } | |
707 | ||
768d7355 RK |
708 | /** @brief Configure event loop for child process handling |
709 | * @return 0 on success, non-0 on error | |
710 | * | |
711 | * Currently at most one event loop can handle child processes and it must be | |
712 | * distinguished from others by calling this function on it. This could be | |
713 | * fixed but since no process ever makes use of more than one event loop there | |
714 | * is no need. | |
715 | */ | |
460b9539 | 716 | int ev_child_setup(ev_source *ev) { |
717 | D(("installing SIGCHLD handler")); | |
718 | return ev_signal(ev, SIGCHLD, sigchld_callback, 0); | |
719 | } | |
720 | ||
768d7355 RK |
721 | /** @brief Wait for a child process to terminate |
722 | * @param ev Event loop | |
723 | * @param pid Process ID of child | |
724 | * @param options Options to pass to @c wait4() | |
725 | * @param callback Called when child terminates (or possibly when it stops) | |
726 | * @param u Passed to @p callback | |
727 | * @return 0 on success, non-0 on error | |
728 | * | |
729 | * You must have called ev_child_setup() on @p ev once first. | |
730 | */ | |
460b9539 | 731 | int ev_child(ev_source *ev, |
732 | pid_t pid, | |
733 | int options, | |
734 | ev_child_callback *callback, | |
735 | void *u) { | |
736 | int n; | |
737 | ||
738 | D(("registering child handling %ld options %d callback %p %p", | |
739 | (long)pid, options, (void *)callback, u)); | |
740 | assert(ev->signals[SIGCHLD].callback == sigchld_callback); | |
741 | if(ev->nchildren >= ev->nchildslots) { | |
742 | ev->nchildslots = ev->nchildslots ? 2 * ev->nchildslots : 16; | |
743 | ev->children = xrealloc(ev->children, | |
744 | ev->nchildslots * sizeof (struct child)); | |
745 | } | |
746 | n = ev->nchildren++; | |
747 | ev->children[n].pid = pid; | |
748 | ev->children[n].options = options; | |
749 | ev->children[n].callback = callback; | |
750 | ev->children[n].u = u; | |
751 | return 0; | |
752 | } | |
753 | ||
768d7355 RK |
754 | /** @brief Stop waiting for a child process |
755 | * @param ev Event loop | |
756 | * @param pid Child process ID | |
757 | * @return 0 on success, non-0 on error | |
758 | */ | |
460b9539 | 759 | int ev_child_cancel(ev_source *ev, |
760 | pid_t pid) { | |
761 | int n; | |
762 | ||
763 | for(n = 0; n < ev->nchildren && ev->children[n].pid != pid; ++n) | |
764 | ; | |
765 | assert(n < ev->nchildren); | |
766 | if(n != ev->nchildren - 1) | |
767 | ev->children[n] = ev->children[ev->nchildren - 1]; | |
768 | --ev->nchildren; | |
769 | return 0; | |
770 | } | |
771 | ||
772 | /* socket listeners ***********************************************************/ | |
773 | ||
768d7355 | 774 | /** @brief State for a socket listener */ |
460b9539 | 775 | struct listen_state { |
776 | ev_listen_callback *callback; | |
777 | void *u; | |
778 | }; | |
779 | ||
768d7355 | 780 | /** @brief Called when a listenign socket is readable */ |
460b9539 | 781 | static int listen_callback(ev_source *ev, int fd, void *u) { |
782 | const struct listen_state *l = u; | |
783 | int newfd; | |
784 | union { | |
785 | struct sockaddr_in in; | |
786 | #if HAVE_STRUCT_SOCKADDR_IN6 | |
787 | struct sockaddr_in6 in6; | |
788 | #endif | |
789 | struct sockaddr_un un; | |
790 | struct sockaddr sa; | |
791 | } addr; | |
792 | socklen_t addrlen; | |
793 | int ret; | |
794 | ||
795 | D(("callback for listener fd %d", fd)); | |
796 | while((addrlen = sizeof addr), | |
797 | (newfd = accept(fd, &addr.sa, &addrlen)) >= 0) { | |
798 | if((ret = l->callback(ev, newfd, &addr.sa, addrlen, l->u))) | |
799 | return ret; | |
800 | } | |
801 | switch(errno) { | |
802 | case EINTR: | |
803 | case EAGAIN: | |
804 | break; | |
805 | #ifdef ECONNABORTED | |
806 | case ECONNABORTED: | |
807 | error(errno, "error calling accept"); | |
808 | break; | |
809 | #endif | |
810 | #ifdef EPROTO | |
811 | case EPROTO: | |
812 | /* XXX on some systems EPROTO should be fatal, but we don't know if | |
813 | * we're running on one of them */ | |
814 | error(errno, "error calling accept"); | |
815 | break; | |
816 | #endif | |
817 | default: | |
818 | fatal(errno, "error calling accept"); | |
819 | break; | |
820 | } | |
821 | if(errno != EINTR && errno != EAGAIN) | |
822 | error(errno, "error calling accept"); | |
823 | return 0; | |
824 | } | |
825 | ||
768d7355 RK |
826 | /** @brief Listen on a socket for inbound stream connections |
827 | * @param ev Event source | |
828 | * @param fd File descriptor of socket | |
829 | * @param callback Called when a new connection arrives | |
830 | * @param u Passed to @p callback | |
831 | * @param what Text description of socket | |
832 | * @return 0 on success, non-0 on error | |
833 | */ | |
460b9539 | 834 | int ev_listen(ev_source *ev, |
835 | int fd, | |
836 | ev_listen_callback *callback, | |
e8c92ba7 RK |
837 | void *u, |
838 | const char *what) { | |
460b9539 | 839 | struct listen_state *l = xmalloc(sizeof *l); |
840 | ||
841 | D(("registering listener fd %d callback %p %p", fd, (void *)callback, u)); | |
842 | l->callback = callback; | |
843 | l->u = u; | |
e8c92ba7 | 844 | return ev_fd(ev, ev_read, fd, listen_callback, l, what); |
460b9539 | 845 | } |
846 | ||
768d7355 RK |
847 | /** @brief Stop listening on a socket |
848 | * @param ev Event loop | |
849 | * @param fd File descriptor of socket | |
850 | * @return 0 on success, non-0 on error | |
851 | */ | |
460b9539 | 852 | int ev_listen_cancel(ev_source *ev, int fd) { |
853 | D(("cancelling listener fd %d", fd)); | |
854 | return ev_fd_cancel(ev, ev_read, fd); | |
855 | } | |
856 | ||
857 | /* buffer *********************************************************************/ | |
858 | ||
768d7355 | 859 | /** @brief Buffer structure */ |
460b9539 | 860 | struct buffer { |
861 | char *base, *start, *end, *top; | |
862 | }; | |
863 | ||
768d7355 | 864 | /* @brief Make sure there is @p bytes available at @c b->end */ |
460b9539 | 865 | static void buffer_space(struct buffer *b, size_t bytes) { |
866 | D(("buffer_space %p %p %p %p want %lu", | |
867 | (void *)b->base, (void *)b->start, (void *)b->end, (void *)b->top, | |
868 | (unsigned long)bytes)); | |
869 | if(b->start == b->end) | |
870 | b->start = b->end = b->base; | |
871 | if((size_t)(b->top - b->end) < bytes) { | |
872 | if((size_t)((b->top - b->end) + (b->start - b->base)) < bytes) { | |
873 | size_t newspace = b->end - b->start + bytes, n; | |
874 | char *newbase; | |
875 | ||
876 | for(n = 16; n < newspace; n *= 2) | |
877 | ; | |
878 | newbase = xmalloc_noptr(n); | |
879 | memcpy(newbase, b->start, b->end - b->start); | |
880 | b->base = newbase; | |
881 | b->end = newbase + (b->end - b->start); | |
882 | b->top = newbase + n; | |
883 | b->start = newbase; /* must be last */ | |
884 | } else { | |
885 | memmove(b->base, b->start, b->end - b->start); | |
886 | b->end = b->base + (b->end - b->start); | |
887 | b->start = b->base; | |
888 | } | |
889 | } | |
890 | D(("result %p %p %p %p", | |
891 | (void *)b->base, (void *)b->start, (void *)b->end, (void *)b->top)); | |
892 | } | |
893 | ||
75d64210 | 894 | /* readers and writers *******************************************************/ |
460b9539 | 895 | |
768d7355 | 896 | /** @brief State structure for a buffered writer */ |
460b9539 | 897 | struct ev_writer { |
75d64210 | 898 | /** @brief Sink used for writing to the buffer */ |
460b9539 | 899 | struct sink s; |
75d64210 RK |
900 | |
901 | /** @brief Output buffer */ | |
460b9539 | 902 | struct buffer b; |
75d64210 RK |
903 | |
904 | /** @brief File descriptor to write to */ | |
460b9539 | 905 | int fd; |
75d64210 RK |
906 | |
907 | /** @brief Set if there'll be no more output */ | |
460b9539 | 908 | int eof; |
75d64210 RK |
909 | |
910 | /** @brief Error/termination callback */ | |
460b9539 | 911 | ev_error_callback *callback; |
75d64210 RK |
912 | |
913 | /** @brief Passed to @p callback */ | |
460b9539 | 914 | void *u; |
75d64210 RK |
915 | |
916 | /** @brief Parent event source */ | |
460b9539 | 917 | ev_source *ev; |
cb9a695c RK |
918 | |
919 | /** @brief Maximum amount of time between succesful writes, 0 = don't care */ | |
920 | int timebound; | |
921 | /** @brief Maximum amount of data to buffer, 0 = don't care */ | |
922 | int spacebound; | |
75d64210 RK |
923 | /** @brief Error code to pass to @p callback (see writer_shutdown()) */ |
924 | int error; | |
cb9a695c RK |
925 | /** @brief Timeout handle for @p timebound (or 0) */ |
926 | ev_timeout_handle timeout; | |
927 | ||
75d64210 | 928 | /** @brief Description of this writer */ |
cb9a695c | 929 | const char *what; |
75d64210 RK |
930 | |
931 | /** @brief Tied reader or 0 */ | |
932 | ev_reader *reader; | |
d742bb47 RK |
933 | |
934 | /** @brief Set when abandoned */ | |
935 | int abandoned; | |
460b9539 | 936 | }; |
937 | ||
75d64210 RK |
938 | /** @brief State structure for a buffered reader */ |
939 | struct ev_reader { | |
940 | /** @brief Input buffer */ | |
941 | struct buffer b; | |
942 | /** @brief File descriptor read from */ | |
943 | int fd; | |
944 | /** @brief Called when new data is available */ | |
945 | ev_reader_callback *callback; | |
946 | /** @brief Called on error and shutdown */ | |
947 | ev_error_callback *error_callback; | |
948 | /** @brief Passed to @p callback and @p error_callback */ | |
949 | void *u; | |
950 | /** @brief Parent event loop */ | |
951 | ev_source *ev; | |
952 | /** @brief Set when EOF is detected */ | |
953 | int eof; | |
954 | /** @brief Error code to pass to error callback */ | |
955 | int error; | |
956 | /** @brief Tied writer or NULL */ | |
957 | ev_writer *writer; | |
958 | }; | |
959 | ||
960 | /* buffered writer ************************************************************/ | |
961 | ||
962 | /** @brief Shut down the writer | |
963 | * | |
964 | * This is called to shut down a writer. The error callback is not called | |
965 | * through any other path. Also we do not cancel @p fd from anywhere else, | |
966 | * though we might disable it. | |
967 | * | |
968 | * It has the signature of a timeout callback so that it can be called from a | |
969 | * time=0 timeout. | |
cb9a695c RK |
970 | * |
971 | * Calls @p callback with @p w->syntherr as the error code (which might be 0). | |
972 | */ | |
973 | static int writer_shutdown(ev_source *ev, | |
974 | const attribute((unused)) struct timeval *now, | |
975 | void *u) { | |
976 | ev_writer *w = u; | |
977 | ||
e4a9c7c5 | 978 | if(w->fd == -1) |
75d64210 | 979 | return 0; /* already shut down */ |
0fa83caa | 980 | D(("writer_shutdown fd=%d error=%d", w->fd, w->error)); |
cb9a695c | 981 | ev_timeout_cancel(ev, w->timeout); |
75d64210 | 982 | ev_fd_cancel(ev, ev_write, w->fd); |
cb9a695c | 983 | w->timeout = 0; |
75d64210 | 984 | if(w->reader) { |
0fa83caa | 985 | D(("found a tied reader")); |
75d64210 RK |
986 | /* If there is a reader still around we just untie it */ |
987 | w->reader->writer = 0; | |
988 | shutdown(w->fd, SHUT_WR); /* there'll be no more writes */ | |
989 | } else { | |
0fa83caa | 990 | D(("no tied reader")); |
75d64210 RK |
991 | /* There's no reader so we are free to close the FD */ |
992 | xclose(w->fd); | |
993 | } | |
e4a9c7c5 | 994 | w->fd = -1; |
75d64210 | 995 | return w->callback(ev, w->error, w->u); |
cb9a695c RK |
996 | } |
997 | ||
998 | /** @brief Called when a writer's @p timebound expires */ | |
999 | static int writer_timebound_exceeded(ev_source *ev, | |
75d64210 | 1000 | const struct timeval *now, |
cb9a695c RK |
1001 | void *u) { |
1002 | ev_writer *const w = u; | |
1003 | ||
d742bb47 RK |
1004 | if(!w->abandoned) { |
1005 | w->abandoned = 1; | |
1006 | error(0, "abandoning writer '%s' because no writes within %ds", | |
1007 | w->what, w->timebound); | |
1008 | w->error = ETIMEDOUT; | |
1009 | } | |
75d64210 | 1010 | return writer_shutdown(ev, now, u); |
cb9a695c RK |
1011 | } |
1012 | ||
1013 | /** @brief Set the time bound callback (if not set already) */ | |
1014 | static void writer_set_timebound(ev_writer *w) { | |
1015 | if(w->timebound && !w->timeout) { | |
1016 | struct timeval when; | |
1017 | ev_source *const ev = w->ev; | |
1018 | ||
1019 | xgettimeofday(&when, 0); | |
1020 | when.tv_sec += w->timebound; | |
1021 | ev_timeout(ev, &w->timeout, &when, writer_timebound_exceeded, w); | |
1022 | } | |
1023 | } | |
1024 | ||
768d7355 | 1025 | /** @brief Called when a writer's file descriptor is writable */ |
460b9539 | 1026 | static int writer_callback(ev_source *ev, int fd, void *u) { |
cb9a695c | 1027 | ev_writer *const w = u; |
460b9539 | 1028 | int n; |
1029 | ||
1030 | n = write(fd, w->b.start, w->b.end - w->b.start); | |
1031 | D(("callback for writer fd %d, %ld bytes, n=%d, errno=%d", | |
1032 | fd, (long)(w->b.end - w->b.start), n, errno)); | |
1033 | if(n >= 0) { | |
75d64210 | 1034 | /* Consume bytes from the buffer */ |
460b9539 | 1035 | w->b.start += n; |
75d64210 | 1036 | /* Suppress any outstanding timeout */ |
cb9a695c RK |
1037 | ev_timeout_cancel(ev, w->timeout); |
1038 | w->timeout = 0; | |
460b9539 | 1039 | if(w->b.start == w->b.end) { |
75d64210 | 1040 | /* The buffer is empty */ |
460b9539 | 1041 | if(w->eof) { |
75d64210 RK |
1042 | /* We're done, we can shut down this writer */ |
1043 | w->error = 0; | |
1044 | return writer_shutdown(ev, 0, w); | |
460b9539 | 1045 | } else |
75d64210 RK |
1046 | /* There might be more to come but we don't need writer_callback() to |
1047 | * be called for the time being */ | |
460b9539 | 1048 | ev_fd_disable(ev, ev_write, fd); |
cb9a695c | 1049 | } else |
75d64210 RK |
1050 | /* The buffer isn't empty, set a timeout so we give up if we don't manage |
1051 | * to write some more within a reasonable time */ | |
cb9a695c | 1052 | writer_set_timebound(w); |
460b9539 | 1053 | } else { |
1054 | switch(errno) { | |
1055 | case EINTR: | |
1056 | case EAGAIN: | |
1057 | break; | |
1058 | default: | |
75d64210 RK |
1059 | w->error = errno; |
1060 | return writer_shutdown(ev, 0, w); | |
460b9539 | 1061 | } |
1062 | } | |
1063 | return 0; | |
1064 | } | |
1065 | ||
768d7355 RK |
1066 | /** @brief Write bytes to a writer's buffer |
1067 | * | |
1068 | * This is the sink write callback. | |
1069 | * | |
1070 | * Calls ev_fd_enable() if necessary (i.e. if the buffer was empty but | |
1071 | * now is not). | |
1072 | */ | |
460b9539 | 1073 | static int ev_writer_write(struct sink *sk, const void *s, int n) { |
1074 | ev_writer *w = (ev_writer *)sk; | |
cb9a695c RK |
1075 | |
1076 | if(!n) | |
1077 | return 0; /* avoid silliness */ | |
38b8221f RK |
1078 | if(w->fd == -1) |
1079 | error(0, "ev_writer_write on %s after shutdown", w->what); | |
75d64210 RK |
1080 | if(w->spacebound && w->b.end - w->b.start + n > w->spacebound) { |
1081 | /* The new buffer contents will exceed the space bound. We assume that the | |
cb9a695c RK |
1082 | * remote client has gone away and TCP hasn't noticed yet, or that it's got |
1083 | * hopelessly stuck. */ | |
d742bb47 RK |
1084 | if(!w->abandoned) { |
1085 | w->abandoned = 1; | |
1086 | error(0, "abandoning writer '%s' because buffer has reached %td bytes", | |
1087 | w->what, w->b.end - w->b.start); | |
1088 | ev_fd_disable(w->ev, ev_write, w->fd); | |
1089 | w->error = EPIPE; | |
1090 | return ev_timeout(w->ev, 0, 0, writer_shutdown, w); | |
1091 | } else | |
1092 | return 0; | |
cb9a695c | 1093 | } |
75d64210 RK |
1094 | /* Make sure there is space */ |
1095 | buffer_space(&w->b, n); | |
1096 | /* If the buffer was formerly empty then we'll need to re-enable the FD */ | |
1097 | if(w->b.start == w->b.end) | |
1098 | ev_fd_enable(w->ev, ev_write, w->fd); | |
1099 | memcpy(w->b.end, s, n); | |
1100 | w->b.end += n; | |
1101 | /* Arrange a timeout if there wasn't one set already */ | |
cb9a695c | 1102 | writer_set_timebound(w); |
460b9539 | 1103 | return 0; |
1104 | } | |
1105 | ||
768d7355 RK |
1106 | /** @brief Create a new buffered writer |
1107 | * @param ev Event loop | |
1108 | * @param fd File descriptor to write to | |
1109 | * @param callback Called if an error occurs and when finished | |
1110 | * @param u Passed to @p callback | |
1111 | * @param what Text description | |
1112 | * @return New writer or @c NULL | |
75d64210 RK |
1113 | * |
1114 | * Writers own their file descriptor and close it when they have finished with | |
1115 | * it. | |
1116 | * | |
1117 | * If you pass the same fd to a reader and writer, you must tie them together | |
1118 | * with ev_tie(). | |
768d7355 | 1119 | */ |
460b9539 | 1120 | ev_writer *ev_writer_new(ev_source *ev, |
1121 | int fd, | |
1122 | ev_error_callback *callback, | |
e8c92ba7 RK |
1123 | void *u, |
1124 | const char *what) { | |
460b9539 | 1125 | ev_writer *w = xmalloc(sizeof *w); |
1126 | ||
1127 | D(("registering writer fd %d callback %p %p", fd, (void *)callback, u)); | |
1128 | w->s.write = ev_writer_write; | |
1129 | w->fd = fd; | |
1130 | w->callback = callback; | |
1131 | w->u = u; | |
1132 | w->ev = ev; | |
cb9a695c RK |
1133 | w->timebound = 10 * 60; |
1134 | w->spacebound = 512 * 1024; | |
1135 | w->what = what; | |
e8c92ba7 | 1136 | if(ev_fd(ev, ev_write, fd, writer_callback, w, what)) |
460b9539 | 1137 | return 0; |
75d64210 | 1138 | /* Buffer is initially empty so we don't want a callback */ |
460b9539 | 1139 | ev_fd_disable(ev, ev_write, fd); |
1140 | return w; | |
1141 | } | |
1142 | ||
cb9a695c RK |
1143 | /** @brief Get/set the time bound |
1144 | * @param w Writer | |
1145 | * @param new_time_bound New bound or -1 for no change | |
1146 | * @return Latest time bound | |
1147 | * | |
1148 | * If @p new_time_bound is negative then the current time bound is returned. | |
1149 | * Otherwise it is set and the new value returned. | |
1150 | * | |
1151 | * The time bound is the number of seconds allowed between writes. If it takes | |
1152 | * longer than this to flush a buffer then the peer will be assumed to be dead | |
1153 | * and an error will be synthesized. 0 means "don't care". The default time | |
1154 | * bound is 10 minutes. | |
1155 | * | |
1156 | * Note that this value does not take into account kernel buffering and | |
1157 | * timeouts. | |
1158 | */ | |
1159 | int ev_writer_time_bound(ev_writer *w, | |
1160 | int new_time_bound) { | |
1161 | if(new_time_bound >= 0) | |
1162 | w->timebound = new_time_bound; | |
1163 | return w->timebound; | |
1164 | } | |
1165 | ||
1166 | /** @brief Get/set the space bound | |
1167 | * @param w Writer | |
1168 | * @param new_space_bound New bound or -1 for no change | |
1169 | * @return Latest space bound | |
1170 | * | |
1171 | * If @p new_space_bound is negative then the current space bound is returned. | |
1172 | * Otherwise it is set and the new value returned. | |
1173 | * | |
1174 | * The space bound is the number of bytes allowed between in the buffer. If | |
1175 | * the buffer exceeds this size an error will be synthesized. 0 means "don't | |
1176 | * care". The default space bound is 512Kbyte. | |
1177 | * | |
1178 | * Note that this value does not take into account kernel buffering. | |
1179 | */ | |
1180 | int ev_writer_space_bound(ev_writer *w, | |
1181 | int new_space_bound) { | |
1182 | if(new_space_bound >= 0) | |
1183 | w->spacebound = new_space_bound; | |
1184 | return w->spacebound; | |
1185 | } | |
1186 | ||
768d7355 RK |
1187 | /** @brief Return the sink associated with a writer |
1188 | * @param w Writer | |
1189 | * @return Pointer to sink | |
1190 | * | |
1191 | * Writing to the sink will arrange for those bytes to be written to the file | |
1192 | * descriptor as and when it is writable. | |
1193 | */ | |
460b9539 | 1194 | struct sink *ev_writer_sink(ev_writer *w) { |
f6033c46 RK |
1195 | if(!w) |
1196 | fatal(0, "ev_write_sink called with null writer"); | |
460b9539 | 1197 | return &w->s; |
1198 | } | |
1199 | ||
768d7355 RK |
1200 | /** @brief Close a writer |
1201 | * @param w Writer to close | |
1202 | * @return 0 on success, non-0 on error | |
1203 | * | |
1204 | * Close a writer. No more bytes should be written to its sink. | |
1205 | * | |
1206 | * When the last byte has been written the callback will be called with an | |
1207 | * error code of 0. It is guaranteed that this will NOT happen before | |
1208 | * ev_writer_close() returns (although the file descriptor for the writer might | |
1209 | * be cancelled by the time it returns). | |
1210 | */ | |
460b9539 | 1211 | int ev_writer_close(ev_writer *w) { |
1212 | D(("close writer fd %d", w->fd)); | |
75d64210 RK |
1213 | if(w->eof) |
1214 | return 0; /* already closed */ | |
460b9539 | 1215 | w->eof = 1; |
1216 | if(w->b.start == w->b.end) { | |
75d64210 RK |
1217 | /* We're already finished */ |
1218 | w->error = 0; /* no error */ | |
460b9539 | 1219 | return ev_timeout(w->ev, 0, 0, writer_shutdown, w); |
1220 | } | |
1221 | return 0; | |
1222 | } | |
1223 | ||
768d7355 RK |
1224 | /** @brief Attempt to flush a writer |
1225 | * @param w Writer to flush | |
1226 | * @return 0 on success, non-0 on error | |
1227 | * | |
1228 | * Does a speculative write of any buffered data. Does not block if it cannot | |
1229 | * be written. | |
1230 | */ | |
460b9539 | 1231 | int ev_writer_flush(ev_writer *w) { |
1232 | return writer_callback(w->ev, w->fd, w); | |
1233 | } | |
1234 | ||
1235 | /* buffered reader ************************************************************/ | |
1236 | ||
75d64210 RK |
1237 | /** @brief Shut down a reader* |
1238 | * | |
1239 | * This is the only path through which we cancel and close the file descriptor. | |
1240 | * As with the writer case it is given timeout signature to allow it be | |
1241 | * deferred to the next iteration of the event loop. | |
1242 | * | |
1243 | * We only call @p error_callback if @p error is nonzero (unlike the writer | |
1244 | * case). | |
1245 | */ | |
1246 | static int reader_shutdown(ev_source *ev, | |
1247 | const attribute((unused)) struct timeval *now, | |
1248 | void *u) { | |
1249 | ev_reader *const r = u; | |
1250 | ||
1251 | if(r->fd == -1) | |
1252 | return 0; /* already shut down */ | |
0fa83caa | 1253 | D(("reader_shutdown fd=%d", r->fd)); |
75d64210 RK |
1254 | ev_fd_cancel(ev, ev_read, r->fd); |
1255 | r->eof = 1; | |
1256 | if(r->writer) { | |
0fa83caa | 1257 | D(("found a tied writer")); |
75d64210 RK |
1258 | /* If there is a writer still around we just untie it */ |
1259 | r->writer->reader = 0; | |
1260 | shutdown(r->fd, SHUT_RD); /* there'll be no more reads */ | |
1261 | } else { | |
0fa83caa | 1262 | D(("no tied writer found")); |
75d64210 RK |
1263 | /* There's no writer so we are free to close the FD */ |
1264 | xclose(r->fd); | |
1265 | } | |
1266 | r->fd = -1; | |
1267 | if(r->error) | |
1268 | return r->error_callback(ev, r->error, r->u); | |
1269 | else | |
1270 | return 0; | |
1271 | } | |
460b9539 | 1272 | |
768d7355 | 1273 | /** @brief Called when a reader's @p fd is readable */ |
460b9539 | 1274 | static int reader_callback(ev_source *ev, int fd, void *u) { |
1275 | ev_reader *r = u; | |
1276 | int n; | |
1277 | ||
1278 | buffer_space(&r->b, 1); | |
1279 | n = read(fd, r->b.end, r->b.top - r->b.end); | |
1280 | D(("read fd %d buffer %d returned %d errno %d", | |
1281 | fd, (int)(r->b.top - r->b.end), n, errno)); | |
1282 | if(n > 0) { | |
1283 | r->b.end += n; | |
75d64210 | 1284 | return r->callback(ev, r, r->b.start, r->b.end - r->b.start, 0, r->u); |
460b9539 | 1285 | } else if(n == 0) { |
75d64210 RK |
1286 | /* No more read callbacks needed */ |
1287 | ev_fd_disable(r->ev, ev_read, r->fd); | |
1288 | ev_timeout(r->ev, 0, 0, reader_shutdown, r); | |
1289 | /* Pass the remaining data and an eof indicator to the user */ | |
1290 | return r->callback(ev, r, r->b.start, r->b.end - r->b.start, 1, r->u); | |
460b9539 | 1291 | } else { |
1292 | switch(errno) { | |
1293 | case EINTR: | |
1294 | case EAGAIN: | |
1295 | break; | |
1296 | default: | |
75d64210 RK |
1297 | /* Fatal error, kill the reader now */ |
1298 | r->error = errno; | |
1299 | return reader_shutdown(ev, 0, r); | |
460b9539 | 1300 | } |
1301 | } | |
1302 | return 0; | |
1303 | } | |
1304 | ||
768d7355 RK |
1305 | /** @brief Create a new buffered reader |
1306 | * @param ev Event loop | |
1307 | * @param fd File descriptor to read from | |
1308 | * @param callback Called when new data is available | |
1309 | * @param error_callback Called if an error occurs | |
1310 | * @param u Passed to callbacks | |
1311 | * @param what Text description | |
1312 | * @return New reader or @c NULL | |
75d64210 RK |
1313 | * |
1314 | * Readers own their fd and close it when they are finished with it. | |
1315 | * | |
1316 | * If you pass the same fd to a reader and writer, you must tie them together | |
1317 | * with ev_tie(). | |
768d7355 | 1318 | */ |
460b9539 | 1319 | ev_reader *ev_reader_new(ev_source *ev, |
1320 | int fd, | |
1321 | ev_reader_callback *callback, | |
1322 | ev_error_callback *error_callback, | |
e8c92ba7 RK |
1323 | void *u, |
1324 | const char *what) { | |
460b9539 | 1325 | ev_reader *r = xmalloc(sizeof *r); |
1326 | ||
1327 | D(("registering reader fd %d callback %p %p %p", | |
1328 | fd, (void *)callback, (void *)error_callback, u)); | |
1329 | r->fd = fd; | |
1330 | r->callback = callback; | |
1331 | r->error_callback = error_callback; | |
1332 | r->u = u; | |
1333 | r->ev = ev; | |
e8c92ba7 | 1334 | if(ev_fd(ev, ev_read, fd, reader_callback, r, what)) |
460b9539 | 1335 | return 0; |
1336 | return r; | |
1337 | } | |
1338 | ||
1339 | void ev_reader_buffer(ev_reader *r, size_t nbytes) { | |
1340 | buffer_space(&r->b, nbytes - (r->b.end - r->b.start)); | |
1341 | } | |
1342 | ||
768d7355 RK |
1343 | /** @brief Consume @p n bytes from the reader's buffer |
1344 | * @param r Reader | |
1345 | * @param n Number of bytes to consume | |
1346 | * | |
1347 | * Tells the reader than the next @p n bytes have been dealt with and can now | |
1348 | * be discarded. | |
1349 | */ | |
460b9539 | 1350 | void ev_reader_consume(ev_reader *r, size_t n) { |
1351 | r->b.start += n; | |
1352 | } | |
1353 | ||
768d7355 RK |
1354 | /** @brief Cancel a reader |
1355 | * @param r Reader | |
1356 | * @return 0 on success, non-0 on error | |
75d64210 RK |
1357 | * |
1358 | * No further callbacks will be made, and the FD will be closed (in a later | |
1359 | * iteration of the event loop). | |
768d7355 | 1360 | */ |
460b9539 | 1361 | int ev_reader_cancel(ev_reader *r) { |
1362 | D(("cancel reader fd %d", r->fd)); | |
75d64210 RK |
1363 | if(r->fd == -1) |
1364 | return 0; /* already thoroughly cancelled */ | |
1365 | ev_fd_disable(r->ev, ev_read, r->fd); | |
1366 | return ev_timeout(r->ev, 0, 0, reader_shutdown, r); | |
460b9539 | 1367 | } |
1368 | ||
768d7355 RK |
1369 | /** @brief Temporarily disable a reader |
1370 | * @param r Reader | |
1371 | * @return 0 on success, non-0 on error | |
1372 | * | |
1373 | * No further callbacks for this reader will be made. Re-enable with | |
1374 | * ev_reader_enable(). | |
1375 | */ | |
460b9539 | 1376 | int ev_reader_disable(ev_reader *r) { |
1377 | D(("disable reader fd %d", r->fd)); | |
75d64210 | 1378 | return ev_fd_disable(r->ev, ev_read, r->fd); |
460b9539 | 1379 | } |
1380 | ||
768d7355 | 1381 | /** @brief Called from ev_run() for ev_reader_incomplete() */ |
460b9539 | 1382 | static int reader_continuation(ev_source attribute((unused)) *ev, |
1383 | const attribute((unused)) struct timeval *now, | |
1384 | void *u) { | |
1385 | ev_reader *r = u; | |
1386 | ||
1387 | D(("reader continuation callback fd %d", r->fd)); | |
75d64210 RK |
1388 | /* If not at EOF turn the FD back on */ |
1389 | if(!r->eof) | |
1390 | if(ev_fd_enable(r->ev, ev_read, r->fd)) | |
1391 | return -1; | |
1392 | /* We're already in a timeout callback so there's no reason we can't call the | |
1393 | * user callback directly (compare ev_reader_enable()). */ | |
1394 | return r->callback(ev, r, r->b.start, r->b.end - r->b.start, r->eof, r->u); | |
460b9539 | 1395 | } |
1396 | ||
768d7355 RK |
1397 | /** @brief Arrange another callback |
1398 | * @param r reader | |
1399 | * @return 0 on success, non-0 on error | |
1400 | * | |
1401 | * Indicates that the reader can process more input but would like to yield to | |
1402 | * other clients of the event loop. Input will be disabled but it will be | |
1403 | * re-enabled on the next iteration of the event loop and the read callback | |
1404 | * will be called again (even if no further bytes are available). | |
1405 | */ | |
460b9539 | 1406 | int ev_reader_incomplete(ev_reader *r) { |
1407 | if(ev_fd_disable(r->ev, ev_read, r->fd)) return -1; | |
1408 | return ev_timeout(r->ev, 0, 0, reader_continuation, r); | |
1409 | } | |
1410 | ||
1411 | static int reader_enabled(ev_source *ev, | |
1412 | const attribute((unused)) struct timeval *now, | |
1413 | void *u) { | |
1414 | ev_reader *r = u; | |
1415 | ||
1416 | D(("reader enabled callback fd %d", r->fd)); | |
75d64210 | 1417 | return r->callback(ev, r, r->b.start, r->b.end - r->b.start, r->eof, r->u); |
460b9539 | 1418 | } |
1419 | ||
768d7355 RK |
1420 | /** @brief Re-enable reading |
1421 | * @param r reader | |
1422 | * @return 0 on success, non-0 on error | |
1423 | * | |
1424 | * If there is unconsumed data then you get a callback next time round the | |
1425 | * event loop even if nothing new has been read. | |
1426 | * | |
1427 | * The idea is in your read callback you come across a line (or whatever) that | |
1428 | * can't be processed immediately. So you set up processing and disable | |
1429 | * reading with ev_reader_disable(). Later when you finish processing you | |
1430 | * re-enable. You'll automatically get another callback directly from the | |
1431 | * event loop (i.e. not from inside ev_reader_enable()) so you can handle the | |
1432 | * next line (or whatever) if the whole thing has in fact already arrived. | |
75d64210 RK |
1433 | * |
1434 | * The difference between this process and calling ev_reader_incomplete() is | |
1435 | * ev_reader_incomplete() deals with the case where you can process now but | |
1436 | * would rather yield to other clients of the event loop, while using | |
1437 | * ev_reader_disable() and ev_reader_enable() deals with the case where you | |
1438 | * cannot process input yet because some other process is actually not | |
1439 | * complete. | |
768d7355 | 1440 | */ |
460b9539 | 1441 | int ev_reader_enable(ev_reader *r) { |
1442 | D(("enable reader fd %d", r->fd)); | |
75d64210 RK |
1443 | |
1444 | /* First if we're not at EOF then we re-enable reading */ | |
1445 | if(!r->eof) | |
1446 | if(ev_fd_enable(r->ev, ev_read, r->fd)) | |
1447 | return -1; | |
1448 | /* Arrange another callback next time round the event loop */ | |
1449 | return ev_timeout(r->ev, 0, 0, reader_enabled, r); | |
1450 | } | |
1451 | ||
1452 | /** @brief Tie a reader and a writer together | |
1453 | * @param r Reader | |
1454 | * @param w Writer | |
1455 | * @return 0 on success, non-0 on error | |
1456 | * | |
1457 | * This function must be called if @p r and @p w share a file descritptor. | |
1458 | */ | |
1459 | int ev_tie(ev_reader *r, ev_writer *w) { | |
1460 | assert(r->writer == 0); | |
1461 | assert(w->reader == 0); | |
1462 | r->writer = w; | |
1463 | w->reader = r; | |
1464 | return 0; | |
460b9539 | 1465 | } |
1466 | ||
1467 | /* | |
1468 | Local Variables: | |
1469 | c-basic-offset:2 | |
1470 | comment-column:40 | |
1471 | fill-column:79 | |
1472 | End: | |
1473 | */ |