apr_thread_proc.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 occuring, 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 occuring,
  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 sent any signals */
  176. APR_KILL_ALWAYS, /**< process is sent SIGKILL on apr_pool_t cleanup */
  177. APR_KILL_AFTER_TIMEOUT, /**< SIGTERM, wait 3 seconds, SIGKILL */
  178. APR_JUST_WAIT, /**< wait forever for the process to complete */
  179. APR_KILL_ONLY_ONCE /**< send SIGTERM and then wait */
  180. } apr_kill_conditions_e;
  181. /* Thread Function definitions */
  182. #if APR_HAS_THREADS
  183. /**
  184. * Create and initialize a new threadattr variable
  185. * @param new_attr The newly created threadattr.
  186. * @param cont The pool to use
  187. */
  188. APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new_attr,
  189. apr_pool_t *cont);
  190. /**
  191. * Set if newly created threads should be created in detached state.
  192. * @param attr The threadattr to affect
  193. * @param on Non-zero if detached threads should be created.
  194. */
  195. APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
  196. apr_int32_t on);
  197. /**
  198. * Get the detach state for this threadattr.
  199. * @param attr The threadattr to reference
  200. * @return APR_DETACH if threads are to be detached, or APR_NOTDETACH
  201. * if threads are to be joinable.
  202. */
  203. APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr);
  204. /**
  205. * Set the stack size of newly created threads.
  206. * @param attr The threadattr to affect
  207. * @param stacksize The stack size in bytes
  208. */
  209. APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
  210. apr_size_t stacksize);
  211. /**
  212. * Set the stack guard area size of newly created threads.
  213. * @param attr The threadattr to affect
  214. * @param guardsize The stack guard area size in bytes
  215. * @note Thread library implementations commonly use a "guard area"
  216. * after each thread's stack which is not readable or writable such that
  217. * stack overflows cause a segfault; this consumes e.g. 4K of memory
  218. * and increases memory management overhead. Setting the guard area
  219. * size to zero hence trades off reliable behaviour on stack overflow
  220. * for performance. */
  221. APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
  222. apr_size_t guardsize);
  223. /**
  224. * Create a new thread of execution
  225. * @param new_thread The newly created thread handle.
  226. * @param attr The threadattr to use to determine how to create the thread
  227. * @param func The function to start the new thread in
  228. * @param data Any data to be passed to the starting function
  229. * @param cont The pool to use
  230. */
  231. APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new_thread,
  232. apr_threadattr_t *attr,
  233. apr_thread_start_t func,
  234. void *data, apr_pool_t *cont);
  235. /**
  236. * stop the current thread
  237. * @param thd The thread to stop
  238. * @param retval The return value to pass back to any thread that cares
  239. */
  240. APR_DECLARE(apr_status_t) apr_thread_exit(apr_thread_t *thd,
  241. apr_status_t retval);
  242. /**
  243. * block until the desired thread stops executing.
  244. * @param retval The return value from the dead thread.
  245. * @param thd The thread to join
  246. */
  247. APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
  248. apr_thread_t *thd);
  249. /**
  250. * force the current thread to yield the processor
  251. */
  252. APR_DECLARE(void) apr_thread_yield(void);
  253. /**
  254. * Initialize the control variable for apr_thread_once. If this isn't
  255. * called, apr_initialize won't work.
  256. * @param control The control variable to initialize
  257. * @param p The pool to allocate data from.
  258. */
  259. APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
  260. apr_pool_t *p);
  261. /**
  262. * Run the specified function one time, regardless of how many threads
  263. * call it.
  264. * @param control The control variable. The same variable should
  265. * be passed in each time the function is tried to be
  266. * called. This is how the underlying functions determine
  267. * if the function has ever been called before.
  268. * @param func The function to call.
  269. */
  270. APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
  271. void (*func)(void));
  272. /**
  273. * detach a thread
  274. * @param thd The thread to detach
  275. */
  276. APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd);
  277. /**
  278. * Return the pool associated with the current thread.
  279. * @param data The user data associated with the thread.
  280. * @param key The key to associate with the data
  281. * @param thread The currently open thread.
  282. */
  283. APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
  284. apr_thread_t *thread);
  285. /**
  286. * Return the pool associated with the current thread.
  287. * @param data The user data to associate with the thread.
  288. * @param key The key to use for associating the data with the thread
  289. * @param cleanup The cleanup routine to use when the thread is destroyed.
  290. * @param thread The currently open thread.
  291. */
  292. APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
  293. apr_status_t (*cleanup) (void *),
  294. apr_thread_t *thread);
  295. /**
  296. * Create and initialize a new thread private address space
  297. * @param key The thread private handle.
  298. * @param dest The destructor to use when freeing the private memory.
  299. * @param cont The pool to use
  300. */
  301. APR_DECLARE(apr_status_t) apr_threadkey_private_create(apr_threadkey_t **key,
  302. void (*dest)(void *),
  303. apr_pool_t *cont);
  304. /**
  305. * Get a pointer to the thread private memory
  306. * @param new_mem The data stored in private memory
  307. * @param key The handle for the desired thread private memory
  308. */
  309. APR_DECLARE(apr_status_t) apr_threadkey_private_get(void **new_mem,
  310. apr_threadkey_t *key);
  311. /**
  312. * Set the data to be stored in thread private memory
  313. * @param priv The data to be stored in private memory
  314. * @param key The handle for the desired thread private memory
  315. */
  316. APR_DECLARE(apr_status_t) apr_threadkey_private_set(void *priv,
  317. apr_threadkey_t *key);
  318. /**
  319. * Free the thread private memory
  320. * @param key The handle for the desired thread private memory
  321. */
  322. APR_DECLARE(apr_status_t) apr_threadkey_private_delete(apr_threadkey_t *key);
  323. /**
  324. * Return the pool associated with the current threadkey.
  325. * @param data The user data associated with the threadkey.
  326. * @param key The key associated with the data
  327. * @param threadkey The currently open threadkey.
  328. */
  329. APR_DECLARE(apr_status_t) apr_threadkey_data_get(void **data, const char *key,
  330. apr_threadkey_t *threadkey);
  331. /**
  332. * Return the pool associated with the current threadkey.
  333. * @param data The data to set.
  334. * @param key The key to associate with the data.
  335. * @param cleanup The cleanup routine to use when the file is destroyed.
  336. * @param threadkey The currently open threadkey.
  337. */
  338. APR_DECLARE(apr_status_t) apr_threadkey_data_set(void *data, const char *key,
  339. apr_status_t (*cleanup) (void *),
  340. apr_threadkey_t *threadkey);
  341. #endif
  342. /**
  343. * Create and initialize a new procattr variable
  344. * @param new_attr The newly created procattr.
  345. * @param cont The pool to use
  346. */
  347. APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new_attr,
  348. apr_pool_t *cont);
  349. /**
  350. * Determine if any of stdin, stdout, or stderr should be linked to pipes
  351. * when starting a child process.
  352. * @param attr The procattr we care about.
  353. * @param in Should stdin be a pipe back to the parent?
  354. * @param out Should stdout be a pipe back to the parent?
  355. * @param err Should stderr be a pipe back to the parent?
  356. * @note If APR_NO_PIPE, there will be no special channel, the child
  357. * inherits the parent's corresponding stdio stream. If APR_NO_FILE is
  358. * specified, that corresponding stream is closed in the child (and will
  359. * be INVALID_HANDLE_VALUE when inspected on Win32). This can have ugly
  360. * side effects, as the next file opened in the child on Unix will fall
  361. * into the stdio stream fd slot!
  362. */
  363. APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr,
  364. apr_int32_t in, apr_int32_t out,
  365. apr_int32_t err);
  366. /**
  367. * Set the child_in and/or parent_in values to existing apr_file_t values.
  368. * @param attr The procattr we care about.
  369. * @param child_in apr_file_t value to use as child_in. Must be a valid file.
  370. * @param parent_in apr_file_t value to use as parent_in. Must be a valid file.
  371. * @remark This is NOT a required initializer function. This is
  372. * useful if you have already opened a pipe (or multiple files)
  373. * that you wish to use, perhaps persistently across multiple
  374. * process invocations - such as a log file. You can save some
  375. * extra function calls by not creating your own pipe since this
  376. * creates one in the process space for you.
  377. * @bug Note that calling this function with two NULL files on some platforms
  378. * creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
  379. * is it supported. @see apr_procattr_io_set instead for simple pipes.
  380. */
  381. APR_DECLARE(apr_status_t) apr_procattr_child_in_set(struct apr_procattr_t *attr,
  382. apr_file_t *child_in,
  383. apr_file_t *parent_in);
  384. /**
  385. * Set the child_out and parent_out values to existing apr_file_t values.
  386. * @param attr The procattr we care about.
  387. * @param child_out apr_file_t value to use as child_out. Must be a valid file.
  388. * @param parent_out apr_file_t value to use as parent_out. Must be a valid file.
  389. * @remark This is NOT a required initializer function. This is
  390. * useful if you have already opened a pipe (or multiple files)
  391. * that you wish to use, perhaps persistently across multiple
  392. * process invocations - such as a log file.
  393. * @bug Note that calling this function with two NULL files on some platforms
  394. * creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
  395. * is it supported. @see apr_procattr_io_set instead for simple pipes.
  396. */
  397. APR_DECLARE(apr_status_t) apr_procattr_child_out_set(struct apr_procattr_t *attr,
  398. apr_file_t *child_out,
  399. apr_file_t *parent_out);
  400. /**
  401. * Set the child_err and parent_err values to existing apr_file_t values.
  402. * @param attr The procattr we care about.
  403. * @param child_err apr_file_t value to use as child_err. Must be a valid file.
  404. * @param parent_err apr_file_t value to use as parent_err. Must be a valid file.
  405. * @remark This is NOT a required initializer function. This is
  406. * useful if you have already opened a pipe (or multiple files)
  407. * that you wish to use, perhaps persistently across multiple
  408. * process invocations - such as a log file.
  409. * @bug Note that calling this function with two NULL files on some platforms
  410. * creates an APR_FULL_BLOCK pipe, but this behavior is neither portable nor
  411. * is it supported. @see apr_procattr_io_set instead for simple pipes.
  412. */
  413. APR_DECLARE(apr_status_t) apr_procattr_child_err_set(struct apr_procattr_t *attr,
  414. apr_file_t *child_err,
  415. apr_file_t *parent_err);
  416. /**
  417. * Set which directory the child process should start executing in.
  418. * @param attr The procattr we care about.
  419. * @param dir Which dir to start in. By default, this is the same dir as
  420. * the parent currently resides in, when the createprocess call
  421. * is made.
  422. */
  423. APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr,
  424. const char *dir);
  425. /**
  426. * Set what type of command the child process will call.
  427. * @param attr The procattr we care about.
  428. * @param cmd The type of command. One of:
  429. * <PRE>
  430. * APR_SHELLCMD -- Anything that the shell can handle
  431. * APR_PROGRAM -- Executable program (default)
  432. * APR_PROGRAM_ENV -- Executable program, copy environment
  433. * APR_PROGRAM_PATH -- Executable program on PATH, copy env
  434. * </PRE>
  435. */
  436. APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
  437. apr_cmdtype_e cmd);
  438. /**
  439. * Determine if the child should start in detached state.
  440. * @param attr The procattr we care about.
  441. * @param detach Should the child start in detached state? Default is no.
  442. */
  443. APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr,
  444. apr_int32_t detach);
  445. #if APR_HAVE_STRUCT_RLIMIT
  446. /**
  447. * Set the Resource Utilization limits when starting a new process.
  448. * @param attr The procattr we care about.
  449. * @param what Which limit to set, one of:
  450. * <PRE>
  451. * APR_LIMIT_CPU
  452. * APR_LIMIT_MEM
  453. * APR_LIMIT_NPROC
  454. * APR_LIMIT_NOFILE
  455. * </PRE>
  456. * @param limit Value to set the limit to.
  457. */
  458. APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr,
  459. apr_int32_t what,
  460. struct rlimit *limit);
  461. #endif
  462. /**
  463. * Specify an error function to be called in the child process if APR
  464. * encounters an error in the child prior to running the specified program.
  465. * @param attr The procattr describing the child process to be created.
  466. * @param errfn The function to call in the child process.
  467. * @remark At the present time, it will only be called from apr_proc_create()
  468. * on platforms where fork() is used. It will never be called on other
  469. * platforms, on those platforms apr_proc_create() will return the error
  470. * in the parent process rather than invoke the callback in the now-forked
  471. * child process.
  472. */
  473. APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
  474. apr_child_errfn_t *errfn);
  475. /**
  476. * Specify that apr_proc_create() should do whatever it can to report
  477. * failures to the caller of apr_proc_create(), rather than find out in
  478. * the child.
  479. * @param attr The procattr describing the child process to be created.
  480. * @param chk Flag to indicate whether or not extra work should be done
  481. * to try to report failures to the caller.
  482. * @remark This flag only affects apr_proc_create() on platforms where
  483. * fork() is used. This leads to extra overhead in the calling
  484. * process, but that may help the application handle such
  485. * errors more gracefully.
  486. */
  487. APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
  488. apr_int32_t chk);
  489. /**
  490. * Determine if the child should start in its own address space or using the
  491. * current one from its parent
  492. * @param attr The procattr we care about.
  493. * @param addrspace Should the child start in its own address space? Default
  494. * is no on NetWare and yes on other platforms.
  495. */
  496. APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
  497. apr_int32_t addrspace);
  498. /**
  499. * Set the username used for running process
  500. * @param attr The procattr we care about.
  501. * @param username The username used
  502. * @param password User password if needed. Password is needed on WIN32
  503. * or any other platform having
  504. * APR_PROCATTR_USER_SET_REQUIRES_PASSWORD set.
  505. */
  506. APR_DECLARE(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
  507. const char *username,
  508. const char *password);
  509. /**
  510. * Set the group used for running process
  511. * @param attr The procattr we care about.
  512. * @param groupname The group name used
  513. */
  514. APR_DECLARE(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
  515. const char *groupname);
  516. #if APR_HAS_FORK
  517. /**
  518. * This is currently the only non-portable call in APR. This executes
  519. * a standard unix fork.
  520. * @param proc The resulting process handle.
  521. * @param cont The pool to use.
  522. * @remark returns APR_INCHILD for the child, and APR_INPARENT for the parent
  523. * or an error.
  524. */
  525. APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *cont);
  526. #endif
  527. /**
  528. * Create a new process and execute a new program within that process.
  529. * @param new_proc The resulting process handle.
  530. * @param progname The program to run
  531. * @param args the arguments to pass to the new program. The first
  532. * one should be the program name.
  533. * @param env The new environment table for the new process. This
  534. * should be a list of NULL-terminated strings. This argument
  535. * is ignored for APR_PROGRAM_ENV, APR_PROGRAM_PATH, and
  536. * APR_SHELLCMD_ENV types of commands.
  537. * @param attr the procattr we should use to determine how to create the new
  538. * process
  539. * @param pool The pool to use.
  540. * @note This function returns without waiting for the new process to terminate;
  541. * use apr_proc_wait for that.
  542. */
  543. APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new_proc,
  544. const char *progname,
  545. const char * const *args,
  546. const char * const *env,
  547. apr_procattr_t *attr,
  548. apr_pool_t *pool);
  549. /**
  550. * Wait for a child process to die
  551. * @param proc The process handle that corresponds to the desired child process
  552. * @param exitcode The returned exit status of the child, if a child process
  553. * dies, or the signal that caused the child to die.
  554. * On platforms that don't support obtaining this information,
  555. * the status parameter will be returned as APR_ENOTIMPL.
  556. * @param exitwhy Why the child died, the bitwise or of:
  557. * <PRE>
  558. * APR_PROC_EXIT -- process terminated normally
  559. * APR_PROC_SIGNAL -- process was killed by a signal
  560. * APR_PROC_SIGNAL_CORE -- process was killed by a signal, and
  561. * generated a core dump.
  562. * </PRE>
  563. * @param waithow How should we wait. One of:
  564. * <PRE>
  565. * APR_WAIT -- block until the child process dies.
  566. * APR_NOWAIT -- return immediately regardless of if the
  567. * child is dead or not.
  568. * </PRE>
  569. * @remark The childs status is in the return code to this process. It is one of:
  570. * <PRE>
  571. * APR_CHILD_DONE -- child is no longer running.
  572. * APR_CHILD_NOTDONE -- child is still running.
  573. * </PRE>
  574. */
  575. APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
  576. int *exitcode, apr_exit_why_e *exitwhy,
  577. apr_wait_how_e waithow);
  578. /**
  579. * Wait for any current child process to die and return information
  580. * about that child.
  581. * @param proc Pointer to NULL on entry, will be filled out with child's
  582. * information
  583. * @param exitcode The returned exit status of the child, if a child process
  584. * dies, or the signal that caused the child to die.
  585. * On platforms that don't support obtaining this information,
  586. * the status parameter will be returned as APR_ENOTIMPL.
  587. * @param exitwhy Why the child died, the bitwise or of:
  588. * <PRE>
  589. * APR_PROC_EXIT -- process terminated normally
  590. * APR_PROC_SIGNAL -- process was killed by a signal
  591. * APR_PROC_SIGNAL_CORE -- process was killed by a signal, and
  592. * generated a core dump.
  593. * </PRE>
  594. * @param waithow How should we wait. One of:
  595. * <PRE>
  596. * APR_WAIT -- block until the child process dies.
  597. * APR_NOWAIT -- return immediately regardless of if the
  598. * child is dead or not.
  599. * </PRE>
  600. * @param p Pool to allocate child information out of.
  601. * @bug Passing proc as a *proc rather than **proc was an odd choice
  602. * for some platforms... this should be revisited in 1.0
  603. */
  604. APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
  605. int *exitcode,
  606. apr_exit_why_e *exitwhy,
  607. apr_wait_how_e waithow,
  608. apr_pool_t *p);
  609. #define APR_PROC_DETACH_FOREGROUND 0 /**< Do not detach */
  610. #define APR_PROC_DETACH_DAEMONIZE 1 /**< Detach */
  611. /**
  612. * Detach the process from the controlling terminal.
  613. * @param daemonize set to non-zero if the process should daemonize
  614. * and become a background process, else it will
  615. * stay in the foreground.
  616. */
  617. APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize);
  618. /**
  619. * Register an other_child -- a child associated to its registered
  620. * maintence callback. This callback is invoked when the process
  621. * dies, is disconnected or disappears.
  622. * @param proc The child process to register.
  623. * @param maintenance maintenance is a function that is invoked with a
  624. * reason and the data pointer passed here.
  625. * @param data Opaque context data passed to the maintenance function.
  626. * @param write_fd An fd that is probed for writing. If it is ever unwritable
  627. * then the maintenance is invoked with reason
  628. * OC_REASON_UNWRITABLE.
  629. * @param p The pool to use for allocating memory.
  630. * @bug write_fd duplicates the proc->out stream, it's really redundant
  631. * and should be replaced in the APR 1.0 API with a bitflag of which
  632. * proc->in/out/err handles should be health checked.
  633. * @bug no platform currently tests the pipes health.
  634. */
  635. APR_DECLARE(void) apr_proc_other_child_register(apr_proc_t *proc,
  636. void (*maintenance) (int reason,
  637. void *,
  638. int status),
  639. void *data, apr_file_t *write_fd,
  640. apr_pool_t *p);
  641. /**
  642. * Stop watching the specified other child.
  643. * @param data The data to pass to the maintenance function. This is
  644. * used to find the process to unregister.
  645. * @warning Since this can be called by a maintenance function while we're
  646. * scanning the other_children list, all scanners should protect
  647. * themself by loading ocr->next before calling any maintenance
  648. * function.
  649. */
  650. APR_DECLARE(void) apr_proc_other_child_unregister(void *data);
  651. /**
  652. * Notify the maintenance callback of a registered other child process
  653. * that application has detected an event, such as death.
  654. * @param proc The process to check
  655. * @param reason The reason code to pass to the maintenance function
  656. * @param status The status to pass to the maintenance function
  657. * @remark An example of code using this behavior;
  658. * <pre>
  659. * rv = apr_proc_wait_all_procs(&proc, &exitcode, &status, APR_WAIT, p);
  660. * if (APR_STATUS_IS_CHILD_DONE(rv)) {
  661. * \#if APR_HAS_OTHER_CHILD
  662. * if (apr_proc_other_child_alert(&proc, APR_OC_REASON_DEATH, status)
  663. * == APR_SUCCESS) {
  664. * ; (already handled)
  665. * }
  666. * else
  667. * \#endif
  668. * [... handling non-otherchild processes death ...]
  669. * </pre>
  670. */
  671. APR_DECLARE(apr_status_t) apr_proc_other_child_alert(apr_proc_t *proc,
  672. int reason,
  673. int status);
  674. /**
  675. * Test one specific other child processes and invoke the maintenance callback
  676. * with the appropriate reason code, if still running, or the appropriate reason
  677. * code if the process is no longer healthy.
  678. * @param ocr The registered other child
  679. * @param reason The reason code (e.g. APR_OC_REASON_RESTART) if still running
  680. */
  681. APR_DECLARE(void) apr_proc_other_child_refresh(apr_other_child_rec_t *ocr,
  682. int reason);
  683. /**
  684. * Test all registered other child processes and invoke the maintenance callback
  685. * with the appropriate reason code, if still running, or the appropriate reason
  686. * code if the process is no longer healthy.
  687. * @param reason The reason code (e.g. APR_OC_REASON_RESTART) to running processes
  688. */
  689. APR_DECLARE(void) apr_proc_other_child_refresh_all(int reason);
  690. /**
  691. * Terminate a process.
  692. * @param proc The process to terminate.
  693. * @param sig How to kill the process.
  694. */
  695. APR_DECLARE(apr_status_t) apr_proc_kill(apr_proc_t *proc, int sig);
  696. /**
  697. * Register a process to be killed when a pool dies.
  698. * @param a The pool to use to define the processes lifetime
  699. * @param proc The process to register
  700. * @param how How to kill the process, one of:
  701. * <PRE>
  702. * APR_KILL_NEVER -- process is never sent any signals
  703. * APR_KILL_ALWAYS -- process is sent SIGKILL on apr_pool_t cleanup
  704. * APR_KILL_AFTER_TIMEOUT -- SIGTERM, wait 3 seconds, SIGKILL
  705. * APR_JUST_WAIT -- wait forever for the process to complete
  706. * APR_KILL_ONLY_ONCE -- send SIGTERM and then wait
  707. * </PRE>
  708. */
  709. APR_DECLARE(void) apr_pool_note_subprocess(apr_pool_t *a, apr_proc_t *proc,
  710. apr_kill_conditions_e how);
  711. #if APR_HAS_THREADS
  712. #if (APR_HAVE_SIGWAIT || APR_HAVE_SIGSUSPEND) && !defined(OS2)
  713. /**
  714. * Setup the process for a single thread to be used for all signal handling.
  715. * @warning This must be called before any threads are created
  716. */
  717. APR_DECLARE(apr_status_t) apr_setup_signal_thread(void);
  718. /**
  719. * Make the current thread listen for signals. This thread will loop
  720. * forever, calling a provided function whenever it receives a signal. That
  721. * functions should return 1 if the signal has been handled, 0 otherwise.
  722. * @param signal_handler The function to call when a signal is received
  723. * apr_status_t apr_signal_thread((int)(*signal_handler)(int signum))
  724. */
  725. APR_DECLARE(apr_status_t) apr_signal_thread(int(*signal_handler)(int signum));
  726. #endif /* (APR_HAVE_SIGWAIT || APR_HAVE_SIGSUSPEND) && !defined(OS2) */
  727. /**
  728. * Get the child-pool used by the thread from the thread info.
  729. * @return apr_pool_t the pool
  730. */
  731. APR_POOL_DECLARE_ACCESSOR(thread);
  732. #endif /* APR_HAS_THREADS */
  733. /** @} */
  734. #ifdef __cplusplus
  735. }
  736. #endif
  737. #endif /* ! APR_THREAD_PROC_H */