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();
248 srv
->cur_ts
= time(NULL
);
249 srv
->startup_ts
= srv
->cur_ts
;
251 srv
->conns
= calloc(1, sizeof(*srv
->conns
));
252 force_assert(srv
->conns
);
254 srv
->joblist
= calloc(1, sizeof(*srv
->joblist
));
255 force_assert(srv
->joblist
);
257 srv
->fdwaitqueue
= calloc(1, sizeof(*srv
->fdwaitqueue
));
258 force_assert(srv
->fdwaitqueue
);
260 srv
->srvconf
.modules
= array_init();
261 srv
->srvconf
.modules_dir
= buffer_init_string(LIBRARY_DIR
);
262 srv
->srvconf
.network_backend
= buffer_init();
263 srv
->srvconf
.upload_tempdirs
= array_init();
264 srv
->srvconf
.reject_expect_100_with_417
= 1;
265 srv
->srvconf
.xattr_name
= buffer_init_string("Content-Type");
266 srv
->srvconf
.http_header_strict
= 1;
267 srv
->srvconf
.http_host_strict
= 1; /*(implies http_host_normalize)*/
268 srv
->srvconf
.http_host_normalize
= 0;
269 srv
->srvconf
.high_precision_timestamps
= 0;
270 srv
->srvconf
.max_request_field_size
= 8192;
271 srv
->srvconf
.loadavg
[0] = 0.0;
272 srv
->srvconf
.loadavg
[1] = 0.0;
273 srv
->srvconf
.loadavg
[2] = 0.0;
276 srv
->errorlog_fd
= STDERR_FILENO
;
277 srv
->errorlog_mode
= ERRORLOG_FD
;
279 srv
->split_vals
= array_init();
284 static void server_free(server
*srv
) {
287 for (i
= 0; i
< FILE_CACHE_MAX
; i
++) {
288 buffer_free(srv
->mtime_cache
[i
].str
);
291 if (oneshot_fd
> 0) {
298 CLEAN(response_header
);
299 CLEAN(parse_full_path
);
303 CLEAN(response_range
);
306 CLEAN(cond_check_buf
);
308 CLEAN(srvconf
.errorlog_file
);
309 CLEAN(srvconf
.breakagelog_file
);
310 CLEAN(srvconf
.groupname
);
311 CLEAN(srvconf
.username
);
312 CLEAN(srvconf
.changeroot
);
313 CLEAN(srvconf
.bindhost
);
314 CLEAN(srvconf
.event_handler
);
315 CLEAN(srvconf
.pid_file
);
316 CLEAN(srvconf
.modules_dir
);
317 CLEAN(srvconf
.network_backend
);
318 CLEAN(srvconf
.xattr_name
);
320 CLEAN(tmp_chunk_len
);
324 fdevent_unregister(srv
->ev
, srv
->fd
);
326 fdevent_free(srv
->ev
);
330 if (srv
->config_storage
) {
331 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
332 specific_config
*s
= srv
->config_storage
[i
];
336 buffer_free(s
->document_root
);
337 buffer_free(s
->server_name
);
338 buffer_free(s
->server_tag
);
339 buffer_free(s
->ssl_pemfile
);
340 buffer_free(s
->ssl_ca_file
);
341 buffer_free(s
->ssl_cipher_list
);
342 buffer_free(s
->ssl_dh_file
);
343 buffer_free(s
->ssl_ec_curve
);
344 buffer_free(s
->error_handler
);
345 buffer_free(s
->error_handler_404
);
346 buffer_free(s
->errorfile_prefix
);
347 array_free(s
->mimetypes
);
348 buffer_free(s
->ssl_verifyclient_username
);
350 SSL_CTX_free(s
->ssl_ctx
);
351 EVP_PKEY_free(s
->ssl_pemfile_pkey
);
352 X509_free(s
->ssl_pemfile_x509
);
353 if (NULL
!= s
->ssl_ca_file_cert_names
) sk_X509_NAME_pop_free(s
->ssl_ca_file_cert_names
, X509_NAME_free
);
357 free(srv
->config_storage
);
358 srv
->config_storage
= NULL
;
364 CLEAN(config_context
);
365 CLEAN(config_touched
);
367 CLEAN(srvconf
.upload_tempdirs
);
370 joblist_free(srv
, srv
->joblist
);
371 fdwaitqueue_free(srv
, srv
->fdwaitqueue
);
373 if (srv
->stat_cache
) {
374 stat_cache_free(srv
->stat_cache
);
377 array_free(srv
->srvconf
.modules
);
378 array_free(srv
->split_vals
);
381 if (srv
->ssl_is_init
) {
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 */
387 CRYPTO_cleanup_all_ex_data();
389 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
390 ERR_remove_thread_state(NULL
);
403 static void remove_pid_file(server
*srv
, int *pid_fd
) {
404 if (!buffer_string_is_empty(srv
->srvconf
.pid_file
) && 0 <= *pid_fd
) {
405 if (0 != ftruncate(*pid_fd
, 0)) {
406 log_error_write(srv
, __FILE__
, __LINE__
, "sbds",
407 "ftruncate failed for:",
408 srv
->srvconf
.pid_file
,
417 if (!buffer_string_is_empty(srv
->srvconf
.pid_file
) &&
418 buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
419 if (0 != unlink(srv
->srvconf
.pid_file
->ptr
)) {
420 if (errno
!= EACCES
&& errno
!= EPERM
) {
421 log_error_write(srv
, __FILE__
, __LINE__
, "sbds",
422 "unlink failed for:",
423 srv
->srvconf
.pid_file
,
432 static server_socket
* server_oneshot_getsock(server
*srv
, sock_addr
*cnt_addr
) {
433 server_socket
*srv_socket
, *srv_socket_wild
= NULL
;
435 for (i
= 0; i
< srv
->srv_sockets
.used
; ++i
) {
436 srv_socket
= srv
->srv_sockets
.ptr
[i
];
437 if (cnt_addr
->plain
.sa_family
!= srv_socket
->addr
.plain
.sa_family
) continue;
438 switch (cnt_addr
->plain
.sa_family
) {
440 if (srv_socket
->addr
.ipv4
.sin_port
!= cnt_addr
->ipv4
.sin_port
) continue;
441 if (srv_socket
->addr
.ipv4
.sin_addr
.s_addr
== cnt_addr
->ipv4
.sin_addr
.s_addr
) {
444 if (srv_socket
->addr
.ipv4
.sin_addr
.s_addr
== htonl(INADDR_ANY
)) {
445 srv_socket_wild
= srv_socket
;
450 if (srv_socket
->addr
.ipv6
.sin6_port
!= cnt_addr
->ipv6
.sin6_port
) continue;
451 if (0 == memcmp(&srv_socket
->addr
.ipv6
.sin6_addr
, &cnt_addr
->ipv6
.sin6_addr
, sizeof(struct in6_addr
))) {
454 if (0 == memcmp(&srv_socket
->addr
.ipv6
.sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
))) {
455 srv_socket_wild
= srv_socket
;
461 if (0 == strcmp(srv_socket
->addr
.un
.sun_path
, cnt_addr
->un
.sun_path
)) {
470 if (NULL
!= srv_socket_wild
) {
471 return srv_socket_wild
;
472 } else if (srv
->srv_sockets
.used
) {
473 return srv
->srv_sockets
.ptr
[0];
475 log_error_write(srv
, __FILE__
, __LINE__
, "s", "no sockets configured");
481 static int server_oneshot_init(server
*srv
, int fd
) {
482 /* Note: does not work with netcat due to requirement that fd be socket.
483 * STDOUT_FILENO was not saved earlier in startup, and that is to where
484 * netcat expects output to be sent. Since lighttpd expects connections
485 * to be sockets, con->fd is where output is sent; separate fds are not
486 * stored for input and output, but netcat has different fds for stdin
487 * and * stdout. To support netcat, would additionally need to avoid
488 * S_ISSOCK(), getsockname(), and getpeername() below, reconstructing
489 * addresses from environment variables:
490 * NCAT_LOCAL_ADDR NCAT_LOCAL_PORT
491 * NCAT_REMOTE_ADDR NCAT_REMOTE_PORT
495 server_socket
*srv_socket
;
500 if (0 != fstat(fd
, &st
)) {
501 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fstat:", strerror(errno
));
505 if (!S_ISSOCK(st
.st_mode
)) {
506 /* require that fd is a socket
507 * (modules might expect STDIN_FILENO and STDOUT_FILENO opened to /dev/null) */
508 log_error_write(srv
, __FILE__
, __LINE__
, "s", "lighttpd -1 stdin is not a socket");
512 cnt_len
= sizeof(cnt_addr
);
513 if (0 != getsockname(fd
, (struct sockaddr
*)&cnt_addr
, &cnt_len
)) {
514 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "getsockname:", strerror(errno
));
518 srv_socket
= server_oneshot_getsock(srv
, &cnt_addr
);
519 if (NULL
== srv_socket
) return 0;
521 cnt_len
= sizeof(cnt_addr
);
522 if (0 != getpeername(fd
, (struct sockaddr
*)&cnt_addr
, &cnt_len
)) {
523 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "getpeername:", strerror(errno
));
527 if (cnt_addr
.plain
.sa_family
!= AF_UNIX
) {
528 network_accept_tcp_nagle_disable(fd
);
531 con
= connection_accepted(srv
, srv_socket
, &cnt_addr
, fd
);
532 if (NULL
== con
) return 0;
534 connection_state_machine(srv
, con
);
539 static void show_version (void) {
541 # define TEXT_SSL " (ssl)"
545 char *b
= PACKAGE_DESC TEXT_SSL \
546 " - a light and fast webserver\n" \
547 "Build-Date: " __DATE__
" " __TIME__
"\n";
550 write_all(STDOUT_FILENO
, b
, strlen(b
));
553 static void show_features (void) {
554 const char features
[] = ""
556 "\t+ select (generic)\n"
558 "\t- select (generic)\n"
565 #ifdef USE_LINUX_EPOLL
566 "\t+ epoll (Linux 2.6)\n"
568 "\t- epoll (Linux 2.6)\n"
570 #ifdef USE_SOLARIS_DEVPOLL
571 "\t+ /dev/poll (Solaris)\n"
573 "\t- /dev/poll (Solaris)\n"
575 #ifdef USE_SOLARIS_PORT
576 "\t+ eventports (Solaris)\n"
578 "\t- eventports (Solaris)\n"
580 #ifdef USE_FREEBSD_KQUEUE
581 "\t+ kqueue (FreeBSD)\n"
583 "\t- kqueue (FreeBSD)\n"
586 "\t+ libev (generic)\n"
588 "\t- libev (generic)\n"
590 "\nNetwork handler:\n\n"
591 #if defined USE_LINUX_SENDFILE
592 "\t+ linux-sendfile\n"
594 "\t- linux-sendfile\n"
596 #if defined USE_FREEBSD_SENDFILE
597 "\t+ freebsd-sendfile\n"
599 "\t- freebsd-sendfile\n"
601 #if defined USE_DARWIN_SENDFILE
602 "\t+ darwin-sendfile\n"
604 "\t- darwin-sendfile\n"
606 #if defined USE_SOLARIS_SENDFILEV
607 "\t+ solaris-sendfilev\n"
609 "\t- solaris-sendfilev\n"
611 #if defined USE_WRITEV
628 #if defined HAVE_ZLIB_H && defined HAVE_LIBZ
633 #if defined HAVE_BZLIB_H && defined HAVE_LIBBZ2
634 "\t+ bzip2 support\n"
636 "\t- bzip2 support\n"
638 #if defined(HAVE_CRYPT) || defined(HAVE_CRYPT_R) || defined(HAVE_LIBCRYPT)
639 "\t+ crypt support\n"
641 "\t- crypt support\n"
654 "\t+ MySQL support\n"
656 "\t- MySQL support\n"
659 "\t+ Kerberos support\n"
661 "\t- Kerberos support\n"
663 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)
669 "\t+ memcached support\n"
671 "\t- memcached support\n"
688 #ifdef HAVE_SQLITE3_H
689 "\t+ SQLite support\n"
691 "\t- SQLite support\n"
700 printf("\nEvent Handlers:\n\n%s", features
);
703 static void show_help (void) {
705 # define TEXT_SSL " (ssl)"
709 char *b
= PACKAGE_DESC TEXT_SSL
" ("__DATE__
" " __TIME__
")" \
710 " - a light and fast webserver\n" \
712 " -f <name> filename of the config-file\n" \
713 " -m <name> module directory (default: "LIBRARY_DIR
")\n" \
714 " -i <secs> graceful shutdown after <secs> of inactivity\n" \
715 " -1 process single (one) request on stdin socket, then exit\n" \
716 " -p print the parsed config-file in internal form, and exit\n" \
717 " -t test config-file syntax, then exit\n" \
718 " -tt test config-file syntax, load and init modules, then exit\n" \
719 " -D don't go to background (default: go to background)\n" \
720 " -v show version\n" \
721 " -V show compile-time features\n" \
722 " -h show this help\n" \
727 write_all(STDOUT_FILENO
, b
, strlen(b
));
730 int main (int argc
, char **argv
) {
732 int print_config
= 0;
739 time_t idle_limit
= 0, last_active_ts
= time(NULL
);
740 #ifdef HAVE_SIGACTION
741 struct sigaction act
;
743 #ifdef HAVE_GETRLIMIT
748 int parent_pipe_fd
= -1;
752 struct itimerval interval
;
754 interval
.it_interval
.tv_sec
= 1;
755 interval
.it_interval
.tv_usec
= 0;
756 interval
.it_value
.tv_sec
= 1;
757 interval
.it_value
.tv_usec
= 0;
760 /* for nice %b handling in strfime() */
761 setlocale(LC_TIME
, "C");
763 if (NULL
== (srv
= server_init())) {
764 fprintf(stderr
, "did this really happen?\n");
768 /* init structs done */
770 srv
->srvconf
.port
= 0;
772 i_am_root
= (getuid() == 0);
776 srv
->srvconf
.dont_daemonize
= 0;
777 srv
->srvconf
.preflight_check
= 0;
779 while(-1 != (o
= getopt(argc
, argv
, "f:m:i:hvVD1pt"))) {
782 if (srv
->config_storage
) {
783 log_error_write(srv
, __FILE__
, __LINE__
, "s",
784 "Can only read one config file. Use the include command to use multiple config files.");
789 if (config_read(srv
, optarg
)) {
795 buffer_copy_string(srv
->srvconf
.modules_dir
, optarg
);
799 long timeout
= strtol(optarg
, &endptr
, 0);
800 if (!*optarg
|| *endptr
|| timeout
< 0) {
801 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
802 "Invalid idle timeout value:", optarg
);
806 idle_limit
= (time_t)timeout
;
809 case 'p': print_config
= 1; break;
810 case 't': ++test_config
; break;
811 case '1': oneshot_fd
= dup(STDIN_FILENO
); break;
812 case 'D': srv
->srvconf
.dont_daemonize
= 1; break;
813 case 'v': show_version(); server_free(srv
); return 0;
814 case 'V': show_features(); server_free(srv
); return 0;
815 case 'h': show_help(); server_free(srv
); return 0;
823 if (!srv
->config_storage
) {
824 log_error_write(srv
, __FILE__
, __LINE__
, "s",
825 "No configuration available. Try using -f option.");
832 data_unset
*dc
= srv
->config_context
->data
[0];
835 fprintf(stdout
, "\n");
837 /* shouldn't happend */
838 fprintf(stderr
, "global config not found\n");
843 if (1 == test_config
) {
844 printf("Syntax OK\n");
845 } else { /*(test_config > 1)*/
847 srv
->srvconf
.preflight_check
= 1;
848 srv
->srvconf
.dont_daemonize
= 1;
849 buffer_reset(srv
->srvconf
.pid_file
);
853 if (test_config
|| print_config
) {
859 if (oneshot_fd
<= STDERR_FILENO
) {
860 log_error_write(srv
, __FILE__
, __LINE__
, "s",
861 "Invalid fds at startup with lighttpd -1");
865 graceful_shutdown
= 1;
866 srv
->sockets_disabled
= 1;
867 srv
->srvconf
.dont_daemonize
= 1;
868 buffer_reset(srv
->srvconf
.pid_file
);
869 if (srv
->srvconf
.max_worker
) {
870 srv
->srvconf
.max_worker
= 0;
871 log_error_write(srv
, __FILE__
, __LINE__
, "s",
872 "server one-shot command line option disables server.max-worker config file option.");
876 /* close stdin and stdout, as they are not needed */
877 openDevNull(STDIN_FILENO
);
878 openDevNull(STDOUT_FILENO
);
880 if (0 != config_set_defaults(srv
)) {
881 log_error_write(srv
, __FILE__
, __LINE__
, "s",
882 "setting default values failed");
889 if (!i_am_root
&& issetugid()) {
890 /* we are setuid-root */
892 log_error_write(srv
, __FILE__
, __LINE__
, "s",
893 "Are you nuts ? Don't apply a SUID bit to this binary");
900 /* check document-root */
901 if (buffer_string_is_empty(srv
->config_storage
[0]->document_root
)) {
902 log_error_write(srv
, __FILE__
, __LINE__
, "s",
903 "document-root is not set\n");
910 if (plugins_load(srv
)) {
911 log_error_write(srv
, __FILE__
, __LINE__
, "s",
912 "loading plugins finally failed");
920 /* open pid file BEFORE chroot */
921 if (!buffer_string_is_empty(srv
->srvconf
.pid_file
)) {
922 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
))) {
924 if (errno
!= EEXIST
) {
925 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
926 "opening pid-file failed:", srv
->srvconf
.pid_file
, strerror(errno
));
930 if (0 != stat(srv
->srvconf
.pid_file
->ptr
, &st
)) {
931 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
932 "stating existing pid-file failed:", srv
->srvconf
.pid_file
, strerror(errno
));
935 if (!S_ISREG(st
.st_mode
)) {
936 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
937 "pid-file exists and isn't regular file:", srv
->srvconf
.pid_file
);
941 if (-1 == (pid_fd
= open(srv
->srvconf
.pid_file
->ptr
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
))) {
942 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
943 "opening pid-file failed:", srv
->srvconf
.pid_file
, strerror(errno
));
949 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
950 /* select limits itself
952 * as it is a hard limit and will lead to a segfault we add some safety
954 srv
->max_fds
= FD_SETSIZE
- 200;
960 struct group
*grp
= NULL
;
961 struct passwd
*pwd
= NULL
;
964 #ifdef HAVE_VALGRIND_VALGRIND_H
965 if (RUNNING_ON_VALGRIND
) use_rlimit
= 0;
968 #ifdef HAVE_GETRLIMIT
969 if (0 != getrlimit(RLIMIT_NOFILE
, &rlim
)) {
970 log_error_write(srv
, __FILE__
, __LINE__
,
971 "ss", "couldn't get 'max filedescriptors'",
976 if (use_rlimit
&& srv
->srvconf
.max_fds
) {
979 rlim
.rlim_cur
= srv
->srvconf
.max_fds
;
980 rlim
.rlim_max
= srv
->srvconf
.max_fds
;
982 if (0 != setrlimit(RLIMIT_NOFILE
, &rlim
)) {
983 log_error_write(srv
, __FILE__
, __LINE__
,
984 "ss", "couldn't set 'max filedescriptors'",
990 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
991 srv
->max_fds
= rlim
.rlim_cur
< (rlim_t
)FD_SETSIZE
- 200 ? (int)rlim
.rlim_cur
: (int)FD_SETSIZE
- 200;
993 srv
->max_fds
= rlim
.rlim_cur
;
996 /* set core file rlimit, if enable_cores is set */
997 if (use_rlimit
&& srv
->srvconf
.enable_cores
&& getrlimit(RLIMIT_CORE
, &rlim
) == 0) {
998 rlim
.rlim_cur
= rlim
.rlim_max
;
999 setrlimit(RLIMIT_CORE
, &rlim
);
1002 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
1003 /* don't raise the limit above FD_SET_SIZE */
1004 if (srv
->max_fds
> ((int)FD_SETSIZE
) - 200) {
1005 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1006 "can't raise max filedescriptors above", FD_SETSIZE
- 200,
1007 "if event-handler is 'select'. Use 'poll' or something else or reduce server.max-fds.");
1014 /* set user and group */
1015 if (!buffer_string_is_empty(srv
->srvconf
.groupname
)) {
1016 if (NULL
== (grp
= getgrnam(srv
->srvconf
.groupname
->ptr
))) {
1017 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1018 "can't find groupname", srv
->srvconf
.groupname
);
1023 if (!buffer_string_is_empty(srv
->srvconf
.username
)) {
1024 if (NULL
== (pwd
= getpwnam(srv
->srvconf
.username
->ptr
))) {
1025 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1026 "can't find username", srv
->srvconf
.username
);
1030 if (pwd
->pw_uid
== 0) {
1031 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1032 "I will not set uid to 0\n");
1036 if (NULL
== grp
&& NULL
== (grp
= getgrgid(pwd
->pw_gid
))) {
1037 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1038 "can't find group id", pwd
->pw_gid
);
1044 if (grp
->gr_gid
== 0) {
1045 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1046 "I will not set gid to 0\n");
1051 /* we need root-perms for port < 1024 */
1052 if (0 != network_init(srv
)) {
1060 * Change group before chroot, when we have access
1064 if (-1 == setgid(grp
->gr_gid
)) {
1065 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "setgid failed: ", strerror(errno
));
1068 if (-1 == setgroups(0, NULL
)) {
1069 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "setgroups failed: ", strerror(errno
));
1072 if (!buffer_string_is_empty(srv
->srvconf
.username
)) {
1073 initgroups(srv
->srvconf
.username
->ptr
, grp
->gr_gid
);
1078 if (!buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
1081 if (-1 == chroot(srv
->srvconf
.changeroot
->ptr
)) {
1082 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "chroot failed: ", strerror(errno
));
1085 if (-1 == chdir("/")) {
1086 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "chdir failed: ", strerror(errno
));
1092 /* drop root privs */
1094 if (-1 == setuid(pwd
->pw_uid
)) {
1095 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "setuid failed: ", strerror(errno
));
1100 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
1102 * on IRIX 6.5.30 they have prctl() but no DUMPABLE
1104 if (srv
->srvconf
.enable_cores
) {
1105 prctl(PR_SET_DUMPABLE
, 1, 0, 0, 0);
1110 #ifdef HAVE_GETRLIMIT
1111 if (0 != getrlimit(RLIMIT_NOFILE
, &rlim
)) {
1112 log_error_write(srv
, __FILE__
, __LINE__
,
1113 "ss", "couldn't get 'max filedescriptors'",
1119 * we are not root can can't increase the fd-limit above rlim_max, but we can reduce it
1121 if (srv
->srvconf
.max_fds
&& srv
->srvconf
.max_fds
<= rlim
.rlim_max
) {
1124 rlim
.rlim_cur
= srv
->srvconf
.max_fds
;
1126 if (0 != setrlimit(RLIMIT_NOFILE
, &rlim
)) {
1127 log_error_write(srv
, __FILE__
, __LINE__
,
1128 "ss", "couldn't set 'max filedescriptors'",
1134 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
1135 srv
->max_fds
= rlim
.rlim_cur
< (rlim_t
)FD_SETSIZE
- 200 ? (int)rlim
.rlim_cur
: (int)FD_SETSIZE
- 200;
1137 srv
->max_fds
= rlim
.rlim_cur
;
1140 /* set core file rlimit, if enable_cores is set */
1141 if (srv
->srvconf
.enable_cores
&& getrlimit(RLIMIT_CORE
, &rlim
) == 0) {
1142 rlim
.rlim_cur
= rlim
.rlim_max
;
1143 setrlimit(RLIMIT_CORE
, &rlim
);
1147 if (srv
->event_handler
== FDEVENT_HANDLER_SELECT
) {
1148 /* don't raise the limit above FD_SET_SIZE */
1149 if (srv
->max_fds
> ((int)FD_SETSIZE
) - 200) {
1150 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1151 "can't raise max filedescriptors above", FD_SETSIZE
- 200,
1152 "if event-handler is 'select'. Use 'poll' or something else or reduce server.max-fds.");
1157 if (0 != network_init(srv
)) {
1166 if (srv
->srvconf
.max_conns
> srv
->max_fds
/2) {
1167 /* we can't have more connections than max-fds/2 */
1168 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "can't have more connections than fds/2: ", srv
->srvconf
.max_conns
, srv
->max_fds
);
1169 srv
->max_conns
= srv
->max_fds
/2;
1170 } else if (srv
->srvconf
.max_conns
) {
1171 /* otherwise respect the wishes of the user */
1172 srv
->max_conns
= srv
->srvconf
.max_conns
;
1174 /* or use the default: we really don't want to hit max-fds */
1175 srv
->max_conns
= srv
->max_fds
/3;
1178 if (HANDLER_GO_ON
!= plugins_call_init(srv
)) {
1179 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Initialization of plugins failed. Going down.");
1189 /* network is up, let's deamonize ourself */
1190 if (srv
->srvconf
.dont_daemonize
== 0) {
1191 parent_pipe_fd
= daemonize();
1196 #ifdef HAVE_SIGACTION
1197 memset(&act
, 0, sizeof(act
));
1198 act
.sa_handler
= SIG_IGN
;
1199 sigaction(SIGPIPE
, &act
, NULL
);
1200 sigaction(SIGUSR1
, &act
, NULL
);
1201 # if defined(SA_SIGINFO)
1202 act
.sa_sigaction
= sigaction_handler
;
1203 sigemptyset(&act
.sa_mask
);
1204 act
.sa_flags
= SA_SIGINFO
;
1206 act
.sa_handler
= signal_handler
;
1207 sigemptyset(&act
.sa_mask
);
1210 sigaction(SIGINT
, &act
, NULL
);
1211 sigaction(SIGTERM
, &act
, NULL
);
1212 sigaction(SIGHUP
, &act
, NULL
);
1213 sigaction(SIGALRM
, &act
, NULL
);
1215 /* it should be safe to restart syscalls after SIGCHLD */
1216 act
.sa_flags
|= SA_RESTART
| SA_NOCLDSTOP
;
1217 sigaction(SIGCHLD
, &act
, NULL
);
1219 #elif defined(HAVE_SIGNAL)
1220 /* ignore the SIGPIPE from sendfile() */
1221 signal(SIGPIPE
, SIG_IGN
);
1222 signal(SIGUSR1
, SIG_IGN
);
1223 signal(SIGALRM
, signal_handler
);
1224 signal(SIGTERM
, signal_handler
);
1225 signal(SIGHUP
, signal_handler
);
1226 signal(SIGCHLD
, signal_handler
);
1227 signal(SIGINT
, signal_handler
);
1231 signal(SIGALRM
, signal_handler
);
1233 /* setup periodic timer (1 second) */
1234 if (setitimer(ITIMER_REAL
, &interval
, NULL
)) {
1235 log_error_write(srv
, __FILE__
, __LINE__
, "s", "setting timer failed");
1239 getitimer(ITIMER_REAL
, &interval
);
1243 srv
->gid
= getgid();
1244 srv
->uid
= getuid();
1246 /* write pid file */
1248 buffer_copy_int(srv
->tmp_buf
, getpid());
1249 buffer_append_string_len(srv
->tmp_buf
, CONST_STR_LEN("\n"));
1250 if (-1 == write_all(pid_fd
, CONST_BUF_LEN(srv
->tmp_buf
))) {
1251 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "Couldn't write pid file:", strerror(errno
));
1257 /* Close stderr ASAP in the child process to make sure that nothing
1258 * is being written to that fd which may not be valid anymore. */
1259 if (!srv
->srvconf
.preflight_check
&& -1 == log_error_open(srv
)) {
1260 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Opening errorlog failed. Going down.");
1268 if (HANDLER_GO_ON
!= plugins_call_set_defaults(srv
)) {
1269 log_error_write(srv
, __FILE__
, __LINE__
, "s", "Configuration of plugins failed. Going down.");
1278 /* settings might be enabled during module config set defaults */
1279 srv
->config_storage
[0]->high_precision_timestamps
= srv
->srvconf
.high_precision_timestamps
;
1281 /* dump unused config-keys */
1282 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
1283 array
*config
= ((data_config
*)srv
->config_context
->data
[i
])->value
;
1286 for (j
= 0; config
&& j
< config
->used
; j
++) {
1287 data_unset
*du
= config
->data
[j
];
1289 /* all var.* is known as user defined variable */
1290 if (strncmp(du
->key
->ptr
, "var.", sizeof("var.") - 1) == 0) {
1294 if (NULL
== array_get_element(srv
->config_touched
, du
->key
->ptr
)) {
1295 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1296 "WARNING: unknown config-key:",
1303 if (srv
->config_unsupported
) {
1304 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1305 "Configuration contains unsupported keys. Going down.");
1308 if (srv
->config_deprecated
) {
1309 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1310 "Configuration contains deprecated keys. Going down.");
1313 if (srv
->config_unsupported
|| srv
->config_deprecated
) {
1321 if (srv
->srvconf
.preflight_check
) {
1322 /*printf("Preflight OK");*//*(stdout reopened to /dev/null)*/
1333 * notify daemonize-grandparent of successful startup
1334 * do this before any further forking is done (workers)
1336 if (srv
->srvconf
.dont_daemonize
== 0) {
1337 if (0 > write(parent_pipe_fd
, "", 1)) return -1;
1338 close(parent_pipe_fd
);
1341 if (idle_limit
&& srv
->srvconf
.max_worker
) {
1342 srv
->srvconf
.max_worker
= 0;
1343 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1344 "server idle time limit command line option disables server.max-worker config file option.");
1347 /* start watcher and workers */
1348 num_childs
= srv
->srvconf
.max_worker
;
1349 if (num_childs
> 0) {
1351 while (!child
&& !srv_shutdown
&& !graceful_shutdown
) {
1352 if (num_childs
> 0) {
1366 if (-1 != wait(&status
)) {
1368 * one of our workers went away
1375 * if we receive a SIGHUP we have to close our logs ourself as we don't
1376 * have the mainloop who can help us here
1378 if (handle_sig_hup
) {
1381 log_error_cycle(srv
);
1384 * forward to all procs in the process-group
1386 * we also send it ourself
1388 if (!forwarded_sig_hup
&& 0 != srv
->srvconf
.max_worker
) {
1389 forwarded_sig_hup
= 1;
1402 * for the parent this is the exit-point
1406 * kill all children too
1408 if (graceful_shutdown
) {
1410 } else if (srv_shutdown
) {
1414 remove_pid_file(srv
, &pid_fd
);
1415 log_error_close(srv
);
1417 connections_free(srv
);
1424 * make sure workers do not muck with pid-file
1430 buffer_reset(srv
->srvconf
.pid_file
);
1436 if (NULL
== (srv
->ev
= fdevent_init(srv
, srv
->max_fds
+ 1, srv
->event_handler
))) {
1437 log_error_write(srv
, __FILE__
, __LINE__
,
1438 "s", "fdevent_init failed");
1442 /* libev backend overwrites our SIGCHLD handler and calls waitpid on SIGCHLD; we want our own SIGCHLD handling. */
1443 #ifdef HAVE_SIGACTION
1444 sigaction(SIGCHLD
, &act
, NULL
);
1445 #elif defined(HAVE_SIGNAL)
1446 signal(SIGCHLD
, signal_handler
);
1450 * kqueue() is called here, select resets its internals,
1451 * all server sockets get their handlers
1454 if (0 != network_register_fdevents(srv
)) {
1462 /* might fail if user is using fam (not gamin) and famd isn't running */
1463 if (NULL
== (srv
->stat_cache
= stat_cache_init())) {
1464 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1465 "stat-cache could not be setup, dieing.");
1471 if (srv
->srvconf
.stat_cache_engine
== STAT_CACHE_ENGINE_FAM
) {
1472 if (0 != FAMOpen2(&srv
->stat_cache
->fam
, "lighttpd")) {
1473 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1474 "could not open a fam connection, dieing.");
1477 #ifdef HAVE_FAMNOEXISTS
1478 FAMNoExists(&srv
->stat_cache
->fam
);
1481 fd_close_on_exec(FAMCONNECTION_GETFD(&srv
->stat_cache
->fam
));
1482 fdevent_register(srv
->ev
, FAMCONNECTION_GETFD(&srv
->stat_cache
->fam
), stat_cache_handle_fdevent
, NULL
);
1483 fdevent_event_set(srv
->ev
, &(srv
->stat_cache
->fam_fcce_ndx
), FAMCONNECTION_GETFD(&srv
->stat_cache
->fam
), FDEVENT_IN
);
1488 /* get the current number of FDs */
1489 srv
->cur_fds
= open("/dev/null", O_RDONLY
);
1490 close(srv
->cur_fds
);
1492 for (i
= 0; i
< srv
->srv_sockets
.used
; i
++) {
1493 server_socket
*srv_socket
= srv
->srv_sockets
.ptr
[i
];
1494 if (srv
->sockets_disabled
) continue; /* lighttpd -1 (one-shot mode) */
1495 if (-1 == fdevent_fcntl_set_nb_cloexec_sock(srv
->ev
, srv_socket
->fd
)) {
1496 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed:", strerror(errno
));
1501 if (oneshot_fd
&& server_oneshot_init(srv
, oneshot_fd
)) {
1506 while (!srv_shutdown
) {
1511 if (handle_sig_hup
) {
1514 /* reset notification */
1518 /* cycle logfiles */
1520 switch(r
= plugins_call_handle_sighup(srv
)) {
1524 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "sighup-handler return with an error", r
);
1528 if (-1 == log_error_cycle(srv
)) {
1529 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cycling errorlog failed, dying");
1533 #ifdef HAVE_SIGACTION
1534 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
1535 "logfiles cycled UID =",
1536 last_sighup_info
.si_uid
,
1538 last_sighup_info
.si_pid
);
1540 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1546 if (handle_sig_alarm
) {
1550 /* reset notification */
1551 handle_sig_alarm
= 0;
1554 /* get current time */
1555 min_ts
= time(NULL
);
1557 if (min_ts
!= srv
->cur_ts
) {
1558 #ifdef DEBUG_CONNECTION_STATES
1561 connections
*conns
= srv
->conns
;
1564 switch(r
= plugins_call_handle_trigger(srv
)) {
1568 log_error_write(srv
, __FILE__
, __LINE__
, "s", "one of the triggers failed");
1571 log_error_write(srv
, __FILE__
, __LINE__
, "d", r
);
1575 /* trigger waitpid */
1576 srv
->cur_ts
= min_ts
;
1578 /* check idle time limit, if enabled */
1579 if (idle_limit
&& idle_limit
< min_ts
- last_active_ts
&& !graceful_shutdown
) {
1580 log_error_write(srv
, __FILE__
, __LINE__
, "sDs", "[note] idle timeout", (int)idle_limit
,
1581 "s exceeded, initiating graceful shutdown");
1582 graceful_shutdown
= 2; /* value 2 indicates idle timeout */
1585 #ifdef HAVE_GETLOADAVG
1586 /* refresh loadavg data every 30 seconds */
1587 if (srv
->srvconf
.loadts
+ 30 < min_ts
) {
1588 if (-1 != getloadavg(srv
->srvconf
.loadavg
, 3)) {
1589 srv
->srvconf
.loadts
= min_ts
;
1594 /* cleanup stat-cache */
1595 stat_cache_trigger_cleanup(srv
);
1597 * check all connections for timeouts
1600 for (ndx
= 0; ndx
< conns
->used
; ndx
++) {
1601 connection
* const con
= conns
->ptr
[ndx
];
1602 const int waitevents
= fdevent_event_get_interest(srv
->ev
, con
->fd
);
1606 if (con
->state
== CON_STATE_CLOSE
) {
1607 if (srv
->cur_ts
- con
->close_timeout_ts
> HTTP_LINGER_TIMEOUT
) {
1610 } else if (waitevents
& FDEVENT_IN
) {
1611 if (con
->request_count
== 1 || con
->state
!= CON_STATE_READ
) { /* e.g. CON_STATE_READ_POST || CON_STATE_WRITE */
1612 if (srv
->cur_ts
- con
->read_idle_ts
> con
->conf
.max_read_idle
) {
1614 if (con
->conf
.log_request_handling
) {
1615 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1616 "connection closed - read timeout:", con
->fd
);
1619 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1623 if (srv
->cur_ts
- con
->read_idle_ts
> con
->keep_alive_idle
) {
1625 if (con
->conf
.log_request_handling
) {
1626 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1627 "connection closed - keep-alive timeout:", con
->fd
);
1630 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1636 /* max_write_idle timeout currently functions as backend timeout,
1637 * too, after response has been started.
1638 * future: have separate backend timeout, and then change this
1639 * to check for write interest before checking for timeout */
1640 /*if (waitevents & FDEVENT_OUT)*/
1641 if ((con
->state
== CON_STATE_WRITE
) &&
1642 (con
->write_request_ts
!= 0)) {
1644 if (srv
->cur_ts
- con
->write_request_ts
> 60) {
1645 log_error_write(srv
, __FILE__
, __LINE__
, "sdd",
1646 "connection closed - pre-write-request-timeout:", con
->fd
, srv
->cur_ts
- con
->write_request_ts
);
1650 if (srv
->cur_ts
- con
->write_request_ts
> con
->conf
.max_write_idle
) {
1652 if (con
->conf
.log_timeouts
) {
1653 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbsosds",
1654 "NOTE: a request from",
1658 "timed out after writing",
1661 (int)con
->conf
.max_write_idle
,
1662 "seconds. If this a problem increase server.max-write-idle");
1664 connection_set_state(srv
, con
, CON_STATE_ERROR
);
1669 /* we don't like div by zero */
1670 if (0 == (t_diff
= srv
->cur_ts
- con
->connection_start
)) t_diff
= 1;
1672 if (con
->traffic_limit_reached
&&
1673 (con
->conf
.kbytes_per_second
== 0 ||
1674 ((con
->bytes_written
/ t_diff
) < con
->conf
.kbytes_per_second
* 1024))) {
1675 /* enable connection again */
1676 con
->traffic_limit_reached
= 0;
1682 connection_state_machine(srv
, con
);
1684 con
->bytes_written_cur_second
= 0;
1685 *(con
->conf
.global_bytes_per_second_cnt_ptr
) = 0;
1687 #if DEBUG_CONNECTION_STATES
1689 fprintf(stderr
, "connection-state: ");
1693 fprintf(stderr
, "c[%d,%d]: %s ",
1696 connection_get_state(con
->state
));
1700 #ifdef DEBUG_CONNECTION_STATES
1701 if (cs
== 1) fprintf(stderr
, "\n");
1706 if (srv
->sockets_disabled
) {
1707 /* our server sockets are disabled, why ? */
1709 if ((srv
->cur_fds
+ srv
->want_fds
< srv
->max_fds
* 8 / 10) && /* we have enough unused fds */
1710 (srv
->conns
->used
<= srv
->max_conns
* 9 / 10) &&
1711 (0 == graceful_shutdown
)) {
1712 for (i
= 0; i
< srv
->srv_sockets
.used
; i
++) {
1713 server_socket
*srv_socket
= srv
->srv_sockets
.ptr
[i
];
1714 fdevent_event_set(srv
->ev
, &(srv_socket
->fde_ndx
), srv_socket
->fd
, FDEVENT_IN
);
1717 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] sockets enabled again");
1719 srv
->sockets_disabled
= 0;
1722 if ((srv
->cur_fds
+ srv
->want_fds
> srv
->max_fds
* 9 / 10) || /* out of fds */
1723 (srv
->conns
->used
>= srv
->max_conns
) || /* out of connections */
1724 (graceful_shutdown
)) { /* graceful_shutdown */
1726 /* disable server-fds */
1728 for (i
= 0; i
< srv
->srv_sockets
.used
; i
++) {
1729 server_socket
*srv_socket
= srv
->srv_sockets
.ptr
[i
];
1731 if (graceful_shutdown
) {
1732 /* we don't want this socket anymore,
1734 * closing it right away will make it possible for
1735 * the next lighttpd to take over (graceful restart)
1738 fdevent_event_del(srv
->ev
, &(srv_socket
->fde_ndx
), srv_socket
->fd
);
1739 fdevent_unregister(srv
->ev
, srv_socket
->fd
);
1740 close(srv_socket
->fd
);
1741 srv_socket
->fd
= -1;
1743 /* network_close() will cleanup after us */
1745 fdevent_event_set(srv
->ev
, &(srv_socket
->fde_ndx
), srv_socket
->fd
, 0);
1749 if (graceful_shutdown
) {
1750 remove_pid_file(srv
, &pid_fd
);
1751 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] graceful shutdown started");
1752 } else if (srv
->conns
->used
>= srv
->max_conns
) {
1753 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] sockets disabled, connection limit reached");
1755 log_error_write(srv
, __FILE__
, __LINE__
, "s", "[note] sockets disabled, out-of-fds");
1758 srv
->sockets_disabled
= 1;
1762 if (graceful_shutdown
&& srv
->conns
->used
== 0) {
1763 /* we are in graceful shutdown phase and all connections are closed
1764 * we are ready to terminate without harming anyone */
1769 /* we still have some fds to share */
1770 if (srv
->want_fds
) {
1771 /* check the fdwaitqueue for waiting fds */
1772 int free_fds
= srv
->max_fds
- srv
->cur_fds
- 16;
1775 for (; free_fds
> 0 && NULL
!= (con
= fdwaitqueue_unshift(srv
, srv
->fdwaitqueue
)); free_fds
--) {
1776 connection_state_machine(srv
, con
);
1782 if ((n
= fdevent_poll(srv
->ev
, 1000)) > 0) {
1783 /* n is the number of events */
1786 last_active_ts
= srv
->cur_ts
;
1789 fdevent_handler handler
;
1792 fd_ndx
= fdevent_event_next_fdndx (srv
->ev
, fd_ndx
);
1793 if (-1 == fd_ndx
) break; /* not all fdevent handlers know how many fds got an event */
1795 revents
= fdevent_event_get_revent (srv
->ev
, fd_ndx
);
1796 fd
= fdevent_event_get_fd (srv
->ev
, fd_ndx
);
1797 handler
= fdevent_get_handler(srv
->ev
, fd
);
1798 context
= fdevent_get_context(srv
->ev
, fd
);
1799 if (NULL
!= handler
) {
1800 (*handler
)(srv
, context
, revents
);
1803 fdevent_sched_run(srv
, srv
->ev
);
1804 } else if (n
< 0 && errno
!= EINTR
) {
1805 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1806 "fdevent_poll failed:",
1810 for (ndx
= 0; ndx
< srv
->joblist
->used
; ndx
++) {
1811 connection
*con
= srv
->joblist
->ptr
[ndx
];
1812 connection_state_machine(srv
, con
);
1813 con
->in_joblist
= 0;
1816 srv
->joblist
->used
= 0;
1819 if (0 == graceful_shutdown
) {
1820 remove_pid_file(srv
, &pid_fd
);
1823 if (2 == graceful_shutdown
) { /* value 2 indicates idle timeout */
1824 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1825 "server stopped after idle timeout");
1827 #ifdef HAVE_SIGACTION
1828 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
1829 "server stopped by UID =",
1830 last_sigterm_info
.si_uid
,
1832 last_sigterm_info
.si_pid
);
1834 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1840 log_error_close(srv
);
1842 connections_free(srv
);