ap_mpm.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * @file ap_mpm.h
  18. * @brief Apache Multi-Processing Module library
  19. *
  20. * @defgroup APACHE_CORE_MPM Multi-Processing Module library
  21. * @ingroup APACHE_CORE
  22. * @{
  23. */
  24. #ifndef AP_MPM_H
  25. #define AP_MPM_H
  26. #include "apr_thread_proc.h"
  27. #include "httpd.h"
  28. #include "scoreboard.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /*
  33. The MPM, "multi-processing model" provides an abstraction of the
  34. interface with the OS for distributing incoming connections to
  35. threads/process for processing. http_main invokes the MPM, and
  36. the MPM runs until a shutdown/restart has been indicated.
  37. The MPM calls out to the apache core via the ap_process_connection
  38. function when a connection arrives.
  39. The MPM may or may not be multithreaded. In the event that it is
  40. multithreaded, at any instant it guarantees a 1:1 mapping of threads
  41. ap_process_connection invocations.
  42. Note: In the future it will be possible for ap_process_connection
  43. to return to the MPM prior to finishing the entire connection; and
  44. the MPM will proceed with asynchronous handling for the connection;
  45. in the future the MPM may call ap_process_connection again -- but
  46. does not guarantee it will occur on the same thread as the first call.
  47. The MPM further guarantees that no asynchronous behaviour such as
  48. longjmps and signals will interfere with the user code that is
  49. invoked through ap_process_connection. The MPM may reserve some
  50. signals for its use (i.e. SIGUSR1), but guarantees that these signals
  51. are ignored when executing outside the MPM code itself. (This
  52. allows broken user code that does not handle EINTR to function
  53. properly.)
  54. The suggested server restart and stop behaviour will be "graceful".
  55. However the MPM may choose to terminate processes when the user
  56. requests a non-graceful restart/stop. When this occurs, the MPM kills
  57. all threads with extreme prejudice, and destroys the pchild pool.
  58. User cleanups registered in the pchild apr_pool_t will be invoked at
  59. this point. (This can pose some complications, the user cleanups
  60. are asynchronous behaviour not unlike longjmp/signal... but if the
  61. admin is asking for a non-graceful shutdown, how much effort should
  62. we put into doing it in a nice way?)
  63. unix/posix notes:
  64. - The MPM does not set a SIGALRM handler, user code may use SIGALRM.
  65. But the preferred method of handling timeouts is to use the
  66. timeouts provided by the BUFF abstraction.
  67. - The proper setting for SIGPIPE is SIG_IGN, if user code changes it
  68. for any of their own processing, it must be restored to SIG_IGN
  69. prior to executing or returning to any apache code.
  70. TODO: add SIGPIPE debugging check somewhere to make sure it's SIG_IGN
  71. */
  72. /**
  73. * Pass control to the MPM for steady-state processing. It 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 DONE for shutdown OK otherwise.
  80. */
  81. AP_DECLARE_HOOK(int, mpm, (apr_pool_t *pconf, apr_pool_t *plog, server_rec *server_conf))
  82. /**
  83. * Spawn a process with privileges that another module has requested
  84. * @param r The request_rec of the current request
  85. * @param newproc The resulting process handle.
  86. * @param progname The program to run
  87. * @param args the arguments to pass to the new program. The first
  88. * one should be the program name.
  89. * @param env The new environment apr_table_t for the new process. This
  90. * should be a list of NULL-terminated strings.
  91. * @param attr the procattr we should use to determine how to create the new
  92. * process
  93. * @param p The pool to use.
  94. */
  95. AP_DECLARE(apr_status_t) ap_os_create_privileged_process(
  96. const request_rec *r,
  97. apr_proc_t *newproc,
  98. const char *progname,
  99. const char * const *args,
  100. const char * const *env,
  101. apr_procattr_t *attr,
  102. apr_pool_t *p);
  103. /* Subtypes/Values for AP_MPMQ_IS_THREADED and AP_MPMQ_IS_FORKED */
  104. #define AP_MPMQ_NOT_SUPPORTED 0 /* This value specifies that an */
  105. /* MPM is not capable of */
  106. /* threading or forking. */
  107. #define AP_MPMQ_STATIC 1 /* This value specifies that */
  108. /* an MPM is using a static */
  109. /* number of threads or daemons */
  110. #define AP_MPMQ_DYNAMIC 2 /* This value specifies that */
  111. /* an MPM is using a dynamic */
  112. /* number of threads or daemons */
  113. /* Values returned for AP_MPMQ_MPM_STATE */
  114. #define AP_MPMQ_STARTING 0
  115. #define AP_MPMQ_RUNNING 1
  116. #define AP_MPMQ_STOPPING 2
  117. #define AP_MPMQ_MAX_DAEMON_USED 1 /* Max # of daemons used so far */
  118. #define AP_MPMQ_IS_THREADED 2 /* MPM can do threading */
  119. #define AP_MPMQ_IS_FORKED 3 /* MPM can do forking */
  120. #define AP_MPMQ_HARD_LIMIT_DAEMONS 4 /* The compiled max # daemons */
  121. #define AP_MPMQ_HARD_LIMIT_THREADS 5 /* The compiled max # threads */
  122. #define AP_MPMQ_MAX_THREADS 6 /* # of threads/child by config */
  123. #define AP_MPMQ_MIN_SPARE_DAEMONS 7 /* Min # of spare daemons */
  124. #define AP_MPMQ_MIN_SPARE_THREADS 8 /* Min # of spare threads */
  125. #define AP_MPMQ_MAX_SPARE_DAEMONS 9 /* Max # of spare daemons */
  126. #define AP_MPMQ_MAX_SPARE_THREADS 10 /* Max # of spare threads */
  127. #define AP_MPMQ_MAX_REQUESTS_DAEMON 11 /* Max # of requests per daemon */
  128. #define AP_MPMQ_MAX_DAEMONS 12 /* Max # of daemons by config */
  129. #define AP_MPMQ_MPM_STATE 13 /* starting, running, stopping */
  130. #define AP_MPMQ_IS_ASYNC 14 /* MPM can process async connections */
  131. #define AP_MPMQ_GENERATION 15 /* MPM generation */
  132. #define AP_MPMQ_HAS_SERF 16 /* MPM can drive serf internally */
  133. /**
  134. * Query a property of the current MPM.
  135. * @param query_code One of APM_MPMQ_*
  136. * @param result A location to place the result of the query
  137. * @return APR_EGENERAL if an mpm-query hook has not been registered;
  138. * APR_SUCCESS or APR_ENOTIMPL otherwise
  139. * @remark The MPM doesn't register the implementing hook until the
  140. * register_hooks hook is called, so modules cannot use ap_mpm_query()
  141. * until after that point.
  142. * @fn int ap_mpm_query(int query_code, int *result)
  143. */
  144. AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result);
  145. typedef void (ap_mpm_callback_fn_t)(void *baton);
  146. /* only added support in the Event MPM.... check for APR_ENOTIMPL */
  147. AP_DECLARE(apr_status_t) ap_mpm_register_timed_callback(apr_time_t t,
  148. ap_mpm_callback_fn_t *cbfn,
  149. void *baton);
  150. typedef enum mpm_child_status {
  151. MPM_CHILD_STARTED,
  152. MPM_CHILD_EXITED,
  153. MPM_CHILD_LOST_SLOT
  154. } mpm_child_status;
  155. /**
  156. * Allow a module to remain aware of MPM child process state changes,
  157. * along with the generation and scoreboard slot of the process changing
  158. * state.
  159. *
  160. * With some MPMs (event and worker), an active MPM child process may lose
  161. * its scoreboard slot if the child process is exiting and the scoreboard
  162. * slot is needed by other processes. When this occurs, the hook will be
  163. * called with the MPM_CHILD_LOST_SLOT state.
  164. *
  165. * @param s The main server_rec.
  166. * @param pid The id of the MPM child process.
  167. * @param gen The server generation of that child process.
  168. * @param slot The scoreboard slot number, or -1. It will be -1 when an
  169. * MPM child process exits, and that child had previously lost its
  170. * scoreboard slot.
  171. * @param state One of the mpm_child_status values. Modules should ignore
  172. * unrecognized values.
  173. */
  174. AP_DECLARE_HOOK(void,child_status,(server_rec *s, pid_t pid, ap_generation_t gen,
  175. int slot, mpm_child_status state))
  176. /**
  177. * Allow a module to be notified when the last child process of a generation
  178. * exits.
  179. *
  180. * @param s The main server_rec.
  181. * @param gen The server generation which is now completely finished.
  182. */
  183. AP_DECLARE_HOOK(void,end_generation,(server_rec *s, ap_generation_t gen))
  184. /* Defining GPROF when compiling uses the moncontrol() function to
  185. * disable gprof profiling in the parent, and enable it only for
  186. * request processing in children (or in one_process mode). It's
  187. * absolutely required to get useful gprof results under linux
  188. * because the profile itimers and such are disabled across a
  189. * fork(). It's probably useful elsewhere as well.
  190. */
  191. #ifdef GPROF
  192. extern void moncontrol(int);
  193. #define AP_MONCONTROL(x) moncontrol(x)
  194. #else
  195. #define AP_MONCONTROL(x)
  196. #endif
  197. #ifdef AP_ENABLE_EXCEPTION_HOOK
  198. typedef struct ap_exception_info_t {
  199. int sig;
  200. pid_t pid;
  201. } ap_exception_info_t;
  202. AP_DECLARE_HOOK(int,fatal_exception,(ap_exception_info_t *ei))
  203. #endif /*AP_ENABLE_EXCEPTION_HOOK*/
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif
  208. /** @} */