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 /* for memset() */ andre@0: andre@0: andre@0: /************************************************************************/ andre@0: andre@0: PRLock *_pr_flock_lock; andre@0: PRCondVar *_pr_flock_cv; andre@0: andre@0: #ifdef WINCE andre@0: /* andre@0: * There are no stdin, stdout, stderr in Windows CE. INVALID_HANDLE_VALUE andre@0: * should cause all I/O functions on the handle to fail. andre@0: */ andre@0: #define STD_INPUT_HANDLE ((DWORD)-10) andre@0: #define STD_OUTPUT_HANDLE ((DWORD)-11) andre@0: #define STD_ERROR_HANDLE ((DWORD)-12) andre@0: andre@0: static HANDLE GetStdHandle(DWORD nStdHandle) andre@0: { andre@0: SetLastError(ERROR_CALL_NOT_IMPLEMENTED); andre@0: return INVALID_HANDLE_VALUE; andre@0: } andre@0: #endif andre@0: andre@0: void _PR_InitIO(void) andre@0: { andre@0: const PRIOMethods *methods = PR_GetFileMethods(); andre@0: andre@0: _PR_InitFdCache(); andre@0: andre@0: _pr_flock_lock = PR_NewLock(); andre@0: _pr_flock_cv = PR_NewCondVar(_pr_flock_lock); andre@0: andre@0: #ifdef WIN32 andre@0: _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE), andre@0: methods); andre@0: _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE), andre@0: methods); andre@0: _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE), andre@0: methods); andre@0: #ifdef WINNT andre@0: _pr_stdin->secret->md.sync_file_io = PR_TRUE; andre@0: _pr_stdout->secret->md.sync_file_io = PR_TRUE; andre@0: _pr_stderr->secret->md.sync_file_io = PR_TRUE; andre@0: #endif andre@0: #else andre@0: _pr_stdin = PR_AllocFileDesc(0, methods); andre@0: _pr_stdout = PR_AllocFileDesc(1, methods); andre@0: _pr_stderr = PR_AllocFileDesc(2, methods); andre@0: #endif andre@0: _PR_MD_INIT_FD_INHERITABLE(_pr_stdin, PR_TRUE); andre@0: _PR_MD_INIT_FD_INHERITABLE(_pr_stdout, PR_TRUE); andre@0: _PR_MD_INIT_FD_INHERITABLE(_pr_stderr, PR_TRUE); andre@0: } andre@0: andre@0: void _PR_CleanupIO(void) andre@0: { andre@0: PR_FreeFileDesc(_pr_stdin); andre@0: _pr_stdin = NULL; andre@0: PR_FreeFileDesc(_pr_stdout); andre@0: _pr_stdout = NULL; andre@0: PR_FreeFileDesc(_pr_stderr); andre@0: _pr_stderr = NULL; andre@0: andre@0: if (_pr_flock_cv) { andre@0: PR_DestroyCondVar(_pr_flock_cv); andre@0: _pr_flock_cv = NULL; andre@0: } andre@0: if (_pr_flock_lock) { andre@0: PR_DestroyLock(_pr_flock_lock); andre@0: _pr_flock_lock = NULL; andre@0: } andre@0: andre@0: _PR_CleanupFdCache(); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd) andre@0: { andre@0: PRFileDesc *result = NULL; andre@0: PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError); andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: switch (osfd) andre@0: { andre@0: case PR_StandardInput: result = _pr_stdin; break; andre@0: case PR_StandardOutput: result = _pr_stdout; break; andre@0: case PR_StandardError: result = _pr_stderr; break; andre@0: default: andre@0: (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: } andre@0: return result; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc( andre@0: PROsfd osfd, const PRIOMethods *methods) andre@0: { andre@0: PRFileDesc *fd; andre@0: andre@0: #ifdef XP_UNIX andre@0: /* andre@0: * Assert that the file descriptor is small enough to fit in the andre@0: * fd_set passed to select andre@0: */ andre@0: PR_ASSERT(osfd < FD_SETSIZE); andre@0: #endif andre@0: fd = _PR_Getfd(); andre@0: if (fd) { andre@0: /* Initialize the members of PRFileDesc and PRFilePrivate */ andre@0: fd->methods = methods; andre@0: fd->secret->state = _PR_FILEDESC_OPEN; andre@0: fd->secret->md.osfd = osfd; andre@0: _PR_MD_INIT_FILEDESC(fd); andre@0: } else { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: } andre@0: andre@0: return fd; andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd) andre@0: { andre@0: PR_ASSERT(fd); andre@0: _PR_Putfd(fd); andre@0: } andre@0: andre@0: /* andre@0: ** Wait for some i/o to finish on one or more more poll descriptors. andre@0: */ andre@0: PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout) andre@0: { andre@0: return(_PR_MD_PR_POLL(pds, npds, timeout)); andre@0: } andre@0: andre@0: /* andre@0: ** Set the inheritance attribute of a file descriptor. andre@0: */ andre@0: PR_IMPLEMENT(PRStatus) PR_SetFDInheritable( andre@0: PRFileDesc *fd, andre@0: PRBool inheritable) andre@0: { andre@0: #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS) andre@0: /* andre@0: * Only a non-layered, NSPR file descriptor can be inherited andre@0: * by a child process. andre@0: */ andre@0: if (fd->identity != PR_NSPR_IO_LAYER) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: if (fd->secret->inheritable != inheritable) { andre@0: if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) { andre@0: return PR_FAILURE; andre@0: } andre@0: fd->secret->inheritable = inheritable; andre@0: } andre@0: return PR_SUCCESS; andre@0: #else andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: #endif andre@0: } andre@0: andre@0: /* andre@0: ** This function only has a useful implementation in the debug build of andre@0: ** the pthreads version. andre@0: */ andre@0: PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg) andre@0: { andre@0: /* do nothing */ andre@0: } /* PT_FPrintStats */