2000-02-17 20:38:33 +01:00
|
|
|
/* wait.cc: Posix wait routines.
|
|
|
|
|
2011-08-03 18:40:48 +02:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
2012-06-17 22:50:24 +02:00
|
|
|
2005, 2009, 2011, 2012 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/wait.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sigproc.h"
|
2003-01-14 21:13:09 +01:00
|
|
|
#include "thread.h"
|
2004-02-12 04:01:58 +01:00
|
|
|
#include "cygtls.h"
|
2012-06-17 22:50:24 +02:00
|
|
|
#include "cygwait.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* This is called _wait and not wait because the real wait is defined
|
|
|
|
in libc/syscalls/syswait.c. It calls us. */
|
|
|
|
|
2003-03-09 21:10:25 +01:00
|
|
|
extern "C" pid_t
|
2002-10-21 03:00:58 +02:00
|
|
|
wait (int *status)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
return wait4 (-1, status, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2003-03-09 21:10:25 +01:00
|
|
|
extern "C" pid_t
|
2000-02-17 20:38:33 +01:00
|
|
|
waitpid (pid_t intpid, int *status, int options)
|
|
|
|
{
|
|
|
|
return wait4 (intpid, status, options, NULL);
|
|
|
|
}
|
|
|
|
|
2003-03-09 21:10:25 +01:00
|
|
|
extern "C" pid_t
|
2000-02-17 20:38:33 +01:00
|
|
|
wait3 (int *status, int options, struct rusage *r)
|
|
|
|
{
|
|
|
|
return wait4 (-1, status, options, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for any child to complete.
|
|
|
|
* Note: this is not thread safe. Use of wait in multiple threads will
|
|
|
|
* not work correctly.
|
|
|
|
*/
|
|
|
|
|
2003-03-09 21:10:25 +01:00
|
|
|
extern "C" pid_t
|
2000-02-17 20:38:33 +01:00
|
|
|
wait4 (int intpid, int *status, int options, struct rusage *r)
|
|
|
|
{
|
2001-01-16 03:29:47 +01:00
|
|
|
int res;
|
2000-02-17 20:38:33 +01:00
|
|
|
HANDLE waitfor;
|
2004-03-12 23:03:33 +01:00
|
|
|
waitq *w = &_my_tls.wq;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-01-14 21:13:09 +01:00
|
|
|
pthread_testcancel ();
|
|
|
|
|
2001-04-01 02:06:17 +02:00
|
|
|
while (1)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-08-19 06:10:42 +02:00
|
|
|
sig_dispatch_pending ();
|
2009-07-18 22:25:07 +02:00
|
|
|
if (options & ~(WNOHANG | WUNTRACED | WCONTINUED))
|
2001-04-01 02:06:17 +02:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
2004-03-12 23:03:33 +01:00
|
|
|
res = -1;
|
|
|
|
break;
|
2001-04-01 02:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
memset (r, 0, sizeof (*r));
|
|
|
|
|
|
|
|
w->pid = intpid;
|
|
|
|
w->options = options;
|
|
|
|
w->rusage = r;
|
|
|
|
sigproc_printf ("calling proc_subproc, pid %d, options %d",
|
2004-03-04 06:31:14 +01:00
|
|
|
w->pid, w->options);
|
2003-12-08 01:21:17 +01:00
|
|
|
if (!proc_subproc (PROC_WAIT, (DWORD) w))
|
2001-04-01 02:06:17 +02:00
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
paranoid_printf ("proc_subproc returned 0");
|
|
|
|
res = -1;
|
2004-03-12 23:03:33 +01:00
|
|
|
break;
|
2001-04-01 02:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((waitfor = w->ev) == NULL)
|
|
|
|
goto nochildren;
|
|
|
|
|
2012-06-19 02:52:59 +02:00
|
|
|
res = cancelable_wait (waitfor, LARGE_NULL, cw_cancel | cw_cancel_self);
|
2001-04-01 02:06:17 +02:00
|
|
|
|
2011-12-17 08:01:21 +01:00
|
|
|
sigproc_printf ("%d = cancelable_wait (...)", res);
|
2001-04-01 02:06:17 +02:00
|
|
|
|
|
|
|
if (w->ev == NULL)
|
|
|
|
{
|
|
|
|
nochildren:
|
|
|
|
/* found no children */
|
|
|
|
set_errno (ECHILD);
|
|
|
|
res = -1;
|
2004-03-12 23:03:33 +01:00
|
|
|
break;
|
2001-04-01 02:06:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (w->status == -1)
|
|
|
|
{
|
2004-03-12 23:03:33 +01:00
|
|
|
if (_my_tls.call_signal_handler ())
|
|
|
|
continue;
|
2001-04-01 02:06:17 +02:00
|
|
|
set_sig_errno (EINTR);
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else if (res != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else if ((res = w->pid) != 0 && status)
|
|
|
|
*status = w->status;
|
2004-03-12 23:03:33 +01:00
|
|
|
break;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 08:01:21 +01:00
|
|
|
syscall_printf ("%R = wait4(%d, %p, %d, %p)", res, intpid, w->status, options, r);
|
2000-02-17 20:38:33 +01:00
|
|
|
w->status = -1;
|
2001-01-16 03:29:47 +01:00
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|