prefork.c revision 00b8146bf5d1f543ce19917c6b4fe252d33baf79
2N/A/* ==================================================================== 2N/A * The Apache Software License, Version 1.1 2N/A * Copyright (c) 2000-2001 The Apache Software Foundation. All rights 2N/A * Redistribution and use in source and binary forms, with or without 2N/A * modification, are permitted provided that the following conditions 2N/A * 1. Redistributions of source code must retain the above copyright 2N/A * notice, this list of conditions and the following disclaimer. 2N/A * 2. Redistributions in binary form must reproduce the above copyright 2N/A * notice, this list of conditions and the following disclaimer in 2N/A * the documentation and/or other materials provided with the 2N/A * 3. The end-user documentation included with the redistribution, 2N/A * if any, must include the following acknowledgment: 2790N/A * "This product includes software developed by the 3739N/A * Alternately, this acknowledgment may appear in the software itself, 2N/A * if and wherever such third-party acknowledgments normally appear. 2N/A * 4. The names "Apache" and "Apache Software Foundation" must 2N/A * not be used to endorse or promote products derived from this 2N/A * software without prior written permission. For written 2N/A * permission, please contact apache@apache.org. 2N/A * 5. Products derived from this software may not be called "Apache", 2N/A * nor may "Apache" appear in their name, without prior written 2N/A * permission of the Apache Software Foundation. 59N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 2N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 2N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 1470N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 38N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1470N/A * ==================================================================== 181N/A * This software consists of voluntary contributions made by many 26N/A * individuals on behalf of the Apache Software Foundation. For more 3739N/A * information on the Apache Software Foundation, please see 3739N/A * Portions of this software are based upon public domain software 3739N/A * originally written at the National Center for Supercomputing Applications, 3739N/A * University of Illinois, Urbana-Champaign. 26N/A * 03-21-93 Rob McCool wrote original code (up to NCSA HTTPd 1.3) 26N/A * changed server number for child-alone processes to 0 and changed name 26N/A * Added numerous speed hacks proposed by Robert S. Thau (rst@ai.mit.edu) 26N/A * including set group before fork, and call gettime before to fork 26N/A * to set up libraries. 26N/A * Brandon's code snarfed from NCSA 1.4, but tinkered to work with the 1498N/A * Apache server, and also to have child processes do accept() directly. 26N/A * Extensive rework for Apache. 26N/A/* TODO: this is a cobbled together prefork MPM example... it should mostly 26N/A * TODO: behave like apache-1.3... here's a short list of things I think 3998N/A * TODO: need cleaning up still: 1256N/A * The max child slot ever assigned, preserved across restarts. Necessary 1256N/A * to deal with MaxClients changes across SIGWINCH restarts. We use this 1256N/A * value to optimize routines that have to scan the entire scoreboard. 1256N/A/* *Non*-shared http_main globals... */ 1256N/A/* one_process --- debugging mode variable; can be set from the command line 1256N/A * with the -X flag. If set, this gets you the child_main loop running 1256N/A * in the process which originally started up (no detach, no make_child), 3109N/A * which is a pretty nice debugging environment. (You'll get a SIGHUP 3109N/A * early in standalone_main; just continue through. This is the server 3109N/A * trying to kill off any child processes which it might have lying 3109N/A * around --- Apache doesn't keep track of their pids, it just sends 3109N/A * SIGHUP to the process group, ignoring it in the root process. 3109N/A * Continue through and you'll be fine.). 1256N/A * change directory for gprof to plop the gmon.out file 26N/A "gprof: error creating directory %s",
dir);
1256N/A/* XXX - I don't know if TPF will ever use this module or not, so leave 1256N/A * the ap_check_signals calls in but disable them - manoj */ 2818N/A/* a clean exit from a child with proper cleanup */ 1256N/A /* XXXX possibly bogus cast */ 1256N/A * Done by each child at its birth 1934N/A "couldn't do child init for accept mutex");
1413N/A * Must be safe to call this on a restart. 3109N/A/* On some architectures it's safe to do unserialized accept()s in the single 3109N/A * Listen case. But it's never safe to do it in the case where there's 3109N/A * multiple Listen statements. Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT 3109N/A * when it's safe in the single Listen case. 181N/A Systems without a real waitpid sometimes lose a child's exit while waiting 181N/A for another. Search through the scoreboard for missing children. 3817N/A /* just mark it as having a successful exit status */ 3817N/A/* handle all varieties of core dumping signals */ 3817N/A "seg fault or similar nasty error detected " 3817N/A /* At this point we've got sig blocked, because we're still inside 3817N/A * the signal handler. When we leave the signal handler it will 3817N/A * be unblocked, and we'll take the signal... and coredump or whatever 3817N/A * is appropriate for this particular Unix. In addition the parent 3817N/A * will see the real signal we received -- whereas if we called 3817N/A * abort() here, the parent would only see SIGABRT. 145N/A/***************************************************************** 145N/A * Connection structures and accounting... 46N/A /* Um, is this _probably_ not an error, if the user has 1899N/A * tried to do a shutdown twice quickly, so we won't 1899N/A * worry about reporting it. 2N/A/* restart() is the signal handler for SIGHUP and SIGWINCH 145N/A * in the parent process, unless running in ONE_PROCESS mode 2N/A /* Probably not an error - don't bother reporting it */ /* we want to ignore HUPs and WINCH while we're busy processing one */ /***************************************************************** * Child process main loop. * The following vars are static to avoid getting clobbered by longjmp(); * they are really private to child_main. /* not ever called anymore... */ /* Get a sub context for global allocations in this child, so that * we can have cleanups occur when the child exits. /* needs to be done before we switch UIDs so we have permissions */ * (Re)initialize this child to a pre-connection state. * Wait for an acceptable connection to arrive. /* Lock around "accept", if necessary */ /* more than one socket */ /* Single Unix documents select as returning errnos * EBADF, EINTR, and EINVAL... and in none of those * cases does it make sense to continue. In fact * on Linux 2.0.x we seem to end up with EFAULT * occasionally, and we'd loop forever due to it. /* we remember the last_lr we searched last time around so that we don't end up starving any particular listening socket */ /* FIXME: if we get here, something bad has happened, and we're probably gonna spin forever. /* only one socket, just pretend we did the other stuff */ /* if we accept() something we don't want to die, so we have to break;
/* We have a socket ready for reading */ /* TODO: this accept result handling stuff should be abstracted... * it's already out of date between the various unix mpms /* Our old behaviour here was to continue after accept() * errors. But this leads us into lots of troubles * because most of the errors are quite fatal. For * example, EMFILE can be caused by slow descriptor * leaks (say in a 3rd party module, or libc). It's * foolish for us to continue after an EMFILE. We also * seem to tickle kernel bugs on some platforms which * lead to never-ending loops here. So it seems best * to just exit in most cases. /* EPROTO on certain older kernels really means * ECONNABORTED, so we need to ignore it for them. * See discussion in new-httpd archives nh.9701 * Also see nh.9603, search for EPROTO: * There is potentially a bug in Solaris 2.x x<6, * and other boxes that implement tcp sockets in * userland (i.e. on top of STREAMS). On these * systems, EPROTO can actually result in a fatal * loop. See PR#981 for example. It's hard to * handle both uses of EPROTO. /* Linux generates the rest of these, other tcp * stacks (i.e. bsd) tend to hide them behind * getsockopt() interfaces. They occur when * the net goes sour or the client disconnects * after the three-way handshake has been done * in the kernel but before userland has picked * When the network layer has been shut down, there * is not much use in simply exiting: the parent * would simply re-create us (and we'd fail again). * Use the CHILDFATAL code to tear the server down. * @@@ Martin's idea for possible improvement: * A different approach would be to define * a new APEXIT_NETDOWN exit code, the reception * of which would make the parent shutdown all * children, then idle-loop until it detected that * the network is up again, and restart the children. * Ben Hyde noted that temporary ENETDOWN situations "apr_accept: giving up.");
"offload device inactive");
"apr_accept: (client socket)");
* We now have a connection, so set it up with the appropriate * socket options, file descriptors, and read/write buffers. "new file descriptor %d is too large; you probably need " "to rebuild Apache with a larger FD_SETSIZE " if (
sockdes == 0) {
/* 0 is invalid socket for TPF */ /* Check the pod after processing a connection so that we'll go away * if a graceful restart occurred while we were processing the * connection. Otherwise, we won't wake up until a real connection * comes in and we'll use the wrong config to process it and we may * block in the wrong syscall (because the new generation is using a * different accept mutex) and in general it is goofy. /* Don't catch SIGWINCH in ONE_PROCESS mode :) */ /* BS2000 requires a "special" version of fork() before a setuid() call */ /* fork didn't succeed. Fix the scoreboard or else * it will say SERVER_STARTING forever and ever /* In case system resources are maxxed out, we don't want Apache running away with the CPU trying to fork over and /* by default AIX binds to a single processor * this bit unbinds children which will then bind to another cpu /* Disable the parent's signal handlers and set up proper handling in /* The child process doesn't do anything for SIGWINCH. Instead, the * pod is used for signalling graceful restart. /* start up a bunch of children */ * idle_spawn_rate is the number of children that will be spawned on the * next maintenance cycle if there aren't enough idle servers. It is * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by * without the need to spawn. /* initialize the free_list */ /* try to keep children numbers as low as possible */ /* We consider a starting server as idle because we started it * at least a cycle ago, and if it still hasn't finished starting * then we're just going to swamp things worse by forking more. * So we hopefully won't need to fork more if we count it. * This depends on the ordering of SERVER_READY and SERVER_STARTING. /* always kill the highest numbered child if we have to... * no really well thought out reason ... other than observing * the server behaviour under linux where lower numbered children * tend to service more hits (and hence are more likely to have * their data in cpu caches). /* kill off one child... we use the pod because that'll cause it to * shut down gracefully, in case it happened to pick up a request /* terminate the free list */ /* only report this condition once */ "server reached MaxClients setting, consider" " raising the MaxClients setting");
"server seems busy, (you may need " "spawning %d children, there are %d idle, and " "No active child processes: shutting down");
/* the next time around we want to spawn twice as many if this * wasn't good enough, but not if we've just done a graceful "no listening sockets available, shutting down");
/***************************************************************** /* XXX: hey, what's the right way for the mpm to indicate a fatal error? */ "Could not open pipe-of-death.");
/* If we're doing a graceful_restart then we're going to see a lot * of children exiting immediately when we get into the main loop * below (because we just sent them SIGWINCH). This happens pretty * rapidly... and for each one that exits we'll start a new one until * we reach at least daemons_min_free. But we may be permitted to * start more than that, so we'll just keep track of how many we're * supposed to start up without the 1 second penalty between each fork. /* give the system some time to recover before kicking into "%s configured -- resuming normal operations",
/* this is a memory leak, but I'll fix it later. */ /* XXX: if it takes longer than 1 second for all our children * to start up and get into IDLE state then we may spawn an /* non-fatal death... note that it's gone in the scoreboard. */ /* we're still doing a 1-for-1 replacement of dead * children with new children /* Great, we've probably just lost a slot in the * scoreboard. Somehow we don't know about this "long lost child came home! (pid %ld)", (
long)
pid.
pid);
/* Don't perform idle maintenance when a child dies, * only do it when there's a timeout. Remember only a * finite number of children can die, and it's pretty * pathological for a lot to die suddenly. /* we hit a 1 second timeout in which none of the previous * generation of children needed to be reaped... so assume * they're all done, and pick up the slack if any is left. /* In any event we really shouldn't do the code below because * few of the servers we just started are in the IDLE state * yet, so we'd mistakenly create an extra server. /* Time to gracefully shut down: * Kill child processes, tell them to call child_exit, etc... /* cleanup pid file on normal shutdown */ "removed PID file %s (pid=%ld)",
"caught SIGTERM, shutting down");
/* we've been told to restart */ /* not worth thinking about */ /* advance to the next generation */ /* XXX: we really need to make sure this new generation number isn't in * use by any of the children. "Graceful restart requested, doing restart");
/* kill off the idle ones */ /* This is mostly for debugging... so that we know what is still * gracefully dealing with existing request. But we can't really * do it if we're in a SCOREBOARD_FILE because it'll cause "SIGHUP received. Attempting to restart");
/* sigh, want this only the second time around */ "WARNING: detected MinSpareServers set to non-positive.");
"Resetting to 1 to avoid almost certain Apache failure.");
"Please read the documentation.");
"WARNING: MaxClients of %d exceeds compile time limit " " lowering MaxClients to %d. To increase, please " " HARD_SERVER_LIMIT define in %s.",
"WARNING: Require MaxClients > 0, setting to 1");
"Number of child processes launched at server startup"),
"Minimum number of idle children, to handle request spikes"),
"Maximum number of idle children"),
"Maximum number of children alive at the same time"),
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 */ NULL,
/* merge per-server config structures */