1 #include "first.h" 2 3 #include "base.h" 4 #include "buffer.h" 5 #include "network.h" 6 #include "log.h" 7 #include "rand.h" 8 #include "chunk.h" 9 #include "h2.h" /* h2_send_1xx() */ 10 #include "http_auth.h" /* http_auth_dumbdata_reset() */ 11 #include "http_vhostdb.h" /* http_vhostdb_dumbdata_reset() */ 12 #include "fdevent.h" 13 #include "connections.h" 14 #include "sock_addr.h" 15 #include "stat_cache.h" 16 #include "plugin.h" 17 #include "plugin_config.h" /* config_plugin_value_tobool() */ 18 #include "network_write.h" /* network_write_show_handlers() */ 19 #include "reqpool.h" /* request_pool_init() request_pool_free() */ 20 #include "response.h" /* http_response_send_1xx_cb_set() strftime_cache_reset() */ 21 22 #ifdef HAVE_VERSIONSTAMP_H 23 # include "versionstamp.h" 24 #else 25 # define REPO_VERSION "" 26 #endif 27 28 #define PACKAGE_DESC PACKAGE_NAME "/" PACKAGE_VERSION REPO_VERSION 29 static const buffer default_server_tag = { CONST_STR_LEN(PACKAGE_DESC)+1, 0 }; 30 31 #include <sys/types.h> 32 #include <sys/time.h> 33 #include <sys/stat.h> 34 #include <sys/wait.h> 35 36 #include <string.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <unistd.h> 40 #include <stdlib.h> 41 #include <time.h> 42 #include <signal.h> 43 #include <locale.h> 44 45 #include <stdio.h> 46 47 #ifdef HAVE_GETOPT_H 48 # include <getopt.h> 49 #endif 50 51 #ifdef HAVE_VALGRIND_VALGRIND_H 52 # include <valgrind/valgrind.h> 53 #endif 54 55 #ifdef HAVE_SYS_WAIT_H 56 # include <sys/wait.h> 57 #endif 58 59 #ifdef HAVE_PWD_H 60 # include <grp.h> 61 # include <pwd.h> 62 #endif 63 64 #ifdef HAVE_SYS_LOADAVG_H 65 # include <sys/loadavg.h> 66 #endif 67 68 #ifdef HAVE_SYS_RESOURCE_H 69 # include <sys/resource.h> 70 #endif 71 72 #ifdef HAVE_SYS_PRCTL_H 73 # include <sys/prctl.h> 74 #endif 75 76 #include "sys-crypto.h" 77 #if defined(USE_OPENSSL_CRYPTO) \ 78 || defined(USE_MBEDTLS_CRYPTO) \ 79 || defined(USE_NSS_CRYPTO) \ 80 || defined(USE_GNUTLS_CRYPTO) \ 81 || defined(USE_WOLFTLS_CRYPTO) 82 #define TEXT_SSL " (ssl)" 83 #else 84 #define TEXT_SSL 85 #endif 86 87 #ifndef __sgi 88 /* IRIX doesn't like the alarm based time() optimization */ 89 /* #define USE_ALARM */ 90 #endif 91 92 static int oneshot_fd = 0; 93 static int oneshot_fdout = -1; 94 static fdnode *oneshot_fdn = NULL; 95 static int (*oneshot_read_cq)(connection *con, chunkqueue *cq, off_t max_bytes); 96 static volatile int pid_fd = -2; 97 static server_socket_array graceful_sockets; 98 static server_socket_array inherited_sockets; 99 static volatile sig_atomic_t graceful_restart = 0; 100 static volatile sig_atomic_t graceful_shutdown = 0; 101 static volatile sig_atomic_t srv_shutdown = 0; 102 static volatile sig_atomic_t handle_sig_child = 0; 103 static volatile sig_atomic_t handle_sig_alarm = 1; 104 static volatile sig_atomic_t handle_sig_hup = 0; 105 static time_t idle_limit = 0; 106 107 #if defined(HAVE_SIGACTION) && defined(SA_SIGINFO) 108 static volatile siginfo_t last_sigterm_info; 109 static volatile siginfo_t last_sighup_info; 110 111 static void sigaction_handler(int sig, siginfo_t *si, void *context) { 112 static const siginfo_t empty_siginfo; 113 UNUSED(context); 114 115 if (!si) *(const siginfo_t **)&si = &empty_siginfo; 116 117 switch (sig) { 118 case SIGTERM: 119 srv_shutdown = 1; 120 last_sigterm_info = *si; 121 break; 122 case SIGUSR1: 123 if (!graceful_shutdown) { 124 graceful_restart = 1; 125 graceful_shutdown = 1; 126 last_sigterm_info = *si; 127 } 128 break; 129 case SIGINT: 130 if (graceful_shutdown) { 131 if (2 == graceful_restart) 132 graceful_restart = 1; 133 else 134 srv_shutdown = 1; 135 } else { 136 graceful_shutdown = 1; 137 } 138 last_sigterm_info = *si; 139 140 break; 141 case SIGALRM: 142 handle_sig_alarm = 1; 143 break; 144 case SIGHUP: 145 handle_sig_hup = 1; 146 last_sighup_info = *si; 147 break; 148 case SIGCHLD: 149 handle_sig_child = 1; 150 break; 151 } 152 } 153 #elif defined(HAVE_SIGNAL) || defined(HAVE_SIGACTION) 154 static void signal_handler(int sig) { 155 switch (sig) { 156 case SIGTERM: srv_shutdown = 1; break; 157 case SIGUSR1: 158 if (!graceful_shutdown) { 159 graceful_restart = 1; 160 graceful_shutdown = 1; 161 } 162 break; 163 case SIGINT: 164 if (graceful_shutdown) { 165 if (2 == graceful_restart) 166 graceful_restart = 1; 167 else 168 srv_shutdown = 1; 169 } else { 170 graceful_shutdown = 1; 171 } 172 break; 173 case SIGALRM: handle_sig_alarm = 1; break; 174 case SIGHUP: handle_sig_hup = 1; break; 175 case SIGCHLD: handle_sig_child = 1; break; 176 } 177 } 178 #endif 179 180 #ifdef HAVE_FORK 181 static int daemonize(void) { 182 int pipefd[2]; 183 pid_t pid; 184 #ifdef SIGTTOU 185 signal(SIGTTOU, SIG_IGN); 186 #endif 187 #ifdef SIGTTIN 188 signal(SIGTTIN, SIG_IGN); 189 #endif 190 #ifdef SIGTSTP 191 signal(SIGTSTP, SIG_IGN); 192 #endif 193 194 if (pipe(pipefd) < 0) exit(-1); 195 196 if (0 > (pid = fork())) exit(-1); 197 198 if (0 < pid) { 199 char buf; 200 ssize_t bytes; 201 202 close(pipefd[1]); 203 /* parent waits for grandchild to be ready */ 204 do { 205 bytes = read(pipefd[0], &buf, sizeof(buf)); 206 } while (bytes < 0 && EINTR == errno); 207 close(pipefd[0]); 208 209 if (bytes <= 0) { 210 /* closed fd (without writing) == failure in grandchild */ 211 fputs("daemonized server failed to start; check error log for details\n", stderr); 212 exit(-1); 213 } 214 215 exit(0); 216 } 217 218 close(pipefd[0]); 219 220 if (-1 == setsid()) exit(0); 221 222 signal(SIGHUP, SIG_IGN); 223 224 if (0 != fork()) exit(0); 225 226 if (0 != chdir("/")) exit(0); 227 228 fdevent_setfd_cloexec(pipefd[1]); 229 return pipefd[1]; 230 } 231 #endif 232 233 __attribute_cold__ 234 static server *server_init(void) { 235 server *srv = calloc(1, sizeof(*srv)); 236 force_assert(srv); 237 #define CLEAN(x) \ 238 srv->x = buffer_init(); 239 240 CLEAN(tmp_buf); 241 #undef CLEAN 242 connection_joblist = &srv->joblist_A; 243 244 strftime_cache_reset(); 245 246 li_rand_reseed(); 247 248 srv->startup_ts = log_epoch_secs = time(NULL); 249 250 srv->errh = log_error_st_init(); 251 252 config_init(srv); 253 254 srv->request_env = plugins_call_handle_request_env; 255 256 srv->loadavg[0] = 0.0; 257 srv->loadavg[1] = 0.0; 258 srv->loadavg[2] = 0.0; 259 srv->stdin_fd = -1; 260 261 return srv; 262 } 263 264 __attribute_cold__ 265 static void server_free(server *srv) { 266 if (oneshot_fd > 0) { 267 if (oneshot_fdn) { 268 fdevent_fdnode_event_del(srv->ev, oneshot_fdn); 269 fdevent_unregister(srv->ev, oneshot_fd); 270 oneshot_fdn = NULL; 271 } 272 close(oneshot_fd); 273 } 274 if (oneshot_fdout >= 0) { 275 close(oneshot_fdout); 276 } 277 if (srv->stdin_fd >= 0) { 278 close(srv->stdin_fd); 279 } 280 281 #define CLEAN(x) \ 282 buffer_free(srv->x); 283 284 CLEAN(tmp_buf); 285 286 #undef CLEAN 287 288 fdevent_free(srv->ev); 289 290 config_free(srv); 291 292 free(srv->joblist_A.ptr); 293 free(srv->joblist_B.ptr); 294 free(srv->fdwaitqueue.ptr); 295 296 stat_cache_free(); 297 298 li_rand_cleanup(); 299 chunkqueue_chunk_pool_free(); 300 301 log_error_st_free(srv->errh); 302 free(srv); 303 } 304 305 __attribute_cold__ 306 static void remove_pid_file(server *srv) { 307 if (pid_fd <= -2) return; 308 if (!buffer_string_is_empty(srv->srvconf.pid_file) && 0 <= pid_fd) { 309 if (0 != ftruncate(pid_fd, 0)) { 310 log_perror(srv->errh, __FILE__, __LINE__, 311 "ftruncate failed for: %s", srv->srvconf.pid_file->ptr); 312 } 313 } 314 if (0 <= pid_fd) { 315 close(pid_fd); 316 pid_fd = -1; 317 } 318 if (!buffer_string_is_empty(srv->srvconf.pid_file) && 319 buffer_string_is_empty(srv->srvconf.changeroot)) { 320 if (0 != unlink(srv->srvconf.pid_file->ptr)) { 321 if (errno != EACCES && errno != EPERM) { 322 log_perror(srv->errh, __FILE__, __LINE__, 323 "unlink failed for: %s", srv->srvconf.pid_file->ptr); 324 } 325 } 326 } 327 } 328 329 330 __attribute_cold__ 331 static server_socket * server_oneshot_getsock(server *srv, sock_addr *cnt_addr) { 332 server_socket *srv_socket, *srv_socket_wild = NULL; 333 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) { 334 srv_socket = srv->srv_sockets.ptr[i]; 335 if (!sock_addr_is_port_eq(&srv_socket->addr,cnt_addr)) continue; 336 if (sock_addr_is_addr_eq(&srv_socket->addr,cnt_addr)) return srv_socket; 337 338 if (NULL != srv_socket_wild) continue; 339 if (sock_addr_is_addr_wildcard(&srv_socket->addr)) { 340 srv_socket_wild = srv_socket; 341 } 342 } 343 344 if (NULL != srv_socket_wild) { 345 return srv_socket_wild; 346 } else if (srv->srv_sockets.used) { 347 return srv->srv_sockets.ptr[0]; 348 } else { 349 log_error(srv->errh, __FILE__, __LINE__, "no sockets configured"); 350 return NULL; 351 } 352 } 353 354 355 static int server_oneshot_read_cq(connection *con, chunkqueue *cq, off_t max_bytes) { 356 /* temporary set con->fd to oneshot_fd (fd input) rather than outshot_fdout 357 * (lighttpd generally assumes operation on sockets, so this is a kludge) */ 358 int fd = con->fd; 359 con->fd = oneshot_fd; 360 int rc = oneshot_read_cq(con, cq, max_bytes); 361 con->fd = fd; 362 363 /* note: optimistic reads (elsewhere) may or may not be enough to re-enable 364 * read interest after FDEVENT_IN interest was paused for other reasons */ 365 366 const int events = fdevent_fdnode_interest(oneshot_fdn); 367 int n = con->is_readable ? 0 : FDEVENT_IN; 368 if (events & FDEVENT_RDHUP) 369 n |= FDEVENT_RDHUP; 370 fdevent_fdnode_event_set(con->srv->ev, oneshot_fdn, n); 371 return rc; 372 } 373 374 375 static handler_t server_oneshot_handle_fdevent(void *context, int revents) { 376 connection *con = context; 377 378 /* note: not sync'd with con->fdn or connection_set_fdevent_interest() */ 379 int rdhup = 0; 380 int n = fdevent_fdnode_interest(oneshot_fdn); 381 if (revents & FDEVENT_IN) 382 n &= ~FDEVENT_IN; 383 request_st * const r = &con->request; 384 if (r->state != CON_STATE_ERROR && (revents & (FDEVENT_HUP|FDEVENT_RDHUP))){ 385 revents &= ~(FDEVENT_HUP|FDEVENT_RDHUP); 386 /* copied and modified from connection_handle_fdevent() 387 * fdevent_is_tcp_half_closed() will fail on pipe 388 * and, besides, read end of pipes should treat POLLHUP as POLLRDHUP */ 389 n &= ~(FDEVENT_IN|FDEVENT_RDHUP); 390 rdhup = 1; 391 } 392 fdevent_fdnode_event_set(con->srv->ev, oneshot_fdn, n); 393 394 fdnode * const fdn = con->fdn; /* fdn->ctx == con */ 395 handler_t rc = ((fdevent_handler)NULL != fdn->handler) 396 ? (*fdn->handler)(con, revents) 397 : HANDLER_FINISHED; 398 399 if (rdhup) { 400 r->conf.stream_request_body &= 401 ~(FDEVENT_STREAM_REQUEST_BUFMIN|FDEVENT_STREAM_REQUEST_POLLIN); 402 r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLRDHUP; 403 r->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_TCP_FIN; 404 con->is_readable = 1; /*(can read 0 for end-of-stream)*/ 405 if (chunkqueue_is_empty(con->read_queue)) r->keep_alive = 0; 406 if (r->reqbody_length < -1) /*(transparent proxy mode; no more data)*/ 407 r->reqbody_length = r->reqbody_queue.bytes_in; 408 } 409 410 return rc; 411 } 412 413 414 __attribute_cold__ 415 static int server_oneshot_init_pipe(server *srv, int fdin, int fdout) { 416 /* Note: attempt to work with netcat pipes though other code expects socket. 417 * netcat has different fds (pipes) for stdin and stdout. To support 418 * netcat, need to avoid S_ISSOCK(), getsockname(), and getpeername(), 419 * reconstructing addresses from environment variables: 420 * NCAT_LOCAL_ADDR NCAT_LOCAL_PORT 421 * NCAT_REMOTE_ADDR NCAT_REMOTE_PORT 422 * NCAT_PROTO (TCP, UDP, SCTP) 423 */ 424 connection *con; 425 server_socket *srv_socket; 426 sock_addr cnt_addr; 427 428 /* detect if called from netcat or else fabricate localhost addrs */ 429 const char * const ncat = 430 getenv("NCAT_LOCAL_ADDR"); 431 const char * const ncat_local_addr = 432 ncat ? ncat : "127.0.0.1"; /*(fabricated)*/ 433 const char * const ncat_local_port = 434 ncat ? getenv("NCAT_LOCAL_PORT") : "80"; /*(fabricated)*/ 435 const char * const ncat_remote_addr = 436 ncat ? getenv("NCAT_REMOTE_ADDR") : "127.0.0.1"; /*(fabricated)*/ 437 const char * const ncat_remote_port = 438 ncat ? getenv("NCAT_REMOTE_PORT") : "48080"; /*(fabricated)*/ 439 if (NULL == ncat_local_addr || NULL == ncat_local_port) return 0; 440 if (NULL == ncat_remote_addr || NULL == ncat_remote_port) return 0; 441 442 const int family = ncat && strchr(ncat_local_addr,':') ? AF_INET6 : AF_INET; 443 unsigned short port; 444 445 port = (unsigned short)strtol(ncat_local_port, NULL, 10); 446 if (1 != sock_addr_inet_pton(&cnt_addr, ncat_local_addr, family, port)) { 447 log_error(srv->errh, __FILE__, __LINE__, "invalid local addr"); 448 return 0; 449 } 450 451 srv_socket = server_oneshot_getsock(srv, &cnt_addr); 452 if (NULL == srv_socket) return 0; 453 454 port = (unsigned short)strtol(ncat_remote_port, NULL, 10); 455 if (1 != sock_addr_inet_pton(&cnt_addr, ncat_remote_addr, family, port)) { 456 log_error(srv->errh, __FILE__, __LINE__, "invalid remote addr"); 457 return 0; 458 } 459 460 /*(must set flags; fd did not pass through fdevent accept() logic)*/ 461 if (-1 == fdevent_fcntl_set_nb_cloexec(fdin)) { 462 log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); 463 return 0; 464 } 465 if (-1 == fdevent_fcntl_set_nb_cloexec(fdout)) { 466 log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); 467 return 0; 468 } 469 470 con = connection_accepted(srv, srv_socket, &cnt_addr, fdout); 471 if (NULL == con) return 0; 472 473 /* note: existing routines assume socket, not pipe 474 * connections.c:connection_read_cq() 475 * uses recv() ifdef __WIN32 476 * passes S_IFSOCK to fdevent_ioctl_fionread() 477 * (The routine could be copied and modified, if required) 478 * This is unlikely to work if TLS is used over pipe since the SSL_CTX 479 * is associated with the other end of the pipe. However, if using 480 * pipes, using TLS is unexpected behavior. 481 */ 482 483 /*assert(oneshot_fd == fdin);*/ 484 oneshot_read_cq = con->network_read; 485 con->network_read = server_oneshot_read_cq; 486 oneshot_fdn = 487 fdevent_register(srv->ev, fdin, server_oneshot_handle_fdevent, con); 488 fdevent_fdnode_event_set(srv->ev, oneshot_fdn, FDEVENT_RDHUP); 489 490 connection_state_machine(con); 491 return 1; 492 } 493 494 495 __attribute_cold__ 496 static int server_oneshot_init(server *srv, int fd) { 497 connection *con; 498 server_socket *srv_socket; 499 sock_addr cnt_addr; 500 socklen_t cnt_len; 501 502 cnt_len = sizeof(cnt_addr); 503 if (0 != getsockname(fd, (struct sockaddr *)&cnt_addr, &cnt_len)) { 504 log_perror(srv->errh, __FILE__, __LINE__, "getsockname()"); 505 return 0; 506 } 507 508 srv_socket = server_oneshot_getsock(srv, &cnt_addr); 509 if (NULL == srv_socket) return 0; 510 511 #ifdef __clang_analyzer__ 512 memset(&cnt_addr, 0, sizeof(cnt_addr)); 513 #endif 514 cnt_len = sizeof(cnt_addr); 515 if (0 != getpeername(fd, (struct sockaddr *)&cnt_addr, &cnt_len)) { 516 log_perror(srv->errh, __FILE__, __LINE__, "getpeername()"); 517 return 0; 518 } 519 520 /*(must set flags; fd did not pass through fdevent accept() logic)*/ 521 if (-1 == fdevent_fcntl_set_nb_cloexec(fd)) { 522 log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); 523 return 0; 524 } 525 526 if (sock_addr_get_family(&cnt_addr) != AF_UNIX) { 527 network_accept_tcp_nagle_disable(fd); 528 } 529 530 con = connection_accepted(srv, srv_socket, &cnt_addr, fd); 531 if (NULL == con) return 0; 532 533 connection_state_machine(con); 534 return 1; 535 } 536 537 538 __attribute_cold__ 539 static void show_version (void) { 540 char *b = PACKAGE_DESC TEXT_SSL \ 541 " - a light and fast webserver\n" 542 #ifdef NONREPRODUCIBLE_BUILD 543 "Build-Date: " __DATE__ " " __TIME__ "\n"; 544 #endif 545 ; 546 write_all(STDOUT_FILENO, b, strlen(b)); 547 } 548 549 __attribute_cold__ 550 static void show_features (void) { 551 static const char features[] = 552 "\nFeatures:\n\n" 553 #ifdef HAVE_IPV6 554 "\t+ IPv6 support\n" 555 #else 556 "\t- IPv6 support\n" 557 #endif 558 #if defined HAVE_ZLIB_H && defined HAVE_LIBZ 559 "\t+ zlib support\n" 560 #else 561 "\t- zlib support\n" 562 #endif 563 #if defined HAVE_BZLIB_H && defined HAVE_LIBBZ2 564 "\t+ bzip2 support\n" 565 #else 566 "\t- bzip2 support\n" 567 #endif 568 #if defined HAVE_BROTLI_ENCODE_H && defined HAVE_BROTLI 569 "\t+ brotli support\n" 570 #else 571 "\t- brotli support\n" 572 #endif 573 #if defined(HAVE_CRYPT) || defined(HAVE_CRYPT_R) || defined(HAVE_LIBCRYPT) 574 "\t+ crypt support\n" 575 #else 576 "\t- crypt support\n" 577 #endif 578 #ifdef USE_OPENSSL_CRYPTO 579 "\t+ OpenSSL support\n" 580 #else 581 "\t- OpenSSL support\n" 582 #endif 583 #ifdef USE_MBEDTLS_CRYPTO 584 "\t+ mbedTLS support\n" 585 #else 586 "\t- mbedTLS support\n" 587 #endif 588 #ifdef USE_NSS_CRYPTO 589 "\t+ NSS crypto support\n" 590 #else 591 "\t- NSS crypto support\n" 592 #endif 593 #ifdef USE_GNUTLS_CRYPTO 594 "\t+ GnuTLS support\n" 595 #else 596 "\t- GnuTLS support\n" 597 #endif 598 #ifdef USE_WOLFSSL_CRYPTO 599 "\t+ WolfSSL support\n" 600 #else 601 "\t- WolfSSL support\n" 602 #endif 603 #ifdef USE_NETTLE_CRYPTO 604 "\t+ Nettle support\n" 605 #else 606 "\t- Nettle support\n" 607 #endif 608 #ifdef HAVE_LIBPCRE 609 "\t+ PCRE support\n" 610 #else 611 "\t- PCRE support\n" 612 #endif 613 #ifdef HAVE_MYSQL 614 "\t+ MySQL support\n" 615 #else 616 "\t- MySQL support\n" 617 #endif 618 #ifdef HAVE_PGSQL 619 "\t+ PgSQL support\n" 620 #else 621 "\t- PgSQL support\n" 622 #endif 623 #ifdef HAVE_DBI 624 "\t+ DBI support\n" 625 #else 626 "\t- DBI support\n" 627 #endif 628 #ifdef HAVE_KRB5 629 "\t+ Kerberos support\n" 630 #else 631 "\t- Kerberos support\n" 632 #endif 633 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER) 634 "\t+ LDAP support\n" 635 #else 636 "\t- LDAP support\n" 637 #endif 638 #ifdef HAVE_PAM 639 "\t+ PAM support\n" 640 #else 641 "\t- PAM support\n" 642 #endif 643 #ifdef USE_MEMCACHED 644 "\t+ memcached support\n" 645 #else 646 "\t- memcached support\n" 647 #endif 648 #ifdef HAVE_FAM_H 649 "\t+ FAM support\n" 650 #else 651 "\t- FAM support\n" 652 #endif 653 #ifdef HAVE_LUA_H 654 "\t+ LUA support\n" 655 #else 656 "\t- LUA support\n" 657 #endif 658 #ifdef HAVE_LIBXML_H 659 "\t+ xml support\n" 660 #else 661 "\t- xml support\n" 662 #endif 663 #ifdef HAVE_SQLITE3_H 664 "\t+ SQLite support\n" 665 #else 666 "\t- SQLite support\n" 667 #endif 668 #ifdef HAVE_GDBM_H 669 "\t+ GDBM support\n" 670 #else 671 "\t- GDBM support\n" 672 #endif 673 ; 674 show_version(); 675 printf("%s%s%s\n", fdevent_show_event_handlers(), network_write_show_handlers(), features); 676 } 677 678 __attribute_cold__ 679 static void show_help (void) { 680 char *b = PACKAGE_DESC TEXT_SSL 681 #ifdef NONREPRODUCIBLE_BUILD 682 " ("__DATE__ " " __TIME__ ")" 683 #endif 684 " - a light and fast webserver\n" \ 685 "usage:\n" \ 686 " -f <name> filename of the config-file\n" \ 687 " -m <name> module directory (default: "LIBRARY_DIR")\n" \ 688 " -i <secs> graceful shutdown after <secs> of inactivity\n" \ 689 " -1 process single (one) request on stdin socket, then exit\n" \ 690 " -p print the parsed config-file in internal form, and exit\n" \ 691 " -t test config-file syntax, then exit\n" \ 692 " -tt test config-file syntax, load and init modules, then exit\n" \ 693 " -D don't go to background (default: go to background)\n" \ 694 " -v show version\n" \ 695 " -V show compile-time features\n" \ 696 " -h show this help\n" \ 697 "\n" 698 ; 699 write_all(STDOUT_FILENO, b, strlen(b)); 700 } 701 702 __attribute_cold__ 703 static void server_sockets_save (server *srv) { /* graceful_restart */ 704 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) 705 srv->srv_sockets.ptr[i]->srv = NULL; /* srv will shortly be invalid */ 706 for (uint32_t i = 0; i < srv->srv_sockets_inherited.used; ++i) 707 srv->srv_sockets_inherited.ptr[i]->srv = NULL; /* srv to be invalid */ 708 memcpy(&graceful_sockets, &srv->srv_sockets, sizeof(server_socket_array)); 709 memset(&srv->srv_sockets, 0, sizeof(server_socket_array)); 710 memcpy(&inherited_sockets, &srv->srv_sockets_inherited, sizeof(server_socket_array)); 711 memset(&srv->srv_sockets_inherited, 0, sizeof(server_socket_array)); 712 } 713 714 __attribute_cold__ 715 static void server_sockets_restore (server *srv) { /* graceful_restart */ 716 memcpy(&srv->srv_sockets, &graceful_sockets, sizeof(server_socket_array)); 717 memset(&graceful_sockets, 0, sizeof(server_socket_array)); 718 memcpy(&srv->srv_sockets_inherited, &inherited_sockets, sizeof(server_socket_array)); 719 memset(&inherited_sockets, 0, sizeof(server_socket_array)); 720 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) 721 srv->srv_sockets.ptr[i]->srv = srv; /* update ptr */ 722 for (uint32_t i = 0; i < srv->srv_sockets_inherited.used; ++i) 723 srv->srv_sockets_inherited.ptr[i]->srv = srv; /* update ptr */ 724 } 725 726 __attribute_cold__ 727 static int server_sockets_set_nb_cloexec (server *srv) { 728 if (srv->sockets_disabled) return 0; /* lighttpd -1 (one-shot mode) */ 729 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) { 730 server_socket *srv_socket = srv->srv_sockets.ptr[i]; 731 if (-1 == fdevent_fcntl_set_nb_cloexec_sock(srv_socket->fd)) { 732 log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); 733 return -1; 734 } 735 } 736 return 0; 737 } 738 739 __attribute_cold__ 740 static void server_sockets_set_event (server *srv, int event) { 741 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) { 742 server_socket *srv_socket = srv->srv_sockets.ptr[i]; 743 fdevent_fdnode_event_set(srv->ev, srv_socket->fdn, event); 744 } 745 } 746 747 __attribute_cold__ 748 static void server_sockets_unregister (server *srv) { 749 if (2 == srv->sockets_disabled) return; 750 srv->sockets_disabled = 2; 751 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) 752 network_unregister_sock(srv, srv->srv_sockets.ptr[i]); 753 } 754 755 __attribute_cold__ 756 static void server_sockets_close (server *srv) { 757 /* closing socket right away will make it possible for the next lighttpd 758 * to take over (old-style graceful restart), but only if backends 759 * (e.g. fastcgi, scgi, etc) are independent from lighttpd, rather 760 * than started by lighttpd via "bin-path") 761 */ 762 if (3 == srv->sockets_disabled) return; 763 for (uint32_t i = 0; i < srv->srv_sockets.used; ++i) { 764 server_socket *srv_socket = srv->srv_sockets.ptr[i]; 765 if (-1 == srv_socket->fd) continue; 766 if (2 != srv->sockets_disabled) network_unregister_sock(srv,srv_socket); 767 close(srv_socket->fd); 768 srv_socket->fd = -1; 769 /* network_close() will cleanup after us */ 770 } 771 srv->sockets_disabled = 3; 772 } 773 774 __attribute_cold__ 775 static int server_graceful_state_bg (server *srv) { 776 /*assert(graceful_restart);*/ 777 /*(SIGUSR1 set to SIG_IGN in workers, so should not reach here if worker)*/ 778 if (srv_shutdown) return 0; 779 if (NULL == srv->srvconf.feature_flags) return 0; 780 781 /* check if server should fork and background (bg) itself 782 * to continue processing requests already in progress */ 783 data_unset * const du = 784 array_get_element_klen(srv->srvconf.feature_flags, 785 CONST_STR_LEN("server.graceful-restart-bg")); 786 if (!config_plugin_value_tobool(du, 0)) return 0; 787 788 /*(set flag to false to avoid repeating)*/ 789 if (du->type == TYPE_STRING) 790 buffer_copy_string_len(&((data_string *)du)->value, 791 CONST_STR_LEN("false")); 792 else /* (du->type == TYPE_INTEGER) */ 793 ((data_integer *)du)->value = 0; 794 795 /* require exec'd via absolute path or daemon in foreground 796 * and exec'd with path containing '/' (e.g. "./xxxxx") */ 797 char ** const argv = srv->argv; 798 if (0 == srv->srvconf.dont_daemonize 799 ? argv[0][0] != '/' 800 : NULL == strchr(argv[0], '/')) return 0; 801 802 #if 0 803 /* disabled; not fully implemented 804 * srv->srvconf.systemd_socket_activation might be cleared in network_init() 805 * leading to issuing a false warning 806 */ 807 /* warn if server.systemd-socket-activation not enabled 808 * (While this warns on existing config rather than new config, 809 * it is probably a decent predictor for presence in new config) */ 810 if (!srv->srvconf.systemd_socket_activation) 811 log_error(srv->errh, __FILE__, __LINE__, 812 "[note] server.systemd-socket-activation not enabled; " 813 "listen sockets will be closed and reopened"); 814 #endif 815 816 /* flush log buffers to avoid potential duplication of entries 817 * server_handle_sighup(srv) does the following, but skip logging */ 818 plugins_call_handle_sighup(srv); 819 config_log_error_cycle(srv); 820 821 /* backgrounding to continue processing requests in progress */ 822 /* re-exec lighttpd in original process 823 * Note: using path in re-exec is portable and allows lighttpd upgrade. 824 * OTOH, getauxval() AT_EXECFD and fexecve() could be used on Linux to 825 * re-exec without access to original executable on disk, which might be 826 * desirable in some situations, but is not implemented here. 827 * Alternatively, if argv[] was not available, could use readlink() on 828 * /proc/self/exe (Linux-specific), though there are ways on many other 829 * platforms to achieve the same: 830 * https://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe 831 */ 832 pid_t pid = fork(); 833 if (pid) { /* original process */ 834 if (pid < 0) return 0; 835 network_socket_activation_to_env(srv); 836 /*while (waitpid(pid, NULL, 0) < 0 && errno == EINTR) ;*//* detach? */ 837 execv(argv[0], argv); 838 _exit(1); 839 } 840 /* else child/grandchild */ 841 842 /*if (-1 == setsid()) _exit(1);*//* should we detach? */ 843 server_sockets_close(srv); /*(close before parent reaps pid in waitpid)*/ 844 /*if (0 != fork()) _exit(0);*//* should we detach? */ 845 /*(grandchild is now backgrounded and detached from original process)*/ 846 847 /* XXX: might extend code to have new server.feature-flags param specify 848 * max lifetime before aborting remaining connections */ 849 850 /* (reached if lighttpd workers or if sole process w/o workers) 851 * use same code as comment elsewhere in server.c: 852 * make sure workers do not muck with pid-file */ 853 if (0 <= pid_fd) { 854 close(pid_fd); 855 pid_fd = -1; 856 } 857 if (srv->srvconf.pid_file) buffer_clear(srv->srvconf.pid_file); 858 859 /* (original process is backgrounded -- even if no active connections -- 860 * to allow graceful shutdown tasks to be run by server and by modules) */ 861 log_error(srv->errh, __FILE__, __LINE__, 862 "[note] pid %lld continuing to handle %u connection(s) in progress", 863 (long long)getpid(), srv->conns.used); 864 865 graceful_restart = 0; 866 return 1; 867 } 868 869 __attribute_cold__ 870 static void server_graceful_state (server *srv) { 871 872 if (!srv_shutdown) { 873 if (0 == srv->graceful_expire_ts && srv->srvconf.feature_flags) { 874 data_unset * const du = 875 array_get_element_klen(srv->srvconf.feature_flags, 876 CONST_STR_LEN("server.graceful-shutdown-timeout")); 877 if (NULL != du) { 878 if (du->type == TYPE_STRING) 879 srv->graceful_expire_ts = 880 strtol(((data_string *)du)->value.ptr, NULL, 10); 881 else if (du->type == TYPE_INTEGER) 882 srv->graceful_expire_ts = ((data_integer *)du)->value; 883 if (srv->graceful_expire_ts) 884 srv->graceful_expire_ts += log_epoch_secs; 885 } 886 } 887 connection_graceful_shutdown_maint(srv); 888 } 889 890 if (!oneshot_fd 891 && (2 == srv->sockets_disabled || 3 == srv->sockets_disabled)) return; 892 893 log_error(srv->errh,__FILE__,__LINE__,"[note] graceful shutdown started"); 894 895 /* no graceful restart if chroot()ed, if oneshot mode, or if idle timeout */ 896 if (!buffer_string_is_empty(srv->srvconf.changeroot) 897 || oneshot_fd || 2 == graceful_shutdown) 898 graceful_restart = 0; 899 900 if (graceful_restart) { 901 if (!server_graceful_state_bg(srv)) 902 server_sockets_unregister(srv); 903 if (pid_fd > 0) pid_fd = -pid_fd; /*(flag to skip removing pid file)*/ 904 } 905 else { 906 server_sockets_close(srv); 907 remove_pid_file(srv); 908 /*(prevent more removal attempts)*/ 909 if (srv->srvconf.pid_file) buffer_clear(srv->srvconf.pid_file); 910 } 911 } 912 913 __attribute_cold__ 914 static void server_sockets_enable (server *srv) { 915 server_sockets_set_event(srv, FDEVENT_IN); 916 srv->sockets_disabled = 0; 917 log_error(srv->errh, __FILE__, __LINE__, "[note] sockets enabled again"); 918 } 919 920 __attribute_cold__ 921 static void server_sockets_disable (server *srv) { 922 server_sockets_set_event(srv, 0); 923 srv->sockets_disabled = 1; 924 log_error(srv->errh, __FILE__, __LINE__, 925 (srv->conns.used >= srv->max_conns) 926 ? "[note] sockets disabled, connection limit reached" 927 : "[note] sockets disabled, out-of-fds"); 928 } 929 930 __attribute_cold__ 931 static void server_overload_check (server *srv) { 932 if (srv->cur_fds + (int)srv->fdwaitqueue.used < srv->max_fds_lowat 933 && srv->conns.used < srv->max_conns) { 934 935 server_sockets_enable(srv); 936 } 937 } 938 939 static void server_load_check (server *srv) { 940 /* check if hit limits for num fds used or num connections */ 941 if (srv->cur_fds > srv->max_fds_hiwat || srv->conns.used >= srv->max_conns) 942 server_sockets_disable(srv); 943 } 944 945 __attribute_cold__ 946 __attribute_noinline__ 947 static void server_process_fdwaitqueue (server *srv) { 948 connections * const fdwaitqueue = &srv->fdwaitqueue; 949 uint32_t i = 0; 950 for (int n = srv->max_fds - srv->cur_fds - 16; n > 0; --n) { 951 if (i == fdwaitqueue->used) break; 952 connection_state_machine(fdwaitqueue->ptr[i++]); 953 } 954 if (i > 0 && 0 != (fdwaitqueue->used -= i)) { 955 memmove(fdwaitqueue->ptr, fdwaitqueue->ptr+i, fdwaitqueue->used * sizeof(*(fdwaitqueue->ptr))); 956 } 957 } 958 959 __attribute_cold__ 960 static int server_main_setup (server * const srv, int argc, char **argv) { 961 int print_config = 0; 962 int test_config = 0; 963 int i_am_root = 0; 964 int o; 965 #ifdef HAVE_FORK 966 int num_childs = 0; 967 #endif 968 uint32_t i; 969 #ifdef HAVE_SIGACTION 970 struct sigaction act; 971 #endif 972 973 #ifdef HAVE_FORK 974 int parent_pipe_fd = -1; 975 #endif 976 977 #ifdef HAVE_GETUID 978 i_am_root = (0 == getuid()); 979 #endif 980 981 /* initialize globals (including file-scoped static globals) */ 982 oneshot_fd = 0; 983 oneshot_fdout = -1; 984 srv_shutdown = 0; 985 graceful_shutdown = 0; 986 handle_sig_alarm = 1; 987 handle_sig_hup = 0; 988 idle_limit = 0; 989 chunkqueue_set_tempdirs_default_reset(); 990 http_auth_dumbdata_reset(); 991 http_vhostdb_dumbdata_reset(); 992 /*graceful_restart = 0;*//*(reset below to avoid further daemonizing)*/ 993 /*(intentionally preserved)*/ 994 /*memset(graceful_sockets, 0, sizeof(graceful_sockets));*/ 995 /*memset(inherited_sockets, 0, sizeof(inherited_sockets));*/ 996 /*pid_fd = -1;*/ 997 srv->argv = argv; 998 999 while(-1 != (o = getopt(argc, argv, "f:m:i:hvVD1pt"))) { 1000 switch(o) { 1001 case 'f': 1002 if (srv->config_data_base) { 1003 log_error(srv->errh, __FILE__, __LINE__, 1004 "Can only read one config file. Use the include command to use multiple config files."); 1005 return -1; 1006 } 1007 if (config_read(srv, optarg)) { 1008 return -1; 1009 } 1010 break; 1011 case 'm': 1012 buffer_copy_string(srv->srvconf.modules_dir, optarg); 1013 break; 1014 case 'i': { 1015 char *endptr; 1016 long timeout = strtol(optarg, &endptr, 0); 1017 if (!*optarg || *endptr || timeout < 0) { 1018 log_error(srv->errh, __FILE__, __LINE__, 1019 "Invalid idle timeout value: %s", optarg); 1020 return -1; 1021 } 1022 idle_limit = (time_t)timeout; 1023 break; 1024 } 1025 case 'p': print_config = 1; break; 1026 case 't': ++test_config; break; 1027 case '1': if (0 == oneshot_fd) oneshot_fd = dup(STDIN_FILENO); 1028 break; 1029 case 'D': srv->srvconf.dont_daemonize = 1; break; 1030 case 'v': show_version(); return 0; 1031 case 'V': show_features(); return 0; 1032 case 'h': show_help(); return 0; 1033 default: 1034 show_help(); 1035 return -1; 1036 } 1037 } 1038 1039 #ifdef __CYGWIN__ 1040 if (!srv->config_data_base && NULL != getenv("NSSM_SERVICE_NAME")) { 1041 char *dir = getenv("NSSM_SERVICE_DIR"); 1042 if (NULL != dir && 0 != chdir(dir)) { 1043 log_perror(srv->errh, __FILE__, __LINE__, "chdir %s failed", dir); 1044 return -1; 1045 } 1046 srv->srvconf.dont_daemonize = 1; 1047 buffer_copy_string_len(srv->srvconf.modules_dir, CONST_STR_LEN("modules")); 1048 if (config_read(srv, "conf/lighttpd.conf")) return -1; 1049 } 1050 #endif 1051 1052 if (!srv->config_data_base) { 1053 log_error(srv->errh, __FILE__, __LINE__, 1054 "No configuration available. Try using -f option."); 1055 return -1; 1056 } 1057 1058 if (print_config) { 1059 config_print(srv); 1060 fprintf(stdout, "\n"); 1061 } 1062 1063 if (test_config) { 1064 if (srv->srvconf.pid_file) buffer_clear(srv->srvconf.pid_file); 1065 if (1 == test_config) { 1066 printf("Syntax OK\n"); 1067 } else { /*(test_config > 1)*/ 1068 test_config = 0; 1069 srv->srvconf.preflight_check = 1; 1070 srv->srvconf.dont_daemonize = 1; 1071 } 1072 } 1073 1074 if (test_config || print_config) { 1075 return 0; 1076 } 1077 1078 if (oneshot_fd) { 1079 if (oneshot_fd <= STDERR_FILENO) { 1080 log_error(srv->errh, __FILE__, __LINE__, 1081 "Invalid fds at startup with lighttpd -1"); 1082 return -1; 1083 } 1084 graceful_shutdown = 1; 1085 srv->sockets_disabled = 1; 1086 srv->srvconf.dont_daemonize = 1; 1087 if (srv->srvconf.pid_file) buffer_clear(srv->srvconf.pid_file); 1088 if (srv->srvconf.max_worker) { 1089 srv->srvconf.max_worker = 0; 1090 log_error(srv->errh, __FILE__, __LINE__, 1091 "server one-shot command line option disables server.max-worker config file option."); 1092 } 1093 1094 struct stat st; 1095 if (0 != fstat(oneshot_fd, &st)) { 1096 log_perror(srv->errh, __FILE__, __LINE__, "fstat()"); 1097 return -1; 1098 } 1099 1100 if (S_ISFIFO(st.st_mode)) { 1101 oneshot_fdout = dup(STDOUT_FILENO); 1102 if (oneshot_fdout <= STDERR_FILENO) { 1103 log_perror(srv->errh, __FILE__, __LINE__, "dup()"); 1104 return -1; 1105 } 1106 } 1107 else if (!S_ISSOCK(st.st_mode)) { 1108 /* require that fd is a socket 1109 * (modules might expect STDIN_FILENO and STDOUT_FILENO opened to /dev/null) */ 1110 log_error(srv->errh, __FILE__, __LINE__, 1111 "lighttpd -1 stdin is not a socket"); 1112 return -1; 1113 } 1114 } 1115 1116 if (srv->srvconf.bindhost && buffer_is_equal_string(srv->srvconf.bindhost, CONST_STR_LEN("/dev/stdin"))) { 1117 if (-1 == srv->stdin_fd) 1118 srv->stdin_fd = dup(STDIN_FILENO); 1119 if (srv->stdin_fd <= STDERR_FILENO) { 1120 log_error(srv->errh, __FILE__, __LINE__, 1121 "Invalid fds at startup"); 1122 return -1; 1123 } 1124 } 1125 1126 /* close stdin and stdout, as they are not needed */ 1127 { 1128 struct stat st; 1129 int devnull; 1130 int errfd; 1131 do { 1132 /* coverity[overwrite_var : FALSE] */ 1133 devnull = fdevent_open_devnull(); 1134 #ifdef __COVERITY__ 1135 __coverity_escape__(devnull); 1136 #endif 1137 } while (-1 != devnull && devnull <= STDERR_FILENO); 1138 if (-1 == devnull) { 1139 log_perror(srv->errh, __FILE__, __LINE__, 1140 "opening /dev/null failed"); 1141 return -1; 1142 } 1143 errfd = (0 == fstat(STDERR_FILENO, &st)) ? -1 : devnull; 1144 if (0 != fdevent_set_stdin_stdout_stderr(devnull, devnull, errfd)) { 1145 log_perror(srv->errh, __FILE__, __LINE__, 1146 "setting default fds failed"); 1147 #ifdef FD_CLOEXEC 1148 if (-1 != errfd) close(errfd); 1149 if (devnull != errfd) close(devnull); 1150 #endif 1151 return -1; 1152 } 1153 #ifdef FD_CLOEXEC 1154 if (-1 != errfd) close(errfd); 1155 if (devnull != errfd) close(devnull); 1156 #endif 1157 } 1158 1159 http_response_send_1xx_cb_set(NULL, HTTP_VERSION_2); 1160 if (srv->srvconf.feature_flags 1161 && !config_plugin_value_tobool( 1162 array_get_element_klen(srv->srvconf.feature_flags, 1163 CONST_STR_LEN("server.h2-discard-backend-1xx")), 0)) 1164 http_response_send_1xx_cb_set(h2_send_1xx, 1165 HTTP_VERSION_2); 1166 1167 http_response_send_1xx_cb_set(NULL, HTTP_VERSION_1_1); 1168 if (srv->srvconf.feature_flags 1169 && !config_plugin_value_tobool( 1170 array_get_element_klen(srv->srvconf.feature_flags, 1171 CONST_STR_LEN("server.h1-discard-backend-1xx")), 0)) 1172 http_response_send_1xx_cb_set(connection_send_1xx, 1173 HTTP_VERSION_1_1); 1174 1175 if (0 != config_set_defaults(srv)) { 1176 log_error(srv->errh, __FILE__, __LINE__, 1177 "setting default values failed"); 1178 return -1; 1179 } 1180 1181 if (plugins_load(srv)) { 1182 log_error(srv->errh, __FILE__, __LINE__, 1183 "loading plugins finally failed"); 1184 return -1; 1185 } 1186 1187 if (HANDLER_GO_ON != plugins_call_init(srv)) { 1188 log_error(srv->errh, __FILE__, __LINE__, 1189 "Initialization of plugins failed. Going down."); 1190 return -1; 1191 } 1192 1193 /* mod_indexfile should be listed in server.modules prior to dynamic handlers */ 1194 i = 0; 1195 for (const char *pname = NULL; i < srv->plugins.used; ++i) { 1196 plugin *p = ((plugin **)srv->plugins.ptr)[i]; 1197 if (NULL != pname && 0 == strcmp(p->name, "indexfile")) { 1198 log_error(srv->errh, __FILE__, __LINE__, 1199 "Warning: mod_indexfile should be listed in server.modules prior to mod_%s", pname); 1200 break; 1201 } 1202 if (p->handle_subrequest_start && p->handle_subrequest) { 1203 if (!pname) pname = p->name; 1204 } 1205 } 1206 1207 /* open pid file BEFORE chroot */ 1208 if (-2 == pid_fd) pid_fd = -1; /*(initial startup state)*/ 1209 if (-1 == pid_fd && !buffer_string_is_empty(srv->srvconf.pid_file)) { 1210 const char *pidfile = srv->srvconf.pid_file->ptr; 1211 if (-1 == (pid_fd = fdevent_open_cloexec(pidfile, 0, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { 1212 struct stat st; 1213 if (errno != EEXIST) { 1214 log_perror(srv->errh, __FILE__, __LINE__, 1215 "opening pid-file failed: %s", pidfile); 1216 return -1; 1217 } 1218 1219 if (0 != stat(pidfile, &st)) { 1220 log_perror(srv->errh, __FILE__, __LINE__, 1221 "stating existing pid-file failed: %s", pidfile); 1222 } 1223 1224 if (!S_ISREG(st.st_mode)) { 1225 log_error(srv->errh, __FILE__, __LINE__, 1226 "pid-file exists and isn't regular file: %s", pidfile); 1227 return -1; 1228 } 1229 1230 if (-1 == (pid_fd = fdevent_open_cloexec(pidfile, 0, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { 1231 log_perror(srv->errh, __FILE__, __LINE__, 1232 "opening pid-file failed: %s", pidfile); 1233 return -1; 1234 } 1235 } 1236 } 1237 1238 { 1239 #ifdef HAVE_GETRLIMIT 1240 struct rlimit rlim; 1241 int use_rlimit = 1; 1242 #ifdef HAVE_VALGRIND_VALGRIND_H 1243 if (RUNNING_ON_VALGRIND) use_rlimit = 0; 1244 #endif 1245 1246 if (0 != getrlimit(RLIMIT_NOFILE, &rlim)) { 1247 log_perror(srv->errh, __FILE__, __LINE__, "getrlimit()"); 1248 return -1; 1249 } 1250 1251 /** 1252 * if we are not root can can't increase the fd-limit above rlim_max, but we can reduce it 1253 */ 1254 if (use_rlimit && srv->srvconf.max_fds 1255 && (i_am_root || srv->srvconf.max_fds <= rlim.rlim_max)) { 1256 /* set rlimits */ 1257 1258 rlim.rlim_cur = srv->srvconf.max_fds; 1259 if (i_am_root) rlim.rlim_max = srv->srvconf.max_fds; 1260 1261 if (0 != setrlimit(RLIMIT_NOFILE, &rlim)) { 1262 log_perror(srv->errh, __FILE__, __LINE__, "setrlimit()"); 1263 return -1; 1264 } 1265 } 1266 1267 /*(default upper limit of 4k if server.max-fds not specified)*/ 1268 if (0 == srv->srvconf.max_fds) 1269 srv->srvconf.max_fds = (rlim.rlim_cur <= 4096) 1270 ? (unsigned short)rlim.rlim_cur 1271 : 4096; 1272 1273 /* set core file rlimit, if enable_cores is set */ 1274 if (use_rlimit && srv->srvconf.enable_cores && getrlimit(RLIMIT_CORE, &rlim) == 0) { 1275 rlim.rlim_cur = rlim.rlim_max; 1276 setrlimit(RLIMIT_CORE, &rlim); 1277 } 1278 #endif 1279 } 1280 1281 /* we need root-perms for port < 1024 */ 1282 if (0 != network_init(srv, srv->stdin_fd)) { 1283 return -1; 1284 } 1285 srv->stdin_fd = -1; 1286 1287 if (i_am_root) { 1288 #ifdef HAVE_PWD_H 1289 /* set user and group */ 1290 struct group *grp = NULL; 1291 struct passwd *pwd = NULL; 1292 1293 if (!buffer_string_is_empty(srv->srvconf.groupname)) { 1294 if (NULL == (grp = getgrnam(srv->srvconf.groupname->ptr))) { 1295 log_error(srv->errh, __FILE__, __LINE__, 1296 "can't find groupname %s", srv->srvconf.groupname->ptr); 1297 return -1; 1298 } 1299 } 1300 1301 if (!buffer_string_is_empty(srv->srvconf.username)) { 1302 if (NULL == (pwd = getpwnam(srv->srvconf.username->ptr))) { 1303 log_error(srv->errh, __FILE__, __LINE__, 1304 "can't find username %s", srv->srvconf.username->ptr); 1305 return -1; 1306 } 1307 1308 if (pwd->pw_uid == 0) { 1309 log_error(srv->errh, __FILE__, __LINE__, 1310 "I will not set uid to 0\n"); 1311 return -1; 1312 } 1313 1314 if (NULL == grp && NULL == (grp = getgrgid(pwd->pw_gid))) { 1315 log_error(srv->errh, __FILE__, __LINE__, 1316 "can't find group id %d", (int)pwd->pw_gid); 1317 return -1; 1318 } 1319 } 1320 1321 if (NULL != grp) { 1322 if (grp->gr_gid == 0) { 1323 log_error(srv->errh, __FILE__, __LINE__, 1324 "I will not set gid to 0\n"); 1325 return -1; 1326 } 1327 } 1328 1329 /* 1330 * Change group before chroot, when we have access 1331 * to /etc/group 1332 * */ 1333 if (NULL != grp) { 1334 if (-1 == setgid(grp->gr_gid)) { 1335 log_perror(srv->errh, __FILE__, __LINE__, "setgid()"); 1336 return -1; 1337 } 1338 if (-1 == setgroups(0, NULL)) { 1339 log_perror(srv->errh, __FILE__, __LINE__, "setgroups()"); 1340 return -1; 1341 } 1342 if (!buffer_string_is_empty(srv->srvconf.username)) { 1343 initgroups(srv->srvconf.username->ptr, grp->gr_gid); 1344 } 1345 } 1346 #endif 1347 #ifdef HAVE_CHROOT 1348 if (!buffer_string_is_empty(srv->srvconf.changeroot)) { 1349 tzset(); 1350 1351 if (-1 == chroot(srv->srvconf.changeroot->ptr)) { 1352 log_perror(srv->errh, __FILE__, __LINE__, "chroot()"); 1353 return -1; 1354 } 1355 if (-1 == chdir("/")) { 1356 log_perror(srv->errh, __FILE__, __LINE__, "chdir()"); 1357 return -1; 1358 } 1359 } 1360 #endif 1361 #ifdef HAVE_PWD_H 1362 /* drop root privs */ 1363 if (NULL != pwd) { 1364 if (-1 == setuid(pwd->pw_uid)) { 1365 log_perror(srv->errh, __FILE__, __LINE__, "setuid()"); 1366 return -1; 1367 } 1368 } 1369 #endif 1370 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE) 1371 /** 1372 * on IRIX 6.5.30 they have prctl() but no DUMPABLE 1373 */ 1374 if (srv->srvconf.enable_cores) { 1375 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); 1376 } 1377 #endif 1378 } 1379 1380 #ifdef HAVE_FORK 1381 /* network is up, let's daemonize ourself */ 1382 if (0 == srv->srvconf.dont_daemonize && 0 == graceful_restart) { 1383 parent_pipe_fd = daemonize(); 1384 } 1385 #endif 1386 graceful_restart = 0;/*(reset here after avoiding further daemonizing)*/ 1387 if (0 == oneshot_fd) graceful_shutdown = 0; 1388 1389 1390 #ifdef HAVE_SIGACTION 1391 memset(&act, 0, sizeof(act)); 1392 act.sa_handler = SIG_IGN; 1393 sigaction(SIGPIPE, &act, NULL); 1394 # if defined(SA_SIGINFO) 1395 last_sighup_info.si_uid = 0, 1396 last_sighup_info.si_pid = 0; 1397 last_sigterm_info.si_uid = 0, 1398 last_sigterm_info.si_pid = 0; 1399 act.sa_sigaction = sigaction_handler; 1400 sigemptyset(&act.sa_mask); 1401 act.sa_flags = SA_SIGINFO; 1402 # else 1403 act.sa_handler = signal_handler; 1404 sigemptyset(&act.sa_mask); 1405 act.sa_flags = 0; 1406 # endif 1407 sigaction(SIGINT, &act, NULL); 1408 sigaction(SIGTERM, &act, NULL); 1409 sigaction(SIGHUP, &act, NULL); 1410 sigaction(SIGALRM, &act, NULL); 1411 sigaction(SIGUSR1, &act, NULL); 1412 1413 /* it should be safe to restart syscalls after SIGCHLD */ 1414 act.sa_flags |= SA_RESTART | SA_NOCLDSTOP; 1415 sigaction(SIGCHLD, &act, NULL); 1416 1417 #elif defined(HAVE_SIGNAL) 1418 /* ignore the SIGPIPE from sendfile() */ 1419 signal(SIGPIPE, SIG_IGN); 1420 signal(SIGALRM, signal_handler); 1421 signal(SIGTERM, signal_handler); 1422 signal(SIGHUP, signal_handler); 1423 signal(SIGCHLD, signal_handler); 1424 signal(SIGINT, signal_handler); 1425 signal(SIGUSR1, signal_handler); 1426 #endif 1427 1428 1429 srv->gid = getgid(); 1430 srv->uid = getuid(); 1431 srv->pid = getpid(); 1432 1433 /* write pid file */ 1434 if (pid_fd > 2) { 1435 buffer * const tb = srv->tmp_buf; 1436 buffer_clear(tb); 1437 buffer_append_int(tb, srv->pid); 1438 buffer_append_string_len(tb, CONST_STR_LEN("\n")); 1439 if (-1 == write_all(pid_fd, CONST_BUF_LEN(tb))) { 1440 log_perror(srv->errh, __FILE__, __LINE__, "Couldn't write pid file"); 1441 close(pid_fd); 1442 pid_fd = -1; 1443 return -1; 1444 } 1445 } else if (pid_fd < -2) { 1446 pid_fd = -pid_fd; 1447 } 1448 1449 /* Close stderr ASAP in the child process to make sure that nothing 1450 * is being written to that fd which may not be valid anymore. */ 1451 if (!srv->srvconf.preflight_check) { 1452 if (-1 == config_log_error_open(srv)) { 1453 log_error(srv->errh, __FILE__, __LINE__, "Opening errorlog failed. Going down."); 1454 return -1; 1455 } 1456 log_error(srv->errh, __FILE__, __LINE__, "server started (" PACKAGE_DESC ")"); 1457 } 1458 1459 if (HANDLER_GO_ON != plugins_call_set_defaults(srv)) { 1460 log_error(srv->errh, __FILE__, __LINE__, "Configuration of plugins failed. Going down."); 1461 return -1; 1462 } 1463 1464 if (!config_finalize(srv, &default_server_tag)) { 1465 return -1; 1466 } 1467 1468 if (srv->srvconf.preflight_check) { 1469 /*printf("Preflight OK");*//*(stdout reopened to /dev/null)*/ 1470 return 0; 1471 } 1472 1473 1474 #ifdef HAVE_FORK 1475 /** 1476 * notify daemonize-grandparent of successful startup 1477 * do this before any further forking is done (workers) 1478 */ 1479 if (0 == srv->srvconf.dont_daemonize && -1 != parent_pipe_fd) { 1480 if (0 > write(parent_pipe_fd, "", 1)) return -1; 1481 close(parent_pipe_fd); 1482 } 1483 1484 if (idle_limit && srv->srvconf.max_worker) { 1485 srv->srvconf.max_worker = 0; 1486 log_error(srv->errh, __FILE__, __LINE__, 1487 "server idle time limit command line option disables server.max-worker config file option."); 1488 } 1489 1490 /* start watcher and workers */ 1491 num_childs = srv->srvconf.max_worker; 1492 if (num_childs > 0) { 1493 pid_t pids[num_childs]; 1494 pid_t pid; 1495 const int npids = num_childs; 1496 int child = 0; 1497 unsigned int timer = 0; 1498 for (int n = 0; n < npids; ++n) pids[n] = -1; 1499 while (!child && !srv_shutdown && !graceful_shutdown) { 1500 if (num_childs > 0) { 1501 switch ((pid = fork())) { 1502 case -1: 1503 return -1; 1504 case 0: 1505 child = 1; 1506 alarm(0); 1507 break; 1508 default: 1509 num_childs--; 1510 for (int n = 0; n < npids; ++n) { 1511 if (-1 == pids[n]) { 1512 pids[n] = pid; 1513 break; 1514 } 1515 } 1516 break; 1517 } 1518 } else { 1519 int status; 1520 1521 if (-1 != (pid = wait(&status))) { 1522 log_epoch_secs = time(NULL); 1523 if (plugins_call_handle_waitpid(srv, pid, status) != HANDLER_GO_ON) { 1524 if (!timer) alarm((timer = 5)); 1525 continue; 1526 } 1527 switch (fdevent_reaped_logger_pipe(pid)) { 1528 default: break; 1529 case -1: if (!timer) alarm((timer = 5)); 1530 /* fall through */ 1531 case 1: continue; 1532 } 1533 /** 1534 * check if one of our workers went away 1535 */ 1536 for (int n = 0; n < npids; ++n) { 1537 if (pid == pids[n]) { 1538 pids[n] = -1; 1539 num_childs++; 1540 break; 1541 } 1542 } 1543 } else { 1544 switch (errno) { 1545 case EINTR: 1546 log_epoch_secs = time(NULL); 1547 /** 1548 * if we receive a SIGHUP we have to close our logs ourself as we don't 1549 * have the mainloop who can help us here 1550 */ 1551 if (handle_sig_hup) { 1552 handle_sig_hup = 0; 1553 1554 config_log_error_cycle(srv); 1555 1556 /* forward SIGHUP to workers */ 1557 for (int n = 0; n < npids; ++n) { 1558 if (pids[n] > 0) kill(pids[n], SIGHUP); 1559 } 1560 } 1561 if (handle_sig_alarm) { 1562 handle_sig_alarm = 0; 1563 timer = 0; 1564 plugins_call_handle_trigger(srv); 1565 fdevent_restart_logger_pipes(log_epoch_secs); 1566 } 1567 break; 1568 default: 1569 break; 1570 } 1571 } 1572 } 1573 } 1574 1575 /** 1576 * for the parent this is the exit-point 1577 */ 1578 if (!child) { 1579 /** 1580 * kill all children too 1581 */ 1582 if (graceful_shutdown || graceful_restart) { 1583 /* flag to ignore one SIGINT if graceful_restart */ 1584 if (graceful_restart) graceful_restart = 2; 1585 kill(0, SIGINT); 1586 server_graceful_state(srv); 1587 } else if (srv_shutdown) { 1588 kill(0, SIGTERM); 1589 } 1590 1591 return 0; 1592 } 1593 1594 /* ignore SIGUSR1 in workers; only parent directs graceful restart */ 1595 #ifdef HAVE_SIGACTION 1596 { 1597 struct sigaction actignore; 1598 memset(&actignore, 0, sizeof(actignore)); 1599 actignore.sa_handler = SIG_IGN; 1600 sigaction(SIGUSR1, &actignore, NULL); 1601 } 1602 #elif defined(HAVE_SIGNAL) 1603 signal(SIGUSR1, SIG_IGN); 1604 #endif 1605 1606 /** 1607 * make sure workers do not muck with pid-file 1608 */ 1609 if (0 <= pid_fd) { 1610 close(pid_fd); 1611 pid_fd = -1; 1612 } 1613 if (srv->srvconf.pid_file) buffer_clear(srv->srvconf.pid_file); 1614 1615 fdevent_clr_logger_pipe_pids(); 1616 srv->pid = getpid(); 1617 li_rand_reseed(); 1618 } 1619 #endif 1620 1621 srv->max_fds = (int)srv->srvconf.max_fds; 1622 srv->ev = fdevent_init(srv->srvconf.event_handler, &srv->max_fds, &srv->cur_fds, srv->errh); 1623 if (NULL == srv->ev) { 1624 log_error(srv->errh, __FILE__, __LINE__, "fdevent_init failed"); 1625 return -1; 1626 } 1627 1628 srv->max_fds_lowat = srv->max_fds * 8 / 10; 1629 srv->max_fds_hiwat = srv->max_fds * 9 / 10; 1630 1631 /* set max-conns */ 1632 if (srv->srvconf.max_conns > srv->max_fds/2) { 1633 /* we can't have more connections than max-fds/2 */ 1634 log_error(srv->errh, __FILE__, __LINE__, 1635 "can't have more connections than fds/2: %hu %d", 1636 srv->srvconf.max_conns, srv->max_fds); 1637 srv->max_conns = srv->max_fds/2; 1638 } else if (srv->srvconf.max_conns) { 1639 /* otherwise respect the wishes of the user */ 1640 srv->max_conns = srv->srvconf.max_conns; 1641 } else { 1642 /* or use the default: we really don't want to hit max-fds */ 1643 srv->max_conns = srv->max_fds/3; 1644 } 1645 1646 request_pool_init(srv->max_conns); 1647 1648 /* libev backend overwrites our SIGCHLD handler and calls waitpid on SIGCHLD; we want our own SIGCHLD handling. */ 1649 #ifdef HAVE_SIGACTION 1650 sigaction(SIGCHLD, &act, NULL); 1651 #elif defined(HAVE_SIGNAL) 1652 signal(SIGCHLD, signal_handler); 1653 #endif 1654 1655 /* 1656 * kqueue() is called here, select resets its internals, 1657 * all server sockets get their handlers 1658 * 1659 * */ 1660 if (0 != network_register_fdevents(srv)) { 1661 return -1; 1662 } 1663 1664 /* might fail if user is using fam (not gamin) and famd isn't running */ 1665 if (!stat_cache_init(srv->ev, srv->errh)) { 1666 log_error(srv->errh, __FILE__, __LINE__, 1667 "stat-cache could not be setup, dying."); 1668 return -1; 1669 } 1670 1671 #ifdef USE_ALARM 1672 { 1673 /* setup periodic timer (1 second) */ 1674 struct itimerval interval; 1675 interval.it_interval.tv_sec = 1; 1676 interval.it_interval.tv_usec = 0; 1677 interval.it_value.tv_sec = 1; 1678 interval.it_value.tv_usec = 0; 1679 if (setitimer(ITIMER_REAL, &interval, NULL)) { 1680 log_perror(srv->errh, __FILE__, __LINE__, "setitimer()"); 1681 return -1; 1682 } 1683 } 1684 #endif 1685 1686 /* get the current number of FDs */ 1687 { 1688 int fd = fdevent_open_devnull(); 1689 if (fd >= 0) { 1690 srv->cur_fds = fd; 1691 close(fd); 1692 } 1693 } 1694 1695 if (0 != server_sockets_set_nb_cloexec(srv)) { 1696 return -1; 1697 } 1698 1699 /* plugin hook for worker_init */ 1700 if (HANDLER_GO_ON != plugins_call_worker_init(srv)) 1701 return -1; 1702 1703 if (oneshot_fdout > 0) { 1704 if (server_oneshot_init_pipe(srv, oneshot_fd, oneshot_fdout)) { 1705 oneshot_fd = -1; 1706 oneshot_fdout = -1; 1707 } 1708 } 1709 else if (oneshot_fd && server_oneshot_init(srv, oneshot_fd)) { 1710 oneshot_fd = -1; 1711 } 1712 1713 return 1; 1714 } 1715 1716 __attribute_cold__ 1717 __attribute_noinline__ 1718 static void server_handle_sighup (server * const srv) { 1719 1720 /* cycle logfiles */ 1721 1722 plugins_call_handle_sighup(srv); 1723 1724 config_log_error_cycle(srv); 1725 #ifdef HAVE_SIGACTION 1726 log_error(srv->errh, __FILE__, __LINE__, 1727 "logfiles cycled UID = %d PID = %d", 1728 (int)last_sighup_info.si_uid, 1729 (int)last_sighup_info.si_pid); 1730 #else 1731 log_error(srv->errh, __FILE__, __LINE__, 1732 "logfiles cycled"); 1733 #endif 1734 } 1735 1736 __attribute_noinline__ 1737 static void server_handle_sigalrm (server * const srv, time_t min_ts, time_t last_active_ts) { 1738 1739 plugins_call_handle_trigger(srv); 1740 1741 log_epoch_secs = min_ts; 1742 1743 /* check idle time limit, if enabled */ 1744 if (idle_limit && idle_limit < min_ts - last_active_ts && !graceful_shutdown) { 1745 log_error(srv->errh, __FILE__, __LINE__, 1746 "[note] idle timeout %ds exceeded, " 1747 "initiating graceful shutdown", (int)idle_limit); 1748 graceful_shutdown = 2; /* value 2 indicates idle timeout */ 1749 if (graceful_restart) { 1750 graceful_restart = 0; 1751 if (pid_fd < -2) pid_fd = -pid_fd; 1752 server_sockets_close(srv); 1753 } 1754 } 1755 1756 #ifdef HAVE_GETLOADAVG 1757 /* refresh loadavg data every 30 seconds */ 1758 if (srv->loadts + 30 < min_ts) { 1759 if (-1 != getloadavg(srv->loadavg, 3)) { 1760 srv->loadts = min_ts; 1761 } 1762 } 1763 #endif 1764 1765 if (0 == (min_ts & 0x3f)) { /*(once every 64 secs)*/ 1766 /* free excess chunkqueue buffers every 64 secs */ 1767 chunkqueue_chunk_pool_clear(); 1768 /* attempt to restart dead piped loggers every 64 secs */ 1769 if (0 == srv->srvconf.max_worker) 1770 fdevent_restart_logger_pipes(min_ts); 1771 } 1772 /* cleanup stat-cache */ 1773 stat_cache_trigger_cleanup(); 1774 /* reset global/aggregate rate limit counters */ 1775 config_reset_config_bytes_sec(srv->config_data_base); 1776 /* if graceful_shutdown, accelerate cleanup of recently completed request/responses */ 1777 if (graceful_shutdown && !srv_shutdown) connection_graceful_shutdown_maint(srv); 1778 connection_periodic_maint(srv, min_ts); 1779 } 1780 1781 __attribute_noinline__ 1782 static void server_handle_sigchld (server * const srv) { 1783 pid_t pid; 1784 do { 1785 int status; 1786 pid = waitpid(-1, &status, WNOHANG); 1787 if (pid > 0) { 1788 if (plugins_call_handle_waitpid(srv, pid, status) != HANDLER_GO_ON) { 1789 continue; 1790 } 1791 if (0 == srv->srvconf.max_worker) { 1792 /* check piped-loggers and restart, even if shutting down */ 1793 if (fdevent_waitpid_logger_pipe_pid(pid, log_epoch_secs)) { 1794 continue; 1795 } 1796 } 1797 } 1798 } while (pid > 0 || (-1 == pid && errno == EINTR)); 1799 } 1800 1801 __attribute_hot__ 1802 static void server_run_con_queue (connections * const restrict joblist) { 1803 connection * const * const restrict conlist = joblist->ptr; 1804 const uint32_t used = joblist->used; 1805 joblist->used = 0; 1806 for (uint32_t i = 0; i < used; ++i) { 1807 connection_state_machine(conlist[i]); 1808 } 1809 } 1810 1811 __attribute_hot__ 1812 __attribute_noinline__ 1813 static void server_main_loop (server * const srv) { 1814 time_t last_active_ts = time(NULL); 1815 1816 while (!srv_shutdown) { 1817 1818 if (handle_sig_hup) { 1819 handle_sig_hup = 0; 1820 server_handle_sighup(srv); 1821 } 1822 1823 /*(USE_ALARM not used; fdevent_poll() is effective periodic timer)*/ 1824 #ifdef USE_ALARM 1825 if (handle_sig_alarm) { 1826 handle_sig_alarm = 0; 1827 #endif 1828 time_t min_ts = time(NULL); 1829 if (min_ts != log_epoch_secs) { 1830 server_handle_sigalrm(srv, min_ts, last_active_ts); 1831 } 1832 #ifdef USE_ALARM 1833 } 1834 #endif 1835 1836 if (handle_sig_child) { 1837 handle_sig_child = 0; 1838 server_handle_sigchld(srv); 1839 } 1840 1841 if (graceful_shutdown) { 1842 server_graceful_state(srv); 1843 if (0 == srv->conns.used) { 1844 /* we are in graceful shutdown phase and all connections are closed 1845 * we are ready to terminate without harming anyone */ 1846 srv_shutdown = 1; 1847 break; 1848 } 1849 } else if (srv->sockets_disabled) { 1850 server_overload_check(srv); 1851 } else { 1852 server_load_check(srv); 1853 } 1854 1855 if (srv->fdwaitqueue.used) { 1856 server_process_fdwaitqueue(srv); 1857 } 1858 1859 connections * const joblist = connection_joblist; 1860 1861 if (fdevent_poll(srv->ev, joblist->used ? 0 : 1000) > 0) { 1862 last_active_ts = log_epoch_secs; 1863 } 1864 1865 connection_joblist = (joblist == &srv->joblist_A) 1866 ? &srv->joblist_B 1867 : &srv->joblist_A; 1868 1869 server_run_con_queue(joblist); 1870 } 1871 } 1872 1873 __attribute_cold__ 1874 int main (int argc, char **argv) { 1875 int rc; 1876 1877 #ifdef HAVE_GETUID 1878 #ifndef HAVE_ISSETUGID 1879 #define issetugid() (geteuid() != getuid() || getegid() != getgid()) 1880 #endif 1881 if (0 != getuid() && issetugid()) { /*check as early as possible in main()*/ 1882 fprintf(stderr, 1883 "Are you nuts ? Don't apply a SUID bit to this binary\n"); 1884 return -1; 1885 } 1886 #endif 1887 1888 /* for nice %b handling in strftime() */ 1889 setlocale(LC_TIME, "C"); 1890 1891 do { 1892 server * const srv = server_init(); 1893 1894 if (graceful_restart) { 1895 server_sockets_restore(srv); 1896 optind = 1; 1897 } 1898 1899 rc = server_main_setup(srv, argc, argv); 1900 if (rc > 0) { 1901 1902 server_main_loop(srv); 1903 1904 if (graceful_shutdown || graceful_restart) { 1905 server_graceful_state(srv); 1906 } 1907 1908 if (2 == graceful_shutdown) { /* value 2 indicates idle timeout */ 1909 log_error(srv->errh, __FILE__, __LINE__, 1910 "server stopped after idle timeout"); 1911 } else { 1912 #ifdef HAVE_SIGACTION 1913 log_error(srv->errh, __FILE__, __LINE__, 1914 "server stopped by UID = %d PID = %d", 1915 (int)last_sigterm_info.si_uid, 1916 (int)last_sigterm_info.si_pid); 1917 #else 1918 log_error(srv->errh, __FILE__, __LINE__, 1919 "server stopped"); 1920 #endif 1921 } 1922 } 1923 1924 /* clean-up */ 1925 remove_pid_file(srv); 1926 config_log_error_close(srv); 1927 if (graceful_restart) 1928 server_sockets_save(srv); 1929 else 1930 network_close(srv); 1931 request_pool_free(); 1932 connections_free(srv); 1933 plugins_free(srv); 1934 server_free(srv); 1935 1936 if (rc < 0 || !graceful_restart) break; 1937 1938 /* wait for all children to exit before graceful restart */ 1939 while (waitpid(-1, NULL, 0) > 0) ; 1940 } while (graceful_restart); 1941 1942 return rc; 1943 } 1944