michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #if defined(AIX) michael@0: #include /* For dlopen, dlsym, dlclose */ michael@0: #endif michael@0: michael@0: #if defined(DARWIN) michael@0: #if defined(HAVE_CRT_EXTERNS_H) michael@0: #include michael@0: #endif michael@0: #else michael@0: PR_IMPORT_DATA(char **) environ; michael@0: #endif michael@0: michael@0: /* michael@0: * HP-UX 9 doesn't have the SA_RESTART flag. michael@0: */ michael@0: #ifndef SA_RESTART michael@0: #define SA_RESTART 0 michael@0: #endif michael@0: michael@0: /* michael@0: ********************************************************************** michael@0: * michael@0: * The Unix process routines michael@0: * michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #define _PR_SIGNALED_EXITSTATUS 256 michael@0: michael@0: typedef enum pr_PidState { michael@0: _PR_PID_DETACHED, michael@0: _PR_PID_REAPED, michael@0: _PR_PID_WAITING michael@0: } pr_PidState; michael@0: michael@0: typedef struct pr_PidRecord { michael@0: pid_t pid; michael@0: int exitStatus; michael@0: pr_PidState state; michael@0: PRCondVar *reapedCV; michael@0: struct pr_PidRecord *next; michael@0: } pr_PidRecord; michael@0: michael@0: /* michael@0: * Irix sprocs and LinuxThreads are actually a kind of processes michael@0: * that can share the virtual address space and file descriptors. michael@0: */ michael@0: #if (defined(IRIX) && !defined(_PR_PTHREADS)) \ michael@0: || ((defined(LINUX) || defined(__GNU__) || defined(__GLIBC__)) \ michael@0: && defined(_PR_PTHREADS)) michael@0: #define _PR_SHARE_CLONES michael@0: #endif michael@0: michael@0: /* michael@0: * The macro _PR_NATIVE_THREADS indicates that we are michael@0: * using native threads only, so waitpid() blocks just the michael@0: * calling thread, not the process. In this case, the waitpid michael@0: * daemon thread can safely block in waitpid(). So we don't michael@0: * need to catch SIGCHLD, and the pipe to unblock PR_Poll() is michael@0: * also not necessary. michael@0: */ michael@0: michael@0: #if defined(_PR_GLOBAL_THREADS_ONLY) \ michael@0: || (defined(_PR_PTHREADS) \ michael@0: && !defined(LINUX) && !defined(__GNU__) && !defined(__GLIBC__)) michael@0: #define _PR_NATIVE_THREADS michael@0: #endif michael@0: michael@0: /* michael@0: * All the static variables used by the Unix process routines are michael@0: * collected in this structure. michael@0: */ michael@0: michael@0: static struct { michael@0: PRCallOnceType once; michael@0: PRThread *thread; michael@0: PRLock *ml; michael@0: #if defined(_PR_NATIVE_THREADS) michael@0: PRInt32 numProcs; michael@0: PRCondVar *cv; michael@0: #else michael@0: int pipefd[2]; michael@0: #endif michael@0: pr_PidRecord **pidTable; michael@0: michael@0: #ifdef _PR_SHARE_CLONES michael@0: struct pr_CreateProcOp *opHead, *opTail; michael@0: #endif michael@0: michael@0: #ifdef AIX michael@0: pid_t (*forkptr)(void); /* Newer versions of AIX (starting in 4.3.2) michael@0: * have f_fork, which is faster than the michael@0: * regular fork in a multithreaded process michael@0: * because it skips calling the fork handlers. michael@0: * So we look up the f_fork symbol to see if michael@0: * it's available and fall back on fork. michael@0: */ michael@0: #endif /* AIX */ michael@0: } pr_wp; michael@0: michael@0: #ifdef _PR_SHARE_CLONES michael@0: static int pr_waitpid_daemon_exit; michael@0: michael@0: void michael@0: _MD_unix_terminate_waitpid_daemon(void) michael@0: { michael@0: if (pr_wp.thread) { michael@0: pr_waitpid_daemon_exit = 1; michael@0: write(pr_wp.pipefd[1], "", 1); michael@0: PR_JoinThread(pr_wp.thread); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static PRStatus _MD_InitProcesses(void); michael@0: #if !defined(_PR_NATIVE_THREADS) michael@0: static void pr_InstallSigchldHandler(void); michael@0: #endif michael@0: michael@0: static PRProcess * michael@0: ForkAndExec( michael@0: const char *path, michael@0: char *const *argv, michael@0: char *const *envp, michael@0: const PRProcessAttr *attr) michael@0: { michael@0: PRProcess *process; michael@0: int nEnv, idx; michael@0: char *const *childEnvp; michael@0: char **newEnvp = NULL; michael@0: int flags; michael@0: michael@0: process = PR_NEW(PRProcess); michael@0: if (!process) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: michael@0: childEnvp = envp; michael@0: if (attr && attr->fdInheritBuffer) { michael@0: PRBool found = PR_FALSE; michael@0: michael@0: if (NULL == childEnvp) { michael@0: #ifdef DARWIN michael@0: #ifdef HAVE_CRT_EXTERNS_H michael@0: childEnvp = *(_NSGetEnviron()); michael@0: #else michael@0: /* _NSGetEnviron() is not available on iOS. */ michael@0: PR_DELETE(process); michael@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); michael@0: return NULL; michael@0: #endif michael@0: #else michael@0: childEnvp = environ; michael@0: #endif michael@0: } michael@0: michael@0: for (nEnv = 0; childEnvp[nEnv]; nEnv++) { michael@0: } michael@0: newEnvp = (char **) PR_MALLOC((nEnv + 2) * sizeof(char *)); michael@0: if (NULL == newEnvp) { michael@0: PR_DELETE(process); michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: for (idx = 0; idx < nEnv; idx++) { michael@0: newEnvp[idx] = childEnvp[idx]; michael@0: if (!found && !strncmp(newEnvp[idx], "NSPR_INHERIT_FDS=", 17)) { michael@0: newEnvp[idx] = attr->fdInheritBuffer; michael@0: found = PR_TRUE; michael@0: } michael@0: } michael@0: if (!found) { michael@0: newEnvp[idx++] = attr->fdInheritBuffer; michael@0: } michael@0: newEnvp[idx] = NULL; michael@0: childEnvp = newEnvp; michael@0: } michael@0: michael@0: #ifdef AIX michael@0: process->md.pid = (*pr_wp.forkptr)(); michael@0: #elif defined(NTO) || defined(SYMBIAN) michael@0: /* michael@0: * fork() & exec() does not work in a multithreaded process. michael@0: * Use spawn() instead. michael@0: */ michael@0: { michael@0: int fd_map[3] = { 0, 1, 2 }; michael@0: michael@0: if (attr) { michael@0: if (attr->stdinFd && attr->stdinFd->secret->md.osfd != 0) { michael@0: fd_map[0] = dup(attr->stdinFd->secret->md.osfd); michael@0: flags = fcntl(fd_map[0], F_GETFL, 0); michael@0: if (flags & O_NONBLOCK) michael@0: fcntl(fd_map[0], F_SETFL, flags & ~O_NONBLOCK); michael@0: } michael@0: if (attr->stdoutFd && attr->stdoutFd->secret->md.osfd != 1) { michael@0: fd_map[1] = dup(attr->stdoutFd->secret->md.osfd); michael@0: flags = fcntl(fd_map[1], F_GETFL, 0); michael@0: if (flags & O_NONBLOCK) michael@0: fcntl(fd_map[1], F_SETFL, flags & ~O_NONBLOCK); michael@0: } michael@0: if (attr->stderrFd && attr->stderrFd->secret->md.osfd != 2) { michael@0: fd_map[2] = dup(attr->stderrFd->secret->md.osfd); michael@0: flags = fcntl(fd_map[2], F_GETFL, 0); michael@0: if (flags & O_NONBLOCK) michael@0: fcntl(fd_map[2], F_SETFL, flags & ~O_NONBLOCK); michael@0: } michael@0: michael@0: PR_ASSERT(attr->currentDirectory == NULL); /* not implemented */ michael@0: } michael@0: michael@0: #ifdef SYMBIAN michael@0: /* In Symbian OS, we use posix_spawn instead of fork() and exec() */ michael@0: posix_spawn(&(process->md.pid), path, NULL, NULL, argv, childEnvp); michael@0: #else michael@0: process->md.pid = spawn(path, 3, fd_map, NULL, argv, childEnvp); michael@0: #endif michael@0: michael@0: if (fd_map[0] != 0) michael@0: close(fd_map[0]); michael@0: if (fd_map[1] != 1) michael@0: close(fd_map[1]); michael@0: if (fd_map[2] != 2) michael@0: close(fd_map[2]); michael@0: } michael@0: #else michael@0: process->md.pid = fork(); michael@0: #endif michael@0: if ((pid_t) -1 == process->md.pid) { michael@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, errno); michael@0: PR_DELETE(process); michael@0: if (newEnvp) { michael@0: PR_DELETE(newEnvp); michael@0: } michael@0: return NULL; michael@0: } else if (0 == process->md.pid) { /* the child process */ michael@0: /* michael@0: * If the child process needs to exit, it must call _exit(). michael@0: * Do not call exit(), because exit() will flush and close michael@0: * the standard I/O file descriptors, and hence corrupt michael@0: * the parent process's standard I/O data structures. michael@0: */ michael@0: michael@0: #if !defined(NTO) && !defined(SYMBIAN) michael@0: if (attr) { michael@0: /* the osfd's to redirect stdin, stdout, and stderr to */ michael@0: int in_osfd = -1, out_osfd = -1, err_osfd = -1; michael@0: michael@0: if (attr->stdinFd michael@0: && attr->stdinFd->secret->md.osfd != 0) { michael@0: in_osfd = attr->stdinFd->secret->md.osfd; michael@0: if (dup2(in_osfd, 0) != 0) { michael@0: _exit(1); /* failed */ michael@0: } michael@0: flags = fcntl(0, F_GETFL, 0); michael@0: if (flags & O_NONBLOCK) { michael@0: fcntl(0, F_SETFL, flags & ~O_NONBLOCK); michael@0: } michael@0: } michael@0: if (attr->stdoutFd michael@0: && attr->stdoutFd->secret->md.osfd != 1) { michael@0: out_osfd = attr->stdoutFd->secret->md.osfd; michael@0: if (dup2(out_osfd, 1) != 1) { michael@0: _exit(1); /* failed */ michael@0: } michael@0: flags = fcntl(1, F_GETFL, 0); michael@0: if (flags & O_NONBLOCK) { michael@0: fcntl(1, F_SETFL, flags & ~O_NONBLOCK); michael@0: } michael@0: } michael@0: if (attr->stderrFd michael@0: && attr->stderrFd->secret->md.osfd != 2) { michael@0: err_osfd = attr->stderrFd->secret->md.osfd; michael@0: if (dup2(err_osfd, 2) != 2) { michael@0: _exit(1); /* failed */ michael@0: } michael@0: flags = fcntl(2, F_GETFL, 0); michael@0: if (flags & O_NONBLOCK) { michael@0: fcntl(2, F_SETFL, flags & ~O_NONBLOCK); michael@0: } michael@0: } michael@0: if (in_osfd != -1) { michael@0: close(in_osfd); michael@0: } michael@0: if (out_osfd != -1 && out_osfd != in_osfd) { michael@0: close(out_osfd); michael@0: } michael@0: if (err_osfd != -1 && err_osfd != in_osfd michael@0: && err_osfd != out_osfd) { michael@0: close(err_osfd); michael@0: } michael@0: if (attr->currentDirectory) { michael@0: if (chdir(attr->currentDirectory) < 0) { michael@0: _exit(1); /* failed */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (childEnvp) { michael@0: (void)execve(path, argv, childEnvp); michael@0: } else { michael@0: /* Inherit the environment of the parent. */ michael@0: (void)execv(path, argv); michael@0: } michael@0: /* Whoops! It returned. That's a bad sign. */ michael@0: _exit(1); michael@0: #endif /* !NTO */ michael@0: } michael@0: michael@0: if (newEnvp) { michael@0: PR_DELETE(newEnvp); michael@0: } michael@0: michael@0: #if defined(_PR_NATIVE_THREADS) michael@0: PR_Lock(pr_wp.ml); michael@0: if (0 == pr_wp.numProcs++) { michael@0: PR_NotifyCondVar(pr_wp.cv); michael@0: } michael@0: PR_Unlock(pr_wp.ml); michael@0: #endif michael@0: return process; michael@0: } michael@0: michael@0: #ifdef _PR_SHARE_CLONES michael@0: michael@0: struct pr_CreateProcOp { michael@0: const char *path; michael@0: char *const *argv; michael@0: char *const *envp; michael@0: const PRProcessAttr *attr; michael@0: PRProcess *process; michael@0: PRErrorCode prerror; michael@0: PRInt32 oserror; michael@0: PRBool done; michael@0: PRCondVar *doneCV; michael@0: struct pr_CreateProcOp *next; michael@0: }; michael@0: michael@0: PRProcess * michael@0: _MD_CreateUnixProcess( michael@0: const char *path, michael@0: char *const *argv, michael@0: char *const *envp, michael@0: const PRProcessAttr *attr) michael@0: { michael@0: struct pr_CreateProcOp *op; michael@0: PRProcess *proc; michael@0: int rv; michael@0: michael@0: if (PR_CallOnce(&pr_wp.once, _MD_InitProcesses) == PR_FAILURE) { michael@0: return NULL; michael@0: } michael@0: michael@0: op = PR_NEW(struct pr_CreateProcOp); michael@0: if (NULL == op) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: op->path = path; michael@0: op->argv = argv; michael@0: op->envp = envp; michael@0: op->attr = attr; michael@0: op->done = PR_FALSE; michael@0: op->doneCV = PR_NewCondVar(pr_wp.ml); michael@0: if (NULL == op->doneCV) { michael@0: PR_DELETE(op); michael@0: return NULL; michael@0: } michael@0: PR_Lock(pr_wp.ml); michael@0: michael@0: /* add to the tail of op queue */ michael@0: op->next = NULL; michael@0: if (pr_wp.opTail) { michael@0: pr_wp.opTail->next = op; michael@0: pr_wp.opTail = op; michael@0: } else { michael@0: PR_ASSERT(NULL == pr_wp.opHead); michael@0: pr_wp.opHead = pr_wp.opTail = op; michael@0: } michael@0: michael@0: /* wake up the daemon thread */ michael@0: do { michael@0: rv = write(pr_wp.pipefd[1], "", 1); michael@0: } while (-1 == rv && EINTR == errno); michael@0: michael@0: while (op->done == PR_FALSE) { michael@0: PR_WaitCondVar(op->doneCV, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: PR_Unlock(pr_wp.ml); michael@0: PR_DestroyCondVar(op->doneCV); michael@0: proc = op->process; michael@0: if (!proc) { michael@0: PR_SetError(op->prerror, op->oserror); michael@0: } michael@0: PR_DELETE(op); michael@0: return proc; michael@0: } michael@0: michael@0: #else /* ! _PR_SHARE_CLONES */ michael@0: michael@0: PRProcess * michael@0: _MD_CreateUnixProcess( michael@0: const char *path, michael@0: char *const *argv, michael@0: char *const *envp, michael@0: const PRProcessAttr *attr) michael@0: { michael@0: if (PR_CallOnce(&pr_wp.once, _MD_InitProcesses) == PR_FAILURE) { michael@0: return NULL; michael@0: } michael@0: return ForkAndExec(path, argv, envp, attr); michael@0: } /* _MD_CreateUnixProcess */ michael@0: michael@0: #endif /* _PR_SHARE_CLONES */ michael@0: michael@0: /* michael@0: * The pid table is a hashtable. michael@0: * michael@0: * The number of buckets in the hashtable (NBUCKETS) must be a power of 2. michael@0: */ michael@0: #define NBUCKETS_LOG2 6 michael@0: #define NBUCKETS (1 << NBUCKETS_LOG2) michael@0: #define PID_HASH_MASK ((pid_t) (NBUCKETS - 1)) michael@0: michael@0: static pr_PidRecord * michael@0: FindPidTable(pid_t pid) michael@0: { michael@0: pr_PidRecord *pRec; michael@0: int keyHash = (int) (pid & PID_HASH_MASK); michael@0: michael@0: pRec = pr_wp.pidTable[keyHash]; michael@0: while (pRec) { michael@0: if (pRec->pid == pid) { michael@0: break; michael@0: } michael@0: pRec = pRec->next; michael@0: } michael@0: return pRec; michael@0: } michael@0: michael@0: static void michael@0: InsertPidTable(pr_PidRecord *pRec) michael@0: { michael@0: int keyHash = (int) (pRec->pid & PID_HASH_MASK); michael@0: michael@0: pRec->next = pr_wp.pidTable[keyHash]; michael@0: pr_wp.pidTable[keyHash] = pRec; michael@0: } michael@0: michael@0: static void michael@0: DeletePidTable(pr_PidRecord *pRec) michael@0: { michael@0: int keyHash = (int) (pRec->pid & PID_HASH_MASK); michael@0: michael@0: if (pr_wp.pidTable[keyHash] == pRec) { michael@0: pr_wp.pidTable[keyHash] = pRec->next; michael@0: } else { michael@0: pr_PidRecord *pred, *cur; /* predecessor and current */ michael@0: michael@0: pred = pr_wp.pidTable[keyHash]; michael@0: cur = pred->next; michael@0: while (cur) { michael@0: if (cur == pRec) { michael@0: pred->next = cur->next; michael@0: break; michael@0: } michael@0: pred = cur; michael@0: cur = cur->next; michael@0: } michael@0: PR_ASSERT(cur != NULL); michael@0: } michael@0: } michael@0: michael@0: static int michael@0: ExtractExitStatus(int rawExitStatus) michael@0: { michael@0: /* michael@0: * We did not specify the WCONTINUED and WUNTRACED options michael@0: * for waitpid, so these two events should not be reported. michael@0: */ michael@0: PR_ASSERT(!WIFSTOPPED(rawExitStatus)); michael@0: #ifdef WIFCONTINUED michael@0: PR_ASSERT(!WIFCONTINUED(rawExitStatus)); michael@0: #endif michael@0: if (WIFEXITED(rawExitStatus)) { michael@0: return WEXITSTATUS(rawExitStatus); michael@0: } else { michael@0: PR_ASSERT(WIFSIGNALED(rawExitStatus)); michael@0: return _PR_SIGNALED_EXITSTATUS; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: ProcessReapedChildInternal(pid_t pid, int status) michael@0: { michael@0: pr_PidRecord *pRec; michael@0: michael@0: pRec = FindPidTable(pid); michael@0: if (NULL == pRec) { michael@0: pRec = PR_NEW(pr_PidRecord); michael@0: pRec->pid = pid; michael@0: pRec->state = _PR_PID_REAPED; michael@0: pRec->exitStatus = ExtractExitStatus(status); michael@0: pRec->reapedCV = NULL; michael@0: InsertPidTable(pRec); michael@0: } else { michael@0: PR_ASSERT(pRec->state != _PR_PID_REAPED); michael@0: if (_PR_PID_DETACHED == pRec->state) { michael@0: PR_ASSERT(NULL == pRec->reapedCV); michael@0: DeletePidTable(pRec); michael@0: PR_DELETE(pRec); michael@0: } else { michael@0: PR_ASSERT(_PR_PID_WAITING == pRec->state); michael@0: PR_ASSERT(NULL != pRec->reapedCV); michael@0: pRec->exitStatus = ExtractExitStatus(status); michael@0: pRec->state = _PR_PID_REAPED; michael@0: PR_NotifyCondVar(pRec->reapedCV); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #if defined(_PR_NATIVE_THREADS) michael@0: michael@0: /* michael@0: * If all the threads are native threads, the daemon thread is michael@0: * simpler. We don't need to catch the SIGCHLD signal. We can michael@0: * just have the daemon thread block in waitpid(). michael@0: */ michael@0: michael@0: static void WaitPidDaemonThread(void *unused) michael@0: { michael@0: pid_t pid; michael@0: int status; michael@0: michael@0: while (1) { michael@0: PR_Lock(pr_wp.ml); michael@0: while (0 == pr_wp.numProcs) { michael@0: PR_WaitCondVar(pr_wp.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: PR_Unlock(pr_wp.ml); michael@0: michael@0: while (1) { michael@0: do { michael@0: pid = waitpid((pid_t) -1, &status, 0); michael@0: } while ((pid_t) -1 == pid && EINTR == errno); michael@0: michael@0: /* michael@0: * waitpid() cannot return 0 because we did not invoke it michael@0: * with the WNOHANG option. michael@0: */ michael@0: PR_ASSERT(0 != pid); michael@0: michael@0: /* michael@0: * The only possible error code is ECHILD. But if we do michael@0: * our accounting correctly, we should only call waitpid() michael@0: * when there is a child process to wait for. michael@0: */ michael@0: PR_ASSERT((pid_t) -1 != pid); michael@0: if ((pid_t) -1 == pid) { michael@0: break; michael@0: } michael@0: michael@0: PR_Lock(pr_wp.ml); michael@0: ProcessReapedChildInternal(pid, status); michael@0: pr_wp.numProcs--; michael@0: while (0 == pr_wp.numProcs) { michael@0: PR_WaitCondVar(pr_wp.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: PR_Unlock(pr_wp.ml); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #else /* _PR_NATIVE_THREADS */ michael@0: michael@0: static void WaitPidDaemonThread(void *unused) michael@0: { michael@0: PRPollDesc pd; michael@0: PRFileDesc *fd; michael@0: int rv; michael@0: char buf[128]; michael@0: pid_t pid; michael@0: int status; michael@0: #ifdef _PR_SHARE_CLONES michael@0: struct pr_CreateProcOp *op; michael@0: #endif michael@0: michael@0: #ifdef _PR_SHARE_CLONES michael@0: pr_InstallSigchldHandler(); michael@0: #endif michael@0: michael@0: fd = PR_ImportFile(pr_wp.pipefd[0]); michael@0: PR_ASSERT(NULL != fd); michael@0: pd.fd = fd; michael@0: pd.in_flags = PR_POLL_READ; michael@0: michael@0: while (1) { michael@0: rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_ASSERT(1 == rv); michael@0: michael@0: #ifdef _PR_SHARE_CLONES michael@0: if (pr_waitpid_daemon_exit) { michael@0: return; michael@0: } michael@0: PR_Lock(pr_wp.ml); michael@0: #endif michael@0: michael@0: do { michael@0: rv = read(pr_wp.pipefd[0], buf, sizeof(buf)); michael@0: } while (sizeof(buf) == rv || (-1 == rv && EINTR == errno)); michael@0: michael@0: #ifdef _PR_SHARE_CLONES michael@0: PR_Unlock(pr_wp.ml); michael@0: while ((op = pr_wp.opHead) != NULL) { michael@0: op->process = ForkAndExec(op->path, op->argv, michael@0: op->envp, op->attr); michael@0: if (NULL == op->process) { michael@0: op->prerror = PR_GetError(); michael@0: op->oserror = PR_GetOSError(); michael@0: } michael@0: PR_Lock(pr_wp.ml); michael@0: pr_wp.opHead = op->next; michael@0: if (NULL == pr_wp.opHead) { michael@0: pr_wp.opTail = NULL; michael@0: } michael@0: op->done = PR_TRUE; michael@0: PR_NotifyCondVar(op->doneCV); michael@0: PR_Unlock(pr_wp.ml); michael@0: } michael@0: #endif michael@0: michael@0: while (1) { michael@0: do { michael@0: pid = waitpid((pid_t) -1, &status, WNOHANG); michael@0: } while ((pid_t) -1 == pid && EINTR == errno); michael@0: if (0 == pid) break; michael@0: if ((pid_t) -1 == pid) { michael@0: /* must be because we have no child processes */ michael@0: PR_ASSERT(ECHILD == errno); michael@0: break; michael@0: } michael@0: michael@0: PR_Lock(pr_wp.ml); michael@0: ProcessReapedChildInternal(pid, status); michael@0: PR_Unlock(pr_wp.ml); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void pr_SigchldHandler(int sig) michael@0: { michael@0: int errnoCopy; michael@0: int rv; michael@0: michael@0: errnoCopy = errno; michael@0: michael@0: do { michael@0: rv = write(pr_wp.pipefd[1], "", 1); michael@0: } while (-1 == rv && EINTR == errno); michael@0: michael@0: #ifdef DEBUG michael@0: if (-1 == rv && EAGAIN != errno && EWOULDBLOCK != errno) { michael@0: char *msg = "cannot write to pipe\n"; michael@0: write(2, msg, strlen(msg) + 1); michael@0: _exit(1); michael@0: } michael@0: #endif michael@0: michael@0: errno = errnoCopy; michael@0: } michael@0: michael@0: static void pr_InstallSigchldHandler() michael@0: { michael@0: #if defined(HPUX) && defined(_PR_DCETHREADS) michael@0: #error "HP-UX DCE threads have their own SIGCHLD handler" michael@0: #endif michael@0: michael@0: struct sigaction act, oact; michael@0: int rv; michael@0: michael@0: act.sa_handler = pr_SigchldHandler; michael@0: sigemptyset(&act.sa_mask); michael@0: act.sa_flags = SA_NOCLDSTOP | SA_RESTART; michael@0: rv = sigaction(SIGCHLD, &act, &oact); michael@0: PR_ASSERT(0 == rv); michael@0: /* Make sure we are not overriding someone else's SIGCHLD handler */ michael@0: #ifndef _PR_SHARE_CLONES michael@0: PR_ASSERT(oact.sa_handler == SIG_DFL); michael@0: #endif michael@0: } michael@0: michael@0: #endif /* !defined(_PR_NATIVE_THREADS) */ michael@0: michael@0: static PRStatus _MD_InitProcesses(void) michael@0: { michael@0: #if !defined(_PR_NATIVE_THREADS) michael@0: int rv; michael@0: int flags; michael@0: #endif michael@0: michael@0: #ifdef AIX michael@0: { michael@0: void *handle = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL); michael@0: pr_wp.forkptr = (pid_t (*)(void)) dlsym(handle, "f_fork"); michael@0: if (!pr_wp.forkptr) { michael@0: pr_wp.forkptr = fork; michael@0: } michael@0: dlclose(handle); michael@0: } michael@0: #endif /* AIX */ michael@0: michael@0: pr_wp.ml = PR_NewLock(); michael@0: PR_ASSERT(NULL != pr_wp.ml); michael@0: michael@0: #if defined(_PR_NATIVE_THREADS) michael@0: pr_wp.numProcs = 0; michael@0: pr_wp.cv = PR_NewCondVar(pr_wp.ml); michael@0: PR_ASSERT(NULL != pr_wp.cv); michael@0: #else michael@0: rv = pipe(pr_wp.pipefd); michael@0: PR_ASSERT(0 == rv); michael@0: flags = fcntl(pr_wp.pipefd[0], F_GETFL, 0); michael@0: fcntl(pr_wp.pipefd[0], F_SETFL, flags | O_NONBLOCK); michael@0: flags = fcntl(pr_wp.pipefd[1], F_GETFL, 0); michael@0: fcntl(pr_wp.pipefd[1], F_SETFL, flags | O_NONBLOCK); michael@0: michael@0: #ifndef _PR_SHARE_CLONES michael@0: pr_InstallSigchldHandler(); michael@0: #endif michael@0: #endif /* !_PR_NATIVE_THREADS */ michael@0: michael@0: pr_wp.thread = PR_CreateThread(PR_SYSTEM_THREAD, michael@0: WaitPidDaemonThread, NULL, PR_PRIORITY_NORMAL, michael@0: #ifdef _PR_SHARE_CLONES michael@0: PR_GLOBAL_THREAD, michael@0: #else michael@0: PR_LOCAL_THREAD, michael@0: #endif michael@0: PR_JOINABLE_THREAD, 0); michael@0: PR_ASSERT(NULL != pr_wp.thread); michael@0: michael@0: pr_wp.pidTable = (pr_PidRecord**)PR_CALLOC(NBUCKETS * sizeof(pr_PidRecord *)); michael@0: PR_ASSERT(NULL != pr_wp.pidTable); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PRStatus _MD_DetachUnixProcess(PRProcess *process) michael@0: { michael@0: PRStatus retVal = PR_SUCCESS; michael@0: pr_PidRecord *pRec; michael@0: michael@0: PR_Lock(pr_wp.ml); michael@0: pRec = FindPidTable(process->md.pid); michael@0: if (NULL == pRec) { michael@0: pRec = PR_NEW(pr_PidRecord); michael@0: if (NULL == pRec) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: retVal = PR_FAILURE; michael@0: goto done; michael@0: } michael@0: pRec->pid = process->md.pid; michael@0: pRec->state = _PR_PID_DETACHED; michael@0: pRec->reapedCV = NULL; michael@0: InsertPidTable(pRec); michael@0: } else { michael@0: PR_ASSERT(_PR_PID_REAPED == pRec->state); michael@0: if (_PR_PID_REAPED != pRec->state) { michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: retVal = PR_FAILURE; michael@0: } else { michael@0: DeletePidTable(pRec); michael@0: PR_ASSERT(NULL == pRec->reapedCV); michael@0: PR_DELETE(pRec); michael@0: } michael@0: } michael@0: PR_DELETE(process); michael@0: michael@0: done: michael@0: PR_Unlock(pr_wp.ml); michael@0: return retVal; michael@0: } michael@0: michael@0: PRStatus _MD_WaitUnixProcess( michael@0: PRProcess *process, michael@0: PRInt32 *exitCode) michael@0: { michael@0: pr_PidRecord *pRec; michael@0: PRStatus retVal = PR_SUCCESS; michael@0: PRBool interrupted = PR_FALSE; michael@0: michael@0: PR_Lock(pr_wp.ml); michael@0: pRec = FindPidTable(process->md.pid); michael@0: if (NULL == pRec) { michael@0: pRec = PR_NEW(pr_PidRecord); michael@0: if (NULL == pRec) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: retVal = PR_FAILURE; michael@0: goto done; michael@0: } michael@0: pRec->pid = process->md.pid; michael@0: pRec->state = _PR_PID_WAITING; michael@0: pRec->reapedCV = PR_NewCondVar(pr_wp.ml); michael@0: if (NULL == pRec->reapedCV) { michael@0: PR_DELETE(pRec); michael@0: retVal = PR_FAILURE; michael@0: goto done; michael@0: } michael@0: InsertPidTable(pRec); michael@0: while (!interrupted && _PR_PID_REAPED != pRec->state) { michael@0: if (PR_WaitCondVar(pRec->reapedCV, michael@0: PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE michael@0: && PR_GetError() == PR_PENDING_INTERRUPT_ERROR) { michael@0: interrupted = PR_TRUE; michael@0: } michael@0: } michael@0: if (_PR_PID_REAPED == pRec->state) { michael@0: if (exitCode) { michael@0: *exitCode = pRec->exitStatus; michael@0: } michael@0: } else { michael@0: PR_ASSERT(interrupted); michael@0: retVal = PR_FAILURE; michael@0: } michael@0: DeletePidTable(pRec); michael@0: PR_DestroyCondVar(pRec->reapedCV); michael@0: PR_DELETE(pRec); michael@0: } else { michael@0: PR_ASSERT(_PR_PID_REAPED == pRec->state); michael@0: PR_ASSERT(NULL == pRec->reapedCV); michael@0: DeletePidTable(pRec); michael@0: if (exitCode) { michael@0: *exitCode = pRec->exitStatus; michael@0: } michael@0: PR_DELETE(pRec); michael@0: } michael@0: PR_DELETE(process); michael@0: michael@0: done: michael@0: PR_Unlock(pr_wp.ml); michael@0: return retVal; michael@0: } /* _MD_WaitUnixProcess */ michael@0: michael@0: PRStatus _MD_KillUnixProcess(PRProcess *process) michael@0: { michael@0: PRErrorCode prerror; michael@0: PRInt32 oserror; michael@0: michael@0: #ifdef SYMBIAN michael@0: /* In Symbian OS, we can not kill other process with Open C */ michael@0: PR_SetError(PR_OPERATION_NOT_SUPPORTED_ERROR, oserror); michael@0: return PR_FAILURE; michael@0: #else michael@0: if (kill(process->md.pid, SIGKILL) == 0) { michael@0: return PR_SUCCESS; michael@0: } michael@0: oserror = errno; michael@0: switch (oserror) { michael@0: case EPERM: michael@0: prerror = PR_NO_ACCESS_RIGHTS_ERROR; michael@0: break; michael@0: case ESRCH: michael@0: prerror = PR_INVALID_ARGUMENT_ERROR; michael@0: break; michael@0: default: michael@0: prerror = PR_UNKNOWN_ERROR; michael@0: break; michael@0: } michael@0: PR_SetError(prerror, oserror); michael@0: return PR_FAILURE; michael@0: #endif michael@0: } /* _MD_KillUnixProcess */