1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "apr_thread_proc.h"
23 * @package Multi-Processing Module library
27 The MPM, "multi-processing model" provides an abstraction of the
28 interface with the OS for distributing incoming connections to
29 threads/process for processing. http_main invokes the MPM, and
30 the MPM runs until a shutdown/restart has been indicated.
31 The MPM calls out to the apache core via the ap_process_connection
32 function when a connection arrives.
34 The MPM may or may not be multithreaded. In the event that it is
35 multithreaded, at any instant it guarantees a 1:1 mapping of threads
36 ap_process_connection invocations.
38 Note: In the future it will be possible for ap_process_connection
39 to return to the MPM prior to finishing the entire connection; and
40 the MPM will proceed with asynchronous handling for the connection;
41 in the future the MPM may call ap_process_connection again -- but
42 does not guarantee it will occur on the same thread as the first call.
44 The MPM further guarantees that no asynchronous behaviour such as
45 longjmps and signals will interfere with the user code that is
46 invoked through ap_process_connection. The MPM may reserve some
47 signals for its use (i.e. SIGUSR1), but guarantees that these signals
48 are ignored when executing outside the MPM code itself. (This
49 allows broken user code that does not handle EINTR to function
52 The suggested server restart and stop behaviour will be "graceful".
53 However the MPM may choose to terminate processes when the user
54 requests a non-graceful restart/stop. When this occurs, the MPM kills
55 all threads with extreme prejudice, and destroys the pchild pool.
56 User cleanups registered in the pchild apr_pool_t will be invoked at
57 this point. (This can pose some complications, the user cleanups
58 are asynchronous behaviour not unlike longjmp/signal... but if the
59 admin is asking for a non-graceful shutdown, how much effort should
60 we put into doing it in a nice way?)
63 - The MPM does not set a SIGALRM handler, user code may use SIGALRM.
64 But the preferred method of handling timeouts is to use the
65 timeouts provided by the BUFF abstraction.
66 - The proper setting for SIGPIPE is SIG_IGN, if user code changes it
67 for any of their own processing, it must be restored to SIG_IGN
68 prior to executing or returning to any apache code.
69 TODO: add SIGPIPE debugging check somewhere to make sure it's SIG_IGN
73 * This is the function that MPMs must create. This function is responsible
74 * for controlling the parent and child processes. It will run until a
75 * restart/shutdown is indicated.
76 * @param pconf the configuration pool, reset before the config file is read
77 * @param plog the log pool, reset after the config file is read
78 * @param server_conf the global server config.
79 * @return 1 for shutdown 0 otherwise.
80 * @deffunc int ap_mpm_run(apr_pool_t *pconf, apr_pool_t *plog, server_rec *server_conf)
82 AP_DECLARE(int) ap_mpm_run(apr_pool_t *pconf, apr_pool_t *plog, server_rec *server_conf);
85 * predicate indicating if a graceful stop has been requested ...
86 * used by the connection loop
87 * @return 1 if a graceful stop has been requested, 0 otherwise
88 * @deffunc int ap_graceful_stop_signalled(*void)
90 AP_DECLARE(int) ap_graceful_stop_signalled(void);
93 * Spawn a process with privileges that another module has requested
94 * @param r The request_rec of the current request
95 * @param newproc The resulting process handle.
96 * @param progname The program to run
97 * @param const_args the arguments to pass to the new program. The first
98 * one should be the program name.
99 * @param env The new environment apr_table_t for the new process. This
100 * should be a list of NULL-terminated strings.
101 * @param attr the procattr we should use to determine how to create the new
103 * @param p The pool to use.
105 AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
106 const request_rec *r,
108 const char *progname,
109 const char * const *args,
110 const char * const *env,
111 apr_procattr_t *attr,
114 /* Subtypes/Values for AP_MPMQ_IS_THREADED and AP_MPMQ_IS_FORKED */
115 #define AP_MPMQ_NOT_SUPPORTED 0 /* This value specifies whether */
116 /* an MPM is capable of */
117 /* threading or forking. */
118 #define AP_MPMQ_STATIC 1 /* This value specifies whether */
119 /* an MPM is using a static # */
120 /* threads or daemons. */
121 #define AP_MPMQ_DYNAMIC 2 /* This value specifies whether */
122 /* an MPM is using a dynamic # */
123 /* threads or daemons. */
125 /* Values returned for AP_MPMQ_MPM_STATE */
126 #define AP_MPMQ_STARTING 0
127 #define AP_MPMQ_RUNNING 1
128 #define AP_MPMQ_STOPPING 2
130 #define AP_MPMQ_MAX_DAEMON_USED 1 /* Max # of daemons used so far */
131 #define AP_MPMQ_IS_THREADED 2 /* MPM can do threading */
132 #define AP_MPMQ_IS_FORKED 3 /* MPM can do forking */
133 #define AP_MPMQ_HARD_LIMIT_DAEMONS 4 /* The compiled max # daemons */
134 #define AP_MPMQ_HARD_LIMIT_THREADS 5 /* The compiled max # threads */
135 #define AP_MPMQ_MAX_THREADS 6 /* # of threads/child by config */
136 #define AP_MPMQ_MIN_SPARE_DAEMONS 7 /* Min # of spare daemons */
137 #define AP_MPMQ_MIN_SPARE_THREADS 8 /* Min # of spare threads */
138 #define AP_MPMQ_MAX_SPARE_DAEMONS 9 /* Max # of spare daemons */
139 #define AP_MPMQ_MAX_SPARE_THREADS 10 /* Max # of spare threads */
140 #define AP_MPMQ_MAX_REQUESTS_DAEMON 11 /* Max # of requests per daemon */
141 #define AP_MPMQ_MAX_DAEMONS 12 /* Max # of daemons by config */
142 #define AP_MPMQ_MPM_STATE 13 /* starting, running, stopping */
145 * Query a property of the current MPM.
146 * @param query_code One of APM_MPMQ_*
147 * @param result A location to place the result of the query
148 * @return APR_SUCCESS or APR_ENOTIMPL
149 * @deffunc int ap_mpm_query(int query_code, int *result)
151 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result);
153 /* Defining GPROF when compiling uses the moncontrol() function to
154 * disable gprof profiling in the parent, and enable it only for
155 * request processing in children (or in one_process mode). It's
156 * absolutely required to get useful gprof results under linux
157 * because the profile itimers and such are disabled across a
158 * fork(). It's probably useful elsewhere as well.
161 extern void moncontrol(int);
162 #define AP_MONCONTROL(x) moncontrol(x)
164 #define AP_MONCONTROL(x)
167 #if AP_ENABLE_EXCEPTION_HOOK
168 typedef struct ap_exception_info_t {
171 } ap_exception_info_t;
173 AP_DECLARE_HOOK(int,fatal_exception,(ap_exception_info_t *ei))
174 #endif /*AP_ENABLE_EXCEPTION_HOOK*/