]> granicus.if.org Git - apache/blobdiff - server/mpm/winnt/mpm_winnt.c
That's not a -D'ef - its an envar - you can't pass foo=bar in apache!
[apache] / server / mpm / winnt / mpm_winnt.c
index 4c773a36d2dac1d6658600da69ed82c9e2e5a3ba..96585a7c5c7b12e68c41d6816b64aa596a9e6cf2 100644 (file)
@@ -73,6 +73,8 @@
 #include "mpm_winnt.h"
 #include "mpm_common.h"
 
+typedef HANDLE thread;
+
 /*
  * Definitions of WINNT MPM specific config globals
  */
@@ -111,8 +113,8 @@ OSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */
 int ap_max_requests_per_child=0;
 int ap_daemons_to_start=0;
 
-static event *exit_event;
-HANDLE maintenance_event;
+static HANDLE exit_event;
+static HANDLE maintenance_event;
 apr_lock_t *start_mutex;
 DWORD my_pid;
 DWORD parent_pid;
@@ -155,16 +157,12 @@ static apr_status_t socket_cleanup(void *sock)
  * or thrown out entirely...
  */
 
-typedef void semaphore;
-typedef void event;
-
-static semaphore *
-create_semaphore(int initial)
+static HANDLE create_semaphore(int initial)
 {
     return(CreateSemaphore(NULL, initial, 1000000, NULL));
 }
 
-static void acquire_semaphore(semaphore *semaphore_id)
+static void acquire_semaphore(HANDLE semaphore_id)
 {
     int rv;
     
@@ -173,12 +171,12 @@ static void acquire_semaphore(semaphore *semaphore_id)
     return;
 }
 
-static int release_semaphore(semaphore *semaphore_id)
+static int release_semaphore(HANDLE semaphore_id)
 {
     return(ReleaseSemaphore(semaphore_id, 1, NULL));
 }
 
-static void destroy_semaphore(semaphore *semaphore_id)
+static void destroy_semaphore(HANDLE semaphore_id)
 {
     CloseHandle(semaphore_id);
 }
@@ -599,7 +597,7 @@ typedef struct joblist_s {
  */
 
 typedef struct globals_s {
-    semaphore *jobsemaphore;
+    HANDLE jobsemaphore;
     joblist *jobhead;
     joblist *jobtail;
     apr_lock_t *jobmutex;
@@ -739,7 +737,7 @@ static void accept_and_queue_connections(void * dummy)
         } while (csd < 0 && APR_STATUS_IS_EINTR(apr_get_netos_error()));
 
        if (csd < 0) {
-            if (apr_get_netos_error() == APR_FROM_OS_ERROR(WSAECONNABORTED)) {
+            if (APR_STATUS_IS_ECONNABORTED(apr_get_netos_error())) {
                ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
                            "accept: (client socket)");
             }
@@ -790,8 +788,6 @@ static PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
             memset(&context->sa_client, '\0', sizeof(context->sa_client));
         }
 
-        context->conn_io = ap_bcreate(context->ptrans, B_RDWR);
-
         /* do we NEED_DUPPED_CSD ?? */
         
         return context;
@@ -822,15 +818,15 @@ static void drain_acceptex_complport(HANDLE hComplPort, BOOLEAN bCleanUp)
         if (!rc) {
             rc = apr_get_os_error();
             if (rc == APR_FROM_OS_ERROR(ERROR_OPERATION_ABORTED)) {
-                ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
-                             "Child %d: Draining an ABORTED packet off "
+                ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server_conf,
+                             "Child %d: Draining an ABORTED packet off "
                              "the AcceptEx completion port.", my_pid);
                 continue;
             }
             break;
         }
         ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
-                     "Child %d: Draining and discarding an active connection "
+                     "Child %d: Draining and discarding an active connection "
                      "off the AcceptEx completion port.", my_pid);
         context = (PCOMP_CONTEXT) pol;
         if (context && bCleanUp) {
@@ -883,16 +879,20 @@ static int create_acceptex_context(apr_pool_t *_pconf, ap_listen_rec *lr)
     }
 
     apr_create_pool(&context->ptrans, _pconf);
-    context->conn_io = ap_bcreate(context->ptrans, B_RDWR);
-    context->recv_buf = context->conn_io->inbase;
-    context->recv_buf_size = context->conn_io->bufsiz - 2*PADDED_ADDR_SIZE;
 
+    /* recv_buf must be large enough to hold the remote and local
+     * addresses. Note that recv_buf_size is the amount of recv_buf
+     * available for AcceptEx to receive bytes into. Since we 
+     * don't want AcceptEx to do a recv, set the size to 0.
+     */
+    context->recv_buf = apr_pcalloc(_pconf, 2*PADDED_ADDR_SIZE);
+    context->recv_buf_size = 0;
 
     /* AcceptEx on the completion context. The completion context will be signaled
      * when a connection is accepted. */
     if (!AcceptEx(nsd, context->accept_socket,
                   context->recv_buf, 
-                  0, //context->recv_buf_size,
+                  context->recv_buf_size,
                   PADDED_ADDR_SIZE, PADDED_ADDR_SIZE,
                   &BytesRead,
                   (LPOVERLAPPED) context)) {
@@ -917,9 +917,6 @@ static apr_inline apr_status_t reset_acceptex_context(PCOMP_CONTEXT context)
     /* reset the buffer pools */
     apr_clear_pool(context->ptrans);
     context->sock = NULL;
-    context->conn_io = ap_bcreate(context->ptrans, B_RDWR);
-    context->recv_buf = context->conn_io->inbase;
-    context->recv_buf_size = context->conn_io->bufsiz - 2*PADDED_ADDR_SIZE;
 
     /* recreate and initialize the accept socket if it is not being reused */
     apr_get_os_sock(&nsd, context->lr->sd);
@@ -951,14 +948,19 @@ static apr_inline apr_status_t reset_acceptex_context(PCOMP_CONTEXT context)
             }
         }
 
-        if (!AcceptEx(nsd, context->accept_socket, context->recv_buf, 0,
-                      PADDED_ADDR_SIZE, PADDED_ADDR_SIZE, &BytesRead, 
+        if (!AcceptEx(nsd, context->accept_socket, 
+                      context->recv_buf,                   
+                      context->recv_buf_size,
+                      PADDED_ADDR_SIZE, 
+                      PADDED_ADDR_SIZE, 
+                      &BytesRead, 
                       (LPOVERLAPPED) context)) {
+
             rc = apr_get_netos_error();
             if (rc != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
-                ap_log_error(APLOG_MARK, APLOG_INFO, rc, server_conf,
+                ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, server_conf,
                              "reset_acceptex_context: AcceptEx failed for "
-                             "listening socket: %d and accept socket: %d", 
+                             "listening socket: %d and accept socket: %d. Getting a new accept socket.", 
                              nsd, context->accept_socket);
                 closesocket(context->accept_socket);
                 context->accept_socket = INVALID_SOCKET;
@@ -1021,7 +1023,7 @@ static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
                  * we see this now and not during AcceptEx(). Reset the
                  * AcceptEx context and continue...
                  */
-                ap_log_error(APLOG_MARK,APLOG_INFO, rc, server_conf,
+                ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, server_conf,
                              "Child %d: - GetQueuedCompletionStatus() failed", 
                              my_pid);
                 /* Reset the completion context */
@@ -1044,7 +1046,7 @@ static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
                 /* Sometimes we catch ERROR_OPERATION_ABORTED completion packets
                  * from the old child process (during a restart). Ignore them.
                  */
-                ap_log_error(APLOG_MARK,APLOG_INFO, rc, server_conf,
+                ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, server_conf,
                              "Child %d: - Draining ERROR_OPERATION_ABORTED packet off "
                              "the completion port.", my_pid);
             }
@@ -1084,9 +1086,8 @@ static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
     }
 
     /* Received a connection */
-    context->conn_io->incnt = BytesRead;
     GetAcceptExSockaddrs(context->recv_buf, 
-                         0, //context->recv_buf_size,
+                         context->recv_buf_size,
                          PADDED_ADDR_SIZE,
                          PADDED_ADDR_SIZE,
                          &context->sa_server,
@@ -1120,6 +1121,7 @@ static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
 static void worker_main(int child_num)
 {
     PCOMP_CONTEXT context = NULL;
+    apr_os_sock_info_t sockinfo;
 
     while (1) {
         conn_rec *c;
@@ -1136,11 +1138,15 @@ static void worker_main(int child_num)
         if (!context)
             break;
         sock_disable_nagle(context->accept_socket);
-        apr_put_os_sock(&context->sock, &context->accept_socket, context->ptrans);
+
+        sockinfo.os_sock = &context->accept_socket;
+        sockinfo.local   = context->sa_server;
+        sockinfo.remote  = context->sa_client;
+        sockinfo.family  = APR_INET;
+        sockinfo.type    = SOCK_STREAM;
+        apr_make_os_sock(&context->sock, &sockinfo, context->ptrans);
 
         c = ap_new_connection(context->ptrans, server_conf, context->sock,
-                              (struct sockaddr_in *) context->sa_client,
-                              (struct sockaddr_in *) context->sa_server,
                               child_num);
 
         ap_process_connection(c);
@@ -1153,7 +1159,7 @@ static void worker_main(int child_num)
         }
     }
 
-    ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server_conf,
                  "Child %d: Thread exiting.", my_pid);
 #if 0
 
@@ -1162,7 +1168,7 @@ static void worker_main(int child_num)
     /* TODO: Add code to clean-up completion contexts here */
 }
 
-static void cleanup_thread(thread **handles, int *thread_cnt, int thread_to_clean)
+static void cleanup_thread(thread *handles, int *thread_cnt, int thread_to_clean)
 {
     int i;
 
@@ -1203,7 +1209,7 @@ static void child_main()
     char* exit_event_name;
     int nthreads = ap_threads_per_child;
     int tid;
-    thread **child_handles;
+    thread *child_handles;
     int rv;
     time_t end_time;
     int i;
@@ -1266,10 +1272,10 @@ static void child_main()
     /* Create the worker thread pool */
     ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf, 
                  "Child %d: Starting %d worker threads.", my_pid, nthreads);
-    child_handles = (thread *) alloca(nthreads * sizeof(int));
+    child_handles = (thread) alloca(nthreads * sizeof(int));
     for (i = 0; i < nthreads; i++) {
-        child_handles[i] = (thread *) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) worker_main,
-                                                     NULL, 0, &tid);
+        child_handles[i] = (thread) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) worker_main,
+                                                   NULL, 0, &tid);
     }
 
     /* Begin accepting connections */
@@ -1617,7 +1623,7 @@ static int create_process(apr_pool_t *p, HANDLE *handles, HANDLE *events, int *p
                          "Parent: Unable to write duplicated socket %d to the child.", lr->sd );
             return -1;
         }
-        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
+        ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, APR_SUCCESS, server_conf,
                      "Parent: BytesWritten = %d WSAProtocolInfo = %x20", BytesWritten, *lpWSAProtocolInfo);
     }
     if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
@@ -1697,7 +1703,7 @@ static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_even
         shutdown_pending = 1;
         printf("shutdown event signaled\n");
         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, s, 
-                     "master_main: Shutdown event signaled -- doing server shutdown.");
+                     "Parent: SHUTDOWN EVENT SIGNALED -- Shutting down the server.");
         if (ResetEvent(shutdown_event) == 0) {
             ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
                          "ResetEvent(shutdown_event)");
@@ -1709,7 +1715,7 @@ static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_even
         int children_to_kill = current_live_processes;
         restart_pending = 1;
         ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, s, 
-                     "master_main: Restart event signaled. Doing a graceful restart.");
+                     "Parent: RESTART EVENT SIGNALED -- Restarting the server.");
         if (ResetEvent(restart_event) == 0) {
             ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
                          "master_main: ResetEvent(restart_event) failed.");
@@ -1740,7 +1746,7 @@ static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_even
          */
         restart_pending = 1;
         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf, 
-                     "master_main: Child process failed. Restarting the child process.");
+                     "Parent: CHILD PROCESS FAILED -- Restarting the child process.");
         ap_assert(cld < current_live_processes);
         cleanup_process(process_handles, process_kill_events, cld, &current_live_processes);
         /* APD2("main_process: child in slot %d died", rv); */
@@ -1778,7 +1784,7 @@ die_now:
         }
         for (i = 0; i < current_live_processes; i++) {
             ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, APR_SUCCESS, server_conf,
-                         "forcing termination of child #%d (handle %d)", i, process_handles[i]);
+                         "Parent: Forcing termination of child #%d (handle %d)", i, process_handles[i]);
             TerminateProcess((HANDLE) process_handles[i], 1);
         }
         return 0;  /* Tell the caller we do not want to restart */
@@ -1788,7 +1794,7 @@ die_now:
 }
 
 
-#define SERVICE_UNNAMED -1
+#define SERVICE_UNNAMED (-1)
 
 /* service_nt_main_fn needs to append the StartService() args 
  * outside of our call stack and thread as the service starts...
@@ -1802,7 +1808,7 @@ apr_array_header_t *mpm_new_argv;
 
 static apr_status_t service_to_start_success;
 static int inst_argc;
-static char **inst_argv;
+static const char * const *inst_argv;
     
 void winnt_rewrite_args(process_rec *process) 
 {
@@ -1820,10 +1826,10 @@ void winnt_rewrite_args(process_rec *process)
     char fnbuf[MAX_PATH];
     char optbuf[3];
     const char *optarg;
-    const char **new_arg;
     int fixed_args;
     char *pid;
     apr_getopt_t *opt;
+    int running_as_service = 1;
 
     osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
     GetVersionEx(&osver);
@@ -1833,8 +1839,8 @@ void winnt_rewrite_args(process_rec *process)
     if (pid) 
     {
         /* This is the child */
-        parent_pid = (DWORD) atol(pid);
         my_pid = GetCurrentProcessId();
+        parent_pid = (DWORD) atol(pid);
 
         /* The parent is responsible for providing the
          * COMPLETE ARGUMENTS REQUIRED to the child.
@@ -1863,40 +1869,39 @@ void winnt_rewrite_args(process_rec *process)
      *         The requested service's (-n) registry ConfigArgs
      *             The WinNT SCM's StartService() args
      */
-
     if (!GetModuleFileName(NULL, fnbuf, sizeof(fnbuf))) {
-        /* WARNING: There is an implict assumption here that the
-         * executable resides in the ServerRoot!
-         */
         rv = apr_get_os_error();
         ap_log_error(APLOG_MARK,APLOG_ERR, rv, NULL, 
-                     "Failed to get the running module's file name");
+                     "Failed to get the path of Apache.exe");
         exit(1);
     }
+    /* WARNING: There is an implict assumption here that the
+     * executable resides in ServerRoot or ServerRoot\bin
+     */
     def_server_root = (char *) apr_filename_of_pathname(fnbuf);
     if (def_server_root > fnbuf) {
         *(def_server_root - 1) = '\0';
-        def_server_root = ap_os_canonical_filename(process->pool, fnbuf);
+        def_server_root = (char *) apr_filename_of_pathname(fnbuf);
+        if (!strcasecmp(def_server_root, "bin"))
+            *(def_server_root - 1) = '\0';
     }
+    def_server_root = ap_os_canonical_filename(process->pool, fnbuf);
 
     /* Use process->pool so that the rewritten argv
      * lasts for the lifetime of the server process,
      * because pconf will be destroyed after the 
      * initial pre-flight of the config parser.
      */
-
-    mpm_new_argv = apr_make_array(process->pool, process->argc + 2, sizeof(char *));
-    new_arg = (char**) apr_push_array(mpm_new_argv);
-    *new_arg = (char *) process->argv[0];
-    
-    new_arg = (char**) apr_push_array(mpm_new_argv);
-    *new_arg = "-d";
-    new_arg = (char**) apr_push_array(mpm_new_argv);
-    *new_arg = def_server_root;
+    mpm_new_argv = apr_make_array(process->pool, process->argc + 2,
+                                  sizeof(const char *));
+    *(const char **)apr_push_array(mpm_new_argv) = process->argv[0];
+    *(const char **)apr_push_array(mpm_new_argv) = "-d";
+    *(const char **)apr_push_array(mpm_new_argv) = def_server_root;
 
     fixed_args = mpm_new_argv->nelts;
 
-    optbuf[0] = '-'; optbuf[2] = '\0';
+    optbuf[0] = '-';
+    optbuf[2] = '\0';
     apr_initopt(&opt, process->pool, process->argc, (char**) process->argv);
     while (apr_getopt(opt, "n:k:iu" AP_SERVER_BASEARGS, 
                       optbuf + 1, &optarg) == APR_SUCCESS) {
@@ -1908,19 +1913,21 @@ void winnt_rewrite_args(process_rec *process)
             signal_arg = optarg;
             break;
         case 'i':
-            /* TODO: warn of depreciated syntax, "use -k install instead" */
+            ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
+                "-i is deprecated.  Use -k install.");
             signal_arg = "install";
             break;
         case 'u':
-            /* TODO: warn of depreciated syntax, "use -k uninstall instead" */
+            ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
+                "-u is deprecated.  Use -k uninstall.");
             signal_arg = "uninstall";
             break;
         default:
-            new_arg = (char**) apr_push_array(mpm_new_argv);
-            *new_arg = apr_pstrdup(process->pool, optbuf);
+            *(const char **)apr_push_array(mpm_new_argv) =
+                apr_pstrdup(process->pool, optbuf);
+
             if (optarg) {
-                new_arg = (char**) apr_push_array(mpm_new_argv);
-                *new_arg = optarg;
+                *(const char **)apr_push_array(mpm_new_argv) = optarg;
             }
             break;
         }
@@ -1931,7 +1938,10 @@ void winnt_rewrite_args(process_rec *process)
 
     /* Provide a default 'run' -k arg to simplify signal_arg tests */
     if (!signal_arg)
+    {
         signal_arg = "run";
+        running_as_service = 0;
+    }
 
     if (!strcasecmp(signal_arg, "runservice")) 
     {
@@ -1955,7 +1965,7 @@ void winnt_rewrite_args(process_rec *process)
         }
     }
 
-    if (service_named == SERVICE_UNNAMED) {
+    if (service_named == SERVICE_UNNAMED && running_as_service) {
         service_named = mpm_service_set_name(process->pool, 
                                              DEFAULT_SERVICE_NAME);
     }
@@ -1969,7 +1979,7 @@ void winnt_rewrite_args(process_rec *process)
             exit(1);
         }
     }
-    else
+    else if (running_as_service)
     {
         if (service_named == APR_SUCCESS) 
         {
@@ -1980,11 +1990,11 @@ void winnt_rewrite_args(process_rec *process)
                              "Using ConfigArgs of the installed service "
                              "\"%s\".", display_name);
             }
-                       else  {
+           else  {
                 ap_log_error(APLOG_MARK,APLOG_INFO, rv, NULL,
                              "No installed ConfigArgs for the service "
                              "\"%s\", using Apache defaults.", display_name);
-                       }
+           }
         }
         else
         {
@@ -1998,10 +2008,11 @@ void winnt_rewrite_args(process_rec *process)
      * These will be used for the -k install parameters, as well as
      * for the -k start service override arguments.
      */
-    inst_argv = (char**) mpm_new_argv->elts + mpm_new_argv->nelts - inst_argc;
+    inst_argv = (const char * const *)mpm_new_argv->elts
+        + mpm_new_argv->nelts - inst_argc;
 
     process->argc = mpm_new_argv->nelts; 
-    process->argv = (char**) mpm_new_argv->elts;
+    process->argv = (const char * const *) mpm_new_argv->elts;
 }
 
 
@@ -2017,7 +2028,7 @@ static void winnt_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *pt
      */
     apr_status_t rv;
 
-    if (getenv("ONE_PROCESS"))
+    if (ap_exists_config_define("ONE_PROCESS"))
         one_process = -1;
 
     if (ap_exists_config_define("ONE_PROCESS"))
@@ -2337,6 +2348,5 @@ AP_MODULE_DECLARE_DATA module mpm_winnt_module = {
     NULL,                      /* create per-server config structure */
     NULL,                      /* merge per-server config structures */
     winnt_cmds,                        /* command apr_table_t */
-    NULL,                      /* handlers */
     winnt_hooks                /* register_hooks */
 };