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