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