arch/m68k-amiga: Define the gcc symbol 'start' instead of using .bss
[AROS.git] / compiler / clib / waitpid.c
blobe7c20d1f7b9a920ba8738c02840b6c90e5ff663e
1 /*
2 Copyright © 2004-2009, 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 <sys/wait.h>
15 #include <errno.h>
17 #include "etask.h"
18 #include "__arosc_privdata.h"
20 /*****************************************************************************
22 NAME */
24 pid_t waitpid(
26 /* SYNOPSIS */
27 pid_t pid,
28 int *status,
29 int options)
31 /* FUNCTION
32 Waits for child process with given process id to change state. State
33 change is one of the following events: child has exited, child was
34 terminated by a signal, child was stopped by a signal, child was
35 resumed by a signal.
37 The function stores status of the process that changed state in the
38 pointer given as status argument.
40 The following macros can be used to extract information from the
41 status value:
43 WIFEXITED(status) - true if the process has exited
44 WEXITSTATUS(status) - exit status of the exited process
45 WIFSIGNALED(status) - true if the child process was terminated by a
46 signal
47 WTERMSIG(status) - number of the signal that caused process
48 termination
49 WIFSTOPPED(status) - true if the child process was stopped by a
50 signal
51 WSTOPSIG(status) - number of the signal that caused child process
52 stop
53 WIFCONTINUED(status) - true if the child process was resumed by the
54 SIGCONT signal.
56 Unless WNOHANG option is set, parent process will be suspended until a
57 child changes state. If a child process has already changed state,
58 function returns immediately.
60 INPUTS
61 pid - Process id of the process you want to wait for or -1 to wait for
62 any child process
63 status - Pointer to int where child status will be stored or NULL if
64 you don't want to store status.
65 options - ORed zero or more of the following constants:
67 WNOHANG - return immediately if no child process changed state
69 RESULT
70 Process id of the child process on success or -1 on error. If an error
71 occurred, the global variable errno is set.
73 NOTES
74 This function will work only for child processeses notifying parent
75 process of their death, for example processes created by vfork() call.
76 If you want to use it for other processes, remember to set the
77 NP_NotifyOnDeath tag value to TRUE during child process creation.
79 EXAMPLE
81 BUGS
83 SEE ALSO
84 wait()
86 INTERNALS
87 Since POSIX signals are not yet implemented, WIFSIGNALED, WIFSTOPPED
88 and WIFCONTINUED macros always return 0. WIFEXITED always returns 1.
90 The et_UniqueID field of the ETask structure is used as process id.
92 ******************************************************************************/
94 struct ETask *et;
95 ULONG tid = pid;
96 pid_t ret = -1;
97 int exchildno;
99 D(bug("waitpid(%d, %p, %d)\n", pid, status, options));
101 et = GetETask(FindTask(NULL));
102 if(!et)
104 /* only ETasks are fertile */
105 errno = ECHILD;
106 return -1;
109 if (pid < -1 || pid == 0)
111 /* process groups not yet supported */
112 errno = EINVAL;
113 return -1;
116 if (pid == -1)
117 tid = 0;
119 if(tid != 0 && ChildStatus(tid) == CHILD_NOTFOUND)
121 /* error if there's no such child */
122 errno = ECHILD;
123 return -1;
126 if (options & ~WNOHANG)
128 /* option not yet supported */
129 errno = EINVAL;
130 return -1;
133 /* not very pretty, perhaps we need a function for counting dead
134 children? */
135 ListLength(&et->et_TaskMsgPort.mp_MsgList, exchildno);
136 if ((options & WNOHANG) && (
137 (tid != 0 && ChildStatus(tid) != CHILD_EXITED) ||
138 (tid == 0 && exchildno == 0)
141 D(bug("waitpid: no dead children\n"));
142 /* no dead children atm */
143 return 0;
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;