Back to index

lightning-sunbird  0.9+nobinonly
prio.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "primpl.h"
00039 
00040 #include <string.h> /* for memset() */
00041 
00042 
00043 /************************************************************************/
00044 
00045 PRLock *_pr_flock_lock;
00046 PRCondVar *_pr_flock_cv;
00047 
00048 void _PR_InitIO(void)
00049 {
00050     const PRIOMethods *methods = PR_GetFileMethods();
00051 
00052     _PR_InitFdCache();
00053 
00054     _pr_flock_lock = PR_NewLock();
00055     _pr_flock_cv = PR_NewCondVar(_pr_flock_lock);
00056 
00057 #ifdef WIN32
00058     _pr_stdin = PR_AllocFileDesc((PRInt32)GetStdHandle(STD_INPUT_HANDLE),
00059             methods);
00060     _pr_stdout = PR_AllocFileDesc((PRInt32)GetStdHandle(STD_OUTPUT_HANDLE),
00061             methods);
00062     _pr_stderr = PR_AllocFileDesc((PRInt32)GetStdHandle(STD_ERROR_HANDLE),
00063             methods);
00064 #ifdef WINNT
00065     _pr_stdin->secret->md.sync_file_io = PR_TRUE;
00066     _pr_stdout->secret->md.sync_file_io = PR_TRUE;
00067     _pr_stderr->secret->md.sync_file_io = PR_TRUE;
00068 #endif
00069 #else
00070     _pr_stdin = PR_AllocFileDesc(0, methods);
00071     _pr_stdout = PR_AllocFileDesc(1, methods);
00072     _pr_stderr = PR_AllocFileDesc(2, methods);
00073 #endif
00074     _PR_MD_INIT_FD_INHERITABLE(_pr_stdin, PR_TRUE);
00075     _PR_MD_INIT_FD_INHERITABLE(_pr_stdout, PR_TRUE);
00076     _PR_MD_INIT_FD_INHERITABLE(_pr_stderr, PR_TRUE);
00077 
00078     _PR_MD_INIT_IO();
00079 }
00080 
00081 void _PR_CleanupIO(void)
00082 {
00083     PR_FreeFileDesc(_pr_stdin);
00084     _pr_stdin = NULL;
00085     PR_FreeFileDesc(_pr_stdout);
00086     _pr_stdout = NULL;
00087     PR_FreeFileDesc(_pr_stderr);
00088     _pr_stderr = NULL;
00089 
00090     if (_pr_flock_cv) {
00091         PR_DestroyCondVar(_pr_flock_cv);
00092         _pr_flock_cv = NULL;
00093     }
00094     if (_pr_flock_lock) {
00095         PR_DestroyLock(_pr_flock_lock);
00096         _pr_flock_lock = NULL;
00097     }
00098 
00099     _PR_CleanupFdCache();
00100 }
00101 
00102 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
00103 {
00104     PRFileDesc *result = NULL;
00105     PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
00106 
00107     if (!_pr_initialized) _PR_ImplicitInitialization();
00108     
00109     switch (osfd)
00110     {
00111         case PR_StandardInput: result = _pr_stdin; break;
00112         case PR_StandardOutput: result = _pr_stdout; break;
00113         case PR_StandardError: result = _pr_stderr; break;
00114         default:
00115             (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
00116     }
00117     return result;
00118 }
00119 
00120 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
00121     PRInt32 osfd, const PRIOMethods *methods)
00122 {
00123     PRFileDesc *fd;
00124 
00125 #ifdef XP_UNIX
00126        /*
00127         * Assert that the file descriptor is small enough to fit in the
00128         * fd_set passed to select
00129         */
00130        PR_ASSERT(osfd < FD_SETSIZE);
00131 #endif
00132     fd = _PR_Getfd();
00133     if (fd) {
00134         /* Initialize the members of PRFileDesc and PRFilePrivate */
00135         fd->methods = methods;
00136         fd->secret->state = _PR_FILEDESC_OPEN;
00137        fd->secret->md.osfd = osfd;
00138         _PR_MD_INIT_FILEDESC(fd);
00139     } else {
00140            PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00141     }
00142 
00143     return fd;
00144 }
00145 
00146 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
00147 {
00148     PR_ASSERT(fd);
00149 #ifdef XP_MAC
00150        _PR_MD_FREE_FILEDESC(fd);
00151 #endif
00152     _PR_Putfd(fd);
00153 }
00154 
00155 /*
00156 ** Wait for some i/o to finish on one or more more poll descriptors.
00157 */
00158 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
00159 {
00160        return(_PR_MD_PR_POLL(pds, npds, timeout));
00161 }
00162 
00163 /*
00164 ** Set the inheritance attribute of a file descriptor.
00165 */
00166 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
00167     PRFileDesc *fd,
00168     PRBool inheritable)
00169 {
00170 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS)
00171     /*
00172      * Only a non-layered, NSPR file descriptor can be inherited
00173      * by a child process.
00174      */
00175     if (fd->identity != PR_NSPR_IO_LAYER) {
00176         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
00177         return PR_FAILURE;
00178     }
00179     if (fd->secret->inheritable != inheritable) {
00180         if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
00181             return PR_FAILURE;
00182         }
00183         fd->secret->inheritable = inheritable;
00184     }
00185     return PR_SUCCESS;
00186 #else
00187 #ifdef XP_MAC
00188 #pragma unused (fd, inheritable)
00189 #endif
00190     PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
00191     return PR_FAILURE;
00192 #endif
00193 }
00194 
00195 /*
00196 ** This function only has a useful implementation in the debug build of
00197 ** the pthreads version.
00198 */
00199 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
00200 {
00201     /* do nothing */
00202 }  /* PT_FPrintStats */