[mod_auth] require digest uri= match original URI
[lighttpd.git] / src / fdevent.c
blob3a038c029507e4a6b2b22915763d0f879a469538
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;
640 } else if (errno == ENOSYS || errno == ENOTSUP) {
641 fd = accept(listenfd, addr, &len);
642 sock_cloexec = 0;
645 else {
646 fd = accept(listenfd, addr, &len);
648 #endif
649 #else
650 const int sock_cloexec = 0;
651 fd = accept(listenfd, addr, &len);
652 #endif
654 if (fd >= 0) {
655 *addrlen = (size_t)len;
656 if (!sock_cloexec && 0 != fdevent_fcntl_set_nb_cloexec(NULL, fd)) {
657 close(fd);
658 fd = -1;
661 return fd;
665 #ifdef __APPLE__
666 #include <crt_externs.h>
667 #define environ (* _NSGetEnviron())
668 #else
669 extern char **environ;
670 #endif
671 char ** fdevent_environ (void) { return environ; }
674 #ifdef FD_CLOEXEC
675 static int fdevent_dup2_close_clrfd_cloexec(int oldfd, int newfd) {
676 if (oldfd >= 0) {
677 if (oldfd != newfd) {
678 force_assert(oldfd > STDERR_FILENO);
679 if (newfd != dup2(oldfd, newfd)) return -1;
681 else {
682 fdevent_clrfd_cloexec(newfd);
685 return newfd;
687 #else
688 static int fdevent_dup2_close_clrfd_cloexec(int oldfd, int newfd, int reuse) {
689 if (oldfd >= 0) {
690 if (oldfd != newfd) {
691 force_assert(oldfd > STDERR_FILENO);
692 if (newfd != dup2(oldfd, newfd)) return -1;
693 if (!reuse) close(oldfd);
696 return newfd;
698 #endif
701 int fdevent_set_stdin_stdout_stderr(int fdin, int fdout, int fderr) {
702 #ifdef FD_CLOEXEC
703 if (STDIN_FILENO != fdevent_dup2_close_clrfd_cloexec(fdin, STDIN_FILENO))
704 return -1;
705 if (STDOUT_FILENO != fdevent_dup2_close_clrfd_cloexec(fdout, STDOUT_FILENO))
706 return -1;
707 if (STDERR_FILENO != fdevent_dup2_close_clrfd_cloexec(fderr, STDERR_FILENO))
708 return -1;
709 #else
710 if (STDIN_FILENO != fdevent_dup2_close_clrfd_cloexec(fdin, STDIN_FILENO,
711 fdin == fdout
712 || fdin == fderr))
713 return -1;
714 if (STDOUT_FILENO != fdevent_dup2_close_clrfd_cloexec(fdout, STDOUT_FILENO,
715 fdout == fderr))
716 return -1;
717 if (STDERR_FILENO != fdevent_dup2_close_clrfd_cloexec(fderr, STDERR_FILENO,
719 return -1;
720 #endif
722 return 0;
726 #include <stdio.h> /* perror() */
727 #include <signal.h> /* signal() */
729 pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd) {
730 #ifdef HAVE_FORK
732 pid_t pid = fork();
733 if (0 != pid) return pid; /* parent (pid > 0) or fork() error (-1 == pid) */
735 /* child (0 == pid) */
737 if (-1 != dfd) {
738 if (0 != fchdir(dfd))
739 _exit(errno);
740 close(dfd);
743 if (0 != fdevent_set_stdin_stdout_stderr(fdin, fdout, fderr)) _exit(errno);
744 #ifdef FD_CLOEXEC
745 /*(might not be sufficient for open fds, but modern OS have FD_CLOEXEC)*/
746 for (int i = 3; i < 256; ++i) close(i);
747 #endif
749 /* reset_signals which may have been ignored (SIG_IGN) */
750 #ifdef SIGTTOU
751 signal(SIGTTOU, SIG_DFL);
752 #endif
753 #ifdef SIGTTIN
754 signal(SIGTTIN, SIG_DFL);
755 #endif
756 #ifdef SIGTSTP
757 signal(SIGTSTP, SIG_DFL);
758 #endif
759 signal(SIGPIPE, SIG_DFL);
761 execve(name, argv, envp ? envp : environ);
763 if (0 == memcmp(argv[0], "/bin/sh", sizeof("/bin/sh")-1)
764 && argv[1] && 0 == memcmp(argv[1], "-c", sizeof("-c")-1))
765 perror(argv[2]);
766 else
767 perror(argv[0]);
768 _exit(errno);
770 #else
772 UNUSED(name);
773 UNUSED(argv);
774 UNUSED(envp);
775 UNUSED(fdin);
776 UNUSED(fdout);
777 UNUSED(fderr);
778 UNUSED(dfd);
779 return (pid_t)-1;
781 #endif
785 typedef struct fdevent_cmd_pipe {
786 pid_t pid;
787 int fds[2];
788 const char *cmd;
789 time_t start;
790 } fdevent_cmd_pipe;
792 typedef struct fdevent_cmd_pipes {
793 fdevent_cmd_pipe *ptr;
794 size_t used;
795 size_t size;
796 } fdevent_cmd_pipes;
798 static fdevent_cmd_pipes cmd_pipes;
801 static pid_t fdevent_open_logger_pipe_spawn(const char *logger, int rfd) {
802 char *args[4];
803 int devnull = fdevent_open_devnull();
804 pid_t pid;
806 if (-1 == devnull) {
807 return -1;
810 *(const char **)&args[0] = "/bin/sh";
811 *(const char **)&args[1] = "-c";
812 *(const char **)&args[2] = logger;
813 args[3] = NULL;
815 pid = fdevent_fork_execve(args[0], args, NULL, rfd, devnull, devnull, -1);
817 if (pid > 0) {
818 close(devnull);
820 else {
821 int errnum = errno;
822 close(devnull);
823 errno = errnum;
825 return pid;
829 static void fdevent_restart_logger_pipe(fdevent_cmd_pipe *fcp, time_t ts) {
830 if (fcp->pid > 0) return; /* assert */
831 if (fcp->start + 5 < ts) { /* limit restart to once every 5 sec */
832 /* restart child process using existing pipe fds */
833 fcp->start = ts;
834 fcp->pid = fdevent_open_logger_pipe_spawn(fcp->cmd, fcp->fds[0]);
839 void fdevent_restart_logger_pipes(time_t ts) {
840 for (size_t i = 0; i < cmd_pipes.used; ++i) {
841 fdevent_cmd_pipe * const fcp = cmd_pipes.ptr+i;
842 if (fcp->pid > 0) continue;
843 fdevent_restart_logger_pipe(fcp, ts);
848 int fdevent_waitpid_logger_pipe_pid(pid_t pid, time_t ts) {
849 for (size_t i = 0; i < cmd_pipes.used; ++i) {
850 fdevent_cmd_pipe * const fcp = cmd_pipes.ptr+i;
851 if (pid != fcp->pid) continue;
852 fcp->pid = -1;
853 fdevent_restart_logger_pipe(fcp, ts);
854 return 1;
856 return 0;
860 void fdevent_clr_logger_pipe_pids(void) {
861 for (size_t i = 0; i < cmd_pipes.used; ++i) {
862 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
863 fcp->pid = -1;
868 int fdevent_reaped_logger_pipe(pid_t pid) {
869 for (size_t i = 0; i < cmd_pipes.used; ++i) {
870 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
871 if (fcp->pid == pid) {
872 time_t ts = time(NULL);
873 if (fcp->start + 5 < ts) { /* limit restart to once every 5 sec */
874 fcp->start = ts;
875 fcp->pid = fdevent_open_logger_pipe_spawn(fcp->cmd,fcp->fds[0]);
876 return 1;
878 else {
879 fcp->pid = -1;
880 return -1;
884 return 0;
888 void fdevent_close_logger_pipes(void) {
889 for (size_t i = 0; i < cmd_pipes.used; ++i) {
890 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
891 close(fcp->fds[0]);
892 if (fcp->fds[1] != STDERR_FILENO) close(fcp->fds[1]);
894 free(cmd_pipes.ptr);
895 cmd_pipes.ptr = NULL;
896 cmd_pipes.used = 0;
897 cmd_pipes.size = 0;
901 void fdevent_breakagelog_logger_pipe(int fd) {
902 for (size_t i = 0; i < cmd_pipes.used; ++i) {
903 fdevent_cmd_pipe *fcp = cmd_pipes.ptr+i;
904 if (fcp->fds[1] != fd) continue;
905 fcp->fds[1] = STDERR_FILENO;
906 break;
911 static void fdevent_init_logger_pipe(const char *cmd, int fds[2], pid_t pid) {
912 fdevent_cmd_pipe *fcp;
913 if (cmd_pipes.used == cmd_pipes.size) {
914 cmd_pipes.size += 4;
915 cmd_pipes.ptr =
916 realloc(cmd_pipes.ptr, cmd_pipes.size * sizeof(fdevent_cmd_pipe));
917 force_assert(cmd_pipes.ptr);
919 fcp = cmd_pipes.ptr + cmd_pipes.used++;
920 fcp->cmd = cmd; /* note: cmd must persist in memory (or else copy here) */
921 fcp->fds[0] = fds[0];
922 fcp->fds[1] = fds[1];
923 fcp->pid = pid;
924 fcp->start = time(NULL);
928 static int fdevent_open_logger_pipe(const char *logger) {
929 int fds[2];
930 pid_t pid;
931 if (pipe(fds)) {
932 return -1;
934 fdevent_setfd_cloexec(fds[0]);
935 fdevent_setfd_cloexec(fds[1]);
936 /*(nonblocking write() from lighttpd)*/
937 if (0 != fdevent_fcntl_set_nb(NULL, fds[1])) { /*(ignore)*/ }
939 pid = fdevent_open_logger_pipe_spawn(logger, fds[0]);
941 if (pid > 0) {
942 fdevent_init_logger_pipe(logger, fds, pid);
943 return fds[1];
945 else {
946 int errnum = errno;
947 close(fds[0]);
948 close(fds[1]);
949 errno = errnum;
950 return -1;
955 int fdevent_open_logger(const char *logger) {
956 if (logger[0] != '|') { /*(permit symlinks)*/
957 int flags = O_APPEND | O_WRONLY | O_CREAT;
958 return fdevent_open_cloexec(logger, 1, flags, 0644);
960 else {
961 return fdevent_open_logger_pipe(logger+1); /*(skip the '|')*/
965 int fdevent_cycle_logger(const char *logger, int *curfd) {
966 if (logger[0] != '|') {
967 int fd = fdevent_open_logger(logger);
968 if (-1 == fd) return -1; /*(error; leave *curfd as-is)*/
969 if (-1 != *curfd) close(*curfd);
970 *curfd = fd;
972 return *curfd;
976 #ifndef MSG_DONTWAIT
977 #define MSG_DONTWAIT 0
978 #endif
979 #ifndef MSG_NOSIGNAL
980 #define MSG_NOSIGNAL 0
981 #endif
984 ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type) {
985 #if defined(MSG_TRUNC) && defined(__linux__)
986 if ((family == AF_INET || family == AF_INET6) && so_type == SOCK_STREAM) {
987 ssize_t len = recv(fd, buf, sz, MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL);
988 if (len >= 0 || errno != EINVAL) return len;
990 #else
991 UNUSED(family);
992 UNUSED(so_type);
993 #endif
994 return read(fd, buf, sz);
998 #include <sys/ioctl.h>
999 #ifdef HAVE_SYS_FILIO_H
1000 #include <sys/filio.h> /* FIONREAD (for illumos (OpenIndiana)) */
1001 #endif
1002 #ifdef _WIN32
1003 #include <winsock2.h>
1004 #endif
1005 int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) {
1006 #ifdef _WIN32
1007 if (fdfmt != S_IFSOCK) { errno = ENOTSOCK; return -1; }
1008 return ioctlsocket(fd, FIONREAD, toread);
1009 #else
1010 #ifdef __CYGWIN__
1011 /*(cygwin supports FIONREAD on pipes, not sockets)*/
1012 if (fdfmt != S_IFIFO) { errno = EOPNOTSUPP; return -1; }
1013 #else
1014 UNUSED(fdfmt);
1015 #endif
1016 return ioctl(fd, FIONREAD, toread);
1017 #endif
1021 int fdevent_connect_status(int fd) {
1022 /* try to finish the connect() */
1023 /*(should be called after connect() only when fd is writable (POLLOUT))*/
1024 int opt;
1025 socklen_t len = sizeof(opt);
1026 return (0 == getsockopt(fd,SOL_SOCKET,SO_ERROR,&opt,&len)) ? opt : errno;
1030 #include <netinet/tcp.h>
1031 #if (defined(__APPLE__) && defined(__MACH__)) \
1032 || defined(__FreeBSD__) || defined(__NetBSD__) \
1033 || defined(__OpenBSD__) || defined(__DragonFly__)
1034 #include <netinet/tcp_fsm.h>
1035 #endif
1037 /* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */
1038 int fdevent_is_tcp_half_closed(int fd) {
1039 #ifdef TCP_CONNECTION_INFO /* Darwin */
1040 struct tcp_connection_info tcpi;
1041 socklen_t tlen = sizeof(tcpi);
1042 return (0 == getsockopt(fd, IPPROTO_TCP, TCP_CONNECTION_INFO, &tcpi, &tlen)
1043 && tcpi.tcpi_state == TCPS_CLOSE_WAIT);
1044 #elif defined(TCP_INFO) && defined(TCPS_CLOSE_WAIT)
1045 /* FreeBSD, NetBSD (not present in OpenBSD or DragonFlyBSD) */
1046 struct tcp_info tcpi;
1047 socklen_t tlen = sizeof(tcpi);
1048 return (0 == getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpi, &tlen)
1049 && tcpi.tcpi_state == TCPS_CLOSE_WAIT);
1050 #elif defined(TCP_INFO) && defined(__linux__)
1051 /* Linux (TCP_CLOSE_WAIT is enum, so can not #ifdef TCP_CLOSE_WAIT) */
1052 struct tcp_info tcpi;
1053 socklen_t tlen = sizeof(tcpi);/*SOL_TCP == IPPROTO_TCP*/
1054 return (0 == getsockopt(fd, SOL_TCP, TCP_INFO, &tcpi, &tlen)
1055 && tcpi.tcpi_state == TCP_CLOSE_WAIT);
1056 #else
1057 UNUSED(fd);
1058 /*(0 != getpeername() error might indicate TCP RST, but success
1059 * would not differentiate between half-close and full-close)*/
1060 return 0; /* false (not half-closed) or TCP state unknown */
1061 #endif
1065 int fdevent_set_tcp_nodelay (const int fd, const int opt)
1067 return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
1071 int fdevent_set_so_reuseaddr (const int fd, const int opt)
1073 return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));