apr_thread_proc.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. /**
  449. * Request OS to tie child-process lifespan to parent process.
  450. * (Currently supported only on Windows.)
  451. * @param attr The procattr we care about.
  452. * @param autokill Nonzero means child lifespan will be tied to calling
  453. * process lifespan. Default is no.
  454. */
  455. APR_DECLARE(apr_status_t) apr_procattr_autokill_set(apr_procattr_t *attr,
  456. apr_int32_t autokill);
  457. // APR_HAS_PROCATTR_AUTOKILL_SET actually has three discernable states. Plain
  458. // APR doesn't define it, so #if ! defined(APR_HAS_PROCATTR_AUTOKILL_SET) can
  459. // detect if the APR in hand lacks the extension. Further, though, we #define
  460. // it to 0 on platforms where we happen to know apr_procattr_autokill_set()
  461. // will return APR_ENOTIMPL.
  462. // It doesn't seem to work to #define MACRO as defined(OTHERMACRO) because
  463. // apparently defined() isn't itself a macro; it doesn't get rescanned. From
  464. // the preprocessor's point of view, MACRO simply has a funny string value.
  465. #if defined(WIN32) || defined(_WIN32)
  466. #define APR_HAS_PROCATTR_AUTOKILL_SET 1 // useful implementation
  467. #else
  468. #define APR_HAS_PROCATTR_AUTOKILL_SET 0 // placeholder implementation
  469. #endif
  470. /**
  471. * Request apr_proc_create() to constrain the set of handles passed to the
  472. * child process. On Windows, with an ordinary CreateProcess() call, you get
  473. * two choices: pass NO handles (bInheritHandles=FALSE), or pass ALL
  474. * currently-open handles, from anywhere in the process (including libraries!)
  475. * -- except those specifically marked with HANDLE_FLAG_INHERIT 0.
  476. * apr_proc_create(), which promises to provide the child process with stdin,
  477. * stdout, stderr, normally passes bInheritHandles=TRUE. But std::ofstream et
  478. * al. open files as inheritable, and provide no API by which to mark them
  479. * otherwise. And since Windows prevents certain operations on files held open
  480. * by any process, even if inadvertently, confusing bugs ensue.
  481. * Calling apr_procattr_constrain_handle_set(attr, 1) engages obscure
  482. * Windows machinery to specifically pass stdin, stdout, stderr -- but no
  483. * other handles.
  484. * See http://blogs.msdn.com/b/oldnewthing/archive/2011/12/16/10248328.aspx .
  485. * apr_procattr_constrain_handle_set() currently only affects apr_proc_create()
  486. * on Windows.
  487. * @param attr The procattr we care about.
  488. * @param constrain On Windows, nonzero means to explicitly constrain the set
  489. * of handles passed to the new child process. Default is 0, like unmodified
  490. * APR.
  491. */
  492. APR_DECLARE(apr_status_t) apr_procattr_constrain_handle_set(apr_procattr_t *attr,
  493. apr_int32_t constrain);
  494. #define APR_HAS_PROCATTR_CONSTRAIN_HANDLE_SET 1 // extension is present
  495. #if APR_HAVE_STRUCT_RLIMIT
  496. /**
  497. * Set the Resource Utilization limits when starting a new process.
  498. * @param attr The procattr we care about.
  499. * @param what Which limit to set, one of:
  500. * <PRE>
  501. * APR_LIMIT_CPU
  502. * APR_LIMIT_MEM
  503. * APR_LIMIT_NPROC
  504. * APR_LIMIT_NOFILE
  505. * </PRE>
  506. * @param limit Value to set the limit to.
  507. */
  508. APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr,
  509. apr_int32_t what,
  510. struct rlimit *limit);
  511. #endif
  512. /**
  513. * Specify an error function to be called in the child process if APR
  514. * encounters an error in the child prior to running the specified program.
  515. * @param attr The procattr describing the child process to be created.
  516. * @param errfn The function to call in the child process.
  517. * @remark At the present time, it will only be called from apr_proc_create()
  518. * on platforms where fork() is used. It will never be called on other
  519. * platforms, on those platforms apr_proc_create() will return the error
  520. * in the parent process rather than invoke the callback in the now-forked
  521. * child process.
  522. */
  523. APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
  524. apr_child_errfn_t *errfn);
  525. /**
  526. * Specify that apr_proc_create() should do whatever it can to report
  527. * failures to the caller of apr_proc_create(), rather than find out in
  528. * the child.
  529. * @param attr The procattr describing the child process to be created.
  530. * @param chk Flag to indicate whether or not extra work should be done
  531. * to try to report failures to the caller.
  532. * @remark This flag only affects apr_proc_create() on platforms where
  533. * fork() is used. This leads to extra overhead in the calling
  534. * process, but that may help the application handle such
  535. * errors more gracefully.
  536. */
  537. APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
  538. apr_int32_t chk);
  539. /**
  540. * Determine if the child should start in its own address space or using the
  541. * current one from its parent
  542. * @param attr The procattr we care about.
  543. * @param addrspace Should the child start in its own address space? Default
  544. * is no on NetWare and yes on other platforms.
  545. */
  546. APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
  547. apr_int32_t addrspace);
  548. /**
  549. * Set the username used for running process
  550. * @param attr The procattr we care about.
  551. * @param username The username used
  552. * @param password User password if needed. Password is needed on WIN32
  553. * or any other platform having
  554. * APR_PROCATTR_USER_SET_REQUIRES_PASSWORD set.
  555. */
  556. APR_DECLARE(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
  557. const char *username,
  558. const char *password);
  559. /**
  560. * Set the group used for running process
  561. * @param attr The procattr we care about.
  562. * @param groupname The group name used
  563. */
  564. APR_DECLARE(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
  565. const char *groupname);
  566. /**
  567. * Register permission set function
  568. * @param attr The procattr we care about.
  569. * @param perms_set_fn Permission set callback
  570. * @param data Data to pass to permission callback function
  571. * @param perms Permissions to set
  572. */
  573. APR_DECLARE(apr_status_t) apr_procattr_perms_set_register(apr_procattr_t *attr,
  574. apr_perms_setfn_t *perms_set_fn,
  575. void *data,
  576. apr_fileperms_t perms);
  577. #if APR_HAS_FORK
  578. /**
  579. * This is currently the only non-portable call in APR. This executes
  580. * a standard unix fork.
  581. * @param proc The resulting process handle.
  582. * @param cont The pool to use.
  583. * @remark returns APR_INCHILD for the child, and APR_INPARENT for the parent
  584. * or an error.
  585. */
  586. APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *cont);
  587. #endif
  588. /**
  589. * Create a new process and execute a new program within that process.
  590. * @param new_proc The resulting process handle.
  591. * @param progname The program to run
  592. * @param args the arguments to pass to the new program. The first
  593. * one should be the program name.
  594. * @param env The new environment table for the new process. This
  595. * should be a list of NULL-terminated strings. This argument
  596. * is ignored for APR_PROGRAM_ENV, APR_PROGRAM_PATH, and
  597. * APR_SHELLCMD_ENV types of commands.
  598. * @param attr the procattr we should use to determine how to create the new
  599. * process
  600. * @param pool The pool to use.
  601. * @note This function returns without waiting for the new process to terminate;
  602. * use apr_proc_wait for that.
  603. */
  604. APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new_proc,
  605. const char *progname,
  606. const char * const *args,
  607. const char * const *env,
  608. apr_procattr_t *attr,
  609. apr_pool_t *pool);
  610. /**
  611. * Wait for a child process to die
  612. * @param proc The process handle that corresponds to the desired child process
  613. * @param exitcode The returned exit status of the child, if a child process
  614. * dies, or the signal that caused the child to die.
  615. * On platforms that don't support obtaining this information,
  616. * the status parameter will be returned as APR_ENOTIMPL.
  617. * @param exitwhy Why the child died, the bitwise or of:
  618. * <PRE>
  619. * APR_PROC_EXIT -- process terminated normally
  620. * APR_PROC_SIGNAL -- process was killed by a signal
  621. * APR_PROC_SIGNAL_CORE -- process was killed by a signal, and
  622. * generated a core dump.
  623. * </PRE>
  624. * @param waithow How should we wait. One of:
  625. * <PRE>
  626. * APR_WAIT -- block until the child process dies.
  627. * APR_NOWAIT -- return immediately regardless of if the
  628. * child is dead or not.
  629. * </PRE>
  630. * @remark The child's status is in the return code to this process. It is one of:
  631. * <PRE>
  632. * APR_CHILD_DONE -- child is no longer running.
  633. * APR_CHILD_NOTDONE -- child is still running.
  634. * </PRE>
  635. */
  636. APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
  637. int *exitcode, apr_exit_why_e *exitwhy,
  638. apr_wait_how_e waithow);
  639. /**
  640. * Wait for any current child process to die and return information
  641. * about that child.
  642. * @param proc Pointer to NULL on entry, will be filled out with child's
  643. * information
  644. * @param exitcode The returned exit status of the child, if a child process
  645. * dies, or the signal that caused the child to die.
  646. * On platforms that don't support obtaining this information,
  647. * the status parameter will be returned as APR_ENOTIMPL.
  648. * @param exitwhy Why the child died, the bitwise or of:
  649. * <PRE>
  650. * APR_PROC_EXIT -- process terminated normally
  651. * APR_PROC_SIGNAL -- process was killed by a signal
  652. * APR_PROC_SIGNAL_CORE -- process was killed by a signal, and
  653. * generated a core dump.
  654. * </PRE>
  655. * @param waithow How should we wait. One of:
  656. * <PRE>
  657. * APR_WAIT -- block until the child process dies.
  658. * APR_NOWAIT -- return immediately regardless of if the
  659. * child is dead or not.
  660. * </PRE>
  661. * @param p Pool to allocate child information out of.
  662. * @bug Passing proc as a *proc rather than **proc was an odd choice
  663. * for some platforms... this should be revisited in 1.0
  664. */
  665. APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
  666. int *exitcode,
  667. apr_exit_why_e *exitwhy,
  668. apr_wait_how_e waithow,
  669. apr_pool_t *p);
  670. #define APR_PROC_DETACH_FOREGROUND 0 /**< Do not detach */
  671. #define APR_PROC_DETACH_DAEMONIZE 1 /**< Detach */
  672. /**
  673. * Detach the process from the controlling terminal.
  674. * @param daemonize set to non-zero if the process should daemonize
  675. * and become a background process, else it will
  676. * stay in the foreground.
  677. */
  678. APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize);
  679. /**
  680. * Register an other_child -- a child associated to its registered
  681. * maintence callback. This callback is invoked when the process
  682. * dies, is disconnected or disappears.
  683. * @param proc The child process to register.
  684. * @param maintenance maintenance is a function that is invoked with a
  685. * reason and the data pointer passed here.
  686. * @param data Opaque context data passed to the maintenance function.
  687. * @param write_fd An fd that is probed for writing. If it is ever unwritable
  688. * then the maintenance is invoked with reason
  689. * OC_REASON_UNWRITABLE.
  690. * @param p The pool to use for allocating memory.
  691. * @bug write_fd duplicates the proc->out stream, it's really redundant
  692. * and should be replaced in the APR 1.0 API with a bitflag of which
  693. * proc->in/out/err handles should be health checked.
  694. * @bug no platform currently tests the pipes health.
  695. */
  696. APR_DECLARE(void) apr_proc_other_child_register(apr_proc_t *proc,
  697. void (*maintenance) (int reason,
  698. void *,
  699. int status),
  700. void *data, apr_file_t *write_fd,
  701. apr_pool_t *p);
  702. /**
  703. * Stop watching the specified other child.
  704. * @param data The data to pass to the maintenance function. This is
  705. * used to find the process to unregister.
  706. * @warning Since this can be called by a maintenance function while we're
  707. * scanning the other_children list, all scanners should protect
  708. * themself by loading ocr->next before calling any maintenance
  709. * function.
  710. */
  711. APR_DECLARE(void) apr_proc_other_child_unregister(void *data);
  712. /**
  713. * Notify the maintenance callback of a registered other child process
  714. * that application has detected an event, such as death.
  715. * @param proc The process to check
  716. * @param reason The reason code to pass to the maintenance function
  717. * @param status The status to pass to the maintenance function
  718. * @remark An example of code using this behavior;
  719. * <pre>
  720. * rv = apr_proc_wait_all_procs(&proc, &exitcode, &status, APR_WAIT, p);
  721. * if (APR_STATUS_IS_CHILD_DONE(rv)) {
  722. * \#if APR_HAS_OTHER_CHILD
  723. * if (apr_proc_other_child_alert(&proc, APR_OC_REASON_DEATH, status)
  724. * == APR_SUCCESS) {
  725. * ; (already handled)
  726. * }
  727. * else
  728. * \#endif
  729. * [... handling non-otherchild processes death ...]
  730. * </pre>
  731. */
  732. APR_DECLARE(apr_status_t) apr_proc_other_child_alert(apr_proc_t *proc,
  733. int reason,
  734. int status);
  735. /**
  736. * Test one specific other child processes and invoke the maintenance callback
  737. * with the appropriate reason code, if still running, or the appropriate reason
  738. * code if the process is no longer healthy.
  739. * @param ocr The registered other child
  740. * @param reason The reason code (e.g. APR_OC_REASON_RESTART) if still running
  741. */
  742. APR_DECLARE(void) apr_proc_other_child_refresh(apr_other_child_rec_t *ocr,
  743. int reason);
  744. /**
  745. * Test all registered other child processes and invoke the maintenance callback
  746. * with the appropriate reason code, if still running, or the appropriate reason
  747. * code if the process is no longer healthy.
  748. * @param reason The reason code (e.g. APR_OC_REASON_RESTART) to running processes
  749. */
  750. APR_DECLARE(void) apr_proc_other_child_refresh_all(int reason);
  751. /**
  752. * Terminate a process.
  753. * @param proc The process to terminate.
  754. * @param sig How to kill the process.
  755. */
  756. APR_DECLARE(apr_status_t) apr_proc_kill(apr_proc_t *proc, int sig);
  757. /**
  758. * Register a process to be killed when a pool dies.
  759. * @param a The pool to use to define the processes lifetime
  760. * @param proc The process to register
  761. * @param how How to kill the process, one of:
  762. * <PRE>
  763. * APR_KILL_NEVER -- process is never sent any signals
  764. * APR_KILL_ALWAYS -- process is sent SIGKILL on apr_pool_t cleanup
  765. * APR_KILL_AFTER_TIMEOUT -- SIGTERM, wait 3 seconds, SIGKILL
  766. * APR_JUST_WAIT -- wait forever for the process to complete
  767. * APR_KILL_ONLY_ONCE -- send SIGTERM and then wait
  768. * </PRE>
  769. */
  770. APR_DECLARE(void) apr_pool_note_subprocess(apr_pool_t *a, apr_proc_t *proc,
  771. apr_kill_conditions_e how);
  772. #if APR_HAS_THREADS
  773. #if (APR_HAVE_SIGWAIT || APR_HAVE_SIGSUSPEND) && !defined(OS2)
  774. /**
  775. * Setup the process for a single thread to be used for all signal handling.
  776. * @warning This must be called before any threads are created
  777. */
  778. APR_DECLARE(apr_status_t) apr_setup_signal_thread(void);
  779. /**
  780. * Make the current thread listen for signals. This thread will loop
  781. * forever, calling a provided function whenever it receives a signal. That
  782. * functions should return 1 if the signal has been handled, 0 otherwise.
  783. * @param signal_handler The function to call when a signal is received
  784. * apr_status_t apr_signal_thread((int)(*signal_handler)(int signum))
  785. * @note Synchronous signals like SIGABRT/SIGSEGV/SIGBUS/... are ignored by
  786. * apr_signal_thread() and thus can't be waited by this function (they remain
  787. * handled by the operating system or its native signals interface).
  788. * @remark In APR version 1.6 and ealier, SIGUSR2 was part of these ignored
  789. * signals and thus was never passed in to the signal_handler. From APR 1.7
  790. * this is no more the case so SIGUSR2 can be handled in signal_handler and
  791. * acted upon like the other asynchronous signals.
  792. */
  793. APR_DECLARE(apr_status_t) apr_signal_thread(int(*signal_handler)(int signum));
  794. #endif /* (APR_HAVE_SIGWAIT || APR_HAVE_SIGSUSPEND) && !defined(OS2) */
  795. /**
  796. * Get the child-pool used by the thread from the thread info.
  797. * @return apr_pool_t the pool
  798. */
  799. APR_POOL_DECLARE_ACCESSOR(thread);
  800. #endif /* APR_HAS_THREADS */
  801. /** @} */
  802. #ifdef __cplusplus
  803. }
  804. #endif
  805. #endif /* ! APR_THREAD_PROC_H */