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