2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
20 #include <afs/errmap_nt.h>
21 #include <afs/secutil_nt.h>
24 #include "pmgtprivate.h"
28 /* Signal disposition table and associated definitions and locks */
31 struct sigaction action; /* signal action information */
34 static sigtable_entry_t signalTable[NSIG]; /* signal table; slot 0 unused */
35 static pthread_mutex_t signalTableLock; /* lock protects signalTable */
37 /* Global signal block lock; all signal handlers are serialized for now */
38 static pthread_mutex_t signalBlockLock;
40 /* Named pipe prefix for sending signals */
41 #define PMGT_SIGNAL_PIPE_PREFIX "\\\\.\\pipe\\TransarcAfsSignalPipe"
43 /* Macro to test process exit status for an uncaught exception */
44 #define PMGT_IS_EXPSTATUS(status) (((status) & 0xF0000000) == 0xC0000000)
48 /* Child process table and associated definitions and locks */
51 HANDLE p_handle; /* process handle (NULL if table entry not valid) */
52 BOOL p_reserved; /* table entry is reserved for spawn */
53 DWORD p_id; /* process id */
54 BOOL p_terminated; /* process terminated; status available/valid */
55 DWORD p_status; /* status of terminated process */
58 /* Max number of active child processes supported */
59 #define PMGT_CHILD_MAX 100
61 static proctable_entry_t procTable[PMGT_CHILD_MAX]; /* child process table */
62 static int procEntryCount; /* count of valid entries in procTable */
63 static int procTermCount; /* count of terminated entries in procTable */
65 /* lock protects procTable, procEntryCount, and procTermCount */
66 static pthread_mutex_t procTableLock;
68 /* Named shared memory prefix for passing a data buffer to a child process */
69 #define PMGT_DATA_MEM_PREFIX "TransarcAfsSpawnDataMemory"
71 /* Named event prefix for indicating that a data buffer has been read */
72 #define PMGT_DATA_EVENT_PREFIX "TransarcAfsSpawnDataEvent"
74 /* event signals termination of a child process */
75 static pthread_cond_t childTermEvent;
78 /* Exported data values */
80 void *pmgt_spawnData = NULL;
81 size_t pmgt_spawnDataLen = 0;
84 /* General definitions */
86 #define DWORD_OF_ONES ((DWORD)0xFFFFFFFF) /* a common Win32 failure code */
93 /* ----------------- Signals ---------------- */
97 * SignalIsDefined() -- Determine if an integer value corresponds to a
98 * signal value defined for this platform.
101 SignalIsDefined(int signo)
105 if (signo >= 1 && signo <= (NSIG - 1)) {
106 /* value is in valid range; check specifics */
130 * DefaultActionHandler() -- Execute the default action for the given signal.
133 DefaultActionHandler(int signo)
142 /* default action is "exit" */
143 ExitProcess(PMGT_SIGSTATUS_ENCODE(signo));
150 /* default action is "core" */
151 /* Best we can do is to raise an exception that can be caught by
152 * Dr. Watson, which can in turn generate a crash dump file.
153 * The default exception handler will call ExitProcess() with
154 * our application-specific exception code.
156 RaiseException((DWORD)PMGT_SIGSTATUS_ENCODE(signo),
157 EXCEPTION_NONCONTINUABLE, 0, NULL);
160 /* default action is "ignore" */
163 /* default action is "stop" */
164 /* No good way to implement this from inside a process so ignore */
167 /* no default action for specified signal value; just ignore */
174 * ProcessSignal() -- Execute the specified or default handler for the given
175 * signal; reset the signal's disposition to SIG_DFL if necessary.
176 * If the signal's disposition is SIG_IGN then no processing takes place.
178 * ASSUMPTIONS: signo is valid (i.e., SignalIsDefined(signo) is TRUE).
181 ProcessSignal(int signo)
183 struct sigaction sigEntry;
185 if (signo != SIGKILL) {
186 /* serialize signals, but never block processing of SIGKILL */
187 (void) pthread_mutex_lock(&signalBlockLock);
190 /* fetch disposition of signo, updating it if necessary */
192 (void) pthread_mutex_lock(&signalTableLock);
193 sigEntry = signalTable[signo].action;
195 if ((sigEntry.sa_handler != SIG_IGN) &&
196 (sigEntry.sa_flags & SA_RESETHAND) &&
198 signalTable[signo].action.sa_handler = SIG_DFL;
200 (void) pthread_mutex_unlock(&signalTableLock);
202 /* execute handler */
204 if (sigEntry.sa_handler != SIG_IGN) {
205 if (sigEntry.sa_handler == SIG_DFL) {
206 sigEntry.sa_handler = DefaultActionHandler;
208 (*sigEntry.sa_handler)(signo);
211 if (signo != SIGKILL) {
212 (void) pthread_mutex_unlock(&signalBlockLock);
218 * RemoteSignalThread() -- Thread spawned to process remote signal.
220 * Param must be the signal number.
223 RemoteSignalThread(LPVOID param)
225 int signo = (int)param;
228 if (SignalIsDefined(signo)) {
230 ProcessSignal(signo);
231 } else if (signo != 0) {
232 /* invalid signal value */
240 * RemoteSignalListenerThread() -- Thread spawned to receive and process
241 * remotely generated signals; never returns.
243 * Param must be a handle for a duplex server message pipe in blocking
247 RemoteSignalListenerThread(LPVOID param)
249 HANDLE sigPipeHandle = (HANDLE)param;
252 /* wait for pipe client to connect */
254 if ((ConnectNamedPipe(sigPipeHandle, NULL)) ||
255 (GetLastError() == ERROR_PIPE_CONNECTED)) {
256 /* client connected; read signal value */
260 if ((ReadFile(sigPipeHandle,
261 &signo, sizeof(signo), &bytesXfered, NULL)) &&
262 (bytesXfered == sizeof(signo))) {
263 HANDLE sigThreadHandle;
266 /* ACK signal to release sender */
267 (void) WriteFile(sigPipeHandle,
268 &signo, sizeof(signo), &bytesXfered, NULL);
270 /* spawn thread to process signal; we do this so that
271 * we can always process a SIGKILL even if a signal handler
272 * invoked earlier fails to return (blocked/spinning).
275 CreateThread(NULL, /* default security attr. */
276 0, /* default stack size */
278 (LPVOID)signo, /* thread argument */
279 0, /* creation flags */
280 &sigThreadId); /* thread id */
282 if (sigThreadHandle != NULL) {
283 (void) CloseHandle(sigThreadHandle);
286 /* nothing to do if ReadFile, WriteFile or CreateThread fails. */
289 /* connect failed; this should never happen */
290 Sleep(2000); /* sleep 2 seconds to avoid tight loop */
293 (void)DisconnectNamedPipe(sigPipeHandle);
305 * pmgt_SigactionSet() -- Examine and/or specify the action for a given
306 * signal (Unix sigaction() semantics).
309 pmgt_SigactionSet(int signo,
310 const struct sigaction *actionP,
311 struct sigaction *old_actionP)
313 /* validate arguments */
315 if (!SignalIsDefined(signo) || signo == SIGKILL) {
316 /* invalid signal value or signal can't be caught/ignored */
321 if (actionP && actionP->sa_handler == SIG_ERR) {
322 /* invalid signal disposition */
327 /* fetch and/or set disposition of signo */
329 (void) pthread_mutex_lock(&signalTableLock);
332 *old_actionP = signalTable[signo].action;
336 signalTable[signo].action = *actionP;
339 (void) pthread_mutex_unlock(&signalTableLock);
346 * pmgt_SignalSet() -- Specify the disposition for a given signal
347 * value (Unix signal() semantics).
349 void (__cdecl *pmgt_SignalSet(int signo,
350 void (__cdecl *dispP)(int)))(int)
352 struct sigaction newAction, oldAction;
354 /* construct action to request Unix signal() semantics */
356 newAction.sa_handler = dispP;
357 sigemptyset(&newAction.sa_mask);
358 newAction.sa_flags = SA_RESETHAND;
360 if (!pmgt_SigactionSet(signo, &newAction, &oldAction)) {
361 /* successfully set new signal action */
362 return oldAction.sa_handler;
364 /* failed to set signal action; errno will have been set */
371 * pmgt_SignalRaiseLocal() -- Raise a signal in this process (C raise()
375 pmgt_SignalRaiseLocal(int signo)
379 /* Process signal directly in the context of the calling thread.
380 * This is the same as if the signal had been raised in this process
381 * and this thread chosen to execute the handler.
384 if (SignalIsDefined(signo)) {
386 ProcessSignal(signo);
387 } else if (signo != 0) {
388 /* invalid signal value */
397 * pmgt_SignalRaiseLocalByName() -- Raise a signal in this process where
398 * the signal is specified by name (C raise() semantics).
400 * Upon successful completion, *libSigno is set to the process management
401 * library's constant value for signame.
403 * Note: exists to implement the native-signal redirector (redirect_nt.c),
404 * which can't include procmgmt.h and hence can't get the SIG* decls.
407 pmgt_SignalRaiseLocalByName(const char *signame, int *libSigno)
412 if (!strcmp(signame, "SIGHUP")) {
414 } else if (!strcmp(signame, "SIGINT")) {
416 } else if (!strcmp(signame, "SIGQUIT")) {
418 } else if (!strcmp(signame, "SIGILL")) {
420 } else if (!strcmp(signame, "SIGABRT")) {
422 } else if (!strcmp(signame, "SIGFPE")) {
424 } else if (!strcmp(signame, "SIGKILL")) {
426 } else if (!strcmp(signame, "SIGSEGV")) {
428 } else if (!strcmp(signame, "SIGTERM")) {
430 } else if (!strcmp(signame, "SIGUSR1")) {
432 } else if (!strcmp(signame, "SIGUSR2")) {
434 } else if (!strcmp(signame, "SIGCLD")) {
436 } else if (!strcmp(signame, "SIGCHLD")) {
438 } else if (!strcmp(signame, "SIGTSTP")) {
441 /* unknown signal name */
448 rc = pmgt_SignalRaiseLocal(signo);
455 * pmgt_SignalRaiseRemote() -- Raise a signal in the specified process (Unix
458 * Note: only supports sending signal to a specific (single) process.
461 pmgt_SignalRaiseRemote(pid_t pid,
465 char sigPipeName[sizeof(PMGT_SIGNAL_PIPE_PREFIX) + 20];
470 /* validate arguments */
472 if ((pid <= (pid_t)0) ||
473 (!SignalIsDefined(signo) && signo != 0)) {
474 /* invalid pid or signo */
479 /* optimize for the "this process" case */
481 if (pid == (pid_t)GetCurrentProcessId()) {
482 return pmgt_SignalRaiseLocal(signo);
485 /* send signal to process via named pipe */
487 sprintf(sigPipeName, "%s%d", PMGT_SIGNAL_PIPE_PREFIX, (int)pid);
490 CallNamedPipe(sigPipeName, /* process pid's signal pipe */
491 &signo, /* data written to pipe */
492 sizeof(signo), /* size of data to write */
493 &signoACK, /* data read from pipe */
494 sizeof(signoACK), /* size of data read buffer */
495 &ackBytesRead, /* number of bytes actually read */
496 5 * 1000); /* 5 second timeout */
499 /* failed to send signal via named pipe */
502 if (signo == SIGKILL) {
503 /* could be a non-AFS process, which might still be kill-able */
506 if (procHandle = OpenProcess(PROCESS_TERMINATE,
507 FALSE, (DWORD)pid)) {
508 if (TerminateProcess(procHandle,
509 PMGT_SIGSTATUS_ENCODE(SIGKILL))) {
510 /* successfully killed process */
513 errno = nterr_nt2unix(GetLastError(), EPERM);
515 (void) CloseHandle(procHandle);
517 if (GetLastError() == ERROR_INVALID_PARAMETER) {
519 } else if (GetLastError() == ERROR_ACCESS_DENIED) {
522 errno = nterr_nt2unix(GetLastError(), EPERM);
526 /* couldn't open pipe so can't send (non-SIGKILL) signal */
527 errno = nterr_nt2unix(GetLastError(), EPERM);
537 /* ----------------- Processes ---------------- */
541 * StringArrayToString() -- convert a null-terminated array of strings,
542 * such as argv, into a single string of space-separated elements
543 * with each element quoted (in case it contains space characters
544 * or is of zero length).
547 StringArrayToString(char *strArray[])
553 for (strCount = 0; strArray[strCount] != NULL; strCount++) {
554 /* sum all string lengths */
555 byteCount += strlen(strArray[strCount]);
558 /* put all strings into buffer; guarantee buffer is at least one char */
559 buffer = (char *)malloc(byteCount + (strCount * 3) /* quotes+space */ + 1);
560 if (buffer != NULL) {
565 for (i = 0; i < strCount; i++) {
566 char *bufp = buffer + strlen(buffer);
568 if (i == strCount - 1) {
569 /* last string; no trailing space */
570 sprintf(bufp, "\"%s\"", strArray[i]);
572 sprintf(bufp, "\"%s\" ", strArray[i]);
582 * StringArrayToMultiString() -- convert a null-terminated array of strings,
583 * such as envp, into a multistring.
586 StringArrayToMultiString(char *strArray[])
592 for (strCount = 0; strArray[strCount] != NULL; strCount++) {
593 /* sum all string lengths */
594 byteCount += strlen(strArray[strCount]);
597 /* put all strings into buffer; guarantee buffer is at least two chars */
598 buffer = (char *)malloc(byteCount + strCount + 2);
599 if (buffer != NULL) {
600 if (byteCount == 0) {
607 for (i = 0; i < strCount; i++) {
608 int strLen = strlen(strArray[i]);
611 /* can not embed zero length string in a multistring */
612 strcpy(bufp, strArray[i]);
616 bufp = '\0'; /* terminate multistring */
626 * ComputeWaitStatus() -- Compute an appropriate wait status value from
627 * a given process termination (exit) code.
630 ComputeWaitStatus(DWORD exitStatus)
634 if (PMGT_IS_SIGSTATUS(exitStatus)) {
635 /* child terminated due to an unhandled signal */
636 int signo = PMGT_SIGSTATUS_DECODE(exitStatus);
637 waitStatus = WSIGNALED_ENCODE(signo);
638 } else if (PMGT_IS_EXPSTATUS(exitStatus)) {
639 /* child terminated due to an uncaught exception */
642 switch (exitStatus) {
643 case EXCEPTION_FLT_DENORMAL_OPERAND:
644 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
645 case EXCEPTION_FLT_INEXACT_RESULT:
646 case EXCEPTION_FLT_INVALID_OPERATION:
647 case EXCEPTION_FLT_OVERFLOW:
648 case EXCEPTION_FLT_STACK_CHECK:
649 case EXCEPTION_FLT_UNDERFLOW:
650 case EXCEPTION_INT_DIVIDE_BY_ZERO:
651 case EXCEPTION_INT_OVERFLOW:
654 case EXCEPTION_PRIV_INSTRUCTION:
655 case EXCEPTION_ILLEGAL_INSTRUCTION:
665 waitStatus = WSIGNALED_ENCODE(signo);
667 /* child terminated normally */
668 waitStatus = WEXITED_ENCODE(exitStatus);
677 * CreateChildDataBuffer() -- Create and fill a named data buffer to pass to
678 * a child process, along with a corresponding buffer read event.
680 * ASSUMPTIONS: child process is linked with this process management library;
681 * otherwise no data transfer will take place.
684 CreateChildDataBuffer(DWORD pid, /* child pid */
685 void *datap, /* data to place in buffer */
686 size_t dataLen, /* size of data in bytes */
687 HANDLE *bufMemHandlep, /* buffer memory handle */
688 HANDLE *bufEventHandlep) /* buffer read event handle */
690 BOOL fsuccess = FALSE;
691 DWORD bufMemSize = dataLen + sizeof(size_t);
692 char bufMemName[sizeof(PMGT_DATA_MEM_PREFIX) + 20];
693 char bufEventName[sizeof(PMGT_DATA_EVENT_PREFIX) + 20];
695 sprintf(bufMemName, "%s%d", PMGT_DATA_MEM_PREFIX, (int)pid);
696 sprintf(bufEventName, "%s%d", PMGT_DATA_EVENT_PREFIX, (int)pid);
698 /* Create and initialize named shared memory and named event */
701 CreateFileMapping((HANDLE)0xFFFFFFFF, /* page-file backed */
708 if (*bufMemHandlep != NULL) {
711 bufMemp = MapViewOfFile(*bufMemHandlep,
712 FILE_MAP_WRITE, 0, 0, bufMemSize);
714 if (bufMemp != NULL) {
715 /* copy data into shared memory, prefixed with data size */
716 size_t *memp = (size_t *)bufMemp;
719 memcpy((void *)memp, datap, dataLen);
721 if (UnmapViewOfFile(bufMemp)) {
722 /* create buffer read event */
723 *bufEventHandlep = CreateEvent(NULL,
724 FALSE /* manual reset */,
725 FALSE /* initial state */,
727 if (*bufEventHandlep != NULL) {
734 (void) CloseHandle(*bufMemHandlep);
739 *bufMemHandlep = *bufEventHandlep = NULL;
747 * ReadChildDataBuffer() -- Read data buffer passed to child from parent,
748 * if any, and place in allocated storage.
751 ReadChildDataBuffer(void **datap, /* allocated data buffer */
752 size_t *dataLen) /* size of data buffer returned */
754 BOOL fsuccess = FALSE;
755 char bufMemName[sizeof(PMGT_DATA_MEM_PREFIX) + 20];
756 char bufEventName[sizeof(PMGT_DATA_EVENT_PREFIX) + 20];
757 HANDLE bufMemHandle, bufEventHandle;
759 sprintf(bufMemName, "%s%d",
760 PMGT_DATA_MEM_PREFIX, (int)GetCurrentProcessId());
761 sprintf(bufEventName, "%s%d",
762 PMGT_DATA_EVENT_PREFIX, (int)GetCurrentProcessId());
764 /* Attempt to open named event and named shared memory */
766 bufEventHandle = OpenEvent(EVENT_MODIFY_STATE, FALSE, bufEventName);
768 if (bufEventHandle != NULL) {
769 bufMemHandle = OpenFileMapping(FILE_MAP_READ, FALSE, bufMemName);
771 if (bufMemHandle != NULL) {
774 bufMemp = MapViewOfFile(bufMemHandle, FILE_MAP_READ, 0, 0, 0);
776 if (bufMemp != NULL) {
777 /* read data size and data from shared memory */
778 size_t *memp = (size_t *)bufMemp;
781 *datap = (void *)malloc(*dataLen);
783 if (*datap != NULL) {
784 memcpy(*datap, (void *)memp, *dataLen);
787 (void) UnmapViewOfFile(bufMemp);
790 (void) CloseHandle(bufMemHandle);
793 (void) SetEvent(bufEventHandle);
794 (void) CloseHandle(bufEventHandle);
807 * ChildMonitorThread() -- Thread spawned to monitor status of child process.
809 * Param must be index into child process table.
812 ChildMonitorThread(LPVOID param)
814 int tidx = (int)param;
815 HANDLE childProcHandle;
819 /* retrieve handle for child process from process table and duplicate */
821 (void) pthread_mutex_lock(&procTableLock);
824 DuplicateHandle(GetCurrentProcess(), /* source process handle */
825 procTable[tidx].p_handle, /* source handle to dup */
826 GetCurrentProcess(), /* target process handle */
827 &childProcHandle, /* target handle (duplicate) */
828 0, /* access (ignored here) */
829 FALSE, /* not inheritable */
830 DUPLICATE_SAME_ACCESS);
832 (void) pthread_mutex_unlock(&procTableLock);
835 /* wait for child process to terminate */
837 if (WaitForSingleObject(childProcHandle, INFINITE) == WAIT_OBJECT_0) {
838 /* child process terminated; mark in table and signal event */
839 (void) pthread_mutex_lock(&procTableLock);
841 procTable[tidx].p_terminated = TRUE;
842 (void) GetExitCodeProcess(childProcHandle,
843 &procTable[tidx].p_status);
846 (void) pthread_mutex_unlock(&procTableLock);
848 (void) pthread_cond_broadcast(&childTermEvent);
850 /* process/raise SIGCHLD; do last in case handler never returns */
851 ProcessSignal(SIGCHLD);
855 (void) CloseHandle(childProcHandle);
858 /* note: nothing can be done if DuplicateHandle() or WaitForSingleObject()
859 * fail; however, this should never happen.
867 * pmgt_ProcessSpawnVEB() -- Spawn a process (Unix fork()/execve() semantics)
869 * Returns pid of the child process ((pid_t)-1 on failure with errno set).
871 * Notes: A senvp value of NULL results in Unix fork()/execv() semantics.
872 * Open files are not inherited; child's stdin, stdout, and stderr
873 * are set to parent's console.
874 * If spath does not specify a filename extension ".exe" is used.
875 * If sdatap is not NULL, and sdatalen > 0, data is passed to child.
876 * The spath and sargv[] strings must not contain quote chars (").
878 * ASSUMPTIONS: sargv[0] is the same as spath (or its last component).
881 pmgt_ProcessSpawnVEB(const char *spath,
888 char *pathbuf, *argbuf, *envbuf;
889 char pathext[_MAX_EXT];
890 STARTUPINFO startInfo;
891 PROCESS_INFORMATION procInfo;
892 HANDLE monitorHandle = NULL;
893 HANDLE bufMemHandle, bufEventHandle;
894 DWORD monitorId, createFlags;
895 BOOL passingBuffer = (sdatap != NULL && sdatalen > 0);
898 /* verify arguments */
899 if (!spath || !sargv) {
902 } else if (*spath == '\0') {
907 /* create path with .exe extension if no filename extension supplied */
908 if (!(pathbuf = (char *)malloc(strlen(spath) + 5 /* .exe */))) {
912 strcpy(pathbuf, spath);
914 _splitpath(pathbuf, NULL, NULL, NULL, pathext);
915 if (*pathext == '\0') {
916 /* no filename extension supplied for spath; .exe is assumed */
917 strcat(pathbuf, ".exe");
920 /* create command line argument string */
921 argbuf = StringArrayToString(sargv);
929 /* create environment variable block (multistring) */
931 /* use environment variables provided */
932 envbuf = StringArrayToMultiString(senvp);
941 /* use default environment variables */
945 /* set process creation flags */
946 createFlags = CREATE_SUSPENDED | NORMAL_PRIORITY_CLASS;
948 if (getenv(PMGT_SPAWN_DETACHED_ENV_NAME) != NULL) {
949 createFlags |= DETACHED_PROCESS;
952 /* clear start-up info; use defaults */
953 memset((void *)&startInfo, 0, sizeof(startInfo));
954 startInfo.cb = sizeof(startInfo);
956 /* perform the following as a logically atomic unit:
957 * 1) allocate a process table entry
958 * 2) spawn child process (suspended)
959 * 3) create data buffer to pass (optional)
960 * 4) initialize process table entry
961 * 5) start child watcher thread
962 * 6) resume spawned child process
965 (void) pthread_mutex_lock(&procTableLock);
967 for (tidx = 0; tidx < PMGT_CHILD_MAX; tidx++) {
968 if (procTable[tidx].p_handle == NULL &&
969 procTable[tidx].p_reserved == FALSE) {
970 procTable[tidx].p_reserved = TRUE;
974 (void) pthread_mutex_unlock(&procTableLock);
976 if (tidx >= PMGT_CHILD_MAX) {
977 /* no space left in process table */
987 CreateProcess(pathbuf, /* executable path */
988 argbuf, /* command line argument string */
989 NULL, /* default process security attr */
990 NULL, /* default thread security attr */
991 FALSE, /* do NOT inherit handles */
992 createFlags, /* creation control flags */
993 envbuf, /* environment variable block */
994 NULL, /* current directory is that of parent */
995 &startInfo, /* startup info block */
1003 /* failed to spawn process */
1004 errno = nterr_nt2unix(GetLastError(), ENOENT);
1006 (void) pthread_mutex_lock(&procTableLock);
1007 procTable[tidx].p_reserved = FALSE; /* mark entry as not reserved */
1008 (void) pthread_mutex_unlock(&procTableLock);
1013 if (passingBuffer) {
1014 /* create named data buffer and read event for child */
1015 fsuccess = CreateChildDataBuffer(procInfo.dwProcessId,
1021 (void) pthread_mutex_lock(&procTableLock);
1022 procTable[tidx].p_reserved = FALSE; /* mark entry not reserved */
1023 (void) pthread_mutex_unlock(&procTableLock);
1025 (void) TerminateProcess(procInfo.hProcess,
1026 PMGT_SIGSTATUS_ENCODE(SIGKILL));
1027 (void) CloseHandle(procInfo.hThread);
1028 (void) CloseHandle(procInfo.hProcess);
1035 (void) pthread_mutex_lock(&procTableLock);
1037 procTable[tidx].p_handle = procInfo.hProcess;
1038 procTable[tidx].p_id = procInfo.dwProcessId;
1039 procTable[tidx].p_terminated = FALSE;
1043 /* Note: must hold procTableLock during monitor thread creation so
1044 * that if creation fails we can clean up process table before another
1045 * thread has a chance to see this procTable entry. Continue to hold
1046 * procTableLock while resuming child process, since the procTable
1047 * entry contains a copy of the child process handle which we might use.
1050 CreateThread(NULL, /* default security attr. */
1051 0, /* default stack size */
1053 (LPVOID)tidx, /* thread argument */
1054 0, /* creation flags */
1055 &monitorId); /* thread id */
1057 if (monitorHandle == NULL) {
1058 /* failed to start child monitor thread */
1059 procTable[tidx].p_handle = NULL; /* invalidate table entry */
1060 procTable[tidx].p_reserved = FALSE; /* mark entry as not reserved */
1063 (void) pthread_mutex_unlock(&procTableLock);
1065 (void) TerminateProcess(procInfo.hProcess,
1066 PMGT_SIGSTATUS_ENCODE(SIGKILL));
1067 (void) CloseHandle(procInfo.hThread);
1068 (void) CloseHandle(procInfo.hProcess);
1070 if (passingBuffer) {
1071 (void) CloseHandle(bufMemHandle);
1072 (void) CloseHandle(bufEventHandle);
1079 /* Resume child process, which was created suspended to implement spawn
1080 * atomically. If resumption fails, which it never should, terminate
1081 * the child process with a status of SIGKILL. Spawn still succeeds and
1082 * the net result is the same as if the child process received a spurious
1083 * SIGKILL signal; the child monitor thread will then handle this.
1085 if (ResumeThread(procInfo.hThread) == DWORD_OF_ONES) {
1086 (void) TerminateProcess(procInfo.hProcess,
1087 PMGT_SIGSTATUS_ENCODE(SIGKILL));
1089 if (passingBuffer) {
1090 /* child will never read data buffer */
1091 (void) SetEvent(bufEventHandle);
1095 (void) pthread_mutex_unlock(&procTableLock);
1097 (void) CloseHandle(procInfo.hThread);
1098 (void) CloseHandle(monitorHandle);
1100 /* After spawn returns, signals can not be sent to the new child process
1101 * until that child initializes its signal-receiving mechanism (assuming
1102 * the child is linked with this library). Shorten (but sadly don't
1103 * eliminate) this window of opportunity for failure by yielding this
1104 * thread's time slice.
1106 (void) SwitchToThread();
1108 /* If passing a data buffer to child, wait until child reads buffer
1109 * before closing handles and thus freeing resources; if don't wait
1110 * then parent can not safely exit immediately after returning from
1111 * this call (hence why wait is not done in a background thread).
1113 if (passingBuffer) {
1114 WaitForSingleObject(bufEventHandle, 10000);
1115 /* note: if wait times out, child may not get to read buffer */
1116 (void) CloseHandle(bufMemHandle);
1117 (void) CloseHandle(bufEventHandle);
1120 return (pid_t)procInfo.dwProcessId;
1126 * pmgt_ProcessWaitPid() -- Wait for child process status; i.e., wait
1127 * for child to terminate (Unix waitpid() semantics).
1129 * Note: does not support waiting for process in group (i.e., pid
1130 * equals (pid_t)0 or pid is less than (pid_t)-1.
1133 pmgt_ProcessWaitPid(pid_t pid,
1139 BOOL statusFound = FALSE;
1142 /* validate arguments */
1143 if (pid < (pid_t)-1 || pid == (pid_t)0) {
1148 /* determine how long caller is willing to wait for child */
1150 waitTime = (options & WNOHANG) ? 0 : INFINITE;
1152 /* get child status */
1154 (void) pthread_mutex_lock(&procTableLock);
1157 BOOL waitForChild = FALSE;
1159 if (procEntryCount == 0) {
1160 /* no child processes */
1164 /* determine if status is available for specified child id */
1166 if (pid == (pid_t)-1) {
1167 /* CASE 1: pid matches any child id */
1169 if (procTermCount == 0) {
1170 /* status not available for any child ... */
1171 if (waitTime == 0) {
1172 /* ... and caller is not willing to wait */
1175 /* ... but caller is willing to wait */
1176 waitForChild = TRUE;
1179 /* status available for some child; locate table entry */
1180 for (tidx = 0; tidx < PMGT_CHILD_MAX; tidx++) {
1181 if (procTable[tidx].p_handle != NULL &&
1182 procTable[tidx].p_terminated == TRUE) {
1189 /* should never happen; indicates a bug */
1190 errno = EINTR; /* plausible lie for failure */
1196 /* CASE 2: pid must match a specific child id */
1198 /* locate table entry */
1199 for (tidx = 0; tidx < PMGT_CHILD_MAX; tidx++) {
1200 if (procTable[tidx].p_handle != NULL &&
1201 procTable[tidx].p_id == (DWORD)pid) {
1206 if (tidx >= PMGT_CHILD_MAX) {
1207 /* pid does not match any child id */
1210 } else if (procTable[tidx].p_terminated == FALSE) {
1211 /* status not available for specified child ... */
1212 if (waitTime == 0) {
1213 /* ... and caller is not willing to wait */
1216 /* ... but caller is willing to wait */
1217 waitForChild = TRUE;
1220 /* status is available for specified child */
1227 (void) pthread_cond_wait(&childTermEvent, &procTableLock);
1234 /* child status available */
1236 *statusP = ComputeWaitStatus(procTable[tidx].p_status);
1238 rc = (pid_t)procTable[tidx].p_id;
1240 /* clean up process table */
1241 (void) CloseHandle(procTable[tidx].p_handle);
1242 procTable[tidx].p_handle = NULL;
1243 procTable[tidx].p_reserved = FALSE;
1249 (void) pthread_mutex_unlock(&procTableLock);
1258 /* ----------------- General ---------------- */
1263 * PmgtLibraryInitialize() -- Initialize process management library.
1266 PmgtLibraryInitialize(void)
1269 HANDLE sigPipeHandle;
1270 char sigPipeName[sizeof(PMGT_SIGNAL_PIPE_PREFIX) + 20];
1271 HANDLE sigListenerHandle;
1272 DWORD sigListenerId;
1274 /* initialize mutex locks and condition variables */
1276 if ((rc = pthread_mutex_init(&signalTableLock, NULL)) ||
1277 (rc = pthread_mutex_init(&signalBlockLock, NULL)) ||
1278 (rc = pthread_mutex_init(&procTableLock, NULL)) ||
1279 (rc = pthread_cond_init(&childTermEvent, NULL))) {
1284 /* initialize signal disposition table */
1286 for (i = 0; i < NSIG; i++) {
1287 if (SignalIsDefined(i)) {
1288 /* initialize to default action for defined signals */
1289 signalTable[i].action.sa_handler = SIG_DFL;
1290 sigemptyset(&signalTable[i].action.sa_mask);
1291 signalTable[i].action.sa_flags = 0;
1293 /* initialize to ignore for undefined signals */
1294 signalTable[i].action.sa_handler = SIG_IGN;
1298 /* initialize child process table */
1300 for (i = 0; i < PMGT_CHILD_MAX; i++) {
1301 procTable[i].p_handle = NULL;
1302 procTable[i].p_reserved = FALSE;
1307 /* retrieve data buffer passed from parent in spawn, if any */
1309 if (!ReadChildDataBuffer(&pmgt_spawnData, &pmgt_spawnDataLen)) {
1310 pmgt_spawnData = NULL;
1311 pmgt_spawnDataLen = 0;
1314 /* create named pipe for delivering signals to this process */
1316 sprintf(sigPipeName, "%s%d",
1317 PMGT_SIGNAL_PIPE_PREFIX, (int)GetCurrentProcessId());
1320 CreateNamedPipe(sigPipeName, /* pipe for this process */
1321 PIPE_ACCESS_DUPLEX | /* full duplex pipe */
1322 WRITE_DAC, /* DACL write access */
1323 PIPE_TYPE_MESSAGE | /* message type pipe */
1324 PIPE_READMODE_MESSAGE | /* message read-mode */
1325 PIPE_WAIT, /* blocking mode */
1326 1, /* max of 1 pipe instance */
1327 64, /* output buffer size (advisory) */
1328 64, /* input buffer size (advisory) */
1329 1000, /* 1 sec default client timeout */
1330 NULL); /* default security attr. */
1332 if (sigPipeHandle == INVALID_HANDLE_VALUE) {
1333 /* failed to create signal pipe */
1334 errno = nterr_nt2unix(GetLastError(), EIO);
1338 /* add entry to signal pipe ACL granting local Administrators R/W access */
1340 (void) ObjectDaclEntryAdd(sigPipeHandle,
1342 LocalAdministratorsGroup,
1343 GENERIC_READ | GENERIC_WRITE,
1347 /* start signal pipe listener thread */
1350 CreateThread(NULL, /* default security attr. */
1351 0, /* default stack size */
1352 RemoteSignalListenerThread,
1353 (LPVOID)sigPipeHandle, /* thread argument */
1354 0, /* creation flags */
1355 &sigListenerId); /* thread id */
1357 if (sigListenerHandle != NULL) {
1358 /* listener thread started; bump priority */
1359 (void) SetThreadPriority(sigListenerHandle, THREAD_PRIORITY_HIGHEST);
1360 (void) CloseHandle(sigListenerHandle);
1362 /* failed to start listener thread */
1364 (void) CloseHandle(sigPipeHandle);
1368 /* redirect native NT signals into this process management library */
1370 if (pmgt_RedirectNativeSignals()) {
1371 /* errno set by called function */
1380 * DllMain() -- Entry-point function called by the DllMainCRTStartup()
1381 * function in the MSVC runtime DLL (msvcrt.dll).
1383 * Note: the system serializes calls to this function.
1386 DllMain(HINSTANCE dllInstHandle, /* instance handle for this DLL module */
1387 DWORD reason, /* reason function is being called */
1388 LPVOID reserved) /* reserved for future use */
1390 if (reason == DLL_PROCESS_ATTACH) {
1391 /* library is being attached to a process */
1392 if (PmgtLibraryInitialize()) {
1393 /* failed to initialize library */
1397 /* disable thread attach/detach notifications */
1398 (void) DisableThreadLibraryCalls(dllInstHandle);