2 Copyright © 2004-2012, The AROS Development Team. All rights reserved.
8 #include <aros/debug.h>
9 #include <proto/exec.h>
10 #include <exec/lists.h>
11 #include <exec/tasks.h>
15 /*****************************************************************************
28 Waits for child process with given process id to change state. State
29 change is one of the following events: child has exited, child was
30 terminated by a signal, child was stopped by a signal, child was
33 The function stores status of the process that changed state in the
34 pointer given as status argument.
36 The following macros can be used to extract information from the
39 WIFEXITED(status) - true if the process has exited
40 WEXITSTATUS(status) - exit status of the exited process
41 WIFSIGNALED(status) - true if the child process was terminated by a
43 WTERMSIG(status) - number of the signal that caused process
45 WIFSTOPPED(status) - true if the child process was stopped by a
47 WSTOPSIG(status) - number of the signal that caused child process
49 WIFCONTINUED(status) - true if the child process was resumed by the
52 Unless WNOHANG option is set, parent process will be suspended until a
53 child changes state. If a child process has already changed state,
54 function returns immediately.
57 pid - Process id of the process you want to wait for or -1 to wait for
59 status - Pointer to int where child status will be stored or NULL if
60 you don't want to store status.
61 options - ORed zero or more of the following constants:
63 WNOHANG - return immediately if no child process changed state
66 Process id of the child process on success or -1 on error. If an error
67 occurred, the global variable errno is set.
70 This function will work only for child processeses notifying parent
71 process of their death, for example processes created by vfork() call.
72 If you want to use it for other processes, remember to set the
73 NP_NotifyOnDeath tag value to TRUE during child process creation.
83 Since POSIX signals are not yet implemented, WIFSIGNALED, WIFSTOPPED
84 and WIFCONTINUED macros always return 0. WIFEXITED always returns 1.
86 The et_UniqueID field of the ETask structure is used as process id.
88 ******************************************************************************/
95 D(bug("waitpid(%d, %p, %d)\n", pid
, status
, options
));
97 et
= GetETask(FindTask(NULL
));
100 /* only ETasks are fertile */
105 if (pid
< -1 || pid
== 0)
107 /* process groups not yet supported */
115 if(tid
!= 0 && ChildStatus(tid
) == CHILD_NOTFOUND
)
117 /* error if there's no such child */
122 if (options
& ~WNOHANG
)
124 /* option not yet supported */
129 /* not very pretty, perhaps we need a function for counting dead
131 ListLength(&et
->et_TaskMsgPort
.mp_MsgList
, exchildno
);
132 if ((options
& WNOHANG
) && (
133 (tid
!= 0 && ChildStatus(tid
) != CHILD_EXITED
) ||
134 (tid
== 0 && exchildno
== 0)
137 D(bug("waitpid: no dead children\n"));
138 /* no dead children atm */
142 D(bug("waitpid: wait for %d to die\n", tid
));
143 et
= (struct ETask
*)ChildWait(tid
);
144 if (et
!= (struct ETask
*)CHILD_NOTNEW
)
148 *status
= et
->et_Result1
;
150 ret
= et
->et_UniqueID
;
151 ChildFree(et
->et_UniqueID
);
156 D(bug("waitpid: leaving (%d)\n", ret
));