]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/unix++.h
   2  * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved. 
   4  * The contents of this file constitute Original Code as defined in and are 
   5  * subject to the Apple Public Source License Version 1.2 (the 'License'). 
   6  * You may not use this file except in compliance with the License. Please obtain 
   7  * a copy of the License at http://www.apple.com/publicsource and read it before 
  10  * This Original Code and all software distributed under the License are 
  11  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS 
  12  * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT 
  13  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  14  * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the 
  15  * specific language governing rights and limitations under the License. 
  20 // unix++ - C++ layer for basic UNIX facilities 
  22 #ifndef _H_UNIXPLUSPLUS 
  23 #define _H_UNIXPLUSPLUS 
  25 #include <Security/utilities.h> 
  26 #include <Security/timeflow.h> 
  27 #include <sys/types.h> 
  28 #include <sys/ioctl.h> 
  40 namespace UnixPlusPlus 
{ 
  44 // Check system call return and throw on error 
  46 inline void checkError(int result
) 
  54 // A UNIX standard 'struct iovec' wrapped 
  56 class IOVec 
: public iovec 
{ 
  59         IOVec(const void *data
, size_t length
)          { set(data
, length
); } 
  60         IOVec(void *data
, size_t length
)                        { set(data
, length
); } 
  62         void set(const void *data
, size_t length
) 
  63         { iov_base 
= reinterpret_cast<char *>(const_cast<void *>(data
)); iov_len 
= length
; } 
  66         void *data() const                      { return iov_base
; } 
  67         size_t length() const           { return iov_len
; } 
  72 // Generic file descriptors 
  76     static const int invalidFd 
= -1; 
  78     void setFd(int fd
)                                  { mFd 
= fd
; mAtEnd 
= false; } 
  79     void checkSetFd(int fd
)                             { checkError(fd
); mFd 
= fd
; mAtEnd 
= false; } 
  82     FileDesc() : mFd(invalidFd
), mAtEnd(false) { } 
  83     FileDesc(int fd
) : mFd(fd
), mAtEnd(false) { } 
  85     // implicit file system open() construction 
  86     FileDesc(const char *path
, int flag 
= O_RDONLY
, mode_t mode 
= 0666) : mFd(invalidFd
) 
  87     { open(path
, flag
, mode
); } 
  88         FileDesc(const std::string 
&path
, int flag 
= O_RDONLY
, mode_t mode 
= 0666) : mFd(invalidFd
) 
  89         { open(path
.c_str(), flag
, mode
); } 
  92     FileDesc 
&operator = (int fd
)               { mFd 
= fd
; mAtEnd 
= false; return *this; } 
  93     FileDesc 
&operator = (const FileDesc 
&fd
) { mFd 
= fd
.mFd
; mAtEnd 
= fd
.mAtEnd
; return *this; } 
  95     bool isOpen() const                 { return mFd 
!= invalidFd
; } 
  96     operator bool() const               { return isOpen(); } 
  97     int fd() const                              { return mFd
; } 
  98     operator int() const                { return fd(); } 
 100     void clear()                                { mFd 
= invalidFd
; } 
 101     void close();                               // close and clear 
 103     void open(const char *path
, int flag 
= O_RDONLY
, mode_t mode 
= 0666); 
 105     // basic I/O: this defines the "Filedescoid" pseudo-type 
 106     size_t read(void *addr
, size_t length
); 
 107     size_t write(const void *addr
, size_t length
); 
 108     bool atEnd() const                  { return mAtEnd
; }      // valid after zero-length read only 
 110     // more convenient I/O 
 111     template <class T
> size_t read(T 
&obj
) { return read(&obj
, sizeof(obj
)); } 
 112     template <class T
> size_t write(const T 
&obj
) { return write(&obj
, sizeof(obj
)); } 
 115     off_t 
seek(off_t position
, int whence 
= SEEK_SET
); 
 118     void *mmap(int prot 
= PROT_READ
, size_t length 
= 0, int flags 
= MAP_FILE
,  
 119         off_t offset 
= 0, void *addr 
= NULL
); 
 122     int fcntl(int cmd
, int arg 
= 0) const; 
 123     int fcntl(int cmd
, void *arg
) const; 
 125     void flags(int flags
) const; 
 126     void setFlag(int flag
, bool on 
= true) const; 
 127     void clearFlag(int flag
) const      { setFlag(flag
, false); } 
 129     int openMode() const        { return flags() & O_ACCMODE
; } 
 130     bool isWritable() const     { return openMode() != O_RDONLY
; } 
 131     bool isReadable() const     { return openMode() != O_WRONLY
; } 
 134     int ioctl(int cmd
, void *arg
) const; 
 135     template <class Arg
> Arg 
iocget(int cmd
) const  
 136         { Arg arg
; ioctl(cmd
, &arg
); return arg
; } 
 137     template <class Arg
> void iocget(int cmd
, Arg 
&arg
) const 
 138         { ioctl(cmd
, &arg
); } 
 139     template <class Arg
> void iocset(int cmd
, const Arg 
&arg
) 
 140         { ioctl(cmd
, const_cast<Arg 
*>(&arg
)); } 
 142     // stat-related utilities. @@@ should cache?? 
 143     typedef struct stat UnixStat
; 
 144     void fstat(UnixStat 
&st
) const; 
 145     size_t fileSize() const; 
 147     // stdio interactions 
 148     FILE *fdopen(const char *mode 
= NULL
);      // fdopen(3) 
 151     int mFd
;                            // UNIX file descriptor 
 154     bool mAtEnd
;                        // end-of-data indicator (after zero read) 
 159 // A (plain) FileDesc that auto-closes 
 161 class AutoFileDesc 
: public FileDesc 
{ 
 164     AutoFileDesc(int fd
) : FileDesc(fd
) { } 
 166     AutoFileDesc(const char *path
, int flag 
= O_RDONLY
, mode_t mode 
= 0666) 
 167         : FileDesc(path
, flag
, mode
) { } 
 168         AutoFileDesc(const std::string 
&path
, int flag 
= O_RDONLY
, mode_t mode 
= 0666) 
 169                 : FileDesc(path
, flag
, mode
) { } 
 171     ~AutoFileDesc()             { close(); } 
 180         SigSet() { sigemptyset(&mValue
); } 
 181         SigSet(const sigset_t 
&s
) : mValue(s
) { } 
 183         SigSet 
&operator += (int sig
) 
 184                 { sigaddset(&mValue
, sig
); return *this; } 
 185         SigSet 
&operator -= (int sig
) 
 186                 { sigdelset(&mValue
, sig
); return *this; } 
 188         bool contains(int sig
) 
 189                 { return sigismember(&mValue
, sig
); } 
 191         sigset_t 
&value()                       { return mValue
; } 
 192         operator sigset_t () const      { return mValue
; } 
 198 SigSet 
sigMask(SigSet set
, int how 
= SIG_SETMASK
); 
 202 // A ForkMonitor determines whether the current thread is a (fork) child of 
 203 // the thread that last checked it. Essentially, it checks for pid changes. 
 205 class StaticForkMonitor 
{ 
 207         bool operator () () const 
 212                 } else if (getpid() != mLastPid
) { 
 220         mutable pid_t mLastPid
; 
 223 class ForkMonitor 
: public StaticForkMonitor 
{ 
 225         ForkMonitor()           { mLastPid 
= getpid(); } 
 229 }       // end namespace UnixPlusPlus 
 230 }       // end namespace Security 
 233 #endif //_H_UNIXPLUSPLUS