chiark / gitweb /
further debugging info() calls; marked TODO to remove when bug fixed
[disorder] / lib / event.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
e8c92ba7 3 * Copyright (C) 2004, 2005, 2007 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 52struct 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 61struct 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 69struct 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 90struct signal {
91 struct sigaction oldsa;
92 ev_signal_callback *callback;
93 void *u;
94};
95
768d7355 96/** @brief A child process */
460b9539 97struct 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 105struct 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 145static 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 153static 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 *
164 * Ought to be in @file lib/timeval.h
165 */
460b9539 166static 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 173ev_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 190int 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 314int 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 354int 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 390int ev_fd_enable(ev_source *ev, ev_fdmode mode, int fd) {
391 D(("enabling mode %s fd %d", modenames[mode], fd));
392 FD_SET(fd, &ev->mode[mode].enabled);
393 return 0;
394}
395
768d7355
RK
396/** @brief Temporarily disable a file descriptor
397 * @param ev Event loop
398 * @param mode @c ev_read or @c ev_write
399 * @param fd File descriptor
400 * @return 0 on success, non-0 on error
401 *
402 * Re-enable with ev_fd_enable(). It is harmless if @p fd is already disabled,
403 * but it must not have been cancelled.
404 */
460b9539 405int ev_fd_disable(ev_source *ev, ev_fdmode mode, int fd) {
406 D(("disabling mode %s fd %d", modenames[mode], fd));
407 FD_CLR(fd, &ev->mode[mode].enabled);
408 FD_CLR(fd, &ev->mode[mode].tripped);
409 return 0;
410}
411
768d7355
RK
412/** @brief Log a report of file descriptor state */
413void ev_report(ev_source *ev) {
414 int n, fd;
415 ev_fdmode mode;
416 struct dynstr d[1];
417 char b[4096];
418
419 dynstr_init(d);
420 for(mode = 0; mode < ev_nmodes; ++mode) {
421 info("mode %s maxfd %d", modenames[mode], ev->mode[mode].maxfd);
422 for(n = 0; n < ev->mode[mode].nfds; ++n) {
423 fd = ev->mode[mode].fds[n].fd;
424 info("fd %s %d%s%s (%s)", modenames[mode], fd,
425 FD_ISSET(fd, &ev->mode[mode].enabled) ? " enabled" : "",
426 FD_ISSET(fd, &ev->mode[mode].tripped) ? " tripped" : "",
427 ev->mode[mode].fds[n].what);
428 }
429 d->nvec = 0;
430 for(fd = 0; fd <= ev->mode[mode].maxfd; ++fd) {
431 if(!FD_ISSET(fd, &ev->mode[mode].enabled))
432 continue;
433 for(n = 0; n < ev->mode[mode].nfds; ++n) {
434 if(ev->mode[mode].fds[n].fd == fd)
435 break;
436 }
437 if(n < ev->mode[mode].nfds)
34a3e246 438 snprintf(b, sizeof b, "%d(%s)", fd, ev->mode[mode].fds[n].what);
768d7355 439 else
34a3e246 440 snprintf(b, sizeof b, "%d", fd);
768d7355
RK
441 dynstr_append(d, ' ');
442 dynstr_append_string(d, b);
443 }
444 dynstr_terminate(d);
445 info("%s enabled:%s", modenames[mode], d->vec);
446 }
447}
448
460b9539 449/* timeouts *******************************************************************/
450
768d7355
RK
451/** @brief Register a timeout
452 * @param ev Event source
453 * @param handle Where to store timeout handle, or @c NULL
454 * @param when Earliest time to call @p callback, or @c NULL
455 * @param callback Function to call at or after @p when
456 * @param u Passed to @p callback
457 * @return 0 on success, non-0 on error
458 *
459 * If @p when is a null pointer then a time of 0 is assumed. The effect is to
460 * call the timeout handler from ev_run() next time around the event loop.
461 * This is used internally to schedule various operations if it is not
462 * convenient to call them from the current place in the call stack, or
463 * externally to ensure that other clients of the event loop get a look in when
464 * performing some lengthy operation.
465 */
460b9539 466int ev_timeout(ev_source *ev,
467 ev_timeout_handle *handlep,
468 const struct timeval *when,
469 ev_timeout_callback *callback,
470 void *u) {
471 struct timeout *t, *p, **pp;
472
473 D(("registering timeout at %ld.%ld callback %p %p",
474 when ? (long)when->tv_sec : 0, when ? (long)when->tv_usec : 0,
475 (void *)callback, u));
476 t = xmalloc(sizeof *t);
477 if(when)
478 t->when = *when;
479 t->callback = callback;
480 t->u = u;
481 pp = &ev->timeouts;
482 while((p = *pp) && gt(&t->when, &p->when))
483 pp = &p->next;
484 t->next = p;
485 *pp = t;
486 if(handlep)
487 *handlep = t;
488 return 0;
489}
490
768d7355
RK
491/** @brief Cancel a timeout
492 * @param ev Event loop
493 * @param handle Handle returned from ev_timeout()
494 * @return 0 on success, non-0 on error
495 */
460b9539 496int ev_timeout_cancel(ev_source *ev,
497 ev_timeout_handle handle) {
498 struct timeout *t = handle, *p, **pp;
499
500 for(pp = &ev->timeouts; (p = *pp) && p != t; pp = &p->next)
501 ;
502 if(p) {
503 *pp = p->next;
504 return 0;
505 } else
506 return -1;
507}
508
509/* signals ********************************************************************/
510
768d7355
RK
511/** @brief Mapping of signals to pipe write ends
512 *
513 * The pipes are per-event loop, it's possible in theory for there to be
514 * multiple event loops (e.g. in different threads), although in fact DisOrder
515 * does not do this.
516 */
460b9539 517static int sigfd[NSIG];
518
768d7355
RK
519/** @brief The signal handler
520 * @param s Signal number
521 *
522 * Writes to @c sigfd[s].
523 */
460b9539 524static void sighandler(int s) {
525 unsigned char sc = s;
526 static const char errmsg[] = "error writing to signal pipe";
527
528 /* probably the reader has stopped listening for some reason */
529 if(write(sigfd[s], &sc, 1) < 0) {
530 write(2, errmsg, sizeof errmsg - 1);
531 abort();
532 }
533}
534
768d7355 535/** @brief Read callback for signals */
460b9539 536static int signal_read(ev_source *ev,
537 int attribute((unused)) fd,
538 void attribute((unused)) *u) {
539 unsigned char s;
540 int n;
541 int ret;
542
543 if((n = read(ev->sigpipe[0], &s, 1)) == 1)
544 if((ret = ev->signals[s].callback(ev, s, ev->signals[s].u)))
545 return ret;
546 assert(n != 0);
547 if(n < 0 && (errno != EINTR && errno != EAGAIN)) {
548 error(errno, "error reading from signal pipe %d", ev->sigpipe[0]);
549 return -1;
550 }
551 return 0;
552}
553
768d7355 554/** @brief Close the signal pipe */
460b9539 555static void close_sigpipe(ev_source *ev) {
556 int save_errno = errno;
557
558 xclose(ev->sigpipe[0]);
559 xclose(ev->sigpipe[1]);
560 ev->sigpipe[0] = ev->sigpipe[1] = -1;
561 errno = save_errno;
562}
563
768d7355
RK
564/** @brief Register a signal handler
565 * @param ev Event loop
566 * @param sig Signal to handle
567 * @param callback Called when signal is delivered
568 * @param u Passed to @p callback
569 * @return 0 on success, non-0 on error
570 *
571 * Note that @p callback is called from inside ev_run(), not from inside the
572 * signal handler, so the usual restrictions on signal handlers do not apply.
573 */
460b9539 574int ev_signal(ev_source *ev,
575 int sig,
576 ev_signal_callback *callback,
577 void *u) {
578 int n;
579 struct sigaction sa;
580
581 D(("registering signal %d handler callback %p %p", sig, (void *)callback, u));
582 assert(sig > 0);
583 assert(sig < NSIG);
584 assert(sig <= UCHAR_MAX);
585 if(ev->sigpipe[0] == -1) {
586 D(("creating signal pipe"));
587 xpipe(ev->sigpipe);
588 D(("signal pipe is %d, %d", ev->sigpipe[0], ev->sigpipe[1]));
589 for(n = 0; n < 2; ++n) {
590 nonblock(ev->sigpipe[n]);
591 cloexec(ev->sigpipe[n]);
592 }
e8c92ba7 593 if(ev_fd(ev, ev_read, ev->sigpipe[0], signal_read, 0, "sigpipe read")) {
460b9539 594 close_sigpipe(ev);
595 return -1;
596 }
597 }
598 sigaddset(&ev->sigmask, sig);
599 xsigprocmask(SIG_BLOCK, &ev->sigmask, 0);
600 sigfd[sig] = ev->sigpipe[1];
601 ev->signals[sig].callback = callback;
602 ev->signals[sig].u = u;
603 sa.sa_handler = sighandler;
604 sigfillset(&sa.sa_mask);
605 sa.sa_flags = SA_RESTART;
606 xsigaction(sig, &sa, &ev->signals[sig].oldsa);
607 ev->escape = 1;
608 return 0;
609}
610
768d7355
RK
611/** @brief Cancel a signal handler
612 * @param ev Event loop
613 * @param sig Signal to cancel
614 * @return 0 on success, non-0 on error
615 */
460b9539 616int ev_signal_cancel(ev_source *ev,
617 int sig) {
618 sigset_t ss;
619
620 xsigaction(sig, &ev->signals[sig].oldsa, 0);
621 ev->signals[sig].callback = 0;
622 ev->escape = 1;
623 sigdelset(&ev->sigmask, sig);
624 sigemptyset(&ss);
625 sigaddset(&ss, sig);
626 xsigprocmask(SIG_UNBLOCK, &ss, 0);
627 return 0;
628}
629
768d7355
RK
630/** @brief Clean up signal handling
631 * @param ev Event loop
632 *
633 * This function can be called from inside a fork. It restores signal
634 * handlers, unblocks the signals, and closes the signal pipe for @p ev.
635 */
460b9539 636void ev_signal_atfork(ev_source *ev) {
637 int sig;
638
639 if(ev->sigpipe[0] != -1) {
640 /* revert any handled signals to their original state */
641 for(sig = 1; sig < NSIG; ++sig) {
642 if(ev->signals[sig].callback != 0)
643 xsigaction(sig, &ev->signals[sig].oldsa, 0);
644 }
645 /* and then unblock them */
646 xsigprocmask(SIG_UNBLOCK, &ev->sigmask, 0);
647 /* don't want a copy of the signal pipe open inside the fork */
648 xclose(ev->sigpipe[0]);
649 xclose(ev->sigpipe[1]);
650 }
651}
652
653/* child processes ************************************************************/
654
768d7355 655/** @brief Called on SIGCHLD */
460b9539 656static int sigchld_callback(ev_source *ev,
657 int attribute((unused)) sig,
658 void attribute((unused)) *u) {
659 struct rusage ru;
660 pid_t r;
661 int status, n, ret, revisit;
662
663 do {
664 revisit = 0;
665 for(n = 0; n < ev->nchildren; ++n) {
666 r = wait4(ev->children[n].pid,
667 &status,
668 ev->children[n].options | WNOHANG,
669 &ru);
670 if(r > 0) {
671 ev_child_callback *c = ev->children[n].callback;
672 void *cu = ev->children[n].u;
673
674 if(WIFEXITED(status) || WIFSIGNALED(status))
675 ev_child_cancel(ev, r);
676 revisit = 1;
677 if((ret = c(ev, r, status, &ru, cu)))
678 return ret;
679 } else if(r < 0) {
680 /* We should "never" get an ECHILD but it can in fact happen. For
681 * instance on Linux 2.4.31, and probably other versions, if someone
682 * straces a child process and then a different child process
683 * terminates, when we wait4() the trace process we will get ECHILD
684 * because it has been reparented to strace. Obviously this is a
685 * hopeless design flaw in the tracing infrastructure, but we don't
686 * want the disorder server to bomb out because of it. So we just log
687 * the problem and ignore it.
688 */
689 error(errno, "error calling wait4 for PID %lu (broken ptrace?)",
690 (unsigned long)ev->children[n].pid);
691 if(errno != ECHILD)
692 return -1;
693 }
694 }
695 } while(revisit);
696 return 0;
697}
698
768d7355
RK
699/** @brief Configure event loop for child process handling
700 * @return 0 on success, non-0 on error
701 *
702 * Currently at most one event loop can handle child processes and it must be
703 * distinguished from others by calling this function on it. This could be
704 * fixed but since no process ever makes use of more than one event loop there
705 * is no need.
706 */
460b9539 707int ev_child_setup(ev_source *ev) {
708 D(("installing SIGCHLD handler"));
709 return ev_signal(ev, SIGCHLD, sigchld_callback, 0);
710}
711
768d7355
RK
712/** @brief Wait for a child process to terminate
713 * @param ev Event loop
714 * @param pid Process ID of child
715 * @param options Options to pass to @c wait4()
716 * @param callback Called when child terminates (or possibly when it stops)
717 * @param u Passed to @p callback
718 * @return 0 on success, non-0 on error
719 *
720 * You must have called ev_child_setup() on @p ev once first.
721 */
460b9539 722int ev_child(ev_source *ev,
723 pid_t pid,
724 int options,
725 ev_child_callback *callback,
726 void *u) {
727 int n;
728
729 D(("registering child handling %ld options %d callback %p %p",
730 (long)pid, options, (void *)callback, u));
731 assert(ev->signals[SIGCHLD].callback == sigchld_callback);
732 if(ev->nchildren >= ev->nchildslots) {
733 ev->nchildslots = ev->nchildslots ? 2 * ev->nchildslots : 16;
734 ev->children = xrealloc(ev->children,
735 ev->nchildslots * sizeof (struct child));
736 }
737 n = ev->nchildren++;
738 ev->children[n].pid = pid;
739 ev->children[n].options = options;
740 ev->children[n].callback = callback;
741 ev->children[n].u = u;
742 return 0;
743}
744
768d7355
RK
745/** @brief Stop waiting for a child process
746 * @param ev Event loop
747 * @param pid Child process ID
748 * @return 0 on success, non-0 on error
749 */
460b9539 750int ev_child_cancel(ev_source *ev,
751 pid_t pid) {
752 int n;
753
754 for(n = 0; n < ev->nchildren && ev->children[n].pid != pid; ++n)
755 ;
756 assert(n < ev->nchildren);
757 if(n != ev->nchildren - 1)
758 ev->children[n] = ev->children[ev->nchildren - 1];
759 --ev->nchildren;
760 return 0;
761}
762
763/* socket listeners ***********************************************************/
764
768d7355 765/** @brief State for a socket listener */
460b9539 766struct listen_state {
767 ev_listen_callback *callback;
768 void *u;
769};
770
768d7355 771/** @brief Called when a listenign socket is readable */
460b9539 772static int listen_callback(ev_source *ev, int fd, void *u) {
773 const struct listen_state *l = u;
774 int newfd;
775 union {
776 struct sockaddr_in in;
777#if HAVE_STRUCT_SOCKADDR_IN6
778 struct sockaddr_in6 in6;
779#endif
780 struct sockaddr_un un;
781 struct sockaddr sa;
782 } addr;
783 socklen_t addrlen;
784 int ret;
785
786 D(("callback for listener fd %d", fd));
787 while((addrlen = sizeof addr),
788 (newfd = accept(fd, &addr.sa, &addrlen)) >= 0) {
789 if((ret = l->callback(ev, newfd, &addr.sa, addrlen, l->u)))
790 return ret;
791 }
792 switch(errno) {
793 case EINTR:
794 case EAGAIN:
795 break;
796#ifdef ECONNABORTED
797 case ECONNABORTED:
798 error(errno, "error calling accept");
799 break;
800#endif
801#ifdef EPROTO
802 case EPROTO:
803 /* XXX on some systems EPROTO should be fatal, but we don't know if
804 * we're running on one of them */
805 error(errno, "error calling accept");
806 break;
807#endif
808 default:
809 fatal(errno, "error calling accept");
810 break;
811 }
812 if(errno != EINTR && errno != EAGAIN)
813 error(errno, "error calling accept");
814 return 0;
815}
816
768d7355
RK
817/** @brief Listen on a socket for inbound stream connections
818 * @param ev Event source
819 * @param fd File descriptor of socket
820 * @param callback Called when a new connection arrives
821 * @param u Passed to @p callback
822 * @param what Text description of socket
823 * @return 0 on success, non-0 on error
824 */
460b9539 825int ev_listen(ev_source *ev,
826 int fd,
827 ev_listen_callback *callback,
e8c92ba7
RK
828 void *u,
829 const char *what) {
460b9539 830 struct listen_state *l = xmalloc(sizeof *l);
831
832 D(("registering listener fd %d callback %p %p", fd, (void *)callback, u));
833 l->callback = callback;
834 l->u = u;
e8c92ba7 835 return ev_fd(ev, ev_read, fd, listen_callback, l, what);
460b9539 836}
837
768d7355
RK
838/** @brief Stop listening on a socket
839 * @param ev Event loop
840 * @param fd File descriptor of socket
841 * @return 0 on success, non-0 on error
842 */
460b9539 843int ev_listen_cancel(ev_source *ev, int fd) {
844 D(("cancelling listener fd %d", fd));
845 return ev_fd_cancel(ev, ev_read, fd);
846}
847
848/* buffer *********************************************************************/
849
768d7355 850/** @brief Buffer structure */
460b9539 851struct buffer {
852 char *base, *start, *end, *top;
853};
854
768d7355 855/* @brief Make sure there is @p bytes available at @c b->end */
460b9539 856static void buffer_space(struct buffer *b, size_t bytes) {
857 D(("buffer_space %p %p %p %p want %lu",
858 (void *)b->base, (void *)b->start, (void *)b->end, (void *)b->top,
859 (unsigned long)bytes));
860 if(b->start == b->end)
861 b->start = b->end = b->base;
862 if((size_t)(b->top - b->end) < bytes) {
863 if((size_t)((b->top - b->end) + (b->start - b->base)) < bytes) {
864 size_t newspace = b->end - b->start + bytes, n;
865 char *newbase;
866
867 for(n = 16; n < newspace; n *= 2)
868 ;
869 newbase = xmalloc_noptr(n);
870 memcpy(newbase, b->start, b->end - b->start);
871 b->base = newbase;
872 b->end = newbase + (b->end - b->start);
873 b->top = newbase + n;
874 b->start = newbase; /* must be last */
875 } else {
876 memmove(b->base, b->start, b->end - b->start);
877 b->end = b->base + (b->end - b->start);
878 b->start = b->base;
879 }
880 }
881 D(("result %p %p %p %p",
882 (void *)b->base, (void *)b->start, (void *)b->end, (void *)b->top));
883}
884
885/* buffered writer ************************************************************/
886
768d7355 887/** @brief State structure for a buffered writer */
460b9539 888struct ev_writer {
889 struct sink s;
890 struct buffer b;
891 int fd;
892 int eof;
893 ev_error_callback *callback;
894 void *u;
895 ev_source *ev;
896};
897
768d7355 898/** @brief Called when a writer's file descriptor is writable */
460b9539 899static int writer_callback(ev_source *ev, int fd, void *u) {
900 ev_writer *w = u;
901 int n;
902
903 n = write(fd, w->b.start, w->b.end - w->b.start);
904 D(("callback for writer fd %d, %ld bytes, n=%d, errno=%d",
905 fd, (long)(w->b.end - w->b.start), n, errno));
906 if(n >= 0) {
907 w->b.start += n;
908 if(w->b.start == w->b.end) {
909 if(w->eof) {
910 ev_fd_cancel(ev, ev_write, fd);
911 return w->callback(ev, fd, 0, w->u);
912 } else
913 ev_fd_disable(ev, ev_write, fd);
914 }
915 } else {
916 switch(errno) {
917 case EINTR:
918 case EAGAIN:
919 break;
920 default:
921 ev_fd_cancel(ev, ev_write, fd);
922 return w->callback(ev, fd, errno, w->u);
923 }
924 }
925 return 0;
926}
927
768d7355
RK
928/** @brief Write bytes to a writer's buffer
929 *
930 * This is the sink write callback.
931 *
932 * Calls ev_fd_enable() if necessary (i.e. if the buffer was empty but
933 * now is not).
934 */
460b9539 935static int ev_writer_write(struct sink *sk, const void *s, int n) {
936 ev_writer *w = (ev_writer *)sk;
937
938 buffer_space(&w->b, n);
939 if(w->b.start == w->b.end)
940 ev_fd_enable(w->ev, ev_write, w->fd);
941 memcpy(w->b.end, s, n);
942 w->b.end += n;
943 return 0;
944}
945
768d7355
RK
946/** @brief Create a new buffered writer
947 * @param ev Event loop
948 * @param fd File descriptor to write to
949 * @param callback Called if an error occurs and when finished
950 * @param u Passed to @p callback
951 * @param what Text description
952 * @return New writer or @c NULL
953 */
460b9539 954ev_writer *ev_writer_new(ev_source *ev,
955 int fd,
956 ev_error_callback *callback,
e8c92ba7
RK
957 void *u,
958 const char *what) {
460b9539 959 ev_writer *w = xmalloc(sizeof *w);
960
961 D(("registering writer fd %d callback %p %p", fd, (void *)callback, u));
962 w->s.write = ev_writer_write;
963 w->fd = fd;
964 w->callback = callback;
965 w->u = u;
966 w->ev = ev;
e8c92ba7 967 if(ev_fd(ev, ev_write, fd, writer_callback, w, what))
460b9539 968 return 0;
969 ev_fd_disable(ev, ev_write, fd);
970 return w;
971}
972
768d7355
RK
973/** @brief Return the sink associated with a writer
974 * @param w Writer
975 * @return Pointer to sink
976 *
977 * Writing to the sink will arrange for those bytes to be written to the file
978 * descriptor as and when it is writable.
979 */
460b9539 980struct sink *ev_writer_sink(ev_writer *w) {
981 return &w->s;
982}
983
768d7355
RK
984/** @brief Shutdown callback
985 *
986 * See ev_writer_close().
987 */
460b9539 988static int writer_shutdown(ev_source *ev,
989 const attribute((unused)) struct timeval *now,
990 void *u) {
991 ev_writer *w = u;
992
993 return w->callback(ev, w->fd, 0, w->u);
994}
995
768d7355
RK
996/** @brief Close a writer
997 * @param w Writer to close
998 * @return 0 on success, non-0 on error
999 *
1000 * Close a writer. No more bytes should be written to its sink.
1001 *
1002 * When the last byte has been written the callback will be called with an
1003 * error code of 0. It is guaranteed that this will NOT happen before
1004 * ev_writer_close() returns (although the file descriptor for the writer might
1005 * be cancelled by the time it returns).
1006 */
460b9539 1007int ev_writer_close(ev_writer *w) {
1008 D(("close writer fd %d", w->fd));
1009 w->eof = 1;
1010 if(w->b.start == w->b.end) {
1011 /* we're already finished */
1012 ev_fd_cancel(w->ev, ev_write, w->fd);
1013 return ev_timeout(w->ev, 0, 0, writer_shutdown, w);
1014 }
1015 return 0;
1016}
1017
768d7355
RK
1018/** @brief Cancel a writer discarding any buffered data
1019 * @param w Writer to close
1020 * @return 0 on success, non-0 on error
1021 *
1022 * This cancels a writer immediately. Any unwritten buffered data is discarded
1023 * and the error callback is never called. This is appropriate to call if (for
1024 * instance) the read half of a TCP connection is known to have failed and the
1025 * writer is therefore obsolete.
1026 */
460b9539 1027int ev_writer_cancel(ev_writer *w) {
1028 D(("cancel writer fd %d", w->fd));
1029 return ev_fd_cancel(w->ev, ev_write, w->fd);
1030}
1031
768d7355
RK
1032/** @brief Attempt to flush a writer
1033 * @param w Writer to flush
1034 * @return 0 on success, non-0 on error
1035 *
1036 * Does a speculative write of any buffered data. Does not block if it cannot
1037 * be written.
1038 */
460b9539 1039int ev_writer_flush(ev_writer *w) {
1040 return writer_callback(w->ev, w->fd, w);
1041}
1042
1043/* buffered reader ************************************************************/
1044
768d7355 1045/** @brief State structure for a buffered reader */
460b9539 1046struct ev_reader {
1047 struct buffer b;
1048 int fd;
1049 ev_reader_callback *callback;
1050 ev_error_callback *error_callback;
1051 void *u;
1052 ev_source *ev;
1053 int eof;
1054};
1055
768d7355 1056/** @brief Called when a reader's @p fd is readable */
460b9539 1057static int reader_callback(ev_source *ev, int fd, void *u) {
1058 ev_reader *r = u;
1059 int n;
1060
1061 buffer_space(&r->b, 1);
1062 n = read(fd, r->b.end, r->b.top - r->b.end);
1063 D(("read fd %d buffer %d returned %d errno %d",
1064 fd, (int)(r->b.top - r->b.end), n, errno));
1065 if(n > 0) {
1066 r->b.end += n;
1067 return r->callback(ev, r, fd, r->b.start, r->b.end - r->b.start, 0, r->u);
1068 } else if(n == 0) {
1069 r->eof = 1;
1070 ev_fd_cancel(ev, ev_read, fd);
1071 return r->callback(ev, r, fd, r->b.start, r->b.end - r->b.start, 1, r->u);
1072 } else {
1073 switch(errno) {
1074 case EINTR:
1075 case EAGAIN:
1076 break;
1077 default:
1078 ev_fd_cancel(ev, ev_read, fd);
1079 return r->error_callback(ev, fd, errno, r->u);
1080 }
1081 }
1082 return 0;
1083}
1084
768d7355
RK
1085/** @brief Create a new buffered reader
1086 * @param ev Event loop
1087 * @param fd File descriptor to read from
1088 * @param callback Called when new data is available
1089 * @param error_callback Called if an error occurs
1090 * @param u Passed to callbacks
1091 * @param what Text description
1092 * @return New reader or @c NULL
1093 */
460b9539 1094ev_reader *ev_reader_new(ev_source *ev,
1095 int fd,
1096 ev_reader_callback *callback,
1097 ev_error_callback *error_callback,
e8c92ba7
RK
1098 void *u,
1099 const char *what) {
460b9539 1100 ev_reader *r = xmalloc(sizeof *r);
1101
1102 D(("registering reader fd %d callback %p %p %p",
1103 fd, (void *)callback, (void *)error_callback, u));
1104 r->fd = fd;
1105 r->callback = callback;
1106 r->error_callback = error_callback;
1107 r->u = u;
1108 r->ev = ev;
e8c92ba7 1109 if(ev_fd(ev, ev_read, fd, reader_callback, r, what))
460b9539 1110 return 0;
1111 return r;
1112}
1113
1114void ev_reader_buffer(ev_reader *r, size_t nbytes) {
1115 buffer_space(&r->b, nbytes - (r->b.end - r->b.start));
1116}
1117
768d7355
RK
1118/** @brief Consume @p n bytes from the reader's buffer
1119 * @param r Reader
1120 * @param n Number of bytes to consume
1121 *
1122 * Tells the reader than the next @p n bytes have been dealt with and can now
1123 * be discarded.
1124 */
460b9539 1125void ev_reader_consume(ev_reader *r, size_t n) {
1126 r->b.start += n;
1127}
1128
768d7355
RK
1129/** @brief Cancel a reader
1130 * @param r Reader
1131 * @return 0 on success, non-0 on error
1132 */
460b9539 1133int ev_reader_cancel(ev_reader *r) {
1134 D(("cancel reader fd %d", r->fd));
1135 return ev_fd_cancel(r->ev, ev_read, r->fd);
1136}
1137
768d7355
RK
1138/** @brief Temporarily disable a reader
1139 * @param r Reader
1140 * @return 0 on success, non-0 on error
1141 *
1142 * No further callbacks for this reader will be made. Re-enable with
1143 * ev_reader_enable().
1144 */
460b9539 1145int ev_reader_disable(ev_reader *r) {
1146 D(("disable reader fd %d", r->fd));
1147 return r->eof ? 0 : ev_fd_disable(r->ev, ev_read, r->fd);
1148}
1149
768d7355 1150/** @brief Called from ev_run() for ev_reader_incomplete() */
460b9539 1151static int reader_continuation(ev_source attribute((unused)) *ev,
1152 const attribute((unused)) struct timeval *now,
1153 void *u) {
1154 ev_reader *r = u;
1155
1156 D(("reader continuation callback fd %d", r->fd));
1157 if(ev_fd_enable(r->ev, ev_read, r->fd)) return -1;
1158 return r->callback(ev, r, r->fd, r->b.start, r->b.end - r->b.start, r->eof, r->u);
1159}
1160
768d7355
RK
1161/** @brief Arrange another callback
1162 * @param r reader
1163 * @return 0 on success, non-0 on error
1164 *
1165 * Indicates that the reader can process more input but would like to yield to
1166 * other clients of the event loop. Input will be disabled but it will be
1167 * re-enabled on the next iteration of the event loop and the read callback
1168 * will be called again (even if no further bytes are available).
1169 */
460b9539 1170int ev_reader_incomplete(ev_reader *r) {
1171 if(ev_fd_disable(r->ev, ev_read, r->fd)) return -1;
1172 return ev_timeout(r->ev, 0, 0, reader_continuation, r);
1173}
1174
1175static int reader_enabled(ev_source *ev,
1176 const attribute((unused)) struct timeval *now,
1177 void *u) {
1178 ev_reader *r = u;
1179
1180 D(("reader enabled callback fd %d", r->fd));
1181 return r->callback(ev, r, r->fd, r->b.start, r->b.end - r->b.start, r->eof, r->u);
1182}
1183
768d7355
RK
1184/** @brief Re-enable reading
1185 * @param r reader
1186 * @return 0 on success, non-0 on error
1187 *
1188 * If there is unconsumed data then you get a callback next time round the
1189 * event loop even if nothing new has been read.
1190 *
1191 * The idea is in your read callback you come across a line (or whatever) that
1192 * can't be processed immediately. So you set up processing and disable
1193 * reading with ev_reader_disable(). Later when you finish processing you
1194 * re-enable. You'll automatically get another callback directly from the
1195 * event loop (i.e. not from inside ev_reader_enable()) so you can handle the
1196 * next line (or whatever) if the whole thing has in fact already arrived.
1197 */
460b9539 1198int ev_reader_enable(ev_reader *r) {
1199 D(("enable reader fd %d", r->fd));
1200 return ((r->eof ? 0 : ev_fd_enable(r->ev, ev_read, r->fd))
1201 || ev_timeout(r->ev, 0, 0, reader_enabled, r)) ? -1 : 0;
1202}
1203
1204/*
1205Local Variables:
1206c-basic-offset:2
1207comment-column:40
1208fill-column:79
1209End:
1210*/