Added missing properties.
[AROS.git] / compiler / clib / waitpid.c
blob025d315b1acf2c5509258c1b5950233ce4760a51
1 /*
2 Copyright © 2004-2011, 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 <aros/startup.h>
13 #include <sys/types.h>
14 #include <errno.h>
16 #include "__arosc_privdata.h"
18 /*****************************************************************************
20 NAME */
21 #include <sys/wait.h>
23 pid_t waitpid(
25 /* SYNOPSIS */
26 pid_t pid,
27 int *status,
28 int options)
30 /* FUNCTION
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
34 resumed by a signal.
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
40 status value:
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
45 signal
46 WTERMSIG(status) - number of the signal that caused process
47 termination
48 WIFSTOPPED(status) - true if the child process was stopped by a
49 signal
50 WSTOPSIG(status) - number of the signal that caused child process
51 stop
52 WIFCONTINUED(status) - true if the child process was resumed by the
53 SIGCONT signal.
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.
59 INPUTS
60 pid - Process id of the process you want to wait for or -1 to wait for
61 any child process
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
68 RESULT
69 Process id of the child process on success or -1 on error. If an error
70 occurred, the global variable errno is set.
72 NOTES
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.
78 EXAMPLE
80 BUGS
82 SEE ALSO
83 wait()
85 INTERNALS
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 ******************************************************************************/
93 struct ETask *et;
94 ULONG tid = pid;
95 pid_t ret = -1;
96 int exchildno;
98 D(bug("waitpid(%d, %p, %d)\n", pid, status, options));
100 et = GetETask(FindTask(NULL));
101 if(!et)
103 /* only ETasks are fertile */
104 errno = ECHILD;
105 return -1;
108 if (pid < -1 || pid == 0)
110 /* process groups not yet supported */
111 errno = EINVAL;
112 return -1;
115 if (pid == -1)
116 tid = 0;
118 if(tid != 0 && ChildStatus(tid) == CHILD_NOTFOUND)
120 /* error if there's no such child */
121 errno = ECHILD;
122 return -1;
125 if (options & ~WNOHANG)
127 /* option not yet supported */
128 errno = EINVAL;
129 return -1;
132 /* not very pretty, perhaps we need a function for counting dead
133 children? */
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 D(bug("waitpid: no dead children\n"));
141 /* no dead children atm */
142 return 0;
145 D(bug("waitpid: wait for %d to die\n", tid));
146 et = (struct ETask *)ChildWait(tid);
147 if (et != (struct ETask *)CHILD_NOTNEW)
149 if(status)
151 *status = et->et_Result1;
153 ret = et->et_UniqueID;
154 ChildFree(et->et_UniqueID);
156 else
157 errno = ECHILD;
159 D(bug("waitpid: leaving (%d)\n", ret));
161 return ret;