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