nsprpub/pr/src/io/prio.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/io/prio.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,180 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "primpl.h"
    1.10 +
    1.11 +#include <string.h> /* for memset() */
    1.12 +
    1.13 +
    1.14 +/************************************************************************/
    1.15 +
    1.16 +PRLock *_pr_flock_lock;
    1.17 +PRCondVar *_pr_flock_cv;
    1.18 +
    1.19 +#ifdef WINCE
    1.20 +/*
    1.21 + * There are no stdin, stdout, stderr in Windows CE.  INVALID_HANDLE_VALUE
    1.22 + * should cause all I/O functions on the handle to fail.
    1.23 + */
    1.24 +#define STD_INPUT_HANDLE  ((DWORD)-10)
    1.25 +#define STD_OUTPUT_HANDLE ((DWORD)-11)
    1.26 +#define STD_ERROR_HANDLE  ((DWORD)-12)
    1.27 +
    1.28 +static HANDLE GetStdHandle(DWORD nStdHandle)
    1.29 +{
    1.30 +    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    1.31 +    return INVALID_HANDLE_VALUE;
    1.32 +}
    1.33 +#endif
    1.34 +
    1.35 +void _PR_InitIO(void)
    1.36 +{
    1.37 +    const PRIOMethods *methods = PR_GetFileMethods();
    1.38 +
    1.39 +    _PR_InitFdCache();
    1.40 +
    1.41 +    _pr_flock_lock = PR_NewLock();
    1.42 +    _pr_flock_cv = PR_NewCondVar(_pr_flock_lock);
    1.43 +
    1.44 +#ifdef WIN32
    1.45 +    _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE),
    1.46 +            methods);
    1.47 +    _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE),
    1.48 +            methods);
    1.49 +    _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE),
    1.50 +            methods);
    1.51 +#ifdef WINNT
    1.52 +    _pr_stdin->secret->md.sync_file_io = PR_TRUE;
    1.53 +    _pr_stdout->secret->md.sync_file_io = PR_TRUE;
    1.54 +    _pr_stderr->secret->md.sync_file_io = PR_TRUE;
    1.55 +#endif
    1.56 +#else
    1.57 +    _pr_stdin = PR_AllocFileDesc(0, methods);
    1.58 +    _pr_stdout = PR_AllocFileDesc(1, methods);
    1.59 +    _pr_stderr = PR_AllocFileDesc(2, methods);
    1.60 +#endif
    1.61 +    _PR_MD_INIT_FD_INHERITABLE(_pr_stdin, PR_TRUE);
    1.62 +    _PR_MD_INIT_FD_INHERITABLE(_pr_stdout, PR_TRUE);
    1.63 +    _PR_MD_INIT_FD_INHERITABLE(_pr_stderr, PR_TRUE);
    1.64 +
    1.65 +    _PR_MD_INIT_IO();
    1.66 +}
    1.67 +
    1.68 +void _PR_CleanupIO(void)
    1.69 +{
    1.70 +    PR_FreeFileDesc(_pr_stdin);
    1.71 +    _pr_stdin = NULL;
    1.72 +    PR_FreeFileDesc(_pr_stdout);
    1.73 +    _pr_stdout = NULL;
    1.74 +    PR_FreeFileDesc(_pr_stderr);
    1.75 +    _pr_stderr = NULL;
    1.76 +
    1.77 +    if (_pr_flock_cv) {
    1.78 +        PR_DestroyCondVar(_pr_flock_cv);
    1.79 +        _pr_flock_cv = NULL;
    1.80 +    }
    1.81 +    if (_pr_flock_lock) {
    1.82 +        PR_DestroyLock(_pr_flock_lock);
    1.83 +        _pr_flock_lock = NULL;
    1.84 +    }
    1.85 +
    1.86 +    _PR_CleanupFdCache();
    1.87 +}
    1.88 +
    1.89 +PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
    1.90 +{
    1.91 +    PRFileDesc *result = NULL;
    1.92 +    PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
    1.93 +
    1.94 +    if (!_pr_initialized) _PR_ImplicitInitialization();
    1.95 +    
    1.96 +    switch (osfd)
    1.97 +    {
    1.98 +        case PR_StandardInput: result = _pr_stdin; break;
    1.99 +        case PR_StandardOutput: result = _pr_stdout; break;
   1.100 +        case PR_StandardError: result = _pr_stderr; break;
   1.101 +        default:
   1.102 +            (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
   1.103 +    }
   1.104 +    return result;
   1.105 +}
   1.106 +
   1.107 +PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
   1.108 +    PROsfd osfd, const PRIOMethods *methods)
   1.109 +{
   1.110 +    PRFileDesc *fd;
   1.111 +
   1.112 +#ifdef XP_UNIX
   1.113 +	/*
   1.114 +	 * Assert that the file descriptor is small enough to fit in the
   1.115 +	 * fd_set passed to select
   1.116 +	 */
   1.117 +	PR_ASSERT(osfd < FD_SETSIZE);
   1.118 +#endif
   1.119 +    fd = _PR_Getfd();
   1.120 +    if (fd) {
   1.121 +        /* Initialize the members of PRFileDesc and PRFilePrivate */
   1.122 +        fd->methods = methods;
   1.123 +        fd->secret->state = _PR_FILEDESC_OPEN;
   1.124 +	fd->secret->md.osfd = osfd;
   1.125 +        _PR_MD_INIT_FILEDESC(fd);
   1.126 +    } else {
   1.127 +	    PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
   1.128 +    }
   1.129 +
   1.130 +    return fd;
   1.131 +}
   1.132 +
   1.133 +PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
   1.134 +{
   1.135 +    PR_ASSERT(fd);
   1.136 +    _PR_Putfd(fd);
   1.137 +}
   1.138 +
   1.139 +/*
   1.140 +** Wait for some i/o to finish on one or more more poll descriptors.
   1.141 +*/
   1.142 +PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
   1.143 +{
   1.144 +	return(_PR_MD_PR_POLL(pds, npds, timeout));
   1.145 +}
   1.146 +
   1.147 +/*
   1.148 +** Set the inheritance attribute of a file descriptor.
   1.149 +*/
   1.150 +PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
   1.151 +    PRFileDesc *fd,
   1.152 +    PRBool inheritable)
   1.153 +{
   1.154 +#if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS)
   1.155 +    /*
   1.156 +     * Only a non-layered, NSPR file descriptor can be inherited
   1.157 +     * by a child process.
   1.158 +     */
   1.159 +    if (fd->identity != PR_NSPR_IO_LAYER) {
   1.160 +        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
   1.161 +        return PR_FAILURE;
   1.162 +    }
   1.163 +    if (fd->secret->inheritable != inheritable) {
   1.164 +        if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
   1.165 +            return PR_FAILURE;
   1.166 +        }
   1.167 +        fd->secret->inheritable = inheritable;
   1.168 +    }
   1.169 +    return PR_SUCCESS;
   1.170 +#else
   1.171 +    PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
   1.172 +    return PR_FAILURE;
   1.173 +#endif
   1.174 +}
   1.175 +
   1.176 +/*
   1.177 +** This function only has a useful implementation in the debug build of
   1.178 +** the pthreads version.
   1.179 +*/
   1.180 +PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
   1.181 +{
   1.182 +    /* do nothing */
   1.183 +}  /* PT_FPrintStats */

mercurial