diff src/os/unix/ngx_process_cycle.c @ 309:2e899477243a

nginx-0.0.3-2004-04-09-20:03:04 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 09 Apr 2004 16:03:04 +0000
parents 4b1a3a4acc60
children a9a9af2c7370
line wrap: on
line diff
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -3,6 +3,8 @@
 #include <ngx_core.h>
 #include <ngx_event.h>
 
+#include <nginx.h>
+
 
 static void ngx_master_exit(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx);
 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
@@ -531,3 +533,54 @@ int ngx_worker_thread_cycle(void *data)
 }
 
 #endif
+
+
+static ngx_int_t ngx_create_pid_file(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx)
+{
+    size_t             len;
+    u_char             pid[NGX_INT64_LEN + 1];
+    ngx_core_conf_t   *ccf;
+
+    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
+
+    if (ccf->pid.len == 0) {
+        ccf->pid.len = sizeof(NGINX_PID) - 1;
+        ccf->pid.data = NGINX_PID;
+        ccf->newpid.len = sizeof(NGINX_NEW_PID) - 1;
+        ccf->newpid.data = NGINX_NEW_PID;
+
+    } else {
+        ccf->newpid.len = ccf->pid.len + sizeof(NGINX_NEW_PID_EXT);
+        if (!(ccf->newpid.data = ngx_alloc(ccf->newpid.len, cycle->log))) {
+            return NGX_ERROR;
+        }
+
+        ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len),
+                   NGINX_NEW_PID_EXT, sizeof(NGINX_NEW_PID_EXT));
+    }
+
+    len = ngx_snprintf((char *) pid, /* STUB */ 10, PID_T_FMT, ngx_getpid());
+    ngx_memzero(&ctx->pid, sizeof(ngx_file_t));
+    ctx->pid.name = ngx_inherited ? ccf->newpid : ccf->pid;
+    ctx->name = ccf->pid.data;
+
+    ctx->pid.fd = ngx_open_file(ctx->pid.name.data, NGX_FILE_RDWR,
+                                NGX_FILE_CREATE_OR_OPEN);
+
+    if (ctx->pid.fd == NGX_INVALID_FILE) {
+        ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+                      ngx_open_file_n " \"%s\" failed", ctx->pid.name.data);
+        return NGX_ERROR;
+    }
+
+    if (ngx_write_file(&ctx->pid, pid, len, 0) == NGX_ERROR) {
+        return NGX_ERROR;
+    }
+
+    if (ngx_close_file(ctx->pid.fd) == NGX_FILE_ERROR) {
+        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+                      ngx_close_file_n " \"%s\" failed", ctx->pid.name.data);
+    }
+
+    return NGX_OK;
+}