tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / compiler / posixc / waitpid.c
blob0d77c97aa2cecaf0be4616eadca8f20ac500a49d
1 /*
2 Copyright © 2004-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
8 #include <aros/debug.h>
9 #include <proto/exec.h>
10 #include <exec/lists.h>
11 #include <exec/tasks.h>
13 #include <errno.h>
15 /*****************************************************************************
17 NAME */
18 #include <sys/wait.h>
20 pid_t waitpid(
22 /* SYNOPSIS */
23 pid_t pid,
24 int *status,
25 int options)
27 /* FUNCTION
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
31 resumed by a signal.
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
37 status value:
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
42 signal
43 WTERMSIG(status) - number of the signal that caused process
44 termination
45 WIFSTOPPED(status) - true if the child process was stopped by a
46 signal
47 WSTOPSIG(status) - number of the signal that caused child process
48 stop
49 WIFCONTINUED(status) - true if the child process was resumed by the
50 SIGCONT signal.
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.
56 INPUTS
57 pid - Process id of the process you want to wait for or -1 to wait for
58 any child process
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
65 RESULT
66 Process id of the child process on success or -1 on error. If an error
67 occurred, the global variable errno is set.
69 NOTES
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.
75 EXAMPLE
77 BUGS
79 SEE ALSO
80 wait()
82 INTERNALS
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 ******************************************************************************/
90 struct ETask *et;
91 ULONG tid = pid;
92 pid_t ret = -1;
93 int exchildno;
95 D(bug("waitpid(%d, %p, %d)\n", pid, status, options));
97 et = GetETask(FindTask(NULL));
98 if(!et)
100 /* only ETasks are fertile */
101 errno = ECHILD;
102 return -1;
105 if (pid < -1 || pid == 0)
107 /* process groups not yet supported */
108 errno = EINVAL;
109 return -1;
112 if (pid == -1)
113 tid = 0;
115 if(tid != 0 && ChildStatus(tid) == CHILD_NOTFOUND)
117 /* error if there's no such child */
118 errno = ECHILD;
119 return -1;
122 if (options & ~WNOHANG)
124 /* option not yet supported */
125 errno = EINVAL;
126 return -1;
129 /* not very pretty, perhaps we need a function for counting dead
130 children? */
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 */
139 return 0;
142 D(bug("waitpid: wait for %d to die\n", tid));
143 et = (struct ETask *)ChildWait(tid);
144 if (et != (struct ETask *)CHILD_NOTNEW)
146 if(status)
148 *status = et->et_Result1;
150 ret = et->et_UniqueID;
151 ChildFree(et->et_UniqueID);
153 else
154 errno = ECHILD;
156 D(bug("waitpid: leaving (%d)\n", ret));
158 return ret;