[core] isolate fdevent processing
[lighttpd.git] / src / fdevent.c
blobaa59942e046ab3b973523b8c2457ab5d3f4b502b
1 #include "first.h"
3 #include "fdevent_impl.h"
4 #include "fdevent.h"
5 #include "base.h"
6 #include "buffer.h"
7 #include "log.h"
9 #include <sys/types.h>
10 #include <sys/wait.h>
11 #include "sys-socket.h"
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <time.h>
20 #ifdef SOCK_CLOEXEC
21 static int use_sock_cloexec;
22 #endif
23 #ifdef SOCK_NONBLOCK
24 static int use_sock_nonblock;
25 #endif
27 int fdevent_config(server *srv) {
28 static const struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
30 /* - epoll is most reliable
31 * - select works everywhere
33 #ifdef FDEVENT_USE_LINUX_EPOLL
34 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
35 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "epoll" },
36 #endif
37 #ifdef FDEVENT_USE_SOLARIS_PORT
38 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
39 #endif
40 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
41 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
42 #endif
43 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
44 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
45 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
46 #endif
47 #ifdef FDEVENT_USE_POLL
48 { FDEVENT_HANDLER_POLL, "poll" },
49 #endif
50 #ifdef FDEVENT_USE_SELECT
51 { FDEVENT_HANDLER_SELECT, "select" },
52 #endif
53 #ifdef FDEVENT_USE_LIBEV
54 { FDEVENT_HANDLER_LIBEV, "libev" },
55 #endif
56 { FDEVENT_HANDLER_UNSET, NULL }
59 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
60 /* choose a good default
62 * the event_handler list is sorted by 'goodness'
63 * taking the first available should be the best solution
65 srv->event_handler = event_handlers[0].et;
67 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
68 log_error_write(srv, __FILE__, __LINE__, "s",
69 "sorry, there is no event handler for this system");
71 return -1;
74 buffer_copy_string(srv->srvconf.event_handler, event_handlers[0].name);
75 } else {
77 * User override
80 for (size_t i = 0; event_handlers[i].name; i++) {
81 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
82 srv->event_handler = event_handlers[i].et;
83 break;
87 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
88 log_error_write(srv, __FILE__, __LINE__, "sb",
89 "the selected event-handler in unknown or not supported:",
90 srv->srvconf.event_handler );
92 return -1;
96 #ifdef FDEVENT_USE_SELECT
97 if (srv->event_handler == FDEVENT_HANDLER_SELECT) {
98 /* select limits itself
100 * as it is a hard limit and will lead to a segfault we add some safety
101 * */
102 srv->max_fds = FD_SETSIZE - 200;
104 else
105 #endif
107 srv->max_fds = 4096;
110 return 0;
113 const char * fdevent_show_event_handlers(void) {
114 return
115 "\nEvent Handlers:\n\n"
116 #ifdef FDEVENT_USE_SELECT
117 "\t+ select (generic)\n"
118 #else
119 "\t- select (generic)\n"
120 #endif
121 #ifdef FDEVENT_USE_POLL
122 "\t+ poll (Unix)\n"
123 #else
124 "\t- poll (Unix)\n"
125 #endif
126 #ifdef FDEVENT_USE_LINUX_EPOLL
127 "\t+ epoll (Linux)\n"
128 #else
129 "\t- epoll (Linux)\n"
130 #endif
131 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
132 "\t+ /dev/poll (Solaris)\n"
133 #else
134 "\t- /dev/poll (Solaris)\n"
135 #endif
136 #ifdef FDEVENT_USE_SOLARIS_PORT
137 "\t+ eventports (Solaris)\n"
138 #else
139 "\t- eventports (Solaris)\n"
140 #endif
141 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
142 "\t+ kqueue (FreeBSD)\n"
143 #else
144 "\t- kqueue (FreeBSD)\n"
145 #endif
146 #ifdef FDEVENT_USE_LIBEV
147 "\t+ libev (generic)\n"
148 #else
149 "\t- libev (generic)\n"
150 #endif
154 fdevents *fdevent_init(server *srv) {
155 fdevents *ev;
156 int type = srv->event_handler;
157 size_t maxfds;
159 #ifdef SOCK_CLOEXEC
160 /* Test if SOCK_CLOEXEC is supported by kernel.
161 * Linux kernels < 2.6.27 might return EINVAL if SOCK_CLOEXEC used
162 * https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=529929
163 * http://www.linksysinfo.org/index.php?threads/lighttpd-no-longer-starts-toastman-1-28-0510-7.73132/
164 * Test if SOCK_NONBLOCK is ignored by kernel on sockets.
165 * (reported on Android running a custom ROM)
166 * https://redmine.lighttpd.net/issues/2883
168 int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
169 if (fd >= 0) {
170 int flags = fcntl(fd, F_GETFL, 0);
171 use_sock_nonblock = (-1 != flags && (flags & O_NONBLOCK));
172 use_sock_cloexec = 1;
173 close(fd);
175 #endif
177 #ifdef FDEVENT_USE_SELECT
178 if (type == FDEVENT_HANDLER_SELECT) {
179 if (srv->max_fds > (int)FD_SETSIZE - 200) {
180 srv->max_fds = (int)FD_SETSIZE - 200;
183 #endif
184 maxfds = srv->max_fds + 1; /*(+1 for event-handler fd)*/
186 ev = calloc(1, sizeof(*ev));
187 force_assert(NULL != ev);
188 ev->srv = srv;
189 ev->fdarray = calloc(maxfds, sizeof(*ev->fdarray));
190 if (NULL == ev->fdarray) {
191 log_error_write(srv, __FILE__, __LINE__, "SDS",
192 "server.max-fds too large? (", maxfds-1, ")");
193 free(ev);
194 return NULL;
196 ev->maxfds = maxfds;
198 switch(type) {
199 #ifdef FDEVENT_USE_POLL
200 case FDEVENT_HANDLER_POLL:
201 if (0 == fdevent_poll_init(ev)) return ev;
202 break;
203 #endif
204 #ifdef FDEVENT_USE_SELECT
205 case FDEVENT_HANDLER_SELECT:
206 if (0 == fdevent_select_init(ev)) return ev;
207 break;
208 #endif
209 #ifdef FDEVENT_USE_LINUX_EPOLL
210 case FDEVENT_HANDLER_LINUX_SYSEPOLL:
211 if (0 == fdevent_linux_sysepoll_init(ev)) return ev;
212 break;
213 #endif
214 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
215 case FDEVENT_HANDLER_SOLARIS_DEVPOLL:
216 if (0 == fdevent_solaris_devpoll_init(ev)) return ev;
217 break;
218 #endif
219 #ifdef FDEVENT_USE_SOLARIS_PORT
220 case FDEVENT_HANDLER_SOLARIS_PORT:
221 if (0 == fdevent_solaris_port_init(ev)) return ev;
222 break;
223 #endif
224 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
225 case FDEVENT_HANDLER_FREEBSD_KQUEUE:
226 if (0 == fdevent_freebsd_kqueue_init(ev)) return ev;
227 break;
228 #endif
229 #ifdef FDEVENT_USE_LIBEV
230 case FDEVENT_HANDLER_LIBEV:
231 if (0 == fdevent_libev_init(ev)) return ev;
232 break;
233 #endif
234 case FDEVENT_HANDLER_UNSET:
235 default:
236 break;
239 free(ev->fdarray);
240 free(ev);
242 log_error_write(srv, __FILE__, __LINE__, "sBS",
243 "event-handler failed:", srv->srvconf.event_handler, "; try to set server.event-handler = \"poll\" or \"select\"");
244 return NULL;
247 void fdevent_free(fdevents *ev) {
248 size_t i;
249 if (!ev) return;
251 if (ev->free) ev->free(ev);
253 for (i = 0; i < ev->maxfds; i++) {
254 /* (fdevent_sched_run() should already have been run,
255 * but take reasonable precautions anyway) */
256 if (ev->fdarray[i])
257 free((fdnode *)((uintptr_t)ev->fdarray[i] & ~0x3));
260 free(ev->fdarray);
261 free(ev);
264 int fdevent_reset(fdevents *ev) {
265 int rc = (NULL != ev->reset) ? ev->reset(ev) : 0;
266 if (-1 == rc) {
267 log_error_write(ev->srv, __FILE__, __LINE__, "sBS",
268 "event-handler failed:", ev->srv->srvconf.event_handler, "; try to set server.event-handler = \"poll\" or \"select\"");
270 return rc;
273 static fdnode *fdnode_init(void) {
274 return calloc(1, sizeof(fdnode));
277 static void fdnode_free(fdnode *fdn) {
278 free(fdn);
281 void fdevent_register(fdevents *ev, int fd, fdevent_handler handler, void *ctx) {
282 fdnode *fdn = ev->fdarray[fd] = fdnode_init();
283 force_assert(NULL != fdn);
284 fdn->handler = handler;
285 fdn->fd = fd;
286 fdn->ctx = ctx;
287 fdn->events = 0;
288 fdn->fde_ndx = -1;
289 #ifdef FDEVENT_USE_LIBEV
290 fdn->handler_ctx = NULL;
291 #endif
294 void fdevent_unregister(fdevents *ev, int fd) {
295 fdnode *fdn = ev->fdarray[fd];
296 if ((uintptr_t)fdn & 0x3) return; /*(should not happen)*/
297 ev->fdarray[fd] = NULL;
298 fdnode_free(fdn);
301 void fdevent_sched_close(fdevents *ev, int fd, int issock) {
302 fdnode *fdn = ev->fdarray[fd];
303 if ((uintptr_t)fdn & 0x3) return;
304 ev->fdarray[fd] = (fdnode *)((uintptr_t)fdn | (issock ? 0x1 : 0x2));
305 fdn->ctx = ev->pendclose;
306 ev->pendclose = fdn;
309 static void fdevent_sched_run(fdevents *ev) {
310 server *srv = ev->srv;
311 for (fdnode *fdn = ev->pendclose; fdn; ) {
312 int fd, rc;
313 fdnode *fdn_tmp;
314 #ifdef _WIN32
315 rc = (uintptr_t)fdn & 0x3;
316 #endif
317 fdn = (fdnode *)((uintptr_t)fdn & ~0x3);
318 fd = fdn->fd;
319 #ifdef _WIN32
320 if (rc == 0x1) {
321 rc = closesocket(fd);
323 else if (rc == 0x2) {
324 rc = close(fd);
326 #else
327 rc = close(fd);
328 #endif
330 if (0 != rc) {
331 log_error_write(srv, __FILE__, __LINE__, "sds", "close failed ", fd, strerror(errno));
333 else {
334 --srv->cur_fds;
337 fdn_tmp = fdn;
338 fdn = (fdnode *)fdn->ctx; /* next */
339 /*(fdevent_unregister)*/
340 fdnode_free(fdn_tmp);
341 ev->fdarray[fd] = NULL;
343 ev->pendclose = NULL;
346 int fdevent_event_get_interest(const fdevents *ev, int fd) {
347 return fd >= 0 ? ev->fdarray[fd]->events : 0;
350 static void fdevent_fdnode_event_del(fdevents *ev, fdnode *fdn) {
351 if (-1 == fdn->fde_ndx) return;
352 if (0 == ev->event_del(ev, fdn)) {
353 fdn->fde_ndx = -1;
354 fdn->events = 0;
356 else {
357 log_error_write(ev->srv, __FILE__, __LINE__, "SS",
358 "fdevent event_del failed: ", strerror(errno));
362 static void fdevent_fdnode_event_set(fdevents *ev, fdnode *fdn, int events) {
363 /*(Note: skips registering with kernel if initial events is 0,
364 * so caller should pass non-zero events for initial registration.
365 * If never registered due to never being called with non-zero events,
366 * then FDEVENT_HUP or FDEVENT_ERR will never be returned.) */
367 if (fdn->events == events) return;/*(no change; nothing to do)*/
369 if (0 == ev->event_set(ev, fdn, events))
370 fdn->events = events;
371 else
372 log_error_write(ev->srv, __FILE__, __LINE__, "SS",
373 "fdevent event_set failed: ", strerror(errno));
376 void fdevent_event_del(fdevents *ev, int fd) {
377 if (-1 != fd) {
378 fdnode *fdn = ev->fdarray[fd];
379 if ((uintptr_t)fdn & 0x3) return;
380 fdevent_fdnode_event_del(ev, fdn);
384 void fdevent_event_set(fdevents *ev, int fd, int events) {
385 if (-1 != fd) fdevent_fdnode_event_set(ev, ev->fdarray[fd], events);
388 void fdevent_event_add(fdevents *ev, int fd, int event) {
389 if (-1 != fd) {
390 fdnode *fdn = ev->fdarray[fd];
391 fdevent_fdnode_event_set(ev, fdn, (fdn->events | event));
395 void fdevent_event_clr(fdevents *ev, int fd, int event) {
396 if (-1 != fd) {
397 fdnode *fdn = ev->fdarray[fd];
398 fdevent_fdnode_event_set(ev, fdn, (fdn->events & ~event));
402 int fdevent_poll(fdevents *ev, int timeout_ms) {
403 int n = ev->poll(ev, timeout_ms);
404 if (n >= 0)
405 fdevent_sched_run(ev);
406 else if (errno != EINTR)
407 log_error_write(ev->srv, __FILE__, __LINE__, "SS",
408 "fdevent_poll failed: ", strerror(errno));
409 return n;
412 void fdevent_setfd_cloexec(int fd) {
413 #ifdef FD_CLOEXEC
414 if (fd < 0) return;
415 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
416 #else
417 UNUSED(fd);
418 #endif
421 void fdevent_clrfd_cloexec(int fd) {
422 #ifdef FD_CLOEXEC
423 if (fd >= 0) force_assert(-1 != fcntl(fd, F_SETFD, 0));
424 #else
425 UNUSED(fd);
426 #endif
429 int fdevent_fcntl_set_nb(fdevents *ev, int fd) {
430 UNUSED(ev);
431 #ifdef O_NONBLOCK
432 return fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR);
433 #else
434 UNUSED(fd);
435 return 0;
436 #endif
439 int fdevent_fcntl_set_nb_cloexec(fdevents *ev, int fd) {
440 fdevent_setfd_cloexec(fd);
441 return fdevent_fcntl_set_nb(ev, fd);
444 int fdevent_fcntl_set_nb_cloexec_sock(fdevents *ev, int fd) {
445 #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
446 if (use_sock_cloexec && use_sock_nonblock)
447 return 0;
448 #endif
449 return fdevent_fcntl_set_nb_cloexec(ev, fd);
452 int fdevent_socket_cloexec(int domain, int type, int protocol) {
453 int fd;
454 #ifdef SOCK_CLOEXEC
455 if (use_sock_cloexec)
456 return socket(domain, type | SOCK_CLOEXEC, protocol);
457 #endif
458 if (-1 != (fd = socket(domain, type, protocol))) {
459 #ifdef FD_CLOEXEC
460 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
461 #endif
463 return fd;
466 int fdevent_socket_nb_cloexec(int domain, int type, int protocol) {
467 int fd;
468 #ifdef SOCK_CLOEXEC
469 if (use_sock_cloexec && use_sock_nonblock)
470 return socket(domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol);
471 #endif
472 if (-1 != (fd = socket(domain, type, protocol))) {
473 #ifdef FD_CLOEXEC
474 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
475 #endif
476 #ifdef O_NONBLOCK
477 force_assert(-1 != fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR));
478 #endif
480 return fd;
483 #ifndef O_NOCTTY
484 #define O_NOCTTY 0
485 #endif
487 int fdevent_open_cloexec(const char *pathname, int flags, mode_t mode) {
488 #ifdef O_CLOEXEC
489 return open(pathname, flags | O_CLOEXEC | O_NOCTTY, mode);
490 #else
491 int fd = open(pathname, flags | O_NOCTTY, mode);
492 #ifdef FD_CLOEXEC
493 if (fd != -1)
494 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
495 #endif
496 return fd;
497 #endif
501 int fdevent_open_devnull(void) {
502 #if defined(_WIN32)
503 return fdevent_open_cloexec("nul", O_RDWR, 0);
504 #else
505 return fdevent_open_cloexec("/dev/null", O_RDWR, 0);
506 #endif
510 int fdevent_open_dirname(char *path) {
511 /*(handle special cases of no dirname or dirname is root directory)*/
512 char * const c = strrchr(path, '/');
513 const char * const dname = (NULL != c ? c == path ? "/" : path : ".");
514 int dfd;
515 int flags = O_RDONLY;
516 #ifdef O_DIRECTORY
517 flags |= O_DIRECTORY;
518 #endif
519 if (NULL != c) *c = '\0';
520 dfd = fdevent_open_cloexec(dname, flags, 0);
521 if (NULL != c) *c = '/';
522 return dfd;
526 int fdevent_accept_listenfd(int listenfd, struct sockaddr *addr, size_t *addrlen) {
527 int fd;
528 socklen_t len = (socklen_t) *addrlen;
530 #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
531 #if defined(__NetBSD__)
532 const int sock_cloexec = 1;
533 fd = paccept(listenfd, addr, &len, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
534 #else
535 int sock_cloexec = use_sock_cloexec;
536 if (sock_cloexec) {
537 fd = accept4(listenfd, addr, &len, SOCK_CLOEXEC | SOCK_NONBLOCK);
538 if (fd >= 0) {
539 if (!use_sock_nonblock) {
540 if (0 != fdevent_fcntl_set_nb(NULL, fd)) {
541 close(fd);
542 fd = -1;
545 } else if (errno == ENOSYS || errno == ENOTSUP) {
546 fd = accept(listenfd, addr, &len);
547 sock_cloexec = 0;
550 else {
551 fd = accept(listenfd, addr, &len);
553 #endif
554 #else
555 const int sock_cloexec = 0;
556 fd = accept(listenfd, addr, &len);
557 #endif
559 if (fd >= 0) {
560 *addrlen = (size_t)len;
561 if (!sock_cloexec && 0 != fdevent_fcntl_set_nb_cloexec(NULL, fd)) {
562 close(fd);
563 fd = -1;
566 return fd;
570 #ifdef __APPLE__
571 #include <crt_externs.h>
572 #define environ (* _NSGetEnviron())
573 #else
574 extern char **environ;
575 #endif
576 char ** fdevent_environ (void) { return environ; }
579 #ifdef FD_CLOEXEC
580 static int fdevent_dup2_close_clrfd_cloexec(int oldfd, int newfd) {
581 if (oldfd >= 0) {
582 if (oldfd != newfd) {
583 force_assert(oldfd > STDERR_FILENO);
584 if (newfd != dup2(oldfd, newfd)) return -1;
586 else {
587 fdevent_clrfd_cloexec(newfd);
590 return newfd;
592 #else
593 static int fdevent_dup2_close_clrfd_cloexec(int oldfd, int newfd, int reuse) {
594 if (oldfd >= 0) {
595 if (oldfd != newfd) {
596 force_assert(oldfd > STDERR_FILENO);
597 if (newfd != dup2(oldfd, newfd)) return -1;
598 if (!reuse) close(oldfd);
601 return newfd;
603 #endif
606 int fdevent_set_stdin_stdout_stderr(int fdin, int fdout, int fderr) {
607 #ifdef FD_CLOEXEC
608 if (STDIN_FILENO != fdevent_dup2_close_clrfd_cloexec(fdin, STDIN_FILENO))
609 return -1;
610 if (STDOUT_FILENO != fdevent_dup2_close_clrfd_cloexec(fdout, STDOUT_FILENO))
611 return -1;
612 if (STDERR_FILENO != fdevent_dup2_close_clrfd_cloexec(fderr, STDERR_FILENO))
613 return -1;
614 #else
615 if (STDIN_FILENO != fdevent_dup2_close_clrfd_cloexec(fdin, STDIN_FILENO,
616 fdin == fdout
617 || fdin == fderr))
618 return -1;
619 if (STDOUT_FILENO != fdevent_dup2_close_clrfd_cloexec(fdout, STDOUT_FILENO,
620 fdout == fderr))
621 return -1;
622 if (STDERR_FILENO != fdevent_dup2_close_clrfd_cloexec(fderr, STDERR_FILENO,
624 return -1;
625 #endif
627 return 0;
631 #include <stdio.h> /* perror() */
632 #include <signal.h> /* signal() */
634 pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd) {
635 #ifdef HAVE_FORK
637 pid_t pid = fork();
638 if (0 != pid) return pid; /* parent (pid > 0) or fork() error (-1 == pid) */
640 /* child (0 == pid) */
642 if (-1 != dfd) {
643 if (0 != fchdir(dfd))
644 _exit(errno);
645 close(dfd);
648 if (0 != fdevent_set_stdin_stdout_stderr(fdin, fdout, fderr)) _exit(errno);
649 #ifdef FD_CLOEXEC
650 /*(might not be sufficient for open fds, but modern OS have FD_CLOEXEC)*/
651 for (int i = 3; i < 256; ++i) close(i);
652 #endif
654 /* reset_signals which may have been ignored (SIG_IGN) */
655 #ifdef SIGTTOU
656 signal(SIGTTOU, SIG_DFL);
657 #endif
658 #ifdef SIGTTIN
659 signal(SIGTTIN, SIG_DFL);
660 #endif
661 #ifdef SIGTSTP
662 signal(SIGTSTP, SIG_DFL);
663 #endif
664 signal(SIGPIPE, SIG_DFL);
666 execve(name, argv, envp ? envp : environ);
668 if (0 == memcmp(argv[0], "/bin/sh", sizeof("/bin/sh")-1)
669 && argv[1] && 0 == memcmp(argv[1], "-c", sizeof("-c")-1))
670 perror(argv[2]);
671 else
672 perror(argv[0]);
673 _exit(errno);
675 #else
677 UNUSED(name);
678 UNUSED(argv);
679 UNUSED(envp);
680 UNUSED(fdin);
681 UNUSED(fdout);
682 UNUSED(fderr);
683 UNUSED(dfd);
684 return (pid_t)-1;
686 #endif
690 typedef struct fdevent_cmd_pipe {
691 pid_t pid;
692 int fds[2];
693 const char *cmd;
694 time_t start;
695 } fdevent_cmd_pipe;
697 typedef struct fdevent_cmd_pipes {
698 fdevent_cmd_pipe *ptr;
699 size_t used;
700 size_t size;
701 } fdevent_cmd_pipes;
703 static fdevent_cmd_pipes cmd_pipes;
706 static pid_t fdevent_open_logger_pipe_spawn(const char *logger, int rfd) {
707 char *args[4];
708 int devnull = fdevent_open_devnull();
709 pid_t pid;
711 if (-1 == devnull) {
712 return -1;
715 *(const char **)&args[0] = "/bin/sh";
716 *(const char **)&args[1] = "-c";
717 *(const char **)&args[2] = logger;
718 args[3] = NULL;
720 pid = fdevent_fork_execve(args[0], args, NULL, rfd, devnull, devnull, -1);
722 if (pid > 0) {
723 close(devnull);
725 else {
726 int errnum = errno;
727 close(devnull);
728 errno = errnum;
730 return pid;
734 static void fdevent_restart_logger_pipe(fdevent_cmd_pipe *fcp, time_t ts) {
735 if (fcp->pid > 0) return; /* assert */
736 if (fcp->start + 5 < ts) { /* limit restart to once every 5 sec */
737 /* restart child process using existing pipe fds */
738 fcp->start = ts;
739 fcp->pid = fdevent_open_logger_pipe_spawn(fcp->cmd, fcp->fds[0]);
744 void fdevent_restart_logger_pipes(time_t ts) {
745 for (size_t i = 0; i < cmd_pipes.used; ++i) {
746 fdevent_cmd_pipe * const fcp = cmd_pipes.ptr+i;
747 if (fcp->pid > 0) continue;
748 fdevent_restart_logger_pipe(fcp, ts);
753 int fdevent_waitpid_logger_pipe_pid(pid_t pid, time_t ts) {
754 for (size_t i = 0; i < cmd_pipes.used; ++i) {
755 fdevent_cmd_pipe * const fcp = cmd_pipes.ptr+i;
756 if (pid != fcp->pid) continue;
757 fcp->pid = -1;
758 fdevent_restart_logger_pipe(fcp, ts);
759 return 1;
761 return 0;
765 void fdevent_clr_logger_pipe_pids(void) {
766 for (size_t i = 0; i < cmd_pipes.used; ++i) {
767 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
768 fcp->pid = -1;
773 int fdevent_reaped_logger_pipe(pid_t pid) {
774 for (size_t i = 0; i < cmd_pipes.used; ++i) {
775 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
776 if (fcp->pid == pid) {
777 time_t ts = time(NULL);
778 if (fcp->start + 5 < ts) { /* limit restart to once every 5 sec */
779 fcp->start = ts;
780 fcp->pid = fdevent_open_logger_pipe_spawn(fcp->cmd,fcp->fds[0]);
781 return 1;
783 else {
784 fcp->pid = -1;
785 return -1;
789 return 0;
793 void fdevent_close_logger_pipes(void) {
794 for (size_t i = 0; i < cmd_pipes.used; ++i) {
795 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
796 close(fcp->fds[0]);
797 if (fcp->fds[1] != STDERR_FILENO) close(fcp->fds[1]);
799 free(cmd_pipes.ptr);
800 cmd_pipes.ptr = NULL;
801 cmd_pipes.used = 0;
802 cmd_pipes.size = 0;
806 void fdevent_breakagelog_logger_pipe(int fd) {
807 for (size_t i = 0; i < cmd_pipes.used; ++i) {
808 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
809 if (fcp->fds[1] != fd) continue;
810 fcp->fds[1] = STDERR_FILENO;
811 break;
816 static void fdevent_init_logger_pipe(const char *cmd, int fds[2], pid_t pid) {
817 fdevent_cmd_pipe *fcp;
818 if (cmd_pipes.used == cmd_pipes.size) {
819 cmd_pipes.size += 4;
820 cmd_pipes.ptr =
821 realloc(cmd_pipes.ptr, cmd_pipes.size * sizeof(fdevent_cmd_pipe));
822 force_assert(cmd_pipes.ptr);
824 fcp = cmd_pipes.ptr + cmd_pipes.used++;
825 fcp->cmd = cmd; /* note: cmd must persist in memory (or else copy here) */
826 fcp->fds[0] = fds[0];
827 fcp->fds[1] = fds[1];
828 fcp->pid = pid;
829 fcp->start = time(NULL);
833 static int fdevent_open_logger_pipe(const char *logger) {
834 int fds[2];
835 pid_t pid;
836 if (pipe(fds)) {
837 return -1;
839 fdevent_setfd_cloexec(fds[0]);
840 fdevent_setfd_cloexec(fds[1]);
841 /*(nonblocking write() from lighttpd)*/
842 if (0 != fdevent_fcntl_set_nb(NULL, fds[1])) { /*(ignore)*/ }
844 pid = fdevent_open_logger_pipe_spawn(logger, fds[0]);
846 if (pid > 0) {
847 fdevent_init_logger_pipe(logger, fds, pid);
848 return fds[1];
850 else {
851 int errnum = errno;
852 close(fds[0]);
853 close(fds[1]);
854 errno = errnum;
855 return -1;
860 #ifndef O_LARGEFILE
861 #define O_LARGEFILE 0
862 #endif
864 int fdevent_open_logger(const char *logger) {
865 if (logger[0] != '|') {
866 int flags = O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE;
867 return fdevent_open_cloexec(logger, flags, 0644);
869 else {
870 return fdevent_open_logger_pipe(logger+1); /*(skip the '|')*/
874 int fdevent_cycle_logger(const char *logger, int *curfd) {
875 if (logger[0] != '|') {
876 int fd = fdevent_open_logger(logger);
877 if (-1 == fd) return -1; /*(error; leave *curfd as-is)*/
878 if (-1 != *curfd) close(*curfd);
879 *curfd = fd;
881 return *curfd;
885 #ifndef MSG_DONTWAIT
886 #define MSG_DONTWAIT 0
887 #endif
888 #ifndef MSG_NOSIGNAL
889 #define MSG_NOSIGNAL 0
890 #endif
893 ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type) {
894 #if defined(MSG_TRUNC) && defined(__linux__)
895 if ((family == AF_INET || family == AF_INET6) && so_type == SOCK_STREAM) {
896 ssize_t len = recv(fd, buf, sz, MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL);
897 if (len >= 0 || errno != EINVAL) return len;
899 #else
900 UNUSED(family);
901 UNUSED(so_type);
902 #endif
903 return read(fd, buf, sz);
907 #include <sys/ioctl.h>
908 #ifdef HAVE_SYS_FILIO_H
909 #include <sys/filio.h> /* FIONREAD (for illumos (OpenIndiana)) */
910 #endif
911 #ifdef _WIN32
912 #include <winsock2.h>
913 #endif
914 int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) {
915 #ifdef _WIN32
916 if (fdfmt != S_IFSOCK) { errno = ENOTSOCK; return -1; }
917 return ioctlsocket(fd, FIONREAD, toread);
918 #else
919 #ifdef __CYGWIN__
920 /*(cygwin supports FIONREAD on pipes, not sockets)*/
921 if (fdfmt != S_IFIFO) { errno = EOPNOTSUPP; return -1; }
922 #else
923 UNUSED(fdfmt);
924 #endif
925 return ioctl(fd, FIONREAD, toread);
926 #endif
930 int fdevent_connect_status(int fd) {
931 /* try to finish the connect() */
932 /*(should be called after connect() only when fd is writable (POLLOUT))*/
933 int opt;
934 socklen_t len = sizeof(opt);
935 return (0 == getsockopt(fd,SOL_SOCKET,SO_ERROR,&opt,&len)) ? opt : errno;
939 #include <netinet/tcp.h>
940 #if (defined(__APPLE__) && defined(__MACH__)) \
941 || defined(__FreeBSD__) || defined(__NetBSD__) \
942 || defined(__OpenBSD__) || defined(__DragonFly__)
943 #include <netinet/tcp_fsm.h>
944 #endif
946 /* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */
947 int fdevent_is_tcp_half_closed(int fd) {
948 #ifdef TCP_CONNECTION_INFO /* Darwin */
949 struct tcp_connection_info tcpi;
950 socklen_t tlen = sizeof(tcpi);
951 return (0 == getsockopt(fd, IPPROTO_TCP, TCP_CONNECTION_INFO, &tcpi, &tlen)
952 && tcpi.tcpi_state == TCPS_CLOSE_WAIT);
953 #elif defined(TCP_INFO) && defined(TCPS_CLOSE_WAIT)
954 /* FreeBSD, NetBSD (not present in OpenBSD or DragonFlyBSD) */
955 struct tcp_info tcpi;
956 socklen_t tlen = sizeof(tcpi);
957 return (0 == getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpi, &tlen)
958 && tcpi.tcpi_state == TCPS_CLOSE_WAIT);
959 #elif defined(TCP_INFO) && defined(__linux__)
960 /* Linux (TCP_CLOSE_WAIT is enum, so can not #ifdef TCP_CLOSE_WAIT) */
961 struct tcp_info tcpi;
962 socklen_t tlen = sizeof(tcpi);/*SOL_TCP == IPPROTO_TCP*/
963 return (0 == getsockopt(fd, SOL_TCP, TCP_INFO, &tcpi, &tlen)
964 && tcpi.tcpi_state == TCP_CLOSE_WAIT);
965 #else
966 UNUSED(fd);
967 /*(0 != getpeername() error might indicate TCP RST, but success
968 * would not differentiate between half-close and full-close)*/
969 return 0; /* false (not half-closed) or TCP state unknown */
970 #endif
974 int fdevent_set_tcp_nodelay (const int fd, const int opt)
976 return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
980 int fdevent_set_so_reuseaddr (const int fd, const int opt)
982 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));