fspr_thread_proc.h 33 KB

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