<div dir="ltr"><div>Hi.</div><div><br></div><div>diff -r 4d874b4d82ed src/core/ngx_cycle.c</div><div>--- a/src/core/ngx_cycle.c<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Thu Mar 16 20:38:31 2017 +0300</div><div>+++ b/src/core/ngx_cycle.c<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Fri Mar 17 22:28:54 2017 -0400</div><div>@@ -979,39 +979,17 @@</div><div> }</div><div><br></div><div><br></div><div>-void</div><div>-ngx_delete_pidfile(ngx_cycle_t *cycle)</div><div>-{</div><div>- u_char *name;</div><div>- ngx_core_conf_t *ccf;</div><div>-</div><div>- ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);</div><div>-</div><div>- name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;</div><div>-</div><div>- if (ngx_delete_file(name) == NGX_FILE_ERROR) {</div><div>- ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,</div><div>- ngx_delete_file_n " \"%s\" failed", name);</div><div>- }</div><div>-}</div><div>-</div><div>-</div><div>-ngx_int_t</div><div>-ngx_signal_process(ngx_cycle_t *cycle, char *sig)</div><div>+static ngx_pid_t</div><div>+ngx_get_filepid(ngx_cycle_t *cycle, ngx_str_t *name)</div><div> {</div><div> ssize_t n;</div><div> ngx_pid_t pid;</div><div> ngx_file_t file;</div><div>- ngx_core_conf_t *ccf;</div><div> u_char buf[NGX_INT64_LEN + 2];</div><div><br></div><div>- ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");</div><div>-</div><div>- ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);</div><div>-</div><div> ngx_memzero(&file, sizeof(ngx_file_t));</div><div><br></div><div>- <a href="http://file.name">file.name</a> = ccf->pid;</div><div>+ <a href="http://file.name">file.name</a> = *name;</div><div> file.log = cycle->log;</div><div><br></div><div> file.fd = ngx_open_file(file.name.data, NGX_FILE_RDONLY,</div><div>@@ -1020,7 +998,7 @@</div><div> if (file.fd == NGX_INVALID_FILE) {</div><div> ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,</div><div> ngx_open_file_n " \"%s\" failed", file.name.data);</div><div>- return 1;</div><div>+ return -1;</div><div> }</div><div><br></div><div> n = ngx_read_file(&file, buf, NGX_INT64_LEN + 2, 0);</div><div>@@ -1031,7 +1009,7 @@</div><div> }</div><div><br></div><div> if (n == NGX_ERROR) {</div><div>- return 1;</div><div>+ return -1;</div><div> }</div><div><br></div><div> while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ }</div><div>@@ -1042,6 +1020,55 @@</div><div> ngx_log_error(NGX_LOG_ERR, cycle->log, 0,</div><div> "invalid PID number \"%*s\" in \"%s\"",</div><div> n, buf, file.name.data);</div><div>+ return -1;</div><div>+ }</div><div>+</div><div>+ return pid;</div><div>+}</div><div>+</div><div>+</div><div>+void</div><div>+ngx_delete_pidfile(ngx_cycle_t *cycle)</div><div>+{</div><div>+ ngx_pid_t pid;</div><div>+ ngx_str_t *name;</div><div>+ ngx_core_conf_t *ccf;</div><div>+</div><div>+ ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);</div><div>+</div><div>+ name = ngx_new_binary ? &ccf->oldpid : &ccf->pid;</div><div>+</div><div>+ pid = ngx_get_filepid(cycle, name);</div><div>+ if (pid == -1) {</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (pid != ngx_pid) {</div><div>+ ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "pidfile has been changed %P %P",</div><div>+ pid, ngx_pid);</div><div>+ return;</div><div>+ }</div><div>+</div><div>+ if (ngx_delete_file(name) == NGX_FILE_ERROR) {</div><div>+ ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,</div><div>+ ngx_delete_file_n " \"%s\" failed", name->data);</div><div>+ }</div><div>+}</div><div>+</div><div>+</div><div>+ngx_int_t</div><div>+ngx_signal_process(ngx_cycle_t *cycle, char *sig)</div><div>+{</div><div>+ ngx_pid_t pid;</div><div>+ ngx_core_conf_t *ccf;</div><div>+</div><div>+ ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");</div><div>+</div><div>+ ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);</div><div>+</div><div>+ pid = ngx_get_filepid(cycle, &ccf->pid);</div><div>+</div><div>+ if (pid == -1) {</div><div> return 1;</div><div> }</div><div><br></div><div>Thanks.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-03-18 15:43 GMT+08:00 洪志道 <span dir="ltr"><<a href="mailto:hongzhidao@gmail.com" target="_blank">hongzhidao@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi.<div><br></div><div>It's easy to reproduce such problem that</div><div>the master/worker process are running without the pid file.</div><div><br></div><div><span style="font-size:14px">Step by step:</span></div><div><span style="font-size:14px"><br></span><div>1. Start master process.</div><div> > ./objs/nginx </div><div> nginx.pid successfully create with content (23234)</div><div><br></div><div>2. Create an active connection, purpose for prevent previous master exit.</div><div> > telnet 127.1 80</div><div><br></div><div>3. Quit the previous master, and it still alive for a while.</div><div> > kill -QUIT 23234</div><div><br></div><div>4. Start new master process 23361, and the content of nginx.pid is changed to 23361</div><div> > ./objs/nginx</div><div> It successfully start because of the listening port is closed by the step 3.<br></div><div><br></div><div>5. Wait for a while, and the nginx.pid is deleted while the old master process (23234) completely quit.</div></div><div><br></div><div>Now the master process (23361) is running without nginx.pid.</div><div><br></div><div>So, there are two key points:</div><div>1. Master start, I think there is not problem.</div><div>2. Master Exit, is it better to add the pid check with the pid file?</div><div><br></div><div>Anyway, I think we should guarantee the pid file associates with the running process.</div><div><br></div><div>src/os/unix/ngx_process_cycle.<wbr>c</div><div><div><br></div><div>+static ngx_int_t</div><div>+ngx_is_same_pid(ngx_cycle_t *cycle)</div><div>+{</div><div>+ return read_pid_from_pidfile. // TODO</div><div>+}</div><div><br></div><div>static void</div><div>ngx_master_process_exit(ngx_<wbr>cycle_t *cycle)</div><div>{</div><div> ngx_uint_t i;</div><div><br></div><div>- ngx_delete_pidfile(cycle);<br></div><div>+ if (ngx_is_same_pid(cycle)) {</div></div><div>+ ngx_delete_pidfile(cycle);</div><div>+</div><div>+ } else {</div><div>+ ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "blalala...");</div><div>+ }</div><div><br></div><div>Thanks.</div><div>B.R.</div></div>
</blockquote></div><br></div>