Win32 master/workers model
diff --git a/src/core/nginx.c b/src/core/nginx.c
index a542eca..cfc2337 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -180,10 +180,14 @@
 };
 
 
-ngx_uint_t  ngx_max_module;
+ngx_uint_t          ngx_max_module;
 
-static ngx_uint_t  ngx_show_version;
-static ngx_uint_t  ngx_show_configure;
+static ngx_uint_t   ngx_show_version;
+static ngx_uint_t   ngx_show_configure;
+#if (NGX_WIN32)
+static char        *ngx_signal;
+#endif
+
 
 static char **ngx_os_environ;
 
@@ -303,22 +307,15 @@
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
+    if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
+        ngx_process = NGX_PROCESS_MASTER;
+    }
 
 #if (NGX_WIN32)
 
-#if 0
-
-    TODO:
-
-    if (ccf->run_as_service) {
-        if (ngx_service(cycle->log) != NGX_OK) {
-            return 1;
-        }
-
-        return 0;
+    if (ngx_signal) {
+        return ngx_signal_process(cycle, ngx_signal);
     }
-#endif
 
 #else
 
@@ -334,17 +331,17 @@
         ngx_daemonized = 1;
     }
 
+#endif
+
     if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
         return 1;
     }
 
-#endif
-
-    if (ngx_process == NGX_PROCESS_MASTER) {
-        ngx_master_process_cycle(cycle);
+    if (ngx_process == NGX_PROCESS_SINGLE) {
+        ngx_single_process_cycle(cycle);
 
     } else {
-        ngx_single_process_cycle(cycle);
+        ngx_master_process_cycle(cycle);
     }
 
     return 0;
@@ -645,6 +642,29 @@
             cycle->conf_param.len = ngx_strlen(cycle->conf_param.data);
             break;
 
+#if (NGX_WIN32)
+        case 's':
+            if (argv[++i] == NULL) {
+                ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
+                              "the option \"-s\" requires parameter");
+                return NGX_ERROR;
+            }
+
+            if (ngx_strcmp(argv[i], "stop") == 0
+                || ngx_strcmp(argv[i], "quit") == 0
+                || ngx_strcmp(argv[i], "reopen") == 0
+                || ngx_strcmp(argv[i], "reload") == 0)
+            {
+                ngx_process = NGX_PROCESS_SIGNALLER;
+                ngx_signal = argv[i];
+                break;
+            }
+
+            ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
+                          "invalid option: \"-s %s\"", argv[i]);
+            return NGX_ERROR;
+#endif
+
         default:
             ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                           "invalid option: \"%s\"", argv[i]);
@@ -786,6 +806,27 @@
 
 #endif
 
+
+    if (ccf->pid.len == 0) {
+        ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
+        ccf->pid.data = (u_char *) NGX_PID_PATH;
+    }
+
+    if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
+        return NGX_CONF_ERROR;
+    }
+
+    ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
+
+    ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
+    if (ccf->oldpid.data == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
+               NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
+
+
 #if !(NGX_WIN32)
 
     if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
@@ -814,25 +855,6 @@
         ccf->group = grp->gr_gid;
     }
 
-    if (ccf->pid.len == 0) {
-        ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
-        ccf->pid.data = (u_char *) NGX_PID_PATH;
-    }
-
-    if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
-        return NGX_CONF_ERROR;
-    }
-
-    ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
-
-    ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
-    if (ccf->oldpid.data == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
-               NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
-
 
     if (ccf->lock_file.len == 0) {
         ccf->lock_file.len = sizeof(NGX_LOCK_PATH) - 1;
diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
index f413ffb..ed9efe7 100644
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -557,29 +557,31 @@
 
         c = ls[i].connection;
 
-        if (c->read->active) {
-            if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
-                ngx_del_conn(c, NGX_CLOSE_EVENT);
+        if (c) {
+            if (c->read->active) {
+                if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
+                    ngx_del_conn(c, NGX_CLOSE_EVENT);
 
-            } else if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
+                } else if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
 
-                /*
-                 * it seems that Linux-2.6.x OpenVZ sends events
-                 * for closed shared listening sockets unless
-                 * the events was explicity deleted
-                 */
+                    /*
+                     * it seems that Linux-2.6.x OpenVZ sends events
+                     * for closed shared listening sockets unless
+                     * the events was explicity deleted
+                     */
 
-                ngx_del_event(c->read, NGX_READ_EVENT, 0);
+                    ngx_del_event(c->read, NGX_READ_EVENT, 0);
 
-            } else {
-                ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
+                } else {
+                    ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
+                }
             }
+
+            ngx_free_connection(c);
+
+            c->fd = (ngx_socket_t) -1;
         }
 
-        ngx_free_connection(c);
-
-        c->fd = (ngx_socket_t) -1;
-
         ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
                        "close listening %V #%d ", &ls[i].addr_text, ls[i].fd);
 
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
index d4186d3..2d829dd 100644
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -295,8 +295,6 @@
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-#if !(NGX_WIN32)
-
     if (ngx_test_config) {
 
         if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
@@ -325,8 +323,6 @@
         }
     }
 
-#endif
-
 
     if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
         goto failed;
@@ -928,8 +924,6 @@
 }
 
 
-#if !(NGX_WIN32)
-
 ngx_int_t
 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
 {
@@ -938,6 +932,10 @@
     ngx_file_t  file;
     u_char      pid[NGX_INT64_LEN + 2];
 
+    if (ngx_process > NGX_PROCESS_MASTER) {
+        return NGX_OK;
+    }
+
     ngx_memzero(&file, sizeof(ngx_file_t));
 
     file.name = *name;
@@ -987,8 +985,6 @@
     }
 }
 
-#endif
-
 
 static ngx_int_t
 ngx_test_lockfile(u_char *file, ngx_log_t *log)
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 35a2417..8769bcd 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -227,9 +227,9 @@
                                   NGX_FILE_DEFAULT_ACCESS);
 
     if (ngx_stderr.fd == NGX_INVALID_FILE) {
-        ngx_message_box("nginx", MB_OK, ngx_errno,
-                        "Could not open error log file: "
-                        ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
+        ngx_event_log(ngx_errno, 
+                      "Could not open error log file: "
+                      ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
         return NULL;
     }