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: #include michael@0: #include michael@0: michael@0: PRLogModuleInfo *_pr_clock_lm; michael@0: PRLogModuleInfo *_pr_cmon_lm; michael@0: PRLogModuleInfo *_pr_io_lm; michael@0: PRLogModuleInfo *_pr_cvar_lm; michael@0: PRLogModuleInfo *_pr_mon_lm; michael@0: PRLogModuleInfo *_pr_linker_lm; michael@0: PRLogModuleInfo *_pr_sched_lm; michael@0: PRLogModuleInfo *_pr_thread_lm; michael@0: PRLogModuleInfo *_pr_gc_lm; michael@0: PRLogModuleInfo *_pr_shm_lm; michael@0: PRLogModuleInfo *_pr_shma_lm; michael@0: michael@0: PRFileDesc *_pr_stdin; michael@0: PRFileDesc *_pr_stdout; michael@0: PRFileDesc *_pr_stderr; michael@0: michael@0: #if !defined(_PR_PTHREADS) && !defined(_PR_BTHREADS) michael@0: michael@0: PRCList _pr_active_local_threadQ = michael@0: PR_INIT_STATIC_CLIST(&_pr_active_local_threadQ); michael@0: PRCList _pr_active_global_threadQ = michael@0: PR_INIT_STATIC_CLIST(&_pr_active_global_threadQ); michael@0: michael@0: _MDLock _pr_cpuLock; /* lock for the CPU Q */ michael@0: PRCList _pr_cpuQ = PR_INIT_STATIC_CLIST(&_pr_cpuQ); michael@0: michael@0: PRUint32 _pr_utid; michael@0: michael@0: PRInt32 _pr_userActive; michael@0: PRInt32 _pr_systemActive; michael@0: PRUintn _pr_maxPTDs; michael@0: michael@0: #ifdef _PR_LOCAL_THREADS_ONLY michael@0: michael@0: struct _PRCPU *_pr_currentCPU; michael@0: PRThread *_pr_currentThread; michael@0: PRThread *_pr_lastThread; michael@0: PRInt32 _pr_intsOff; michael@0: michael@0: #endif /* _PR_LOCAL_THREADS_ONLY */ michael@0: michael@0: /* Lock protecting all "termination" condition variables of all threads */ michael@0: PRLock *_pr_terminationCVLock; michael@0: michael@0: #endif /* !defined(_PR_PTHREADS) */ michael@0: michael@0: PRLock *_pr_sleeplock; /* used in PR_Sleep(), classic and pthreads */ michael@0: michael@0: static void _PR_InitCallOnce(void); michael@0: michael@0: PRBool _pr_initialized = PR_FALSE; michael@0: michael@0: michael@0: PR_IMPLEMENT(PRBool) PR_VersionCheck(const char *importedVersion) michael@0: { michael@0: /* michael@0: ** This is the secret handshake algorithm. michael@0: ** michael@0: ** This release has a simple version compatibility michael@0: ** check algorithm. This release is not backward michael@0: ** compatible with previous major releases. It is michael@0: ** not compatible with future major, minor, or michael@0: ** patch releases. michael@0: */ michael@0: int vmajor = 0, vminor = 0, vpatch = 0; michael@0: const char *ptr = importedVersion; michael@0: michael@0: while (isdigit(*ptr)) { michael@0: vmajor = 10 * vmajor + *ptr - '0'; michael@0: ptr++; michael@0: } michael@0: if (*ptr == '.') { michael@0: ptr++; michael@0: while (isdigit(*ptr)) { michael@0: vminor = 10 * vminor + *ptr - '0'; michael@0: ptr++; michael@0: } michael@0: if (*ptr == '.') { michael@0: ptr++; michael@0: while (isdigit(*ptr)) { michael@0: vpatch = 10 * vpatch + *ptr - '0'; michael@0: ptr++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (vmajor != PR_VMAJOR) { michael@0: return PR_FALSE; michael@0: } michael@0: if (vmajor == PR_VMAJOR && vminor > PR_VMINOR) { michael@0: return PR_FALSE; michael@0: } michael@0: if (vmajor == PR_VMAJOR && vminor == PR_VMINOR && vpatch > PR_VPATCH) { michael@0: return PR_FALSE; michael@0: } michael@0: return PR_TRUE; michael@0: } /* PR_VersionCheck */ michael@0: michael@0: PR_IMPLEMENT(const char*) PR_GetVersion(void) michael@0: { michael@0: return PR_VERSION; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRBool) PR_Initialized(void) michael@0: { michael@0: return _pr_initialized; michael@0: } michael@0: michael@0: PRInt32 _native_threads_only = 0; michael@0: michael@0: #ifdef WINNT michael@0: static void _pr_SetNativeThreadsOnlyMode(void) michael@0: { michael@0: HMODULE mainExe; michael@0: PRBool *globalp; michael@0: char *envp; michael@0: michael@0: mainExe = GetModuleHandle(NULL); michael@0: PR_ASSERT(NULL != mainExe); michael@0: globalp = (PRBool *) GetProcAddress(mainExe, "nspr_native_threads_only"); michael@0: if (globalp) { michael@0: _native_threads_only = (*globalp != PR_FALSE); michael@0: } else if (envp = getenv("NSPR_NATIVE_THREADS_ONLY")) { michael@0: _native_threads_only = (atoi(envp) == 1); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: static void _PR_InitStuff(void) michael@0: { michael@0: michael@0: if (_pr_initialized) return; michael@0: _pr_initialized = PR_TRUE; michael@0: #ifdef _PR_ZONE_ALLOCATOR michael@0: _PR_InitZones(); michael@0: #endif michael@0: #ifdef WINNT michael@0: _pr_SetNativeThreadsOnlyMode(); michael@0: #endif michael@0: michael@0: michael@0: (void) PR_GetPageSize(); michael@0: michael@0: _pr_clock_lm = PR_NewLogModule("clock"); michael@0: _pr_cmon_lm = PR_NewLogModule("cmon"); michael@0: _pr_io_lm = PR_NewLogModule("io"); michael@0: _pr_mon_lm = PR_NewLogModule("mon"); michael@0: _pr_linker_lm = PR_NewLogModule("linker"); michael@0: _pr_cvar_lm = PR_NewLogModule("cvar"); michael@0: _pr_sched_lm = PR_NewLogModule("sched"); michael@0: _pr_thread_lm = PR_NewLogModule("thread"); michael@0: _pr_gc_lm = PR_NewLogModule("gc"); michael@0: _pr_shm_lm = PR_NewLogModule("shm"); michael@0: _pr_shma_lm = PR_NewLogModule("shma"); michael@0: michael@0: /* NOTE: These init's cannot depend on _PR_MD_CURRENT_THREAD() */ michael@0: _PR_MD_EARLY_INIT(); michael@0: michael@0: _PR_InitLocks(); michael@0: _PR_InitAtomic(); michael@0: _PR_InitSegs(); michael@0: _PR_InitStacks(); michael@0: _PR_InitTPD(); michael@0: _PR_InitEnv(); michael@0: _PR_InitLayerCache(); michael@0: _PR_InitClock(); michael@0: michael@0: _pr_sleeplock = PR_NewLock(); michael@0: PR_ASSERT(NULL != _pr_sleeplock); michael@0: michael@0: _PR_InitThreads(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: michael@0: #ifdef WIN16 michael@0: { michael@0: PRInt32 top; /* artificial top of stack, win16 */ michael@0: _pr_top_of_task_stack = (char *) ⊤ michael@0: } michael@0: #endif michael@0: michael@0: #ifndef _PR_GLOBAL_THREADS_ONLY michael@0: _PR_InitCPUs(); michael@0: #endif michael@0: michael@0: /* michael@0: * XXX: call _PR_InitMem only on those platforms for which nspr implements michael@0: * malloc, for now. michael@0: */ michael@0: #ifdef _PR_OVERRIDE_MALLOC michael@0: _PR_InitMem(); michael@0: #endif michael@0: michael@0: _PR_InitCMon(); michael@0: _PR_InitIO(); michael@0: _PR_InitNet(); michael@0: _PR_InitTime(); michael@0: _PR_InitLog(); michael@0: _PR_InitLinker(); michael@0: _PR_InitCallOnce(); michael@0: _PR_InitDtoa(); michael@0: _PR_InitMW(); michael@0: _PR_InitRWLocks(); michael@0: michael@0: nspr_InitializePRErrorTable(); michael@0: michael@0: _PR_MD_FINAL_INIT(); michael@0: } michael@0: michael@0: void _PR_ImplicitInitialization(void) michael@0: { michael@0: _PR_InitStuff(); michael@0: michael@0: /* Enable interrupts */ michael@0: #if !defined(_PR_PTHREADS) && !defined(_PR_GLOBAL_THREADS_ONLY) michael@0: _PR_MD_START_INTERRUPTS(); michael@0: #endif michael@0: michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_DisableClockInterrupts(void) michael@0: { michael@0: #if !defined(_PR_PTHREADS) && !defined(_PR_BTHREADS) michael@0: if (!_pr_initialized) { michael@0: _PR_InitStuff(); michael@0: } else { michael@0: _PR_MD_DISABLE_CLOCK_INTERRUPTS(); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_EnableClockInterrupts(void) michael@0: { michael@0: #if !defined(_PR_PTHREADS) && !defined(_PR_BTHREADS) michael@0: if (!_pr_initialized) { michael@0: _PR_InitStuff(); michael@0: } michael@0: _PR_MD_ENABLE_CLOCK_INTERRUPTS(); michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_BlockClockInterrupts(void) michael@0: { michael@0: #if !defined(_PR_PTHREADS) && !defined(_PR_BTHREADS) michael@0: _PR_MD_BLOCK_CLOCK_INTERRUPTS(); michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_UnblockClockInterrupts(void) michael@0: { michael@0: #if !defined(_PR_PTHREADS) && !defined(_PR_BTHREADS) michael@0: _PR_MD_UNBLOCK_CLOCK_INTERRUPTS(); michael@0: #endif michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) PR_Init( michael@0: PRThreadType type, PRThreadPriority priority, PRUintn maxPTDs) michael@0: { michael@0: _PR_ImplicitInitialization(); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRIntn) PR_Initialize( michael@0: PRPrimordialFn prmain, PRIntn argc, char **argv, PRUintn maxPTDs) michael@0: { michael@0: PRIntn rv; michael@0: _PR_ImplicitInitialization(); michael@0: rv = prmain(argc, argv); michael@0: PR_Cleanup(); michael@0: return rv; michael@0: } /* PR_Initialize */ michael@0: michael@0: /* michael@0: *----------------------------------------------------------------------- michael@0: * michael@0: * _PR_CleanupBeforeExit -- michael@0: * michael@0: * Perform the cleanup work before exiting the process. michael@0: * We first do the cleanup generic to all platforms. Then michael@0: * we call _PR_MD_CLEANUP_BEFORE_EXIT(), where platform-dependent michael@0: * cleanup is done. This function is used by PR_Cleanup(). michael@0: * michael@0: * See also: PR_Cleanup(). michael@0: * michael@0: *----------------------------------------------------------------------- michael@0: */ michael@0: #if defined(_PR_PTHREADS) || defined(_PR_BTHREADS) michael@0: /* see ptthread.c */ michael@0: #else michael@0: static void michael@0: _PR_CleanupBeforeExit(void) michael@0: { michael@0: /* michael@0: Do not make any calls here other than to destroy resources. For example, michael@0: do not make any calls that eventually may end up in PR_Lock. Because the michael@0: thread is destroyed, can not access current thread any more. michael@0: */ michael@0: _PR_CleanupTPD(); michael@0: if (_pr_terminationCVLock) michael@0: /* michael@0: * In light of the comment above, this looks real suspicious. michael@0: * I'd go so far as to say it's just a problem waiting to happen. michael@0: */ michael@0: PR_DestroyLock(_pr_terminationCVLock); michael@0: michael@0: _PR_MD_CLEANUP_BEFORE_EXIT(); michael@0: } michael@0: #endif /* defined(_PR_PTHREADS) */ michael@0: michael@0: /* michael@0: *---------------------------------------------------------------------- michael@0: * michael@0: * PR_Cleanup -- michael@0: * michael@0: * Perform a graceful shutdown of the NSPR runtime. PR_Cleanup() may michael@0: * only be called from the primordial thread, typically at the michael@0: * end of the main() function. It returns when it has completed michael@0: * its platform-dependent duty and the process must not make any other michael@0: * NSPR library calls prior to exiting from main(). michael@0: * michael@0: * PR_Cleanup() first blocks the primordial thread until all the michael@0: * other user (non-system) threads, if any, have terminated. michael@0: * Then it performs cleanup in preparation for exiting the process. michael@0: * PR_Cleanup() does not exit the primordial thread (which would michael@0: * in turn exit the process). michael@0: * michael@0: * PR_Cleanup() only responds when it is called by the primordial michael@0: * thread. Calls by any other thread are silently ignored. michael@0: * michael@0: * See also: PR_ExitProcess() michael@0: * michael@0: *---------------------------------------------------------------------- michael@0: */ michael@0: #if defined(_PR_PTHREADS) || defined(_PR_BTHREADS) michael@0: /* see ptthread.c */ michael@0: #else michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_Cleanup() michael@0: { michael@0: PRThread *me = PR_GetCurrentThread(); michael@0: PR_ASSERT((NULL != me) && (me->flags & _PR_PRIMORDIAL)); michael@0: if ((NULL != me) && (me->flags & _PR_PRIMORDIAL)) michael@0: { michael@0: PR_LOG(_pr_thread_lm, PR_LOG_MIN, ("PR_Cleanup: shutting down NSPR")); michael@0: michael@0: /* michael@0: * No more recycling of threads michael@0: */ michael@0: _pr_recycleThreads = 0; michael@0: michael@0: /* michael@0: * Wait for all other user (non-system/daemon) threads michael@0: * to terminate. michael@0: */ michael@0: PR_Lock(_pr_activeLock); michael@0: while (_pr_userActive > _pr_primordialExitCount) { michael@0: PR_WaitCondVar(_pr_primordialExitCVar, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: if (me->flags & _PR_SYSTEM) { michael@0: _pr_systemActive--; michael@0: } else { michael@0: _pr_userActive--; michael@0: } michael@0: PR_Unlock(_pr_activeLock); michael@0: michael@0: #ifdef IRIX michael@0: _PR_MD_PRE_CLEANUP(me); michael@0: /* michael@0: * The primordial thread must now be running on the primordial cpu michael@0: */ michael@0: PR_ASSERT((_PR_IS_NATIVE_THREAD(me)) || (me->cpu->id == 0)); michael@0: #endif michael@0: michael@0: _PR_MD_EARLY_CLEANUP(); michael@0: michael@0: _PR_CleanupMW(); michael@0: _PR_CleanupTime(); michael@0: _PR_CleanupDtoa(); michael@0: _PR_CleanupCallOnce(); michael@0: _PR_ShutdownLinker(); michael@0: _PR_CleanupNet(); michael@0: _PR_CleanupIO(); michael@0: /* Release the primordial thread's private data, etc. */ michael@0: _PR_CleanupThread(me); michael@0: michael@0: _PR_MD_STOP_INTERRUPTS(); michael@0: michael@0: PR_LOG(_pr_thread_lm, PR_LOG_MIN, michael@0: ("PR_Cleanup: clean up before destroying thread")); michael@0: _PR_LogCleanup(); michael@0: michael@0: /* michael@0: * This part should look like the end of _PR_NativeRunThread michael@0: * and _PR_UserRunThread. michael@0: */ michael@0: if (_PR_IS_NATIVE_THREAD(me)) { michael@0: _PR_MD_EXIT_THREAD(me); michael@0: _PR_NativeDestroyThread(me); michael@0: } else { michael@0: _PR_UserDestroyThread(me); michael@0: PR_DELETE(me->stack); michael@0: PR_DELETE(me); michael@0: } michael@0: michael@0: /* michael@0: * XXX: We are freeing the heap memory here so that Purify won't michael@0: * complain, but we should also free other kinds of resources michael@0: * that are allocated by the _PR_InitXXX() functions. michael@0: * Ideally, for each _PR_InitXXX(), there should be a corresponding michael@0: * _PR_XXXCleanup() that we can call here. michael@0: */ michael@0: #ifdef WINNT michael@0: _PR_CleanupCPUs(); michael@0: #endif michael@0: _PR_CleanupThreads(); michael@0: _PR_CleanupCMon(); michael@0: PR_DestroyLock(_pr_sleeplock); michael@0: _pr_sleeplock = NULL; michael@0: _PR_CleanupLayerCache(); michael@0: _PR_CleanupEnv(); michael@0: _PR_CleanupStacks(); michael@0: _PR_CleanupBeforeExit(); michael@0: _pr_initialized = PR_FALSE; michael@0: return PR_SUCCESS; michael@0: } michael@0: return PR_FAILURE; michael@0: } michael@0: #endif /* defined(_PR_PTHREADS) */ michael@0: michael@0: /* michael@0: *------------------------------------------------------------------------ michael@0: * PR_ProcessExit -- michael@0: * michael@0: * Cause an immediate, nongraceful, forced termination of the process. michael@0: * It takes a PRIntn argument, which is the exit status code of the michael@0: * process. michael@0: * michael@0: * See also: PR_Cleanup() michael@0: * michael@0: *------------------------------------------------------------------------ michael@0: */ michael@0: michael@0: #if defined(_PR_PTHREADS) || defined(_PR_BTHREADS) michael@0: /* see ptthread.c */ michael@0: #else michael@0: PR_IMPLEMENT(void) PR_ProcessExit(PRIntn status) michael@0: { michael@0: _PR_MD_EXIT(status); michael@0: } michael@0: michael@0: #endif /* defined(_PR_PTHREADS) */ michael@0: michael@0: PR_IMPLEMENT(PRProcessAttr *) michael@0: PR_NewProcessAttr(void) michael@0: { michael@0: PRProcessAttr *attr; michael@0: michael@0: attr = PR_NEWZAP(PRProcessAttr); michael@0: if (!attr) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: } michael@0: return attr; michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_ResetProcessAttr(PRProcessAttr *attr) michael@0: { michael@0: PR_FREEIF(attr->currentDirectory); michael@0: PR_FREEIF(attr->fdInheritBuffer); michael@0: memset(attr, 0, sizeof(*attr)); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_DestroyProcessAttr(PRProcessAttr *attr) michael@0: { michael@0: PR_FREEIF(attr->currentDirectory); michael@0: PR_FREEIF(attr->fdInheritBuffer); michael@0: PR_DELETE(attr); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PR_ProcessAttrSetStdioRedirect( michael@0: PRProcessAttr *attr, michael@0: PRSpecialFD stdioFd, michael@0: PRFileDesc *redirectFd) michael@0: { michael@0: switch (stdioFd) { michael@0: case PR_StandardInput: michael@0: attr->stdinFd = redirectFd; michael@0: break; michael@0: case PR_StandardOutput: michael@0: attr->stdoutFd = redirectFd; michael@0: break; michael@0: case PR_StandardError: michael@0: attr->stderrFd = redirectFd; michael@0: break; michael@0: default: michael@0: PR_ASSERT(0); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * OBSOLETE michael@0: */ michael@0: PR_IMPLEMENT(void) michael@0: PR_SetStdioRedirect( michael@0: PRProcessAttr *attr, michael@0: PRSpecialFD stdioFd, michael@0: PRFileDesc *redirectFd) michael@0: { michael@0: #if defined(DEBUG) michael@0: static PRBool warn = PR_TRUE; michael@0: if (warn) { michael@0: warn = _PR_Obsolete("PR_SetStdioRedirect()", michael@0: "PR_ProcessAttrSetStdioRedirect()"); michael@0: } michael@0: #endif michael@0: PR_ProcessAttrSetStdioRedirect(attr, stdioFd, redirectFd); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_ProcessAttrSetCurrentDirectory( michael@0: PRProcessAttr *attr, michael@0: const char *dir) michael@0: { michael@0: PR_FREEIF(attr->currentDirectory); michael@0: attr->currentDirectory = (char *) PR_MALLOC(strlen(dir) + 1); michael@0: if (!attr->currentDirectory) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: strcpy(attr->currentDirectory, dir); michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_ProcessAttrSetInheritableFD( michael@0: PRProcessAttr *attr, michael@0: PRFileDesc *fd, michael@0: const char *name) michael@0: { michael@0: /* We malloc the fd inherit buffer in multiples of this number. */ michael@0: #define FD_INHERIT_BUFFER_INCR 128 michael@0: /* The length of "NSPR_INHERIT_FDS=" */ michael@0: #define NSPR_INHERIT_FDS_STRLEN 17 michael@0: /* The length of osfd (PROsfd) printed in hexadecimal with 0x prefix */ michael@0: #ifdef _WIN64 michael@0: #define OSFD_STRLEN 18 michael@0: #else michael@0: #define OSFD_STRLEN 10 michael@0: #endif michael@0: /* The length of fd type (PRDescType) printed in decimal */ michael@0: #define FD_TYPE_STRLEN 1 michael@0: PRSize newSize; michael@0: int remainder; michael@0: char *newBuffer; michael@0: int nwritten; michael@0: char *cur; michael@0: int freeSize; michael@0: michael@0: if (fd->identity != PR_NSPR_IO_LAYER) { michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: if (fd->secret->inheritable == _PR_TRI_UNKNOWN) { michael@0: _PR_MD_QUERY_FD_INHERITABLE(fd); michael@0: } michael@0: if (fd->secret->inheritable != _PR_TRI_TRUE) { michael@0: PR_SetError(PR_NO_ACCESS_RIGHTS_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: /* michael@0: * We also need to account for the : separators and the michael@0: * terminating null byte. michael@0: */ michael@0: if (NULL == attr->fdInheritBuffer) { michael@0: /* The first time, we print "NSPR_INHERIT_FDS=::" */ michael@0: newSize = NSPR_INHERIT_FDS_STRLEN + strlen(name) michael@0: + FD_TYPE_STRLEN + OSFD_STRLEN + 2 + 1; michael@0: } else { michael@0: /* At other times, we print ":::" */ michael@0: newSize = attr->fdInheritBufferUsed + strlen(name) michael@0: + FD_TYPE_STRLEN + OSFD_STRLEN + 3 + 1; michael@0: } michael@0: if (newSize > attr->fdInheritBufferSize) { michael@0: /* Make newSize a multiple of FD_INHERIT_BUFFER_INCR */ michael@0: remainder = newSize % FD_INHERIT_BUFFER_INCR; michael@0: if (remainder != 0) { michael@0: newSize += (FD_INHERIT_BUFFER_INCR - remainder); michael@0: } michael@0: if (NULL == attr->fdInheritBuffer) { michael@0: newBuffer = (char *) PR_MALLOC(newSize); michael@0: } else { michael@0: newBuffer = (char *) PR_REALLOC(attr->fdInheritBuffer, newSize); michael@0: } michael@0: if (NULL == newBuffer) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: attr->fdInheritBuffer = newBuffer; michael@0: attr->fdInheritBufferSize = newSize; michael@0: } michael@0: cur = attr->fdInheritBuffer + attr->fdInheritBufferUsed; michael@0: freeSize = attr->fdInheritBufferSize - attr->fdInheritBufferUsed; michael@0: if (0 == attr->fdInheritBufferUsed) { michael@0: nwritten = PR_snprintf(cur, freeSize, michael@0: "NSPR_INHERIT_FDS=%s:%d:0x%" PR_PRIxOSFD, michael@0: name, (PRIntn)fd->methods->file_type, fd->secret->md.osfd); michael@0: } else { michael@0: nwritten = PR_snprintf(cur, freeSize, ":%s:%d:0x%" PR_PRIxOSFD, michael@0: name, (PRIntn)fd->methods->file_type, fd->secret->md.osfd); michael@0: } michael@0: attr->fdInheritBufferUsed += nwritten; michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRFileDesc *) PR_GetInheritedFD( michael@0: const char *name) michael@0: { michael@0: PRFileDesc *fd; michael@0: const char *envVar; michael@0: const char *ptr; michael@0: int len = strlen(name); michael@0: PROsfd osfd; michael@0: int nColons; michael@0: PRIntn fileType; michael@0: michael@0: envVar = PR_GetEnv("NSPR_INHERIT_FDS"); michael@0: if (NULL == envVar || '\0' == envVar[0]) { michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: michael@0: ptr = envVar; michael@0: while (1) { michael@0: if ((ptr[len] == ':') && (strncmp(ptr, name, len) == 0)) { michael@0: ptr += len + 1; michael@0: PR_sscanf(ptr, "%d:0x%" PR_SCNxOSFD, &fileType, &osfd); michael@0: switch ((PRDescType)fileType) { michael@0: case PR_DESC_FILE: michael@0: fd = PR_ImportFile(osfd); michael@0: break; michael@0: case PR_DESC_PIPE: michael@0: fd = PR_ImportPipe(osfd); michael@0: break; michael@0: case PR_DESC_SOCKET_TCP: michael@0: fd = PR_ImportTCPSocket(osfd); michael@0: break; michael@0: case PR_DESC_SOCKET_UDP: michael@0: fd = PR_ImportUDPSocket(osfd); michael@0: break; michael@0: default: michael@0: PR_ASSERT(0); michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: fd = NULL; michael@0: break; michael@0: } michael@0: if (fd) { michael@0: /* michael@0: * An inherited FD is inheritable by default. michael@0: * The child process needs to call PR_SetFDInheritable michael@0: * to make it non-inheritable if so desired. michael@0: */ michael@0: fd->secret->inheritable = _PR_TRI_TRUE; michael@0: } michael@0: return fd; michael@0: } michael@0: /* Skip three colons */ michael@0: nColons = 0; michael@0: while (*ptr) { michael@0: if (*ptr == ':') { michael@0: if (++nColons == 3) { michael@0: break; michael@0: } michael@0: } michael@0: ptr++; michael@0: } michael@0: if (*ptr == '\0') { michael@0: PR_SetError(PR_UNKNOWN_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: ptr++; michael@0: } michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRProcess*) PR_CreateProcess( michael@0: const char *path, michael@0: char *const *argv, michael@0: char *const *envp, michael@0: const PRProcessAttr *attr) michael@0: { michael@0: return _PR_MD_CREATE_PROCESS(path, argv, envp, attr); michael@0: } /* PR_CreateProcess */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_CreateProcessDetached( 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: PRStatus rv; michael@0: michael@0: process = PR_CreateProcess(path, argv, envp, attr); michael@0: if (NULL == process) { michael@0: return PR_FAILURE; michael@0: } michael@0: rv = PR_DetachProcess(process); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: if (rv == PR_FAILURE) { michael@0: PR_DELETE(process); michael@0: return PR_FAILURE; michael@0: } michael@0: return PR_SUCCESS; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_DetachProcess(PRProcess *process) michael@0: { michael@0: return _PR_MD_DETACH_PROCESS(process); michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_WaitProcess(PRProcess *process, PRInt32 *exitCode) michael@0: { michael@0: return _PR_MD_WAIT_PROCESS(process, exitCode); michael@0: } /* PR_WaitProcess */ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_KillProcess(PRProcess *process) michael@0: { michael@0: return _PR_MD_KILL_PROCESS(process); michael@0: } michael@0: michael@0: /* michael@0: ******************************************************************** michael@0: * michael@0: * Module initialization michael@0: * michael@0: ******************************************************************** michael@0: */ michael@0: michael@0: static struct { michael@0: PRLock *ml; michael@0: PRCondVar *cv; michael@0: } mod_init; michael@0: michael@0: static void _PR_InitCallOnce(void) { michael@0: mod_init.ml = PR_NewLock(); michael@0: PR_ASSERT(NULL != mod_init.ml); michael@0: mod_init.cv = PR_NewCondVar(mod_init.ml); michael@0: PR_ASSERT(NULL != mod_init.cv); michael@0: } michael@0: michael@0: void _PR_CleanupCallOnce() michael@0: { michael@0: PR_DestroyLock(mod_init.ml); michael@0: mod_init.ml = NULL; michael@0: PR_DestroyCondVar(mod_init.cv); michael@0: mod_init.cv = NULL; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_CallOnce( michael@0: PRCallOnceType *once, michael@0: PRCallOnceFN func) michael@0: { michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: if (!once->initialized) { michael@0: if (PR_ATOMIC_SET(&once->inProgress, 1) == 0) { michael@0: once->status = (*func)(); michael@0: PR_Lock(mod_init.ml); michael@0: once->initialized = 1; michael@0: PR_NotifyAllCondVar(mod_init.cv); michael@0: PR_Unlock(mod_init.ml); michael@0: } else { michael@0: PR_Lock(mod_init.ml); michael@0: while (!once->initialized) { michael@0: PR_WaitCondVar(mod_init.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: PR_Unlock(mod_init.ml); michael@0: } michael@0: } else { michael@0: if (PR_SUCCESS != once->status) { michael@0: PR_SetError(PR_CALL_ONCE_ERROR, 0); michael@0: } michael@0: } michael@0: return once->status; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_CallOnceWithArg( michael@0: PRCallOnceType *once, michael@0: PRCallOnceWithArgFN func, michael@0: void *arg) michael@0: { michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: if (!once->initialized) { michael@0: if (PR_ATOMIC_SET(&once->inProgress, 1) == 0) { michael@0: once->status = (*func)(arg); michael@0: PR_Lock(mod_init.ml); michael@0: once->initialized = 1; michael@0: PR_NotifyAllCondVar(mod_init.cv); michael@0: PR_Unlock(mod_init.ml); michael@0: } else { michael@0: PR_Lock(mod_init.ml); michael@0: while (!once->initialized) { michael@0: PR_WaitCondVar(mod_init.cv, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: PR_Unlock(mod_init.ml); michael@0: } michael@0: } else { michael@0: if (PR_SUCCESS != once->status) { michael@0: PR_SetError(PR_CALL_ONCE_ERROR, 0); michael@0: } michael@0: } michael@0: return once->status; michael@0: } michael@0: michael@0: PRBool _PR_Obsolete(const char *obsolete, const char *preferred) michael@0: { michael@0: #if defined(DEBUG) michael@0: PR_fprintf( michael@0: PR_STDERR, "'%s' is obsolete. Use '%s' instead.\n", michael@0: obsolete, (NULL == preferred) ? "something else" : preferred); michael@0: #endif michael@0: return PR_FALSE; michael@0: } /* _PR_Obsolete */ michael@0: michael@0: /* prinit.c */ michael@0: michael@0: