2 Copyright (C) 2004, The AROS Development Team. All rights reserved.
8 #include <aros/debug.h>
9 #include <proto/exec.h>
10 #include <exec/lists.h>
11 #include <aros/startup.h>
13 #include <sys/types.h>
19 /*****************************************************************************
31 Waits for child process with given process id to change state. State
32 change is one of the following events: child has exited, child was
33 terminated by a signal, child was stopped by a signal, child was
36 The function stores status of the process that changed state in the
37 pointer given as status argument.
39 The following macros can be used to extract information from the
42 WIFEXITED(status) - true if the process has exited
43 WEXITSTATUS(status) - exit status of the exited process
44 WIFSIGNALED(status) - true if the child process was terminated by a
46 WTERMSIG(status) - number of the signal that caused process
48 WIFSTOPPED(status) - true if the child process was stopped by a
50 WSTOPSIG(status) - number of the signal that caused child process
52 WIFCONTINUED(status) - true if the child process was resumed by the
55 Unless WNOHANG option is set, parent process will be suspended until a
56 child changes state. If a child process has already changed state,
57 function returns immediately.
60 pid - Process id of the process you want to wait for or -1 to wait for
62 status - Pointer to int where child status will be stored or NULL if
63 you don't want to store status.
64 options - ORed zero or more of the following constants:
66 WNOHANG - return immediately if no child process changed state
69 Process id of the child process on success or -1 on error. If an error
70 occurred, the global variable errno is set.
73 This function will work only for child processeses notifying parent
74 process of their death, for example processes created by vfork() call.
75 If you want to use it for other processes, remember to set the
76 NP_NotifyOnDeath tag value to TRUE during child process creation.
86 Since POSIX signals are not yet implemented, WIFSIGNALED, WIFSTOPPED
87 and WIFCONTINUED macros always return 0. WIFEXITED always returns 1.
89 The et_UniqueID field of the ETask structure is used as process id.
91 ******************************************************************************/
94 APTR tid
= (APTR
) pid
;
98 D(bug("waitpid(%d, %p, %d)\n", pid
, status
, options
));
100 et
= GetETask(FindTask(NULL
));
103 /* only ETasks are fertile */
108 if (pid
< -1 || pid
== 0)
110 /* process groups not yet supported */
118 if(tid
!= 0 && ChildStatus(tid
) == CHILD_NOTFOUND
)
120 /* error if there's no such child */
125 if (options
& ~WNOHANG
)
127 /* option not yet supported */
132 /* not very pretty, perhaps we need a function for counting dead
134 ListLength(&et
->et_TaskMsgPort
.mp_MsgList
, exchildno
);
135 if ((options
& WNOHANG
) && (
136 (tid
!= 0 && ChildStatus(tid
) != CHILD_EXITED
) ||
137 (tid
== 0 && exchildno
== 0)
140 /* no dead children atm */
144 et
= (struct ETask
*)ChildWait(tid
);
145 if (et
!= CHILD_NOTNEW
)
147 if(status
&& IntETask(et
)->iet_startup
)
149 struct aros_startup
*startup
= IntETask(et
)->iet_startup
;
150 *status
= startup
->as_startup_error
;
152 ret
= et
->et_UniqueID
;
153 ChildFree(et
->et_UniqueID
);