]> granicus.if.org Git - apache/blob - include/mpm_common.h
b366a08b12befbbd4a12d75115be1d62180dce03
[apache] / include / mpm_common.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 /* The purpose of this file is to store the code that MOST mpm's will need
18  * this does not mean a function only goes into this file if every MPM needs
19  * it.  It means that if a function is needed by more than one MPM, and
20  * future maintenance would be served by making the code common, then the
21  * function belongs here.
22  *
23  * This is going in src/main because it is not platform specific, it is
24  * specific to multi-process servers, but NOT to Unix.  Which is why it
25  * does not belong in src/os/unix
26  */
27
28 /**
29  * @file  mpm_common.h
30  * @brief Multi-Processing Modules functions
31  *
32  * @defgroup APACHE_MPM Multi-Processing Modules
33  * @ingroup  APACHE
34  * @{
35  */
36
37 #ifndef APACHE_MPM_COMMON_H
38 #define APACHE_MPM_COMMON_H
39
40 #include "ap_config.h"
41 #include "ap_mpm.h"
42 #include "scoreboard.h"
43
44 #if APR_HAVE_NETINET_TCP_H
45 #include <netinet/tcp.h>    /* for TCP_NODELAY */
46 #endif
47
48 #include "apr_proc_mutex.h"
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 /* The maximum length of the queue of pending connections, as defined
55  * by listen(2).  Under some systems, it should be increased if you
56  * are experiencing a heavy TCP SYN flood attack.
57  *
58  * It defaults to 511 instead of 512 because some systems store it
59  * as an 8-bit datatype; 512 truncated to 8-bits is 0, while 511 is
60  * 255 when truncated.
61  */
62 #ifndef DEFAULT_LISTENBACKLOG
63 #define DEFAULT_LISTENBACKLOG 511
64 #endif
65
66 /* Signal used to gracefully restart */
67 #define AP_SIG_GRACEFUL SIGUSR1
68
69 /* Signal used to gracefully restart (without SIG prefix) */
70 #define AP_SIG_GRACEFUL_SHORT USR1
71
72 /* Signal used to gracefully restart (as a quoted string) */
73 #define AP_SIG_GRACEFUL_STRING "SIGUSR1"
74
75 /* Signal used to gracefully stop */
76 #define AP_SIG_GRACEFUL_STOP SIGWINCH
77
78 /* Signal used to gracefully stop (without SIG prefix) */
79 #define AP_SIG_GRACEFUL_STOP_SHORT WINCH
80
81 /* Signal used to gracefully stop (as a quoted string) */
82 #define AP_SIG_GRACEFUL_STOP_STRING "SIGWINCH"
83
84 /**
85  * Callback function used for ap_reclaim_child_processes() and
86  * ap_relieve_child_processes().  The callback function will be
87  * called for each terminated child process.
88  */
89 typedef void ap_reclaim_callback_fn_t(int childnum, pid_t pid,
90                                       ap_generation_t gen);
91
92 #if (!defined(WIN32) && !defined(NETWARE)) || defined(DOXYGEN)
93 /**
94  * Make sure all child processes that have been spawned by the parent process
95  * have died.  This includes process registered as "other_children".
96  *
97  * @param terminate Either 1 or 0.  If 1, send the child processes SIGTERM
98  *        each time through the loop.  If 0, give the process time to die
99  *        on its own before signalling it.
100  * @param mpm_callback Callback invoked for each dead child process
101  *
102  * @note The MPM child processes which are reclaimed are those listed
103  * in the scoreboard as well as those currently registered via
104  * ap_register_extra_mpm_process().
105  */
106 AP_DECLARE(void) ap_reclaim_child_processes(int terminate,
107                                             ap_reclaim_callback_fn_t *mpm_callback);
108
109 /**
110  * Catch any child processes that have been spawned by the parent process
111  * which have exited. This includes processes registered as "other_children".
112  *
113  * @param mpm_callback Callback invoked for each dead child process
114
115  * @note The MPM child processes which are relieved are those listed
116  * in the scoreboard as well as those currently registered via
117  * ap_register_extra_mpm_process().
118  */
119 AP_DECLARE(void) ap_relieve_child_processes(ap_reclaim_callback_fn_t *mpm_callback);
120
121 /**
122  * Tell ap_reclaim_child_processes() and ap_relieve_child_processes() about
123  * an MPM child process which has no entry in the scoreboard.
124  * @param pid The process id of an MPM child process which should be
125  * reclaimed when ap_reclaim_child_processes() is called.
126  * @param gen The generation of this MPM child process.
127  *
128  * @note If an extra MPM child process terminates prior to calling
129  * ap_reclaim_child_processes(), remove it from the list of such processes
130  * by calling ap_unregister_extra_mpm_process().
131  */
132 AP_DECLARE(void) ap_register_extra_mpm_process(pid_t pid, ap_generation_t gen);
133
134 /**
135  * Unregister an MPM child process which was previously registered by a
136  * call to ap_register_extra_mpm_process().
137  * @param pid The process id of an MPM child process which no longer needs to
138  * be reclaimed.
139  * @param old_gen Set to the server generation of the process, if found.
140  * @return 1 if the process was found and removed, 0 otherwise
141  */
142 AP_DECLARE(int) ap_unregister_extra_mpm_process(pid_t pid, ap_generation_t *old_gen);
143
144 /**
145  * Safely signal an MPM child process, if the process is in the
146  * current process group.  Otherwise fail.
147  * @param pid the process id of a child process to signal
148  * @param sig the signal number to send
149  * @return APR_SUCCESS if signal is sent, otherwise an error as per kill(3);
150  * APR_EINVAL is returned if passed either an invalid (< 1) pid, or if
151  * the pid is not in the current process group
152  */
153 AP_DECLARE(apr_status_t) ap_mpm_safe_kill(pid_t pid, int sig);
154
155 /**
156  * Log why a child died to the error log, if the child died without the
157  * parent signalling it.
158  * @param pid The child that has died
159  * @param why The return code of the child process
160  * @param status The status returned from ap_wait_or_timeout
161  * @return 0 on success, APEXIT_CHILDFATAL if MPM should terminate
162  */
163 AP_DECLARE(int) ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status);
164
165 AP_DECLARE(apr_status_t) ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf);
166 AP_DECLARE(apr_status_t) ap_fatal_signal_child_setup(server_rec *s);
167
168 #endif /* (!WIN32 && !NETWARE) || DOXYGEN */
169
170 /**
171  * Pool cleanup for end-generation hook implementation
172  * (core httpd function)
173  */
174 apr_status_t ap_mpm_end_gen_helper(void *unused);
175
176 /**
177  * Run the monitor hook (once every ten calls), determine if any child
178  * process has died and, if none died, sleep one second.
179  * @param status The return code if a process has died
180  * @param exitcode The returned exit status of the child, if a child process
181  *                 dies, or the signal that caused the child to die.
182  * @param ret The process id of the process that died
183  * @param p The pool to allocate out of
184  * @param s The server_rec to pass
185  */
186 AP_DECLARE(void) ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode,
187                                     apr_proc_t *ret, apr_pool_t *p, 
188                                     server_rec *s);
189
190 #if defined(TCP_NODELAY)
191 /**
192  * Turn off the nagle algorithm for the specified socket.  The nagle algorithm
193  * says that we should delay sending partial packets in the hopes of getting
194  * more data.  There are bad interactions between persistent connections and
195  * Nagle's algorithm that have severe performance penalties.
196  * @param s The socket to disable nagle for.
197  */
198 void ap_sock_disable_nagle(apr_socket_t *s);
199 #else
200 #define ap_sock_disable_nagle(s)        /* NOOP */
201 #endif
202
203 #ifdef HAVE_GETPWNAM
204 /**
205  * Convert a username to a numeric ID
206  * @param name The name to convert
207  * @return The user id corresponding to a name
208  * @fn uid_t ap_uname2id(const char *name)
209  */
210 AP_DECLARE(uid_t) ap_uname2id(const char *name);
211 #endif
212
213 #ifdef HAVE_GETGRNAM
214 /**
215  * Convert a group name to a numeric ID
216  * @param name The name to convert
217  * @return The group id corresponding to a name
218  * @fn gid_t ap_gname2id(const char *name)
219  */
220 AP_DECLARE(gid_t) ap_gname2id(const char *name);
221 #endif
222
223 #ifndef HAVE_INITGROUPS
224 /**
225  * The initgroups() function initializes the group access list by reading the
226  * group database /etc/group and using all groups of which user is a member.
227  * The additional group basegid is also added to the list.
228  * @param name The user name - must be non-NULL
229  * @param basegid The basegid to add
230  * @return returns 0 on success
231  * @fn int initgroups(const char *name, gid_t basegid)
232  */
233 int initgroups(const char *name, gid_t basegid);
234 #endif
235
236 #if (!defined(WIN32) && !defined(NETWARE)) || defined(DOXYGEN)
237
238 typedef struct ap_pod_t ap_pod_t;
239
240 struct ap_pod_t {
241     apr_file_t *pod_in;
242     apr_file_t *pod_out;
243     apr_pool_t *p;
244 };
245
246 /**
247  * Open the pipe-of-death.  The pipe of death is used to tell all child
248  * processes that it is time to die gracefully.
249  * @param p The pool to use for allocating the pipe
250  * @param pod the pipe-of-death that is created.
251  */
252 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod);
253
254 /**
255  * Check the pipe to determine if the process has been signalled to die.
256  */
257 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod);
258
259 /**
260  * Close the pipe-of-death
261  *
262  * @param pod the pipe-of-death to close.
263  */
264 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod);
265
266 /**
267  * Write data to the pipe-of-death, signalling that one child process
268  * should die.
269  * @param pod the pipe-of-death to write to.
270  */
271 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod);
272
273 /**
274  * Write data to the pipe-of-death, signalling that all child process
275  * should die.
276  * @param pod The pipe-of-death to write to.
277  * @param num The number of child processes to kill
278  */
279 AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t *pod, int num);
280
281 #define AP_MPM_PODX_RESTART_CHAR '$'
282 #define AP_MPM_PODX_GRACEFUL_CHAR '!'
283
284 typedef enum { AP_MPM_PODX_NORESTART, AP_MPM_PODX_RESTART, AP_MPM_PODX_GRACEFUL } ap_podx_restart_t;
285
286 /**
287  * Open the extended pipe-of-death.
288  * @param p The pool to use for allocating the pipe
289  * @param pod The pipe-of-death that is created.
290  */
291 AP_DECLARE(apr_status_t) ap_mpm_podx_open(apr_pool_t *p, ap_pod_t **pod);
292
293 /**
294  * Check the extended pipe to determine if the process has been signalled to die.
295  */
296 AP_DECLARE(int) ap_mpm_podx_check(ap_pod_t *pod);
297
298 /**
299  * Close the pipe-of-death
300  *
301  * @param pod The pipe-of-death to close.
302  */
303 AP_DECLARE(apr_status_t) ap_mpm_podx_close(ap_pod_t *pod);
304
305 /**
306  * Write data to the extended pipe-of-death, signalling that one child process
307  * should die.
308  * @param pod the pipe-of-death to write to.
309  * @param graceful restart-type
310  */
311 AP_DECLARE(apr_status_t) ap_mpm_podx_signal(ap_pod_t *pod,
312                                             ap_podx_restart_t graceful);
313
314 /**
315  * Write data to the extended pipe-of-death, signalling that all child process
316  * should die.
317  * @param pod The pipe-of-death to write to.
318  * @param num The number of child processes to kill
319  * @param graceful restart-type
320  */
321 AP_DECLARE(void) ap_mpm_podx_killpg(ap_pod_t *pod, int num,
322                                     ap_podx_restart_t graceful);
323
324 #endif /* (!WIN32 && !NETWARE) || DOXYGEN */
325
326 /**
327  * Check that exactly one MPM is loaded
328  * Returns NULL if yes, error string if not.
329  */
330 AP_DECLARE(const char *) ap_check_mpm(void);
331
332 /*
333  * These data members are common to all mpms. Each new mpm
334  * should either use the appropriate ap_mpm_set_* function
335  * in their command table or create their own for custom or
336  * OS specific needs. These should work for most.
337  */
338
339 /**
340  * The maximum number of requests each child thread or
341  * process handles before dying off
342  */
343 AP_DECLARE_DATA extern int ap_max_requests_per_child;
344 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
345                                     const char *arg);
346
347 /**
348  * The filename used to store the process id.
349  */
350 AP_DECLARE_DATA extern const char *ap_pid_fname;
351 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
352                                const char *arg);
353 void ap_mpm_dump_pidfile(apr_pool_t *p, apr_file_t *out);
354
355 /*
356  * The directory that the server changes directory to dump core.
357  */
358 AP_DECLARE_DATA extern char ap_coredump_dir[MAX_STRING_LEN];
359 AP_DECLARE_DATA extern int ap_coredumpdir_configured;
360 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
361                                    const char *arg);
362
363 /**
364  * Set the timeout period for a graceful shutdown.
365  */
366 AP_DECLARE_DATA extern int ap_graceful_shutdown_timeout;
367 AP_DECLARE(const char *)ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
368                                          const char *arg);
369 #define AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND \
370 AP_INIT_TAKE1("GracefulShutdownTimeout", ap_mpm_set_graceful_shutdown, NULL, \
371               RSRC_CONF, "Maximum time in seconds to wait for child "        \
372               "processes to complete transactions during shutdown")
373
374
375 int ap_signal_server(int *, apr_pool_t *);
376 void ap_mpm_rewrite_args(process_rec *);
377
378 AP_DECLARE_DATA extern apr_uint32_t ap_max_mem_free;
379 extern const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
380                                            const char *arg);
381
382 AP_DECLARE_DATA extern apr_size_t ap_thread_stacksize;
383 extern const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
384                                                const char *arg);
385
386 /* core's implementation of child_status hook */
387 extern void ap_core_child_status(server_rec *s, pid_t pid, ap_generation_t gen,
388                                  int slot, mpm_child_status status);
389
390 #if AP_ENABLE_EXCEPTION_HOOK
391 extern const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
392                                              const char *arg);
393 #endif
394
395 /**
396  * This hook allows modules to be called at intervals by some MPMs
397  * in the parent process.  IOW, this is not portable to all platforms
398  * or MPMs.
399  * @param p The pconf pool
400  * @param s The main server
401  * @return OK or DECLINED (errors are ignored)
402  * @ingroup hooks
403  */
404 AP_DECLARE_HOOK(int,monitor,(apr_pool_t *p, server_rec *s))
405
406 /* register modules that undertake to manage system security */
407 AP_DECLARE(int) ap_sys_privileges_handlers(int inc);
408 AP_DECLARE_HOOK(int, drop_privileges, (apr_pool_t * pchild, server_rec * s))
409
410 /**
411  * implement the ap_mpm_query() function
412  * The MPM should return OK+APR_ENOTIMPL for any unimplemented query codes;
413  * modules which intercede for specific query codes should DECLINE for others.
414  * @ingroup hooks
415  */
416 AP_DECLARE_HOOK(int, mpm_query, (int query_code, int *result, apr_status_t *rv))
417
418 /**
419  * register the specified callback
420  * @ingroup hooks
421  */
422 AP_DECLARE_HOOK(apr_status_t, mpm_register_timed_callback,
423                 (apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton))
424
425 /**
426  * register the specified callback
427  * @ingroup hooks
428  */
429 AP_DECLARE_HOOK(apr_status_t, mpm_register_poll_callback,
430                 (apr_array_header_t *pds, ap_mpm_callback_fn_t *cbfn, void *baton))
431
432 /* register the specified callback, with timeout 
433  * @ingroup hooks
434  *
435  */
436 AP_DECLARE_HOOK(apr_status_t, mpm_register_poll_callback_timeout,
437         (apr_array_header_t *pds,
438                 ap_mpm_callback_fn_t *cbfn,
439                 ap_mpm_callback_fn_t *tofn,
440                 void *baton,
441                 apr_time_t timeout))
442
443 /**
444  * Unregister the specified callback
445  * @ingroup hooks
446  */
447 AP_DECLARE_HOOK(apr_status_t, mpm_unregister_poll_callback,
448                 (apr_array_header_t *pds))
449
450 /** Resume the suspended connection 
451  * @ingroup hooks
452  */
453 AP_DECLARE_HOOK(apr_status_t, mpm_resume_suspended, (conn_rec*))
454
455 /**
456  * Get MPM name (e.g., "prefork" or "event")
457  * @ingroup hooks
458  */
459 AP_DECLARE_HOOK(const char *,mpm_get_name,(void))
460
461 /**
462  * Hook called to determine whether we should stay within the write completion
463  * phase.
464  * @param c The current connection
465  * @return OK if write completion should continue, DECLINED if write completion
466  * should end gracefully, or a positive error if we should begin to linger.
467  * @ingroup hooks
468  */
469 AP_DECLARE_HOOK(int, output_pending, (conn_rec *c))
470
471 /**
472  * Hook called to determine whether any data is pending in the input filters.
473  * @param c The current connection
474  * @return OK if we can read without blocking, DECLINED if a read would block.
475  * @ingroup hooks
476  */
477 AP_DECLARE_HOOK(int, input_pending, (conn_rec *c))
478
479 /**
480  * Notification that connection handling is suspending (disassociating from the
481  * current thread)
482  * @param c The current connection
483  * @param r The current request, or NULL if there is no active request
484  * @ingroup hooks
485  * @see ap_hook_resume_connection
486  * @note This hook is not implemented by MPMs like Prefork and Worker which 
487  * handle all processing of a particular connection on the same thread.
488  * @note This hook will be called on the thread that was previously
489  * processing the connection.
490  * @note This hook is not called at the end of connection processing.  This
491  * hook only notifies a module when processing of an active connection is
492  * suspended.
493  * @note Resumption and subsequent suspension of a connection solely to perform
494  * I/O by the MPM, with no execution of non-MPM code, may not necessarily result
495  * in a call to this hook.
496  * @ingroup hooks
497  */
498 AP_DECLARE_HOOK(void, suspend_connection,
499                 (conn_rec *c, request_rec *r))
500
501 /**
502  * Notification that connection handling is resuming (associating with a thread)
503  * @param c The current connection
504  * @param r The current request, or NULL if there is no active request
505  * @ingroup hooks
506  * @see ap_hook_suspend_connection
507  * @note This hook is not implemented by MPMs like Prefork and Worker which 
508  * handle all processing of a particular connection on the same thread.
509  * @note This hook will be called on the thread that will resume processing
510  * the connection.
511  * @note This hook is not called at the beginning of connection processing.
512  * This hook only notifies a module when processing resumes for a
513  * previously-suspended connection.
514  * @note Resumption and subsequent suspension of a connection solely to perform
515  * I/O by the MPM, with no execution of non-MPM code, may not necessarily result
516  * in a call to this hook.
517  * @ingroup hooks
518  */
519 AP_DECLARE_HOOK(void, resume_connection,
520                 (conn_rec *c, request_rec *r))
521
522 /* mutex type string for accept mutex, if any; MPMs should use the
523  * same mutex type for ease of configuration
524  */
525 #define AP_ACCEPT_MUTEX_TYPE "mpm-accept"
526
527 /* internal pre-config logic for MPM-related settings, callable only from
528  * core's pre-config hook
529  */
530 void mpm_common_pre_config(apr_pool_t *pconf);
531
532 #ifdef __cplusplus
533 }
534 #endif
535
536 #endif /* !APACHE_MPM_COMMON_H */
537 /** @} */