mpm_common.h 17 KB

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