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