2000-02-17 20:38:33 +01:00
|
|
|
/* sigproc.cc: inter/intra signal and sub process handler
|
|
|
|
|
2002-01-13 21:03:03 +01:00
|
|
|
Copyright 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
Written by Christopher Faylor <cgf@cygnus.com>
|
|
|
|
|
|
|
|
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 <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdlib.h>
|
2000-09-08 04:56:55 +02:00
|
|
|
#include <sys/cygwin.h>
|
2002-08-11 21:19:29 +02:00
|
|
|
#include <assert.h>
|
2003-09-01 04:05:32 +02:00
|
|
|
#include <sys/signal.h>
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sync.h"
|
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "dtable.h"
|
2000-09-06 23:03:10 +02:00
|
|
|
#include "cygheap.h"
|
2001-12-26 05:53:34 +01:00
|
|
|
#include "child_info_magic.h"
|
2001-09-15 02:47:44 +02:00
|
|
|
#define NEED_VFORK
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "perthread.h"
|
2000-09-07 18:23:51 +02:00
|
|
|
#include "shared_info.h"
|
2002-08-01 18:20:31 +02:00
|
|
|
#include "cygthread.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convenience defines
|
|
|
|
*/
|
|
|
|
#define WSSC 60000 // Wait for signal completion
|
|
|
|
#define WPSP 40000 // Wait for proc_subproc mutex
|
|
|
|
#define WSPX 20000 // Wait for wait_sig to terminate
|
|
|
|
#define WWSP 20000 // Wait for wait_subproc to terminate
|
|
|
|
|
|
|
|
#define TOTSIGS (NSIG + __SIGOFFSET)
|
|
|
|
|
|
|
|
#define wake_wait_subproc() SetEvent (events[0])
|
|
|
|
|
|
|
|
#define no_signals_available() (!hwait_sig || !sig_loop_wait)
|
|
|
|
|
2001-09-10 02:19:06 +02:00
|
|
|
#define NZOMBIES 256
|
2001-06-10 18:00:23 +02:00
|
|
|
|
2003-09-01 04:05:32 +02:00
|
|
|
static LONG local_sigtodo[TOTSIGS];
|
|
|
|
struct sigaction *global_sigs;
|
|
|
|
|
|
|
|
inline LONG *
|
|
|
|
getlocal_sigtodo (int sig)
|
2003-08-19 07:47:44 +02:00
|
|
|
{
|
|
|
|
return local_sigtodo + __SIGOFFSET + sig;
|
|
|
|
}
|
|
|
|
|
2003-09-01 04:05:32 +02:00
|
|
|
void __stdcall
|
|
|
|
sigalloc ()
|
|
|
|
{
|
|
|
|
cygheap->sigs = global_sigs =
|
|
|
|
(struct sigaction *) ccalloc (HEAP_SIGS, NSIG, sizeof (struct sigaction));
|
|
|
|
}
|
|
|
|
|
|
|
|
void __stdcall
|
|
|
|
signal_fixup_after_exec ()
|
|
|
|
{
|
|
|
|
global_sigs = cygheap->sigs;
|
|
|
|
/* Set up child's signal handlers */
|
|
|
|
for (int i = 0; i < NSIG; i++)
|
|
|
|
{
|
|
|
|
myself->getsig (i).sa_mask = 0;
|
|
|
|
if (myself->getsig (i).sa_handler != SIG_IGN)
|
|
|
|
myself->getsig (i).sa_handler = SIG_DFL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
const char *__sp_fn ;
|
|
|
|
int __sp_ln;
|
|
|
|
|
|
|
|
char NO_COPY myself_nowait_dummy[1] = {'0'};// Flag to sig_send that signal goes to
|
|
|
|
// current process but no wait is required
|
|
|
|
char NO_COPY myself_nowait_nonmain_dummy[1] = {'1'};// Flag to sig_send that signal goes to
|
|
|
|
// current process but no wait is required
|
|
|
|
// if this is not the main thread.
|
|
|
|
|
|
|
|
HANDLE NO_COPY signal_arrived; // Event signaled when a signal has
|
|
|
|
// resulted in a user-specified
|
|
|
|
// function call
|
|
|
|
/*
|
|
|
|
* Common variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* How long to wait for message/signals. Normally this is infinite.
|
|
|
|
* On termination, however, these are set to zero as a flag to exit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define Static static NO_COPY
|
|
|
|
|
2001-08-31 20:16:16 +02:00
|
|
|
Static DWORD proc_loop_wait = 1000; // Wait for subprocesses to exit
|
|
|
|
Static DWORD sig_loop_wait = INFINITE; // Wait for signals to arrive
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE sigcatch_nonmain; // The semaphore signaled when
|
2000-02-17 20:38:33 +01:00
|
|
|
// signals are available for
|
|
|
|
// processing from non-main thread
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE sigcatch_main; // Signalled when main thread sends a
|
2000-02-17 20:38:33 +01:00
|
|
|
// signal
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE sigcatch_nosync; // Signal wait_sig to scan sigtodo
|
2000-02-17 20:38:33 +01:00
|
|
|
// but not to bother with any
|
|
|
|
// synchronization
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE sigcomplete_main; // Event signaled when a signal has
|
2000-02-17 20:38:33 +01:00
|
|
|
// finished processing for the main
|
|
|
|
// thread
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE sigcomplete_nonmain; // Semaphore raised for non-main
|
2000-02-17 20:38:33 +01:00
|
|
|
// threads when a signal has finished
|
|
|
|
// processing
|
2002-08-18 06:13:57 +02:00
|
|
|
HANDLE NO_COPY sigCONT; // Used to "STOP" a process
|
2002-08-01 18:20:31 +02:00
|
|
|
Static cygthread *hwait_sig; // Handle of wait_sig thread
|
|
|
|
Static cygthread *hwait_subproc; // Handle of sig_subproc thread
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE wait_sig_inited; // Control synchronization of
|
2000-02-17 20:38:33 +01:00
|
|
|
// message queue startup
|
|
|
|
|
|
|
|
/* Used by WaitForMultipleObjects. These are handles to child processes.
|
|
|
|
*/
|
2002-08-18 06:13:57 +02:00
|
|
|
Static HANDLE events[PSIZE + 1]; // All my children's handles++
|
2000-02-17 20:38:33 +01:00
|
|
|
#define hchildren (events + 1) // Where the children handles begin
|
2002-02-19 06:58:44 +01:00
|
|
|
Static char cpchildren[PSIZE * sizeof (pinfo)]; // All my children info
|
2002-08-18 06:13:57 +02:00
|
|
|
Static int nchildren; // Number of active children
|
2002-08-19 16:59:27 +02:00
|
|
|
Static char czombies[(NZOMBIES + 1) * sizeof (pinfo)]; // All my deceased children info
|
2002-08-18 06:13:57 +02:00
|
|
|
Static int nzombies; // Number of deceased children
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-02-19 06:58:44 +01:00
|
|
|
#define pchildren ((pinfo *) cpchildren)
|
|
|
|
#define zombies ((pinfo *) czombies)
|
|
|
|
|
2001-08-31 20:16:16 +02:00
|
|
|
Static waitq waitq_head = {0, 0, 0, 0, 0, 0, 0};// Start of queue for wait'ing threads
|
2000-02-17 20:38:33 +01:00
|
|
|
Static waitq waitq_main; // Storage for main thread
|
|
|
|
|
2001-08-31 20:16:16 +02:00
|
|
|
muto NO_COPY *sync_proc_subproc = NULL; // Control access to subproc stuff
|
2000-02-24 03:49:44 +01:00
|
|
|
|
2001-08-31 20:16:16 +02:00
|
|
|
DWORD NO_COPY sigtid = 0; // ID of the signal thread
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-09-03 16:15:55 +02:00
|
|
|
bool NO_COPY pending_signals = false; // true if signals pending
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Functions
|
|
|
|
*/
|
2003-08-29 04:59:06 +02:00
|
|
|
static int __stdcall checkstate (waitq *) __attribute__ ((regparm (1)));
|
2000-02-21 06:20:38 +01:00
|
|
|
static __inline__ BOOL get_proc_lock (DWORD, DWORD);
|
2003-08-29 04:59:06 +02:00
|
|
|
static HANDLE __stdcall getevent (_pinfo *, const char *) __attribute__ ((regparm (2)));
|
2000-02-17 20:38:33 +01:00
|
|
|
static void __stdcall remove_zombie (int);
|
|
|
|
static DWORD WINAPI wait_sig (VOID *arg);
|
2000-07-29 18:24:59 +02:00
|
|
|
static int __stdcall stopped_or_terminated (waitq *, _pinfo *);
|
2000-02-17 20:38:33 +01:00
|
|
|
static DWORD WINAPI wait_subproc (VOID *);
|
|
|
|
|
|
|
|
/* Determine if the parent process is alive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL __stdcall
|
|
|
|
my_parent_is_alive ()
|
|
|
|
{
|
|
|
|
DWORD res;
|
2000-11-15 07:27:48 +01:00
|
|
|
if (!myself->ppid_handle)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-11-15 07:27:48 +01:00
|
|
|
debug_printf ("No myself->ppid_handle");
|
2000-02-17 20:38:33 +01:00
|
|
|
res = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (int i = 0; i < 2; i++)
|
2000-11-15 07:27:48 +01:00
|
|
|
switch (res = WaitForSingleObject (myself->ppid_handle, 0))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
debug_printf ("parent dead.");
|
|
|
|
res = FALSE;
|
|
|
|
goto out;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
debug_printf ("parent still alive");
|
|
|
|
res = TRUE;
|
|
|
|
goto out;
|
|
|
|
case WAIT_FAILED:
|
|
|
|
DWORD werr = GetLastError ();
|
|
|
|
if (werr == ERROR_INVALID_HANDLE && i == 0)
|
|
|
|
continue;
|
2000-11-15 07:27:48 +01:00
|
|
|
system_printf ("WFSO for myself->ppid_handle(%p) failed, error %d",
|
|
|
|
myself->ppid_handle, werr);
|
2000-02-17 20:38:33 +01:00
|
|
|
res = FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-11 21:19:29 +02:00
|
|
|
void __stdcall
|
|
|
|
wait_for_sigthread ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-02-21 05:33:53 +01:00
|
|
|
sigproc_printf ("wait_sig_inited %p", wait_sig_inited);
|
|
|
|
HANDLE hsig_inited = wait_sig_inited;
|
|
|
|
(void) WaitForSingleObject (hsig_inited, INFINITE);
|
2002-08-11 21:19:29 +02:00
|
|
|
wait_sig_inited = NULL;
|
2003-02-21 05:33:53 +01:00
|
|
|
(void) ForceCloseHandle1 (hsig_inited, wait_sig_inited);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-11-03 04:32:27 +01:00
|
|
|
/* Get the sync_proc_subproc muto to control access to
|
|
|
|
* children, zombie arrays.
|
|
|
|
* Attempt to handle case where process is exiting as we try to grab
|
|
|
|
* the mutex.
|
|
|
|
*/
|
|
|
|
static BOOL
|
|
|
|
get_proc_lock (DWORD what, DWORD val)
|
|
|
|
{
|
|
|
|
Static int lastwhat = -1;
|
|
|
|
if (!sync_proc_subproc)
|
|
|
|
return FALSE;
|
|
|
|
if (sync_proc_subproc->acquire (WPSP))
|
|
|
|
{
|
|
|
|
lastwhat = what;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (!sync_proc_subproc)
|
|
|
|
return FALSE;
|
|
|
|
system_printf ("Couldn't aquire sync_proc_subproc for(%d,%d), %E, last %d",
|
|
|
|
what, val, lastwhat);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
static BOOL __stdcall
|
2000-07-29 18:24:59 +02:00
|
|
|
proc_can_be_signalled (_pinfo *p)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
if (p == myself_nowait || p == myself_nowait_nonmain || p == myself)
|
|
|
|
{
|
2002-08-11 21:19:29 +02:00
|
|
|
assert (!wait_sig_inited);
|
2000-02-17 20:38:33 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ISSTATE (p, PID_INITIALIZING) ||
|
|
|
|
(((p)->process_state & (PID_ACTIVE | PID_IN_USE)) ==
|
|
|
|
(PID_ACTIVE | PID_IN_USE));
|
|
|
|
}
|
|
|
|
|
2000-07-29 18:24:59 +02:00
|
|
|
BOOL __stdcall
|
2000-09-01 22:54:22 +02:00
|
|
|
pid_exists (pid_t pid)
|
2000-07-29 18:24:59 +02:00
|
|
|
{
|
|
|
|
pinfo p (pid);
|
|
|
|
return proc_exists (p);
|
|
|
|
}
|
|
|
|
|
2000-10-15 03:37:07 +02:00
|
|
|
/* Test to determine if a process really exists and is processing signals.
|
2000-02-17 20:38:33 +01:00
|
|
|
*/
|
|
|
|
BOOL __stdcall
|
2000-07-29 18:24:59 +02:00
|
|
|
proc_exists (_pinfo *p)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-01-19 19:15:23 +01:00
|
|
|
return p && !(p->process_state & PID_EXITED);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-09-02 03:19:58 +02:00
|
|
|
/* Return 1 if this is one of our children, zero otherwise.
|
|
|
|
FIXME: This really should be integrated with the rest of the proc_subproc
|
|
|
|
testing. Scanning these lists twice is inefficient. */
|
|
|
|
int __stdcall
|
|
|
|
mychild (int pid)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nchildren; i++)
|
|
|
|
if (pchildren[i]->pid == pid)
|
|
|
|
return 1;
|
|
|
|
for (int i = 0; i < nzombies; i++)
|
|
|
|
if (zombies[i]->pid == pid)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Handle all subprocess requests
|
|
|
|
*/
|
2000-07-29 18:24:59 +02:00
|
|
|
#define vchild (*((pinfo *) val))
|
2000-02-17 20:38:33 +01:00
|
|
|
int __stdcall
|
|
|
|
proc_subproc (DWORD what, DWORD val)
|
|
|
|
{
|
|
|
|
int rc = 1;
|
|
|
|
int potential_match;
|
2000-07-29 18:24:59 +02:00
|
|
|
_pinfo *child;
|
2000-02-24 07:45:32 +01:00
|
|
|
int clearing;
|
2000-02-17 20:38:33 +01:00
|
|
|
waitq *w;
|
|
|
|
|
|
|
|
#define wval ((waitq *) val)
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("args: %x, %d", what, val);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (!get_proc_lock (what, val)) // Serialize access to this function
|
|
|
|
{
|
2000-11-06 07:36:32 +01:00
|
|
|
system_printf ("couldn't get proc lock. Something is wrong.");
|
2000-02-17 20:38:33 +01:00
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
/* Add a new subprocess to the children arrays.
|
|
|
|
* (usually called from the main thread)
|
|
|
|
*/
|
|
|
|
case PROC_ADDCHILD:
|
|
|
|
if (nchildren >= PSIZE - 1)
|
2001-02-10 05:20:52 +01:00
|
|
|
{
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
pchildren[nchildren] = vchild;
|
|
|
|
hchildren[nchildren] = vchild->hProcess;
|
2000-10-09 04:53:44 +02:00
|
|
|
if (!DuplicateHandle (hMainProc, vchild->hProcess, hMainProc, &vchild->pid_handle,
|
2000-10-15 03:37:07 +02:00
|
|
|
0, 0, DUPLICATE_SAME_ACCESS))
|
2000-10-09 04:53:44 +02:00
|
|
|
system_printf ("Couldn't duplicate child handle for pid %d, %E", vchild->pid);
|
|
|
|
ProtectHandle1 (vchild->pid_handle, pid_handle);
|
2000-11-06 07:36:32 +01:00
|
|
|
|
|
|
|
if (!DuplicateHandle (hMainProc, hMainProc, vchild->hProcess, &vchild->ppid_handle,
|
|
|
|
0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
system_printf ("Couldn't duplicate my handle<%p> for pid %d, %E", hMainProc, vchild->pid);
|
|
|
|
vchild->ppid = myself->pid;
|
2000-11-07 00:12:05 +01:00
|
|
|
vchild->uid = myself->uid;
|
2000-11-06 07:36:32 +01:00
|
|
|
vchild->gid = myself->gid;
|
|
|
|
vchild->pgid = myself->pgid;
|
|
|
|
vchild->sid = myself->sid;
|
|
|
|
vchild->ctty = myself->ctty;
|
|
|
|
vchild->process_state |= PID_INITIALIZING | (myself->process_state & PID_USETTY);
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("added pid %d to wait list, slot %d, winpid %p, handle %p",
|
2000-02-17 20:38:33 +01:00
|
|
|
vchild->pid, nchildren, vchild->dwProcessId,
|
|
|
|
vchild->hProcess);
|
2000-10-13 00:15:47 +02:00
|
|
|
nchildren++;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
wake_wait_subproc ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* A child process had terminated.
|
2000-11-05 04:08:39 +01:00
|
|
|
Possibly this is just due to an exec(). Cygwin implements an exec()
|
|
|
|
as a "handoff" from one windows process to another. If child->hProcess
|
|
|
|
is different from what is recorded in hchildren, then this is an exec().
|
|
|
|
Otherwise this is a normal child termination event.
|
|
|
|
(called from wait_subproc thread) */
|
2000-02-17 20:38:33 +01:00
|
|
|
case PROC_CHILDTERMINATED:
|
2000-09-02 03:19:58 +02:00
|
|
|
if (hchildren[val] != pchildren[val]->hProcess)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("pid %d[%d], reparented old hProcess %p, new %p",
|
2002-02-22 20:33:41 +01:00
|
|
|
pchildren[val]->pid, val, hchildren[val], pchildren[val]->hProcess);
|
|
|
|
HANDLE h = hchildren[val];
|
2000-07-29 18:24:59 +02:00
|
|
|
hchildren[val] = pchildren[val]->hProcess; /* Filled out by child */
|
2002-02-22 20:33:41 +01:00
|
|
|
sync_proc_subproc->release (); // Release the lock ASAP
|
|
|
|
ForceCloseHandle1 (h, childhProc);
|
2000-07-29 18:24:59 +02:00
|
|
|
ProtectHandle1 (pchildren[val]->hProcess, childhProc);
|
2000-10-17 20:55:58 +02:00
|
|
|
rc = 0;
|
2002-02-22 20:33:41 +01:00
|
|
|
goto out; // This was an exec()
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("pid %d[%d] terminated, handle %p, nchildren %d, nzombies %d",
|
2000-07-29 18:24:59 +02:00
|
|
|
pchildren[val]->pid, val, hchildren[val], nchildren, nzombies);
|
2001-06-11 19:57:10 +02:00
|
|
|
|
|
|
|
int thiszombie;
|
|
|
|
thiszombie = nzombies;
|
|
|
|
zombies[nzombies] = pchildren[val]; // Add to zombie array
|
|
|
|
zombies[nzombies++]->process_state = PID_ZOMBIE;// Walking dead
|
|
|
|
|
2001-06-24 23:57:50 +02:00
|
|
|
sigproc_printf ("zombifying [%d], pid %d, handle %p, nchildren %d",
|
2000-11-04 06:54:57 +01:00
|
|
|
val, pchildren[val]->pid, hchildren[val], nchildren);
|
|
|
|
if ((int) val < --nchildren)
|
|
|
|
{
|
|
|
|
hchildren[val] = hchildren[nchildren];
|
|
|
|
pchildren[val] = pchildren[nchildren];
|
|
|
|
}
|
2001-06-11 19:57:10 +02:00
|
|
|
|
|
|
|
/* See if we should care about the this terminated process. If we've
|
|
|
|
filled up our table or if we're ignoring SIGCHLD, then we immediately
|
|
|
|
remove the process and move on. Otherwise, this process becomes a zombie
|
|
|
|
which must be reaped by a wait() call. */
|
2001-09-09 05:34:36 +02:00
|
|
|
if (nzombies >= NZOMBIES
|
2001-06-11 19:57:10 +02:00
|
|
|
|| myself->getsig (SIGCHLD).sa_handler == (void *) SIG_IGN)
|
|
|
|
{
|
|
|
|
sigproc_printf ("automatically removing zombie %d", thiszombie);
|
|
|
|
remove_zombie (thiszombie);
|
|
|
|
}
|
|
|
|
|
2000-11-15 07:27:48 +01:00
|
|
|
/* Don't scan the wait queue yet. Caller will send SIGCHLD to this process.
|
|
|
|
This will cause an eventual scan of waiters. */
|
2000-02-24 07:45:32 +01:00
|
|
|
break;
|
2000-02-24 03:49:44 +01:00
|
|
|
|
2000-10-09 04:53:44 +02:00
|
|
|
/* Handle a wait4() operation. Allocates an event for the calling
|
|
|
|
* thread which is signaled when the appropriate pid exits or stops.
|
|
|
|
* (usually called from the main thread)
|
|
|
|
*/
|
|
|
|
case PROC_WAIT:
|
|
|
|
wval->ev = NULL; // Don't know event flag yet
|
|
|
|
|
|
|
|
if (wval->pid <= 0)
|
|
|
|
child = NULL; // Not looking for a specific pid
|
|
|
|
else if (!mychild (wval->pid))
|
|
|
|
goto out; // invalid pid. flag no such child
|
|
|
|
|
|
|
|
wval->status = 0; // Don't know status yet
|
|
|
|
sigproc_printf ("wval->pid %d, wval->options %d", wval->pid, wval->options);
|
|
|
|
|
|
|
|
/* If the first time for this thread, create a new event, otherwise
|
|
|
|
* reset the event.
|
|
|
|
*/
|
|
|
|
if ((wval->ev = wval->thread_ev) == NULL)
|
|
|
|
{
|
|
|
|
wval->ev = wval->thread_ev = CreateEvent (&sec_none_nih, TRUE,
|
|
|
|
FALSE, NULL);
|
|
|
|
ProtectHandle (wval->ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetEvent (wval->ev);
|
|
|
|
w = waitq_head.next;
|
|
|
|
waitq_head.next = wval; /* Add at the beginning. */
|
|
|
|
wval->next = w; /* Link in rest of the list. */
|
|
|
|
clearing = 0;
|
|
|
|
goto scan_wait;
|
|
|
|
|
2000-02-24 03:49:44 +01:00
|
|
|
/* Clear all waiting threads. Called from exceptions.cc prior to
|
|
|
|
* the main thread's dispatch to a signal handler function.
|
|
|
|
* (called from wait_sig thread)
|
|
|
|
*/
|
|
|
|
case PROC_CLEARWAIT:
|
|
|
|
/* Clear all "wait"ing threads. */
|
2000-02-24 20:54:01 +01:00
|
|
|
if (val)
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("clear waiting threads");
|
2000-02-24 20:54:01 +01:00
|
|
|
else
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("looking for processes to reap");
|
2000-02-24 07:45:32 +01:00
|
|
|
clearing = val;
|
2000-02-24 03:49:44 +01:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
scan_wait:
|
|
|
|
/* Scan the linked list of wait()ing threads. If a wait's parameters
|
|
|
|
* match this pid, then activate it.
|
|
|
|
*/
|
|
|
|
for (w = &waitq_head; w->next != NULL; w = w->next)
|
|
|
|
{
|
|
|
|
if ((potential_match = checkstate (w)) > 0)
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("released waiting thread");
|
2000-10-09 04:53:44 +02:00
|
|
|
else if (!clearing && !(w->next->options & WNOHANG) && potential_match < 0)
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("only found non-terminated children");
|
2000-02-24 03:49:44 +01:00
|
|
|
else if (potential_match <= 0) // nothing matched
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("waiting thread found no children");
|
2000-02-17 20:38:33 +01:00
|
|
|
HANDLE oldw = w->next->ev;
|
2000-10-09 04:53:44 +02:00
|
|
|
w->next->pid = 0;
|
2000-02-24 03:49:44 +01:00
|
|
|
if (clearing)
|
|
|
|
w->next->status = -1; /* flag that a signal was received */
|
2000-10-23 22:50:36 +02:00
|
|
|
else if (!potential_match || !(w->next->options & WNOHANG))
|
2000-02-28 17:25:34 +01:00
|
|
|
w->next->ev = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!SetEvent (oldw))
|
|
|
|
system_printf ("couldn't wake up wait event %p, %E", oldw);
|
|
|
|
w->next = w->next->next;
|
|
|
|
}
|
|
|
|
if (w->next == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-02-24 03:49:44 +01:00
|
|
|
if (!clearing)
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("finished processing terminated/stopped child");
|
2000-02-24 03:49:44 +01:00
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-02-24 03:49:44 +01:00
|
|
|
waitq_head.next = NULL;
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("finished clearing");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
sync_proc_subproc->release (); // Release the lock
|
|
|
|
out1:
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("returning %d", rc);
|
2000-02-17 20:38:33 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the wait_subproc thread.
|
|
|
|
* Called on process exit.
|
|
|
|
* Also called by spawn_guts to disassociate any subprocesses from this
|
|
|
|
* process. Subprocesses will then know to clean up after themselves and
|
|
|
|
* will not become zombies.
|
|
|
|
*/
|
|
|
|
void __stdcall
|
|
|
|
proc_terminate (void)
|
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("nchildren %d, nzombies %d", nchildren, nzombies);
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Signal processing is assumed to be blocked in this routine. */
|
|
|
|
if (hwait_subproc)
|
|
|
|
{
|
|
|
|
proc_loop_wait = 0; // Tell wait_subproc thread to exit
|
2002-10-10 07:23:23 +02:00
|
|
|
sync_proc_subproc->acquire (WPSP);
|
2000-02-17 20:38:33 +01:00
|
|
|
wake_wait_subproc (); // Wake wait_subproc loop
|
|
|
|
hwait_subproc = NULL;
|
|
|
|
|
2000-02-24 07:45:32 +01:00
|
|
|
(void) proc_subproc (PROC_CLEARWAIT, 1);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Clean out zombie processes from the pid list. */
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nzombies; i++)
|
|
|
|
{
|
2000-07-29 18:24:59 +02:00
|
|
|
if (zombies[i]->hProcess)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-07-29 18:24:59 +02:00
|
|
|
ForceCloseHandle1 (zombies[i]->hProcess, childhProc);
|
2000-10-09 04:53:44 +02:00
|
|
|
ForceCloseHandle1 (zombies[i]->pid_handle, pid_handle);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2002-10-21 03:00:58 +02:00
|
|
|
zombies[i]->ppid = 1;
|
2000-10-15 03:37:07 +02:00
|
|
|
zombies[i]->process_state = PID_EXITED; /* CGF FIXME - still needed? */
|
2002-09-22 05:38:57 +02:00
|
|
|
zombies[i].release (); // FIXME: this breaks older gccs for some reason
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Disassociate my subprocesses */
|
|
|
|
for (i = 0; i < nchildren; i++)
|
|
|
|
{
|
2000-07-29 18:24:59 +02:00
|
|
|
if (!pchildren[i]->hProcess)
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("%d(%d) hProcess cleared already?", pchildren[i]->pid,
|
2000-07-29 18:24:59 +02:00
|
|
|
pchildren[i]->dwProcessId);
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
|
|
|
{
|
2000-07-29 18:24:59 +02:00
|
|
|
ForceCloseHandle1 (pchildren[i]->hProcess, childhProc);
|
2000-10-14 07:52:38 +02:00
|
|
|
sigproc_printf ("%d(%d) closed child handle", pchildren[i]->pid,
|
2000-07-29 18:24:59 +02:00
|
|
|
pchildren[i]->dwProcessId);
|
2000-10-14 07:52:38 +02:00
|
|
|
pchildren[i]->ppid = 1;
|
|
|
|
if (pchildren[i]->pgid == myself->pid)
|
|
|
|
pchildren[i]->process_state |= PID_ORPHANED;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-10-14 07:52:38 +02:00
|
|
|
pchildren[i].release ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
nchildren = nzombies = 0;
|
2000-08-25 04:27:42 +02:00
|
|
|
/* Just zero sync_proc_subproc as the delete below seems to cause
|
|
|
|
problems for older gccs. */
|
|
|
|
sync_proc_subproc = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("leaving");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear pending signal from the sigtodo array
|
|
|
|
*/
|
|
|
|
void __stdcall
|
|
|
|
sig_clear (int sig)
|
|
|
|
{
|
2001-05-04 23:02:15 +02:00
|
|
|
(void) InterlockedExchange (myself->getsigtodo (sig), 0L);
|
2003-08-19 07:47:44 +02:00
|
|
|
(void) InterlockedExchange (getlocal_sigtodo (sig), 0L);
|
2000-02-17 20:38:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-08-19 07:47:44 +02:00
|
|
|
extern "C" int
|
|
|
|
sigpending (sigset_t *set)
|
|
|
|
{
|
|
|
|
unsigned bit;
|
|
|
|
*set = 0;
|
|
|
|
for (int sig = 1; sig < NSIG; sig++)
|
|
|
|
if ((*getlocal_sigtodo (sig) || *myself->getsigtodo (sig))
|
|
|
|
&& (myself->getsigmask () & (bit = SIGTOMASK (sig))))
|
|
|
|
*set |= bit;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Force the wait_sig thread to wake up and scan the sigtodo array.
|
|
|
|
*/
|
|
|
|
extern "C" int __stdcall
|
2003-08-19 06:10:42 +02:00
|
|
|
sig_dispatch_pending ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-08-19 06:10:42 +02:00
|
|
|
if (!hwait_sig || GetCurrentThreadId () == sigtid)
|
2000-02-17 20:38:33 +01:00
|
|
|
return 0;
|
2002-11-22 21:51:13 +01:00
|
|
|
|
|
|
|
sigframe thisframe (mainthread);
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
#ifdef DEBUGGING
|
2003-08-31 20:26:58 +02:00
|
|
|
sigproc_printf ("pending_signals %d", pending_signals);
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
2003-08-19 06:10:42 +02:00
|
|
|
|
2003-08-31 20:26:58 +02:00
|
|
|
if (!pending_signals)
|
2000-02-17 20:38:33 +01:00
|
|
|
#ifdef DEBUGGING
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("no need to wake anything up");
|
2000-02-17 20:38:33 +01:00
|
|
|
#else
|
|
|
|
;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
2003-08-19 06:10:42 +02:00
|
|
|
(void) sig_send (myself, __SIGFLUSH);
|
2000-02-17 20:38:33 +01:00
|
|
|
#ifdef DEBUGGING
|
2003-08-19 06:10:42 +02:00
|
|
|
sigproc_printf ("woke up wait_sig");
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
|
|
|
}
|
2003-08-19 06:10:42 +02:00
|
|
|
|
2003-08-31 20:26:58 +02:00
|
|
|
return thisframe.call_signal_handler ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Message initialization. Called from dll_crt0_1
|
|
|
|
*
|
|
|
|
* This routine starts the signal handling thread. The wait_sig_inited
|
|
|
|
* event is used to signal that the thread is ready to handle signals.
|
|
|
|
* We don't wait for this during initialization but instead detect it
|
|
|
|
* in sig_send to gain a little concurrency.
|
|
|
|
*/
|
|
|
|
void __stdcall
|
|
|
|
sigproc_init ()
|
|
|
|
{
|
2000-09-01 23:04:34 +02:00
|
|
|
wait_sig_inited = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
ProtectHandle (wait_sig_inited);
|
|
|
|
|
2002-10-21 03:00:58 +02:00
|
|
|
/* sync_proc_subproc is used by proc_subproc. It serialises
|
|
|
|
* access to the children and zombie arrays.
|
|
|
|
*/
|
|
|
|
new_muto (sync_proc_subproc);
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* local event signaled when main thread has been dispatched
|
|
|
|
to a signal handler function. */
|
2002-09-22 05:38:57 +02:00
|
|
|
signal_arrived = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2000-10-14 07:52:38 +02:00
|
|
|
ProtectHandle (signal_arrived);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-08-06 07:08:55 +02:00
|
|
|
hwait_sig = new cygthread (wait_sig, cygself, "sig");
|
2002-10-09 07:55:40 +02:00
|
|
|
hwait_sig->zap_h ();
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Initialize waitq structure for main thread. A waitq structure is
|
|
|
|
* allocated for each thread that executes a wait to allow multiple threads
|
|
|
|
* to perform waits. Pre-allocate a waitq structure for the main thread.
|
|
|
|
*/
|
|
|
|
waitq *w;
|
|
|
|
if ((w = (waitq *)waitq_storage.get ()) == NULL)
|
|
|
|
{
|
|
|
|
w = &waitq_main;
|
|
|
|
waitq_storage.set (w);
|
|
|
|
}
|
|
|
|
memset (w, 0, sizeof *w); // Just to be safe
|
|
|
|
|
2001-01-12 06:38:25 +01:00
|
|
|
myself->getsig (SIGSTOP).sa_flags = SA_RESTART | SA_NODEFER;
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("process/signal handling enabled(%x)", myself->process_state);
|
2000-02-17 20:38:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called on process termination to terminate signal and process threads.
|
|
|
|
*/
|
|
|
|
void __stdcall
|
|
|
|
sigproc_terminate (void)
|
|
|
|
{
|
|
|
|
hwait_sig = NULL;
|
|
|
|
|
2002-10-21 03:00:58 +02:00
|
|
|
if (!sig_loop_wait)
|
2003-03-17 20:08:11 +01:00
|
|
|
sigproc_printf ("sigproc handling not active");
|
2002-10-21 03:00:58 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sigproc_printf ("entering");
|
|
|
|
sig_loop_wait = 0; // Tell wait_sig to exit when it is
|
|
|
|
// finished with anything it is doing
|
2000-02-17 20:38:33 +01:00
|
|
|
ForceCloseHandle (sigcomplete_main);
|
|
|
|
for (int i = 0; i < 20; i++)
|
|
|
|
(void) ReleaseSemaphore (sigcomplete_nonmain, 1, NULL);
|
2000-10-15 03:37:07 +02:00
|
|
|
// ForceCloseHandle (sigcomplete_nonmain);
|
|
|
|
// ForceCloseHandle (sigcatch_main);
|
|
|
|
// ForceCloseHandle (sigcatch_nonmain);
|
|
|
|
// ForceCloseHandle (sigcatch_nosync);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
proc_terminate (); // Terminate process handling thread
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send a signal to another process by raising its signal semaphore.
|
|
|
|
* If pinfo *p == NULL, send to the current process.
|
|
|
|
* If sending to this process, wait for notification that a signal has
|
|
|
|
* completed before returning.
|
|
|
|
*/
|
|
|
|
int __stdcall
|
2001-04-27 20:50:59 +02:00
|
|
|
sig_send (_pinfo *p, int sig, DWORD ebp, bool exception)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
int rc = 1;
|
|
|
|
DWORD tid = GetCurrentThreadId ();
|
|
|
|
BOOL its_me;
|
|
|
|
HANDLE thiscatch = NULL;
|
|
|
|
HANDLE thiscomplete = NULL;
|
|
|
|
BOOL wait_for_completion;
|
2000-05-17 07:49:51 +02:00
|
|
|
sigframe thisframe;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (p == myself_nowait_nonmain)
|
2000-07-29 18:24:59 +02:00
|
|
|
p = (tid == mainthread.id) ? (_pinfo *) myself : myself_nowait;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (!(its_me = (p == NULL || p == myself || p == myself_nowait)))
|
|
|
|
wait_for_completion = FALSE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (no_signals_available ())
|
|
|
|
goto out; // Either exiting or not yet initializing
|
2003-02-21 05:33:53 +01:00
|
|
|
if (wait_sig_inited)
|
|
|
|
wait_for_sigthread ();
|
2000-02-17 20:38:33 +01:00
|
|
|
wait_for_completion = p != myself_nowait;
|
|
|
|
p = myself;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is possible that the process is not yet ready to receive messages
|
|
|
|
* or that it has exited. Detect this.
|
|
|
|
*/
|
|
|
|
if (!proc_can_be_signalled (p)) /* Is the process accepting messages? */
|
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("invalid pid %d(%x), signal %d",
|
2000-02-17 20:38:33 +01:00
|
|
|
p->pid, p->process_state, sig);
|
|
|
|
set_errno (ESRCH);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("pid %d, signal %d, its_me %d", p->pid, sig, its_me);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-08-19 07:47:44 +02:00
|
|
|
LONG *todo;
|
2003-08-29 04:59:06 +02:00
|
|
|
bool issem;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (its_me)
|
|
|
|
{
|
|
|
|
if (!wait_for_completion)
|
2003-08-21 05:18:46 +02:00
|
|
|
{
|
|
|
|
thiscatch = sigcatch_nosync;
|
|
|
|
todo = myself->getsigtodo (sig);
|
2003-08-29 04:59:06 +02:00
|
|
|
issem = false;
|
2003-08-21 05:18:46 +02:00
|
|
|
}
|
2000-05-17 07:49:51 +02:00
|
|
|
else if (tid != mainthread.id)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
thiscatch = sigcatch_nonmain;
|
|
|
|
thiscomplete = sigcomplete_nonmain;
|
2003-08-21 05:18:46 +02:00
|
|
|
todo = getlocal_sigtodo (sig);
|
2003-08-29 04:59:06 +02:00
|
|
|
issem = true;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
thiscatch = sigcatch_main;
|
|
|
|
thiscomplete = sigcomplete_main;
|
2003-09-09 05:11:31 +02:00
|
|
|
thisframe.init (mainthread, ebp, exception);
|
2003-08-21 05:18:46 +02:00
|
|
|
todo = getlocal_sigtodo (sig);
|
2003-08-29 04:59:06 +02:00
|
|
|
issem = true;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
}
|
2003-08-29 04:59:06 +02:00
|
|
|
else if ((thiscatch = getevent (p, "sigcatch")))
|
|
|
|
{
|
|
|
|
todo = p->getsigtodo (sig);
|
|
|
|
issem = false;
|
|
|
|
}
|
2003-08-19 07:47:44 +02:00
|
|
|
else
|
2003-08-29 04:59:06 +02:00
|
|
|
goto out; // Couldn't get the semaphore. getevent issued
|
2000-02-17 20:38:33 +01:00
|
|
|
// an error, if appropriate.
|
|
|
|
|
|
|
|
#if WHEN_MULTI_THREAD_SIGNALS_WORK
|
|
|
|
signal_dispatch *sd;
|
|
|
|
sd = signal_dispatch_storage.get ();
|
|
|
|
if (sd == NULL)
|
|
|
|
sd = signal_dispatch_storage.create ();
|
|
|
|
#endif
|
2000-03-05 07:34:55 +01:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Increment the sigtodo array to signify which signal to assert.
|
|
|
|
*/
|
2003-08-19 07:47:44 +02:00
|
|
|
(void) InterlockedIncrement (todo);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Notify the process that a signal has arrived.
|
|
|
|
*/
|
2003-08-29 04:59:06 +02:00
|
|
|
if (issem ? !ReleaseSemaphore (thiscatch, 1, NULL) : !SetEvent (thiscatch))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
/* Couldn't signal the semaphore. This probably means that the
|
|
|
|
* process is exiting.
|
|
|
|
*/
|
|
|
|
if (!its_me)
|
|
|
|
ForceCloseHandle (thiscatch);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (no_signals_available ())
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("I'm going away now");
|
2000-02-17 20:38:33 +01:00
|
|
|
else if ((int) GetLastError () == -1)
|
|
|
|
rc = WaitForSingleObject (thiscomplete, 500);
|
|
|
|
else
|
|
|
|
system_printf ("error sending signal %d to pid %d, semaphore %p, %E",
|
|
|
|
sig, p->pid, thiscatch);
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No need to wait for signal completion unless this was a signal to
|
|
|
|
* this process.
|
|
|
|
*
|
|
|
|
* If it was a signal to this process, wait for a dispatched signal.
|
|
|
|
* Otherwise just wait for the wait_sig to signal that it has finished
|
|
|
|
* processing the signal.
|
|
|
|
*/
|
|
|
|
if (!wait_for_completion)
|
|
|
|
{
|
2000-03-05 07:34:55 +01:00
|
|
|
rc = WAIT_OBJECT_0;
|
2001-04-01 02:06:17 +02:00
|
|
|
sigproc_printf ("Not waiting for sigcomplete. its_me %d signal %d", its_me, sig);
|
2000-03-05 07:34:55 +01:00
|
|
|
if (!its_me)
|
|
|
|
ForceCloseHandle (thiscatch);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("Waiting for thiscomplete %p", thiscomplete);
|
2000-02-17 20:38:33 +01:00
|
|
|
rc = WaitForSingleObject (thiscomplete, WSSC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == WAIT_OBJECT_0)
|
|
|
|
rc = 0; // Successful exit
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It's an error unless sig_loop_wait == 0 (the process is exiting). */
|
|
|
|
if (!no_signals_available ())
|
2001-04-01 02:06:17 +02:00
|
|
|
system_printf ("wait for sig_complete event failed, signal %d, rc %d, %E",
|
2000-02-17 20:38:33 +01:00
|
|
|
sig, rc);
|
|
|
|
set_errno (ENOSYS);
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("returning %d from sending signal %d", rc, sig);
|
2000-02-17 20:38:33 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set pending signal from the sigtodo array
|
|
|
|
*/
|
|
|
|
void __stdcall
|
|
|
|
sig_set_pending (int sig)
|
|
|
|
{
|
2003-08-19 07:47:44 +02:00
|
|
|
(void) InterlockedIncrement (getlocal_sigtodo (sig));
|
2000-02-17 20:38:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the wait_subproc thread.
|
|
|
|
* Called from fork() or spawn() to initialize the handling of subprocesses.
|
|
|
|
*/
|
|
|
|
void __stdcall
|
|
|
|
subproc_init (void)
|
|
|
|
{
|
|
|
|
if (hwait_subproc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* A "wakeup" handle which can be toggled to make wait_subproc reexamine
|
|
|
|
* the hchildren array.
|
|
|
|
*/
|
|
|
|
events[0] = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
2002-08-01 18:20:31 +02:00
|
|
|
hwait_subproc = new cygthread (wait_subproc, NULL, "proc");
|
2002-10-14 05:51:44 +02:00
|
|
|
hwait_subproc->zap_h ();
|
2000-02-17 20:38:33 +01:00
|
|
|
ProtectHandle (events[0]);
|
2002-10-14 05:51:44 +02:00
|
|
|
sigproc_printf ("started wait_subproc thread");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize some of the memory block passed to child processes
|
|
|
|
by fork/spawn/exec. */
|
|
|
|
|
|
|
|
void __stdcall
|
2000-07-29 18:24:59 +02:00
|
|
|
init_child_info (DWORD chtype, child_info *ch, pid_t pid, HANDLE subproc_ready)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
memset (ch, 0, sizeof *ch);
|
2001-12-26 22:35:16 +01:00
|
|
|
ch->cb = chtype == PROC_FORK ? sizeof (child_info_fork) : sizeof (child_info);
|
2001-12-26 05:53:34 +01:00
|
|
|
ch->intro = PROC_MAGIC_GENERIC;
|
|
|
|
ch->magic = CHILD_INFO_MAGIC;
|
2000-02-17 20:38:33 +01:00
|
|
|
ch->type = chtype;
|
|
|
|
ch->cygpid = pid;
|
|
|
|
ch->subproc_ready = subproc_ready;
|
2000-11-06 07:36:32 +01:00
|
|
|
ch->pppid_handle = myself->ppid_handle;
|
2001-12-26 22:35:16 +01:00
|
|
|
ch->fhandler_union_cb = sizeof (fhandler_union);
|
2002-08-06 07:48:33 +02:00
|
|
|
ch->mount_h = cygwin_mount_h;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the state of all of our children to see if any are stopped or
|
|
|
|
* terminated.
|
|
|
|
*/
|
|
|
|
static int __stdcall
|
2000-10-09 04:53:44 +02:00
|
|
|
checkstate (waitq *parent_w)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-09 04:53:44 +02:00
|
|
|
int potential_match = 0;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("nchildren %d, nzombies %d", nchildren, nzombies);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Check already dead processes first to see if they match the criteria
|
|
|
|
* given in w->next.
|
|
|
|
*/
|
2000-10-09 04:53:44 +02:00
|
|
|
for (int i = 0; i < nzombies; i++)
|
|
|
|
switch (stopped_or_terminated (parent_w, zombies[i]))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-09 04:53:44 +02:00
|
|
|
case -1:
|
|
|
|
potential_match = -1;
|
|
|
|
break;
|
|
|
|
case 1:
|
2000-02-17 20:38:33 +01:00
|
|
|
remove_zombie (i);
|
|
|
|
potential_match = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("checking alive children");
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* No dead terminated children matched. Check for stopped children. */
|
2000-10-09 04:53:44 +02:00
|
|
|
for (int i = 0; i < nchildren; i++)
|
|
|
|
switch (stopped_or_terminated (parent_w, pchildren[i]))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-09 04:53:44 +02:00
|
|
|
case -1:
|
|
|
|
potential_match = -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
break;
|
2000-10-09 04:53:44 +02:00
|
|
|
case 1:
|
|
|
|
potential_match = 1;
|
|
|
|
goto out;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("returning %d", potential_match);
|
2000-02-17 20:38:33 +01:00
|
|
|
return potential_match;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get or create a process specific semaphore used in message passing.
|
|
|
|
*/
|
|
|
|
static HANDLE __stdcall
|
2003-08-29 04:59:06 +02:00
|
|
|
getevent (_pinfo *p, const char *str)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
HANDLE h;
|
2003-05-21 10:01:57 +02:00
|
|
|
char sem_name[MAX_PATH];
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
if (!proc_can_be_signalled (p))
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-01 22:15:53 +01:00
|
|
|
int wait = 1000;
|
2002-01-07 17:47:12 +01:00
|
|
|
/* Wait for new process to generate its semaphores. */
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("pid %d, ppid %d, wait %d, initializing %x", p->pid, p->ppid, wait,
|
2000-02-17 20:38:33 +01:00
|
|
|
ISSTATE (p, PID_INITIALIZING));
|
|
|
|
for (int i = 0; ISSTATE (p, PID_INITIALIZING) && i < wait; i++)
|
2002-11-13 20:36:12 +01:00
|
|
|
low_priority_sleep (1);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
|
|
|
char sa_buf[1024];
|
|
|
|
|
|
|
|
DWORD winpid = GetCurrentProcessId ();
|
2003-08-29 04:59:06 +02:00
|
|
|
#if 0
|
2002-02-19 06:58:44 +01:00
|
|
|
h = CreateSemaphore (sec_user_nih (sa_buf), init, max,
|
2003-05-21 10:01:57 +02:00
|
|
|
str = shared_name (sem_name, str, winpid));
|
2003-08-29 04:59:06 +02:00
|
|
|
#else
|
|
|
|
h = CreateEvent (sec_user_nih (sa_buf), FALSE, FALSE,
|
|
|
|
str = shared_name (sem_name, str, winpid));
|
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
p = myself;
|
2002-01-07 17:47:12 +01:00
|
|
|
if (!h)
|
|
|
|
{
|
2002-01-07 23:18:36 +01:00
|
|
|
system_printf ("can't create semaphore %s, %E", str);
|
2002-01-07 17:47:12 +01:00
|
|
|
__seterrno ();
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-08-29 04:59:06 +02:00
|
|
|
#if 0
|
2000-02-17 20:38:33 +01:00
|
|
|
h = OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE,
|
2003-05-21 10:01:57 +02:00
|
|
|
shared_name (sem_name, str, p->dwProcessId));
|
2003-08-29 04:59:06 +02:00
|
|
|
#else
|
|
|
|
h = OpenEvent (EVENT_ALL_ACCESS, FALSE,
|
|
|
|
shared_name (sem_name, str, p->dwProcessId));
|
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-01-07 17:47:12 +01:00
|
|
|
if (!h)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
if (GetLastError () == ERROR_FILE_NOT_FOUND && !proc_exists (p))
|
2002-01-07 17:47:12 +01:00
|
|
|
set_errno (ESRCH); /* No such process */
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2002-01-07 17:47:12 +01:00
|
|
|
set_errno (EPERM); /* Couldn't access the semaphore --
|
|
|
|
different cygwin DLL maybe? */
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a zombie from zombies by swapping it with the last child in the list.
|
|
|
|
*/
|
|
|
|
static void __stdcall
|
|
|
|
remove_zombie (int ci)
|
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("removing %d, pid %d, nzombies %d", ci, zombies[ci]->pid,
|
2000-10-09 04:53:44 +02:00
|
|
|
nzombies);
|
2000-07-29 18:24:59 +02:00
|
|
|
|
|
|
|
if (zombies[ci])
|
2000-10-09 04:53:44 +02:00
|
|
|
{
|
|
|
|
ForceCloseHandle1 (zombies[ci]->hProcess, childhProc);
|
|
|
|
ForceCloseHandle1 (zombies[ci]->pid_handle, pid_handle);
|
|
|
|
zombies[ci].release ();
|
|
|
|
}
|
2000-07-29 18:24:59 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
if (ci < --nzombies)
|
|
|
|
zombies[ci] = zombies[nzombies];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check status of child process vs. waitq member.
|
|
|
|
*
|
|
|
|
* parent_w is the pointer to the parent of the waitq member in question.
|
|
|
|
* child is the subprocess being considered.
|
|
|
|
*
|
|
|
|
* Returns
|
|
|
|
* 1 if stopped or terminated child matches parent_w->next criteria
|
|
|
|
* -1 if a non-stopped/terminated child matches parent_w->next criteria
|
|
|
|
* 0 if child does not match parent_w->next criteria
|
|
|
|
*/
|
|
|
|
static int __stdcall
|
2000-07-29 18:24:59 +02:00
|
|
|
stopped_or_terminated (waitq *parent_w, _pinfo *child)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
int potential_match;
|
|
|
|
waitq *w = parent_w->next;
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("considering pid %d", child->pid);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (w->pid == -1)
|
|
|
|
potential_match = 1;
|
|
|
|
else if (w->pid == 0)
|
|
|
|
potential_match = child->pgid == myself->pgid;
|
|
|
|
else if (w->pid < 0)
|
|
|
|
potential_match = child->pgid == -w->pid;
|
|
|
|
else
|
|
|
|
potential_match = (w->pid == child->pid);
|
|
|
|
|
|
|
|
if (!potential_match)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BOOL terminated;
|
|
|
|
|
|
|
|
if ((terminated = child->process_state == PID_ZOMBIE) ||
|
2000-10-23 18:50:21 +02:00
|
|
|
((w->options & WUNTRACED) && child->stopsig))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
parent_w->next = w->next; /* successful wait. remove from wait queue */
|
|
|
|
w->pid = child->pid;
|
|
|
|
|
|
|
|
if (!terminated)
|
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("stopped child");
|
2000-02-17 20:38:33 +01:00
|
|
|
w->status = (child->stopsig << 8) | 0x7f;
|
|
|
|
child->stopsig = 0;
|
|
|
|
}
|
2000-07-29 18:24:59 +02:00
|
|
|
else /* Should only get here when child has been moved to the zombies array */
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
if (!GetExitCodeProcess (child->hProcess, &status))
|
|
|
|
status = 0xffff;
|
|
|
|
if (status & EXIT_SIGNAL)
|
|
|
|
w->status = (status >> 8) & 0xff; /* exited due to signal */
|
|
|
|
else
|
|
|
|
w->status = (status & 0xff) << 8; /* exited via "exit ()" */
|
|
|
|
|
|
|
|
add_rusage (&myself->rusage_children, &child->rusage_children);
|
|
|
|
add_rusage (&myself->rusage_children, &child->rusage_self);
|
|
|
|
|
|
|
|
if (w->rusage)
|
|
|
|
{
|
|
|
|
add_rusage ((struct rusage *) w->rusage, &child->rusage_children);
|
|
|
|
add_rusage ((struct rusage *) w->rusage, &child->rusage_self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetEvent (w->ev)) /* wake up wait4 () immediately */
|
|
|
|
system_printf ("couldn't wake up wait event %p, %E", w->ev);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -potential_match;
|
|
|
|
}
|
|
|
|
|
2002-10-30 22:05:18 +01:00
|
|
|
static void
|
|
|
|
talktome ()
|
|
|
|
{
|
|
|
|
winpids pids;
|
|
|
|
for (unsigned i = 0; i < pids.npids; i++)
|
|
|
|
if (pids[i]->hello_pid == myself->pid)
|
|
|
|
pids[i]->commune_recv ();
|
|
|
|
}
|
|
|
|
|
2003-08-20 04:31:26 +02:00
|
|
|
#define RC_MAIN 0
|
|
|
|
#define RC_NONMAIN 1
|
|
|
|
#define RC_NOSYNC 2
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Process signals by waiting for a semaphore to become signaled.
|
|
|
|
* Then scan an in-memory array representing queued signals.
|
|
|
|
* Executes in a separate thread.
|
|
|
|
*
|
|
|
|
* Signals sent from this process are sent a completion signal so
|
|
|
|
* that returns from kill/raise do not occur until the signal has
|
|
|
|
* has been handled, as per POSIX.
|
|
|
|
*/
|
|
|
|
static DWORD WINAPI
|
2002-08-06 07:08:55 +02:00
|
|
|
wait_sig (VOID *self)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-08-19 07:47:44 +02:00
|
|
|
LONG *todos[] = {getlocal_sigtodo (0), myself->getsigtodo (0)};
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Initialization */
|
2002-10-09 07:55:40 +02:00
|
|
|
(void) SetThreadPriority (GetCurrentThread (), WAIT_SIG_PRIORITY);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* sigcatch_nosync - semaphore incremented by sig_dispatch_pending and
|
|
|
|
* by foreign processes to force an examination of
|
|
|
|
* the sigtodo array.
|
|
|
|
* sigcatch_main - ditto for local main thread.
|
|
|
|
* sigcatch_nonmain - ditto for local non-main threads.
|
|
|
|
*
|
|
|
|
* sigcomplete_main - event used to signal main thread on signal
|
|
|
|
* completion
|
|
|
|
* sigcomplete_nonmain - semaphore signaled for non-main thread on signal
|
|
|
|
* completion
|
|
|
|
*/
|
2003-08-29 04:59:06 +02:00
|
|
|
sigcatch_nosync = getevent (NULL, "sigcatch");
|
2000-02-17 20:38:33 +01:00
|
|
|
sigcatch_nonmain = CreateSemaphore (&sec_none_nih, 0, MAXLONG, NULL);
|
|
|
|
sigcatch_main = CreateSemaphore (&sec_none_nih, 0, MAXLONG, NULL);
|
|
|
|
sigcomplete_nonmain = CreateSemaphore (&sec_none_nih, 0, MAXLONG, NULL);
|
2000-03-09 22:04:05 +01:00
|
|
|
sigcomplete_main = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
2000-10-13 00:15:47 +02:00
|
|
|
sigproc_printf ("sigcatch_nonmain %p, sigcatch_main %p", sigcatch_nonmain, sigcatch_main);
|
2002-08-18 06:13:57 +02:00
|
|
|
sigCONT = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Setting dwProcessId flags that this process is now capable of receiving
|
|
|
|
* signals. Prior to this, dwProcessId was set to the windows pid of
|
|
|
|
* of the original windows process which spawned us unless this was a
|
|
|
|
* "toplevel" process.
|
|
|
|
*/
|
|
|
|
myself->dwProcessId = GetCurrentProcessId ();
|
|
|
|
myself->process_state |= PID_ACTIVE;
|
|
|
|
myself->process_state &= ~PID_INITIALIZING;
|
|
|
|
|
|
|
|
ProtectHandle (sigcatch_nosync);
|
|
|
|
ProtectHandle (sigcatch_nonmain);
|
|
|
|
ProtectHandle (sigcatch_main);
|
|
|
|
ProtectHandle (sigcomplete_nonmain);
|
|
|
|
ProtectHandle (sigcomplete_main);
|
|
|
|
|
|
|
|
/* If we've been execed, then there is still a stub left in the previous
|
|
|
|
* windows process waiting to see if it's started a cygwin process or not.
|
|
|
|
* Signalling subproc_ready indicates that we are a cygwin process.
|
|
|
|
*/
|
2001-07-17 05:41:52 +02:00
|
|
|
if (child_proc_info && child_proc_info->type == PROC_EXEC)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
debug_printf ("subproc_ready %p", child_proc_info->subproc_ready);
|
|
|
|
if (!SetEvent (child_proc_info->subproc_ready))
|
|
|
|
system_printf ("SetEvent (subproc_ready) failed, %E");
|
2002-07-13 22:00:27 +02:00
|
|
|
ForceCloseHandle1 (child_proc_info->subproc_ready, subproc_ready);
|
2000-08-26 03:36:20 +02:00
|
|
|
/* Initialize an "indirect" pid block so that if someone looks up this
|
|
|
|
process via its Windows PID it will be redirected to the appropriate
|
|
|
|
Cygwin PID shared memory block. */
|
|
|
|
static pinfo NO_COPY myself_identity;
|
|
|
|
myself_identity.init (cygwin_pid (myself->dwProcessId), PID_EXECED);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SetEvent (wait_sig_inited);
|
|
|
|
sigtid = GetCurrentThreadId ();
|
|
|
|
|
|
|
|
HANDLE catchem[] = {sigcatch_main, sigcatch_nonmain, sigcatch_nosync};
|
|
|
|
sigproc_printf ("Ready. dwProcessid %d", myself->dwProcessId);
|
2003-08-20 07:15:33 +02:00
|
|
|
DWORD rc = RC_NOSYNC;
|
|
|
|
bool flush = false;
|
2000-11-16 06:16:59 +01:00
|
|
|
for (;;)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-08-20 07:15:33 +02:00
|
|
|
DWORD i;
|
|
|
|
if (rc == RC_MAIN || rc == RC_NONMAIN)
|
|
|
|
i = RC_NOSYNC;
|
|
|
|
else
|
|
|
|
i = RC_MAIN;
|
|
|
|
rc = WaitForSingleObject (catchem[i], 0);
|
|
|
|
if (rc != WAIT_OBJECT_0)
|
|
|
|
rc = WaitForMultipleObjects (3, catchem, FALSE, sig_loop_wait);
|
|
|
|
else
|
|
|
|
rc = i + WAIT_OBJECT_0;
|
2002-11-15 05:35:13 +01:00
|
|
|
(void) SetThreadPriority (GetCurrentThread (), WAIT_SIG_PRIORITY);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* sigproc_terminate sets sig_loop_wait to zero to indicate that
|
2003-08-27 22:42:52 +02:00
|
|
|
this thread should terminate. */
|
2000-02-17 20:38:33 +01:00
|
|
|
if (rc == WAIT_TIMEOUT)
|
2000-10-27 17:38:32 +02:00
|
|
|
{
|
|
|
|
if (!sig_loop_wait)
|
|
|
|
break; // Exiting
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (rc == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
if (sig_loop_wait != 0)
|
|
|
|
system_printf ("WFMO failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc -= WAIT_OBJECT_0;
|
2003-08-19 07:47:44 +02:00
|
|
|
sigproc_printf ("awake, rc %d", rc);
|
2003-08-21 05:18:46 +02:00
|
|
|
LONG *todo;
|
|
|
|
if (rc != RC_NOSYNC)
|
2003-08-29 04:05:00 +02:00
|
|
|
todo = todos[0];
|
2003-08-21 05:18:46 +02:00
|
|
|
else
|
|
|
|
todo = todos[1];
|
2003-08-19 07:47:44 +02:00
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* A sigcatch semaphore has been signaled. Scan the sigtodo
|
2003-08-27 22:42:52 +02:00
|
|
|
array looking for any unprocessed signals. */
|
2003-08-21 05:18:46 +02:00
|
|
|
pending_signals = false;
|
2003-08-27 22:42:52 +02:00
|
|
|
unsigned more_signals = 0;
|
2003-08-22 03:07:01 +02:00
|
|
|
bool saw_failed_interrupt = false;
|
2003-08-21 05:18:46 +02:00
|
|
|
do
|
2003-08-27 22:42:52 +02:00
|
|
|
{
|
|
|
|
more_signals = 0;
|
|
|
|
for (int sig = -__SIGOFFSET; sig < NSIG; sig++)
|
|
|
|
{
|
|
|
|
LONG x = InterlockedDecrement (todo + sig);
|
|
|
|
if (x < 0)
|
|
|
|
InterlockedIncrement (todo + sig);
|
|
|
|
else if (x >= 0)
|
|
|
|
{
|
|
|
|
/* If x > 0, we have to deal with a signal at some later point */
|
|
|
|
if (rc != RC_NOSYNC && x > 0)
|
2003-09-03 16:15:55 +02:00
|
|
|
/*pending_signals = true*/; // There should be an armed semaphore, in this case
|
2003-08-27 22:42:52 +02:00
|
|
|
|
|
|
|
if (sig > 0 && sig != SIGKILL && sig != SIGSTOP &&
|
|
|
|
(sigismember (&myself->getsigmask (), sig) ||
|
|
|
|
main_vfork->pid ||
|
|
|
|
(sig != SIGCONT && ISSTATE (myself, PID_STOPPED))))
|
|
|
|
{
|
|
|
|
sigproc_printf ("signal %d blocked", sig);
|
|
|
|
x = InterlockedIncrement (myself->getsigtodo (sig));
|
2003-09-03 16:15:55 +02:00
|
|
|
/* pending_signals = true;*/ // will be set by set_process_mask
|
2003-08-27 22:42:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sigproc_printf ("processing signal %d", sig);
|
|
|
|
switch (sig)
|
|
|
|
{
|
|
|
|
case __SIGFLUSH:
|
|
|
|
if (rc == RC_MAIN)
|
|
|
|
{
|
|
|
|
flush = true;
|
2003-08-29 04:59:06 +02:00
|
|
|
SetEvent (sigcatch_nosync);
|
2003-08-27 22:42:52 +02:00
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Internal signal to turn on stracing. */
|
|
|
|
case __SIGSTRACE:
|
|
|
|
strace.hello ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case __SIGCOMMUNE:
|
|
|
|
talktome ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* A normal UNIX signal */
|
|
|
|
default:
|
|
|
|
sigproc_printf ("Got signal %d", sig);
|
|
|
|
if (!sig_handle (sig))
|
|
|
|
{
|
2003-09-03 16:15:55 +02:00
|
|
|
pending_signals = true;
|
2003-08-27 22:42:52 +02:00
|
|
|
saw_failed_interrupt = true;
|
|
|
|
x = InterlockedIncrement (myself->getsigtodo (sig));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc == RC_NOSYNC && x > 0)
|
|
|
|
more_signals++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sig == SIGCHLD)
|
|
|
|
proc_subproc (PROC_CLEARWAIT, 0);
|
|
|
|
|
|
|
|
/* Need to take special action if an interrupt failed due to main thread not
|
|
|
|
getting around to calling handler yet. */
|
|
|
|
if (saw_failed_interrupt || rc != RC_NOSYNC)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
if (more_signals > 100)
|
|
|
|
system_printf ("hmm. infinite loop? more_signals %u\n", more_signals);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
while (more_signals && sig_loop_wait);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-08-20 04:31:26 +02:00
|
|
|
out:
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Signal completion of signal handling depending on which semaphore
|
2003-08-20 04:31:26 +02:00
|
|
|
woke up the WaitForMultipleObjects above. */
|
2003-08-20 07:15:33 +02:00
|
|
|
if (rc == RC_NONMAIN) // FIXME: This is broken
|
|
|
|
ReleaseSemaphore (sigcomplete_nonmain, 1, NULL);
|
|
|
|
else if (rc == RC_MAIN || flush)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
SetEvent (sigcomplete_main);
|
2001-03-11 00:37:50 +01:00
|
|
|
sigproc_printf ("set main thread completion event");
|
2003-08-20 07:15:33 +02:00
|
|
|
flush = false;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2003-08-27 22:42:52 +02:00
|
|
|
|
2003-08-20 07:15:33 +02:00
|
|
|
out1:
|
2003-08-20 04:31:26 +02:00
|
|
|
if (saw_failed_interrupt)
|
2003-08-21 05:18:46 +02:00
|
|
|
{
|
2003-08-29 04:59:06 +02:00
|
|
|
SetEvent (sigcatch_nosync);
|
2003-08-21 05:18:46 +02:00
|
|
|
low_priority_sleep (0); /* Hopefully, other thread will be waking up soon. */
|
|
|
|
}
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("looping");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("done");
|
2002-10-09 07:55:40 +02:00
|
|
|
ExitThread (0);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for subprocesses to terminate. Executes in a separate thread. */
|
|
|
|
static DWORD WINAPI
|
2000-02-21 06:20:38 +01:00
|
|
|
wait_subproc (VOID *)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("starting");
|
2000-02-17 20:38:33 +01:00
|
|
|
int errloop = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
DWORD rc = WaitForMultipleObjects (nchildren + 1, events, FALSE,
|
|
|
|
proc_loop_wait);
|
|
|
|
if (rc == WAIT_TIMEOUT)
|
|
|
|
if (!proc_loop_wait)
|
|
|
|
break; // Exiting
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rc == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
if (!proc_loop_wait)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* It's ok to get an ERROR_INVALID_HANDLE since another thread may have
|
|
|
|
closed a handle in the children[] array. So, we try looping a couple
|
|
|
|
of times to stabilize. FIXME - this is not foolproof. Probably, this
|
|
|
|
thread should be responsible for closing the children. */
|
2000-11-04 06:54:57 +01:00
|
|
|
if (!errloop++)
|
|
|
|
proc_subproc (PROC_NOTHING, 0); // Just synchronize and continue
|
|
|
|
if (errloop < 10)
|
2000-02-17 20:38:33 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
system_printf ("wait failed. nchildren %d, wait %d, %E",
|
|
|
|
nchildren, proc_loop_wait);
|
|
|
|
|
2000-11-04 20:25:55 +01:00
|
|
|
for (int i = 0; i <= nchildren; i++)
|
2000-02-17 20:38:33 +01:00
|
|
|
if ((rc = WaitForSingleObject (events[i], 0)) == WAIT_OBJECT_0 ||
|
|
|
|
rc == WAIT_TIMEOUT)
|
|
|
|
continue;
|
2000-11-05 19:47:28 +01:00
|
|
|
else if (i == 0)
|
|
|
|
system_printf ("nchildren %d, event[%d] %p, %E", nchildren, i, events[i]);
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
2000-11-04 02:08:23 +01:00
|
|
|
{
|
2000-11-04 20:25:55 +01:00
|
|
|
system_printf ("nchildren %d, event[%d] %p, pchildren[%d] %p, events[0] %p, %E",
|
2000-11-05 17:47:00 +01:00
|
|
|
nchildren, i, events[i], i - 1, (_pinfo *) pchildren[i - 1], events[0]);
|
2000-11-05 19:47:28 +01:00
|
|
|
system_printf ("pid %d, dwProcessId %u, hProcess %p, progname '%s'",
|
2000-11-05 17:47:00 +01:00
|
|
|
pchildren[i - 1]->pid, pchildren[i - 1]->dwProcessId,
|
2000-11-05 19:47:28 +01:00
|
|
|
pchildren[i - 1]->hProcess, pchildren[i - 1]->progname);
|
2000-11-04 02:08:23 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
errloop = 0;
|
|
|
|
rc -= WAIT_OBJECT_0;
|
|
|
|
if (rc-- != 0)
|
2000-10-15 03:37:07 +02:00
|
|
|
{
|
2000-10-17 20:55:58 +02:00
|
|
|
rc = proc_subproc (PROC_CHILDTERMINATED, rc);
|
2000-10-15 03:37:07 +02:00
|
|
|
if (!proc_loop_wait) // Don't bother if wait_subproc is
|
|
|
|
break; // exiting
|
|
|
|
|
2000-10-17 20:55:58 +02:00
|
|
|
/* Send a SIGCHLD to myself. We do this here, rather than in proc_subproc
|
|
|
|
to avoid the proc_subproc lock since the signal thread will eventually
|
|
|
|
be calling proc_subproc and could unnecessarily block. */
|
|
|
|
if (rc)
|
|
|
|
sig_send (myself_nowait, SIGCHLD);
|
2000-10-15 03:37:07 +02:00
|
|
|
}
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("looping");
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ForceCloseHandle (events[0]);
|
|
|
|
events[0] = NULL;
|
2000-08-02 05:42:31 +02:00
|
|
|
sigproc_printf ("done");
|
2002-10-09 07:55:40 +02:00
|
|
|
ExitThread (0);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-05-17 07:49:51 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
/* Provide a stack frame when calling WaitFor* functions */
|
|
|
|
|
|
|
|
#undef WaitForSingleObject
|
|
|
|
|
|
|
|
DWORD __stdcall
|
|
|
|
WFSO (HANDLE hHandle, DWORD dwMilliseconds)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
sigframe thisframe (mainthread);
|
|
|
|
ret = WaitForSingleObject (hHandle, dwMilliseconds);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef WaitForMultipleObjects
|
|
|
|
|
|
|
|
DWORD __stdcall
|
|
|
|
WFMO (DWORD nCount, CONST HANDLE *lpHandles, BOOL fWaitAll, DWORD dwMilliseconds)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
sigframe thisframe (mainthread);
|
|
|
|
ret = WaitForMultipleObjects (nCount, lpHandles, fWaitAll, dwMilliseconds);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|