#include "http_log.h"
#include "http_config.h" /* for read_config */
#include "http_core.h" /* for get_remote_host */
+#include "http_protocol.h"
#include "http_connection.h"
#include "ap_mpm.h"
#include "unixd.h"
#include "mpm_common.h"
-#include "iol_socket.h"
+#include "ap_iol.h"
#include "ap_listen.h"
#include "mpm_default.h"
#include "mpm.h"
#include <grp.h>
#include <pwd.h>
#include <pthread.h>
+#include <sys/stat.h>
+#include <sys/un.h>
#include <signal.h>
-
-typedef struct child_info_t child_info_t;
-
-struct child_info_t {
- uid_t uid;
- gid_t gid;
-};
-static int curr_child_num = 0; /* how many children have been setup so
- far */
+#include <setjmp.h>
+#include <stropts.h>
/*
* Actual definitions of config globals
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;
+ int sd;
+};
+
+typedef struct {
+ 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;
+
+/* 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 int thread_socket_table[HARD_THREAD_LIMIT];
-static ap_hash_t *child_hash;
-struct ap_ctable ap_child_table[HARD_SERVER_LIMIT];
+struct ap_ctable ap_child_table[HARD_SERVER_LIMIT];
/*
* The max child slot ever assigned, preserved across restarts. Necessary
char ap_coredump_dir[MAX_STRING_LEN];
-static ap_file_t *pipe_of_death_in = NULL;
-static ap_file_t *pipe_of_death_out = NULL;
+module MODULE_VAR_EXPORT mpm_perchild_module;
+
+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... */
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;
#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;
static void clean_child_exit(int code)
{
if (pchild) {
- ap_destroy_pool(pchild);
+ apr_destroy_pool(pchild);
}
exit(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
}
/* do a graceful restart if graceful == 1 */
-void ap_start_restart(int graceful)
+static void ap_start_restart(int graceful)
{
if (restart_pending == 1) {
}
restart_pending = 1;
is_graceful = graceful;
+ if (is_graceful) {
+ apr_kill_cleanup(pconf, NULL, ap_cleanup_shared_mem);
+ }
}
static void sig_term(int sig)
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
* 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) {
"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);
static int start_thread(void)
{
pthread_t thread;
+ int rc;
pthread_mutex_lock(&worker_thread_count_mutex);
if (worker_thread_count < max_threads) {
- if (pthread_create(&thread, &worker_thread_attr, worker_thread,
- &worker_thread_free_ids[worker_thread_count])) {
- ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
+ if ((rc = pthread_create(&thread, &worker_thread_attr, worker_thread,
+ &worker_thread_free_ids[worker_thread_count]))) {
+#ifdef PTHREAD_SETS_ERRNO
+ rc = errno;
+#endif
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rc, ap_server_conf,
"pthread_create: unable to create worker thread");
/* In case system resources are maxxed out, we don't want
Apache running away with the CPU trying to fork over and
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. */
}
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);
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;
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];
}
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;
}
}
}
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;
}
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--;
static int perchild_setup_child(int childnum)
{
- child_info_t *ug;
- char child_num_str[5];
+ child_info_t *ug = &child_info_table[childnum];
- ap_snprintf(child_num_str, 5, "%d", childnum);
- ug = ap_hash_get(child_hash, child_num_str, 1);
- if (!ug) {
+ if (ug->uid == -1 && ug->gid == -1) {
return unixd_setup_child();
}
if (set_group_privs(ug->uid, ug->gid)) {
int signal_received;
int i;
ap_listen_rec *lr;
- ap_status_t rv;
+ 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,
ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, "sigprocmask");
}
#else
- if (pthread_sigmask(SIG_SETMASK, &sig_mask, NULL) != 0) {
- ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
+ if ((rv = pthread_sigmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
+#ifdef PTHREAD_SETS_ERRNO
+ rv = errno;
+#endif
+ ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
"pthread_sigmask");
}
#endif
requests_this_child = max_requests_per_child;
- /* Set up the pollfd array */
- listenfds = ap_pcalloc(pchild, sizeof(*listenfds) * (num_listenfds + 1));
+ /* Set up the pollfd array, num_listenfds + 1 for the pipe and 1 for
+ * the child socket.
+ */
+ 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 */
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);
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);
int i;
for (i = 0; number_to_start && i < num_daemons; ++i) {
- if (ap_child_table[i].status != SERVER_DEAD) {
+ if (ap_child_table[i].pid) {
continue;
}
if (make_child(ap_server_conf, i, 0) < 0) {
free_length = 0;
for (i = 0; i < num_daemons; ++i) {
- if (ap_child_table[i].status == SERVER_DEAD) {
+ if (ap_child_table[i].pid == 0) {
if (free_length < spawn_rate) {
free_slots[free_length] = i;
++free_length;
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) {
}
}
if (child_slot >= 0) {
- ap_child_table[child_slot].status = SERVER_DEAD;
+ ap_child_table[child_slot].pid = 0;
if (remaining_children_to_start
&& child_slot < num_daemons) {
}
#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
}
}
}
-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;
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,
/* Initialize the child table */
if (!is_graceful) {
for (i = 0; i < HARD_SERVER_LIMIT; i++) {
- ap_child_table[i].status = SERVER_DEAD;
+ ap_child_table[i].pid = 0;
}
}
}
/* we've been told to restart */
- ap_signal(SIGHUP, SIG_IGN);
+ apr_signal(SIGHUP, SIG_IGN);
if (one_process) {
/* not worth thinking about */
*/
for (i = 0; i < num_daemons; ++i) {
- if (ap_child_table[i].status != SERVER_DEAD) {
+ if (ap_child_table[i].pid) {
ap_child_table[i].status = SERVER_DYING;
}
}
/* 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");
}
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();
lock_fname = DEFAULT_LOCKFILE;
max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
ap_perchild_set_maintain_connection_status(1);
- child_hash = ap_make_hash(p);
+ curr_child_num = 0;
+
+ apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
- ap_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 pass_request(request_rec *r)
+{
+ 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;
+
+ if (sendmsg(sconf->sd2, &msg, 0) == -1) {
+ apr_destroy_pool(r->pool);
+ return -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];
+ }
+ }
+
+ 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;
+ }
+ return OK;
}
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
+ * just going to be forwarded along.
+ */
+ ap_hook_post_read_request(perchild_post_read, NULL, NULL, AP_HOOK_REALLY_FIRST);
}
static const char *set_pidfile(cmd_parms *cmd, void *dummy, const char *arg)
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) {
}
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 ap_status_t reset_child_process(void *data)
-{
- curr_child_num = 0;
- return APR_SUCCESS;
-}
-
-static const char *set_childprocess(cmd_parms *cmd, void *dummy, 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)
{
- child_info_t *ug = ap_palloc(cmd->pool, sizeof(*ug));
- char child_num_str[5];
+ 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);
- ap_snprintf(child_num_str, 5, "%d", curr_child_num++);
- ap_hash_set(child_hash, ap_pstrdup(cmd->pool, child_num_str), 1, ug);
-
- ap_register_cleanup(cmd->pool, &curr_child_num, reset_child_process, ap_null_cleanup);
-
return NULL;
}
-
-
-#if 0
- if (unlink(sconf->sockname) < 0 &&
- errno != ENOENT) {
- ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
- "Couldn't unlink unix domain socket %s",
- sconf->sockname);
- /* JUSt a warning; don't bail out */
- }
- if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
- ap_log_error(APLOG_MARK, APLOG_ERR, errno, main_server,
- "Couldn't create unix domain socket");
- return errno;
- }
+static const char *assign_childuid(cmd_parms *cmd, void *dummy, const char *uid,
+ const char *gid)
+{
+ 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);
- memset(&unix_addr, 0, sizeof(unix_addr));
- unix_addr.sun_family = AF_UNIX;
- strcpy(unix_addr.sun_path, sconf->sockname);
+ sconf->fullsockname = apr_pstrcat(cmd->pool, sconf->sockname, ".", uid, ":", gid, NULL);
- omask = umask(0077); /* so that only Apache can use socket */
- rc = bind(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
-}
-#endif
+ 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;
+ }
+ }
+ return NULL;
+}
static const command_rec perchild_cmds[] = {
"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_TAKE2("ChildProcess", set_childprocess, NULL, RSRC_CONF,
- "The User and Group this child Process should run as."),
+AP_INIT_TAKE3("ChildperUserID", set_child_per_uid, NULL, RSRC_CONF,
+ "Specify a User and Group for a specific child process."),
+AP_INIT_TAKE2("AssignUserID", assign_childuid, NULL, RSRC_CONF,
+ "Tie a virtual host to a specific child process."),
{ NULL }
};
+static void *perchild_create_config(apr_pool_t *p, server_rec *s)
+{
+ perchild_server_conf *c =
+ (perchild_server_conf *) apr_pcalloc(p, sizeof(perchild_server_conf));
+
+ c->sd = -1;
+ return c;
+}
+
module MODULE_VAR_EXPORT mpm_perchild_module = {
MPM20_MODULE_STUFF,
NULL, /* hook to run before apache parses args */
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
- NULL, /* create per-server config structure */
+ 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 */
};