2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007 Richard Kettlewell
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.
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.
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
21 * @brief DisOrder event loop
29 #include <sys/types.h>
30 #include <sys/resource.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
51 /** @brief A timeout */
55 ev_timeout_callback *callback;
60 /** @brief A file descriptor in one mode */
63 ev_fd_callback *callback;
68 /** @brief All the file descriptors in a given mode */
70 /** @brief Mask of active file descriptors passed to @c select() */
73 /** @brief File descriptor mask returned from @c select() */
76 /** @brief Number of file descriptors in @p fds */
79 /** @brief Number of slots in @p fds */
82 /** @brief Array of all active file descriptors */
85 /** @brief Highest-numbered file descriptor or 0 */
89 /** @brief A signal handler */
91 struct sigaction oldsa;
92 ev_signal_callback *callback;
96 /** @brief A child process */
100 ev_child_callback *callback;
104 /** @brief An event loop */
106 /** @brief File descriptors, per mode */
107 struct fdmode mode[ev_nmodes];
109 /** @brief Sorted linked list of timeouts
111 * We could use @ref HEAP_TYPE now, but there aren't many timeouts.
113 struct timeout *timeouts;
115 /** @brief Array of handled signals */
116 struct signal signals[NSIG];
118 /** @brief Mask of handled signals */
121 /** @brief Escape early from handling of @c select() results
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.
128 /** @brief Signal handling pipe
130 * The signal handle writes signal numbers down this pipe.
134 /** @brief Number of child processes in @p children */
137 /** @brief Number of slots in @p children */
140 /** @brief Array of child processes */
141 struct child *children;
144 /** @brief Names of file descriptor modes */
145 static const char *modenames[] = { "read", "write", "except" };
147 /* utilities ******************************************************************/
149 /** @brief Great-than comparison for timevals
151 * Ought to be in @file lib/timeval.h
153 static inline int gt(const struct timeval *a, const struct timeval *b) {
154 if(a->tv_sec > b->tv_sec)
156 if(a->tv_sec == b->tv_sec
157 && a->tv_usec > b->tv_usec)
162 /** @brief Greater-than-or-equal comparison for timevals
164 * Ought to be in @ref lib/timeval.h
166 static inline int ge(const struct timeval *a, const struct timeval *b) {
170 /* creation *******************************************************************/
172 /** @brief Create a new event loop */
173 ev_source *ev_new(void) {
174 ev_source *ev = xmalloc(sizeof *ev);
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);
185 /* event loop *****************************************************************/
187 /** @brief Run the event loop
188 * @return -1 on error, non-0 if any callback returned non-0
190 int ev_run(ev_source *ev) {
193 struct timeval delta;
197 struct timeout *t, **tt;
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);
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);
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;
229 xsigprocmask(SIG_UNBLOCK, &ev->sigmask, 0);
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;
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,
247 n = select(maxfd + 1,
248 &ev->mode[ev_read].tripped,
249 &ev->mode[ev_write].tripped,
250 &ev->mode[ev_except].tripped,
253 } while(n < 0 && errno == EINTR);
254 xsigprocmask(SIG_BLOCK, &ev->sigmask, 0);
256 error(errno, "error calling select");
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;
264 if(FD_ISSET(fd, &ev->mode[mode].enabled)
265 && fstat(fd, &sb) < 0)
266 error(errno, "mode %s fstat %d (%s)",
267 modenames[mode], fd, ev->mode[mode].fds[n].what);
269 for(n = 0; n <= maxfd; ++n)
270 if(FD_ISSET(n, &ev->mode[mode].enabled)
271 && fstat(n, &sb) < 0)
272 error(errno, "mode %s fstat %d", modenames[mode], n);
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@. */
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);
296 /* we'll pick up timeouts back round the loop */
300 /* file descriptors ***********************************************************/
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
311 * Sets @ref ev_source::escape, so no further processing of file descriptors
312 * will occur this time round the event loop.
314 int ev_fd(ev_source *ev,
317 ev_fd_callback *callback,
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));
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;
338 ev->mode[mode].fds[n].what = what;
339 if(fd > ev->mode[mode].maxfd)
340 ev->mode[mode].maxfd = fd;
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
351 * Sets @ref ev_source::escape, so no further processing of file descriptors
352 * will occur this time round the event loop.
354 int ev_fd_cancel(ev_source *ev, ev_fdmode mode, int fd) {
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)
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) {
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;
375 /* don't tell select about this fd any more */
376 FD_CLR(fd, &ev->mode[mode].enabled);
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
387 * It is harmless if @p fd is currently disabled, but it must not have been
390 int ev_fd_enable(ev_source *ev, ev_fdmode mode, int fd) {
392 D(("enabling mode %s fd %d", modenames[mode], fd));
393 FD_SET(fd, &ev->mode[mode].enabled);
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
403 * Re-enable with ev_fd_enable(). It is harmless if @p fd is already disabled,
404 * but it must not have been cancelled.
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);
410 /* Suppress any pending callbacks */
415 /** @brief Log a report of file descriptor state */
416 void ev_report(ev_source *ev) {
425 for(mode = 0; mode < ev_nmodes; ++mode) {
426 D(("mode %s maxfd %d", modenames[mode], ev->mode[mode].maxfd));
427 for(n = 0; n < ev->mode[mode].nfds; ++n) {
428 fd = ev->mode[mode].fds[n].fd;
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));
435 for(fd = 0; fd <= ev->mode[mode].maxfd; ++fd) {
436 if(!FD_ISSET(fd, &ev->mode[mode].enabled))
438 for(n = 0; n < ev->mode[mode].nfds; ++n) {
439 if(ev->mode[mode].fds[n].fd == fd)
442 if(n < ev->mode[mode].nfds)
443 snprintf(b, sizeof b, "%d(%s)", fd, ev->mode[mode].fds[n].what);
445 snprintf(b, sizeof b, "%d", fd);
446 dynstr_append(d, ' ');
447 dynstr_append_string(d, b);
450 D(("%s enabled:%s", modenames[mode], d->vec));
454 /* timeouts *******************************************************************/
456 /** @brief Register a timeout
457 * @param ev Event source
458 * @param handlep Where to store timeout handle, or @c NULL
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
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.
471 int ev_timeout(ev_source *ev,
472 ev_timeout_handle *handlep,
473 const struct timeval *when,
474 ev_timeout_callback *callback,
476 struct timeout *t, *p, **pp;
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);
484 t->callback = callback;
487 while((p = *pp) && gt(&t->when, &p->when))
496 /** @brief Cancel a timeout
497 * @param ev Event loop
498 * @param handle Handle returned from ev_timeout(), or 0
499 * @return 0 on success, non-0 on error
501 * If @p handle is 0 then this is a no-op.
503 int ev_timeout_cancel(ev_source *ev,
504 ev_timeout_handle handle) {
505 struct timeout *t = handle, *p, **pp;
509 for(pp = &ev->timeouts; (p = *pp) && p != t; pp = &p->next)
518 /* signals ********************************************************************/
520 /** @brief Mapping of signals to pipe write ends
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
526 static int sigfd[NSIG];
528 /** @brief The signal handler
529 * @param s Signal number
531 * Writes to @c sigfd[s].
533 static void sighandler(int s) {
534 unsigned char sc = s;
535 static const char errmsg[] = "error writing to signal pipe";
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);
544 /** @brief Read callback for signals */
545 static int signal_read(ev_source *ev,
546 int attribute((unused)) fd,
547 void attribute((unused)) *u) {
552 if((n = read(ev->sigpipe[0], &s, 1)) == 1)
553 if((ret = ev->signals[s].callback(ev, s, ev->signals[s].u)))
556 if(n < 0 && (errno != EINTR && errno != EAGAIN)) {
557 error(errno, "error reading from signal pipe %d", ev->sigpipe[0]);
563 /** @brief Close the signal pipe */
564 static void close_sigpipe(ev_source *ev) {
565 int save_errno = errno;
567 xclose(ev->sigpipe[0]);
568 xclose(ev->sigpipe[1]);
569 ev->sigpipe[0] = ev->sigpipe[1] = -1;
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
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.
583 int ev_signal(ev_source *ev,
585 ev_signal_callback *callback,
590 D(("registering signal %d handler callback %p %p", sig, (void *)callback, u));
593 assert(sig <= UCHAR_MAX);
594 if(ev->sigpipe[0] == -1) {
595 D(("creating signal pipe"));
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]);
602 if(ev_fd(ev, ev_read, ev->sigpipe[0], signal_read, 0, "sigpipe read")) {
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);
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
625 int ev_signal_cancel(ev_source *ev,
629 xsigaction(sig, &ev->signals[sig].oldsa, 0);
630 ev->signals[sig].callback = 0;
632 sigdelset(&ev->sigmask, sig);
635 xsigprocmask(SIG_UNBLOCK, &ss, 0);
639 /** @brief Clean up signal handling
640 * @param ev Event loop
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.
645 void ev_signal_atfork(ev_source *ev) {
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);
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]);
662 /* child processes ************************************************************/
664 /** @brief Called on SIGCHLD */
665 static int sigchld_callback(ev_source *ev,
666 int attribute((unused)) sig,
667 void attribute((unused)) *u) {
670 int status, n, ret, revisit;
674 for(n = 0; n < ev->nchildren; ++n) {
675 r = wait4(ev->children[n].pid,
677 ev->children[n].options | WNOHANG,
680 ev_child_callback *c = ev->children[n].callback;
681 void *cu = ev->children[n].u;
683 if(WIFEXITED(status) || WIFSIGNALED(status))
684 ev_child_cancel(ev, r);
686 if((ret = c(ev, r, status, &ru, cu)))
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.
698 error(errno, "error calling wait4 for PID %lu (broken ptrace?)",
699 (unsigned long)ev->children[n].pid);
708 /** @brief Configure event loop for child process handling
709 * @return 0 on success, non-0 on error
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
716 int ev_child_setup(ev_source *ev) {
717 D(("installing SIGCHLD handler"));
718 return ev_signal(ev, SIGCHLD, sigchld_callback, 0);
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
729 * You must have called ev_child_setup() on @p ev once first.
731 int ev_child(ev_source *ev,
734 ev_child_callback *callback,
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));
747 ev->children[n].pid = pid;
748 ev->children[n].options = options;
749 ev->children[n].callback = callback;
750 ev->children[n].u = u;
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
759 int ev_child_cancel(ev_source *ev,
763 for(n = 0; n < ev->nchildren && ev->children[n].pid != pid; ++n)
765 assert(n < ev->nchildren);
766 if(n != ev->nchildren - 1)
767 ev->children[n] = ev->children[ev->nchildren - 1];
772 /* socket listeners ***********************************************************/
774 /** @brief State for a socket listener */
775 struct listen_state {
776 ev_listen_callback *callback;
780 /** @brief Called when a listenign socket is readable */
781 static int listen_callback(ev_source *ev, int fd, void *u) {
782 const struct listen_state *l = u;
785 struct sockaddr_in in;
786 #if HAVE_STRUCT_SOCKADDR_IN6
787 struct sockaddr_in6 in6;
789 struct sockaddr_un un;
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)))
807 error(errno, "error calling accept");
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");
818 fatal(errno, "error calling accept");
821 if(errno != EINTR && errno != EAGAIN)
822 error(errno, "error calling accept");
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
834 int ev_listen(ev_source *ev,
836 ev_listen_callback *callback,
839 struct listen_state *l = xmalloc(sizeof *l);
841 D(("registering listener fd %d callback %p %p", fd, (void *)callback, u));
842 l->callback = callback;
844 return ev_fd(ev, ev_read, fd, listen_callback, l, what);
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
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);
857 /* buffer *********************************************************************/
859 /** @brief Buffer structure */
861 char *base, *start, *end, *top;
864 /* @brief Make sure there is @p bytes available at @c b->end */
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;
876 for(n = 16; n < newspace; n *= 2)
878 newbase = xmalloc_noptr(n);
879 memcpy(newbase, b->start, b->end - b->start);
881 b->end = newbase + (b->end - b->start);
882 b->top = newbase + n;
883 b->start = newbase; /* must be last */
885 memmove(b->base, b->start, b->end - b->start);
886 b->end = b->base + (b->end - b->start);
890 D(("result %p %p %p %p",
891 (void *)b->base, (void *)b->start, (void *)b->end, (void *)b->top));
894 /* readers and writers *******************************************************/
896 /** @brief State structure for a buffered writer */
898 /** @brief Sink used for writing to the buffer */
901 /** @brief Output buffer */
904 /** @brief File descriptor to write to */
907 /** @brief Set if there'll be no more output */
910 /** @brief Error/termination callback */
911 ev_error_callback *callback;
913 /** @brief Passed to @p callback */
916 /** @brief Parent event source */
919 /** @brief Maximum amount of time between succesful writes, 0 = don't care */
921 /** @brief Maximum amount of data to buffer, 0 = don't care */
923 /** @brief Error code to pass to @p callback (see writer_shutdown()) */
925 /** @brief Timeout handle for @p timebound (or 0) */
926 ev_timeout_handle timeout;
928 /** @brief Description of this writer */
931 /** @brief Tied reader or 0 */
935 /** @brief State structure for a buffered reader */
937 /** @brief Input buffer */
939 /** @brief File descriptor read from */
941 /** @brief Called when new data is available */
942 ev_reader_callback *callback;
943 /** @brief Called on error and shutdown */
944 ev_error_callback *error_callback;
945 /** @brief Passed to @p callback and @p error_callback */
947 /** @brief Parent event loop */
949 /** @brief Set when EOF is detected */
951 /** @brief Error code to pass to error callback */
953 /** @brief Tied writer or NULL */
957 /* buffered writer ************************************************************/
959 /** @brief Shut down the writer
961 * This is called to shut down a writer. The error callback is not called
962 * through any other path. Also we do not cancel @p fd from anywhere else,
963 * though we might disable it.
965 * It has the signature of a timeout callback so that it can be called from a
968 * Calls @p callback with @p w->syntherr as the error code (which might be 0).
970 static int writer_shutdown(ev_source *ev,
971 const attribute((unused)) struct timeval *now,
976 return 0; /* already shut down */
977 D(("writer_shutdown fd=%d error=%d", w->fd, w->error));
978 ev_timeout_cancel(ev, w->timeout);
979 ev_fd_cancel(ev, ev_write, w->fd);
982 D(("found a tied reader"));
983 /* If there is a reader still around we just untie it */
984 w->reader->writer = 0;
985 shutdown(w->fd, SHUT_WR); /* there'll be no more writes */
987 D(("no tied reader"));
988 /* There's no reader so we are free to close the FD */
992 return w->callback(ev, w->error, w->u);
995 /** @brief Called when a writer's @p timebound expires */
996 static int writer_timebound_exceeded(ev_source *ev,
997 const struct timeval *now,
999 ev_writer *const w = u;
1001 error(0, "abandoning writer %s because no writes within %ds",
1002 w->what, w->timebound);
1003 w->error = ETIMEDOUT;
1004 return writer_shutdown(ev, now, u);
1007 /** @brief Set the time bound callback (if not set already) */
1008 static void writer_set_timebound(ev_writer *w) {
1009 if(w->timebound && !w->timeout) {
1010 struct timeval when;
1011 ev_source *const ev = w->ev;
1013 xgettimeofday(&when, 0);
1014 when.tv_sec += w->timebound;
1015 ev_timeout(ev, &w->timeout, &when, writer_timebound_exceeded, w);
1019 /** @brief Called when a writer's file descriptor is writable */
1020 static int writer_callback(ev_source *ev, int fd, void *u) {
1021 ev_writer *const w = u;
1024 n = write(fd, w->b.start, w->b.end - w->b.start);
1025 D(("callback for writer fd %d, %ld bytes, n=%d, errno=%d",
1026 fd, (long)(w->b.end - w->b.start), n, errno));
1028 /* Consume bytes from the buffer */
1030 /* Suppress any outstanding timeout */
1031 ev_timeout_cancel(ev, w->timeout);
1033 if(w->b.start == w->b.end) {
1034 /* The buffer is empty */
1036 /* We're done, we can shut down this writer */
1038 return writer_shutdown(ev, 0, w);
1040 /* There might be more to come but we don't need writer_callback() to
1041 * be called for the time being */
1042 ev_fd_disable(ev, ev_write, fd);
1044 /* The buffer isn't empty, set a timeout so we give up if we don't manage
1045 * to write some more within a reasonable time */
1046 writer_set_timebound(w);
1054 return writer_shutdown(ev, 0, w);
1060 /** @brief Write bytes to a writer's buffer
1062 * This is the sink write callback.
1064 * Calls ev_fd_enable() if necessary (i.e. if the buffer was empty but
1067 static int ev_writer_write(struct sink *sk, const void *s, int n) {
1068 ev_writer *w = (ev_writer *)sk;
1071 return 0; /* avoid silliness */
1073 error(0, "ev_writer_write on %s after shutdown", w->what);
1074 if(w->spacebound && w->b.end - w->b.start + n > w->spacebound) {
1075 /* The new buffer contents will exceed the space bound. We assume that the
1076 * remote client has gone away and TCP hasn't noticed yet, or that it's got
1077 * hopelessly stuck. */
1078 error(0, "abandoning writer %s because buffer has reached %td bytes",
1079 w->what, w->b.end - w->b.start);
1080 ev_fd_disable(w->ev, ev_write, w->fd);
1082 return ev_timeout(w->ev, 0, 0, writer_shutdown, w);
1084 /* Make sure there is space */
1085 buffer_space(&w->b, n);
1086 /* If the buffer was formerly empty then we'll need to re-enable the FD */
1087 if(w->b.start == w->b.end)
1088 ev_fd_enable(w->ev, ev_write, w->fd);
1089 memcpy(w->b.end, s, n);
1091 /* Arrange a timeout if there wasn't one set already */
1092 writer_set_timebound(w);
1096 /** @brief Create a new buffered writer
1097 * @param ev Event loop
1098 * @param fd File descriptor to write to
1099 * @param callback Called if an error occurs and when finished
1100 * @param u Passed to @p callback
1101 * @param what Text description
1102 * @return New writer or @c NULL
1104 * Writers own their file descriptor and close it when they have finished with
1107 * If you pass the same fd to a reader and writer, you must tie them together
1110 ev_writer *ev_writer_new(ev_source *ev,
1112 ev_error_callback *callback,
1115 ev_writer *w = xmalloc(sizeof *w);
1117 D(("registering writer fd %d callback %p %p", fd, (void *)callback, u));
1118 w->s.write = ev_writer_write;
1120 w->callback = callback;
1123 w->timebound = 10 * 60;
1124 w->spacebound = 512 * 1024;
1126 if(ev_fd(ev, ev_write, fd, writer_callback, w, what))
1128 /* Buffer is initially empty so we don't want a callback */
1129 ev_fd_disable(ev, ev_write, fd);
1133 /** @brief Get/set the time bound
1135 * @param new_time_bound New bound or -1 for no change
1136 * @return Latest time bound
1138 * If @p new_time_bound is negative then the current time bound is returned.
1139 * Otherwise it is set and the new value returned.
1141 * The time bound is the number of seconds allowed between writes. If it takes
1142 * longer than this to flush a buffer then the peer will be assumed to be dead
1143 * and an error will be synthesized. 0 means "don't care". The default time
1144 * bound is 10 minutes.
1146 * Note that this value does not take into account kernel buffering and
1149 int ev_writer_time_bound(ev_writer *w,
1150 int new_time_bound) {
1151 if(new_time_bound >= 0)
1152 w->timebound = new_time_bound;
1153 return w->timebound;
1156 /** @brief Get/set the space bound
1158 * @param new_space_bound New bound or -1 for no change
1159 * @return Latest space bound
1161 * If @p new_space_bound is negative then the current space bound is returned.
1162 * Otherwise it is set and the new value returned.
1164 * The space bound is the number of bytes allowed between in the buffer. If
1165 * the buffer exceeds this size an error will be synthesized. 0 means "don't
1166 * care". The default space bound is 512Kbyte.
1168 * Note that this value does not take into account kernel buffering.
1170 int ev_writer_space_bound(ev_writer *w,
1171 int new_space_bound) {
1172 if(new_space_bound >= 0)
1173 w->spacebound = new_space_bound;
1174 return w->spacebound;
1177 /** @brief Return the sink associated with a writer
1179 * @return Pointer to sink
1181 * Writing to the sink will arrange for those bytes to be written to the file
1182 * descriptor as and when it is writable.
1184 struct sink *ev_writer_sink(ev_writer *w) {
1186 fatal(0, "ev_write_sink called with null writer");
1190 /** @brief Close a writer
1191 * @param w Writer to close
1192 * @return 0 on success, non-0 on error
1194 * Close a writer. No more bytes should be written to its sink.
1196 * When the last byte has been written the callback will be called with an
1197 * error code of 0. It is guaranteed that this will NOT happen before
1198 * ev_writer_close() returns (although the file descriptor for the writer might
1199 * be cancelled by the time it returns).
1201 int ev_writer_close(ev_writer *w) {
1202 D(("close writer fd %d", w->fd));
1204 return 0; /* already closed */
1206 if(w->b.start == w->b.end) {
1207 /* We're already finished */
1208 w->error = 0; /* no error */
1209 return ev_timeout(w->ev, 0, 0, writer_shutdown, w);
1214 /** @brief Attempt to flush a writer
1215 * @param w Writer to flush
1216 * @return 0 on success, non-0 on error
1218 * Does a speculative write of any buffered data. Does not block if it cannot
1221 int ev_writer_flush(ev_writer *w) {
1222 return writer_callback(w->ev, w->fd, w);
1225 /* buffered reader ************************************************************/
1227 /** @brief Shut down a reader*
1229 * This is the only path through which we cancel and close the file descriptor.
1230 * As with the writer case it is given timeout signature to allow it be
1231 * deferred to the next iteration of the event loop.
1233 * We only call @p error_callback if @p error is nonzero (unlike the writer
1236 static int reader_shutdown(ev_source *ev,
1237 const attribute((unused)) struct timeval *now,
1239 ev_reader *const r = u;
1242 return 0; /* already shut down */
1243 D(("reader_shutdown fd=%d", r->fd));
1244 ev_fd_cancel(ev, ev_read, r->fd);
1247 D(("found a tied writer"));
1248 /* If there is a writer still around we just untie it */
1249 r->writer->reader = 0;
1250 shutdown(r->fd, SHUT_RD); /* there'll be no more reads */
1252 D(("no tied writer found"));
1253 /* There's no writer so we are free to close the FD */
1258 return r->error_callback(ev, r->error, r->u);
1263 /** @brief Called when a reader's @p fd is readable */
1264 static int reader_callback(ev_source *ev, int fd, void *u) {
1268 buffer_space(&r->b, 1);
1269 n = read(fd, r->b.end, r->b.top - r->b.end);
1270 D(("read fd %d buffer %d returned %d errno %d",
1271 fd, (int)(r->b.top - r->b.end), n, errno));
1274 return r->callback(ev, r, r->b.start, r->b.end - r->b.start, 0, r->u);
1276 /* No more read callbacks needed */
1277 ev_fd_disable(r->ev, ev_read, r->fd);
1278 ev_timeout(r->ev, 0, 0, reader_shutdown, r);
1279 /* Pass the remaining data and an eof indicator to the user */
1280 return r->callback(ev, r, r->b.start, r->b.end - r->b.start, 1, r->u);
1287 /* Fatal error, kill the reader now */
1289 return reader_shutdown(ev, 0, r);
1295 /** @brief Create a new buffered reader
1296 * @param ev Event loop
1297 * @param fd File descriptor to read from
1298 * @param callback Called when new data is available
1299 * @param error_callback Called if an error occurs
1300 * @param u Passed to callbacks
1301 * @param what Text description
1302 * @return New reader or @c NULL
1304 * Readers own their fd and close it when they are finished with it.
1306 * If you pass the same fd to a reader and writer, you must tie them together
1309 ev_reader *ev_reader_new(ev_source *ev,
1311 ev_reader_callback *callback,
1312 ev_error_callback *error_callback,
1315 ev_reader *r = xmalloc(sizeof *r);
1317 D(("registering reader fd %d callback %p %p %p",
1318 fd, (void *)callback, (void *)error_callback, u));
1320 r->callback = callback;
1321 r->error_callback = error_callback;
1324 if(ev_fd(ev, ev_read, fd, reader_callback, r, what))
1329 void ev_reader_buffer(ev_reader *r, size_t nbytes) {
1330 buffer_space(&r->b, nbytes - (r->b.end - r->b.start));
1333 /** @brief Consume @p n bytes from the reader's buffer
1335 * @param n Number of bytes to consume
1337 * Tells the reader than the next @p n bytes have been dealt with and can now
1340 void ev_reader_consume(ev_reader *r, size_t n) {
1344 /** @brief Cancel a reader
1346 * @return 0 on success, non-0 on error
1348 * No further callbacks will be made, and the FD will be closed (in a later
1349 * iteration of the event loop).
1351 int ev_reader_cancel(ev_reader *r) {
1352 D(("cancel reader fd %d", r->fd));
1354 return 0; /* already thoroughly cancelled */
1355 ev_fd_disable(r->ev, ev_read, r->fd);
1356 return ev_timeout(r->ev, 0, 0, reader_shutdown, r);
1359 /** @brief Temporarily disable a reader
1361 * @return 0 on success, non-0 on error
1363 * No further callbacks for this reader will be made. Re-enable with
1364 * ev_reader_enable().
1366 int ev_reader_disable(ev_reader *r) {
1367 D(("disable reader fd %d", r->fd));
1368 return ev_fd_disable(r->ev, ev_read, r->fd);
1371 /** @brief Called from ev_run() for ev_reader_incomplete() */
1372 static int reader_continuation(ev_source attribute((unused)) *ev,
1373 const attribute((unused)) struct timeval *now,
1377 D(("reader continuation callback fd %d", r->fd));
1378 /* If not at EOF turn the FD back on */
1380 if(ev_fd_enable(r->ev, ev_read, r->fd))
1382 /* We're already in a timeout callback so there's no reason we can't call the
1383 * user callback directly (compare ev_reader_enable()). */
1384 return r->callback(ev, r, r->b.start, r->b.end - r->b.start, r->eof, r->u);
1387 /** @brief Arrange another callback
1389 * @return 0 on success, non-0 on error
1391 * Indicates that the reader can process more input but would like to yield to
1392 * other clients of the event loop. Input will be disabled but it will be
1393 * re-enabled on the next iteration of the event loop and the read callback
1394 * will be called again (even if no further bytes are available).
1396 int ev_reader_incomplete(ev_reader *r) {
1397 if(ev_fd_disable(r->ev, ev_read, r->fd)) return -1;
1398 return ev_timeout(r->ev, 0, 0, reader_continuation, r);
1401 static int reader_enabled(ev_source *ev,
1402 const attribute((unused)) struct timeval *now,
1406 D(("reader enabled callback fd %d", r->fd));
1407 return r->callback(ev, r, r->b.start, r->b.end - r->b.start, r->eof, r->u);
1410 /** @brief Re-enable reading
1412 * @return 0 on success, non-0 on error
1414 * If there is unconsumed data then you get a callback next time round the
1415 * event loop even if nothing new has been read.
1417 * The idea is in your read callback you come across a line (or whatever) that
1418 * can't be processed immediately. So you set up processing and disable
1419 * reading with ev_reader_disable(). Later when you finish processing you
1420 * re-enable. You'll automatically get another callback directly from the
1421 * event loop (i.e. not from inside ev_reader_enable()) so you can handle the
1422 * next line (or whatever) if the whole thing has in fact already arrived.
1424 * The difference between this process and calling ev_reader_incomplete() is
1425 * ev_reader_incomplete() deals with the case where you can process now but
1426 * would rather yield to other clients of the event loop, while using
1427 * ev_reader_disable() and ev_reader_enable() deals with the case where you
1428 * cannot process input yet because some other process is actually not
1431 int ev_reader_enable(ev_reader *r) {
1432 D(("enable reader fd %d", r->fd));
1434 /* First if we're not at EOF then we re-enable reading */
1436 if(ev_fd_enable(r->ev, ev_read, r->fd))
1438 /* Arrange another callback next time round the event loop */
1439 return ev_timeout(r->ev, 0, 0, reader_enabled, r);
1442 /** @brief Tie a reader and a writer together
1445 * @return 0 on success, non-0 on error
1447 * This function must be called if @p r and @p w share a file descritptor.
1449 int ev_tie(ev_reader *r, ev_writer *w) {
1450 assert(r->writer == 0);
1451 assert(w->reader == 0);