apr_thread_proc.h 36 KB

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