3 #include "fdevent_impl.h"
11 #include "sys-socket.h"
21 static int use_sock_cloexec
;
24 static int use_sock_nonblock
;
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" },
37 #ifdef FDEVENT_USE_SOLARIS_PORT
38 { FDEVENT_HANDLER_SOLARIS_PORT
, "solaris-eventports" },
40 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
41 { FDEVENT_HANDLER_SOLARIS_DEVPOLL
,"solaris-devpoll" },
43 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
44 { FDEVENT_HANDLER_FREEBSD_KQUEUE
, "freebsd-kqueue" },
45 { FDEVENT_HANDLER_FREEBSD_KQUEUE
, "kqueue" },
47 #ifdef FDEVENT_USE_POLL
48 { FDEVENT_HANDLER_POLL
, "poll" },
50 #ifdef FDEVENT_USE_SELECT
51 { FDEVENT_HANDLER_SELECT
, "select" },
53 #ifdef FDEVENT_USE_LIBEV
54 { FDEVENT_HANDLER_LIBEV
, "libev" },
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");
74 buffer_copy_string(srv
->srvconf
.event_handler
, event_handlers
[0].name
);
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
;
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
);
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
102 srv
->max_fds
= FD_SETSIZE
- 200;
113 const char * fdevent_show_event_handlers(void) {
115 "\nEvent Handlers:\n\n"
116 #ifdef FDEVENT_USE_SELECT
117 "\t+ select (generic)\n"
119 "\t- select (generic)\n"
121 #ifdef FDEVENT_USE_POLL
126 #ifdef FDEVENT_USE_LINUX_EPOLL
127 "\t+ epoll (Linux)\n"
129 "\t- epoll (Linux)\n"
131 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
132 "\t+ /dev/poll (Solaris)\n"
134 "\t- /dev/poll (Solaris)\n"
136 #ifdef FDEVENT_USE_SOLARIS_PORT
137 "\t+ eventports (Solaris)\n"
139 "\t- eventports (Solaris)\n"
141 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
142 "\t+ kqueue (FreeBSD)\n"
144 "\t- kqueue (FreeBSD)\n"
146 #ifdef FDEVENT_USE_LIBEV
147 "\t+ libev (generic)\n"
149 "\t- libev (generic)\n"
154 fdevents
*fdevent_init(server
*srv
) {
156 int type
= srv
->event_handler
;
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
169 int fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, 0);
171 int fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
174 int flags
= fcntl(fd
, F_GETFL
, 0);
176 use_sock_nonblock
= (-1 != flags
&& (flags
& O_NONBLOCK
));
178 use_sock_cloexec
= 1;
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;
190 maxfds
= srv
->max_fds
+ 1; /*(+1 for event-handler fd)*/
192 ev
= calloc(1, sizeof(*ev
));
193 force_assert(NULL
!= ev
);
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, ")");
205 #ifdef FDEVENT_USE_POLL
206 case FDEVENT_HANDLER_POLL
:
207 if (0 == fdevent_poll_init(ev
)) return ev
;
210 #ifdef FDEVENT_USE_SELECT
211 case FDEVENT_HANDLER_SELECT
:
212 if (0 == fdevent_select_init(ev
)) return ev
;
215 #ifdef FDEVENT_USE_LINUX_EPOLL
216 case FDEVENT_HANDLER_LINUX_SYSEPOLL
:
217 if (0 == fdevent_linux_sysepoll_init(ev
)) return ev
;
220 #ifdef FDEVENT_USE_SOLARIS_DEVPOLL
221 case FDEVENT_HANDLER_SOLARIS_DEVPOLL
:
222 if (0 == fdevent_solaris_devpoll_init(ev
)) return ev
;
225 #ifdef FDEVENT_USE_SOLARIS_PORT
226 case FDEVENT_HANDLER_SOLARIS_PORT
:
227 if (0 == fdevent_solaris_port_init(ev
)) return ev
;
230 #ifdef FDEVENT_USE_FREEBSD_KQUEUE
231 case FDEVENT_HANDLER_FREEBSD_KQUEUE
:
232 if (0 == fdevent_freebsd_kqueue_init(ev
)) return ev
;
235 #ifdef FDEVENT_USE_LIBEV
236 case FDEVENT_HANDLER_LIBEV
:
237 if (0 == fdevent_libev_init(ev
)) return ev
;
240 case FDEVENT_HANDLER_UNSET
:
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\"");
253 void fdevent_free(fdevents
*ev
) {
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) */
263 free((fdnode
*)((uintptr_t)ev
->fdarray
[i
] & ~0x3));
270 int fdevent_reset(fdevents
*ev
) {
271 int rc
= (NULL
!= ev
->reset
) ? ev
->reset(ev
) : 0;
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\"");
279 static fdnode
*fdnode_init(void) {
280 return calloc(1, sizeof(fdnode
));
283 static void fdnode_free(fdnode
*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
;
295 #ifdef FDEVENT_USE_LIBEV
296 fdn
->handler_ctx
= NULL
;
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
;
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
;
317 static void fdevent_sched_run(fdevents
*ev
) {
318 server
*srv
= ev
->srv
;
319 for (fdnode
*fdn
= ev
->pendclose
; fdn
; ) {
323 rc
= (uintptr_t)fdn
& 0x3;
325 fdn
= (fdnode
*)((uintptr_t)fdn
& ~0x3);
329 rc
= closesocket(fd
);
331 else if (rc
== 0x2) {
339 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "close failed ", fd
, strerror(errno
));
346 fdn
= (fdnode
*)fdn
->ctx
; /* next */
347 /*(fdevent_unregister)*/
348 fdnode_free(fdn_tmp
);
349 ev
->fdarray
[fd
] = NULL
;
351 ev
->pendclose
= NULL
;
355 __attribute_noinline__
356 static int fdevent_fdnode_event_unsetter_retry(fdevents
*ev
, fdnode
*fdn
) {
360 #if EAGAIN != EWOULDBLOCK
366 /* temporary error; retry */
370 /* unrecoverable error; might leak fd */
371 log_error_write(ev
->srv
, __FILE__
, __LINE__
, "sDsS",
372 "fdevent event_del failed on fd", fdn
->fd
, ":",
376 } while (0 != ev
->event_del(ev
, fdn
));
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
);
389 __attribute_noinline__
390 static int fdevent_fdnode_event_setter_retry(fdevents
*ev
, fdnode
*fdn
, int events
) {
394 #if EAGAIN != EWOULDBLOCK
400 /* temporary error; retry */
404 /* unrecoverable error */
405 log_error_write(ev
->srv
, __FILE__
, __LINE__
, "sDsS",
406 "fdevent event_set failed on fd", fdn
->fd
, ":",
410 } while (0 != ev
->event_set(ev
, fdn
, events
));
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
);
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
));
452 void fdevent_setfd_cloexec(int fd
) {
455 force_assert(-1 != fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
461 void fdevent_clrfd_cloexec(int fd
) {
463 if (fd
>= 0) force_assert(-1 != fcntl(fd
, F_SETFD
, 0));
469 int fdevent_fcntl_set_nb(fdevents
*ev
, int fd
) {
472 return fcntl(fd
, F_SETFL
, O_NONBLOCK
| O_RDWR
);
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
)
489 return fdevent_fcntl_set_nb_cloexec(ev
, fd
);
492 int fdevent_socket_cloexec(int domain
, int type
, int protocol
) {
495 if (use_sock_cloexec
)
496 return socket(domain
, type
| SOCK_CLOEXEC
, protocol
);
498 if (-1 != (fd
= socket(domain
, type
, protocol
))) {
500 force_assert(-1 != fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
506 int fdevent_socket_nb_cloexec(int domain
, int type
, int protocol
) {
510 if (use_sock_cloexec
&& use_sock_nonblock
)
511 return socket(domain
, type
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, protocol
);
513 if (use_sock_cloexec
) {
514 fd
= socket(domain
, type
| SOCK_CLOEXEC
, protocol
);
516 if (-1 != fd
) force_assert(-1 != fcntl(fd
,F_SETFL
,O_NONBLOCK
|O_RDWR
));
522 if (-1 != (fd
= socket(domain
, type
, protocol
))) {
524 force_assert(-1 != fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
527 force_assert(-1 != fcntl(fd
, F_SETFL
, O_NONBLOCK
| O_RDWR
));
537 #define O_LARGEFILE 0
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) */
552 #define FDEVENT_O_FLAGS \
553 (O_BINARY | O_LARGEFILE | O_NOCTTY | O_NONBLOCK)
555 #define FDEVENT_O_FLAGS \
556 (O_BINARY | O_LARGEFILE | O_NOCTTY )
559 int fdevent_open_cloexec(const char *pathname
, int symlinks
, int flags
, mode_t mode
) {
560 if (!symlinks
) flags
|= O_NOFOLLOW
;
562 return open(pathname
, flags
| O_CLOEXEC
| FDEVENT_O_FLAGS
, mode
);
564 int fd
= open(pathname
, flags
| FDEVENT_O_FLAGS
, mode
);
567 force_assert(-1 != fcntl(fd
, F_SETFD
, FD_CLOEXEC
));
574 int fdevent_open_devnull(void) {
576 return fdevent_open_cloexec("nul", 0, O_RDWR
, 0);
578 return fdevent_open_cloexec("/dev/null", 0, O_RDWR
, 0);
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
: ".");
588 int flags
= O_RDONLY
;
590 flags
|= O_DIRECTORY
;
592 if (NULL
!= c
) *c
= '\0';
593 dfd
= fdevent_open_cloexec(dname
, symlinks
, flags
, 0);
594 if (NULL
!= c
) *c
= '/';
599 int fdevent_mkstemp_append(char *path
) {
601 /* POSIX-2008 requires mkstemp create file with 0600 perms */
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) */
616 fdevent_setfd_cloexec(fd
);
621 int fdevent_accept_listenfd(int listenfd
, struct sockaddr
*addr
, size_t *addrlen
) {
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
);
630 int sock_cloexec
= use_sock_cloexec
;
632 fd
= accept4(listenfd
, addr
, &len
, SOCK_CLOEXEC
| SOCK_NONBLOCK
);
634 if (!use_sock_nonblock
) {
635 if (0 != fdevent_fcntl_set_nb(NULL
, fd
)) {
646 fd
= accept(listenfd
, addr
, &len
);
655 fd
= accept(listenfd
, addr
, &len
);
659 const int sock_cloexec
= 0;
660 fd
= accept(listenfd
, addr
, &len
);
664 *addrlen
= (size_t)len
;
665 if (!sock_cloexec
&& 0 != fdevent_fcntl_set_nb_cloexec(NULL
, fd
)) {
675 #include <crt_externs.h>
676 #define environ (* _NSGetEnviron())
678 extern char **environ
;
680 char ** fdevent_environ (void) { return environ
; }
684 static int fdevent_dup2_close_clrfd_cloexec(int oldfd
, int newfd
) {
686 if (oldfd
!= newfd
) {
687 force_assert(oldfd
> STDERR_FILENO
);
688 if (newfd
!= dup2(oldfd
, newfd
)) return -1;
691 fdevent_clrfd_cloexec(newfd
);
697 static int fdevent_dup2_close_clrfd_cloexec(int oldfd
, int newfd
, int reuse
) {
699 if (oldfd
!= newfd
) {
700 force_assert(oldfd
> STDERR_FILENO
);
701 if (newfd
!= dup2(oldfd
, newfd
)) return -1;
702 if (!reuse
) close(oldfd
);
710 int fdevent_set_stdin_stdout_stderr(int fdin
, int fdout
, int fderr
) {
712 if (STDIN_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdin
, STDIN_FILENO
))
714 if (STDOUT_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdout
, STDOUT_FILENO
))
716 if (STDERR_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fderr
, STDERR_FILENO
))
719 if (STDIN_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdin
, STDIN_FILENO
,
723 if (STDOUT_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdout
, STDOUT_FILENO
,
726 if (STDERR_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fderr
, STDERR_FILENO
,
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
) {
742 if (0 != pid
) return pid
; /* parent (pid > 0) or fork() error (-1 == pid) */
744 /* child (0 == pid) */
747 if (0 != fchdir(dfd
))
752 if (0 != fdevent_set_stdin_stdout_stderr(fdin
, fdout
, fderr
)) _exit(errno
);
754 /*(might not be sufficient for open fds, but modern OS have FD_CLOEXEC)*/
755 for (int i
= 3; i
< 256; ++i
) close(i
);
758 /* reset_signals which may have been ignored (SIG_IGN) */
760 signal(SIGTTOU
, SIG_DFL
);
763 signal(SIGTTIN
, SIG_DFL
);
766 signal(SIGTSTP
, SIG_DFL
);
768 signal(SIGPIPE
, SIG_DFL
);
770 execve(name
, argv
, envp
? envp
: environ
);
773 if (0 == memcmp(argv
[0], "/bin/sh", sizeof("/bin/sh")-1)
774 && argv
[1] && 0 == memcmp(argv
[1], "-c", sizeof("-c")-1))
795 typedef struct fdevent_cmd_pipe
{
802 typedef struct fdevent_cmd_pipes
{
803 fdevent_cmd_pipe
*ptr
;
808 static fdevent_cmd_pipes cmd_pipes
;
811 static pid_t
fdevent_open_logger_pipe_spawn(const char *logger
, int rfd
) {
813 int devnull
= fdevent_open_devnull();
820 *(const char **)&args
[0] = "/bin/sh";
821 *(const char **)&args
[1] = "-c";
822 *(const char **)&args
[2] = logger
;
825 pid
= fdevent_fork_execve(args
[0], args
, NULL
, rfd
, devnull
, devnull
, -1);
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 */
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;
863 fdevent_restart_logger_pipe(fcp
, ts
);
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
;
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 */
885 fcp
->pid
= fdevent_open_logger_pipe_spawn(fcp
->cmd
,fcp
->fds
[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
;
902 if (fcp
->fds
[1] != STDERR_FILENO
) close(fcp
->fds
[1]);
905 cmd_pipes
.ptr
= NULL
;
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
;
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
) {
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];
934 fcp
->start
= time(NULL
);
938 static int fdevent_open_logger_pipe(const char *logger
) {
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]);
952 fdevent_init_logger_pipe(logger
, fds
, pid
);
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);
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
);
987 #define MSG_DONTWAIT 0
990 #define MSG_NOSIGNAL 0
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
;
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)) */
1013 #include <winsock2.h>
1015 int fdevent_ioctl_fionread (int fd
, int fdfmt
, int *toread
) {
1017 if (fdfmt
!= S_IFSOCK
) { errno
= ENOTSOCK
; return -1; }
1018 return ioctlsocket(fd
, FIONREAD
, toread
);
1021 /*(cygwin supports FIONREAD on pipes, not sockets)*/
1022 if (fdfmt
!= S_IFIFO
) { errno
= EOPNOTSUPP
; return -1; }
1026 return ioctl(fd
, FIONREAD
, toread
);
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))*/
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>
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
);
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 */
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
));