- next is 1.4.56
[lighttpd.git] / src / fdevent.c
blobbdea35960afab328716c3be0fbabbdf59411a2f8
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 #ifdef SOCK_NONBLOCK
169 int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
170 #else
171 int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
172 #endif
173 if (fd >= 0) {
174 int flags = fcntl(fd, F_GETFL, 0);
175 #ifdef SOCK_NONBLOCK
176 use_sock_nonblock = (-1 != flags && (flags & O_NONBLOCK));
177 #endif
178 use_sock_cloexec = 1;
179 close(fd);
181 #endif
183 #ifdef FDEVENT_USE_SELECT
184 if (type == FDEVENT_HANDLER_SELECT) {
185 if (srv->max_fds > (int)FD_SETSIZE - 200) {
186 srv->max_fds = (int)FD_SETSIZE - 200;
189 #endif
190 maxfds = srv->max_fds + 1; /*(+1 for event-handler fd)*/
192 ev = calloc(1, sizeof(*ev));
193 force_assert(NULL != ev);
194 ev->srv = srv;
195 ev->fdarray = calloc(maxfds, sizeof(*ev->fdarray));
196 if (NULL == ev->fdarray) {
197 log_error_write(srv, __FILE__, __LINE__, "SDS",
198 "server.max-fds too large? (", maxfds-1, ")");
199 free(ev);
200 return NULL;
202 ev->maxfds = maxfds;
204 switch(type) {
205 #ifdef FDEVENT_USE_POLL
206 case FDEVENT_HANDLER_POLL:
207 if (0 == fdevent_poll_init(ev)) return ev;
208 break;
209 #endif
210 #ifdef FDEVENT_USE_SELECT
211 case FDEVENT_HANDLER_SELECT:
212 if (0 == fdevent_select_init(ev)) return ev;
213 break;
214 #endif
215 #ifdef FDEVENT_USE_LINUX_EPOLL
216 case FDEVENT_HANDLER_LINUX_SYSEPOLL:
217 if (0 == fdevent_linux_sysepoll_init(ev)) return ev;
218 break;
219 #endif
220 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
221 case FDEVENT_HANDLER_SOLARIS_DEVPOLL:
222 if (0 == fdevent_solaris_devpoll_init(ev)) return ev;
223 break;
224 #endif
225 #ifdef FDEVENT_USE_SOLARIS_PORT
226 case FDEVENT_HANDLER_SOLARIS_PORT:
227 if (0 == fdevent_solaris_port_init(ev)) return ev;
228 break;
229 #endif
230 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
231 case FDEVENT_HANDLER_FREEBSD_KQUEUE:
232 if (0 == fdevent_freebsd_kqueue_init(ev)) return ev;
233 break;
234 #endif
235 #ifdef FDEVENT_USE_LIBEV
236 case FDEVENT_HANDLER_LIBEV:
237 if (0 == fdevent_libev_init(ev)) return ev;
238 break;
239 #endif
240 case FDEVENT_HANDLER_UNSET:
241 default:
242 break;
245 free(ev->fdarray);
246 free(ev);
248 log_error_write(srv, __FILE__, __LINE__, "sBS",
249 "event-handler failed:", srv->srvconf.event_handler, "; try to set server.event-handler = \"poll\" or \"select\"");
250 return NULL;
253 void fdevent_free(fdevents *ev) {
254 size_t i;
255 if (!ev) return;
257 if (ev->free) ev->free(ev);
259 for (i = 0; i < ev->maxfds; i++) {
260 /* (fdevent_sched_run() should already have been run,
261 * but take reasonable precautions anyway) */
262 if (ev->fdarray[i])
263 free((fdnode *)((uintptr_t)ev->fdarray[i] & ~0x3));
266 free(ev->fdarray);
267 free(ev);
270 int fdevent_reset(fdevents *ev) {
271 int rc = (NULL != ev->reset) ? ev->reset(ev) : 0;
272 if (-1 == rc) {
273 log_error_write(ev->srv, __FILE__, __LINE__, "sBS",
274 "event-handler failed:", ev->srv->srvconf.event_handler, "; try to set server.event-handler = \"poll\" or \"select\"");
276 return rc;
279 static fdnode *fdnode_init(void) {
280 return calloc(1, sizeof(fdnode));
283 static void fdnode_free(fdnode *fdn) {
284 free(fdn);
287 fdnode * fdevent_register(fdevents *ev, int fd, fdevent_handler handler, void *ctx) {
288 fdnode *fdn = ev->fdarray[fd] = fdnode_init();
289 force_assert(NULL != fdn);
290 fdn->handler = handler;
291 fdn->fd = fd;
292 fdn->ctx = ctx;
293 fdn->events = 0;
294 fdn->fde_ndx = -1;
295 #ifdef FDEVENT_USE_LIBEV
296 fdn->handler_ctx = NULL;
297 #endif
298 return fdn;
301 void fdevent_unregister(fdevents *ev, int fd) {
302 fdnode *fdn = ev->fdarray[fd];
303 if ((uintptr_t)fdn & 0x3) return; /*(should not happen)*/
304 ev->fdarray[fd] = NULL;
305 fdnode_free(fdn);
308 void fdevent_sched_close(fdevents *ev, int fd, int issock) {
309 fdnode *fdn = ev->fdarray[fd];
310 if ((uintptr_t)fdn & 0x3) return;
311 ev->fdarray[fd] = (fdnode *)((uintptr_t)fdn | (issock ? 0x1 : 0x2));
312 fdn->handler = (fdevent_handler)NULL;
313 fdn->ctx = ev->pendclose;
314 ev->pendclose = fdn;
317 static void fdevent_sched_run(fdevents *ev) {
318 server *srv = ev->srv;
319 for (fdnode *fdn = ev->pendclose; fdn; ) {
320 int fd, rc;
321 fdnode *fdn_tmp;
322 #ifdef _WIN32
323 rc = (uintptr_t)fdn & 0x3;
324 #endif
325 fdn = (fdnode *)((uintptr_t)fdn & ~0x3);
326 fd = fdn->fd;
327 #ifdef _WIN32
328 if (rc == 0x1) {
329 rc = closesocket(fd);
331 else if (rc == 0x2) {
332 rc = close(fd);
334 #else
335 rc = close(fd);
336 #endif
338 if (0 != rc) {
339 log_error_write(srv, __FILE__, __LINE__, "sds", "close failed ", fd, strerror(errno));
341 else {
342 --srv->cur_fds;
345 fdn_tmp = fdn;
346 fdn = (fdnode *)fdn->ctx; /* next */
347 /*(fdevent_unregister)*/
348 fdnode_free(fdn_tmp);
349 ev->fdarray[fd] = NULL;
351 ev->pendclose = NULL;
354 __attribute_cold__
355 __attribute_noinline__
356 static int fdevent_fdnode_event_unsetter_retry(fdevents *ev, fdnode *fdn) {
357 do {
358 switch (errno) {
359 #ifdef EWOULDBLOCK
360 #if EAGAIN != EWOULDBLOCK
361 case EWOULDBLOCK:
362 #endif
363 #endif
364 case EAGAIN:
365 case EINTR:
366 /* temporary error; retry */
367 break;
368 /*case ENOMEM:*/
369 default:
370 /* unrecoverable error; might leak fd */
371 log_error_write(ev->srv, __FILE__, __LINE__, "sDsS",
372 "fdevent event_del failed on fd", fdn->fd, ":",
373 strerror(errno));
374 return 0;
376 } while (0 != ev->event_del(ev, fdn));
377 return 1;
380 static void fdevent_fdnode_event_unsetter(fdevents *ev, fdnode *fdn) {
381 if (-1 == fdn->fde_ndx) return;
382 if (0 != ev->event_del(ev, fdn))
383 fdevent_fdnode_event_unsetter_retry(ev, fdn);
384 fdn->fde_ndx = -1;
385 fdn->events = 0;
388 __attribute_cold__
389 __attribute_noinline__
390 static int fdevent_fdnode_event_setter_retry(fdevents *ev, fdnode *fdn, int events) {
391 do {
392 switch (errno) {
393 #ifdef EWOULDBLOCK
394 #if EAGAIN != EWOULDBLOCK
395 case EWOULDBLOCK:
396 #endif
397 #endif
398 case EAGAIN:
399 case EINTR:
400 /* temporary error; retry */
401 break;
402 /*case ENOMEM:*/
403 default:
404 /* unrecoverable error */
405 log_error_write(ev->srv, __FILE__, __LINE__, "sDsS",
406 "fdevent event_set failed on fd", fdn->fd, ":",
407 strerror(errno));
408 return 0;
410 } while (0 != ev->event_set(ev, fdn, events));
411 return 1;
414 static void fdevent_fdnode_event_setter(fdevents *ev, fdnode *fdn, int events) {
415 /*(Note: skips registering with kernel if initial events is 0,
416 * so caller should pass non-zero events for initial registration.
417 * If never registered due to never being called with non-zero events,
418 * then FDEVENT_HUP or FDEVENT_ERR will never be returned.) */
419 if (fdn->events == events) return;/*(no change; nothing to do)*/
421 if (0 == ev->event_set(ev, fdn, events)
422 || fdevent_fdnode_event_setter_retry(ev, fdn, events))
423 fdn->events = events;
426 void fdevent_fdnode_event_del(fdevents *ev, fdnode *fdn) {
427 if (NULL != fdn) fdevent_fdnode_event_unsetter(ev, fdn);
430 void fdevent_fdnode_event_set(fdevents *ev, fdnode *fdn, int events) {
431 if (NULL != fdn) fdevent_fdnode_event_setter(ev, fdn, events);
434 void fdevent_fdnode_event_add(fdevents *ev, fdnode *fdn, int event) {
435 if (NULL != fdn) fdevent_fdnode_event_setter(ev, fdn, (fdn->events|event));
438 void fdevent_fdnode_event_clr(fdevents *ev, fdnode *fdn, int event) {
439 if (NULL != fdn) fdevent_fdnode_event_setter(ev, fdn, (fdn->events&~event));
442 int fdevent_poll(fdevents *ev, int timeout_ms) {
443 int n = ev->poll(ev, timeout_ms);
444 if (n >= 0)
445 fdevent_sched_run(ev);
446 else if (errno != EINTR)
447 log_error_write(ev->srv, __FILE__, __LINE__, "SS",
448 "fdevent_poll failed: ", strerror(errno));
449 return n;
452 void fdevent_setfd_cloexec(int fd) {
453 #ifdef FD_CLOEXEC
454 if (fd < 0) return;
455 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
456 #else
457 UNUSED(fd);
458 #endif
461 void fdevent_clrfd_cloexec(int fd) {
462 #ifdef FD_CLOEXEC
463 if (fd >= 0) force_assert(-1 != fcntl(fd, F_SETFD, 0));
464 #else
465 UNUSED(fd);
466 #endif
469 int fdevent_fcntl_set_nb(fdevents *ev, int fd) {
470 UNUSED(ev);
471 #ifdef O_NONBLOCK
472 return fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR);
473 #else
474 UNUSED(fd);
475 return 0;
476 #endif
479 int fdevent_fcntl_set_nb_cloexec(fdevents *ev, int fd) {
480 fdevent_setfd_cloexec(fd);
481 return fdevent_fcntl_set_nb(ev, fd);
484 int fdevent_fcntl_set_nb_cloexec_sock(fdevents *ev, int fd) {
485 #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
486 if (use_sock_cloexec && use_sock_nonblock)
487 return 0;
488 #endif
489 return fdevent_fcntl_set_nb_cloexec(ev, fd);
492 int fdevent_socket_cloexec(int domain, int type, int protocol) {
493 int fd;
494 #ifdef SOCK_CLOEXEC
495 if (use_sock_cloexec)
496 return socket(domain, type | SOCK_CLOEXEC, protocol);
497 #endif
498 if (-1 != (fd = socket(domain, type, protocol))) {
499 #ifdef FD_CLOEXEC
500 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
501 #endif
503 return fd;
506 int fdevent_socket_nb_cloexec(int domain, int type, int protocol) {
507 int fd;
508 #ifdef SOCK_CLOEXEC
509 #ifdef SOCK_NONBLOCK
510 if (use_sock_cloexec && use_sock_nonblock)
511 return socket(domain, type | SOCK_CLOEXEC | SOCK_NONBLOCK, protocol);
512 #else
513 if (use_sock_cloexec) {
514 fd = socket(domain, type | SOCK_CLOEXEC, protocol);
515 #ifdef O_NONBLOCK
516 if (-1 != fd) force_assert(-1 != fcntl(fd,F_SETFL,O_NONBLOCK|O_RDWR));
517 #endif
518 return fd;
520 #endif
521 #endif
522 if (-1 != (fd = socket(domain, type, protocol))) {
523 #ifdef FD_CLOEXEC
524 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
525 #endif
526 #ifdef O_NONBLOCK
527 force_assert(-1 != fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR));
528 #endif
530 return fd;
533 #ifndef O_BINARY
534 #define O_BINARY 0
535 #endif
536 #ifndef O_LARGEFILE
537 #define O_LARGEFILE 0
538 #endif
539 #ifndef O_NOCTTY
540 #define O_NOCTTY 0
541 #endif
542 #ifndef O_NOFOLLOW
543 #define O_NOFOLLOW 0
544 #endif
546 /*(O_NOFOLLOW is not handled here)*/
547 /*(Note: O_NOFOLLOW affects only the final path segment, the target file,
548 * not any intermediate symlinks along the path)*/
550 /* O_CLOEXEC handled further below, if defined) */
551 #ifdef O_NONBLOCK
552 #define FDEVENT_O_FLAGS \
553 (O_BINARY | O_LARGEFILE | O_NOCTTY | O_NONBLOCK)
554 #else
555 #define FDEVENT_O_FLAGS \
556 (O_BINARY | O_LARGEFILE | O_NOCTTY )
557 #endif
559 int fdevent_open_cloexec(const char *pathname, int symlinks, int flags, mode_t mode) {
560 if (!symlinks) flags |= O_NOFOLLOW;
561 #ifdef O_CLOEXEC
562 return open(pathname, flags | O_CLOEXEC | FDEVENT_O_FLAGS, mode);
563 #else
564 int fd = open(pathname, flags | FDEVENT_O_FLAGS, mode);
565 #ifdef FD_CLOEXEC
566 if (fd != -1)
567 force_assert(-1 != fcntl(fd, F_SETFD, FD_CLOEXEC));
568 #endif
569 return fd;
570 #endif
574 int fdevent_open_devnull(void) {
575 #if defined(_WIN32)
576 return fdevent_open_cloexec("nul", 0, O_RDWR, 0);
577 #else
578 return fdevent_open_cloexec("/dev/null", 0, O_RDWR, 0);
579 #endif
583 int fdevent_open_dirname(char *path, int symlinks) {
584 /*(handle special cases of no dirname or dirname is root directory)*/
585 char * const c = strrchr(path, '/');
586 const char * const dname = (NULL != c ? c == path ? "/" : path : ".");
587 int dfd;
588 int flags = O_RDONLY;
589 #ifdef O_DIRECTORY
590 flags |= O_DIRECTORY;
591 #endif
592 if (NULL != c) *c = '\0';
593 dfd = fdevent_open_cloexec(dname, symlinks, flags, 0);
594 if (NULL != c) *c = '/';
595 return dfd;
599 int fdevent_mkstemp_append(char *path) {
600 #ifdef __COVERITY__
601 /* POSIX-2008 requires mkstemp create file with 0600 perms */
602 umask(0600);
603 #endif
604 /* coverity[secure_temp : FALSE] */
605 const int fd = mkstemp(path);
606 if (fd < 0) return fd;
608 if (0 != fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_APPEND)) {
609 /* (should not happen; fd is regular file) */
610 int errnum = errno;
611 close(fd);
612 errno = errnum;
613 return -1;
616 fdevent_setfd_cloexec(fd);
617 return fd;
621 int fdevent_accept_listenfd(int listenfd, struct sockaddr *addr, size_t *addrlen) {
622 int fd;
623 socklen_t len = (socklen_t) *addrlen;
625 #if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
626 #if defined(__NetBSD__)
627 const int sock_cloexec = 1;
628 fd = paccept(listenfd, addr, &len, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
629 #else
630 int sock_cloexec = use_sock_cloexec;
631 if (sock_cloexec) {
632 fd = accept4(listenfd, addr, &len, SOCK_CLOEXEC | SOCK_NONBLOCK);
633 if (fd >= 0) {
634 if (!use_sock_nonblock) {
635 if (0 != fdevent_fcntl_set_nb(NULL, fd)) {
636 close(fd);
637 fd = -1;
641 else {
642 switch (errno) {
643 case ENOSYS:
644 case ENOTSUP:
645 case EPERM:
646 fd = accept(listenfd, addr, &len);
647 sock_cloexec = 0;
648 break;
649 default:
650 break;
654 else {
655 fd = accept(listenfd, addr, &len);
657 #endif
658 #else
659 const int sock_cloexec = 0;
660 fd = accept(listenfd, addr, &len);
661 #endif
663 if (fd >= 0) {
664 *addrlen = (size_t)len;
665 if (!sock_cloexec && 0 != fdevent_fcntl_set_nb_cloexec(NULL, fd)) {
666 close(fd);
667 fd = -1;
670 return fd;
674 #ifdef __APPLE__
675 #include <crt_externs.h>
676 #define environ (* _NSGetEnviron())
677 #else
678 extern char **environ;
679 #endif
680 char ** fdevent_environ (void) { return environ; }
683 #ifdef FD_CLOEXEC
684 static int fdevent_dup2_close_clrfd_cloexec(int oldfd, int newfd) {
685 if (oldfd >= 0) {
686 if (oldfd != newfd) {
687 force_assert(oldfd > STDERR_FILENO);
688 if (newfd != dup2(oldfd, newfd)) return -1;
690 else {
691 fdevent_clrfd_cloexec(newfd);
694 return newfd;
696 #else
697 static int fdevent_dup2_close_clrfd_cloexec(int oldfd, int newfd, int reuse) {
698 if (oldfd >= 0) {
699 if (oldfd != newfd) {
700 force_assert(oldfd > STDERR_FILENO);
701 if (newfd != dup2(oldfd, newfd)) return -1;
702 if (!reuse) close(oldfd);
705 return newfd;
707 #endif
710 int fdevent_set_stdin_stdout_stderr(int fdin, int fdout, int fderr) {
711 #ifdef FD_CLOEXEC
712 if (STDIN_FILENO != fdevent_dup2_close_clrfd_cloexec(fdin, STDIN_FILENO))
713 return -1;
714 if (STDOUT_FILENO != fdevent_dup2_close_clrfd_cloexec(fdout, STDOUT_FILENO))
715 return -1;
716 if (STDERR_FILENO != fdevent_dup2_close_clrfd_cloexec(fderr, STDERR_FILENO))
717 return -1;
718 #else
719 if (STDIN_FILENO != fdevent_dup2_close_clrfd_cloexec(fdin, STDIN_FILENO,
720 fdin == fdout
721 || fdin == fderr))
722 return -1;
723 if (STDOUT_FILENO != fdevent_dup2_close_clrfd_cloexec(fdout, STDOUT_FILENO,
724 fdout == fderr))
725 return -1;
726 if (STDERR_FILENO != fdevent_dup2_close_clrfd_cloexec(fderr, STDERR_FILENO,
728 return -1;
729 #endif
731 return 0;
735 #include <stdio.h> /* perror() */
736 #include <signal.h> /* signal() */
738 pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd) {
739 #ifdef HAVE_FORK
741 pid_t pid = fork();
742 if (0 != pid) return pid; /* parent (pid > 0) or fork() error (-1 == pid) */
744 /* child (0 == pid) */
746 if (-1 != dfd) {
747 if (0 != fchdir(dfd))
748 _exit(errno);
749 close(dfd);
752 if (0 != fdevent_set_stdin_stdout_stderr(fdin, fdout, fderr)) _exit(errno);
753 #ifdef FD_CLOEXEC
754 /*(might not be sufficient for open fds, but modern OS have FD_CLOEXEC)*/
755 for (int i = 3; i < 256; ++i) close(i);
756 #endif
758 /* reset_signals which may have been ignored (SIG_IGN) */
759 #ifdef SIGTTOU
760 signal(SIGTTOU, SIG_DFL);
761 #endif
762 #ifdef SIGTTIN
763 signal(SIGTTIN, SIG_DFL);
764 #endif
765 #ifdef SIGTSTP
766 signal(SIGTSTP, SIG_DFL);
767 #endif
768 signal(SIGPIPE, SIG_DFL);
770 execve(name, argv, envp ? envp : environ);
772 int errnum = errno;
773 if (0 == memcmp(argv[0], "/bin/sh", sizeof("/bin/sh")-1)
774 && argv[1] && 0 == memcmp(argv[1], "-c", sizeof("-c")-1))
775 perror(argv[2]);
776 else
777 perror(argv[0]);
778 _exit(errnum);
780 #else
782 UNUSED(name);
783 UNUSED(argv);
784 UNUSED(envp);
785 UNUSED(fdin);
786 UNUSED(fdout);
787 UNUSED(fderr);
788 UNUSED(dfd);
789 return (pid_t)-1;
791 #endif
795 typedef struct fdevent_cmd_pipe {
796 pid_t pid;
797 int fds[2];
798 const char *cmd;
799 time_t start;
800 } fdevent_cmd_pipe;
802 typedef struct fdevent_cmd_pipes {
803 fdevent_cmd_pipe *ptr;
804 size_t used;
805 size_t size;
806 } fdevent_cmd_pipes;
808 static fdevent_cmd_pipes cmd_pipes;
811 static pid_t fdevent_open_logger_pipe_spawn(const char *logger, int rfd) {
812 char *args[4];
813 int devnull = fdevent_open_devnull();
814 pid_t pid;
816 if (-1 == devnull) {
817 return -1;
820 *(const char **)&args[0] = "/bin/sh";
821 *(const char **)&args[1] = "-c";
822 *(const char **)&args[2] = logger;
823 args[3] = NULL;
825 pid = fdevent_fork_execve(args[0], args, NULL, rfd, devnull, devnull, -1);
827 if (pid > 0) {
828 close(devnull);
830 else {
831 int errnum = errno;
832 close(devnull);
833 errno = errnum;
835 return pid;
839 static void fdevent_restart_logger_pipe(fdevent_cmd_pipe *fcp, time_t ts) {
840 if (fcp->pid > 0) return; /* assert */
841 if (fcp->start + 5 < ts) { /* limit restart to once every 5 sec */
842 /* restart child process using existing pipe fds */
843 fcp->start = ts;
844 fcp->pid = fdevent_open_logger_pipe_spawn(fcp->cmd, fcp->fds[0]);
849 void fdevent_restart_logger_pipes(time_t ts) {
850 for (size_t i = 0; i < cmd_pipes.used; ++i) {
851 fdevent_cmd_pipe * const fcp = cmd_pipes.ptr+i;
852 if (fcp->pid > 0) continue;
853 fdevent_restart_logger_pipe(fcp, ts);
858 int fdevent_waitpid_logger_pipe_pid(pid_t pid, time_t ts) {
859 for (size_t i = 0; i < cmd_pipes.used; ++i) {
860 fdevent_cmd_pipe * const fcp = cmd_pipes.ptr+i;
861 if (pid != fcp->pid) continue;
862 fcp->pid = -1;
863 fdevent_restart_logger_pipe(fcp, ts);
864 return 1;
866 return 0;
870 void fdevent_clr_logger_pipe_pids(void) {
871 for (size_t i = 0; i < cmd_pipes.used; ++i) {
872 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
873 fcp->pid = -1;
878 int fdevent_reaped_logger_pipe(pid_t pid) {
879 for (size_t i = 0; i < cmd_pipes.used; ++i) {
880 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
881 if (fcp->pid == pid) {
882 time_t ts = time(NULL);
883 if (fcp->start + 5 < ts) { /* limit restart to once every 5 sec */
884 fcp->start = ts;
885 fcp->pid = fdevent_open_logger_pipe_spawn(fcp->cmd,fcp->fds[0]);
886 return 1;
888 else {
889 fcp->pid = -1;
890 return -1;
894 return 0;
898 void fdevent_close_logger_pipes(void) {
899 for (size_t i = 0; i < cmd_pipes.used; ++i) {
900 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
901 close(fcp->fds[0]);
902 if (fcp->fds[1] != STDERR_FILENO) close(fcp->fds[1]);
904 free(cmd_pipes.ptr);
905 cmd_pipes.ptr = NULL;
906 cmd_pipes.used = 0;
907 cmd_pipes.size = 0;
911 void fdevent_breakagelog_logger_pipe(int fd) {
912 for (size_t i = 0; i < cmd_pipes.used; ++i) {
913 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
914 if (fcp->fds[1] != fd) continue;
915 fcp->fds[1] = STDERR_FILENO;
916 break;
921 static void fdevent_init_logger_pipe(const char *cmd, int fds[2], pid_t pid) {
922 fdevent_cmd_pipe *fcp;
923 if (cmd_pipes.used == cmd_pipes.size) {
924 cmd_pipes.size += 4;
925 cmd_pipes.ptr =
926 realloc(cmd_pipes.ptr, cmd_pipes.size * sizeof(fdevent_cmd_pipe));
927 force_assert(cmd_pipes.ptr);
929 fcp = cmd_pipes.ptr + cmd_pipes.used++;
930 fcp->cmd = cmd; /* note: cmd must persist in memory (or else copy here) */
931 fcp->fds[0] = fds[0];
932 fcp->fds[1] = fds[1];
933 fcp->pid = pid;
934 fcp->start = time(NULL);
938 static int fdevent_open_logger_pipe(const char *logger) {
939 int fds[2];
940 pid_t pid;
941 if (pipe(fds)) {
942 return -1;
944 fdevent_setfd_cloexec(fds[0]);
945 fdevent_setfd_cloexec(fds[1]);
946 /*(nonblocking write() from lighttpd)*/
947 if (0 != fdevent_fcntl_set_nb(NULL, fds[1])) { /*(ignore)*/ }
949 pid = fdevent_open_logger_pipe_spawn(logger, fds[0]);
951 if (pid > 0) {
952 fdevent_init_logger_pipe(logger, fds, pid);
953 return fds[1];
955 else {
956 int errnum = errno;
957 close(fds[0]);
958 close(fds[1]);
959 errno = errnum;
960 return -1;
965 int fdevent_open_logger(const char *logger) {
966 if (logger[0] != '|') { /*(permit symlinks)*/
967 int flags = O_APPEND | O_WRONLY | O_CREAT;
968 return fdevent_open_cloexec(logger, 1, flags, 0644);
970 else {
971 return fdevent_open_logger_pipe(logger+1); /*(skip the '|')*/
975 int fdevent_cycle_logger(const char *logger, int *curfd) {
976 if (logger[0] != '|') {
977 int fd = fdevent_open_logger(logger);
978 if (-1 == fd) return -1; /*(error; leave *curfd as-is)*/
979 if (-1 != *curfd) close(*curfd);
980 *curfd = fd;
982 return *curfd;
986 #ifndef MSG_DONTWAIT
987 #define MSG_DONTWAIT 0
988 #endif
989 #ifndef MSG_NOSIGNAL
990 #define MSG_NOSIGNAL 0
991 #endif
994 ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type) {
995 #if defined(MSG_TRUNC) && defined(__linux__)
996 if ((family == AF_INET || family == AF_INET6) && so_type == SOCK_STREAM) {
997 ssize_t len = recv(fd, buf, sz, MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL);
998 if (len >= 0 || errno != EINVAL) return len;
1000 #else
1001 UNUSED(family);
1002 UNUSED(so_type);
1003 #endif
1004 return read(fd, buf, sz);
1008 #include <sys/ioctl.h>
1009 #ifdef HAVE_SYS_FILIO_H
1010 #include <sys/filio.h> /* FIONREAD (for illumos (OpenIndiana)) */
1011 #endif
1012 #ifdef _WIN32
1013 #include <winsock2.h>
1014 #endif
1015 int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) {
1016 #ifdef _WIN32
1017 if (fdfmt != S_IFSOCK) { errno = ENOTSOCK; return -1; }
1018 return ioctlsocket(fd, FIONREAD, toread);
1019 #else
1020 #ifdef __CYGWIN__
1021 /*(cygwin supports FIONREAD on pipes, not sockets)*/
1022 if (fdfmt != S_IFIFO) { errno = EOPNOTSUPP; return -1; }
1023 #else
1024 UNUSED(fdfmt);
1025 #endif
1026 return ioctl(fd, FIONREAD, toread);
1027 #endif
1031 int fdevent_connect_status(int fd) {
1032 /* try to finish the connect() */
1033 /*(should be called after connect() only when fd is writable (POLLOUT))*/
1034 int opt;
1035 socklen_t len = sizeof(opt);
1036 return (0 == getsockopt(fd,SOL_SOCKET,SO_ERROR,&opt,&len)) ? opt : errno;
1040 #include <netinet/tcp.h>
1041 #if (defined(__APPLE__) && defined(__MACH__)) \
1042 || defined(__FreeBSD__) || defined(__NetBSD__) \
1043 || defined(__OpenBSD__) || defined(__DragonFly__)
1044 #include <netinet/tcp_fsm.h>
1045 #endif
1047 /* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */
1048 int fdevent_is_tcp_half_closed(int fd) {
1049 #ifdef TCP_CONNECTION_INFO /* Darwin */
1050 struct tcp_connection_info tcpi;
1051 socklen_t tlen = sizeof(tcpi);
1052 return (0 == getsockopt(fd, IPPROTO_TCP, TCP_CONNECTION_INFO, &tcpi, &tlen)
1053 && tcpi.tcpi_state == TCPS_CLOSE_WAIT);
1054 #elif defined(TCP_INFO) && defined(TCPS_CLOSE_WAIT)
1055 /* FreeBSD, NetBSD (not present in OpenBSD or DragonFlyBSD) */
1056 struct tcp_info tcpi;
1057 socklen_t tlen = sizeof(tcpi);
1058 return (0 == getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpi, &tlen)
1059 && tcpi.tcpi_state == TCPS_CLOSE_WAIT);
1060 #elif defined(TCP_INFO) && defined(__linux__)
1061 /* Linux (TCP_CLOSE_WAIT is enum, so can not #ifdef TCP_CLOSE_WAIT) */
1062 struct tcp_info tcpi;
1063 socklen_t tlen = sizeof(tcpi);/*SOL_TCP == IPPROTO_TCP*/
1064 return (0 == getsockopt(fd, SOL_TCP, TCP_INFO, &tcpi, &tlen)
1065 && tcpi.tcpi_state == TCP_CLOSE_WAIT);
1066 #else
1067 UNUSED(fd);
1068 /*(0 != getpeername() error might indicate TCP RST, but success
1069 * would not differentiate between half-close and full-close)*/
1070 return 0; /* false (not half-closed) or TCP state unknown */
1071 #endif
1075 int fdevent_set_tcp_nodelay (const int fd, const int opt)
1077 return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
1081 int fdevent_set_so_reuseaddr (const int fd, const int opt)
1083 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));