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
51extern "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 */
89typedef 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 */
106AP_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 */
119AP_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 */
132AP_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 */
142AP_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 */
153AP_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 */
163AP_DECLARE(int) ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status);
164
165AP_DECLARE(apr_status_t) ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf);
166AP_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 */
174apr_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 */
186AP_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 */
198void 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 */
210AP_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 */
220AP_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 */
233int initgroups(const char *name, gid_t basegid);
234#endif
235
236#if (!defined(WIN32) && !defined(NETWARE)) || defined(DOXYGEN)
237
238typedef struct ap_pod_t ap_pod_t;
239
240struct 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 */
252AP_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 */
257AP_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 */
264AP_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 */
271AP_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 */
279AP_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
284typedef 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 */
291AP_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 */
296AP_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 */
303AP_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 */
311AP_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 */
321AP_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 */
330AP_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 */
343AP_DECLARE_DATA extern int ap_max_requests_per_child;
344const 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 */
350AP_DECLARE_DATA extern const char *ap_pid_fname;
351const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
352                               const char *arg);
353void 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 */
358AP_DECLARE_DATA extern char ap_coredump_dir[MAX_STRING_LEN];
359AP_DECLARE_DATA extern int ap_coredumpdir_configured;
360const 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 */
366AP_DECLARE_DATA extern int ap_graceful_shutdown_timeout;
367AP_DECLARE(const char *)ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
368                                         const char *arg);
369#define AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND \
370AP_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
375int ap_signal_server(int *, apr_pool_t *);
376void ap_mpm_rewrite_args(process_rec *);
377
378AP_DECLARE_DATA extern apr_uint32_t ap_max_mem_free;
379extern const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
380                                           const char *arg);
381
382AP_DECLARE_DATA extern apr_size_t ap_thread_stacksize;
383extern 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 */
387extern 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
391extern const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
392                                             const char *arg);
393#endif
394
395AP_DECLARE_HOOK(int,monitor,(apr_pool_t *p, server_rec *s))
396
397/* register modules that undertake to manage system security */
398AP_DECLARE(int) ap_sys_privileges_handlers(int inc);
399AP_DECLARE_HOOK(int, drop_privileges, (apr_pool_t * pchild, server_rec * s))
400
401/* implement the ap_mpm_query() function
402 * The MPM should return OK+APR_ENOTIMPL for any unimplemented query codes;
403 * modules which intercede for specific query codes should DECLINE for others.
404 */
405AP_DECLARE_HOOK(int, mpm_query, (int query_code, int *result, apr_status_t *rv))
406
407/* register the specified callback */
408AP_DECLARE_HOOK(apr_status_t, mpm_register_timed_callback,
409                (apr_time_t t, ap_mpm_callback_fn_t *cbfn, void *baton))
410
411/* get MPM name (e.g., "prefork" or "event") */
412AP_DECLARE_HOOK(const char *,mpm_get_name,(void))
413
414/* mutex type string for accept mutex, if any; MPMs should use the
415 * same mutex type for ease of configuration
416 */
417#define AP_ACCEPT_MUTEX_TYPE "mpm-accept"
418
419/* internal pre-config logic for MPM-related settings, callable only from
420 * core's pre-config hook
421 */
422void mpm_common_pre_config(apr_pool_t *pconf);
423
424#ifdef __cplusplus
425}
426#endif
427
428#endif /* !APACHE_MPM_COMMON_H */
429/** @} */
430