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
)) {
640 } else if (errno
== ENOSYS
|| errno
== ENOTSUP
) {
641 fd
= accept(listenfd
, addr
, &len
);
646 fd
= accept(listenfd
, addr
, &len
);
650 const int sock_cloexec
= 0;
651 fd
= accept(listenfd
, addr
, &len
);
655 *addrlen
= (size_t)len
;
656 if (!sock_cloexec
&& 0 != fdevent_fcntl_set_nb_cloexec(NULL
, fd
)) {
666 #include <crt_externs.h>
667 #define environ (* _NSGetEnviron())
669 extern char **environ
;
671 char ** fdevent_environ (void) { return environ
; }
675 static int fdevent_dup2_close_clrfd_cloexec(int oldfd
, int newfd
) {
677 if (oldfd
!= newfd
) {
678 force_assert(oldfd
> STDERR_FILENO
);
679 if (newfd
!= dup2(oldfd
, newfd
)) return -1;
682 fdevent_clrfd_cloexec(newfd
);
688 static int fdevent_dup2_close_clrfd_cloexec(int oldfd
, int newfd
, int reuse
) {
690 if (oldfd
!= newfd
) {
691 force_assert(oldfd
> STDERR_FILENO
);
692 if (newfd
!= dup2(oldfd
, newfd
)) return -1;
693 if (!reuse
) close(oldfd
);
701 int fdevent_set_stdin_stdout_stderr(int fdin
, int fdout
, int fderr
) {
703 if (STDIN_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdin
, STDIN_FILENO
))
705 if (STDOUT_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdout
, STDOUT_FILENO
))
707 if (STDERR_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fderr
, STDERR_FILENO
))
710 if (STDIN_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdin
, STDIN_FILENO
,
714 if (STDOUT_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fdout
, STDOUT_FILENO
,
717 if (STDERR_FILENO
!= fdevent_dup2_close_clrfd_cloexec(fderr
, STDERR_FILENO
,
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
) {
733 if (0 != pid
) return pid
; /* parent (pid > 0) or fork() error (-1 == pid) */
735 /* child (0 == pid) */
738 if (0 != fchdir(dfd
))
743 if (0 != fdevent_set_stdin_stdout_stderr(fdin
, fdout
, fderr
)) _exit(errno
);
745 /*(might not be sufficient for open fds, but modern OS have FD_CLOEXEC)*/
746 for (int i
= 3; i
< 256; ++i
) close(i
);
749 /* reset_signals which may have been ignored (SIG_IGN) */
751 signal(SIGTTOU
, SIG_DFL
);
754 signal(SIGTTIN
, SIG_DFL
);
757 signal(SIGTSTP
, SIG_DFL
);
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))
785 typedef struct fdevent_cmd_pipe
{
792 typedef struct fdevent_cmd_pipes
{
793 fdevent_cmd_pipe
*ptr
;
798 static fdevent_cmd_pipes cmd_pipes
;
801 static pid_t
fdevent_open_logger_pipe_spawn(const char *logger
, int rfd
) {
803 int devnull
= fdevent_open_devnull();
810 *(const char **)&args
[0] = "/bin/sh";
811 *(const char **)&args
[1] = "-c";
812 *(const char **)&args
[2] = logger
;
815 pid
= fdevent_fork_execve(args
[0], args
, NULL
, rfd
, devnull
, devnull
, -1);
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 */
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;
853 fdevent_restart_logger_pipe(fcp
, ts
);
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
;
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 */
875 fcp
->pid
= fdevent_open_logger_pipe_spawn(fcp
->cmd
,fcp
->fds
[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
;
892 if (fcp
->fds
[1] != STDERR_FILENO
) close(fcp
->fds
[1]);
895 cmd_pipes
.ptr
= NULL
;
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
;
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
) {
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];
924 fcp
->start
= time(NULL
);
928 static int fdevent_open_logger_pipe(const char *logger
) {
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]);
942 fdevent_init_logger_pipe(logger
, fds
, pid
);
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);
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
);
977 #define MSG_DONTWAIT 0
980 #define MSG_NOSIGNAL 0
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
;
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)) */
1003 #include <winsock2.h>
1005 int fdevent_ioctl_fionread (int fd
, int fdfmt
, int *toread
) {
1007 if (fdfmt
!= S_IFSOCK
) { errno
= ENOTSOCK
; return -1; }
1008 return ioctlsocket(fd
, FIONREAD
, toread
);
1011 /*(cygwin supports FIONREAD on pipes, not sockets)*/
1012 if (fdfmt
!= S_IFIFO
) { errno
= EOPNOTSUPP
; return -1; }
1016 return ioctl(fd
, FIONREAD
, toread
);
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))*/
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>
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
);
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 */
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
));