]> granicus.if.org Git - apache/blob - include/ap_mpm.h
Follow up to r1740928: update docs (contexts) and minor MMN.
[apache] / include / ap_mpm.h
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file  ap_mpm.h
19  * @brief Apache Multi-Processing Module library
20  *
21  * @defgroup APACHE_CORE_MPM Multi-Processing Module library
22  * @ingroup  APACHE_CORE
23  * @{
24  */
25
26 #ifndef AP_MPM_H
27 #define AP_MPM_H
28
29 #include "apr_thread_proc.h"
30 #include "httpd.h"
31 #include "scoreboard.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*
38     The MPM, "multi-processing model" provides an abstraction of the
39     interface with the OS for distributing incoming connections to
40     threads/process for processing.  http_main invokes the MPM, and
41     the MPM runs until a shutdown/restart has been indicated.
42     The MPM calls out to the apache core via the ap_process_connection
43     function when a connection arrives.
44
45     The MPM may or may not be multithreaded.  In the event that it is
46     multithreaded, at any instant it guarantees a 1:1 mapping of threads
47     ap_process_connection invocations.
48
49     Note: In the future it will be possible for ap_process_connection
50     to return to the MPM prior to finishing the entire connection; and
51     the MPM will proceed with asynchronous handling for the connection;
52     in the future the MPM may call ap_process_connection again -- but
53     does not guarantee it will occur on the same thread as the first call.
54
55     The MPM further guarantees that no asynchronous behaviour such as
56     longjmps and signals will interfere with the user code that is
57     invoked through ap_process_connection.  The MPM may reserve some
58     signals for its use (i.e. SIGUSR1), but guarantees that these signals
59     are ignored when executing outside the MPM code itself.  (This
60     allows broken user code that does not handle EINTR to function
61     properly.)
62
63     The suggested server restart and stop behaviour will be "graceful".
64     However the MPM may choose to terminate processes when the user
65     requests a non-graceful restart/stop.  When this occurs, the MPM kills
66     all threads with extreme prejudice, and destroys the pchild pool.
67     User cleanups registered in the pchild apr_pool_t will be invoked at
68     this point.  (This can pose some complications, the user cleanups
69     are asynchronous behaviour not unlike longjmp/signal... but if the
70     admin is asking for a non-graceful shutdown, how much effort should
71     we put into doing it in a nice way?)
72
73     unix/posix notes:
74     - The MPM does not set a SIGALRM handler, user code may use SIGALRM.
75         But the preferred method of handling timeouts is to use the
76         timeouts provided by the BUFF abstraction.
77     - The proper setting for SIGPIPE is SIG_IGN, if user code changes it
78         for any of their own processing, it must be restored to SIG_IGN
79         prior to executing or returning to any apache code.
80     TODO: add SIGPIPE debugging check somewhere to make sure it's SIG_IGN
81 */
82
83 /**
84  * Pass control to the MPM for steady-state processing.  It is responsible
85  * for controlling the parent and child processes.  It will run until a
86  * restart/shutdown is indicated.
87  * @param pconf the configuration pool, reset before the config file is read
88  * @param plog the log pool, reset after the config file is read
89  * @param server_conf the global server config.
90  * @return DONE for shutdown OK otherwise.
91  * @ingroup hooks
92  */
93 AP_DECLARE_HOOK(int, mpm, (apr_pool_t *pconf, apr_pool_t *plog, server_rec *server_conf))
94
95 /**
96  * Spawn a process with privileges that another module has requested
97  * @param r The request_rec of the current request
98  * @param newproc The resulting process handle.
99  * @param progname The program to run
100  * @param args the arguments to pass to the new program.  The first
101  *                   one should be the program name.
102  * @param env The new environment apr_table_t for the new process.  This
103  *            should be a list of NULL-terminated strings.
104  * @param attr the procattr we should use to determine how to create the new
105  *         process
106  * @param p The pool to use.
107  */
108 AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
109     const request_rec *r,
110     apr_proc_t *newproc,
111     const char *progname,
112     const char * const *args,
113     const char * const *env,
114     apr_procattr_t *attr,
115     apr_pool_t *p);
116
117 /** @defgroup mpmq MPM query
118  * @{
119  */
120
121 /** @defgroup thrdfrk Subtypes/Values returned for AP_MPMQ_IS_THREADED and AP_MPMQ_IS_FORKED
122  *  @ingroup mpmq
123  *  @{
124  */
125 #define AP_MPMQ_NOT_SUPPORTED      0  /**< This value specifies that an
126                                        * MPM is not capable of
127                                        * threading or forking.        */
128 #define AP_MPMQ_STATIC             1  /**< This value specifies that
129                                        * an MPM is using a static
130                                        * number of threads or daemons */
131 #define AP_MPMQ_DYNAMIC            2  /**< This value specifies that
132                                        * an MPM is using a dynamic
133                                        * number of threads or daemons */
134 /** @} */
135
136 /** @defgroup qstate Values returned for AP_MPMQ_MPM_STATE
137  *  @ingroup mpmq
138  *  @{
139  */
140 #define AP_MPMQ_STARTING              0
141 #define AP_MPMQ_RUNNING               1
142 #define AP_MPMQ_STOPPING              2
143 /** @} */
144
145 /** @defgroup qcodes Query codes for ap_mpm_query()
146  *  @ingroup mpmq
147  *  @{
148  */
149 /** Max # of daemons used so far */
150 #define AP_MPMQ_MAX_DAEMON_USED       1
151 /** MPM can do threading         */
152 #define AP_MPMQ_IS_THREADED           2
153 /** MPM can do forking           */
154 #define AP_MPMQ_IS_FORKED             3
155 /** The compiled max # daemons   */
156 #define AP_MPMQ_HARD_LIMIT_DAEMONS    4
157 /** The compiled max # threads   */
158 #define AP_MPMQ_HARD_LIMIT_THREADS    5
159 /** \# of threads/child by config */
160 #define AP_MPMQ_MAX_THREADS           6
161 /** Min # of spare daemons       */
162 #define AP_MPMQ_MIN_SPARE_DAEMONS     7
163 /** Min # of spare threads       */
164 #define AP_MPMQ_MIN_SPARE_THREADS     8
165 /** Max # of spare daemons       */
166 #define AP_MPMQ_MAX_SPARE_DAEMONS     9
167 /** Max # of spare threads       */
168 #define AP_MPMQ_MAX_SPARE_THREADS    10
169 /** Max # of requests per daemon */
170 #define AP_MPMQ_MAX_REQUESTS_DAEMON  11
171 /** Max # of daemons by config   */
172 #define AP_MPMQ_MAX_DAEMONS          12
173 /** starting, running, stopping  */
174 #define AP_MPMQ_MPM_STATE            13
175 /** MPM can process async connections  */
176 #define AP_MPMQ_IS_ASYNC             14
177 /** MPM generation */
178 #define AP_MPMQ_GENERATION           15
179 /** MPM can drive serf internally  */
180 #define AP_MPMQ_HAS_SERF             16
181 /** MPM supports suspending/resuming connections */
182 #define AP_MPMQ_CAN_SUSPEND          17
183 /** MPM supports additional pollfds */
184 #define AP_MPMQ_CAN_POLL             18
185 /** @} */
186
187 /**
188  * Query a property of the current MPM.
189  * @param query_code One of AP_MPMQ_*
190  * @param result A location to place the result of the query
191  * @return APR_EGENERAL if an mpm-query hook has not been registered;
192  * APR_SUCCESS or APR_ENOTIMPL otherwise
193  * @remark The MPM doesn't register the implementing hook until the
194  * register_hooks hook is called, so modules cannot use ap_mpm_query()
195  * until after that point.
196  * @fn int ap_mpm_query(int query_code, int *result)
197  */
198 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result);
199
200 /** @} */
201
202 typedef void (ap_mpm_callback_fn_t)(void *baton);
203
204 /* only added support in the Event MPM....  check for APR_ENOTIMPL */
205 AP_DECLARE(apr_status_t) ap_mpm_resume_suspended(conn_rec *c);
206 /* only added support in the Event MPM....  check for APR_ENOTIMPL */
207 AP_DECLARE(apr_status_t) ap_mpm_register_timed_callback(
208         apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton);
209
210 /**
211  * Register a callback on the readability or writability on a group of
212  * sockets/pipes.
213  * @param pfds Array of apr_pollfd_t
214  * @param p pool for use between registration and callback
215  * @param cbfn The callback function
216  * @param baton userdata for the callback function
217  * @return APR_SUCCESS if all sockets/pipes could be added to a pollset,
218  * APR_ENOTIMPL if no asynch support, or an apr_pollset_add error.
219  * @remark When activity is found on any 1 socket/pipe in the list, all are removed
220  * from the pollset and only 1 callback is issued.
221  */
222
223 AP_DECLARE(apr_status_t) ap_mpm_register_poll_callback(apr_array_header_t *pds,
224         ap_mpm_callback_fn_t *cbfn, void *baton);
225
226 /**
227  * Register a callback on the readability or writability on a group of sockets/pipes,
228  * with a timeout.
229  * @param pfds Array of apr_pollfd_t
230  * @param p pool for use between registration and callback
231  * @param cbfn The callback function
232  * @param tofn The callback function if the timeout expires
233  * @param baton userdata for the callback function
234  * @param timeout timeout for I/O in microseconds, unlimited if <= 0
235  * @return APR_SUCCESS if all sockets/pipes could be added to a pollset,
236  * APR_ENOTIMPL if no asynch support, or an apr_pollset_add error.
237  * @remark When activity is found on any 1 socket/pipe in the list, all are removed
238  * from the pollset and only 1 callback is issued. 
239  * @remark For each call, only one of tofn or cbfn will be called, never both.
240  */
241
242 AP_DECLARE(apr_status_t) ap_mpm_register_poll_callback_timeout(
243         apr_array_header_t *pfds, ap_mpm_callback_fn_t *cbfn,
244         ap_mpm_callback_fn_t *tofn, void *baton, apr_time_t timeout);
245
246
247 /**
248 * Unregister a previously registered callback.
249 * @param pfds Array of apr_pollfd_t
250 * @param p pool for use between registration and callback
251 * @return APR_SUCCESS if all sockets/pipes could be removed from the pollset,
252 * APR_ENOTIMPL if no asynch support, or an apr_pollset_remove error.
253 * @remark This function triggers the cleanup registered on the pool p during
254 * callback registration.
255 */
256 AP_DECLARE(apr_status_t) ap_mpm_unregister_poll_callback(apr_array_header_t *pfds);
257
258 typedef enum mpm_child_status {
259     MPM_CHILD_STARTED,
260     MPM_CHILD_EXITED,
261     MPM_CHILD_LOST_SLOT
262 } mpm_child_status;
263
264 /**
265  * Allow a module to remain aware of MPM child process state changes,
266  * along with the generation and scoreboard slot of the process changing
267  * state.
268  *
269  * With some MPMs (event and worker), an active MPM child process may lose
270  * its scoreboard slot if the child process is exiting and the scoreboard
271  * slot is needed by other processes.  When this occurs, the hook will be
272  * called with the MPM_CHILD_LOST_SLOT state.
273  *
274  * @param s The main server_rec.
275  * @param pid The id of the MPM child process.
276  * @param gen The server generation of that child process.
277  * @param slot The scoreboard slot number, or -1.  It will be -1 when an
278  * MPM child process exits, and that child had previously lost its
279  * scoreboard slot.
280  * @param state One of the mpm_child_status values.  Modules should ignore
281  * unrecognized values.
282  * @ingroup hooks
283  */
284 AP_DECLARE_HOOK(void,child_status,(server_rec *s, pid_t pid, ap_generation_t gen,
285                                    int slot, mpm_child_status state))
286
287 /**
288  * Allow a module to be notified when the last child process of a generation
289  * exits.
290  *
291  * @param s The main server_rec.
292  * @param gen The server generation which is now completely finished.
293  * @ingroup hooks
294  */
295 AP_DECLARE_HOOK(void,end_generation,(server_rec *s, ap_generation_t gen))
296
297 /* Defining GPROF when compiling uses the moncontrol() function to
298  * disable gprof profiling in the parent, and enable it only for
299  * request processing in children (or in one_process mode).  It's
300  * absolutely required to get useful gprof results under linux
301  * because the profile itimers and such are disabled across a
302  * fork().  It's probably useful elsewhere as well.
303  */
304 #ifdef GPROF
305 extern void moncontrol(int);
306 #define AP_MONCONTROL(x) moncontrol(x)
307 #else
308 #define AP_MONCONTROL(x)
309 #endif
310
311 #ifdef AP_ENABLE_EXCEPTION_HOOK
312 typedef struct ap_exception_info_t {
313     int sig;
314     pid_t pid;
315 } ap_exception_info_t;
316
317 /**
318  * Run the fatal_exception hook for each module; this hook is run
319  * from some MPMs in the event of a child process crash, if the
320  * server was built with --enable-exception-hook and the
321  * EnableExceptionHook directive is On.
322  * @param ei information about the exception
323  * @ingroup hooks
324  */
325 AP_DECLARE_HOOK(int,fatal_exception,(ap_exception_info_t *ei))
326 #endif /*AP_ENABLE_EXCEPTION_HOOK*/
327
328 #ifdef __cplusplus
329 }
330 #endif
331
332 #endif
333 /** @} */