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 
GetStdHandle(DWORD nStdHandle)25 static HANDLE GetStdHandle(DWORD nStdHandle)
26 {
27     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
28     return INVALID_HANDLE_VALUE;
29 }
30 #endif
31 
_PR_InitIO(void)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     _PR_MD_INIT_IO();
63 }
64 
_PR_CleanupIO(void)65 void _PR_CleanupIO(void)
66 {
67     PR_FreeFileDesc(_pr_stdin);
68     _pr_stdin = NULL;
69     PR_FreeFileDesc(_pr_stdout);
70     _pr_stdout = NULL;
71     PR_FreeFileDesc(_pr_stderr);
72     _pr_stderr = NULL;
73 
74     if (_pr_flock_cv) {
75         PR_DestroyCondVar(_pr_flock_cv);
76         _pr_flock_cv = NULL;
77     }
78     if (_pr_flock_lock) {
79         PR_DestroyLock(_pr_flock_lock);
80         _pr_flock_lock = NULL;
81     }
82 
83     _PR_CleanupFdCache();
84 }
85 
PR_GetSpecialFD(PRSpecialFD osfd)86 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
87 {
88     PRFileDesc *result = NULL;
89     PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
90 
91     if (!_pr_initialized) {
92         _PR_ImplicitInitialization();
93     }
94 
95     switch (osfd)
96     {
97         case PR_StandardInput: result = _pr_stdin; break;
98         case PR_StandardOutput: result = _pr_stdout; break;
99         case PR_StandardError: result = _pr_stderr; break;
100         default:
101             (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
102     }
103     return result;
104 }
105 
PR_AllocFileDesc(PROsfd osfd,const PRIOMethods * methods)106 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
107     PROsfd osfd, const PRIOMethods *methods)
108 {
109     PRFileDesc *fd;
110 
111 #ifdef XP_UNIX
112     /*
113      * Assert that the file descriptor is small enough to fit in the
114      * fd_set passed to select
115      */
116     PR_ASSERT(osfd < FD_SETSIZE);
117 #endif
118     fd = _PR_Getfd();
119     if (fd) {
120         /* Initialize the members of PRFileDesc and PRFilePrivate */
121         fd->methods = methods;
122         fd->secret->state = _PR_FILEDESC_OPEN;
123         fd->secret->md.osfd = osfd;
124 #if defined(_WIN64)
125         fd->secret->alreadyConnected = PR_FALSE;
126         fd->secret->overlappedActive = PR_FALSE;
127 #endif
128         _PR_MD_INIT_FILEDESC(fd);
129     } else {
130         PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
131     }
132 
133     return fd;
134 }
135 
PR_FreeFileDesc(PRFileDesc * fd)136 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
137 {
138     PR_ASSERT(fd);
139     _PR_Putfd(fd);
140 }
141 
142 #if defined(_WIN64) && defined(WIN95)
143 
144 PRFileDescList *_fd_waiting_for_overlapped_done = NULL;
145 PRLock *_fd_waiting_for_overlapped_done_lock = NULL;
146 
CheckOverlappedPendingSocketsAreDone()147 void CheckOverlappedPendingSocketsAreDone()
148 {
149     if (!_fd_waiting_for_overlapped_done_lock ||
150         !_fd_waiting_for_overlapped_done) {
151         return;
152     }
153 
154     PR_Lock(_fd_waiting_for_overlapped_done_lock);
155 
156     PRFileDescList *cur = _fd_waiting_for_overlapped_done;
157     PRFileDescList *previous = NULL;
158     while (cur) {
159         PR_ASSERT(cur->fd->secret->overlappedActive);
160         PRFileDesc *fd = cur->fd;
161         DWORD rvSent;
162         if (GetOverlappedResult((HANDLE)fd->secret->md.osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) {
163             fd->secret->overlappedActive = PR_FALSE;
164             PR_LOG(_pr_io_lm, PR_LOG_MIN,
165                    ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult succeeded\n"));
166         } else {
167             DWORD err = WSAGetLastError();
168             PR_LOG(_pr_io_lm, PR_LOG_MIN,
169                    ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult failed %d\n", err));
170             if (err != ERROR_IO_INCOMPLETE) {
171                 fd->secret->overlappedActive = PR_FALSE;
172             }
173         }
174 
175         if (!fd->secret->overlappedActive) {
176 
177             _PR_MD_CLOSE_SOCKET(fd->secret->md.osfd);
178             fd->secret->state = _PR_FILEDESC_CLOSED;
179 #ifdef _PR_HAVE_PEEK_BUFFER
180             if (fd->secret->peekBuffer) {
181                 PR_ASSERT(fd->secret->peekBufSize > 0);
182                 PR_DELETE(fd->secret->peekBuffer);
183                 fd->secret->peekBufSize = 0;
184                 fd->secret->peekBytes = 0;
185             }
186 #endif
187 
188             PR_FreeFileDesc(fd);
189 
190             if (previous) {
191                 previous->next = cur->next;
192             } else {
193                 _fd_waiting_for_overlapped_done = cur->next;
194             }
195             PRFileDescList *del = cur;
196             cur = cur->next;
197             PR_Free(del);
198         } else {
199             previous = cur;
200             cur = cur->next;
201         }
202     }
203 
204     PR_Unlock(_fd_waiting_for_overlapped_done_lock);
205 }
206 #endif
207 
208 /*
209 ** Wait for some i/o to finish on one or more more poll descriptors.
210 */
PR_Poll(PRPollDesc * pds,PRIntn npds,PRIntervalTime timeout)211 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
212 {
213 #if defined(_WIN64) && defined(WIN95)
214     // For each iteration check if TFO overlapped IOs are down.
215     CheckOverlappedPendingSocketsAreDone();
216 #endif
217 
218     return(_PR_MD_PR_POLL(pds, npds, timeout));
219 }
220 
221 /*
222 ** Set the inheritance attribute of a file descriptor.
223 */
PR_SetFDInheritable(PRFileDesc * fd,PRBool inheritable)224 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
225     PRFileDesc *fd,
226     PRBool inheritable)
227 {
228 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2)
229     /*
230      * Only a non-layered, NSPR file descriptor can be inherited
231      * by a child process.
232      */
233     if (fd->identity != PR_NSPR_IO_LAYER) {
234         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
235         return PR_FAILURE;
236     }
237     if (fd->secret->inheritable != inheritable) {
238         if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
239             return PR_FAILURE;
240         }
241         fd->secret->inheritable = inheritable;
242     }
243     return PR_SUCCESS;
244 #else
245     PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
246     return PR_FAILURE;
247 #endif
248 }
249 
250 /*
251 ** This function only has a useful implementation in the debug build of
252 ** the pthreads version.
253 */
PT_FPrintStats(PRFileDesc * debug_out,const char * msg)254 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
255 {
256     /* do nothing */
257 }  /* PT_FPrintStats */
258