## modules/up/src/Core/sys/FileSet.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. FileSet

       1 //
       2 // $Id: FileSet.cc,v 1.1.1.1 2000/03/10 16:32:20 engin Exp $
       3 //
       4 // system.cc
       5 // Author: Ramesh Govindan <govindan@isi.edu>
       6 //
       7 // Abstracted OS facilities for file system access and
       8 // for communication primitives. This file contains implementations of:
       9 //      - network addresses (Address class)
      10 //      - OS file descriptors and descriptor sets
      11 //      - a common time representation
      12 //
      13
      14 #ifdef HAVE_CONFIG_H
      15 #include <config.h>
      16 #endif
      17
      18 #include <cstdio>
      19 #include <cstdlib>
      20 #include <cerrno>
      21
      22 extern "C" {
      23 #if HAVE_UNISTD_H
      24 #include <unistd.h>
      25 #endif // HAVE_UNISTD_H
      26
      27 #include <sys/types.h>
      28 #include <sys/time.h>
      29 #include <sys/resource.h>
      30 }
      31 #include "sys/FileSet.hh"
      32 #include "util/Types.hh"
      33 #include "util/List.hh"
      34 #include "util/Trail.hh"
      35
      36
      37 //#include "sched/Dispatcher.hh"
      38
      39 extern "C" {
      40 #ifndef STDC_HEADERS
      41 extern int getrlimit(...);
      42 extern int select(...);
      43 #endif
      44 }
      45
      46 // File local variables
      47 static TraceCode traceFileSet("file_set");
      48
      49 // The FileSet construct hides the internal details of
      50 // UNIX descriptor sets.
      51
      52 FileSet::FileSet()
         /* [<][>][^][v][top][bottom][index][help] */
      53         : List<File>()
      54 {
      55     TRACE(traceFileSet,
      56           "creating new file set");
      57
      58     FD_ZERO(&readDescriptors);
      59     FD_ZERO(&writeDescriptors);
      60
      61 #ifdef RLIMIT_NOFILE
      62     struct rlimit rl;
      63
      64     (void) getrlimit(RLIMIT_NOFILE,
      65                      &rl);
      66     fdWidth = rl.rlim_cur;    // Current limit, assume we don't set it
      67 #else // RLIMIT_NOFILE
      68 #ifndef NOFILE
      69 #ifdef  _NFILE
      70 #define NOFILE  _NFILE
      71 #else   // _NFILE
      72 #ifdef  OPEN_MAX
      73 #define NOFILE  OPEN_MAX
      74 #else   // OPEN_MAX
      75 #define NOFILE  20
      76 #endif  // OPEN_MAX
      77 #endif  // _NFILE
      78 #endif  // NOFILE
      79     fdWidth = NOFILE;
      80 #endif // RLIMIT_NOFILE
      81 }
      82
      83 FileSet::~FileSet()
      84 {
      85     // Empty
      86 }
      87
      88 void
      89 FileSet::inset(File* file)
      90 {
      91     TRACE(traceFileSet,
      92           "adding new descriptor %d to file set",
      93           file->descriptor());
      94
      95     if (file->mode() != FileModeReadOnly) {
      96         if (file->has_wh()) {
      97             FD_SET(file->descriptor(), &writeDescriptors);
      98         }
      99     }
     100     if (file->has_rh()) {
     101         FD_SET(file->descriptor(), &readDescriptors);
     102     }
     103     append(file);
     104 }
     105
     106 void
     107 FileSet::outset(File* file)
     108 {
     109     TRACE(traceFileSet,
     110           "removing descriptor %d from file set",
     111           file->descriptor());
     112
     113     remove(file);
     114     if (file->mode() != FileModeReadOnly) {
     115         FD_CLR(file->descriptor(),
     116                &writeDescriptors);
     117     }
     118     FD_CLR(file->descriptor(),
     119            &readDescriptors);
     120     return;
     121 }
     122
     123 Boolean
     124 FileSet::issetRead(File* file)
     125 {
     126     TRACE(traceFileSet,
     127           "testing if descriptor %d is readable",
     128           file->descriptor());
     129
     130     return FD_ISSET(file->descriptor(),
     131                     &lastRead);
     132 }
     133
     134 Boolean
     135 FileSet::issetWrite(File* file)
     136 {
     137     TRACE(traceFileSet,
     138           "testing if descriptor %d is writable",
     139           file->descriptor());
     140
     141     return FD_ISSET(file->descriptor(),
     142                      &lastWrite);
     143 }
     144
     145 int
     146 FileSet::select(TimeShort& interval)
     147 {
     148     struct timeval tv;
     149     TimeLong intv;
     150     int retval;
     151
     152     TRACE(traceFileSet,
     153           "selecting on file set");
     154
     155     intv = interval.lengthen();
     156     intv.systemTime(&tv);
     157     lastRead = readDescriptors;
     158     lastWrite = writeDescriptors;
     159     retval = ::select(fdWidth,
     160                       &lastRead,
     161                       &lastWrite,
     162                       NULL,
     163                       &tv);
     164     if (retval < 0) {
     165         switch (errno) {
     166             case EINTR:
     167                 return FileOpSoftError;
     168             default:
     169                 return FileOpHardError;
     170         }
     171     }
     172     return retval;
     173 }
     174
     175 //
     176 //  Copyright (c) 1994 by the University of Southern California.
     177 //  All rights reserved.
     178 //
     179 //  Permission to use, copy, modify, and distribute this software and
     180 //  its documentation in source and binary forms for lawful
     181 //  non-commercial purposes and without fee is hereby granted, provided
     182 //  that the above copyright notice appear in all copies and that both
     183 //  the copyright notice and this permission notice appear in supporting
     184 //  documentation, and that any documentation, advertising materials,
     185 //  and other materials related to such distribution and use acknowledge
     186 //  that the software was developed by the University of Southern
     187 //  California and/or Information Sciences Institute.
     188 //  The name of the University of Southern California may not
     189 //  be used to endorse or promote products derived from this software
     190 //  without specific prior written permission.
     191 //
     192 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
     193 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
     194 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     195 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     196 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
     197 //  NON-INFRINGEMENT.
     198 //
     199 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
     200 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
     201 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
     202 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
     203 //
     204 //  Questions concerning this software should be directed to
     205 //  info-ra@isi.edu.
     206 //

/* [<][>][^][v][top][bottom][index][help] */