Cygwin: strptime: add release note
[newlib-cygwin.git] / winsup / cygwin / wait.cc
blob719547761ded7d6c15b222a23c7a08c649562040
1 /* wait.cc: Posix wait routines.
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #include "winsup.h"
10 #include <sys/wait.h>
11 #include "sigproc.h"
12 #include "thread.h"
13 #include "cygtls.h"
14 #include "cygwait.h"
16 /* This is called _wait and not wait because the real wait is defined
17 in libc/syscalls/syswait.c. It calls us. */
19 extern "C" pid_t
20 wait (int *status)
22 return wait4 (-1, status, 0, NULL);
25 extern "C" pid_t
26 waitpid (pid_t intpid, int *status, int options)
28 return wait4 (intpid, status, options, NULL);
31 extern "C" pid_t
32 wait3 (int *status, int options, struct rusage *r)
34 return wait4 (-1, status, options, r);
37 /* Wait for any child to complete.
38 * Note: this is not thread safe. Use of wait in multiple threads will
39 * not work correctly.
42 extern "C" pid_t
43 wait4 (int intpid, int *status, int options, struct rusage *r)
45 int res;
46 HANDLE waitfor;
47 waitq *w = &_my_tls.wq;
49 pthread_testcancel ();
51 while (1)
53 sig_dispatch_pending ();
54 if (options & ~(WNOHANG | WUNTRACED | WCONTINUED))
56 set_errno (EINVAL);
57 res = -1;
58 break;
61 if (r)
62 memset (r, 0, sizeof (*r));
64 w->pid = intpid;
65 w->options = options;
66 w->rusage = r;
67 sigproc_printf ("calling proc_subproc, pid %d, options %d",
68 w->pid, w->options);
69 if (!proc_subproc (PROC_WAIT, (uintptr_t) w))
71 set_errno (ENOSYS);
72 paranoid_printf ("proc_subproc returned 0");
73 res = -1;
74 break;
77 if ((waitfor = w->ev) == NULL)
78 goto nochildren;
80 res = cygwait (waitfor, cw_infinite, cw_cancel | cw_cancel_self);
82 sigproc_printf ("%d = cygwait (...)", res);
84 if (w->ev == NULL)
86 nochildren:
87 /* found no children */
88 set_errno (ECHILD);
89 res = -1;
90 break;
93 if (w->status == -1)
95 if (_my_tls.call_signal_handler ())
96 continue;
97 set_sig_errno (EINTR);
98 res = -1;
100 else if (res != WAIT_OBJECT_0)
102 set_errno (EINVAL);
103 res = -1;
105 else if ((res = w->pid) != 0 && status)
106 *status = w->status;
107 break;
110 syscall_printf ("%R = wait4(%d, %y, %d, %p)", res, intpid, w->status, options, r);
111 w->status = -1;
112 return res;