apr_thread_proc.inc 35 KB

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