2000-02-18 03:38:33 +08:00
|
|
|
/* wait.cc: Posix wait routines.
|
|
|
|
|
2003-01-15 18:21:23 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
|
2000-02-18 03:38:33 +08: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-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "perthread.h"
|
2003-01-15 04:13:09 +08:00
|
|
|
#include "thread.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* This is called _wait and not wait because the real wait is defined
|
|
|
|
in libc/syscalls/syswait.c. It calls us. */
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
pid_t
|
2002-10-21 09:00:58 +08:00
|
|
|
wait (int *status)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
return wait4 (-1, status, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
waitpid (pid_t intpid, int *status, int options)
|
|
|
|
{
|
|
|
|
return wait4 (intpid, status, options, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
wait4 (int intpid, int *status, int options, struct rusage *r)
|
|
|
|
{
|
2001-01-16 10:29:47 +08:00
|
|
|
int res;
|
2000-02-18 03:38:33 +08:00
|
|
|
waitq *w;
|
|
|
|
HANDLE waitfor;
|
2001-01-17 22:57:09 +08:00
|
|
|
bool sawsig;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-01-15 04:13:09 +08:00
|
|
|
pthread_testcancel ();
|
|
|
|
|
2001-04-01 08:06:17 +08:00
|
|
|
while (1)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-11-24 04:24:08 +08:00
|
|
|
sig_dispatch_pending (0);
|
2001-04-01 08:06:17 +08:00
|
|
|
sigframe thisframe (mainthread);
|
|
|
|
sawsig = 0;
|
|
|
|
if (options & ~(WNOHANG | WUNTRACED))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
memset (r, 0, sizeof (*r));
|
|
|
|
|
|
|
|
if ((w = (waitq *) waitq_storage.get ()) == NULL)
|
|
|
|
w = (waitq *) waitq_storage.create ();
|
|
|
|
|
|
|
|
w->pid = intpid;
|
|
|
|
w->options = options;
|
|
|
|
w->rusage = r;
|
|
|
|
sigproc_printf ("calling proc_subproc, pid %d, options %d",
|
|
|
|
w->pid, w->options);
|
|
|
|
if (!proc_subproc (PROC_WAIT, (DWORD)w))
|
|
|
|
{
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
paranoid_printf ("proc_subproc returned 0");
|
|
|
|
res = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((waitfor = w->ev) == NULL)
|
|
|
|
goto nochildren;
|
|
|
|
|
2003-01-15 04:13:09 +08:00
|
|
|
res = pthread::cancelable_wait (waitfor, INFINITE);
|
2001-04-01 08:06:17 +08:00
|
|
|
|
|
|
|
sigproc_printf ("%d = WaitForSingleObject (...)", res);
|
|
|
|
|
|
|
|
if (w->ev == NULL)
|
|
|
|
{
|
|
|
|
nochildren:
|
|
|
|
/* found no children */
|
|
|
|
set_errno (ECHILD);
|
|
|
|
res = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w->status == -1)
|
|
|
|
{
|
|
|
|
set_sig_errno (EINTR);
|
|
|
|
sawsig = 1;
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else if (res != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
/* We shouldn't set errno to any random value if we can help it.
|
|
|
|
See the Posix manual for a list of valid values for `errno'. */
|
|
|
|
set_errno (EINVAL);
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else if ((res = w->pid) != 0 && status)
|
|
|
|
*status = w->status;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (!sawsig || !thisframe.call_signal_handler ())
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-01-16 10:29:47 +08:00
|
|
|
sigproc_printf ("intpid %d, status %p, w->status %d, options %d, res %d",
|
|
|
|
intpid, status, w->status, options, res);
|
2000-02-18 03:38:33 +08:00
|
|
|
w->status = -1;
|
2001-01-16 10:29:47 +08:00
|
|
|
if (res < 0)
|
2001-03-19 05:11:25 +08:00
|
|
|
sigproc_printf ("*** errno = %d", get_errno ());
|
2001-01-16 10:29:47 +08:00
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|