Back to index

courier  0.68.2
waitlib.h
Go to the documentation of this file.
00001 #ifndef       waitlib_h
00002 #define       waitlib_h
00003 
00004 /*
00005 ** Copyright 1998 - 1999 Double Precision, Inc.
00006 ** See COPYING for distribution information.
00007 */
00008 
00009 #if    HAVE_CONFIG_H
00010 #include      "config.h"
00011 #endif
00012 
00013 
00014 #include <sys/types.h>
00015 #if HAVE_SYS_WAIT_H
00016 #include <sys/wait.h>
00017 #endif
00018 #ifndef WEXITSTATUS
00019 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
00020 #endif
00021 #ifndef WIFEXITED
00022 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
00023 #endif
00024 
00025 /*
00026 ** Ok, wait() pecularities are handled by the following functions.  Except
00027 ** for them, nobody else should care about wait's quirks.
00028 **
00029 ** First, call signal(SIGCHLD) as usual to set up your signal handler.
00030 ** Within the signal handler, call wait_reap to reap one (or more)
00031 ** child processes.
00032 */
00033 
00034 void wait_reap( void (*)(pid_t, int), /* Called to process reaped child */
00035        RETSIGTYPE (*)(int));       /* Should point back to signal handler */
00036 
00037 /*
00038 ** Main program can call wait_block and wait_clear to temporarily
00039 ** suspend reaping while in a critical section.
00040 */
00041 
00042 void wait_block();
00043 void wait_clear(RETSIGTYPE (*)(int));     /* The signal handler */
00044 
00045 /*
00046 ** wait_restore should be called instead of signal(SIGCHLD, SIG_DFL)
00047 ** to restore the signal handler just before exiting.  It should also
00048 ** be called by any forked process.
00049 */
00050 
00051 void wait_restore();
00052 
00053 /*
00054 ** Sometimes the parent wants to wait for one child to terminate.
00055 ** call wait_forchild for that.  First, wait_block() must be called to
00056 ** suspend all asynchronous reaping.  Then, call wait_forchild.  Before
00057 ** wait_forchild returns, the reaper function is guaranteed to be called.
00058 ** Asynchronous reaping is still blocked upon exit, call wait_clear() to
00059 ** reenable it.
00060 */
00061 
00062 void wait_forchild( void (*)(pid_t, int), /* Reaper */
00063        RETSIGTYPE (*)(int));       /* Signal handler stub */
00064 
00065 /*
00066 ** wait_startchildren() is a convenient function to start a given number
00067 ** of child processes.  The function returns 0 for the parent process, >0
00068 ** for each child process, and < 0 if there was an error starting child
00069 ** processes.  pidptr points to a pointer to the array of started pids,
00070 ** which wait_startchildren initializes, if *pidptr is NULL, wait_startchildren
00071 ** mallocs this array.  If pidptr is NULL, wait_startchildren uses its own
00072 ** internal array.
00073 */
00074 
00075 int wait_startchildren(unsigned nchildren, pid_t **pidptr);
00076 
00077 /*
00078 ** wait_reforkchild() is used in conjunction with wait_startchildren's array,
00079 ** and is intended to be called from the wait handler.  It checks if the
00080 ** pid is one of the listed children, and, if so, reforks it.
00081 ** wait_reforkchild() returns < 0 if there was a problem reforking the child
00082 ** process, 0 if the child process started succesfully, or if the terminated
00083 ** pid is unknown, > 0 in the reforked process.
00084 */
00085 
00086 int wait_reforkchild(unsigned nchildren, pid_t *pidptr, pid_t pid);
00087 
00088 
00089 
00090 #endif