📄 sigproc.cc
字号:
/* sigproc.cc: inter/intra signal and sub process handler Copyright 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. Written by Christopher Faylor <cgf@cygnus.com>This file is part of Cygwin.This software is a copyrighted work licensed under the terms of theCygwin license. Please consult the file "CYGWIN_LICENSE" fordetails. */#include "winsup.h"#include <stdlib.h>#include <time.h>#include <sys/wait.h>#include <errno.h>#include <stdlib.h>#include <sys/cygwin.h>#include <assert.h>#include "cygerrno.h"#include "sync.h"#include "sigproc.h"#include "pinfo.h"#include "security.h"#include "fhandler.h"#include "path.h"#include "dtable.h"#include "cygheap.h"#include "child_info_magic.h"#define NEED_VFORK#include "perthread.h"#include "shared_info.h"#include "cygthread.h"/* * 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)#define NZOMBIES 256/* * 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 requiredchar 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_COPYStatic DWORD proc_loop_wait = 1000; // Wait for subprocesses to exitStatic DWORD sig_loop_wait = INFINITE; // Wait for signals to arriveStatic HANDLE sigcatch_nonmain; // The semaphore signaled when // signals are available for // processing from non-main threadStatic HANDLE sigcatch_main; // Signalled when main thread sends a // signalStatic HANDLE sigcatch_nosync; // Signal wait_sig to scan sigtodo // but not to bother with any // synchronizationStatic HANDLE sigcomplete_main; // Event signaled when a signal has // finished processing for the main // threadStatic HANDLE sigcomplete_nonmain; // Semaphore raised for non-main // threads when a signal has finished // processingHANDLE NO_COPY sigCONT; // Used to "STOP" a processStatic cygthread *hwait_sig; // Handle of wait_sig threadStatic cygthread *hwait_subproc; // Handle of sig_subproc threadStatic HANDLE wait_sig_inited; // Control synchronization of // message queue startup/* Used by WaitForMultipleObjects. These are handles to child processes. */Static HANDLE events[PSIZE + 1]; // All my children's handles++#define hchildren (events + 1) // Where the children handles beginStatic char cpchildren[PSIZE * sizeof (pinfo)]; // All my children infoStatic int nchildren; // Number of active childrenStatic char czombies[(NZOMBIES + 1) * sizeof (pinfo)]; // All my deceased children infoStatic int nzombies; // Number of deceased children#define pchildren ((pinfo *) cpchildren)#define zombies ((pinfo *) czombies)Static waitq waitq_head = {0, 0, 0, 0, 0, 0, 0};// Start of queue for wait'ing threadsStatic waitq waitq_main; // Storage for main threadmuto NO_COPY *sync_proc_subproc = NULL; // Control access to subproc stuffDWORD NO_COPY sigtid = 0; // ID of the signal threadint NO_COPY pending_signals = 0; // TRUE if signals pending/* Functions */static int __stdcall checkstate (waitq *);static __inline__ BOOL get_proc_lock (DWORD, DWORD);static HANDLE __stdcall getsem (_pinfo *, const char *, int, int);static void __stdcall remove_zombie (int);static DWORD WINAPI wait_sig (VOID *arg);static int __stdcall stopped_or_terminated (waitq *, _pinfo *);static DWORD WINAPI wait_subproc (VOID *);/* Determine if the parent process is alive. */BOOL __stdcallmy_parent_is_alive (){ DWORD res; if (!myself->ppid_handle) { debug_printf ("No myself->ppid_handle"); res = FALSE; } else for (int i = 0; i < 2; i++) switch (res = WaitForSingleObject (myself->ppid_handle, 0)) { 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; system_printf ("WFSO for myself->ppid_handle(%p) failed, error %d", myself->ppid_handle, werr); res = FALSE; goto out; }out: return res;}void __stdcallwait_for_sigthread (){ assert (wait_sig_inited); (void) WaitForSingleObject (wait_sig_inited, INFINITE); (void) ForceCloseHandle (wait_sig_inited); wait_sig_inited = NULL;}/* 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 BOOLget_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;}static BOOL __stdcallproc_can_be_signalled (_pinfo *p){ if (p == myself_nowait || p == myself_nowait_nonmain || p == myself) { assert (!wait_sig_inited); return 1; } return ISSTATE (p, PID_INITIALIZING) || (((p)->process_state & (PID_ACTIVE | PID_IN_USE)) == (PID_ACTIVE | PID_IN_USE));}BOOL __stdcallpid_exists (pid_t pid){ pinfo p (pid); return proc_exists (p);}/* Test to determine if a process really exists and is processing signals. */BOOL __stdcallproc_exists (_pinfo *p){ return p && !(p->process_state & PID_EXITED);}/* 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 __stdcallmychild (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;}/* Handle all subprocess requests */#define vchild (*((pinfo *) val))int __stdcallproc_subproc (DWORD what, DWORD val){ int rc = 1; int potential_match; _pinfo *child; int clearing; waitq *w;#define wval ((waitq *) val) sigproc_printf ("args: %x, %d", what, val); if (!get_proc_lock (what, val)) // Serialize access to this function { system_printf ("couldn't get proc lock. Something is wrong."); 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) { rc = 0; break; } pchildren[nchildren] = vchild; hchildren[nchildren] = vchild->hProcess; if (!DuplicateHandle (hMainProc, vchild->hProcess, hMainProc, &vchild->pid_handle, 0, 0, DUPLICATE_SAME_ACCESS)) system_printf ("Couldn't duplicate child handle for pid %d, %E", vchild->pid); ProtectHandle1 (vchild->pid_handle, pid_handle); 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; vchild->uid = myself->uid; 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); sigproc_printf ("added pid %d to wait list, slot %d, winpid %p, handle %p", vchild->pid, nchildren, vchild->dwProcessId, vchild->hProcess); nchildren++; wake_wait_subproc (); break; /* A child process had terminated. 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) */ case PROC_CHILDTERMINATED: if (hchildren[val] != pchildren[val]->hProcess) { sigproc_printf ("pid %d[%d], reparented old hProcess %p, new %p", pchildren[val]->pid, val, hchildren[val], pchildren[val]->hProcess); HANDLE h = hchildren[val]; hchildren[val] = pchildren[val]->hProcess; /* Filled out by child */ sync_proc_subproc->release (); // Release the lock ASAP ForceCloseHandle1 (h, childhProc); ProtectHandle1 (pchildren[val]->hProcess, childhProc); rc = 0; goto out; // This was an exec() } sigproc_printf ("pid %d[%d] terminated, handle %p, nchildren %d, nzombies %d", pchildren[val]->pid, val, hchildren[val], nchildren, nzombies); int thiszombie; thiszombie = nzombies; zombies[nzombies] = pchildren[val]; // Add to zombie array zombies[nzombies++]->process_state = PID_ZOMBIE;// Walking dead sigproc_printf ("zombifying [%d], pid %d, handle %p, nchildren %d", val, pchildren[val]->pid, hchildren[val], nchildren); if ((int) val < --nchildren) { hchildren[val] = hchildren[nchildren]; pchildren[val] = pchildren[nchildren]; } /* 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. */ if (nzombies >= NZOMBIES || myself->getsig (SIGCHLD).sa_handler == (void *) SIG_IGN) { sigproc_printf ("automatically removing zombie %d", thiszombie); remove_zombie (thiszombie); } /* Don't scan the wait queue yet. Caller will send SIGCHLD to this process. This will cause an eventual scan of waiters. */ break; /* 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; /* 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. */ if (val) sigproc_printf ("clear waiting threads"); else sigproc_printf ("looking for processes to reap"); clearing = val; 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) sigproc_printf ("released waiting thread"); else if (!clearing && !(w->next->options & WNOHANG) && potential_match < 0) sigproc_printf ("only found non-terminated children"); else if (potential_match <= 0) // nothing matched { sigproc_printf ("waiting thread found no children"); HANDLE oldw = w->next->ev; w->next->pid = 0; if (clearing) w->next->status = -1; /* flag that a signal was received */ else if (!potential_match || !(w->next->options & WNOHANG)) w->next->ev = NULL; if (!SetEvent (oldw)) system_printf ("couldn't wake up wait event %p, %E", oldw); w->next = w->next->next; } if (w->next == NULL) break; } if (!clearing) sigproc_printf ("finished processing terminated/stopped child"); else { waitq_head.next = NULL; sigproc_printf ("finished clearing"); } break; }out: sync_proc_subproc->release (); // Release the lock
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -