]> granicus.if.org Git - apache/blobdiff - server/mpm/perchild/perchild.c
APRize disabling nagle (setting TCP_NODELAY).
[apache] / server / mpm / perchild / perchild.c
index bca25653fa9a675f856acc2934027cead9c03e4b..ab3a0cbd574d1fc070936087a8215339af7f1bfa 100644 (file)
 #include <pwd.h>
 #include <pthread.h>
 #include <sys/stat.h>
-#include <signal.h>
 #include <sys/un.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <stropts.h>
 
 /*
  * Actual definitions of config globals
@@ -108,34 +110,34 @@ static int max_requests_per_child = 0;
 static const char *ap_pid_fname=NULL;
 API_VAR_EXPORT const char *ap_scoreboard_fname=NULL;
 static int num_daemons=0;
+static int curr_child_num=0;
 static int workers_may_exit = 0;
 static int requests_this_child;
 static int num_listenfds = 0;
-static ap_socket_t **listenfds;
+static apr_socket_t **listenfds;
+static jmp_buf jmpbuffer;
 
 struct child_info_t {
     uid_t uid;
     gid_t gid;
-};
-
-struct socket_info_t {
-    const char *sname;   /* The servername */
-    int        cnum;     /* The child number */
+    int sd;
 };
 
 typedef struct {
-    const char *sockname;
+    const char *sockname;    /* The base name for the socket */
+    const char *fullsockname;   /* socket base name + extension */
+    int        sd;       /* The socket descriptor */
+    int        sd2;       /* The socket descriptor */
 } perchild_server_conf;
 
 typedef struct child_info_t child_info_t;
-typedef struct socket_info_t socket_info_t;
 
 /* Tables used to determine the user and group each child process should
  * run as.  The hash table is used to correlate a server name with a child
  * process.
  */
 static child_info_t child_info_table[HARD_SERVER_LIMIT];
-static ap_hash_t    *socket_info_table = NULL;
+static int          thread_socket_table[HARD_THREAD_LIMIT];
 
 
 struct ap_ctable    ap_child_table[HARD_SERVER_LIMIT];
@@ -154,8 +156,8 @@ char ap_coredump_dir[MAX_STRING_LEN];
 
 module MODULE_VAR_EXPORT mpm_perchild_module;
 
-static ap_file_t *pipe_of_death_in = NULL;
-static ap_file_t *pipe_of_death_out = NULL;
+static apr_file_t *pipe_of_death_in = NULL;
+static apr_file_t *pipe_of_death_out = NULL;
 static pthread_mutex_t pipe_of_death_mutex;
 
 /* *Non*-shared http_main globals... */
@@ -179,9 +181,9 @@ static int one_process = 0;
 int raise_sigstop_flags;
 #endif
 
-static ap_pool_t *pconf;               /* Pool for config stuff */
-static ap_pool_t *pchild;              /* Pool for httpd child stuff */
-static ap_pool_t *thread_pool_parent; /* Parent of per-thread pools */
+static apr_pool_t *pconf;              /* Pool for config stuff */
+static apr_pool_t *pchild;             /* Pool for httpd child stuff */
+static apr_pool_t *thread_pool_parent; /* Parent of per-thread pools */
 static pthread_mutex_t thread_pool_parent_mutex;
 
 static int child_num;
@@ -202,7 +204,7 @@ static pthread_mutex_t idle_thread_count_mutex;
 #define SAFE_ACCEPT(stmt) APR_SUCCESS
 #else
 #define SAFE_ACCEPT(stmt) (stmt)
-static ap_lock_t *process_accept_mutex;
+static apr_lock_t *process_accept_mutex;
 #endif /* NO_SERIALIZED_ACCEPT */
 static const char *lock_fname;
 static pthread_mutex_t thread_accept_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -216,7 +218,7 @@ API_EXPORT(int) ap_get_max_daemons(void)
 static void clean_child_exit(int code)
 {
     if (pchild) {
-       ap_destroy_pool(pchild);
+       apr_destroy_pool(pchild);
     }
     exit(code);
 }
@@ -225,7 +227,7 @@ static void clean_child_exit(int code)
 static void sig_coredump(int sig)
 {
     chdir(ap_coredump_dir);
-    ap_signal(sig, SIG_DFL);
+    apr_signal(sig, SIG_DFL);
     kill(getpid(), sig);
     /* At this point we've got sig blocked, because we're still inside
      * the signal handler.  When we leave the signal handler it will
@@ -292,7 +294,7 @@ static void ap_start_restart(int graceful)
     restart_pending = 1;
     is_graceful = graceful;
     if (is_graceful) {
-        ap_kill_cleanup(pconf, NULL, ap_cleanup_shared_mem);
+        apr_kill_cleanup(pconf, NULL, ap_cleanup_shared_mem);
     }
 }
 
@@ -378,36 +380,36 @@ static void set_signals(void)
        ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGWINCH)");
 #else
     if (!one_process) {
-       ap_signal(SIGSEGV, sig_coredump);
+       apr_signal(SIGSEGV, sig_coredump);
 #ifdef SIGBUS
-       ap_signal(SIGBUS, sig_coredump);
+       apr_signal(SIGBUS, sig_coredump);
 #endif /* SIGBUS */
 #ifdef SIGABORT
-       ap_signal(SIGABORT, sig_coredump);
+       apr_signal(SIGABORT, sig_coredump);
 #endif /* SIGABORT */
 #ifdef SIGABRT
-       ap_signal(SIGABRT, sig_coredump);
+       apr_signal(SIGABRT, sig_coredump);
 #endif /* SIGABRT */
 #ifdef SIGILL
-       ap_signal(SIGILL, sig_coredump);
+       apr_signal(SIGILL, sig_coredump);
 #endif /* SIGILL */
 #ifdef SIGXCPU
-       ap_signal(SIGXCPU, SIG_DFL);
+       apr_signal(SIGXCPU, SIG_DFL);
 #endif /* SIGXCPU */
 #ifdef SIGXFSZ
-       ap_signal(SIGXFSZ, SIG_DFL);
+       apr_signal(SIGXFSZ, SIG_DFL);
 #endif /* SIGXFSZ */
     }
 
-    ap_signal(SIGTERM, sig_term);
+    apr_signal(SIGTERM, sig_term);
 #ifdef SIGHUP
-    ap_signal(SIGHUP, restart);
+    apr_signal(SIGHUP, restart);
 #endif /* SIGHUP */
 #ifdef SIGWINCH
-    ap_signal(SIGWINCH, restart);
+    apr_signal(SIGWINCH, restart);
 #endif /* SIGWINCH */
 #ifdef SIGPIPE
-    ap_signal(SIGPIPE, SIG_IGN);
+    apr_signal(SIGPIPE, SIG_IGN);
 #endif /* SIGPIPE */
 
 #endif
@@ -427,16 +429,17 @@ int ap_graceful_stop_signalled(void)
  * Child process main loop.
  */
 
-static void process_socket(ap_pool_t *p, ap_socket_t *sock, long conn_id)
+static void process_socket(apr_pool_t *p, apr_socket_t *sock, long conn_id)
 {
     BUFF *conn_io;
     conn_rec *current_conn;
     ap_iol *iol;
     int csd;
-    ap_status_t rv;
+    apr_status_t rv;
+    int thread_num = conn_id % HARD_THREAD_LIMIT;
 
-    if ((rv = ap_get_os_sock(&csd, sock)) != APR_SUCCESS) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "ap_get_os_sock");
+    if ((rv = apr_get_os_sock(&csd, sock)) != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "apr_get_os_sock");
     }
 
     if (csd >= FD_SETSIZE) {
@@ -445,11 +448,13 @@ static void process_socket(ap_pool_t *p, ap_socket_t *sock, long conn_id)
                      "to rebuild Apache with a larger FD_SETSIZE "
                      "(currently %d)", 
                      csd, FD_SETSIZE);
-        ap_close_socket(sock);
+        apr_close_socket(sock);
         return;
     }
 
-    ap_sock_disable_nagle(csd);
+    if (thread_socket_table[thread_num] < 0) {
+        ap_sock_disable_nagle(sock);
+    }
     iol = ap_iol_attach_socket(p, sock);
     conn_io = ap_bcreate(p, B_RDWR);
     ap_bpush_iol(conn_io, iol);
@@ -513,10 +518,10 @@ static void check_pipe_of_death(void)
     if (!workers_may_exit) {
         int ret;
         char pipe_read_char;
-        ap_ssize_t n = 1;
+        apr_ssize_t n = 1;
 
-        ret = ap_recv(listenfds[0], &pipe_read_char, &n);
-        if (ap_canonical_error(ret) == APR_EAGAIN) {
+        ret = apr_recv(listenfds[0], &pipe_read_char, &n);
+        if (apr_canonical_error(ret) == APR_EAGAIN) {
             /* It lost the lottery. It must continue to suffer
              * through a life of servitude. */
         }
@@ -533,27 +538,28 @@ static void check_pipe_of_death(void)
 
 static void *worker_thread(void *arg)
 {
-    ap_socket_t *csd = NULL;
-    ap_pool_t *tpool;          /* Pool for this thread           */
-    ap_pool_t *ptrans;         /* Pool for per-transaction stuff */
-    ap_socket_t *sd = NULL;
+    apr_socket_t *csd = NULL;
+    apr_pool_t *tpool;         /* Pool for this thread           */
+    apr_pool_t *ptrans;                /* Pool for per-transaction stuff */
+    apr_socket_t *sd = NULL;
     int srv;
     int curr_pollfd, last_pollfd = 0;
     int thread_just_started = 1;
     int thread_num = *((int *) arg);
     long conn_id = child_num * HARD_THREAD_LIMIT + thread_num;
-    ap_pollfd_t *pollset;
+    apr_pollfd_t *pollset;
     int n;
-    ap_status_t rv;
+    apr_status_t rv;
 
     pthread_mutex_lock(&thread_pool_parent_mutex);
-    ap_create_pool(&tpool, thread_pool_parent);
+    apr_create_pool(&tpool, thread_pool_parent);
     pthread_mutex_unlock(&thread_pool_parent_mutex);
-    ap_create_pool(&ptrans, tpool);
+    apr_create_pool(&ptrans, tpool);
 
-    ap_setup_poll(&pollset, num_listenfds+1, tpool);
-    for(n=0 ; n <= num_listenfds ; ++n)
-        ap_add_poll_socket(pollset, listenfds[n], APR_POLLIN);
+    apr_setup_poll(&pollset, num_listenfds+1, tpool);
+    for(n=0 ; n <= num_listenfds ; ++n) {
+        apr_add_poll_socket(pollset, listenfds[n], APR_POLLIN);
+    }
 
     while (!workers_may_exit) {
         workers_may_exit |= (max_requests_per_child != 0) && (requests_this_child <= 0);
@@ -577,38 +583,48 @@ static void *worker_thread(void *arg)
             pthread_mutex_unlock(&thread_accept_mutex);
             break;
         }
-        if ((rv = SAFE_ACCEPT(ap_lock(process_accept_mutex)))
+        if ((rv = SAFE_ACCEPT(apr_lock(process_accept_mutex)))
             != APR_SUCCESS) {
             ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
-                         "ap_lock failed. Attempting to shutdown "
+                         "apr_lock failed. Attempting to shutdown "
                          "process gracefully.");
             workers_may_exit = 1;
         }
 
         while (!workers_may_exit) {
-            ap_int16_t event;
-            srv = ap_poll(pollset, &n, -1);
+            apr_int16_t event;
+            srv = apr_poll(pollset, &n, -1);
 
             if (srv != APR_SUCCESS) {
-                if (ap_canonical_error(srv) == APR_EINTR) {
+                if (apr_canonical_error(srv) == APR_EINTR) {
                     continue;
                 }
 
-                /* ap_poll() will only return errors in catastrophic
+                /* apr_poll() will only return errors in catastrophic
                  * circumstances. Let's try exiting gracefully, for now. */
                 ap_log_error(APLOG_MARK, APLOG_ERR, srv, (const server_rec *)
-                             ap_server_conf, "ap_poll: (listen)");
+                             ap_server_conf, "apr_poll: (listen)");
                 workers_may_exit = 1;
             }
             if (workers_may_exit) break;
 
-            ap_get_revents(&event, listenfds[0], pollset);
+            apr_get_revents(&event, listenfds[0], pollset);
             if (event & APR_POLLIN) {
                 /* A process got a signal on the shutdown pipe. Check if we're
                  * the lucky process to die. */
                 check_pipe_of_death();
                 continue;
             }
+            
+            apr_get_revents(&event, listenfds[1], pollset);
+            if (event & APR_POLLIN || event & APR_POLLOUT) {
+                /* This request is from another child in our current process.
+                 * We should set a flag here, and then below we will read
+                 * two bytes (the socket number and the NULL byte.
+                 */
+                thread_socket_table[thread_num] = -2;
+                goto got_from_other_child;
+            }
 
             if (num_listenfds == 1) {
                 sd = ap_listeners->sd;
@@ -623,7 +639,7 @@ static void *worker_thread(void *arg)
                         curr_pollfd = 1;
                     }
                     /* XXX: Should we check for POLLERR? */
-                    ap_get_revents(&event, listenfds[curr_pollfd], pollset);
+                    apr_get_revents(&event, listenfds[curr_pollfd], pollset);
                     if (event & APR_POLLIN) {
                         last_pollfd = curr_pollfd;
                         sd = listenfds[curr_pollfd];
@@ -634,13 +650,13 @@ static void *worker_thread(void *arg)
         }
     got_fd:
         if (!workers_may_exit) {
-            if ((rv = ap_accept(&csd, sd, ptrans)) != APR_SUCCESS) {
-                ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, "ap_accept");
+            if ((rv = apr_accept(&csd, sd, ptrans)) != APR_SUCCESS) {
+                ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, "apr_accept");
             }
-            if ((rv = SAFE_ACCEPT(ap_unlock(process_accept_mutex)))
+            if ((rv = SAFE_ACCEPT(apr_unlock(process_accept_mutex)))
                 != APR_SUCCESS) {
                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
-                             "ap_unlock failed. Attempting to shutdown "
+                             "apr_unlock failed. Attempting to shutdown "
                              "process gracefully.");
                 workers_may_exit = 1;
             }
@@ -655,13 +671,47 @@ static void *worker_thread(void *arg)
                 }
             }
             pthread_mutex_unlock(&idle_thread_count_mutex);
-            process_socket(ptrans, csd, conn_id);
+        got_from_other_child:
+            if (thread_socket_table[thread_num] == -2) {
+                struct msghdr msg;
+                struct cmsghdr *cmsg;
+                char sockname[80];
+                struct iovec iov;
+                int ret, sd, dp;
+
+                iov.iov_base = sockname;
+                iov.iov_len = 80;
+
+                msg.msg_name = NULL;
+                msg.msg_namelen = 0;
+                msg.msg_iov = &iov;
+                msg.msg_iovlen = 1;
+
+                cmsg = apr_palloc(ptrans, sizeof(*cmsg) + sizeof(sd));
+                cmsg->cmsg_len = sizeof(*cmsg) + sizeof(sd);
+                msg.msg_control = cmsg;
+                msg.msg_controllen = cmsg->cmsg_len;
+                msg.msg_flags = 0;
+                
+                ret = recvmsg(child_info_table[child_num].sd, &msg, 0);
+
+                memcpy(&dp, CMSG_DATA(cmsg), sizeof(dp));
+
+                thread_socket_table[thread_num] = dp;
+                apr_put_os_sock(&csd, &child_info_table[child_num].sd, ptrans);
+            }
+            if (setjmp(jmpbuffer) != 1) {
+                process_socket(ptrans, csd, conn_id);
+            }
+            else {
+                thread_socket_table[thread_num] = -1;
+            }  
             requests_this_child--;
        } else {
-            if ((rv = SAFE_ACCEPT(ap_unlock(process_accept_mutex)))
+            if ((rv = SAFE_ACCEPT(apr_unlock(process_accept_mutex)))
                 != APR_SUCCESS) {
                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
-                             "ap_unlock failed. Attempting to shutdown "
+                             "apr_unlock failed. Attempting to shutdown "
                              "process gracefully.");
                 workers_may_exit = 1;
             }
@@ -671,11 +721,11 @@ static void *worker_thread(void *arg)
             pthread_mutex_unlock(&idle_thread_count_mutex);
            break;
        }
-        ap_clear_pool(ptrans);
+        apr_clear_pool(ptrans);
     }
 
     pthread_mutex_lock(&thread_pool_parent_mutex);
-    ap_destroy_pool(tpool);
+    apr_destroy_pool(tpool);
     pthread_mutex_unlock(&thread_pool_parent_mutex);
     pthread_mutex_lock(&worker_thread_count_mutex);
     worker_thread_count--;
@@ -764,78 +814,21 @@ static int perchild_setup_child(int childnum)
     return 0;
 }
 
-static int create_child_socket(int child_num, ap_pool_t *p)
-{
-    struct sockaddr_un unix_addr;
-    mode_t omask;
-    int rc;
-    int sd;
-    perchild_server_conf *sconf = (perchild_server_conf *)
-              ap_get_module_config(ap_server_conf->module_config, &mpm_perchild_module);
-    char *socket_name = ap_palloc(p, strlen(sconf->sockname) + 6);
-
-    ap_snprintf(socket_name, strlen(socket_name), "%s.%d", sconf->sockname, child_num);
-    if (unlink(socket_name) < 0 &&
-        errno != ENOENT) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
-                     "Couldn't unlink unix domain socket %s",
-                     socket_name);
-        /* Just a warning; don't bail out */
-    }
-
-    if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
-                     "Couldn't create unix domain socket");
-        return -1;
-    }
-
-    memset(&unix_addr, 0, sizeof(unix_addr));
-    unix_addr.sun_family = AF_UNIX;
-    strcpy(unix_addr.sun_path, socket_name);
-
-    omask = umask(0077); /* so that only Apache can use socket */
-    rc = bind(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
-    umask(omask); /* can't fail, so can't clobber errno */
-    if (rc < 0) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
-                     "Couldn't bind unix domain socket %s",
-                     socket_name);
-        return -1;
-    }
-
-    if (listen(sd, DEFAULT_PERCHILD_LISTENBACKLOG) < 0) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
-                     "Couldn't listen on unix domain socket");
-        return -1;
-    }
-
-    if (!geteuid()) {
-        if (chown(socket_name, unixd_config.user_id, -1) < 0) {
-            ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
-                         "Couldn't change owner of unix domain socket %s",
-                         socket_name);
-            return -1;
-        }
-    }
-    return sd;
-}
-
 static void child_main(int child_num_arg)
 {
     sigset_t sig_mask;
     int signal_received;
     int i;
     ap_listen_rec *lr;
-    ap_status_t rv;
-    int sd;
+    apr_status_t rv;
 
     my_pid = getpid();
     child_num = child_num_arg;
-    ap_create_pool(&pchild, pconf);
+    apr_create_pool(&pchild, pconf);
 
     /*stuff to do before we switch id's, so we have permissions.*/
 
-    rv = SAFE_ACCEPT(ap_child_init_lock(&process_accept_mutex, lock_fname,
+    rv = SAFE_ACCEPT(apr_child_init_lock(&process_accept_mutex, lock_fname,
                                         pchild));
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
@@ -843,9 +836,6 @@ static void child_main(int child_num_arg)
         clean_child_exit(APEXIT_CHILDFATAL);
     }
 
-    /* Add the sockets for this child process's virtual hosts */
-    sd = create_child_socket(child_num, pchild);
-
     if (perchild_setup_child(child_num)) {
        clean_child_exit(APEXIT_CHILDFATAL);
     }
@@ -876,11 +866,16 @@ static void child_main(int child_num_arg)
     /* Set up the pollfd array, num_listenfds + 1 for the pipe and 1 for
      * the child socket.
      */
-    listenfds = ap_pcalloc(pchild, sizeof(*listenfds) * (num_listenfds + 2));
+    listenfds = apr_pcalloc(pchild, sizeof(*listenfds) * (num_listenfds + 2));
 #if APR_FILES_AS_SOCKETS
-    ap_socket_from_file(&listenfds[0], pipe_of_death_in);
+    apr_socket_from_file(&listenfds[0], pipe_of_death_in);
 #endif
-    for (lr = ap_listeners, i = 1; i <= num_listenfds; lr = lr->next, ++i)
+
+    /* The child socket */
+    apr_put_os_sock(&listenfds[1], &child_info_table[child_num].sd, pchild);
+
+    num_listenfds++;
+    for (lr = ap_listeners, i = 2; i <= num_listenfds; lr = lr->next, ++i)
         listenfds[i]=lr->sd;
 
     /* Setup worker threads */
@@ -893,7 +888,7 @@ static void child_main(int child_num_arg)
     for (i = 0; i < max_threads; i++) {
         worker_thread_free_ids[i] = i;
     }
-    ap_create_pool(&thread_pool_parent, pchild);
+    apr_create_pool(&thread_pool_parent, pchild);
     pthread_mutex_init(&thread_pool_parent_mutex, NULL);
     pthread_mutex_init(&idle_thread_count_mutex, NULL);
     pthread_mutex_init(&worker_thread_count_mutex, NULL);
@@ -976,7 +971,7 @@ static int make_child(server_rec *s, int slot, time_t now)
         RAISE_SIGSTOP(MAKE_CHILD);
 
        /* XXX - For an unthreaded server, a signal handler will be necessary
-        ap_signal(SIGTERM, just_die);
+        apr_signal(SIGTERM, just_die);
        */
         child_main(slot);
 
@@ -1069,8 +1064,8 @@ static void perform_child_maintenance(void)
 static void server_main_loop(int remaining_children_to_start)
 {
     int child_slot;
-    ap_wait_t status;
-    ap_proc_t pid;
+    apr_wait_t status;
+    apr_proc_t pid;
     int i;
 
     while (!restart_pending && !shutdown_pending) {
@@ -1105,7 +1100,7 @@ static void server_main_loop(int remaining_children_to_start)
                }
 #if APR_HAS_OTHER_CHILD
            }
-           else if (ap_reap_other_child(&pid, status) == 0) {
+           else if (apr_reap_other_child(&pid, status) == 0) {
                /* handled */
 #endif
            }
@@ -1144,26 +1139,26 @@ static void server_main_loop(int remaining_children_to_start)
     }
 }
 
-int ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
+int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
 {
     int remaining_children_to_start;
     int i;
-    ap_status_t rv;
-    ap_ssize_t one = 1;
+    apr_status_t rv;
+    apr_ssize_t one = 1;
 
     pconf = _pconf;
     ap_server_conf = s;
-    if ((rv = ap_create_pipe(&pipe_of_death_in, &pipe_of_death_out, pconf)) 
+    if ((rv = apr_create_pipe(&pipe_of_death_in, &pipe_of_death_out, pconf)) 
         != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv,
                      (const server_rec*) ap_server_conf,
-                     "ap_create_pipe (pipe_of_death)");
+                     "apr_create_pipe (pipe_of_death)");
         exit(1);
     }
-    if ((rv = ap_set_pipe_timeout(pipe_of_death_in, 0)) != APR_SUCCESS) {
+    if ((rv = apr_set_pipe_timeout(pipe_of_death_in, 0)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_ERR, rv,
                      (const server_rec*) ap_server_conf,
-                     "ap_set_pipe_timeout (pipe_of_death)");
+                     "apr_set_pipe_timeout (pipe_of_death)");
         exit(1);
     }
     ap_server_conf = s;
@@ -1176,10 +1171,10 @@ int ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
     ap_log_pid(pconf, ap_pid_fname);
 
     /* Initialize cross-process accept lock */
-    lock_fname = ap_psprintf(_pconf, "%s.%u",
+    lock_fname = apr_psprintf(_pconf, "%s.%u",
                              ap_server_root_relative(_pconf, lock_fname),
                              my_pid);
-    rv = SAFE_ACCEPT(ap_create_lock(&process_accept_mutex, APR_MUTEX,
+    rv = SAFE_ACCEPT(apr_create_lock(&process_accept_mutex, APR_MUTEX,
                                     APR_CROSS_PROCESS, lock_fname, _pconf));
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
@@ -1255,7 +1250,7 @@ int ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
     }
 
     /* we've been told to restart */
-    ap_signal(SIGHUP, SIG_IGN);
+    apr_signal(SIGHUP, SIG_IGN);
 
     if (one_process) {
        /* not worth thinking about */
@@ -1279,8 +1274,8 @@ int ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
        }
        /* give the children the signal to die */
         for (i = 0; i < num_daemons;) {
-            if ((rv = ap_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS) {
-                if (ap_canonical_error(rv) == APR_EINTR) continue;
+            if ((rv = apr_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS) {
+                if (apr_canonical_error(rv) == APR_EINTR) continue;
                 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
                              "write pipe_of_death");
             }
@@ -1303,19 +1298,21 @@ int ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
     return 0;
 }
 
-static void perchild_pre_config(ap_pool_t *p, ap_pool_t *plog, ap_pool_t *ptemp)
+static void perchild_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
 {
     static int restart_num = 0;
+    int no_detach = 0;
     int i;
 
     one_process = !!getenv("ONE_PROCESS");
+    no_detach = !!getenv("NO_DETACH");
 
     /* sigh, want this only the second time around */
     if (restart_num++ == 1) {
        is_graceful = 0;
 
-       if (!one_process) {
-           ap_detach();
+       if (!one_process && !no_detach) {
+           apr_detach();
        }
 
        my_pid = getpid();
@@ -1333,36 +1330,135 @@ static void perchild_pre_config(ap_pool_t *p, ap_pool_t *plog, ap_pool_t *ptemp)
     lock_fname = DEFAULT_LOCKFILE;
     max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
     ap_perchild_set_maintain_connection_status(1);
+    curr_child_num = 0;
 
-    ap_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
+    apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
 
     for (i = 0; i < HARD_SERVER_LIMIT; i++) {
         child_info_table[i].uid = -1;
         child_info_table[i].gid = -1;
+        child_info_table[i].sd = -1;
+    }
+    for (i = 0; i < HARD_THREAD_LIMIT; i++) {
+        thread_socket_table[i] = -1;
     }
 }
 
-static int perchild_post_read(request_rec *r)
+static int pass_request(request_rec *r)
 {
-    const char *hostname = ap_table_get(r->headers_in, "Host");
-    char *process_num;
-    int num;
+    apr_socket_t *thesock = ap_iol_get_socket(r->connection->client->iol);
+    struct msghdr msg;
+    struct cmsghdr *cmsg;
+    int sfd;
+    struct iovec iov;
+    char *foo = r->connection->client->inbase;
+    int len = r->connection->client->inptr - r->connection->client->inbase;
+    perchild_server_conf *sconf = (perchild_server_conf *)
+                            ap_get_module_config(r->server->module_config, 
+                                                 &mpm_perchild_module);
+
+    apr_get_os_sock(&sfd, thesock);
+
+    iov.iov_base = (char *)sconf->fullsockname;
+    iov.iov_len = strlen(sconf->fullsockname) + 1;
+
+    msg.msg_name = NULL;
+    msg.msg_namelen = 0;
+    msg.msg_iov = &iov;
+    msg.msg_iovlen = 1;
+
+    cmsg = apr_palloc(r->pool, sizeof(*cmsg) + sizeof(sfd));
+    cmsg->cmsg_len = sizeof(*cmsg) + sizeof(int);
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+
+    memcpy(CMSG_DATA(cmsg), &sfd, sizeof(sfd));
+
+    msg.msg_control = cmsg;
+    msg.msg_controllen = cmsg->cmsg_len;
+    msg.msg_flags=0;
 
-fprintf(stderr, "In perchild_post_read\n");
-    process_num = ap_hash_get(socket_info_table, hostname, 0);
-    if (process_num) {
-        num = atoi(process_num);
+    if (sendmsg(sconf->sd2, &msg, 0) == -1) {
+        apr_destroy_pool(r->pool);
+        return -1;
     }
-    else {
-        num = -1;
+
+    write(sconf->sd2, foo, len);
+   
+    apr_destroy_pool(r->pool);
+    return 1;
+}
+
+static char *make_perchild_socket(const char *fullsockname, int sd[2])
+{
+    socketpair(PF_UNIX, SOCK_STREAM, 0, sd);
+    return NULL;
+}
+
+
+static void perchild_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
+{
+    int i;
+    server_rec *sr;
+    perchild_server_conf *sconf;
+    int def_sd[2];
+    
+    def_sd[0] = -1;
+    def_sd[1] = -1;
+
+    for (sr = s; sr; sr = sr->next) {
+        sconf = (perchild_server_conf *)ap_get_module_config(sr->module_config,
+                                                      &mpm_perchild_module);
+
+        if (sconf->sd == -1) {
+            sconf->fullsockname = apr_pstrcat(sr->process->pool, 
+                                             sconf->sockname, ".DEFAULT", NULL);
+            if (def_sd[0] == -1) {
+                if (!make_perchild_socket(sconf->fullsockname, def_sd)) {
+                    /* log error */
+                }
+            }
+            sconf->sd = def_sd[0];
+            sconf->sd2 = def_sd[1];
+        }
     }
 
-    if (num != child_num) {
-        /* package the request and send it to another child */
-fprintf(stderr, "leaving DECLINED\n");
-        return DECLINED;
+    for (i = 0; i < num_daemons; i++) {
+        if (child_info_table[i].uid == -1) {
+            child_info_table[i].sd = def_sd[0];
+        }
+    }
+}
+
+static int perchild_post_read(request_rec *r)
+{
+    int thread_num = r->connection->id % HARD_THREAD_LIMIT;
+    perchild_server_conf *sconf = (perchild_server_conf *)
+                            ap_get_module_config(r->server->module_config, 
+                                                 &mpm_perchild_module);
+
+    if (thread_socket_table[thread_num] != -1) {
+        apr_socket_t *csd = NULL;
+        ap_iol *iol;
+
+        apr_put_os_sock(&csd, &thread_socket_table[thread_num], 
+                             r->connection->client->pool);
+        ap_sock_disable_nagle(thread_socket_table[thread_num]);
+        iol = ap_iol_attach_socket(r->connection->client->pool, csd);
+        ap_bpush_iol(r->connection->client, iol);
+        return OK;
+    }
+    else {
+        if (sconf->sd != child_info_table[child_num].sd) {
+            if (pass_request(r) == -1) {
+                ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0,
+                             ap_server_conf, "Could not pass request to proper "
+                             "child, request will not be honored.");
+            }
+            longjmp(jmpbuffer, 1); 
+        }
+        return OK;
     }
-fprintf(stderr, "leaving OK\n");
     return OK;
 }
 
@@ -1372,6 +1468,7 @@ static void perchild_hooks(void)
     one_process = 0;
 
     ap_hook_pre_config(perchild_pre_config, NULL, NULL, AP_HOOK_MIDDLE); 
+    ap_hook_post_config(perchild_post_config, NULL, NULL, AP_HOOK_MIDDLE); 
     /* This must be run absolutely first.  If this request isn't for this
      * server then we need to forward it to the proper child.  No sense
      * tying up this server running more post_read request hooks if it is
@@ -1554,7 +1651,7 @@ static const char *set_maintain_connection_status(cmd_parms *cmd,
 
 static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, const char *arg) 
 {
-    ap_finfo_t finfo;
+    apr_finfo_t finfo;
     const char *fname;
     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
     if (err != NULL) {
@@ -1562,55 +1659,61 @@ static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, const char *arg
     }
 
     fname = ap_server_root_relative(cmd->pool, arg);
-    if ((ap_stat(&finfo, fname, cmd->pool) != APR_SUCCESS) || 
+    if ((apr_stat(&finfo, fname, cmd->pool) != APR_SUCCESS) || 
         (finfo.filetype != APR_DIR)) {
-       return ap_pstrcat(cmd->pool, "CoreDumpDirectory ", fname, 
+       return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname, 
                          " does not exist or is not a directory", NULL);
     }
-    ap_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
+    apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
     return NULL;
 }
 
-static const char *set_childprocess(cmd_parms *cmd, void *dummy, const char *p,
-                                    const char *u, const char *g) 
+static const char *set_child_per_uid(cmd_parms *cmd, void *dummy, const char *u,
+                                     const char *g, const char *num)
 {
-    int curr_child_num = atoi(p);
-    child_info_t *ug = &child_info_table[curr_child_num - 1];
+    int i;
+    int max_this_time = atoi(num) + curr_child_num;
+    for (i = curr_child_num; i < max_this_time; i++, curr_child_num++); {
+        child_info_t *ug = &child_info_table[i - 1];
 
-    if (curr_child_num > num_daemons) {
-        return "Trying to use more child ID's than NumServers.  Increase "
-               "NumServers in your config file.";
+        if (i > num_daemons) {
+            return "Trying to use more child ID's than NumServers.  Increase "
+                   "NumServers in your config file.";
+        }
+    
+        ug->uid = atoi(u);
+        ug->gid = atoi(g); 
     }
-   
-    ug->uid = atoi(u);
-    ug->gid = atoi(g); 
-
     return NULL;
 }
 
-static const char *set_socket_name(cmd_parms *cmd, void *dummy, const char *arg)
+static const char *assign_childuid(cmd_parms *cmd, void *dummy, const char *uid,
+                                   const char *gid)
 {
-    server_rec *s = cmd->server;
-    perchild_server_conf *conf = (perchild_server_conf *) 
-                  ap_get_module_config(s->module_config, &mpm_perchild_module);
+    int i;
+    int u = atoi(uid);
+    int g = atoi(gid);
+    const char *errstr;
+    int socks[2];
+    perchild_server_conf *sconf = (perchild_server_conf *)
+                            ap_get_module_config(cmd->server->module_config, 
+                                                 &mpm_perchild_module);
 
-    conf->sockname = ap_server_root_relative(cmd->pool, arg);
-    return NULL;
-}
-    
-static ap_status_t cleanup_hash(void *dptr)
-{
-    socket_info_table = NULL;
-    return APR_SUCCESS;
-}
+    sconf->fullsockname = apr_pstrcat(cmd->pool, sconf->sockname, ".", uid, ":", gid, NULL);
 
-static const char *assign_childprocess(cmd_parms *cmd, void *dummy, const char *p)
-{
-    if (socket_info_table == NULL) {
-        socket_info_table = ap_make_hash(cmd->pool);
-        ap_register_cleanup(cmd->pool, socket_info_table, cleanup_hash, NULL);
+    if ((errstr = make_perchild_socket(sconf->fullsockname, socks))) {
+        return errstr;
+    }
+
+    sconf->sd = socks[0]; 
+    sconf->sd2 = socks[1];
+
+    for (i = 0; i < num_daemons; i++) {
+        if (u == child_info_table[i].uid && g == child_info_table[i].gid) {
+            child_info_table[i].sd = sconf->sd;
+        }
     }
-    ap_hash_set(socket_info_table, cmd->server->server_hostname, 0, p);
+
     return NULL;
 }
 
@@ -1640,23 +1743,19 @@ AP_INIT_FLAG("ConnectionStatus", set_maintain_connection_status, NULL, RSRC_CONF
              "Whether or not to maintain status information on current connections"),
 AP_INIT_TAKE1("CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF,
               "The location of the directory Apache changes to before dumping core"),
-AP_INIT_TAKE3("ChildProcess", set_childprocess, NULL, RSRC_CONF,
+AP_INIT_TAKE3("ChildperUserID", set_child_per_uid, NULL, RSRC_CONF,
               "Specify a User and Group for a specific child process."),
-AP_INIT_TAKE1("AssignChild", assign_childprocess, NULL, RSRC_CONF,
+AP_INIT_TAKE2("AssignUserID", assign_childuid, NULL, RSRC_CONF,
               "Tie a virtual host to a specific child process."),
-AP_INIT_TAKE1("ChildSockName", set_socket_name, NULL, RSRC_CONF,
-              "the base name of the socket to use for communication between "
-              "child processes.  The actual socket will be "
-              "basename.childnum"),
 { NULL }
 };
 
-static void *perchild_create_config(ap_pool_t *p, server_rec *s)
+static void *perchild_create_config(apr_pool_t *p, server_rec *s)
 {
     perchild_server_conf *c =
-    (perchild_server_conf *) ap_pcalloc(p, sizeof(perchild_server_conf));
+    (perchild_server_conf *) apr_pcalloc(p, sizeof(perchild_server_conf));
 
-    c->sockname = ap_server_root_relative(p, DEFAULT_PERCHILD_SOCKET);
+    c->sd = -1;
     return c;
 }
 
@@ -1667,7 +1766,7 @@ module MODULE_VAR_EXPORT mpm_perchild_module = {
     NULL,                      /* merge per-directory config structures */
     perchild_create_config,    /* create per-server config structure */
     NULL,                      /* merge per-server config structures */
-    perchild_cmds,             /* command ap_table_t */
+    perchild_cmds,             /* command apr_table_t */
     NULL,                      /* handlers */
     perchild_hooks             /* register_hooks */
 };