1 /************************************************************************
2  * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
3  * provided to you without charge, and with no warranty.  You may give  *
4  * away copies of JOVE, including sources, provided that this notice is *
5  * included in all the files.                                           *
6  ************************************************************************/
7 
8 /* The diversity of process management is complicated and difficult to handle.
9  * - In some systems (noteably POSIX), a process id has type "pid_t"
10  * - V7 only has wait.  POSIX has waitpid (with options).  BSD has
11  *   wait3.  Someone has wait2.
12  * - The status result set by wait and used by WIF* has type
13  *   "union wait" in BSD, but "int" everywhere else.
14  * - The WIF* functions are defined in <sys/wait.h> by BSD and POSIX.
15  * - WTERMSIG seems to be a creation of POSIX
16  * - Some systems have vfork(1) and perform better if it is used
17  *   in place of fork(1).
18  *
19  * This header attempts to span this diversity.  We provide:
20  * - POSIX pid_t
21  * - wait_opt, to accept options (and use them, if possible).
22  * - wait_status_t
23  * - WIF*
24  * - WTERMSIG
25  */
26 
27 #ifdef POSIX_PROCS
28 
29 # include <sys/types.h>	/* defines pid_t */
30 # include <sys/wait.h>
31   typedef int	wait_status_t;
32 # define wait_opt(stat_loc, options)	waitpid(-1, stat_loc, options)
33 
34 #else /*!POSIX_PROCS*/
35 
36  typedef int	pid_t;
37 
38 # ifdef BSD_WAIT
39 
40 #  include <sys/wait.h>
41 
42   typedef union wait	wait_status_t;
43 
44 #  ifndef WEXITSTATUS
45 #   define WEXITSTATUS(w)	((w).w_retcode)
46 #  endif
47 
48 #  ifndef WTERMSIG
49 #   define WTERMSIG(w)	((w).w_termsig)
50 #  endif
51 
52 #  ifndef WAIT3
53 #   define wait_opt(stat_loc, options)	wait2(stat_loc, options)
54 #  else
55 #   define wait_opt(stat_loc, options)	wait3(stat_loc, options, (struct rusage *)NULL)
56 #  endif
57 
58 # else /*!BSD_WAIT*/
59 
60   typedef int	wait_status_t;
61 
62 #  ifdef UNIX
63 
64 #   define WIFSTOPPED(w)	((w & 0377) == 0177)
65 #   define WIFEXITED(w)	((w & 0377) == 0)
66 #   define WIFSIGNALED(w)	(((w >> 8) & 0377) == 0)
67 #   define WEXITSTATUS(w)	((w >> 8) & 0377)
68 #   define WTERMSIG(w)	(w & 0177)
69 
70 #   define wait_opt(stat_loc, options)		wait(stat_loc)
71 
72 #  endif /* UNIX */
73 
74 # endif /*!BSD_WAIT*/
75 #endif /*!POSIX_PROCS*/
76 
77 #ifndef FULL_UNISTD
78 # ifndef POSIX_UNISTD
79 
80 /* ??? pid_t may be changed by default argument promotions.
81  * If so, this prototype might be wrong.
82  */
83 extern int	kill proto((pid_t /*pid*/, int /*sig*/));	/* signal.h */
84 
85 extern pid_t	fork proto((void));
86 extern pid_t	getpid proto((void));
87 extern int	getuid proto((void));
88 extern int	setuid proto((int));
89 # endif /* !POSIX_UNISTD */
90 
91 # ifdef USE_VFORK
92 extern int	UNMACRO(vfork) proto((void));
93 # endif
94 #endif /* !FULL_UNISTD */
95 
96 /* This nest of #ifdefs is simply to define NEWPG() which makes
97  * the current process a process group leader of a new process group.
98  * ??? pid_t may be changed by default argument promotions.
99  * If so, this prototype might be wrong.
100  */
101 #ifdef POSIX_PROCS
102 # ifndef FULL_UNISTD
103    extern int	UNMACRO(setpgid) proto((pid_t /*pid*/, pid_t /*pgid*/));
104 # endif
105 # define NEWPG()	setpgid(0, getpid())
106 #else /* !POSIX_PROCS */
107 # ifdef BSD_SETPGRP
108 #  ifndef FULL_UNISTD
109    extern int	UNMACRO(setpgrp) proto((pid_t /*pid*/, pid_t /*pgrp*/));
110 #  endif
111 #  define NEWPG()	setpgrp(0, getpid())
112 # else /* !(defined(BSD_SETPGRP) || defined(POSIX_PROCS)) */
113 #  ifndef FULL_UNISTD
114    extern int	UNMACRO(setpgrp) proto((void));
115 #  endif
116 #  define NEWPG()	setpgrp()
117 # endif /* !(defined(BSD_SETPGRP) || defined(POSIX_PROCS)) */
118 #endif /* !POSIX_PROCS */
119