Debian Codegraph @

compat/waitpid.c

raw
1 /*
2 * waitpid.c --
3 *
4 * This procedure emulates the POSIX waitpid kernel call on BSD systems
5 * that don't have waitpid but do have wait3. This code is based on a
6 * prototype version written by Mark Diekhans and Karl Lehenbauer.
7 *
8 * Copyright (c) 1993 The Regents of the University of California.
9 * Copyright (c) 1994 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution of
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 */
14
15 #include "tclPort.h"
16
17 #ifndef pid_t
18 #define pid_t int
19 #endif
20
21 /*
22 * A linked list of the following structures is used to keep track of
23 * processes for which we received notification from the kernel, but the
24 * application hasn't waited for them yet (this can happen because wait may
25 * not return the process we really want). We save the information here until
26 * the application finally does wait for the process.
27 */
28
29 typedef struct WaitInfo {
30 pid_t pid; /* Pid of process that exited. */
31 WAIT_STATUS_TYPE status; /* Status returned when child exited or
32 * suspended. */
33 struct WaitInfo *nextPtr; /* Next in list of exited processes. */
34 } WaitInfo;
35
36 static WaitInfo *deadList = NULL;
37 /* First in list of all dead processes. */
38
39 /*
40 *----------------------------------------------------------------------
41 *
42 * waitpid --
43 *
44 * This procedure emulates the functionality of the POSIX waitpid kernel
45 * call, using the BSD wait3 kernel call. Note: it doesn't emulate
46 * absolutely all of the waitpid functionality, in that it doesn't
47 * support pid's of 0 or < -1.
48 *
49 * Results:
50 * -1 is returned if there is an error in the wait kernel call. Otherwise
51 * the pid of an exited or suspended process is returned and *statusPtr
52 * is set to the status value of the process.
53 *
54 * Side effects:
55 * None.
56 *
57 *----------------------------------------------------------------------
58 */
59
60 #ifdef waitpid
61 # undef waitpid
62 #endif
63
64 pid_t
65 waitpid(
66 pid_t pid, /* The pid to wait on. Must be -1 or greater
67 * than zero. */
68 int *statusPtr, /* Where to store wait status for the
69 * process. */
70 int options) /* OR'ed combination of WNOHANG and
71 * WUNTRACED. */
72 {
73 WaitInfo *waitPtr, *prevPtr;
74 pid_t result;
75 WAIT_STATUS_TYPE status;
76
77 if ((pid < -1) || (pid == 0)) {
78 errno = EINVAL;
79 return -1;
80 }
81
82 /*
83 * See if there's a suitable process that has already stopped or exited.
84 * If so, remove it from the list of exited processes and return its
85 * information.
86 */
87
88 for (waitPtr = deadList, prevPtr = NULL; waitPtr != NULL;
89 prevPtr = waitPtr, waitPtr = waitPtr->nextPtr) {
90 if ((pid != waitPtr->pid) && (pid != -1)) {
91 continue;
92 }
93 if (!(options & WUNTRACED) && (WIFSTOPPED(waitPtr->status))) {
94 continue;
95 }
96 result = waitPtr->pid;
97 *statusPtr = *((int *) &waitPtr->status);
98 if (prevPtr == NULL) {
99 deadList = waitPtr->nextPtr;
100 } else {
101 prevPtr->nextPtr = waitPtr->nextPtr;
102 }
103 ckfree((char *) waitPtr);
104 return result;
105 }
106
107 /*
108 * Wait for any process to stop or exit. If it's an acceptable one then
109 * return it to the caller; otherwise store information about it in the
110 * list of exited processes and try again. On systems that have only wait
111 * but not wait3, there are several situations we can't handle, but we do
112 * the best we can (e.g. can still handle some combinations of options by
113 * invoking wait instead of wait3).
114 */
115
116 while (1) {
117 #if NO_WAIT3
118 if (options & WNOHANG) {
119 return 0;
120 }
121 if (options != 0) {
122 errno = EINVAL;
123 return -1;
124 }
125 result = wait(&status);
126 #else
127 result = wait3(&status, options, 0);
128 #endif
129 if ((result == -1) && (errno == EINTR)) {
130 continue;
131 }
132 if (result <= 0) {
133 return result;
134 }
135
136 if ((pid != result) && (pid != -1)) {
137 goto saveInfo;
138 }
139 if (!(options & WUNTRACED) && (WIFSTOPPED(status))) {
140 goto saveInfo;
141 }
142 *statusPtr = *((int *) &status);
143 return result;
144
145 /*
146 * Can't return this info to caller. Save it in the list of stopped or
147 * exited processes. Tricky point: first check for an existing entry
148 * for the process and overwrite it if it exists (e.g. a previously
149 * stopped process might now be dead).
150 */
151
152 saveInfo:
153 for (waitPtr = deadList; waitPtr != NULL; waitPtr = waitPtr->nextPtr) {
154 if (waitPtr->pid == result) {
155 waitPtr->status = status;
156 goto waitAgain;
157 }
158 }
159 waitPtr = (WaitInfo *) attemptckalloc(sizeof(WaitInfo));
160 if (!waitPtr) {
161 errno = ENOMEM;
162 return -1;
163 }
164 waitPtr->pid = result;
165 waitPtr->status = status;
166 waitPtr->nextPtr = deadList;
167 deadList = waitPtr;
168
169 waitAgain:
170 continue;
171 }
172 }