| OLD | NEW |
| (Empty) |
| 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | |
| 2 /* This Source Code Form is subject to the terms of the Mozilla Public | |
| 3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| 5 | |
| 6 #include "primpl.h" | |
| 7 | |
| 8 #include <string.h> /* for memset() */ | |
| 9 | |
| 10 | |
| 11 /************************************************************************/ | |
| 12 | |
| 13 PRLock *_pr_flock_lock; | |
| 14 PRCondVar *_pr_flock_cv; | |
| 15 | |
| 16 #ifdef WINCE | |
| 17 /* | |
| 18 * There are no stdin, stdout, stderr in Windows CE. INVALID_HANDLE_VALUE | |
| 19 * should cause all I/O functions on the handle to fail. | |
| 20 */ | |
| 21 #define STD_INPUT_HANDLE ((DWORD)-10) | |
| 22 #define STD_OUTPUT_HANDLE ((DWORD)-11) | |
| 23 #define STD_ERROR_HANDLE ((DWORD)-12) | |
| 24 | |
| 25 static HANDLE GetStdHandle(DWORD nStdHandle) | |
| 26 { | |
| 27 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); | |
| 28 return INVALID_HANDLE_VALUE; | |
| 29 } | |
| 30 #endif | |
| 31 | |
| 32 void _PR_InitIO(void) | |
| 33 { | |
| 34 const PRIOMethods *methods = PR_GetFileMethods(); | |
| 35 | |
| 36 _PR_InitFdCache(); | |
| 37 | |
| 38 _pr_flock_lock = PR_NewLock(); | |
| 39 _pr_flock_cv = PR_NewCondVar(_pr_flock_lock); | |
| 40 | |
| 41 #ifdef WIN32 | |
| 42 _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE), | |
| 43 methods); | |
| 44 _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE), | |
| 45 methods); | |
| 46 _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE), | |
| 47 methods); | |
| 48 #ifdef WINNT | |
| 49 _pr_stdin->secret->md.sync_file_io = PR_TRUE; | |
| 50 _pr_stdout->secret->md.sync_file_io = PR_TRUE; | |
| 51 _pr_stderr->secret->md.sync_file_io = PR_TRUE; | |
| 52 #endif | |
| 53 #else | |
| 54 _pr_stdin = PR_AllocFileDesc(0, methods); | |
| 55 _pr_stdout = PR_AllocFileDesc(1, methods); | |
| 56 _pr_stderr = PR_AllocFileDesc(2, methods); | |
| 57 #endif | |
| 58 _PR_MD_INIT_FD_INHERITABLE(_pr_stdin, PR_TRUE); | |
| 59 _PR_MD_INIT_FD_INHERITABLE(_pr_stdout, PR_TRUE); | |
| 60 _PR_MD_INIT_FD_INHERITABLE(_pr_stderr, PR_TRUE); | |
| 61 } | |
| 62 | |
| 63 void _PR_CleanupIO(void) | |
| 64 { | |
| 65 PR_FreeFileDesc(_pr_stdin); | |
| 66 _pr_stdin = NULL; | |
| 67 PR_FreeFileDesc(_pr_stdout); | |
| 68 _pr_stdout = NULL; | |
| 69 PR_FreeFileDesc(_pr_stderr); | |
| 70 _pr_stderr = NULL; | |
| 71 | |
| 72 if (_pr_flock_cv) { | |
| 73 PR_DestroyCondVar(_pr_flock_cv); | |
| 74 _pr_flock_cv = NULL; | |
| 75 } | |
| 76 if (_pr_flock_lock) { | |
| 77 PR_DestroyLock(_pr_flock_lock); | |
| 78 _pr_flock_lock = NULL; | |
| 79 } | |
| 80 | |
| 81 _PR_CleanupFdCache(); | |
| 82 } | |
| 83 | |
| 84 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd) | |
| 85 { | |
| 86 PRFileDesc *result = NULL; | |
| 87 PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError); | |
| 88 | |
| 89 if (!_pr_initialized) _PR_ImplicitInitialization(); | |
| 90 | |
| 91 switch (osfd) | |
| 92 { | |
| 93 case PR_StandardInput: result = _pr_stdin; break; | |
| 94 case PR_StandardOutput: result = _pr_stdout; break; | |
| 95 case PR_StandardError: result = _pr_stderr; break; | |
| 96 default: | |
| 97 (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); | |
| 98 } | |
| 99 return result; | |
| 100 } | |
| 101 | |
| 102 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc( | |
| 103 PROsfd osfd, const PRIOMethods *methods) | |
| 104 { | |
| 105 PRFileDesc *fd; | |
| 106 | |
| 107 #ifdef XP_UNIX | |
| 108 /* | |
| 109 * Assert that the file descriptor is small enough to fit in the | |
| 110 * fd_set passed to select | |
| 111 */ | |
| 112 PR_ASSERT(osfd < FD_SETSIZE); | |
| 113 #endif | |
| 114 fd = _PR_Getfd(); | |
| 115 if (fd) { | |
| 116 /* Initialize the members of PRFileDesc and PRFilePrivate */ | |
| 117 fd->methods = methods; | |
| 118 fd->secret->state = _PR_FILEDESC_OPEN; | |
| 119 fd->secret->md.osfd = osfd; | |
| 120 _PR_MD_INIT_FILEDESC(fd); | |
| 121 } else { | |
| 122 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); | |
| 123 } | |
| 124 | |
| 125 return fd; | |
| 126 } | |
| 127 | |
| 128 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd) | |
| 129 { | |
| 130 PR_ASSERT(fd); | |
| 131 _PR_Putfd(fd); | |
| 132 } | |
| 133 | |
| 134 /* | |
| 135 ** Wait for some i/o to finish on one or more more poll descriptors. | |
| 136 */ | |
| 137 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeo
ut) | |
| 138 { | |
| 139 return(_PR_MD_PR_POLL(pds, npds, timeout)); | |
| 140 } | |
| 141 | |
| 142 /* | |
| 143 ** Set the inheritance attribute of a file descriptor. | |
| 144 */ | |
| 145 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable( | |
| 146 PRFileDesc *fd, | |
| 147 PRBool inheritable) | |
| 148 { | |
| 149 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS) | |
| 150 /* | |
| 151 * Only a non-layered, NSPR file descriptor can be inherited | |
| 152 * by a child process. | |
| 153 */ | |
| 154 if (fd->identity != PR_NSPR_IO_LAYER) { | |
| 155 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); | |
| 156 return PR_FAILURE; | |
| 157 } | |
| 158 if (fd->secret->inheritable != inheritable) { | |
| 159 if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) { | |
| 160 return PR_FAILURE; | |
| 161 } | |
| 162 fd->secret->inheritable = inheritable; | |
| 163 } | |
| 164 return PR_SUCCESS; | |
| 165 #else | |
| 166 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | |
| 167 return PR_FAILURE; | |
| 168 #endif | |
| 169 } | |
| 170 | |
| 171 /* | |
| 172 ** This function only has a useful implementation in the debug build of | |
| 173 ** the pthreads version. | |
| 174 */ | |
| 175 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg) | |
| 176 { | |
| 177 /* do nothing */ | |
| 178 } /* PT_FPrintStats */ | |
| OLD | NEW |