newlib-cygwin/winsup/cygwin/wait.cc

123 lines
2.6 KiB
C++
Raw Normal View History

2000-02-18 03:38:33 +08:00
/* wait.cc: Posix wait routines.
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 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. */
#include "winsup.h"
2000-02-18 03:38:33 +08:00
#include <sys/wait.h>
#include <stdlib.h>
#include "cygerrno.h"
#include "sigproc.h"
#include "perthread.h"
2003-01-15 04:13:09 +08:00
#include "thread.h"
Rename _threadinfo to _cygtls, throughout. * cygtls.h (_cygtls::call_signal_handler): Rename from call_signal_handler_now. (_cygtls::push): Make second argument mandatory. (_cygtls::fixup_after_fork): Declare new function. (_cygtls::lock): Ditto. * cygtls.cc (_cygtls::fixup_after_fork): Define new function. * dcrt0.cc (cygwin_finished_initializing): Define as bool. (alloc_stack): Use _tlstop rather than arbitrary variable in probably vain attempt to avoid strange fork problem on CTRL-C. (dll_crt0_0): Remove obsolete winpids::init call. * dll_init.cc (dll_dllcrt0): Detect forkee condition as equivalent to initializing. * winsup.h (cygwin_finished_initializing): Declare as bool. * exceptions.cc (handle_exceptions): Rely on cygwin_finished_initializing to determine how to handle exception during process startup. (_cygtls::call_signal_handler): Rename from call_signal_handler_now. (_cygtls::interrupt_now): Fill in second argument to push. (signal_fixup_after_fork): Eliminate. (setup_handler): Initialize locked to avoid potential inappropriate unlock. Resume thread if it has acquired the stack lock. (ctrl_c_handler): Just exit if ctrl-c is hit before cygiwn has finished initializing. * fork.cc (sync_with_child): Don't call abort since it can cause exit deadlocks. (sync_with_child): Change debugging output slightly. (fork_child): Set cygwin_finished_initializing here. Call _cygtls fork fixup and explicitly call sigproc_init. (fork_parent): Release malloc lock on fork failure. (vfork): Call signal handler via _my_tls. * sigproc.cc (sig_send): Ditto. * syscalls.cc (readv): Ditto. * termios.cc (tcsetattr): Ditto. * wait.cc (wait4): Ditto. * signal.cc (nanosleep): Ditto. (abort): Ditto. (kill_pgrp): Avoid killing self if exiting. * sync.cc (muto::acquire): Remove (temporarily?) ill-advised exiting_thread check. * gendef (_sigfe): Be more agressive in protecting stack pointer from other access by signal thread. (_cygtls::locked): Define new function. (_sigbe): Ditto. (_cygtls::pop): Protect edx. (_cygtls::lock): Use guaranteed method to set eax to 1. (longjmp): Aggressively protect signal stack. * miscfuncs.cc (low_priority_sleep): Reduce "sleep time" for secs == 0. * pinfo.cc (winpids::set): Counterintuitively use malloc's lock to protect simultaneous access to the pids list since there are pathological conditions which can cause malloc to call winpid. (winpids::init): Eliminate. * pinfo.h (winpids::cs): Eliminate declaration. * pinfo.h (winpids::init): Eliminate definition.
2004-02-12 11:01:58 +08:00
#include "cygtls.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
wait (int *status)
2000-02-18 03:38:33 +08:00
{
return wait4 (-1, status, 0, NULL);
}
extern "C" pid_t
2000-02-18 03:38:33 +08:00
waitpid (pid_t intpid, int *status, int options)
{
return wait4 (intpid, status, options, NULL);
}
extern "C" pid_t
2000-02-18 03:38:33 +08: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.
*/
extern "C" pid_t
2000-02-18 03:38:33 +08:00
wait4 (int intpid, int *status, int options, struct rusage *r)
{
int res;
2000-02-18 03:38:33 +08:00
HANDLE waitfor;
waitq *w = &_my_tls.wq;
2000-02-18 03:38:33 +08:00
2003-01-15 04:13:09 +08:00
pthread_testcancel ();
while (1)
2000-02-18 03:38:33 +08:00
{
sig_dispatch_pending ();
if (options & ~(WNOHANG | WUNTRACED))
{
set_errno (EINVAL);
res = -1;
break;
}
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",
w->pid, w->options);
if (!proc_subproc (PROC_WAIT, (DWORD) w))
{
set_errno (ENOSYS);
paranoid_printf ("proc_subproc returned 0");
res = -1;
break;
}
if ((waitfor = w->ev) == NULL)
goto nochildren;
2003-01-15 04:13:09 +08:00
res = pthread::cancelable_wait (waitfor, INFINITE);
sigproc_printf ("%d = WaitForSingleObject (...)", res);
if (w->ev == NULL)
{
nochildren:
/* found no children */
set_errno (ECHILD);
res = -1;
break;
}
if (w->status == -1)
{
if (_my_tls.call_signal_handler ())
continue;
set_sig_errno (EINTR);
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;
break;
2000-02-18 03:38:33 +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;
if (res < 0)
2001-03-19 05:11:25 +08:00
sigproc_printf ("*** errno = %d", get_errno ());
return res;
2000-02-18 03:38:33 +08:00
}