2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 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>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
49 /** @brief A timeout */
53 ev_timeout_callback
*callback
;
58 /** @brief Comparison function for timeouts */
59 static int timeout_lt(const struct timeout
*a
,
60 const struct timeout
*b
) {
61 return tvlt(&a
->when
, &b
->when
);
64 HEAP_TYPE(timeout_heap
, struct timeout
*, timeout_lt
);
65 HEAP_DEFINE(timeout_heap
, struct timeout
*, timeout_lt
);
67 /** @brief A file descriptor in one mode */
70 ev_fd_callback
*callback
;
75 /** @brief All the file descriptors in a given mode */
77 /** @brief Mask of active file descriptors passed to @c select() */
80 /** @brief File descriptor mask returned from @c select() */
83 /** @brief Number of file descriptors in @p fds */
86 /** @brief Number of slots in @p fds */
89 /** @brief Array of all active file descriptors */
92 /** @brief Highest-numbered file descriptor or 0 */
96 /** @brief A signal handler */
98 struct sigaction oldsa
;
99 ev_signal_callback
*callback
;
103 /** @brief A child process */
107 ev_child_callback
*callback
;
111 /** @brief An event loop */
113 /** @brief File descriptors, per mode */
114 struct fdmode mode
[ev_nmodes
];
116 /** @brief Heap of timeouts */
117 struct timeout_heap timeouts
[1];
119 /** @brief Array of handled signals */
120 struct signal signals
[NSIG
];
122 /** @brief Mask of handled signals */
125 /** @brief Escape early from handling of @c select() results
127 * This is set if any of the file descriptor arrays are invalidated, since
128 * it's then not safe for processing of them to continue.
132 /** @brief Signal handling pipe
134 * The signal handle writes signal numbers down this pipe.
138 /** @brief Number of child processes in @p children */
141 /** @brief Number of slots in @p children */
144 /** @brief Array of child processes */
145 struct child
*children
;
148 /** @brief Names of file descriptor modes */
149 static const char *modenames
[] = { "read", "write", "except" };
151 /* utilities ******************************************************************/
153 /* creation *******************************************************************/
155 /** @brief Create a new event loop */
156 ev_source
*ev_new(void) {
157 ev_source
*ev
= xmalloc(sizeof *ev
);
160 memset(ev
, 0, sizeof *ev
);
161 for(n
= 0; n
< ev_nmodes
; ++n
)
162 FD_ZERO(&ev
->mode
[n
].enabled
);
163 ev
->sigpipe
[0] = ev
->sigpipe
[1] = -1;
164 sigemptyset(&ev
->sigmask
);
165 timeout_heap_init(ev
->timeouts
);
169 /* event loop *****************************************************************/
171 /** @brief Run the event loop
172 * @return -1 on error, non-0 if any callback returned non-0
174 int ev_run(ev_source
*ev
) {
177 struct timeval delta
;
181 struct timeout
*timeouts
, *t
, **tt
;
184 xgettimeofday(&now
, 0);
185 /* Handle timeouts. We don't want to handle any timeouts that are added
186 * while we're handling them (otherwise we'd have to break out of infinite
187 * loops, preferrably without starving better-behaved subsystems). Hence
188 * the slightly complicated two-phase approach here. */
189 /* First we read those timeouts that have triggered out of the heap. We
190 * keep them in the same order they came out of the heap in. */
192 while(timeout_heap_count(ev
->timeouts
)
193 && tvle(&timeout_heap_first(ev
->timeouts
)->when
, &now
)) {
194 /* This timeout has reached its trigger time; provided it has not been
195 * cancelled we add it to the timeouts list. */
196 t
= timeout_heap_remove(ev
->timeouts
);
203 /* Now we can run the callbacks for those timeouts. They might add further
204 * timeouts that are already in the past but they won't trigger until the
205 * next time round the event loop. */
206 for(t
= timeouts
; t
; t
= t
->next
) {
207 D(("calling timeout for %ld.%ld callback %p %p",
208 (long)t
->when
.tv_sec
, (long)t
->when
.tv_usec
,
209 (void *)t
->callback
, t
->u
));
210 ret
= t
->callback(ev
, &now
, t
->u
);
215 for(mode
= 0; mode
< ev_nmodes
; ++mode
) {
216 ev
->mode
[mode
].tripped
= ev
->mode
[mode
].enabled
;
217 if(ev
->mode
[mode
].maxfd
> maxfd
)
218 maxfd
= ev
->mode
[mode
].maxfd
;
220 xsigprocmask(SIG_UNBLOCK
, &ev
->sigmask
, 0);
222 if(timeout_heap_count(ev
->timeouts
)) {
223 t
= timeout_heap_first(ev
->timeouts
);
224 xgettimeofday(&now
, 0);
225 delta
.tv_sec
= t
->when
.tv_sec
- now
.tv_sec
;
226 delta
.tv_usec
= t
->when
.tv_usec
- now
.tv_usec
;
227 if(delta
.tv_usec
< 0) {
228 delta
.tv_usec
+= 1000000;
232 delta
.tv_sec
= delta
.tv_usec
= 0;
233 n
= select(maxfd
+ 1,
234 &ev
->mode
[ev_read
].tripped
,
235 &ev
->mode
[ev_write
].tripped
,
236 &ev
->mode
[ev_except
].tripped
,
239 n
= select(maxfd
+ 1,
240 &ev
->mode
[ev_read
].tripped
,
241 &ev
->mode
[ev_write
].tripped
,
242 &ev
->mode
[ev_except
].tripped
,
245 } while(n
< 0 && errno
== EINTR
);
246 xsigprocmask(SIG_BLOCK
, &ev
->sigmask
, 0);
248 error(errno
, "error calling select");
250 /* If there's a bad FD in the mix then check them all and log what we
251 * find, to ease debugging */
252 for(mode
= 0; mode
< ev_nmodes
; ++mode
) {
253 for(n
= 0; n
< ev
->mode
[mode
].nfds
; ++n
) {
254 const int fd
= ev
->mode
[mode
].fds
[n
].fd
;
256 if(FD_ISSET(fd
, &ev
->mode
[mode
].enabled
)
257 && fstat(fd
, &sb
) < 0)
258 error(errno
, "mode %s fstat %d (%s)",
259 modenames
[mode
], fd
, ev
->mode
[mode
].fds
[n
].what
);
261 for(n
= 0; n
<= maxfd
; ++n
)
262 if(FD_ISSET(n
, &ev
->mode
[mode
].enabled
)
263 && fstat(n
, &sb
) < 0)
264 error(errno
, "mode %s fstat %d", modenames
[mode
], n
);
270 /* if anything deranges the meaning of an fd, or re-orders the
271 * fds[] tables, we'd better give up; such operations will
272 * therefore set @escape@. */
274 for(mode
= 0; mode
< ev_nmodes
&& !ev
->escape
; ++mode
)
275 for(n
= 0; n
< ev
->mode
[mode
].nfds
&& !ev
->escape
; ++n
) {
276 int fd
= ev
->mode
[mode
].fds
[n
].fd
;
277 if(FD_ISSET(fd
, &ev
->mode
[mode
].tripped
)) {
278 D(("calling %s fd %d callback %p %p", modenames
[mode
], fd
,
279 (void *)ev
->mode
[mode
].fds
[n
].callback
,
280 ev
->mode
[mode
].fds
[n
].u
));
281 ret
= ev
->mode
[mode
].fds
[n
].callback(ev
, fd
,
282 ev
->mode
[mode
].fds
[n
].u
);
288 /* we'll pick up timeouts back round the loop */
292 /* file descriptors ***********************************************************/
294 /** @brief Register a file descriptor
295 * @param ev Event loop
296 * @param mode @c ev_read or @c ev_write
297 * @param fd File descriptor
298 * @param callback Called when @p is readable/writable
299 * @param u Passed to @p callback
300 * @param what Text description
301 * @return 0 on success, non-0 on error
303 * Sets @ref ev_source::escape, so no further processing of file descriptors
304 * will occur this time round the event loop.
306 int ev_fd(ev_source
*ev
,
309 ev_fd_callback
*callback
,
314 D(("registering %s fd %d callback %p %p", modenames
[mode
], fd
,
315 (void *)callback
, u
));
316 assert(mode
< ev_nmodes
);
317 if(ev
->mode
[mode
].nfds
>= ev
->mode
[mode
].fdslots
) {
318 ev
->mode
[mode
].fdslots
= (ev
->mode
[mode
].fdslots
319 ?
2 * ev
->mode
[mode
].fdslots
: 16);
320 D(("expanding %s fd table to %d entries", modenames
[mode
],
321 ev
->mode
[mode
].fdslots
));
322 ev
->mode
[mode
].fds
= xrealloc(ev
->mode
[mode
].fds
,
323 ev
->mode
[mode
].fdslots
* sizeof (struct fd
));
325 n
= ev
->mode
[mode
].nfds
++;
326 FD_SET(fd
, &ev
->mode
[mode
].enabled
);
327 ev
->mode
[mode
].fds
[n
].fd
= fd
;
328 ev
->mode
[mode
].fds
[n
].callback
= callback
;
329 ev
->mode
[mode
].fds
[n
].u
= u
;
330 ev
->mode
[mode
].fds
[n
].what
= what
;
331 if(fd
> ev
->mode
[mode
].maxfd
)
332 ev
->mode
[mode
].maxfd
= fd
;
337 /** @brief Cancel a file descriptor
338 * @param ev Event loop
339 * @param mode @c ev_read or @c ev_write
340 * @param fd File descriptor
341 * @return 0 on success, non-0 on error
343 * Sets @ref ev_source::escape, so no further processing of file descriptors
344 * will occur this time round the event loop.
346 int ev_fd_cancel(ev_source
*ev
, ev_fdmode mode
, int fd
) {
350 D(("cancelling mode %s fd %d", modenames
[mode
], fd
));
351 /* find the right struct fd */
352 for(n
= 0; n
< ev
->mode
[mode
].nfds
&& fd
!= ev
->mode
[mode
].fds
[n
].fd
; ++n
)
354 assert(n
< ev
->mode
[mode
].nfds
);
355 /* swap in the last fd and reduce the count */
356 if(n
!= ev
->mode
[mode
].nfds
- 1)
357 ev
->mode
[mode
].fds
[n
] = ev
->mode
[mode
].fds
[ev
->mode
[mode
].nfds
- 1];
358 --ev
->mode
[mode
].nfds
;
359 /* if that was the biggest fd, find the new biggest one */
360 if(fd
== ev
->mode
[mode
].maxfd
) {
362 for(n
= 0; n
< ev
->mode
[mode
].nfds
; ++n
)
363 if(ev
->mode
[mode
].fds
[n
].fd
> maxfd
)
364 maxfd
= ev
->mode
[mode
].fds
[n
].fd
;
365 ev
->mode
[mode
].maxfd
= maxfd
;
367 /* don't tell select about this fd any more */
368 FD_CLR(fd
, &ev
->mode
[mode
].enabled
);
373 /** @brief Re-enable a file descriptor
374 * @param ev Event loop
375 * @param mode @c ev_read or @c ev_write
376 * @param fd File descriptor
377 * @return 0 on success, non-0 on error
379 * It is harmless if @p fd is currently disabled, but it must not have been
382 int ev_fd_enable(ev_source
*ev
, ev_fdmode mode
, int fd
) {
384 D(("enabling mode %s fd %d", modenames
[mode
], fd
));
385 FD_SET(fd
, &ev
->mode
[mode
].enabled
);
389 /** @brief Temporarily disable a file descriptor
390 * @param ev Event loop
391 * @param mode @c ev_read or @c ev_write
392 * @param fd File descriptor
393 * @return 0 on success, non-0 on error
395 * Re-enable with ev_fd_enable(). It is harmless if @p fd is already disabled,
396 * but it must not have been cancelled.
398 int ev_fd_disable(ev_source
*ev
, ev_fdmode mode
, int fd
) {
399 D(("disabling mode %s fd %d", modenames
[mode
], fd
));
400 FD_CLR(fd
, &ev
->mode
[mode
].enabled
);
401 FD_CLR(fd
, &ev
->mode
[mode
].tripped
);
402 /* Suppress any pending callbacks */
407 /** @brief Log a report of file descriptor state */
408 void ev_report(ev_source
*ev
) {
417 for(mode
= 0; mode
< ev_nmodes
; ++mode
) {
418 D(("mode %s maxfd %d", modenames
[mode
], ev
->mode
[mode
].maxfd
));
419 for(n
= 0; n
< ev
->mode
[mode
].nfds
; ++n
) {
420 fd
= ev
->mode
[mode
].fds
[n
].fd
;
421 D(("fd %s %d%s%s (%s)", modenames
[mode
], fd
,
422 FD_ISSET(fd
, &ev
->mode
[mode
].enabled
) ?
" enabled" : "",
423 FD_ISSET(fd
, &ev
->mode
[mode
].tripped
) ?
" tripped" : "",
424 ev
->mode
[mode
].fds
[n
].what
));
427 for(fd
= 0; fd
<= ev
->mode
[mode
].maxfd
; ++fd
) {
428 if(!FD_ISSET(fd
, &ev
->mode
[mode
].enabled
))
430 for(n
= 0; n
< ev
->mode
[mode
].nfds
; ++n
) {
431 if(ev
->mode
[mode
].fds
[n
].fd
== fd
)
434 if(n
< ev
->mode
[mode
].nfds
)
435 snprintf(b
, sizeof b
, "%d(%s)", fd
, ev
->mode
[mode
].fds
[n
].what
);
437 snprintf(b
, sizeof b
, "%d", fd
);
438 dynstr_append(d
, ' ');
439 dynstr_append_string(d
, b
);
442 D(("%s enabled:%s", modenames
[mode
], d
->vec
));
446 /* timeouts *******************************************************************/
448 /** @brief Register a timeout
449 * @param ev Event source
450 * @param handlep Where to store timeout handle, or @c NULL
451 * @param when Earliest time to call @p callback, or @c NULL
452 * @param callback Function to call at or after @p when
453 * @param u Passed to @p callback
454 * @return 0 on success, non-0 on error
456 * If @p when is a null pointer then a time of 0 is assumed. The effect is to
457 * call the timeout handler from ev_run() next time around the event loop.
458 * This is used internally to schedule various operations if it is not
459 * convenient to call them from the current place in the call stack, or
460 * externally to ensure that other clients of the event loop get a look in when
461 * performing some lengthy operation.
463 int ev_timeout(ev_source
*ev
,
464 ev_timeout_handle
*handlep
,
465 const struct timeval
*when
,
466 ev_timeout_callback
*callback
,
470 D(("registering timeout at %ld.%ld callback %p %p",
471 when ?
(long)when
->tv_sec
: 0, when ?
(long)when
->tv_usec
: 0,
472 (void *)callback
, u
));
473 t
= xmalloc(sizeof *t
);
476 t
->callback
= callback
;
479 timeout_heap_insert(ev
->timeouts
, t
);
485 /** @brief Cancel a timeout
486 * @param ev Event loop
487 * @param handle Handle returned from ev_timeout(), or 0
488 * @return 0 on success, non-0 on error
490 * If @p handle is 0 then this is a no-op.
492 int ev_timeout_cancel(ev_source
attribute((unused
)) *ev
,
493 ev_timeout_handle handle
) {
494 struct timeout
*t
= handle
;
501 /* signals ********************************************************************/
503 /** @brief Mapping of signals to pipe write ends
505 * The pipes are per-event loop, it's possible in theory for there to be
506 * multiple event loops (e.g. in different threads), although in fact DisOrder
509 static int sigfd
[NSIG
];
511 /** @brief The signal handler
512 * @param s Signal number
514 * Writes to @c sigfd[s].
516 static void sighandler(int s
) {
517 unsigned char sc
= s
;
518 static const char errmsg
[] = "error writing to signal pipe";
520 /* probably the reader has stopped listening for some reason */
521 if(write(sigfd
[s
], &sc
, 1) < 0) {
522 write(2, errmsg
, sizeof errmsg
- 1);
527 /** @brief Read callback for signals */
528 static int signal_read(ev_source
*ev
,
529 int attribute((unused
)) fd
,
530 void attribute((unused
)) *u
) {
535 if((n
= read(ev
->sigpipe
[0], &s
, 1)) == 1)
536 if((ret
= ev
->signals
[s
].callback(ev
, s
, ev
->signals
[s
].u
)))
539 if(n
< 0 && (errno
!= EINTR
&& errno
!= EAGAIN
)) {
540 error(errno
, "error reading from signal pipe %d", ev
->sigpipe
[0]);
546 /** @brief Close the signal pipe */
547 static void close_sigpipe(ev_source
*ev
) {
548 int save_errno
= errno
;
550 xclose(ev
->sigpipe
[0]);
551 xclose(ev
->sigpipe
[1]);
552 ev
->sigpipe
[0] = ev
->sigpipe
[1] = -1;
556 /** @brief Register a signal handler
557 * @param ev Event loop
558 * @param sig Signal to handle
559 * @param callback Called when signal is delivered
560 * @param u Passed to @p callback
561 * @return 0 on success, non-0 on error
563 * Note that @p callback is called from inside ev_run(), not from inside the
564 * signal handler, so the usual restrictions on signal handlers do not apply.
566 int ev_signal(ev_source
*ev
,
568 ev_signal_callback
*callback
,
573 D(("registering signal %d handler callback %p %p", sig
, (void *)callback
, u
));
576 assert(sig
<= UCHAR_MAX
);
577 if(ev
->sigpipe
[0] == -1) {
578 D(("creating signal pipe"));
580 D(("signal pipe is %d, %d", ev
->sigpipe
[0], ev
->sigpipe
[1]));
581 for(n
= 0; n
< 2; ++n
) {
582 nonblock(ev
->sigpipe
[n
]);
583 cloexec(ev
->sigpipe
[n
]);
585 if(ev_fd(ev
, ev_read
, ev
->sigpipe
[0], signal_read
, 0, "sigpipe read")) {
590 sigaddset(&ev
->sigmask
, sig
);
591 xsigprocmask(SIG_BLOCK
, &ev
->sigmask
, 0);
592 sigfd
[sig
] = ev
->sigpipe
[1];
593 ev
->signals
[sig
].callback
= callback
;
594 ev
->signals
[sig
].u
= u
;
595 sa
.sa_handler
= sighandler
;
596 sigfillset(&sa
.sa_mask
);
597 sa
.sa_flags
= SA_RESTART
;
598 xsigaction(sig
, &sa
, &ev
->signals
[sig
].oldsa
);
603 /** @brief Cancel a signal handler
604 * @param ev Event loop
605 * @param sig Signal to cancel
606 * @return 0 on success, non-0 on error
608 int ev_signal_cancel(ev_source
*ev
,
612 xsigaction(sig
, &ev
->signals
[sig
].oldsa
, 0);
613 ev
->signals
[sig
].callback
= 0;
615 sigdelset(&ev
->sigmask
, sig
);
618 xsigprocmask(SIG_UNBLOCK
, &ss
, 0);
622 /** @brief Clean up signal handling
623 * @param ev Event loop
625 * This function can be called from inside a fork. It restores signal
626 * handlers, unblocks the signals, and closes the signal pipe for @p ev.
628 void ev_signal_atfork(ev_source
*ev
) {
631 if(ev
->sigpipe
[0] != -1) {
632 /* revert any handled signals to their original state */
633 for(sig
= 1; sig
< NSIG
; ++sig
) {
634 if(ev
->signals
[sig
].callback
!= 0)
635 xsigaction(sig
, &ev
->signals
[sig
].oldsa
, 0);
637 /* and then unblock them */
638 xsigprocmask(SIG_UNBLOCK
, &ev
->sigmask
, 0);
639 /* don't want a copy of the signal pipe open inside the fork */
640 xclose(ev
->sigpipe
[0]);
641 xclose(ev
->sigpipe
[1]);
645 /* child processes ************************************************************/
647 /** @brief Called on SIGCHLD */
648 static int sigchld_callback(ev_source
*ev
,
649 int attribute((unused
)) sig
,
650 void attribute((unused
)) *u
) {
653 int status
, n
, ret
, revisit
;
657 for(n
= 0; n
< ev
->nchildren
; ++n
) {
658 r
= wait4(ev
->children
[n
].pid
,
660 ev
->children
[n
].options
| WNOHANG
,
663 ev_child_callback
*c
= ev
->children
[n
].callback
;
664 void *cu
= ev
->children
[n
].u
;
666 if(WIFEXITED(status
) || WIFSIGNALED(status
))
667 ev_child_cancel(ev
, r
);
669 if((ret
= c(ev
, r
, status
, &ru
, cu
)))
672 /* We should "never" get an ECHILD but it can in fact happen. For
673 * instance on Linux 2.4.31, and probably other versions, if someone
674 * straces a child process and then a different child process
675 * terminates, when we wait4() the trace process we will get ECHILD
676 * because it has been reparented to strace. Obviously this is a
677 * hopeless design flaw in the tracing infrastructure, but we don't
678 * want the disorder server to bomb out because of it. So we just log
679 * the problem and ignore it.
681 error(errno
, "error calling wait4 for PID %lu (broken ptrace?)",
682 (unsigned long)ev
->children
[n
].pid
);
691 /** @brief Configure event loop for child process handling
692 * @return 0 on success, non-0 on error
694 * Currently at most one event loop can handle child processes and it must be
695 * distinguished from others by calling this function on it. This could be
696 * fixed but since no process ever makes use of more than one event loop there
699 int ev_child_setup(ev_source
*ev
) {
700 D(("installing SIGCHLD handler"));
701 return ev_signal(ev
, SIGCHLD
, sigchld_callback
, 0);
704 /** @brief Wait for a child process to terminate
705 * @param ev Event loop
706 * @param pid Process ID of child
707 * @param options Options to pass to @c wait4()
708 * @param callback Called when child terminates (or possibly when it stops)
709 * @param u Passed to @p callback
710 * @return 0 on success, non-0 on error
712 * You must have called ev_child_setup() on @p ev once first.
714 int ev_child(ev_source
*ev
,
717 ev_child_callback
*callback
,
721 D(("registering child handling %ld options %d callback %p %p",
722 (long)pid
, options
, (void *)callback
, u
));
723 assert(ev
->signals
[SIGCHLD
].callback
== sigchld_callback
);
724 if(ev
->nchildren
>= ev
->nchildslots
) {
725 ev
->nchildslots
= ev
->nchildslots ?
2 * ev
->nchildslots
: 16;
726 ev
->children
= xrealloc(ev
->children
,
727 ev
->nchildslots
* sizeof (struct child
));
730 ev
->children
[n
].pid
= pid
;
731 ev
->children
[n
].options
= options
;
732 ev
->children
[n
].callback
= callback
;
733 ev
->children
[n
].u
= u
;
737 /** @brief Stop waiting for a child process
738 * @param ev Event loop
739 * @param pid Child process ID
740 * @return 0 on success, non-0 on error
742 int ev_child_cancel(ev_source
*ev
,
746 for(n
= 0; n
< ev
->nchildren
&& ev
->children
[n
].pid
!= pid
; ++n
)
748 assert(n
< ev
->nchildren
);
749 if(n
!= ev
->nchildren
- 1)
750 ev
->children
[n
] = ev
->children
[ev
->nchildren
- 1];
755 /* socket listeners ***********************************************************/
757 /** @brief State for a socket listener */
758 struct listen_state
{
759 ev_listen_callback
*callback
;
763 /** @brief Called when a listenign socket is readable */
764 static int listen_callback(ev_source
*ev
, int fd
, void *u
) {
765 const struct listen_state
*l
= u
;
768 struct sockaddr_in in
;
769 #if HAVE_STRUCT_SOCKADDR_IN6
770 struct sockaddr_in6 in6
;
772 struct sockaddr_un un
;
778 D(("callback for listener fd %d", fd
));
779 while((addrlen
= sizeof addr
),
780 (newfd
= accept(fd
, &addr
.sa
, &addrlen
)) >= 0) {
781 if((ret
= l
->callback(ev
, newfd
, &addr
.sa
, addrlen
, l
->u
)))
790 error(errno
, "error calling accept");
795 /* XXX on some systems EPROTO should be fatal, but we don't know if
796 * we're running on one of them */
797 error(errno
, "error calling accept");
801 fatal(errno
, "error calling accept");
804 if(errno
!= EINTR
&& errno
!= EAGAIN
)
805 error(errno
, "error calling accept");
809 /** @brief Listen on a socket for inbound stream connections
810 * @param ev Event source
811 * @param fd File descriptor of socket
812 * @param callback Called when a new connection arrives
813 * @param u Passed to @p callback
814 * @param what Text description of socket
815 * @return 0 on success, non-0 on error
817 int ev_listen(ev_source
*ev
,
819 ev_listen_callback
*callback
,
822 struct listen_state
*l
= xmalloc(sizeof *l
);
824 D(("registering listener fd %d callback %p %p", fd
, (void *)callback
, u
));
825 l
->callback
= callback
;
827 return ev_fd(ev
, ev_read
, fd
, listen_callback
, l
, what
);
830 /** @brief Stop listening on a socket
831 * @param ev Event loop
832 * @param fd File descriptor of socket
833 * @return 0 on success, non-0 on error
835 int ev_listen_cancel(ev_source
*ev
, int fd
) {
836 D(("cancelling listener fd %d", fd
));
837 return ev_fd_cancel(ev
, ev_read
, fd
);
840 /* buffer *********************************************************************/
842 /** @brief Buffer structure */
844 char *base
, *start
, *end
, *top
;
847 /* @brief Make sure there is @p bytes available at @c b->end */
848 static void buffer_space(struct buffer
*b
, size_t bytes
) {
849 D(("buffer_space %p %p %p %p want %lu",
850 (void *)b
->base
, (void *)b
->start
, (void *)b
->end
, (void *)b
->top
,
851 (unsigned long)bytes
));
852 if(b
->start
== b
->end
)
853 b
->start
= b
->end
= b
->base
;
854 if((size_t)(b
->top
- b
->end
) < bytes
) {
855 if((size_t)((b
->top
- b
->end
) + (b
->start
- b
->base
)) < bytes
) {
856 size_t newspace
= b
->end
- b
->start
+ bytes
, n
;
859 for(n
= 16; n
< newspace
; n
*= 2)
861 newbase
= xmalloc_noptr(n
);
862 memcpy(newbase
, b
->start
, b
->end
- b
->start
);
864 b
->end
= newbase
+ (b
->end
- b
->start
);
865 b
->top
= newbase
+ n
;
866 b
->start
= newbase
; /* must be last */
868 memmove(b
->base
, b
->start
, b
->end
- b
->start
);
869 b
->end
= b
->base
+ (b
->end
- b
->start
);
873 D(("result %p %p %p %p",
874 (void *)b
->base
, (void *)b
->start
, (void *)b
->end
, (void *)b
->top
));
877 /* readers and writers *******************************************************/
879 /** @brief State structure for a buffered writer */
881 /** @brief Sink used for writing to the buffer */
884 /** @brief Output buffer */
887 /** @brief File descriptor to write to */
890 /** @brief Set if there'll be no more output */
893 /** @brief Error/termination callback */
894 ev_error_callback
*callback
;
896 /** @brief Passed to @p callback */
899 /** @brief Parent event source */
902 /** @brief Maximum amount of time between succesful writes, 0 = don't care */
904 /** @brief Maximum amount of data to buffer, 0 = don't care */
906 /** @brief Error code to pass to @p callback (see writer_shutdown()) */
908 /** @brief Timeout handle for @p timebound (or 0) */
909 ev_timeout_handle timeout
;
911 /** @brief Description of this writer */
914 /** @brief Tied reader or 0 */
917 /** @brief Set when abandoned */
921 /** @brief State structure for a buffered reader */
923 /** @brief Input buffer */
925 /** @brief File descriptor read from */
927 /** @brief Called when new data is available */
928 ev_reader_callback
*callback
;
929 /** @brief Called on error and shutdown */
930 ev_error_callback
*error_callback
;
931 /** @brief Passed to @p callback and @p error_callback */
933 /** @brief Parent event loop */
935 /** @brief Set when EOF is detected */
937 /** @brief Error code to pass to error callback */
939 /** @brief Tied writer or NULL */
943 /* buffered writer ************************************************************/
945 /** @brief Shut down the writer
947 * This is called to shut down a writer. The error callback is not called
948 * through any other path. Also we do not cancel @p fd from anywhere else,
949 * though we might disable it.
951 * It has the signature of a timeout callback so that it can be called from a
954 * Calls @p callback with @p w->syntherr as the error code (which might be 0).
956 static int writer_shutdown(ev_source
*ev
,
957 const attribute((unused
)) struct timeval
*now
,
962 return 0; /* already shut down */
963 D(("writer_shutdown fd=%d error=%d", w
->fd
, w
->error
));
964 ev_timeout_cancel(ev
, w
->timeout
);
965 ev_fd_cancel(ev
, ev_write
, w
->fd
);
968 D(("found a tied reader"));
969 /* If there is a reader still around we just untie it */
970 w
->reader
->writer
= 0;
971 shutdown(w
->fd
, SHUT_WR
); /* there'll be no more writes */
973 D(("no tied reader"));
974 /* There's no reader so we are free to close the FD */
978 return w
->callback(ev
, w
->error
, w
->u
);
981 /** @brief Called when a writer's @p timebound expires */
982 static int writer_timebound_exceeded(ev_source
*ev
,
983 const struct timeval
*now
,
985 ev_writer
*const w
= u
;
989 error(0, "abandoning writer '%s' because no writes within %ds",
990 w
->what
, w
->timebound
);
991 w
->error
= ETIMEDOUT
;
993 return writer_shutdown(ev
, now
, u
);
996 /** @brief Set the time bound callback (if not set already) */
997 static void writer_set_timebound(ev_writer
*w
) {
998 if(w
->timebound
&& !w
->timeout
) {
1000 ev_source
*const ev
= w
->ev
;
1002 xgettimeofday(&when
, 0);
1003 when
.tv_sec
+= w
->timebound
;
1004 ev_timeout(ev
, &w
->timeout
, &when
, writer_timebound_exceeded
, w
);
1008 /** @brief Called when a writer's file descriptor is writable */
1009 static int writer_callback(ev_source
*ev
, int fd
, void *u
) {
1010 ev_writer
*const w
= u
;
1013 n
= write(fd
, w
->b
.start
, w
->b
.end
- w
->b
.start
);
1014 D(("callback for writer fd %d, %ld bytes, n=%d, errno=%d",
1015 fd
, (long)(w
->b
.end
- w
->b
.start
), n
, errno
));
1017 /* Consume bytes from the buffer */
1019 /* Suppress any outstanding timeout */
1020 ev_timeout_cancel(ev
, w
->timeout
);
1022 if(w
->b
.start
== w
->b
.end
) {
1023 /* The buffer is empty */
1025 /* We're done, we can shut down this writer */
1027 return writer_shutdown(ev
, 0, w
);
1029 /* There might be more to come but we don't need writer_callback() to
1030 * be called for the time being */
1031 ev_fd_disable(ev
, ev_write
, fd
);
1033 /* The buffer isn't empty, set a timeout so we give up if we don't manage
1034 * to write some more within a reasonable time */
1035 writer_set_timebound(w
);
1043 return writer_shutdown(ev
, 0, w
);
1049 /** @brief Write bytes to a writer's buffer
1051 * This is the sink write callback.
1053 * Calls ev_fd_enable() if necessary (i.e. if the buffer was empty but
1056 static int ev_writer_write(struct sink
*sk
, const void *s
, int n
) {
1057 ev_writer
*w
= (ev_writer
*)sk
;
1060 return 0; /* avoid silliness */
1062 error(0, "ev_writer_write on %s after shutdown", w
->what
);
1063 if(w
->spacebound
&& w
->b
.end
- w
->b
.start
+ n
> w
->spacebound
) {
1064 /* The new buffer contents will exceed the space bound. We assume that the
1065 * remote client has gone away and TCP hasn't noticed yet, or that it's got
1066 * hopelessly stuck. */
1069 error(0, "abandoning writer '%s' because buffer has reached %td bytes",
1070 w
->what
, w
->b
.end
- w
->b
.start
);
1071 ev_fd_disable(w
->ev
, ev_write
, w
->fd
);
1073 return ev_timeout(w
->ev
, 0, 0, writer_shutdown
, w
);
1077 /* Make sure there is space */
1078 buffer_space(&w
->b
, n
);
1079 /* If the buffer was formerly empty then we'll need to re-enable the FD */
1080 if(w
->b
.start
== w
->b
.end
)
1081 ev_fd_enable(w
->ev
, ev_write
, w
->fd
);
1082 memcpy(w
->b
.end
, s
, n
);
1084 /* Arrange a timeout if there wasn't one set already */
1085 writer_set_timebound(w
);
1089 /** @brief Create a new buffered writer
1090 * @param ev Event loop
1091 * @param fd File descriptor to write to
1092 * @param callback Called if an error occurs and when finished
1093 * @param u Passed to @p callback
1094 * @param what Text description
1095 * @return New writer or @c NULL
1097 * Writers own their file descriptor and close it when they have finished with
1100 * If you pass the same fd to a reader and writer, you must tie them together
1103 ev_writer
*ev_writer_new(ev_source
*ev
,
1105 ev_error_callback
*callback
,
1108 ev_writer
*w
= xmalloc(sizeof *w
);
1110 D(("registering writer fd %d callback %p %p", fd
, (void *)callback
, u
));
1111 w
->s
.write
= ev_writer_write
;
1113 w
->callback
= callback
;
1116 w
->timebound
= 10 * 60;
1117 w
->spacebound
= 512 * 1024;
1119 if(ev_fd(ev
, ev_write
, fd
, writer_callback
, w
, what
))
1121 /* Buffer is initially empty so we don't want a callback */
1122 ev_fd_disable(ev
, ev_write
, fd
);
1126 /** @brief Get/set the time bound
1128 * @param new_time_bound New bound or -1 for no change
1129 * @return Latest time bound
1131 * If @p new_time_bound is negative then the current time bound is returned.
1132 * Otherwise it is set and the new value returned.
1134 * The time bound is the number of seconds allowed between writes. If it takes
1135 * longer than this to flush a buffer then the peer will be assumed to be dead
1136 * and an error will be synthesized. 0 means "don't care". The default time
1137 * bound is 10 minutes.
1139 * Note that this value does not take into account kernel buffering and
1142 int ev_writer_time_bound(ev_writer
*w
,
1143 int new_time_bound
) {
1144 if(new_time_bound
>= 0)
1145 w
->timebound
= new_time_bound
;
1146 return w
->timebound
;
1149 /** @brief Get/set the space bound
1151 * @param new_space_bound New bound or -1 for no change
1152 * @return Latest space bound
1154 * If @p new_space_bound is negative then the current space bound is returned.
1155 * Otherwise it is set and the new value returned.
1157 * The space bound is the number of bytes allowed between in the buffer. If
1158 * the buffer exceeds this size an error will be synthesized. 0 means "don't
1159 * care". The default space bound is 512Kbyte.
1161 * Note that this value does not take into account kernel buffering.
1163 int ev_writer_space_bound(ev_writer
*w
,
1164 int new_space_bound
) {
1165 if(new_space_bound
>= 0)
1166 w
->spacebound
= new_space_bound
;
1167 return w
->spacebound
;
1170 /** @brief Return the sink associated with a writer
1172 * @return Pointer to sink
1174 * Writing to the sink will arrange for those bytes to be written to the file
1175 * descriptor as and when it is writable.
1177 struct sink
*ev_writer_sink(ev_writer
*w
) {
1179 fatal(0, "ev_write_sink called with null writer");
1183 /** @brief Close a writer
1184 * @param w Writer to close
1185 * @return 0 on success, non-0 on error
1187 * Close a writer. No more bytes should be written to its sink.
1189 * When the last byte has been written the callback will be called with an
1190 * error code of 0. It is guaranteed that this will NOT happen before
1191 * ev_writer_close() returns (although the file descriptor for the writer might
1192 * be cancelled by the time it returns).
1194 int ev_writer_close(ev_writer
*w
) {
1195 D(("close writer fd %d", w
->fd
));
1197 return 0; /* already closed */
1199 if(w
->b
.start
== w
->b
.end
) {
1200 /* We're already finished */
1201 w
->error
= 0; /* no error */
1202 return ev_timeout(w
->ev
, 0, 0, writer_shutdown
, w
);
1207 /** @brief Attempt to flush a writer
1208 * @param w Writer to flush
1209 * @return 0 on success, non-0 on error
1211 * Does a speculative write of any buffered data. Does not block if it cannot
1214 int ev_writer_flush(ev_writer
*w
) {
1215 return writer_callback(w
->ev
, w
->fd
, w
);
1218 /* buffered reader ************************************************************/
1220 /** @brief Shut down a reader
1222 * This is the only path through which we cancel and close the file descriptor.
1223 * As with the writer case it is given timeout signature to allow it be
1224 * deferred to the next iteration of the event loop.
1226 * We only call @p error_callback if @p error is nonzero (unlike the writer
1229 static int reader_shutdown(ev_source
*ev
,
1230 const attribute((unused
)) struct timeval
*now
,
1232 ev_reader
*const r
= u
;
1235 return 0; /* already shut down */
1236 D(("reader_shutdown fd=%d", r
->fd
));
1237 ev_fd_cancel(ev
, ev_read
, r
->fd
);
1240 D(("found a tied writer"));
1241 /* If there is a writer still around we just untie it */
1242 r
->writer
->reader
= 0;
1243 shutdown(r
->fd
, SHUT_RD
); /* there'll be no more reads */
1245 D(("no tied writer found"));
1246 /* There's no writer so we are free to close the FD */
1251 return r
->error_callback(ev
, r
->error
, r
->u
);
1256 /** @brief Called when a reader's @p fd is readable */
1257 static int reader_callback(ev_source
*ev
, int fd
, void *u
) {
1261 buffer_space(&r
->b
, 1);
1262 n
= read(fd
, r
->b
.end
, r
->b
.top
- r
->b
.end
);
1263 D(("read fd %d buffer %d returned %d errno %d",
1264 fd
, (int)(r
->b
.top
- r
->b
.end
), n
, errno
));
1267 return r
->callback(ev
, r
, r
->b
.start
, r
->b
.end
- r
->b
.start
, 0, r
->u
);
1269 /* No more read callbacks needed */
1270 ev_fd_disable(r
->ev
, ev_read
, r
->fd
);
1271 ev_timeout(r
->ev
, 0, 0, reader_shutdown
, r
);
1272 /* Pass the remaining data and an eof indicator to the user */
1273 return r
->callback(ev
, r
, r
->b
.start
, r
->b
.end
- r
->b
.start
, 1, r
->u
);
1280 /* Fatal error, kill the reader now */
1282 return reader_shutdown(ev
, 0, r
);
1288 /** @brief Create a new buffered reader
1289 * @param ev Event loop
1290 * @param fd File descriptor to read from
1291 * @param callback Called when new data is available
1292 * @param error_callback Called if an error occurs
1293 * @param u Passed to callbacks
1294 * @param what Text description
1295 * @return New reader or @c NULL
1297 * Readers own their fd and close it when they are finished with it.
1299 * If you pass the same fd to a reader and writer, you must tie them together
1302 ev_reader
*ev_reader_new(ev_source
*ev
,
1304 ev_reader_callback
*callback
,
1305 ev_error_callback
*error_callback
,
1308 ev_reader
*r
= xmalloc(sizeof *r
);
1310 D(("registering reader fd %d callback %p %p %p",
1311 fd
, (void *)callback
, (void *)error_callback
, u
));
1313 r
->callback
= callback
;
1314 r
->error_callback
= error_callback
;
1317 if(ev_fd(ev
, ev_read
, fd
, reader_callback
, r
, what
))
1322 void ev_reader_buffer(ev_reader
*r
, size_t nbytes
) {
1323 buffer_space(&r
->b
, nbytes
- (r
->b
.end
- r
->b
.start
));
1326 /** @brief Consume @p n bytes from the reader's buffer
1328 * @param n Number of bytes to consume
1330 * Tells the reader than the next @p n bytes have been dealt with and can now
1333 void ev_reader_consume(ev_reader
*r
, size_t n
) {
1337 /** @brief Cancel a reader
1339 * @return 0 on success, non-0 on error
1341 * No further callbacks will be made, and the FD will be closed (in a later
1342 * iteration of the event loop).
1344 int ev_reader_cancel(ev_reader
*r
) {
1345 D(("cancel reader fd %d", r
->fd
));
1347 return 0; /* already thoroughly cancelled */
1348 ev_fd_disable(r
->ev
, ev_read
, r
->fd
);
1349 return ev_timeout(r
->ev
, 0, 0, reader_shutdown
, r
);
1352 /** @brief Temporarily disable a reader
1354 * @return 0 on success, non-0 on error
1356 * No further callbacks for this reader will be made. Re-enable with
1357 * ev_reader_enable().
1359 int ev_reader_disable(ev_reader
*r
) {
1360 D(("disable reader fd %d", r
->fd
));
1361 return ev_fd_disable(r
->ev
, ev_read
, r
->fd
);
1364 /** @brief Called from ev_run() for ev_reader_incomplete() */
1365 static int reader_continuation(ev_source
attribute((unused
)) *ev
,
1366 const attribute((unused
)) struct timeval
*now
,
1370 D(("reader continuation callback fd %d", r
->fd
));
1371 /* If not at EOF turn the FD back on */
1373 if(ev_fd_enable(r
->ev
, ev_read
, r
->fd
))
1375 /* We're already in a timeout callback so there's no reason we can't call the
1376 * user callback directly (compare ev_reader_enable()). */
1377 return r
->callback(ev
, r
, r
->b
.start
, r
->b
.end
- r
->b
.start
, r
->eof
, r
->u
);
1380 /** @brief Arrange another callback
1382 * @return 0 on success, non-0 on error
1384 * Indicates that the reader can process more input but would like to yield to
1385 * other clients of the event loop. Input will be disabled but it will be
1386 * re-enabled on the next iteration of the event loop and the read callback
1387 * will be called again (even if no further bytes are available).
1389 int ev_reader_incomplete(ev_reader
*r
) {
1390 if(ev_fd_disable(r
->ev
, ev_read
, r
->fd
)) return -1;
1391 return ev_timeout(r
->ev
, 0, 0, reader_continuation
, r
);
1394 static int reader_enabled(ev_source
*ev
,
1395 const attribute((unused
)) struct timeval
*now
,
1399 D(("reader enabled callback fd %d", r
->fd
));
1400 return r
->callback(ev
, r
, r
->b
.start
, r
->b
.end
- r
->b
.start
, r
->eof
, r
->u
);
1403 /** @brief Re-enable reading
1405 * @return 0 on success, non-0 on error
1407 * If there is unconsumed data then you get a callback next time round the
1408 * event loop even if nothing new has been read.
1410 * The idea is in your read callback you come across a line (or whatever) that
1411 * can't be processed immediately. So you set up processing and disable
1412 * reading with ev_reader_disable(). Later when you finish processing you
1413 * re-enable. You'll automatically get another callback directly from the
1414 * event loop (i.e. not from inside ev_reader_enable()) so you can handle the
1415 * next line (or whatever) if the whole thing has in fact already arrived.
1417 * The difference between this process and calling ev_reader_incomplete() is
1418 * ev_reader_incomplete() deals with the case where you can process now but
1419 * would rather yield to other clients of the event loop, while using
1420 * ev_reader_disable() and ev_reader_enable() deals with the case where you
1421 * cannot process input yet because some other process is actually not
1424 int ev_reader_enable(ev_reader
*r
) {
1425 D(("enable reader fd %d", r
->fd
));
1427 /* First if we're not at EOF then we re-enable reading */
1429 if(ev_fd_enable(r
->ev
, ev_read
, r
->fd
))
1431 /* Arrange another callback next time round the event loop */
1432 return ev_timeout(r
->ev
, 0, 0, reader_enabled
, r
);
1435 /** @brief Tie a reader and a writer together
1438 * @return 0 on success, non-0 on error
1440 * This function must be called if @p r and @p w share a file descritptor.
1442 int ev_tie(ev_reader
*r
, ev_writer
*w
) {
1443 assert(r
->writer
== 0);
1444 assert(w
->reader
== 0);