comparison src/os/unix/ngx_daemon.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 8b84d60ef13d
children
comparison
equal deleted inserted replaced
9269:4eb02e5ddb48 9270:3d455e37abf8
7 7
8 #include <ngx_config.h> 8 #include <ngx_config.h>
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10 10
11 11
12 static ngx_fd_t ngx_daemon_fd = NGX_INVALID_FILE;
13
14
12 ngx_int_t 15 ngx_int_t
13 ngx_daemon(ngx_log_t *log) 16 ngx_daemon(ngx_log_t *log)
14 { 17 {
15 int fd; 18 u_char buf[1];
19 ssize_t n;
20 ngx_fd_t fd, pp[2];
21
22 if (pipe(pp) == -1) {
23 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed");
24 return NGX_ERROR;
25 }
16 26
17 switch (fork()) { 27 switch (fork()) {
18 case -1: 28 case -1:
19 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed"); 29 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
20 return NGX_ERROR; 30 return NGX_ERROR;
21 31
22 case 0: 32 case 0:
33 if (close(pp[0]) == -1) {
34 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
35 return NGX_ERROR;
36 }
37
38 ngx_daemon_fd = pp[1];
23 break; 39 break;
24 40
25 default: 41 default:
42 if (close(pp[1]) == -1) {
43 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
44 return NGX_ERROR;
45 }
46
47 n = read(pp[0], buf, 1);
48
49 if (n == 0) {
50 /* child exited */
51 return NGX_ERROR;
52 }
53
54 if (n != 1) {
55 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
56 "read() pipe failed");
57 return NGX_ERROR;
58 }
59
60 if (close(pp[0]) == -1) {
61 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
62 return NGX_ERROR;
63 }
64
26 exit(0); 65 exit(0);
27 } 66 }
28 67
29 ngx_parent = ngx_pid; 68 ngx_parent = ngx_pid;
30 ngx_pid = ngx_getpid(); 69 ngx_pid = ngx_getpid();
67 } 106 }
68 } 107 }
69 108
70 return NGX_OK; 109 return NGX_OK;
71 } 110 }
111
112
113 ngx_int_t
114 ngx_daemon_sync(ngx_log_t *log)
115 {
116 if (ngx_daemon_fd == NGX_INVALID_FILE) {
117 return NGX_OK;
118 }
119
120 if (write(ngx_daemon_fd, "", 1) != 1) {
121 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "write() pipe failed");
122 return NGX_ERROR;
123 }
124
125 if (close(ngx_daemon_fd) == -1) {
126 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
127 return NGX_ERROR;
128 }
129
130 ngx_daemon_fd = NGX_INVALID_FILE;
131
132 return NGX_OK;
133 }