apr_thread_proc.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. #ifndef APR_THREAD_PROC_H
  17. #define APR_THREAD_PROC_H
  18. /**
  19. * @file apr_thread_proc.h
  20. * @brief APR Thread and Process Library
  21. */
  22. #include "apr.h"
  23. #include "apr_file_io.h"
  24. #include "apr_pools.h"
  25. #include "apr_errno.h"
  26. #if APR_HAVE_STRUCT_RLIMIT
  27. #include <sys/time.h>
  28. #include <sys/resource.h>
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif /* __cplusplus */
  33. /**
  34. * @defgroup apr_thread_proc Threads and Process Functions
  35. * @ingroup APR
  36. * @{
  37. */
  38. typedef enum {
  39. APR_SHELLCMD, /**< use the shell to invoke the program */
  40. APR_PROGRAM, /**< invoke the program directly, no copied env */
  41. APR_PROGRAM_ENV, /**< invoke the program, replicating our environment */
  42. APR_PROGRAM_PATH, /**< find program on PATH, use our environment */
  43. APR_SHELLCMD_ENV /**< use the shell to invoke the program,
  44. * replicating our environment
  45. */
  46. } apr_cmdtype_e;
  47. typedef enum {
  48. APR_WAIT, /**< wait for the specified process to finish */
  49. APR_NOWAIT /**< do not wait -- just see if it has finished */
  50. } apr_wait_how_e;
  51. /* I am specifically calling out the values so that the macros below make
  52. * more sense. Yes, I know I don't need to, but I am hoping this makes what
  53. * I am doing more clear. If you want to add more reasons to exit, continue
  54. * to use bitmasks.
  55. */
  56. typedef enum {
  57. APR_PROC_EXIT = 1, /**< process exited normally */
  58. APR_PROC_SIGNAL = 2, /**< process exited due to a signal */
  59. APR_PROC_SIGNAL_CORE = 4 /**< process exited and dumped a core file */
  60. } apr_exit_why_e;
  61. /** did we exit the process */
  62. #define APR_PROC_CHECK_EXIT(x) (x & APR_PROC_EXIT)
  63. /** did we get a signal */
  64. #define APR_PROC_CHECK_SIGNALED(x) (x & APR_PROC_SIGNAL)
  65. /** did we get core */
  66. #define APR_PROC_CHECK_CORE_DUMP(x) (x & APR_PROC_SIGNAL_CORE)
  67. /** @see apr_procattr_io_set */
  68. #define APR_NO_PIPE 0
  69. /** @see apr_procattr_io_set and apr_file_pipe_create_ex */
  70. #define APR_FULL_BLOCK 1
  71. /** @see apr_procattr_io_set and apr_file_pipe_create_ex */
  72. #define APR_FULL_NONBLOCK 2
  73. /** @see apr_procattr_io_set */
  74. #define APR_PARENT_BLOCK 3
  75. /** @see apr_procattr_io_set */
  76. #define APR_CHILD_BLOCK 4
  77. /** @see apr_procattr_io_set */
  78. #define APR_NO_FILE 8
  79. /** @see apr_file_pipe_create_ex */
  80. #define APR_READ_BLOCK 3
  81. /** @see apr_file_pipe_create_ex */
  82. #define APR_WRITE_BLOCK 4
  83. /** @see apr_procattr_io_set
  84. * @note Win32 only effective with version 1.2.12, portably introduced in 1.3.0
  85. */
  86. #define APR_NO_FILE 8
  87. /** @see apr_procattr_limit_set */
  88. #define APR_LIMIT_CPU 0
  89. /** @see apr_procattr_limit_set */
  90. #define APR_LIMIT_MEM 1
  91. /** @see apr_procattr_limit_set */
  92. #define APR_LIMIT_NPROC 2
  93. /** @see apr_procattr_limit_set */
  94. #define APR_LIMIT_NOFILE 3
  95. /**
  96. * @defgroup APR_OC Other Child Flags
  97. * @{
  98. */
  99. #define APR_OC_REASON_DEATH 0 /**< child has died, caller must call
  100. * unregister still */
  101. #define APR_OC_REASON_UNWRITABLE 1 /**< write_fd is unwritable */
  102. #define APR_OC_REASON_RESTART 2 /**< a restart is occurring, perform
  103. * any necessary cleanup (including
  104. * sending a special signal to child)
  105. */
  106. #define APR_OC_REASON_UNREGISTER 3 /**< unregister has been called, do
  107. * whatever is necessary (including
  108. * kill the child) */
  109. #define APR_OC_REASON_LOST 4 /**< somehow the child exited without
  110. * us knowing ... buggy os? */
  111. #define APR_OC_REASON_RUNNING 5 /**< a health check is occurring,
  112. * for most maintainence functions
  113. * this is a no-op.
  114. */
  115. /** @} */
  116. /** The APR process type */
  117. typedef struct apr_proc_t {
  118. /** The process ID */
  119. pid_t pid;
  120. /** Parent's side of pipe to child's stdin */
  121. apr_file_t *in;
  122. /** Parent's side of pipe to child's stdout */
  123. apr_file_t *out;
  124. /** Parent's side of pipe to child's stdouterr */
  125. apr_file_t *err;
  126. #if APR_HAS_PROC_INVOKED || defined(DOXYGEN)
  127. /** Diagnositics/debugging string of the command invoked for
  128. * this process [only present if APR_HAS_PROC_INVOKED is true]
  129. * @remark Only enabled on Win32 by default.
  130. * @bug This should either always or never be present in release
  131. * builds - since it breaks binary compatibility. We may enable
  132. * it always in APR 1.0 yet leave it undefined in most cases.
  133. */
  134. char *invoked;
  135. #endif
  136. #if defined(WIN32) || defined(DOXYGEN)
  137. /** (Win32 only) Creator's handle granting access to the process
  138. * @remark This handle is closed and reset to NULL in every case
  139. * corresponding to a waitpid() on Unix which returns the exit status.
  140. * Therefore Win32 correspond's to Unix's zombie reaping characteristics
  141. * and avoids potential handle leaks.
  142. */
  143. HANDLE hproc;
  144. #endif
  145. } apr_proc_t;
  146. /**
  147. * The prototype for APR child errfn functions. (See the description
  148. * of apr_procattr_child_errfn_set() for more information.)
  149. * It is passed the following parameters:
  150. * @param pool Pool associated with the apr_proc_t. If your child
  151. * error function needs user data, associate it with this
  152. * pool.
  153. * @param err APR error code describing the error
  154. * @param description Text description of type of processing which failed
  155. */
  156. typedef void (apr_child_errfn_t)(apr_pool_t *proc, apr_status_t err,
  157. const char *description);
  158. /** Opaque Thread structure. */
  159. typedef struct apr_thread_t apr_thread_t;
  160. /** Opaque Thread attributes structure. */
  161. typedef struct apr_threadattr_t apr_threadattr_t;
  162. /** Opaque Process attributes structure. */
  163. typedef struct apr_procattr_t apr_procattr_t;
  164. /** Opaque control variable for one-time atomic variables. */
  165. typedef struct apr_thread_once_t apr_thread_once_t;
  166. /** Opaque thread private address space. */
  167. typedef struct apr_threadkey_t apr_threadkey_t;
  168. /** Opaque record of child process. */
  169. typedef struct apr_other_child_rec_t apr_other_child_rec_t;
  170. /**
  171. * The prototype for any APR thread worker functions.
  172. */
  173. typedef void *(APR_THREAD_FUNC *apr_thread_start_t)(apr_thread_t*, void*);
  174. typedef enum {
  175. APR_KILL_NEVER, /**< process is never killed (i.e., never sent
  176. * any signals), but it will be reaped if it exits
  177. * before the pool is cleaned up */
  178. APR_KILL_ALWAYS, /**< process is sent SIGKILL on apr_pool_t cleanup */
  179. APR_KILL_AFTER_TIMEOUT, /**< SIGTERM, wait 3 seconds, SIGKILL */
  180. APR_JUST_WAIT, /**< wait forever for the process to complete */
  181. APR_KILL_ONLY_ONCE /**< send SIGTERM and then wait */
  182. } apr_kill_conditions_e;
  183. /* Thread Function definitions */
  184. #if APR_HAS_THREADS
  185. /**
  186. * Create and initialize a new threadattr variable
  187. * @param new_attr The newly created threadattr.
  188. * @param cont The pool to use
  189. */
  190. APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new_attr,
  191. apr_pool_t *cont);
  192. /**
  193. * Set if newly created threads should be created in detached state.
  194. * @param attr The threadattr to affect
  195. * @param on Non-zero if detached threads should be created.
  196. */
  197. APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
  198. apr_int32_t on);
  199. /**
  200. * Get the detach state for this threadattr.
  201. * @param attr The threadattr to reference
  202. * @return APR_DETACH if threads are to be detached, or APR_NOTDETACH
  203. * if threads are to be joinable.
  204. */
  205. APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr);
  206. /**
  207. * Set the stack size of newly created threads.
  208. * @param attr The threadattr to affect
  209. * @param stacksize The stack size in bytes
  210. */
  211. APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
  212. apr_size_t stacksize);
  213. /**
  214. * Set the stack guard area size of newly created threads.
  215. * @param attr The threadattr to affect
  216. * @param guardsize The stack guard area size in bytes
  217. * @note Thread library implementations commonly use a "guard area"
  218. * after each thread's stack which is not readable or writable such that
  219. * stack overflows cause a segfault; this consumes e.g. 4K of memory
  220. * and increases memory management overhead. Setting the guard area
  221. * size to zero hence trades off reliable behaviour on stack overflow
  222. * for performance. */
  223. APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
  224. apr_size_t guardsize);
  225. /**
  226. * Create a new thread of execution
  227. * @param new_thread The newly created thread handle.
  228. * @param attr The threadattr to use to determine how to create the thread
  229. * @param func The function to start the new thread in
  230. * @param data Any data to be passed to the starting function
  231. * @param cont The pool to use
  232. */
  233. APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new_thread,
  234. apr_threadattr_t *attr,
  235. apr_thread_start_t func,
  236. void *data, apr_pool_t *cont);
  237. /**
  238. * stop the current thread
  239. * @param thd The thread to stop
  240. * @param retval The return value to pass back to any thread that cares
  241. */
  242. APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
  243. apr_status_t retval);
  244. /**
  245. * block until the desired thread stops executing.
  246. * @param retval The return value from the dead thread.
  247. * @param thd The thread to join
  248. */
  249. APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
  250. apr_thread_t *thd);
  251. /**
  252. * force the current thread to yield the processor
  253. */
  254. APR_DECLARE(void) apr_thread_yield(void);
  255. /**
  256. * Initialize the control variable for apr_thread_once. If this isn't
  257. * called, apr_initialize won't work.
  258. * @param control The control variable to initialize
  259. * @param p The pool to allocate data from.
  260. */
  261. APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
  262. apr_pool_t *p);
  263. /**
  264. * Run the specified function one time, regardless of how many threads
  265. * call it.
  266. * @param control The control variable. The same variable should
  267. * be passed in each time the function is tried to be
  268. * called. This is how the underlying functions determine
  269. * if the function has ever been called before.
  270. * @param func The function to call.
  271. */
  272. APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
  273. void (*func)(void));
  274. /**
  275. * detach a thread
  276. * @param thd The thread to detach
  277. */
  278. APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd);
  279. /**
  280. * Return user data associated with the current thread.
  281. * @param data The user data associated with the thread.
  282. * @param key The key to associate with the data
  283. * @param thread The currently open thread.
  284. */
  285. APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
  286. apr_thread_t *thread);
  287. /**
  288. * Set user data associated with the current thread.
  289. * @param data The user data to associate with the thread.
  290. * @param key The key to use for associating the data with the thread
  291. * @param cleanup The cleanup routine to use when the thread is destroyed.
  292. * @param thread The currently open thread.
  293. */
  294. APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
  295. apr_status_t (*cleanup) (void *),
  296. apr_thread_t *thread);
  297. /**
  298. * Create and initialize a new thread private address space
  299. * @param key The thread private handle.
  300. * @param dest The destructor to use when freeing the private memory.
  301. * @param cont The pool to use
  302. */
  303. APR_DECLARE(apr_status_t) apr_threadkey_private_create(apr_threadkey_t **key,
  304. void (*dest)(void *),
  305. apr_pool_t *cont);
  306. /**
  307. * Get a pointer to the thread private memory
  308. * @param new_mem The data stored in private memory
  309. * @param key The handle for the desired thread private memory
  310. */
  311. APR_DECLARE(apr_status_t) apr_threadkey_private_get(void **new_mem,
  312. apr_threadkey_t *key);
  313. /**
  314. * Set the data to be stored in thread private memory
  315. * @param priv The data to be stored in private memory
  316. * @param key The handle for the desired thread private memory
  317. */
  318. APR_DECLARE(apr_status_t) apr_threadkey_private_set(void *priv,
  319. apr_threadkey_t *key);
  320. /**
  321. * Free the thread private memory
  322. * @param key The handle for the desired thread private memory
  323. */
  324. APR_DECLARE(apr_status_t) apr_threadkey_private_delete(apr_threadkey_t *key);
  325. /**
  326. * Return the pool associated with the current threadkey.
  327. * @param data The user data associated with the threadkey.
  328. * @param key The key associated with the data
  329. * @param threadkey The currently open threadkey.
  330. */
  331. APR_DECLARE(apr_status_t) apr_threadkey_data_get(void **data, const char *key,
  332. apr_threadkey_t *threadkey);
  333. /**
  334. * Return the pool associated with the current threadkey.
  335. * @param data The data to set.
  336. * @param key The key to associate with the data.
  337. * @param cleanup The cleanup routine to use when the file is destroyed.
  338. * @param threadkey The currently open threadkey.
  339. */
  340. APR_DECLARE(apr_status_t) apr_threadkey_data_set(void *data, const char *key,
  341. apr_status_t (*cleanup) (void *),
  342. apr_threadkey_t *threadkey);
  343. #endif
  344. /**
  345. * Create and initialize a new procattr variable
  346. * @param new_attr The newly created procattr.
  347. * @param cont The pool to use
  348. */
  349. APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new_attr,
  350. apr_pool_t *cont);
  351. /**
  352. * Determine if any of stdin, stdout, or stderr should be linked to pipes
  353. * when starting a child process.
  354. * @param attr The procattr we care about.
  355. * @param in Should stdin be a pipe back to the parent?
  356. * @param out Should stdout be a pipe back to the parent?
  357. * @param err Should stderr be a pipe back to the parent?
  358. * @note If APR_NO_PIPE, there will be no special channel, the child
  359. * inherits the parent's corresponding stdio stream. If APR_NO_FILE is
  360. * specified, that corresponding stream is closed in the child (and will
  361. * be INVALID_HANDLE_VALUE when inspected on Win32). This can have ugly
  362. * side effects, as the next file opened in the child on Unix will fall
  363. * into the stdio stream fd slot!
  364. */
  365. APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr,
  366. apr_int32_t in, apr_int32_t out,
  367. apr_int32_t err);
  368. /**
  369. * Set the child_in and/or parent_in values to existing apr_file_t values.
  370. * @param attr The procattr we care about.
  371. * @param child_in apr_file_t value to use as child_in. Must be a valid file.
  372. * @param parent_in apr_file_t value to use as parent_in. Must be a valid file.
  373. * @remark This is NOT a required initializer function. This is
  374. * useful if you have already opened a pipe (or multiple files)
  375. * that you wish to use, perhaps persistently across multiple
  376. * process invocations - such as a log file. You can save some
  377. * extra function calls by not creating your own pipe since this
  378. * creates one in the process space for you.
  379. * @bug Note that calling this function with two NULL files on some platforms
  380. * creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
  381. * is it supported. @see apr_procattr_io_set instead for simple pipes.
  382. */
  383. APR_DECLARE(apr_status_t) apr_procattr_child_in_set(struct apr_procattr_t *attr,
  384. apr_file_t *child_in,
  385. apr_file_t *parent_in);
  386. /**
  387. * Set the child_out and parent_out values to existing apr_file_t values.
  388. * @param attr The procattr we care about.
  389. * @param child_out apr_file_t value to use as child_out. Must be a valid file.
  390. * @param parent_out apr_file_t value to use as parent_out. Must be a valid file.
  391. * @remark This is NOT a required initializer function. This is
  392. * useful if you have already opened a pipe (or multiple files)
  393. * that you wish to use, perhaps persistently across multiple
  394. * process invocations - such as a log file.
  395. * @bug Note that calling this function with two NULL files on some platforms
  396. * creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
  397. * is it supported. @see apr_procattr_io_set instead for simple pipes.
  398. */
  399. APR_DECLARE(apr_status_t) apr_procattr_child_out_set(struct apr_procattr_t *attr,
  400. apr_file_t *child_out,
  401. apr_file_t *parent_out);
  402. /**
  403. * Set the child_err and parent_err values to existing apr_file_t values.
  404. * @param attr The procattr we care about.
  405. * @param child_err apr_file_t value to use as child_err. Must be a valid file.
  406. * @param parent_err apr_file_t value to use as parent_err. Must be a valid file.
  407. * @remark This is NOT a required initializer function. This is
  408. * useful if you have already opened a pipe (or multiple files)
  409. * that you wish to use, perhaps persistently across multiple
  410. * process invocations - such as a log file.
  411. * @bug Note that calling this function with two NULL files on some platforms
  412. * creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
  413. * is it supported. @see apr_procattr_io_set instead for simple pipes.
  414. */
  415. APR_DECLARE(apr_status_t) apr_procattr_child_err_set(struct apr_procattr_t *attr,
  416. apr_file_t *child_err,
  417. apr_file_t *parent_err);
  418. /**
  419. * Set which directory the child process should start executing in.
  420. * @param attr The procattr we care about.
  421. * @param dir Which dir to start in. By default, this is the same dir as
  422. * the parent currently resides in, when the createprocess call
  423. * is made.
  424. */
  425. APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr,
  426. const char *dir);
  427. /**
  428. * Set what type of command the child process will call.
  429. * @param attr The procattr we care about.
  430. * @param cmd The type of command. One of:
  431. * <PRE>
  432. * APR_SHELLCMD -- Anything that the shell can handle
  433. * APR_PROGRAM -- Executable program (default)
  434. * APR_PROGRAM_ENV -- Executable program, copy environment
  435. * APR_PROGRAM_PATH -- Executable program on PATH, copy env
  436. * </PRE>
  437. */
  438. APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
  439. apr_cmdtype_e cmd);
  440. /**
  441. * Determine if the child should start in detached state.
  442. * @param attr The procattr we care about.
  443. * @param detach Should the child start in detached state? Default is no.
  444. */
  445. APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr,
  446. apr_int32_t detach);
  447. #if APR_HAVE_STRUCT_RLIMIT
  448. /**
  449. * Set the Resource Utilization limits when starting a new process.
  450. * @param attr The procattr we care about.
  451. * @param what Which limit to set, one of:
  452. * <PRE>
  453. * APR_LIMIT_CPU
  454. * APR_LIMIT_MEM
  455. * APR_LIMIT_NPROC
  456. * APR_LIMIT_NOFILE
  457. * </PRE>
  458. * @param limit Value to set the limit to.
  459. */
  460. APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr,
  461. apr_int32_t what,
  462. struct rlimit *limit);
  463. #endif
  464. /**
  465. * Specify an error function to be called in the child process if APR
  466. * encounters an error in the child prior to running the specified program.
  467. * @param attr The procattr describing the child process to be created.
  468. * @param errfn The function to call in the child process.
  469. * @remark At the present time, it will only be called from apr_proc_create()
  470. * on platforms where fork() is used. It will never be called on other
  471. * platforms, on those platforms apr_proc_create() will return the error
  472. * in the parent process rather than invoke the callback in the now-forked
  473. * child process.
  474. */
  475. APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
  476. apr_child_errfn_t *errfn);
  477. /**
  478. * Specify that apr_proc_create() should do whatever it can to report
  479. * failures to the caller of apr_proc_create(), rather than find out in
  480. * the child.
  481. * @param attr The procattr describing the child process to be created.
  482. * @param chk Flag to indicate whether or not extra work should be done
  483. * to try to report failures to the caller.
  484. * @remark This flag only affects apr_proc_create() on platforms where
  485. * fork() is used. This leads to extra overhead in the calling
  486. * process, but that may help the application handle such
  487. * errors more gracefully.
  488. */
  489. APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
  490. apr_int32_t chk);
  491. /**
  492. * Determine if the child should start in its own address space or using the
  493. * current one from its parent
  494. * @param attr The procattr we care about.
  495. * @param addrspace Should the child start in its own address space? Default
  496. * is no on NetWare and yes on other platforms.
  497. */
  498. APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
  499. apr_int32_t addrspace);
  500. /**
  501. * Set the username used for running process
  502. * @param attr The procattr we care about.
  503. * @param username The username used
  504. * @param password User password if needed. Password is needed on WIN32
  505. * or any other platform having
  506. * APR_PROCATTR_USER_SET_REQUIRES_PASSWORD set.
  507. */
  508. APR_DECLARE(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
  509. const char *username,
  510. const char *password);
  511. /**
  512. * Set the group used for running process
  513. * @param attr The procattr we care about.
  514. * @param groupname The group name used
  515. */
  516. APR_DECLARE(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
  517. const char *groupname);
  518. #if APR_HAS_FORK
  519. /**
  520. * This is currently the only non-portable call in APR. This executes
  521. * a standard unix fork.
  522. * @param proc The resulting process handle.
  523. * @param cont The pool to use.
  524. * @remark returns APR_INCHILD for the child, and APR_INPARENT for the parent
  525. * or an error.
  526. */
  527. APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *cont);
  528. #endif
  529. /**
  530. * Create a new process and execute a new program within that process.
  531. * @param new_proc The resulting process handle.
  532. * @param progname The program to run
  533. * @param args the arguments to pass to the new program. The first
  534. * one should be the program name.
  535. * @param env The new environment table for the new process. This
  536. * should be a list of NULL-terminated strings. This argument
  537. * is ignored for APR_PROGRAM_ENV, APR_PROGRAM_PATH, and
  538. * APR_SHELLCMD_ENV types of commands.
  539. * @param attr the procattr we should use to determine how to create the new
  540. * process
  541. * @param pool The pool to use.
  542. * @note This function returns without waiting for the new process to terminate;
  543. * use apr_proc_wait for that.
  544. */
  545. APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new_proc,
  546. const char *progname,
  547. const char * const *args,
  548. const char * const *env,
  549. apr_procattr_t *attr,
  550. apr_pool_t *pool);
  551. /**
  552. * Wait for a child process to die
  553. * @param proc The process handle that corresponds to the desired child process
  554. * @param exitcode The returned exit status of the child, if a child process
  555. * dies, or the signal that caused the child to die.
  556. * On platforms that don't support obtaining this information,
  557. * the status parameter will be returned as APR_ENOTIMPL.
  558. * @param exitwhy Why the child died, the bitwise or of:
  559. * <PRE>
  560. * APR_PROC_EXIT -- process terminated normally
  561. * APR_PROC_SIGNAL -- process was killed by a signal
  562. * APR_PROC_SIGNAL_CORE -- process was killed by a signal, and
  563. * generated a core dump.
  564. * </PRE>
  565. * @param waithow How should we wait. One of:
  566. * <PRE>
  567. * APR_WAIT -- block until the child process dies.
  568. * APR_NOWAIT -- return immediately regardless of if the
  569. * child is dead or not.
  570. * </PRE>
  571. * @remark The child's status is in the return code to this process. It is one of:
  572. * <PRE>
  573. * APR_CHILD_DONE -- child is no longer running.
  574. * APR_CHILD_NOTDONE -- child is still running.
  575. * </PRE>
  576. */
  577. APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
  578. int *exitcode, apr_exit_why_e *exitwhy,
  579. apr_wait_how_e waithow);
  580. /**
  581. * Wait for any current child process to die and return information
  582. * about that child.
  583. * @param proc Pointer to NULL on entry, will be filled out with child's
  584. * information
  585. * @param exitcode The returned exit status of the child, if a child process
  586. * dies, or the signal that caused the child to die.
  587. * On platforms that don't support obtaining this information,
  588. * the status parameter will be returned as APR_ENOTIMPL.
  589. * @param exitwhy Why the child died, the bitwise or of:
  590. * <PRE>
  591. * APR_PROC_EXIT -- process terminated normally
  592. * APR_PROC_SIGNAL -- process was killed by a signal
  593. * APR_PROC_SIGNAL_CORE -- process was killed by a signal, and
  594. * generated a core dump.
  595. * </PRE>
  596. * @param waithow How should we wait. One of:
  597. * <PRE>
  598. * APR_WAIT -- block until the child process dies.
  599. * APR_NOWAIT -- return immediately regardless of if the
  600. * child is dead or not.
  601. * </PRE>
  602. * @param p Pool to allocate child information out of.
  603. * @bug Passing proc as a *proc rather than **proc was an odd choice
  604. * for some platforms... this should be revisited in 1.0
  605. */
  606. APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
  607. int *exitcode,
  608. apr_exit_why_e *exitwhy,
  609. apr_wait_how_e waithow,
  610. apr_pool_t *p);
  611. #define APR_PROC_DETACH_FOREGROUND 0 /**< Do not detach */
  612. #define APR_PROC_DETACH_DAEMONIZE 1 /**< Detach */
  613. /**
  614. * Detach the process from the controlling terminal.
  615. * @param daemonize set to non-zero if the process should daemonize
  616. * and become a background process, else it will
  617. * stay in the foreground.
  618. */
  619. APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize);
  620. /**
  621. * Register an other_child -- a child associated to its registered
  622. * maintence callback. This callback is invoked when the process
  623. * dies, is disconnected or disappears.
  624. * @param proc The child process to register.
  625. * @param maintenance maintenance is a function that is invoked with a
  626. * reason and the data pointer passed here.
  627. * @param data Opaque context data passed to the maintenance function.
  628. * @param write_fd An fd that is probed for writing. If it is ever unwritable
  629. * then the maintenance is invoked with reason
  630. * OC_REASON_UNWRITABLE.
  631. * @param p The pool to use for allocating memory.
  632. * @bug write_fd duplicates the proc->out stream, it's really redundant
  633. * and should be replaced in the APR 1.0 API with a bitflag of which
  634. * proc->in/out/err handles should be health checked.
  635. * @bug no platform currently tests the pipes health.
  636. */
  637. APR_DECLARE(void) apr_proc_other_child_register(apr_proc_t *proc,
  638. void (*maintenance) (int reason,
  639. void *,
  640. int status),
  641. void *data, apr_file_t *write_fd,
  642. apr_pool_t *p);
  643. /**
  644. * Stop watching the specified other child.
  645. * @param data The data to pass to the maintenance function. This is
  646. * used to find the process to unregister.
  647. * @warning Since this can be called by a maintenance function while we're
  648. * scanning the other_children list, all scanners should protect
  649. * themself by loading ocr->next before calling any maintenance
  650. * function.
  651. */
  652. APR_DECLARE(void) apr_proc_other_child_unregister(void *data);
  653. /**
  654. * Notify the maintenance callback of a registered other child process
  655. * that application has detected an event, such as death.
  656. * @param proc The process to check
  657. * @param reason The reason code to pass to the maintenance function
  658. * @param status The status to pass to the maintenance function
  659. * @remark An example of code using this behavior;
  660. * <pre>
  661. * rv = apr_proc_wait_all_procs(&proc, &exitcode, &status, APR_WAIT, p);
  662. * if (APR_STATUS_IS_CHILD_DONE(rv)) {
  663. * \#if APR_HAS_OTHER_CHILD
  664. * if (apr_proc_other_child_alert(&proc, APR_OC_REASON_DEATH, status)
  665. * == APR_SUCCESS) {
  666. * ; (already handled)
  667. * }
  668. * else
  669. * \#endif
  670. * [... handling non-otherchild processes death ...]
  671. * </pre>
  672. */
  673. APR_DECLARE(apr_status_t) apr_proc_other_child_alert(apr_proc_t *proc,
  674. int reason,
  675. int status);
  676. /**
  677. * Test one specific other child processes and invoke the maintenance callback
  678. * with the appropriate reason code, if still running, or the appropriate reason
  679. * code if the process is no longer healthy.
  680. * @param ocr The registered other child
  681. * @param reason The reason code (e.g. APR_OC_REASON_RESTART) if still running
  682. */
  683. APR_DECLARE(void) apr_proc_other_child_refresh(apr_other_child_rec_t *ocr,
  684. int reason);
  685. /**
  686. * Test all registered other child processes and invoke the maintenance callback
  687. * with the appropriate reason code, if still running, or the appropriate reason
  688. * code if the process is no longer healthy.
  689. * @param reason The reason code (e.g. APR_OC_REASON_RESTART) to running processes
  690. */
  691. APR_DECLARE(void) apr_proc_other_child_refresh_all(int reason);
  692. /**
  693. * Terminate a process.
  694. * @param proc The process to terminate.
  695. * @param sig How to kill the process.
  696. */
  697. APR_DECLARE(apr_status_t) apr_proc_kill(apr_proc_t *proc, int sig);
  698. /**
  699. * Register a process to be killed when a pool dies.
  700. * @param a The pool to use to define the processes lifetime
  701. * @param proc The process to register
  702. * @param how How to kill the process, one of:
  703. * <PRE>
  704. * APR_KILL_NEVER -- process is never sent any signals
  705. * APR_KILL_ALWAYS -- process is sent SIGKILL on apr_pool_t cleanup
  706. * APR_KILL_AFTER_TIMEOUT -- SIGTERM, wait 3 seconds, SIGKILL
  707. * APR_JUST_WAIT -- wait forever for the process to complete
  708. * APR_KILL_ONLY_ONCE -- send SIGTERM and then wait
  709. * </PRE>
  710. */
  711. APR_DECLARE(void) apr_pool_note_subprocess(apr_pool_t *a, apr_proc_t *proc,
  712. apr_kill_conditions_e how);
  713. #if APR_HAS_THREADS
  714. #if (APR_HAVE_SIGWAIT || APR_HAVE_SIGSUSPEND) && !defined(OS2)
  715. /**
  716. * Setup the process for a single thread to be used for all signal handling.
  717. * @warning This must be called before any threads are created
  718. */
  719. APR_DECLARE(apr_status_t) apr_setup_signal_thread(void);
  720. /**
  721. * Make the current thread listen for signals. This thread will loop
  722. * forever, calling a provided function whenever it receives a signal. That
  723. * functions should return 1 if the signal has been handled, 0 otherwise.
  724. * @param signal_handler The function to call when a signal is received
  725. * apr_status_t apr_signal_thread((int)(*signal_handler)(int signum))
  726. */
  727. APR_DECLARE(apr_status_t) apr_signal_thread(int(*signal_handler)(int signum));
  728. #endif /* (APR_HAVE_SIGWAIT || APR_HAVE_SIGSUSPEND) && !defined(OS2) */
  729. /**
  730. * Get the child-pool used by the thread from the thread info.
  731. * @return apr_pool_t the pool
  732. */
  733. APR_POOL_DECLARE_ACCESSOR(thread);
  734. #endif /* APR_HAS_THREADS */
  735. /** @} */
  736. #ifdef __cplusplus
  737. }
  738. #endif
  739. #endif /* ! APR_THREAD_PROC_H */