nsprpub/pr/src/io/prio.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     6 #include "primpl.h"
     8 #include <string.h> /* for memset() */
    11 /************************************************************************/
    13 PRLock *_pr_flock_lock;
    14 PRCondVar *_pr_flock_cv;
    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)
    25 static HANDLE GetStdHandle(DWORD nStdHandle)
    26 {
    27     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    28     return INVALID_HANDLE_VALUE;
    29 }
    30 #endif
    32 void _PR_InitIO(void)
    33 {
    34     const PRIOMethods *methods = PR_GetFileMethods();
    36     _PR_InitFdCache();
    38     _pr_flock_lock = PR_NewLock();
    39     _pr_flock_cv = PR_NewCondVar(_pr_flock_lock);
    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);
    62     _PR_MD_INIT_IO();
    63 }
    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;
    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     }
    83     _PR_CleanupFdCache();
    84 }
    86 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
    87 {
    88     PRFileDesc *result = NULL;
    89     PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
    91     if (!_pr_initialized) _PR_ImplicitInitialization();
    93     switch (osfd)
    94     {
    95         case PR_StandardInput: result = _pr_stdin; break;
    96         case PR_StandardOutput: result = _pr_stdout; break;
    97         case PR_StandardError: result = _pr_stderr; break;
    98         default:
    99             (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
   100     }
   101     return result;
   102 }
   104 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
   105     PROsfd osfd, const PRIOMethods *methods)
   106 {
   107     PRFileDesc *fd;
   109 #ifdef XP_UNIX
   110 	/*
   111 	 * Assert that the file descriptor is small enough to fit in the
   112 	 * fd_set passed to select
   113 	 */
   114 	PR_ASSERT(osfd < FD_SETSIZE);
   115 #endif
   116     fd = _PR_Getfd();
   117     if (fd) {
   118         /* Initialize the members of PRFileDesc and PRFilePrivate */
   119         fd->methods = methods;
   120         fd->secret->state = _PR_FILEDESC_OPEN;
   121 	fd->secret->md.osfd = osfd;
   122         _PR_MD_INIT_FILEDESC(fd);
   123     } else {
   124 	    PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
   125     }
   127     return fd;
   128 }
   130 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
   131 {
   132     PR_ASSERT(fd);
   133     _PR_Putfd(fd);
   134 }
   136 /*
   137 ** Wait for some i/o to finish on one or more more poll descriptors.
   138 */
   139 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
   140 {
   141 	return(_PR_MD_PR_POLL(pds, npds, timeout));
   142 }
   144 /*
   145 ** Set the inheritance attribute of a file descriptor.
   146 */
   147 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
   148     PRFileDesc *fd,
   149     PRBool inheritable)
   150 {
   151 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS)
   152     /*
   153      * Only a non-layered, NSPR file descriptor can be inherited
   154      * by a child process.
   155      */
   156     if (fd->identity != PR_NSPR_IO_LAYER) {
   157         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
   158         return PR_FAILURE;
   159     }
   160     if (fd->secret->inheritable != inheritable) {
   161         if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
   162             return PR_FAILURE;
   163         }
   164         fd->secret->inheritable = inheritable;
   165     }
   166     return PR_SUCCESS;
   167 #else
   168     PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
   169     return PR_FAILURE;
   170 #endif
   171 }
   173 /*
   174 ** This function only has a useful implementation in the debug build of
   175 ** the pthreads version.
   176 */
   177 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
   178 {
   179     /* do nothing */
   180 }  /* PT_FPrintStats */

mercurial