12 #include "http_chunk.h"
14 #include "connections.h"
15 #include "stat_cache.h"
18 #include "network_backends.h"
21 #include <sys/types.h>
41 #ifdef HAVE_VALGRIND_VALGRIND_H
42 # include <valgrind/valgrind.h>
45 #ifdef HAVE_SYS_WAIT_H
46 # include <sys/wait.h>
54 #ifdef HAVE_SYS_RESOURCE_H
55 # include <sys/resource.h>
58 #ifdef HAVE_SYS_PRCTL_H
59 # include <sys/prctl.h>
63 # include <openssl/err.h>
67 /* IRIX doesn't like the alarm based time() optimization */
68 /* #define USE_ALARM */
72 # ifndef HAVE_ISSETUGID
74 static int l_issetugid(void) {
75 return (geteuid() != getuid() || getegid() != getgid());
78 # define issetugid l_issetugid
82 static int oneshot_fd
= 0;
83 static volatile sig_atomic_t srv_shutdown
= 0;
84 static volatile sig_atomic_t graceful_shutdown
= 0;
85 static volatile sig_atomic_t handle_sig_alarm
= 1;
86 static volatile sig_atomic_t handle_sig_hup
= 0;
87 static volatile sig_atomic_t forwarded_sig_hup
= 0;
89 #if defined(HAVE_SIGACTION) && defined(SA_SIGINFO)
90 static volatile siginfo_t last_sigterm_info
;
91 static volatile siginfo_t last_sighup_info
;
93 static void sigaction_handler(int sig
, siginfo_t
*si
, void *context
) {
94 static siginfo_t empty_siginfo
;
97 if (!si
) si
= &empty_siginfo
;
102 last_sigterm_info
= *si
;
105 if (graceful_shutdown
) {
108 graceful_shutdown
= 1;
110 last_sigterm_info
= *si
;
114 handle_sig_alarm
= 1;
118 * we send the SIGHUP to all procs in the process-group
119 * this includes ourself
121 * make sure we only send it once and don't create a
124 if (!forwarded_sig_hup
) {
126 last_sighup_info
= *si
;
128 forwarded_sig_hup
= 0;
135 #elif defined(HAVE_SIGNAL) || defined(HAVE_SIGACTION)
136 static void signal_handler(int sig
) {
138 case SIGTERM
: srv_shutdown
= 1; break;
140 if (graceful_shutdown
) srv_shutdown
= 1;
141 else graceful_shutdown
= 1;
144 case SIGALRM
: handle_sig_alarm
= 1; break;
145 case SIGHUP
: handle_sig_hup
= 1; break;
152 static int daemonize(void) {
156 signal(SIGTTOU
, SIG_IGN
);
159 signal(SIGTTIN
, SIG_IGN
);
162 signal(SIGTSTP
, SIG_IGN
);
165 if (pipe(pipefd
) < 0) exit(-1);
167 if (0 > (pid
= fork())) exit(-1);
174 /* parent waits for grandchild to be ready */
176 bytes
= read(pipefd
[0], &buf
, sizeof(buf
));
177 } while (bytes
< 0 && EINTR
== errno
);
181 /* closed fd (without writing) == failure in grandchild */
182 fputs("daemonized server failed to start; check error log for details\n", stderr
);
191 if (-1 == setsid()) exit(0);
193 signal(SIGHUP
, SIG_IGN
);
195 if (0 != fork()) exit(0);
197 if (0 != chdir("/")) exit(0);
199 fd_close_on_exec(pipefd
[1]);
204 static server
*server_init(void) {
206 server
*srv
= calloc(1, sizeof(*srv
));
209 srv->x = buffer_init();
211 CLEAN(response_header
);
212 CLEAN(parse_full_path
);
216 CLEAN(response_range
);
218 srv
->empty_string
= buffer_init_string("");
219 CLEAN(cond_check_buf
);
221 CLEAN(srvconf
.errorlog_file
);
222 CLEAN(srvconf
.breakagelog_file
);
223 CLEAN(srvconf
.groupname
);
224 CLEAN(srvconf
.username
);
225 CLEAN(srvconf
.changeroot
);
226 CLEAN(srvconf
.bindhost
);
227 CLEAN(srvconf
.event_handler
);
228 CLEAN(srvconf
.pid_file
);
230 CLEAN(tmp_chunk_len
);
234 srv->x = array_init();
236 CLEAN(config_context
);
237 CLEAN(config_touched
);
241 for (i
= 0; i
< FILE_CACHE_MAX
; i
++) {
242 srv
->mtime_cache
[i
].mtime
= (time_t)-1;
243 srv
->mtime_cache
[i
].str
= buffer_init();
247 li_rand_bytes((unsigned char *)srv
->entropy
, (int)sizeof(srv
->entropy
));
249 srv
->cur_ts
= time(NULL
);
250 srv
->startup_ts
= srv
->cur_ts
;
252 srv
->conns
= calloc(1, sizeof(*srv
->conns
));
253 force_assert(srv
->conns
);
255 srv
->joblist
= calloc(1, sizeof(*srv
->joblist
));
256 force_assert(srv
->joblist
);
258 srv
->fdwaitqueue
= calloc(1, sizeof(*srv
->fdwaitqueue
));
259 force_assert(srv
->fdwaitqueue
);
261 srv
->srvconf
.modules
= array_init();
262 srv
->srvconf
.modules_dir
= buffer_init_string(LIBRARY_DIR
);
263 srv
->srvconf
.network_backend
= buffer_init();
264 srv
->srvconf
.upload_tempdirs
= array_init();
265 srv
->srvconf
.reject_expect_100_with_417
= 1;
266 srv
->srvconf
.xattr_name
= buffer_init_string("Content-Type");
267 srv
->srvconf
.http_header_strict
= 1;
268 srv
->srvconf
.http_host_strict
= 1; /*(implies http_host_normalize)*/
269 srv
->srvconf
.http_host_normalize
= 0;
270 srv
->srvconf
.high_precision_timestamps
= 0;
271 srv
->srvconf
.max_request_field_size
= 8192;
274 srv
->errorlog_fd
= STDERR_FILENO
;
275 srv
->errorlog_mode
= ERRORLOG_FD
;
277 srv
->split_vals
= array_init();
282 static void server_free(server
*srv
) {
285 for (i
= 0; i
< FILE_CACHE_MAX
; i
++) {
286 buffer_free(srv
->mtime_cache
[i
].str
);
289 if (oneshot_fd
> 0) {
296 CLEAN(response_header
);
297 CLEAN(parse_full_path
);
301 CLEAN(response_range
);
304 CLEAN(cond_check_buf
);
306 CLEAN(srvconf
.errorlog_file
);
307 CLEAN(srvconf
.breakagelog_file
);
308 CLEAN(srvconf
.groupname
);
309 CLEAN(srvconf
.username
);
310 CLEAN(srvconf
.changeroot
);
311 CLEAN(srvconf
.bindhost
);
312 CLEAN(srvconf
.event_handler
);
313 CLEAN(srvconf
.pid_file
);
314 CLEAN(srvconf
.modules_dir
);
315 CLEAN(srvconf
.network_backend
);
316 CLEAN(srvconf
.xattr_name
);
318 CLEAN(tmp_chunk_len
);
322 fdevent_unregister(srv
->ev
, srv
->fd
);
324 fdevent_free(srv
->ev
);
328 if (srv
->config_storage
) {
329 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
330 specific_config
*s
= srv
->config_storage
[i
];
334 buffer_free(s
->document_root
);
335 buffer_free(s
->server_name
);
336 buffer_free(s
->server_tag
);
337 buffer_free(s
->ssl_pemfile
);
338 buffer_free(s
->ssl_ca_file
);
339 buffer_free(s
->ssl_cipher_list
);
340 buffer_free(s
->ssl_dh_file
);
341 buffer_free(s
->ssl_ec_curve
);
342 buffer_free(s
->error_handler
);
343 buffer_free(s
->error_handler_404
);
344 buffer_free(s
->errorfile_prefix
);
345 array_free(s
->mimetypes
);
346 buffer_free(s
->ssl_verifyclient_username
);
348 SSL_CTX_free(s
->ssl_ctx
);
349 EVP_PKEY_free(s
->ssl_pemfile_pkey
);
350 X509_free(s
->ssl_pemfile_x509
);
351 if (NULL
!= s
->ssl_ca_file_cert_names
) sk_X509_NAME_pop_free(s
->ssl_ca_file_cert_names
, X509_NAME_free
);
355 free(srv
->config_storage
);
356 srv
->config_storage
= NULL
;
362 CLEAN(config_context
);
363 CLEAN(config_touched
);
365 CLEAN(srvconf
.upload_tempdirs
);
368 joblist_free(srv
, srv
->joblist
);
369 fdwaitqueue_free(srv
, srv
->fdwaitqueue
);
371 if (srv
->stat_cache
) {
372 stat_cache_free(srv
->stat_cache
);
375 array_free(srv
->srvconf
.modules
);
376 array_free(srv
->split_vals
);
379 if (srv
->ssl_is_init
) {
380 CRYPTO_cleanup_all_ex_data();
382 #if OPENSSL_VERSION_NUMBER >= 0x10100000L \
383 && !defined(LIBRESSL_VERSION_NUMBER)
384 /*(OpenSSL libraries handle thread init and deinit)
385 * https://github.com/openssl/openssl/pull/1048 */
386 #elif OPENSSL_VERSION_NUMBER >= 0x10000000L
387 ERR_remove_thread_state(NULL
);
399 static void remove_pid_file(server
*srv
, int *pid_fd
) {
400 if (!buffer_string_is_empty(srv
->srvconf
.pid_file
) && 0 <= *pid_fd
) {
401 if (0 != ftruncate(*pid_fd
, 0)) {
402 log_error_write(srv
, __FILE__
, __LINE__
, "sbds",
403 "ftruncate failed for:",
404 srv
->srvconf
.pid_file
,
413 if (!buffer_string_is_empty(srv
->srvconf
.pid_file
) &&
414 buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
415 if (0 != unlink(srv
->srvconf
.pid_file
->ptr
)) {
416 if (errno
!= EACCES
&& errno
!= EPERM
) {
417 log_error_write(srv
, __FILE__
, __LINE__
, "sbds",
418 "unlink failed for:",
419 srv
->srvconf
.pid_file
,
428 static server_socket
* server_oneshot_getsock(server
*srv
, sock_addr
*cnt_addr
) {
429 server_socket
*srv_socket
, *srv_socket_wild
= NULL
;
431 for (i
= 0; i
< srv
->srv_sockets
.used
; ++i
) {
432 srv_socket
= srv
->srv_sockets
.ptr
[i
];
433 if (cnt_addr
->plain
.sa_family
!= srv_socket
->addr
.plain
.sa_family
) continue;
434 switch (cnt_addr
->plain
.sa_family
) {
436 if (srv_socket
->addr
.ipv4
.sin_port
!= cnt_addr
->ipv4
.sin_port
) continue;
437 if (srv_socket
->addr
.ipv4
.sin_addr
.s_addr
== cnt_addr
->ipv4
.sin_addr
.s_addr
) {
440 if (srv_socket
->addr
.ipv4
.sin_addr
.s_addr
== htonl(INADDR_ANY
)) {
441 srv_socket_wild
= srv_socket
;
446 if (srv_socket
->addr
.ipv6
.sin6_port
!= cnt_addr
->ipv6
.sin6_port
) continue;
447 if (0 == memcmp(&srv_socket
->addr
.ipv6
.sin6_addr
, &cnt_addr
->ipv6
.sin6_addr
, sizeof(struct in6_addr
))) {
450 if (0 == memcmp(&srv_socket
->addr
.ipv6
.sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
))) {
451 srv_socket_wild
= srv_socket
;
457 if (0 == strcmp(srv_socket
->addr
.un
.sun_path
, cnt_addr
->un
.sun_path
)) {
466 if (NULL
!= srv_socket_wild
) {
467 return srv_socket_wild
;
468 } else if (srv
->srv_sockets
.used
) {
469 return srv
->srv_sockets
.ptr
[0];
471 log_error_write(srv
, __FILE__
, __LINE__
, "s", "no sockets configured");
477 static int server_oneshot_init(server
*srv
, int fd
) {
478 /* Note: does not work with netcat due to requirement that fd be socket.
479 * STDOUT_FILENO was not saved earlier in startup, and that is to where
480 * netcat expects output to be sent. Since lighttpd expects connections
481 * to be sockets, con->fd is where output is sent; separate fds are not
482 * stored for input and output, but netcat has different fds for stdin
483 * and * stdout. To support netcat, would additionally need to avoid
484 * S_ISSOCK(), getsockname(), and getpeername() below, reconstructing
485 * addresses from environment variables:
486 * NCAT_LOCAL_ADDR NCAT_LOCAL_PORT
487 * NCAT_REMOTE_ADDR NCAT_REMOTE_PORT
491 server_socket
*srv_socket
;
496 if (0 != fstat(fd
, &st
)) {
497 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fstat:", strerror(errno
));
501 if (!S_ISSOCK(st
.st_mode
)) {
502 /* require that fd is a socket
503 * (modules might expect STDIN_FILENO and STDOUT_FILENO opened to /dev/null) */
504 log_error_write(srv
, __FILE__
, __LINE__
, "s", "lighttpd -1 stdin is not a socket");
508 cnt_len
= sizeof(cnt_addr
);
509 if (0 != getsockname(fd
, (struct sockaddr
*)&cnt_addr
, &cnt_len
)) {
510 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "getsockname:", strerror(errno
));
514 srv_socket
= server_oneshot_getsock(srv
, &cnt_addr
);
515 if (NULL
== srv_socket
) return 0;
517 cnt_len
= sizeof(cnt_addr
);
518 if (0 != getpeername(fd
, (struct sockaddr
*)&cnt_addr
, &cnt_len
)) {
519 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "getpeername:", strerror(errno
));
523 if (cnt_addr
.plain
.sa_family
!= AF_UNIX
) {
524 network_accept_tcp_nagle_disable(fd
);
527 con
= connection_accepted(srv
, srv_socket
, &cnt_addr
, fd
);
528 if (NULL
== con
) return 0;
530 connection_state_machine(srv
, con
);
535 static void show_version (void) {
537 # define TEXT_SSL " (ssl)"
541 char *b
= PACKAGE_DESC TEXT_SSL \
542 " - a light and fast webserver\n" \
543 "Build-Date: " __DATE__
" " __TIME__
"\n";
546 write_all(STDOUT_FILENO
, b
, strlen(b
));
549 static void show_features (void) {
550 const char features
[] = ""
552 "\t+ select (generic)\n"
554 "\t- select (generic)\n"
561 #ifdef USE_LINUX_SIGIO
562 "\t+ rt-signals (Linux 2.4+)\n"
564 "\t- rt-signals (Linux 2.4+)\n"
566 #ifdef USE_LINUX_EPOLL
567 "\t+ epoll (Linux 2.6)\n"
569 "\t- epoll (Linux 2.6)\n"
571 #ifdef USE_SOLARIS_DEVPOLL
572 "\t+ /dev/poll (Solaris)\n"
574 "\t- /dev/poll (Solaris)\n"
576 #ifdef USE_SOLARIS_PORT
577 "\t+ eventports (Solaris)\n"
579 "\t- eventports (Solaris)\n"
581 #ifdef USE_FREEBSD_KQUEUE
582 "\t+ kqueue (FreeBSD)\n"
584 "\t- kqueue (FreeBSD)\n"
587 "\t+ libev (generic)\n"
589 "\t- libev (generic)\n"
591 "\nNetwork handler:\n\n"
592 #if defined USE_LINUX_SENDFILE
593 "\t+ linux-sendfile\n"
595 "\t- linux-sendfile\n"
597 #if defined USE_FREEBSD_SENDFILE
598 "\t+ freebsd-sendfile\n"
600 "\t- freebsd-sendfile\n"
602 #if defined USE_DARWIN_SENDFILE
603 "\t+ darwin-sendfile\n"
605 "\t- darwin-sendfile\n"
607 #if defined USE_SOLARIS_SENDFILEV
608 "\t+ solaris-sendfilev\n"
610 "\t- solaris-sendfilev\n"
612 #if defined USE_WRITEV
629 #if defined HAVE_ZLIB_H && defined HAVE_LIBZ
634 #if defined HAVE_BZLIB_H && defined HAVE_LIBBZ2
635 "\t+ bzip2 support\n"
637 "\t- bzip2 support\n"
639 #if defined(HAVE_CRYPT) || defined(HAVE_CRYPT_R) || defined(HAVE_LIBCRYPT)
640 "\t+ crypt support\n"
642 "\t- crypt support\n"
655 "\t+ MySQL support\n"
657 "\t- MySQL support\n"
660 "\t+ Kerberos support\n"
662 "\t- Kerberos support\n"
664 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)
670 "\t+ memcached support\n"
672 "\t- memcached support\n"
689 #ifdef HAVE_SQLITE3_H
690 "\t+ SQLite support\n"
692 "\t- SQLite support\n"
701 printf("\nEvent Handlers:\n\n%s", features
);
704 static void show_help (void) {
706 # define TEXT_SSL " (ssl)"
710 char *b
= PACKAGE_DESC TEXT_SSL
" ("__DATE__
" " __TIME__
")" \
711 " - a light and fast webserver\n" \
713 " -f <name> filename of the config-file\n" \
714 " -m <name> module directory (default: "LIBRARY_DIR
")\n" \
715 " -i <secs> graceful shutdown after <secs> of inactivity\n" \
716 " -1 process single (one) request on stdin socket, then exit\n" \
717 " -p print the parsed config-file in internal form, and exit\n" \
718 " -t test config-file syntax, then exit\n" \
719 " -tt test config-file syntax, load and init modules, then exit\n" \
720 " -D don't go to background (default: go to background)\n" \
721 " -v show version\n" \
722 " -V show compile-time features\n" \
723 " -h show this help\n" \
728 write_all(STDOUT_FILENO
, b
, strlen(b
));
731 int main (int argc
, char **argv
) {
733 int print_config
= 0;
740 time_t idle_limit
= 0, last_active_ts
= time(NULL
);
741 #ifdef HAVE_SIGACTION
742 struct sigaction act
;
744 #ifdef HAVE_GETRLIMIT
749 int parent_pipe_fd
= -1;
753 struct itimerval interval
;
755 interval
.it_interval
.tv_sec
= 1;
756 interval
.it_interval
.tv_usec
= 0;
757 interval
.it_value
.tv_sec
= 1;
758 interval
.it_value
.tv_usec
= 0;
761 /* for nice %b handling in strfime() */
762 setlocale(LC_TIME
, "C");
764 if (NULL
== (srv
= server_init())) {
765 fprintf(stderr
, "did this really happen?\n");
769 /* init structs done */
771 srv
->srvconf
.port
= 0;
773 i_am_root
= (getuid() == 0);
777 srv
->srvconf
.dont_daemonize
= 0;
778 srv
->srvconf
.preflight_check
= 0;
780 while(-1 != (o
= getopt(argc
, argv
, "f:m:i:hvVD1pt"))) {
783 if (srv
->config_storage
) {
784 log_error_write(srv
, __FILE__
, __LINE__
, "s",
785 "Can only read one config file. Use the include command to use multiple config files.");
790 if (config_read(srv
, optarg
)) {
796 buffer_copy_string(srv
->srvconf
.modules_dir
, optarg
);
800 long timeout
= strtol(optarg
, &endptr
, 0);
801 if (!*optarg
|| *endptr
|| timeout
< 0) {
802 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
803 "Invalid idle timeout value:", optarg
);
807 idle_limit
= (time_t)timeout
;
810 case 'p': print_config
= 1; break;
811 case 't': ++test_config
; break;
812 case '1': oneshot_fd
= dup(STDIN_FILENO
); break;
813 case 'D': srv
->srvconf
.dont_daemonize
= 1; break;
814 case 'v': show_version(); server_free(srv
); return 0;
815 case 'V': show_features(); server_free(srv
); return 0;
816 case 'h': show_help(); server_free(srv
); return 0;
824 if (!srv
->config_storage
) {
825 log_error_write(srv
, __FILE__
, __LINE__
, "s",
826 "No configuration available. Try using -f option.");
833 data_unset
*dc
= srv
->config_context
->data
[0];
836 fprintf(stdout
, "\n");
838 /* shouldn't happend */
839 fprintf(stderr
, "global config not found\n");
844 if (1 == test_config
) {
845 printf("Syntax OK\n");
846 } else { /*(test_config > 1)*/
848 srv
->srvconf
.preflight_check
= 1;
849 srv
->srvconf
.dont_daemonize
= 1;
850 buffer_reset(srv
->srvconf
.pid_file
);
854 if (test_config
|| print_config
) {
860 if (oneshot_fd
<= STDERR_FILENO
) {
861 log_error_write(srv
, __FILE__
, __LINE__
, "s",
862 "Invalid fds at startup with lighttpd -1");
866 graceful_shutdown
= 1;
867 srv
->sockets_disabled
= 1;
868 srv
->srvconf
.dont_daemonize
= 1;
869 buffer_reset(srv
->srvconf
.pid_file
);
870 if (srv
->srvconf
.max_worker
) {
871 srv
->srvconf
.max_worker
= 0;
872 log_error_write(srv
, __FILE__
, __LINE__
, "s",
873 "server one-shot command line option disables server.max-worker config file option.");
877 /* close stdin and stdout, as they are not needed */
878 openDevNull(STDIN_FILENO
);
879 openDevNull(STDOUT_FILENO
);
881 if (0 != config_set_defaults(srv
)) {
882 log_error_write(srv
, __FILE__
, __LINE__
, "s",
883 "setting default values failed");
890 if (!i_am_root
&& issetugid()) {
891 /* we are setuid-root */
893 log_error_write(srv
, __FILE__
, __LINE__
, "s",
894 "Are you nuts ? Don't apply a SUID bit to this binary");
901 /* check document-root */
902 if (buffer_string_is_empty(srv
->config_storage
[0]->document_root
)) {
903 log_error_write(srv
, __FILE__
, __LINE__
, "s",
904 "document-root is not set\n");
911 if (plugins_load(srv
)) {
912 log_error_write(srv
, __FILE__
, __LINE__
, "s",
913 "loading plugins finally failed");
921 /* open pid file BEFORE chroot */
922 if (!buffer_string_is_empty(srv
->srvconf
.pid_file
)) {
923 if (-1 == (pid_fd
= fdevent_open_cloexec(srv
->srvconf
.pid_file
->ptr
, O_WRONLY
| O_CREAT
| O_EXCL
| O_TRUNC
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
))) {
925 if (errno
!= EEXIST
) {
926 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
927 "opening pid-file failed:", srv
->srvconf
.pid_file
, strerror(errno
));
931 if (0 != stat(srv
->srvconf
.pid_file
->ptr
, &st
)) {
932 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
933 "stating existing pid-file failed:", srv
->srvconf
.pid_file
, strerror(errno
));
936 if (!S_ISREG(st
.st_mode
)) {
937 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
938 "pid-file exists and isn't regular file:", srv
->srvconf
.pid_file
);
942 if (-1 == (pid_fd
= open(srv
->srvconf
.pid_file
->ptr
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
))) {
943 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
944 "opening pid-file failed:", srv
->srvconf
.pid_file
, strerror(errno
));
950 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
951 /* select limits itself
953 * as it is a hard limit and will lead to a segfault we add some safety
955 srv
->max_fds
= FD_SETSIZE
- 200;
961 struct group
*grp
= NULL
;
962 struct passwd
*pwd
= NULL
;
965 #ifdef HAVE_VALGRIND_VALGRIND_H
966 if (RUNNING_ON_VALGRIND
) use_rlimit
= 0;
969 #ifdef HAVE_GETRLIMIT
970 if (0 != getrlimit(RLIMIT_NOFILE
, &rlim
)) {
971 log_error_write(srv
, __FILE__
, __LINE__
,
972 "ss", "couldn't get 'max filedescriptors'",
977 if (use_rlimit
&& srv
->srvconf
.max_fds
) {
980 rlim
.rlim_cur
= srv
->srvconf
.max_fds
;
981 rlim
.rlim_max
= srv
->srvconf
.max_fds
;
983 if (0 != setrlimit(RLIMIT_NOFILE
, &rlim
)) {
984 log_error_write(srv
, __FILE__
, __LINE__
,
985 "ss", "couldn't set 'max filedescriptors'",
991 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
992 srv
->max_fds
= rlim
.rlim_cur
< (rlim_t
)FD_SETSIZE
- 200 ? (int)rlim
.rlim_cur
: (int)FD_SETSIZE
- 200;
994 srv
->max_fds
= rlim
.rlim_cur
;
997 /* set core file rlimit, if enable_cores is set */
998 if (use_rlimit
&& srv
->srvconf
.enable_cores
&& getrlimit(RLIMIT_CORE
, &rlim
) == 0) {
999 rlim
.rlim_cur
= rlim
.rlim_max
;
1000 setrlimit(RLIMIT_CORE
, &rlim
);
1003 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
1004 /* don't raise the limit above FD_SET_SIZE */
1005 if (srv
->max_fds
> ((int)FD_SETSIZE
) - 200) {
1006 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1007 "can't raise max filedescriptors above", FD_SETSIZE
- 200,
1008 "if event-handler is 'select'. Use 'poll' or something else or reduce server.max-fds.");
1015 /* set user and group */
1016 if (!buffer_string_is_empty(srv
->srvconf
.groupname
)) {
1017 if (NULL
== (grp
= getgrnam(srv
->srvconf
.groupname
->ptr
))) {
1018 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1019 "can't find groupname", srv
->srvconf
.groupname
);
1024 if (!buffer_string_is_empty(srv
->srvconf
.username
)) {
1025 if (NULL
== (pwd
= getpwnam(srv
->srvconf
.username
->ptr
))) {
1026 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1027 "can't find username", srv
->srvconf
.username
);
1031 if (pwd
->pw_uid
== 0) {
1032 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1033 "I will not set uid to 0\n");
1037 if (NULL
== grp
&& NULL
== (grp
= getgrgid(pwd
->pw_gid
))) {
1038 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1039 "can't find group id", pwd
->pw_gid
);
1045 if (grp
->gr_gid
== 0) {
1046 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1047 "I will not set gid to 0\n");
1052 /* we need root-perms for port < 1024 */
1053 if (0 != network_init(srv
)) {
1061 * Change group before chroot, when we have access
1065 if (-1 == setgid(grp
->gr_gid
)) {
1066 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "setgid failed: ", strerror(errno
));
1069 if (-1 == setgroups(0, NULL
)) {
1070 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "setgroups failed: ", strerror(errno
));
1073 if (!buffer_string_is_empty(srv
->srvconf
.username
)) {
1074 initgroups(srv
->srvconf
.username
->ptr
, grp
->gr_gid
);
1079 if (!buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
1082 if (-1 == chroot(srv
->srvconf
.changeroot
->ptr
)) {
1083 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "chroot failed: ", strerror(errno
));
1086 if (-1 == chdir("/")) {
1087 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "chdir failed: ", strerror(errno
));
1093 /* drop root privs */
1095 if (-1 == setuid(pwd
->pw_uid
)) {
1096 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "setuid failed: ", strerror(errno
));
1101 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
1103 * on IRIX 6.5.30 they have prctl() but no DUMPABLE
1105 if (srv
->srvconf
.enable_cores
) {
1106 prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1111 #ifdef HAVE_GETRLIMIT
1112 if (0 != getrlimit(RLIMIT_NOFILE
, &rlim
)) {
1113 log_error_write(srv
, __FILE__
, __LINE__
,
1114 "ss", "couldn't get 'max filedescriptors'",
1120 * we are not root can can't increase the fd-limit above rlim_max, but we can reduce it
1122 if (srv
->srvconf
.max_fds
&& srv
->srvconf
.max_fds
<= rlim
.rlim_max
) {
1125 rlim
.rlim_cur
= srv
->srvconf
.max_fds
;
1127 if (0 != setrlimit(RLIMIT_NOFILE
, &rlim
)) {
1128 log_error_write(srv
, __FILE__
, __LINE__
,
1129 "ss", "couldn't set 'max filedescriptors'",
1135 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
1136 srv
->max_fds
= rlim
.rlim_cur
< (rlim_t
)FD_SETSIZE
- 200 ? (int)rlim
.rlim_cur
: (int)FD_SETSIZE
- 200;
1138 srv
->max_fds
= rlim
.rlim_cur
;
1141 /* set core file rlimit, if enable_cores is set */
1142 if (srv
->srvconf
.enable_cores
&& getrlimit(RLIMIT_CORE
, &rlim
) == 0) {
1143 rlim
.rlim_cur
= rlim
.rlim_max
;
1144 setrlimit(RLIMIT_CORE
, &rlim
);
1148 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
1149 /* don't raise the limit above FD_SET_SIZE */
1150 if (srv
->max_fds
> ((int)FD_SETSIZE
) - 200) {
1151 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1152 "can't raise max filedescriptors above", FD_SETSIZE
- 200,
1153 "if event-handler is 'select'. Use 'poll' or something else or reduce server.max-fds.");
1158 if (0 != network_init(srv
)) {
1167 if (srv
->srvconf
.max_conns
> srv
->max_fds
/2) {
1168 /* we can't have more connections than max-fds/2 */
1169 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "can't have more connections than fds/2: ", srv
->srvconf
.max_conns
, srv
->max_fds
);
1170 srv
->max_conns
= srv
->max_fds
/2;
1171 } else if (srv
->srvconf
.max_conns
) {
1172 /* otherwise respect the wishes of the user */
1173 srv
->max_conns
= srv
->srvconf
.max_conns
;
1175 /* or use the default: we really don't want to hit max-fds */
1176 srv
->max_conns
= srv
->max_fds
/3;
1179 if (HANDLER_GO_ON
!= plugins_call_init(srv
)) {
1180 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Initialization of plugins failed. Going down.");
1190 /* network is up, let's deamonize ourself */
1191 if (srv
->srvconf
.dont_daemonize
== 0) {
1192 parent_pipe_fd
= daemonize();
1197 #ifdef HAVE_SIGACTION
1198 memset(&act
, 0, sizeof(act
));
1199 act
.sa_handler
= SIG_IGN
;
1200 sigaction(SIGPIPE
, &act
, NULL
);
1201 sigaction(SIGUSR1
, &act
, NULL
);
1202 # if defined(SA_SIGINFO)
1203 act
.sa_sigaction
= sigaction_handler
;
1204 sigemptyset(&act
.sa_mask
);
1205 act
.sa_flags
= SA_SIGINFO
;
1207 act
.sa_handler
= signal_handler
;
1208 sigemptyset(&act
.sa_mask
);
1211 sigaction(SIGINT
, &act
, NULL
);
1212 sigaction(SIGTERM
, &act
, NULL
);
1213 sigaction(SIGHUP
, &act
, NULL
);
1214 sigaction(SIGALRM
, &act
, NULL
);
1216 /* it should be safe to restart syscalls after SIGCHLD */
1217 act
.sa_flags
|= SA_RESTART
| SA_NOCLDSTOP
;
1218 sigaction(SIGCHLD
, &act
, NULL
);
1220 #elif defined(HAVE_SIGNAL)
1221 /* ignore the SIGPIPE from sendfile() */
1222 signal(SIGPIPE
, SIG_IGN
);
1223 signal(SIGUSR1
, SIG_IGN
);
1224 signal(SIGALRM
, signal_handler
);
1225 signal(SIGTERM
, signal_handler
);
1226 signal(SIGHUP
, signal_handler
);
1227 signal(SIGCHLD
, signal_handler
);
1228 signal(SIGINT
, signal_handler
);
1232 signal(SIGALRM
, signal_handler
);
1234 /* setup periodic timer (1 second) */
1235 if (setitimer(ITIMER_REAL
, &interval
, NULL
)) {
1236 log_error_write(srv
, __FILE__
, __LINE__
, "s", "setting timer failed");
1240 getitimer(ITIMER_REAL
, &interval
);
1244 srv
->gid
= getgid();
1245 srv
->uid
= getuid();
1247 /* write pid file */
1249 buffer_copy_int(srv
->tmp_buf
, getpid());
1250 buffer_append_string_len(srv
->tmp_buf
, CONST_STR_LEN("\n"));
1251 if (-1 == write_all(pid_fd
, CONST_BUF_LEN(srv
->tmp_buf
))) {
1252 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't write pid file:", strerror(errno
));
1258 /* Close stderr ASAP in the child process to make sure that nothing
1259 * is being written to that fd which may not be valid anymore. */
1260 if (!srv
->srvconf
.preflight_check
&& -1 == log_error_open(srv
)) {
1261 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Opening errorlog failed. Going down.");
1269 if (HANDLER_GO_ON
!= plugins_call_set_defaults(srv
)) {
1270 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Configuration of plugins failed. Going down.");
1279 /* settings might be enabled during module config set defaults */
1280 srv
->config_storage
[0]->high_precision_timestamps
= srv
->srvconf
.high_precision_timestamps
;
1282 /* dump unused config-keys */
1283 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
1284 array
*config
= ((data_config
*)srv
->config_context
->data
[i
])->value
;
1287 for (j
= 0; config
&& j
< config
->used
; j
++) {
1288 data_unset
*du
= config
->data
[j
];
1290 /* all var.* is known as user defined variable */
1291 if (strncmp(du
->key
->ptr
, "var.", sizeof("var.") - 1) == 0) {
1295 if (NULL
== array_get_element(srv
->config_touched
, du
->key
->ptr
)) {
1296 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1297 "WARNING: unknown config-key:",
1304 if (srv
->config_unsupported
) {
1305 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1306 "Configuration contains unsupported keys. Going down.");
1309 if (srv
->config_deprecated
) {
1310 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1311 "Configuration contains deprecated keys. Going down.");
1314 if (srv
->config_unsupported
|| srv
->config_deprecated
) {
1322 if (srv
->srvconf
.preflight_check
) {
1323 /*printf("Preflight OK");*//*(stdout reopened to /dev/null)*/
1334 * notify daemonize-grandparent of successful startup
1335 * do this before any further forking is done (workers)
1337 if (srv
->srvconf
.dont_daemonize
== 0) {
1338 if (0 > write(parent_pipe_fd
, "", 1)) return -1;
1339 close(parent_pipe_fd
);
1342 if (idle_limit
&& srv
->srvconf
.max_worker
) {
1343 srv
->srvconf
.max_worker
= 0;
1344 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1345 "server idle time limit command line option disables server.max-worker config file option.");
1348 /* start watcher and workers */
1349 num_childs
= srv
->srvconf
.max_worker
;
1350 if (num_childs
> 0) {
1352 while (!child
&& !srv_shutdown
&& !graceful_shutdown
) {
1353 if (num_childs
> 0) {
1367 if (-1 != wait(&status
)) {
1369 * one of our workers went away
1376 * if we receive a SIGHUP we have to close our logs ourself as we don't
1377 * have the mainloop who can help us here
1379 if (handle_sig_hup
) {
1382 log_error_cycle(srv
);
1385 * forward to all procs in the process-group
1387 * we also send it ourself
1389 if (!forwarded_sig_hup
&& 0 != srv
->srvconf
.max_worker
) {
1390 forwarded_sig_hup
= 1;
1403 * for the parent this is the exit-point
1407 * kill all children too
1409 if (graceful_shutdown
) {
1411 } else if (srv_shutdown
) {
1415 remove_pid_file(srv
, &pid_fd
);
1416 log_error_close(srv
);
1418 connections_free(srv
);
1425 * make sure workers do not muck with pid-file
1431 buffer_reset(srv
->srvconf
.pid_file
);
1437 if (NULL
== (srv
->ev
= fdevent_init(srv
, srv
->max_fds
+ 1, srv
->event_handler
))) {
1438 log_error_write(srv
, __FILE__
, __LINE__
,
1439 "s", "fdevent_init failed");
1443 /* libev backend overwrites our SIGCHLD handler and calls waitpid on SIGCHLD; we want our own SIGCHLD handling. */
1444 #ifdef HAVE_SIGACTION
1445 sigaction(SIGCHLD
, &act
, NULL
);
1446 #elif defined(HAVE_SIGNAL)
1447 signal(SIGCHLD
, signal_handler
);
1451 * kqueue() is called here, select resets its internals,
1452 * all server sockets get their handlers
1455 if (0 != network_register_fdevents(srv
)) {
1463 /* might fail if user is using fam (not gamin) and famd isn't running */
1464 if (NULL
== (srv
->stat_cache
= stat_cache_init())) {
1465 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1466 "stat-cache could not be setup, dieing.");
1472 if (srv
->srvconf
.stat_cache_engine
== STAT_CACHE_ENGINE_FAM
) {
1473 if (0 != FAMOpen2(&srv
->stat_cache
->fam
, "lighttpd")) {
1474 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1475 "could not open a fam connection, dieing.");
1478 #ifdef HAVE_FAMNOEXISTS
1479 FAMNoExists(&srv
->stat_cache
->fam
);
1482 fd_close_on_exec(FAMCONNECTION_GETFD(&srv
->stat_cache
->fam
));
1483 fdevent_register(srv
->ev
, FAMCONNECTION_GETFD(&srv
->stat_cache
->fam
), stat_cache_handle_fdevent
, NULL
);
1484 fdevent_event_set(srv
->ev
, &(srv
->stat_cache
->fam_fcce_ndx
), FAMCONNECTION_GETFD(&srv
->stat_cache
->fam
), FDEVENT_IN
);
1489 /* get the current number of FDs */
1490 srv
->cur_fds
= open("/dev/null", O_RDONLY
);
1491 close(srv
->cur_fds
);
1493 for (i
= 0; i
< srv
->srv_sockets
.used
; i
++) {
1494 server_socket
*srv_socket
= srv
->srv_sockets
.ptr
[i
];
1495 if (srv
->sockets_disabled
) continue; /* lighttpd -1 (one-shot mode) */
1496 if (-1 == fdevent_fcntl_set_nb_cloexec_sock(srv
->ev
, srv_socket
->fd
)) {
1497 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed:", strerror(errno
));
1502 if (oneshot_fd
&& server_oneshot_init(srv
, oneshot_fd
)) {
1507 while (!srv_shutdown
) {
1512 if (handle_sig_hup
) {
1515 /* reset notification */
1519 /* cycle logfiles */
1521 switch(r
= plugins_call_handle_sighup(srv
)) {
1525 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "sighup-handler return with an error", r
);
1529 if (-1 == log_error_cycle(srv
)) {
1530 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cycling errorlog failed, dying");
1534 #ifdef HAVE_SIGACTION
1535 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
1536 "logfiles cycled UID =",
1537 last_sighup_info
.si_uid
,
1539 last_sighup_info
.si_pid
);
1541 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1547 if (handle_sig_alarm
) {
1551 /* reset notification */
1552 handle_sig_alarm
= 0;
1555 /* get current time */
1556 min_ts
= time(NULL
);
1558 if (min_ts
!= srv
->cur_ts
) {
1559 #ifdef DEBUG_CONNECTION_STATES
1562 connections
*conns
= srv
->conns
;
1565 switch(r
= plugins_call_handle_trigger(srv
)) {
1569 log_error_write(srv
, __FILE__
, __LINE__
, "s", "one of the triggers failed");
1572 log_error_write(srv
, __FILE__
, __LINE__
, "d", r
);
1576 /* trigger waitpid */
1577 srv
->cur_ts
= min_ts
;
1579 /* check idle time limit, if enabled */
1580 if (idle_limit
&& idle_limit
< min_ts
- last_active_ts
&& !graceful_shutdown
) {
1581 log_error_write(srv
, __FILE__
, __LINE__
, "sDs", "[note] idle timeout", (int)idle_limit
,
1582 "s exceeded, initiating graceful shutdown");
1583 graceful_shutdown
= 2; /* value 2 indicates idle timeout */
1586 /* cleanup stat-cache */
1587 stat_cache_trigger_cleanup(srv
);
1589 * check all connections for timeouts
1592 for (ndx
= 0; ndx
< conns
->used
; ndx
++) {
1593 connection
* const con
= conns
->ptr
[ndx
];
1594 const int waitevents
= fdevent_event_get_interest(srv
->ev
, con
->fd
);
1598 if (con
->state
== CON_STATE_CLOSE
) {
1599 if (srv
->cur_ts
- con
->close_timeout_ts
> HTTP_LINGER_TIMEOUT
) {
1602 } else if (waitevents
& FDEVENT_IN
) {
1603 if (con
->request_count
== 1 || con
->state
!= CON_STATE_READ
) { /* e.g. CON_STATE_READ_POST || CON_STATE_WRITE */
1604 if (srv
->cur_ts
- con
->read_idle_ts
> con
->conf
.max_read_idle
) {
1606 if (con
->conf
.log_request_handling
) {
1607 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1608 "connection closed - read timeout:", con
->fd
);
1611 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1615 if (srv
->cur_ts
- con
->read_idle_ts
> con
->keep_alive_idle
) {
1617 if (con
->conf
.log_request_handling
) {
1618 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1619 "connection closed - keep-alive timeout:", con
->fd
);
1622 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1628 /* max_write_idle timeout currently functions as backend timeout,
1629 * too, after response has been started.
1630 * future: have separate backend timeout, and then change this
1631 * to check for write interest before checking for timeout */
1632 /*if (waitevents & FDEVENT_OUT)*/
1633 if ((con
->state
== CON_STATE_WRITE
) &&
1634 (con
->write_request_ts
!= 0)) {
1636 if (srv
->cur_ts
- con
->write_request_ts
> 60) {
1637 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
1638 "connection closed - pre-write-request-timeout:", con
->fd
, srv
->cur_ts
- con
->write_request_ts
);
1642 if (srv
->cur_ts
- con
->write_request_ts
> con
->conf
.max_write_idle
) {
1644 if (con
->conf
.log_timeouts
) {
1645 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsosds",
1646 "NOTE: a request from",
1650 "timed out after writing",
1653 (int)con
->conf
.max_write_idle
,
1654 "seconds. If this a problem increase server.max-write-idle");
1656 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1661 /* we don't like div by zero */
1662 if (0 == (t_diff
= srv
->cur_ts
- con
->connection_start
)) t_diff
= 1;
1664 if (con
->traffic_limit_reached
&&
1665 (con
->conf
.kbytes_per_second
== 0 ||
1666 ((con
->bytes_written
/ t_diff
) < con
->conf
.kbytes_per_second
* 1024))) {
1667 /* enable connection again */
1668 con
->traffic_limit_reached
= 0;
1674 connection_state_machine(srv
, con
);
1676 con
->bytes_written_cur_second
= 0;
1677 *(con
->conf
.global_bytes_per_second_cnt_ptr
) = 0;
1679 #if DEBUG_CONNECTION_STATES
1681 fprintf(stderr
, "connection-state: ");
1685 fprintf(stderr
, "c[%d,%d]: %s ",
1688 connection_get_state(con
->state
));
1692 #ifdef DEBUG_CONNECTION_STATES
1693 if (cs
== 1) fprintf(stderr
, "\n");
1698 if (srv
->sockets_disabled
) {
1699 /* our server sockets are disabled, why ? */
1701 if ((srv
->cur_fds
+ srv
->want_fds
< srv
->max_fds
* 8 / 10) && /* we have enough unused fds */
1702 (srv
->conns
->used
<= srv
->max_conns
* 9 / 10) &&
1703 (0 == graceful_shutdown
)) {
1704 for (i
= 0; i
< srv
->srv_sockets
.used
; i
++) {
1705 server_socket
*srv_socket
= srv
->srv_sockets
.ptr
[i
];
1706 fdevent_event_set(srv
->ev
, &(srv_socket
->fde_ndx
), srv_socket
->fd
, FDEVENT_IN
);
1709 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] sockets enabled again");
1711 srv
->sockets_disabled
= 0;
1714 if ((srv
->cur_fds
+ srv
->want_fds
> srv
->max_fds
* 9 / 10) || /* out of fds */
1715 (srv
->conns
->used
>= srv
->max_conns
) || /* out of connections */
1716 (graceful_shutdown
)) { /* graceful_shutdown */
1718 /* disable server-fds */
1720 for (i
= 0; i
< srv
->srv_sockets
.used
; i
++) {
1721 server_socket
*srv_socket
= srv
->srv_sockets
.ptr
[i
];
1723 if (graceful_shutdown
) {
1724 /* we don't want this socket anymore,
1726 * closing it right away will make it possible for
1727 * the next lighttpd to take over (graceful restart)
1730 fdevent_event_del(srv
->ev
, &(srv_socket
->fde_ndx
), srv_socket
->fd
);
1731 fdevent_unregister(srv
->ev
, srv_socket
->fd
);
1732 close(srv_socket
->fd
);
1733 srv_socket
->fd
= -1;
1735 /* network_close() will cleanup after us */
1737 fdevent_event_set(srv
->ev
, &(srv_socket
->fde_ndx
), srv_socket
->fd
, 0);
1741 if (graceful_shutdown
) {
1742 remove_pid_file(srv
, &pid_fd
);
1743 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] graceful shutdown started");
1744 } else if (srv
->conns
->used
>= srv
->max_conns
) {
1745 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] sockets disabled, connection limit reached");
1747 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] sockets disabled, out-of-fds");
1750 srv
->sockets_disabled
= 1;
1754 if (graceful_shutdown
&& srv
->conns
->used
== 0) {
1755 /* we are in graceful shutdown phase and all connections are closed
1756 * we are ready to terminate without harming anyone */
1761 /* we still have some fds to share */
1762 if (srv
->want_fds
) {
1763 /* check the fdwaitqueue for waiting fds */
1764 int free_fds
= srv
->max_fds
- srv
->cur_fds
- 16;
1767 for (; free_fds
> 0 && NULL
!= (con
= fdwaitqueue_unshift(srv
, srv
->fdwaitqueue
)); free_fds
--) {
1768 connection_state_machine(srv
, con
);
1774 if ((n
= fdevent_poll(srv
->ev
, 1000)) > 0) {
1775 /* n is the number of events */
1778 last_active_ts
= srv
->cur_ts
;
1781 fdevent_handler handler
;
1784 fd_ndx
= fdevent_event_next_fdndx (srv
->ev
, fd_ndx
);
1785 if (-1 == fd_ndx
) break; /* not all fdevent handlers know how many fds got an event */
1787 revents
= fdevent_event_get_revent (srv
->ev
, fd_ndx
);
1788 fd
= fdevent_event_get_fd (srv
->ev
, fd_ndx
);
1789 handler
= fdevent_get_handler(srv
->ev
, fd
);
1790 context
= fdevent_get_context(srv
->ev
, fd
);
1791 if (NULL
!= handler
) {
1792 (*handler
)(srv
, context
, revents
);
1795 fdevent_sched_run(srv
, srv
->ev
);
1796 } else if (n
< 0 && errno
!= EINTR
) {
1797 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1798 "fdevent_poll failed:",
1802 for (ndx
= 0; ndx
< srv
->joblist
->used
; ndx
++) {
1803 connection
*con
= srv
->joblist
->ptr
[ndx
];
1804 connection_state_machine(srv
, con
);
1805 con
->in_joblist
= 0;
1808 srv
->joblist
->used
= 0;
1811 if (0 == graceful_shutdown
) {
1812 remove_pid_file(srv
, &pid_fd
);
1815 if (2 == graceful_shutdown
) { /* value 2 indicates idle timeout */
1816 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1817 "server stopped after idle timeout");
1819 #ifdef HAVE_SIGACTION
1820 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
1821 "server stopped by UID =",
1822 last_sigterm_info
.si_uid
,
1824 last_sigterm_info
.si_pid
);
1826 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1832 log_error_close(srv
);
1834 connections_free(srv
);