Mercurial > hg > nginx
view src/os/unix/ngx_posix_init.c @ 9270:3d455e37abf8
Core: PID file writing synchronization.
Now, ngx_daemon() does not call exit() in the parent process immediately,
but instead waits for the child process to signal it actually started (and
wrote the PID file if configured to). This ensures that the PID file
already exists when the parent process exits.
To make sure that signal handlers won't cause unexpected logging in the
parent process if the child process dies (for example, due to errors when
writing the PID file), ngx_init_signals() is moved to the child process.
This resolves "PID file ... not readable (yet?) after start" and "Failed
to parse PID from file..." errors as observed with systemd.
Note that the errors observed are considered to be a bug in systemd, which
isn't able to work properly with traditional Unix daemons. Still, the
workaround is implemented to make sure there will be no OS vendor patches
trying to address this.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 13 May 2024 06:13:22 +0300 |
parents | 057adb2a9d23 |
children |
line wrap: on
line source
/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include <ngx_core.h> #include <nginx.h> ngx_int_t ngx_ncpu; ngx_int_t ngx_max_sockets; ngx_uint_t ngx_inherited_nonblocking; ngx_uint_t ngx_tcp_nodelay_and_tcp_nopush; struct rlimit rlmt; ngx_os_io_t ngx_os_io = { ngx_unix_recv, ngx_readv_chain, ngx_udp_unix_recv, ngx_unix_send, ngx_udp_unix_send, ngx_udp_unix_sendmsg_chain, ngx_writev_chain, 0 }; ngx_int_t ngx_os_init(ngx_log_t *log) { ngx_time_t *tp; ngx_uint_t n; #if (NGX_HAVE_LEVEL1_DCACHE_LINESIZE) long size; #endif #if (NGX_HAVE_OS_SPECIFIC_INIT) if (ngx_os_specific_init(log) != NGX_OK) { return NGX_ERROR; } #endif if (ngx_init_setproctitle(log) != NGX_OK) { return NGX_ERROR; } ngx_pagesize = getpagesize(); ngx_cacheline_size = NGX_CPU_CACHE_LINE; for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ } #if (NGX_HAVE_SC_NPROCESSORS_ONLN) if (ngx_ncpu == 0) { ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN); } #endif if (ngx_ncpu < 1) { ngx_ncpu = 1; } #if (NGX_HAVE_LEVEL1_DCACHE_LINESIZE) size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); if (size > 0) { ngx_cacheline_size = size; } #endif ngx_cpuinfo(); if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) { ngx_log_error(NGX_LOG_ALERT, log, errno, "getrlimit(RLIMIT_NOFILE) failed"); return NGX_ERROR; } ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur; #if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4) ngx_inherited_nonblocking = 1; #else ngx_inherited_nonblocking = 0; #endif tp = ngx_timeofday(); srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec); return NGX_OK; } void ngx_os_status(ngx_log_t *log) { ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER_BUILD); #ifdef NGX_COMPILER ngx_log_error(NGX_LOG_NOTICE, log, 0, "built by " NGX_COMPILER); #endif #if (NGX_HAVE_OS_SPECIFIC_INIT) ngx_os_specific_status(log); #endif ngx_log_error(NGX_LOG_NOTICE, log, 0, "getrlimit(RLIMIT_NOFILE): %r:%r", rlmt.rlim_cur, rlmt.rlim_max); } #if 0 ngx_int_t ngx_posix_post_conf_init(ngx_log_t *log) { ngx_fd_t pp[2]; if (pipe(pp) == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed"); return NGX_ERROR; } if (dup2(pp[1], STDERR_FILENO) == -1) { ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDERR) failed"); return NGX_ERROR; } if (pp[1] > STDERR_FILENO) { if (close(pp[1]) == -1) { ngx_log_error(NGX_LOG_EMERG, log, errno, "close() failed"); return NGX_ERROR; } } return NGX_OK; } #endif