1 /* Copyright (C) 1998 Transarc Corporation. All Rights Reserved.
16 #include <afs/errmap_nt.h>
17 #include <afs/secutil_nt.h>
20 #include "pmgtprivate.h"
24 /* Signal disposition table and associated definitions and locks */
27 struct sigaction action; /* signal action information */
30 static sigtable_entry_t signalTable[NSIG]; /* signal table; slot 0 unused */
31 static pthread_mutex_t signalTableLock; /* lock protects signalTable */
33 /* Global signal block lock; all signal handlers are serialized for now */
34 static pthread_mutex_t signalBlockLock;
36 /* Named pipe prefix for sending signals */
37 #define PMGT_SIGNAL_PIPE_PREFIX "\\\\.\\pipe\\TransarcAfsSignalPipe"
39 /* Macro to test process exit status for an uncaught exception */
40 #define PMGT_IS_EXPSTATUS(status) (((status) & 0xF0000000) == 0xC0000000)
44 /* Child process table and associated definitions and locks */
47 HANDLE p_handle; /* process handle (NULL if table entry not valid) */
48 BOOL p_reserved; /* table entry is reserved for spawn */
49 DWORD p_id; /* process id */
50 BOOL p_terminated; /* process terminated; status available/valid */
51 DWORD p_status; /* status of terminated process */
54 /* Max number of active child processes supported */
55 #define PMGT_CHILD_MAX 100
57 static proctable_entry_t procTable[PMGT_CHILD_MAX]; /* child process table */
58 static int procEntryCount; /* count of valid entries in procTable */
59 static int procTermCount; /* count of terminated entries in procTable */
61 /* lock protects procTable, procEntryCount, and procTermCount */
62 static pthread_mutex_t procTableLock;
64 /* Named shared memory prefix for passing a data buffer to a child process */
65 #define PMGT_DATA_MEM_PREFIX "TransarcAfsSpawnDataMemory"
67 /* Named event prefix for indicating that a data buffer has been read */
68 #define PMGT_DATA_EVENT_PREFIX "TransarcAfsSpawnDataEvent"
70 /* event signals termination of a child process */
71 static pthread_cond_t childTermEvent;
74 /* Exported data values */
76 void *pmgt_spawnData = NULL;
77 size_t pmgt_spawnDataLen = 0;
80 /* General definitions */
82 #define DWORD_OF_ONES ((DWORD)0xFFFFFFFF) /* a common Win32 failure code */
89 /* ----------------- Signals ---------------- */
93 * SignalIsDefined() -- Determine if an integer value corresponds to a
94 * signal value defined for this platform.
97 SignalIsDefined(int signo)
101 if (signo >= 1 && signo <= (NSIG - 1)) {
102 /* value is in valid range; check specifics */
126 * DefaultActionHandler() -- Execute the default action for the given signal.
129 DefaultActionHandler(int signo)
138 /* default action is "exit" */
139 ExitProcess(PMGT_SIGSTATUS_ENCODE(signo));
146 /* default action is "core" */
147 /* Best we can do is to raise an exception that can be caught by
148 * Dr. Watson, which can in turn generate a crash dump file.
149 * The default exception handler will call ExitProcess() with
150 * our application-specific exception code.
152 RaiseException((DWORD)PMGT_SIGSTATUS_ENCODE(signo),
153 EXCEPTION_NONCONTINUABLE, 0, NULL);
156 /* default action is "ignore" */
159 /* default action is "stop" */
160 /* No good way to implement this from inside a process so ignore */
163 /* no default action for specified signal value; just ignore */
170 * ProcessSignal() -- Execute the specified or default handler for the given
171 * signal; reset the signal's disposition to SIG_DFL if necessary.
172 * If the signal's disposition is SIG_IGN then no processing takes place.
174 * ASSUMPTIONS: signo is valid (i.e., SignalIsDefined(signo) is TRUE).
177 ProcessSignal(int signo)
179 struct sigaction sigEntry;
181 if (signo != SIGKILL) {
182 /* serialize signals, but never block processing of SIGKILL */
183 (void) pthread_mutex_lock(&signalBlockLock);
186 /* fetch disposition of signo, updating it if necessary */
188 (void) pthread_mutex_lock(&signalTableLock);
189 sigEntry = signalTable[signo].action;
191 if ((sigEntry.sa_handler != SIG_IGN) &&
192 (sigEntry.sa_flags & SA_RESETHAND) &&
194 signalTable[signo].action.sa_handler = SIG_DFL;
196 (void) pthread_mutex_unlock(&signalTableLock);
198 /* execute handler */
200 if (sigEntry.sa_handler != SIG_IGN) {
201 if (sigEntry.sa_handler == SIG_DFL) {
202 sigEntry.sa_handler = DefaultActionHandler;
204 (*sigEntry.sa_handler)(signo);
207 if (signo != SIGKILL) {
208 (void) pthread_mutex_unlock(&signalBlockLock);
214 * RemoteSignalThread() -- Thread spawned to process remote signal.
216 * Param must be the signal number.
219 RemoteSignalThread(LPVOID param)
221 int signo = (int)param;
224 if (SignalIsDefined(signo)) {
226 ProcessSignal(signo);
227 } else if (signo != 0) {
228 /* invalid signal value */
236 * RemoteSignalListenerThread() -- Thread spawned to receive and process
237 * remotely generated signals; never returns.
239 * Param must be a handle for a duplex server message pipe in blocking
243 RemoteSignalListenerThread(LPVOID param)
245 HANDLE sigPipeHandle = (HANDLE)param;
248 /* wait for pipe client to connect */
250 if ((ConnectNamedPipe(sigPipeHandle, NULL)) ||
251 (GetLastError() == ERROR_PIPE_CONNECTED)) {
252 /* client connected; read signal value */
256 if ((ReadFile(sigPipeHandle,
257 &signo, sizeof(signo), &bytesXfered, NULL)) &&
258 (bytesXfered == sizeof(signo))) {
259 HANDLE sigThreadHandle;
262 /* ACK signal to release sender */
263 (void) WriteFile(sigPipeHandle,
264 &signo, sizeof(signo), &bytesXfered, NULL);
266 /* spawn thread to process signal; we do this so that
267 * we can always process a SIGKILL even if a signal handler
268 * invoked earlier fails to return (blocked/spinning).
271 CreateThread(NULL, /* default security attr. */
272 0, /* default stack size */
274 (LPVOID)signo, /* thread argument */
275 0, /* creation flags */
276 &sigThreadId); /* thread id */
278 if (sigThreadHandle != NULL) {
279 (void) CloseHandle(sigThreadHandle);
282 /* nothing to do if ReadFile, WriteFile or CreateThread fails. */
285 /* connect failed; this should never happen */
286 Sleep(2000); /* sleep 2 seconds to avoid tight loop */
289 (void)DisconnectNamedPipe(sigPipeHandle);
301 * pmgt_SigactionSet() -- Examine and/or specify the action for a given
302 * signal (Unix sigaction() semantics).
305 pmgt_SigactionSet(int signo,
306 const struct sigaction *actionP,
307 struct sigaction *old_actionP)
309 /* validate arguments */
311 if (!SignalIsDefined(signo) || signo == SIGKILL) {
312 /* invalid signal value or signal can't be caught/ignored */
317 if (actionP && actionP->sa_handler == SIG_ERR) {
318 /* invalid signal disposition */
323 /* fetch and/or set disposition of signo */
325 (void) pthread_mutex_lock(&signalTableLock);
328 *old_actionP = signalTable[signo].action;
332 signalTable[signo].action = *actionP;
335 (void) pthread_mutex_unlock(&signalTableLock);
342 * pmgt_SignalSet() -- Specify the disposition for a given signal
343 * value (Unix signal() semantics).
345 void (__cdecl *pmgt_SignalSet(int signo,
346 void (__cdecl *dispP)(int)))(int)
348 struct sigaction newAction, oldAction;
350 /* construct action to request Unix signal() semantics */
352 newAction.sa_handler = dispP;
353 sigemptyset(&newAction.sa_mask);
354 newAction.sa_flags = SA_RESETHAND;
356 if (!pmgt_SigactionSet(signo, &newAction, &oldAction)) {
357 /* successfully set new signal action */
358 return oldAction.sa_handler;
360 /* failed to set signal action; errno will have been set */
367 * pmgt_SignalRaiseLocal() -- Raise a signal in this process (C raise()
371 pmgt_SignalRaiseLocal(int signo)
375 /* Process signal directly in the context of the calling thread.
376 * This is the same as if the signal had been raised in this process
377 * and this thread chosen to execute the handler.
380 if (SignalIsDefined(signo)) {
382 ProcessSignal(signo);
383 } else if (signo != 0) {
384 /* invalid signal value */
393 * pmgt_SignalRaiseLocalByName() -- Raise a signal in this process where
394 * the signal is specified by name (C raise() semantics).
396 * Upon successful completion, *libSigno is set to the process management
397 * library's constant value for signame.
399 * Note: exists to implement the native-signal redirector (redirect_nt.c),
400 * which can't include procmgmt.h and hence can't get the SIG* decls.
403 pmgt_SignalRaiseLocalByName(const char *signame, int *libSigno)
408 if (!strcmp(signame, "SIGHUP")) {
410 } else if (!strcmp(signame, "SIGINT")) {
412 } else if (!strcmp(signame, "SIGQUIT")) {
414 } else if (!strcmp(signame, "SIGILL")) {
416 } else if (!strcmp(signame, "SIGABRT")) {
418 } else if (!strcmp(signame, "SIGFPE")) {
420 } else if (!strcmp(signame, "SIGKILL")) {
422 } else if (!strcmp(signame, "SIGSEGV")) {
424 } else if (!strcmp(signame, "SIGTERM")) {
426 } else if (!strcmp(signame, "SIGUSR1")) {
428 } else if (!strcmp(signame, "SIGUSR2")) {
430 } else if (!strcmp(signame, "SIGCLD")) {
432 } else if (!strcmp(signame, "SIGCHLD")) {
434 } else if (!strcmp(signame, "SIGTSTP")) {
437 /* unknown signal name */
444 rc = pmgt_SignalRaiseLocal(signo);
451 * pmgt_SignalRaiseRemote() -- Raise a signal in the specified process (Unix
454 * Note: only supports sending signal to a specific (single) process.
457 pmgt_SignalRaiseRemote(pid_t pid,
461 char sigPipeName[sizeof(PMGT_SIGNAL_PIPE_PREFIX) + 20];
466 /* validate arguments */
468 if ((pid <= (pid_t)0) ||
469 (!SignalIsDefined(signo) && signo != 0)) {
470 /* invalid pid or signo */
475 /* optimize for the "this process" case */
477 if (pid == (pid_t)GetCurrentProcessId()) {
478 return pmgt_SignalRaiseLocal(signo);
481 /* send signal to process via named pipe */
483 sprintf(sigPipeName, "%s%d", PMGT_SIGNAL_PIPE_PREFIX, (int)pid);
486 CallNamedPipe(sigPipeName, /* process pid's signal pipe */
487 &signo, /* data written to pipe */
488 sizeof(signo), /* size of data to write */
489 &signoACK, /* data read from pipe */
490 sizeof(signoACK), /* size of data read buffer */
491 &ackBytesRead, /* number of bytes actually read */
492 5 * 1000); /* 5 second timeout */
495 /* failed to send signal via named pipe */
498 if (signo == SIGKILL) {
499 /* could be a non-AFS process, which might still be kill-able */
502 if (procHandle = OpenProcess(PROCESS_TERMINATE,
503 FALSE, (DWORD)pid)) {
504 if (TerminateProcess(procHandle,
505 PMGT_SIGSTATUS_ENCODE(SIGKILL))) {
506 /* successfully killed process */
509 errno = nterr_nt2unix(GetLastError(), EPERM);
511 (void) CloseHandle(procHandle);
513 if (GetLastError() == ERROR_INVALID_PARAMETER) {
515 } else if (GetLastError() == ERROR_ACCESS_DENIED) {
518 errno = nterr_nt2unix(GetLastError(), EPERM);
522 /* couldn't open pipe so can't send (non-SIGKILL) signal */
523 errno = nterr_nt2unix(GetLastError(), EPERM);
533 /* ----------------- Processes ---------------- */
537 * StringArrayToString() -- convert a null-terminated array of strings,
538 * such as argv, into a single string of space-separated elements
539 * with each element quoted (in case it contains space characters
540 * or is of zero length).
543 StringArrayToString(char *strArray[])
549 for (strCount = 0; strArray[strCount] != NULL; strCount++) {
550 /* sum all string lengths */
551 byteCount += strlen(strArray[strCount]);
554 /* put all strings into buffer; guarantee buffer is at least one char */
555 buffer = (char *)malloc(byteCount + (strCount * 3) /* quotes+space */ + 1);
556 if (buffer != NULL) {
561 for (i = 0; i < strCount; i++) {
562 char *bufp = buffer + strlen(buffer);
564 if (i == strCount - 1) {
565 /* last string; no trailing space */
566 sprintf(bufp, "\"%s\"", strArray[i]);
568 sprintf(bufp, "\"%s\" ", strArray[i]);
578 * StringArrayToMultiString() -- convert a null-terminated array of strings,
579 * such as envp, into a multistring.
582 StringArrayToMultiString(char *strArray[])
588 for (strCount = 0; strArray[strCount] != NULL; strCount++) {
589 /* sum all string lengths */
590 byteCount += strlen(strArray[strCount]);
593 /* put all strings into buffer; guarantee buffer is at least two chars */
594 buffer = (char *)malloc(byteCount + strCount + 2);
595 if (buffer != NULL) {
596 if (byteCount == 0) {
603 for (i = 0; i < strCount; i++) {
604 int strLen = strlen(strArray[i]);
607 /* can not embed zero length string in a multistring */
608 strcpy(bufp, strArray[i]);
612 bufp = '\0'; /* terminate multistring */
622 * ComputeWaitStatus() -- Compute an appropriate wait status value from
623 * a given process termination (exit) code.
626 ComputeWaitStatus(DWORD exitStatus)
630 if (PMGT_IS_SIGSTATUS(exitStatus)) {
631 /* child terminated due to an unhandled signal */
632 int signo = PMGT_SIGSTATUS_DECODE(exitStatus);
633 waitStatus = WSIGNALED_ENCODE(signo);
634 } else if (PMGT_IS_EXPSTATUS(exitStatus)) {
635 /* child terminated due to an uncaught exception */
638 switch (exitStatus) {
639 case EXCEPTION_FLT_DENORMAL_OPERAND:
640 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
641 case EXCEPTION_FLT_INEXACT_RESULT:
642 case EXCEPTION_FLT_INVALID_OPERATION:
643 case EXCEPTION_FLT_OVERFLOW:
644 case EXCEPTION_FLT_STACK_CHECK:
645 case EXCEPTION_FLT_UNDERFLOW:
646 case EXCEPTION_INT_DIVIDE_BY_ZERO:
647 case EXCEPTION_INT_OVERFLOW:
650 case EXCEPTION_PRIV_INSTRUCTION:
651 case EXCEPTION_ILLEGAL_INSTRUCTION:
661 waitStatus = WSIGNALED_ENCODE(signo);
663 /* child terminated normally */
664 waitStatus = WEXITED_ENCODE(exitStatus);
673 * CreateChildDataBuffer() -- Create and fill a named data buffer to pass to
674 * a child process, along with a corresponding buffer read event.
676 * ASSUMPTIONS: child process is linked with this process management library;
677 * otherwise no data transfer will take place.
680 CreateChildDataBuffer(DWORD pid, /* child pid */
681 void *datap, /* data to place in buffer */
682 size_t dataLen, /* size of data in bytes */
683 HANDLE *bufMemHandlep, /* buffer memory handle */
684 HANDLE *bufEventHandlep) /* buffer read event handle */
686 BOOL fsuccess = FALSE;
687 DWORD bufMemSize = dataLen + sizeof(size_t);
688 char bufMemName[sizeof(PMGT_DATA_MEM_PREFIX) + 20];
689 char bufEventName[sizeof(PMGT_DATA_EVENT_PREFIX) + 20];
691 sprintf(bufMemName, "%s%d", PMGT_DATA_MEM_PREFIX, (int)pid);
692 sprintf(bufEventName, "%s%d", PMGT_DATA_EVENT_PREFIX, (int)pid);
694 /* Create and initialize named shared memory and named event */
697 CreateFileMapping((HANDLE)0xFFFFFFFF, /* page-file backed */
704 if (*bufMemHandlep != NULL) {
707 bufMemp = MapViewOfFile(*bufMemHandlep,
708 FILE_MAP_WRITE, 0, 0, bufMemSize);
710 if (bufMemp != NULL) {
711 /* copy data into shared memory, prefixed with data size */
712 size_t *memp = (size_t *)bufMemp;
715 memcpy((void *)memp, datap, dataLen);
717 if (UnmapViewOfFile(bufMemp)) {
718 /* create buffer read event */
719 *bufEventHandlep = CreateEvent(NULL,
720 FALSE /* manual reset */,
721 FALSE /* initial state */,
723 if (*bufEventHandlep != NULL) {
730 (void) CloseHandle(*bufMemHandlep);
735 *bufMemHandlep = *bufEventHandlep = NULL;
743 * ReadChildDataBuffer() -- Read data buffer passed to child from parent,
744 * if any, and place in allocated storage.
747 ReadChildDataBuffer(void **datap, /* allocated data buffer */
748 size_t *dataLen) /* size of data buffer returned */
750 BOOL fsuccess = FALSE;
751 char bufMemName[sizeof(PMGT_DATA_MEM_PREFIX) + 20];
752 char bufEventName[sizeof(PMGT_DATA_EVENT_PREFIX) + 20];
753 HANDLE bufMemHandle, bufEventHandle;
755 sprintf(bufMemName, "%s%d",
756 PMGT_DATA_MEM_PREFIX, (int)GetCurrentProcessId());
757 sprintf(bufEventName, "%s%d",
758 PMGT_DATA_EVENT_PREFIX, (int)GetCurrentProcessId());
760 /* Attempt to open named event and named shared memory */
762 bufEventHandle = OpenEvent(EVENT_MODIFY_STATE, FALSE, bufEventName);
764 if (bufEventHandle != NULL) {
765 bufMemHandle = OpenFileMapping(FILE_MAP_READ, FALSE, bufMemName);
767 if (bufMemHandle != NULL) {
770 bufMemp = MapViewOfFile(bufMemHandle, FILE_MAP_READ, 0, 0, 0);
772 if (bufMemp != NULL) {
773 /* read data size and data from shared memory */
774 size_t *memp = (size_t *)bufMemp;
777 *datap = (void *)malloc(*dataLen);
779 if (*datap != NULL) {
780 memcpy(*datap, (void *)memp, *dataLen);
783 (void) UnmapViewOfFile(bufMemp);
786 (void) CloseHandle(bufMemHandle);
789 (void) SetEvent(bufEventHandle);
790 (void) CloseHandle(bufEventHandle);
803 * ChildMonitorThread() -- Thread spawned to monitor status of child process.
805 * Param must be index into child process table.
808 ChildMonitorThread(LPVOID param)
810 int tidx = (int)param;
811 HANDLE childProcHandle;
815 /* retrieve handle for child process from process table and duplicate */
817 (void) pthread_mutex_lock(&procTableLock);
820 DuplicateHandle(GetCurrentProcess(), /* source process handle */
821 procTable[tidx].p_handle, /* source handle to dup */
822 GetCurrentProcess(), /* target process handle */
823 &childProcHandle, /* target handle (duplicate) */
824 0, /* access (ignored here) */
825 FALSE, /* not inheritable */
826 DUPLICATE_SAME_ACCESS);
828 (void) pthread_mutex_unlock(&procTableLock);
831 /* wait for child process to terminate */
833 if (WaitForSingleObject(childProcHandle, INFINITE) == WAIT_OBJECT_0) {
834 /* child process terminated; mark in table and signal event */
835 (void) pthread_mutex_lock(&procTableLock);
837 procTable[tidx].p_terminated = TRUE;
838 (void) GetExitCodeProcess(childProcHandle,
839 &procTable[tidx].p_status);
842 (void) pthread_mutex_unlock(&procTableLock);
844 (void) pthread_cond_broadcast(&childTermEvent);
846 /* process/raise SIGCHLD; do last in case handler never returns */
847 ProcessSignal(SIGCHLD);
851 (void) CloseHandle(childProcHandle);
854 /* note: nothing can be done if DuplicateHandle() or WaitForSingleObject()
855 * fail; however, this should never happen.
863 * pmgt_ProcessSpawnVEB() -- Spawn a process (Unix fork()/execve() semantics)
865 * Returns pid of the child process ((pid_t)-1 on failure with errno set).
867 * Notes: A senvp value of NULL results in Unix fork()/execv() semantics.
868 * Open files are not inherited; child's stdin, stdout, and stderr
869 * are set to parent's console.
870 * If spath does not specify a filename extension ".exe" is used.
871 * If sdatap is not NULL, and sdatalen > 0, data is passed to child.
872 * The spath and sargv[] strings must not contain quote chars (").
874 * ASSUMPTIONS: sargv[0] is the same as spath (or its last component).
877 pmgt_ProcessSpawnVEB(const char *spath,
884 char *pathbuf, *argbuf, *envbuf;
885 char pathext[_MAX_EXT];
886 STARTUPINFO startInfo;
887 PROCESS_INFORMATION procInfo;
888 HANDLE monitorHandle = NULL;
889 HANDLE bufMemHandle, bufEventHandle;
890 DWORD monitorId, createFlags;
891 BOOL passingBuffer = (sdatap != NULL && sdatalen > 0);
894 /* verify arguments */
895 if (!spath || !sargv) {
898 } else if (*spath == '\0') {
903 /* create path with .exe extension if no filename extension supplied */
904 if (!(pathbuf = (char *)malloc(strlen(spath) + 5 /* .exe */))) {
908 strcpy(pathbuf, spath);
910 _splitpath(pathbuf, NULL, NULL, NULL, pathext);
911 if (*pathext == '\0') {
912 /* no filename extension supplied for spath; .exe is assumed */
913 strcat(pathbuf, ".exe");
916 /* create command line argument string */
917 argbuf = StringArrayToString(sargv);
925 /* create environment variable block (multistring) */
927 /* use environment variables provided */
928 envbuf = StringArrayToMultiString(senvp);
937 /* use default environment variables */
941 /* set process creation flags */
942 createFlags = CREATE_SUSPENDED | NORMAL_PRIORITY_CLASS;
944 if (getenv(PMGT_SPAWN_DETACHED_ENV_NAME) != NULL) {
945 createFlags |= DETACHED_PROCESS;
948 /* clear start-up info; use defaults */
949 memset((void *)&startInfo, 0, sizeof(startInfo));
950 startInfo.cb = sizeof(startInfo);
952 /* perform the following as a logically atomic unit:
953 * 1) allocate a process table entry
954 * 2) spawn child process (suspended)
955 * 3) create data buffer to pass (optional)
956 * 4) initialize process table entry
957 * 5) start child watcher thread
958 * 6) resume spawned child process
961 (void) pthread_mutex_lock(&procTableLock);
963 for (tidx = 0; tidx < PMGT_CHILD_MAX; tidx++) {
964 if (procTable[tidx].p_handle == NULL &&
965 procTable[tidx].p_reserved == FALSE) {
966 procTable[tidx].p_reserved = TRUE;
970 (void) pthread_mutex_unlock(&procTableLock);
972 if (tidx >= PMGT_CHILD_MAX) {
973 /* no space left in process table */
983 CreateProcess(pathbuf, /* executable path */
984 argbuf, /* command line argument string */
985 NULL, /* default process security attr */
986 NULL, /* default thread security attr */
987 FALSE, /* do NOT inherit handles */
988 createFlags, /* creation control flags */
989 envbuf, /* environment variable block */
990 NULL, /* current directory is that of parent */
991 &startInfo, /* startup info block */
999 /* failed to spawn process */
1000 errno = nterr_nt2unix(GetLastError(), ENOENT);
1002 (void) pthread_mutex_lock(&procTableLock);
1003 procTable[tidx].p_reserved = FALSE; /* mark entry as not reserved */
1004 (void) pthread_mutex_unlock(&procTableLock);
1009 if (passingBuffer) {
1010 /* create named data buffer and read event for child */
1011 fsuccess = CreateChildDataBuffer(procInfo.dwProcessId,
1017 (void) pthread_mutex_lock(&procTableLock);
1018 procTable[tidx].p_reserved = FALSE; /* mark entry not reserved */
1019 (void) pthread_mutex_unlock(&procTableLock);
1021 (void) TerminateProcess(procInfo.hProcess,
1022 PMGT_SIGSTATUS_ENCODE(SIGKILL));
1023 (void) CloseHandle(procInfo.hThread);
1024 (void) CloseHandle(procInfo.hProcess);
1031 (void) pthread_mutex_lock(&procTableLock);
1033 procTable[tidx].p_handle = procInfo.hProcess;
1034 procTable[tidx].p_id = procInfo.dwProcessId;
1035 procTable[tidx].p_terminated = FALSE;
1039 /* Note: must hold procTableLock during monitor thread creation so
1040 * that if creation fails we can clean up process table before another
1041 * thread has a chance to see this procTable entry. Continue to hold
1042 * procTableLock while resuming child process, since the procTable
1043 * entry contains a copy of the child process handle which we might use.
1046 CreateThread(NULL, /* default security attr. */
1047 0, /* default stack size */
1049 (LPVOID)tidx, /* thread argument */
1050 0, /* creation flags */
1051 &monitorId); /* thread id */
1053 if (monitorHandle == NULL) {
1054 /* failed to start child monitor thread */
1055 procTable[tidx].p_handle = NULL; /* invalidate table entry */
1056 procTable[tidx].p_reserved = FALSE; /* mark entry as not reserved */
1059 (void) pthread_mutex_unlock(&procTableLock);
1061 (void) TerminateProcess(procInfo.hProcess,
1062 PMGT_SIGSTATUS_ENCODE(SIGKILL));
1063 (void) CloseHandle(procInfo.hThread);
1064 (void) CloseHandle(procInfo.hProcess);
1066 if (passingBuffer) {
1067 (void) CloseHandle(bufMemHandle);
1068 (void) CloseHandle(bufEventHandle);
1075 /* Resume child process, which was created suspended to implement spawn
1076 * atomically. If resumption fails, which it never should, terminate
1077 * the child process with a status of SIGKILL. Spawn still succeeds and
1078 * the net result is the same as if the child process received a spurious
1079 * SIGKILL signal; the child monitor thread will then handle this.
1081 if (ResumeThread(procInfo.hThread) == DWORD_OF_ONES) {
1082 (void) TerminateProcess(procInfo.hProcess,
1083 PMGT_SIGSTATUS_ENCODE(SIGKILL));
1085 if (passingBuffer) {
1086 /* child will never read data buffer */
1087 (void) SetEvent(bufEventHandle);
1091 (void) pthread_mutex_unlock(&procTableLock);
1093 (void) CloseHandle(procInfo.hThread);
1094 (void) CloseHandle(monitorHandle);
1096 /* After spawn returns, signals can not be sent to the new child process
1097 * until that child initializes its signal-receiving mechanism (assuming
1098 * the child is linked with this library). Shorten (but sadly don't
1099 * eliminate) this window of opportunity for failure by yielding this
1100 * thread's time slice.
1102 (void) SwitchToThread();
1104 /* If passing a data buffer to child, wait until child reads buffer
1105 * before closing handles and thus freeing resources; if don't wait
1106 * then parent can not safely exit immediately after returning from
1107 * this call (hence why wait is not done in a background thread).
1109 if (passingBuffer) {
1110 WaitForSingleObject(bufEventHandle, 10000);
1111 /* note: if wait times out, child may not get to read buffer */
1112 (void) CloseHandle(bufMemHandle);
1113 (void) CloseHandle(bufEventHandle);
1116 return (pid_t)procInfo.dwProcessId;
1122 * pmgt_ProcessWaitPid() -- Wait for child process status; i.e., wait
1123 * for child to terminate (Unix waitpid() semantics).
1125 * Note: does not support waiting for process in group (i.e., pid
1126 * equals (pid_t)0 or pid is less than (pid_t)-1.
1129 pmgt_ProcessWaitPid(pid_t pid,
1135 BOOL statusFound = FALSE;
1138 /* validate arguments */
1139 if (pid < (pid_t)-1 || pid == (pid_t)0) {
1144 /* determine how long caller is willing to wait for child */
1146 waitTime = (options & WNOHANG) ? 0 : INFINITE;
1148 /* get child status */
1150 (void) pthread_mutex_lock(&procTableLock);
1153 BOOL waitForChild = FALSE;
1155 if (procEntryCount == 0) {
1156 /* no child processes */
1160 /* determine if status is available for specified child id */
1162 if (pid == (pid_t)-1) {
1163 /* CASE 1: pid matches any child id */
1165 if (procTermCount == 0) {
1166 /* status not available for any child ... */
1167 if (waitTime == 0) {
1168 /* ... and caller is not willing to wait */
1171 /* ... but caller is willing to wait */
1172 waitForChild = TRUE;
1175 /* status available for some child; locate table entry */
1176 for (tidx = 0; tidx < PMGT_CHILD_MAX; tidx++) {
1177 if (procTable[tidx].p_handle != NULL &&
1178 procTable[tidx].p_terminated == TRUE) {
1185 /* should never happen; indicates a bug */
1186 errno = EINTR; /* plausible lie for failure */
1192 /* CASE 2: pid must match a specific child id */
1194 /* locate table entry */
1195 for (tidx = 0; tidx < PMGT_CHILD_MAX; tidx++) {
1196 if (procTable[tidx].p_handle != NULL &&
1197 procTable[tidx].p_id == (DWORD)pid) {
1202 if (tidx >= PMGT_CHILD_MAX) {
1203 /* pid does not match any child id */
1206 } else if (procTable[tidx].p_terminated == FALSE) {
1207 /* status not available for specified child ... */
1208 if (waitTime == 0) {
1209 /* ... and caller is not willing to wait */
1212 /* ... but caller is willing to wait */
1213 waitForChild = TRUE;
1216 /* status is available for specified child */
1223 (void) pthread_cond_wait(&childTermEvent, &procTableLock);
1230 /* child status available */
1232 *statusP = ComputeWaitStatus(procTable[tidx].p_status);
1234 rc = (pid_t)procTable[tidx].p_id;
1236 /* clean up process table */
1237 (void) CloseHandle(procTable[tidx].p_handle);
1238 procTable[tidx].p_handle = NULL;
1239 procTable[tidx].p_reserved = FALSE;
1245 (void) pthread_mutex_unlock(&procTableLock);
1254 /* ----------------- General ---------------- */
1259 * PmgtLibraryInitialize() -- Initialize process management library.
1262 PmgtLibraryInitialize(void)
1265 HANDLE sigPipeHandle;
1266 char sigPipeName[sizeof(PMGT_SIGNAL_PIPE_PREFIX) + 20];
1267 HANDLE sigListenerHandle;
1268 DWORD sigListenerId;
1270 /* initialize mutex locks and condition variables */
1272 if ((rc = pthread_mutex_init(&signalTableLock, NULL)) ||
1273 (rc = pthread_mutex_init(&signalBlockLock, NULL)) ||
1274 (rc = pthread_mutex_init(&procTableLock, NULL)) ||
1275 (rc = pthread_cond_init(&childTermEvent, NULL))) {
1280 /* initialize signal disposition table */
1282 for (i = 0; i < NSIG; i++) {
1283 if (SignalIsDefined(i)) {
1284 /* initialize to default action for defined signals */
1285 signalTable[i].action.sa_handler = SIG_DFL;
1286 sigemptyset(&signalTable[i].action.sa_mask);
1287 signalTable[i].action.sa_flags = 0;
1289 /* initialize to ignore for undefined signals */
1290 signalTable[i].action.sa_handler = SIG_IGN;
1294 /* initialize child process table */
1296 for (i = 0; i < PMGT_CHILD_MAX; i++) {
1297 procTable[i].p_handle = NULL;
1298 procTable[i].p_reserved = FALSE;
1303 /* retrieve data buffer passed from parent in spawn, if any */
1305 if (!ReadChildDataBuffer(&pmgt_spawnData, &pmgt_spawnDataLen)) {
1306 pmgt_spawnData = NULL;
1307 pmgt_spawnDataLen = 0;
1310 /* create named pipe for delivering signals to this process */
1312 sprintf(sigPipeName, "%s%d",
1313 PMGT_SIGNAL_PIPE_PREFIX, (int)GetCurrentProcessId());
1316 CreateNamedPipe(sigPipeName, /* pipe for this process */
1317 PIPE_ACCESS_DUPLEX | /* full duplex pipe */
1318 WRITE_DAC, /* DACL write access */
1319 PIPE_TYPE_MESSAGE | /* message type pipe */
1320 PIPE_READMODE_MESSAGE | /* message read-mode */
1321 PIPE_WAIT, /* blocking mode */
1322 1, /* max of 1 pipe instance */
1323 64, /* output buffer size (advisory) */
1324 64, /* input buffer size (advisory) */
1325 1000, /* 1 sec default client timeout */
1326 NULL); /* default security attr. */
1328 if (sigPipeHandle == INVALID_HANDLE_VALUE) {
1329 /* failed to create signal pipe */
1330 errno = nterr_nt2unix(GetLastError(), EIO);
1334 /* add entry to signal pipe ACL granting local Administrators R/W access */
1336 (void) ObjectDaclEntryAdd(sigPipeHandle,
1338 LocalAdministratorsGroup,
1339 GENERIC_READ | GENERIC_WRITE,
1343 /* start signal pipe listener thread */
1346 CreateThread(NULL, /* default security attr. */
1347 0, /* default stack size */
1348 RemoteSignalListenerThread,
1349 (LPVOID)sigPipeHandle, /* thread argument */
1350 0, /* creation flags */
1351 &sigListenerId); /* thread id */
1353 if (sigListenerHandle != NULL) {
1354 /* listener thread started; bump priority */
1355 (void) SetThreadPriority(sigListenerHandle, THREAD_PRIORITY_HIGHEST);
1356 (void) CloseHandle(sigListenerHandle);
1358 /* failed to start listener thread */
1360 (void) CloseHandle(sigPipeHandle);
1364 /* redirect native NT signals into this process management library */
1366 if (pmgt_RedirectNativeSignals()) {
1367 /* errno set by called function */
1376 * DllMain() -- Entry-point function called by the DllMainCRTStartup()
1377 * function in the MSVC runtime DLL (msvcrt.dll).
1379 * Note: the system serializes calls to this function.
1382 DllMain(HINSTANCE dllInstHandle, /* instance handle for this DLL module */
1383 DWORD reason, /* reason function is being called */
1384 LPVOID reserved) /* reserved for future use */
1386 if (reason == DLL_PROCESS_ATTACH) {
1387 /* library is being attached to a process */
1388 if (PmgtLibraryInitialize()) {
1389 /* failed to initialize library */
1393 /* disable thread attach/detach notifications */
1394 (void) DisableThreadLibraryCalls(dllInstHandle);