apr_thread_proc.inc 34 KB

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