Main Page   Compound List   File List   Compound Members   File Members  

sysdep.cc File Reference

#include "copyright.h"
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/un.h>
#include <sys/mman.h>
#include "interrupt.h"
#include "system.h"

Go to the source code of this file.

Functions

int creat (const char *name, unsigned short mode)
int open (const char *name, int flags,...)
int select (int numBits, void *readFds, void *writeFds, void *exceptFds, struct timeval *timeout)
int unlink (char *name)
int read (int filedes, char *buf, int numBytes)
int write (int filedes, char *buf, int numBytes)
int lseek (int filedes, int offset, int whence)
int tell (int filedes)
int close (int filedes)
void srand (unsigned seed)
int rand (void)
unsigned sleep (unsigned)
void abort ()
void exit ()
int mprotect (char *addr, int len, int prot)
int socket (int, int, int)
int bind (int, const void *, int)
int getpagesize ()
bool PollFile (int fd)
int OpenForWrite (char *name)
int OpenForReadWrite (char *name, bool crashOnError)
void Read (int fd, char *buffer, int nBytes)
int ReadPartial (int fd, char *buffer, int nBytes)
void WriteFile (int fd, char *buffer, int nBytes)
void Lseek (int fd, int offset, int whence)
int Tell (int fd)
void Close (int fd)
bool Unlink (char *name)
int OpenSocket ()
void CloseSocket (int sockID)
void InitSocketName (struct sockaddr_un *uname, char *name)
void AssignNameToSocket (char *socketName, int sockID)
void DeAssignNameToSocket (char *socketName)
bool PollSocket (int sockID)
void ReadFromSocket (int sockID, char *buffer, int packetSize)
void SendToSocket (int sockID, char *buffer, int packetSize, char *toName)
void CallOnUserAbort (VoidNoArgFunctionPtr func)
void Delay (int seconds)
void Abort ()
void Exit (int exitCode)
void RandomInit (unsigned seed)
int Random ()
char * AllocBoundedArray (int size)
void DeallocBoundedArray (char *ptr, int size)


Function Documentation

void Abort  
 

Definition at line 430 of file sysdep.cc.

References abort.

00431 {
00432     abort();
00433 }

void abort  
 

Referenced by Abort.

char* AllocBoundedArray int    size
 

Definition at line 482 of file sysdep.cc.

References getpagesize, and mprotect.

00483 {
00484     int pgSize = getpagesize();
00485     char *ptr = new char[pgSize * 2 + size];
00486 
00487     mprotect(ptr, pgSize, 0);
00488     mprotect(ptr + pgSize + size, pgSize, 0);
00489     return ptr + pgSize;
00490 }

void AssignNameToSocket char *    socketName,
int    sockID
 

Definition at line 319 of file sysdep.cc.

References ASSERT, bind, DEBUG, InitSocketName, and unlink.

00320 {
00321     struct sockaddr_un uName;
00322     int retVal;
00323 
00324     (void) unlink(socketName);    // in case it's still around from last time
00325 
00326     InitSocketName(&uName, socketName);
00327     retVal = bind(sockID, (struct sockaddr *) &uName, sizeof(uName));
00328     ASSERT(retVal >= 0);
00329     DEBUG('n', "Created socket %s\n", socketName);
00330 }

int bind int   ,
const void *   ,
int   
 

Referenced by AssignNameToSocket.

void CallOnUserAbort VoidNoArgFunctionPtr    func
 

Definition at line 406 of file sysdep.cc.

References VoidFunctionPtr, and VoidNoArgFunctionPtr.

00407 {
00408     (void)signal(SIGINT, (VoidFunctionPtr) func);
00409 }

void Close int    fd
 

Definition at line 254 of file sysdep.cc.

References ASSERT, and close.

00255 {
00256     int retVal = close(fd);
00257     ASSERT(retVal >= 0); 
00258 }

int close int    filedes
 

Referenced by Close, CloseSocket, and main.

void CloseSocket int    sockID
 

Definition at line 295 of file sysdep.cc.

References close.

00296 {
00297     (void) close(sockID);
00298 }

int creat const char *    name,
unsigned short    mode
 

void DeallocBoundedArray char *    ptr,
int    size
 

Definition at line 501 of file sysdep.cc.

References getpagesize, and mprotect.

00502 {
00503     int pgSize = getpagesize();
00504 
00505     mprotect(ptr - pgSize, pgSize, PROT_READ | PROT_WRITE | PROT_EXEC);
00506     mprotect(ptr + size, pgSize, PROT_READ | PROT_WRITE | PROT_EXEC);
00507     delete [] (ptr - pgSize);
00508 }

void DeAssignNameToSocket char *    socketName
 

Definition at line 337 of file sysdep.cc.

References unlink.

00338 {
00339     (void) unlink(socketName);
00340 }

void Delay int    seconds
 

Definition at line 419 of file sysdep.cc.

References sleep.

00420 {
00421     (void) sleep((unsigned) seconds);
00422 }

void Exit int    exitCode
 

Definition at line 441 of file sysdep.cc.

References exit.

00442 {
00443     exit(exitCode);
00444 }

void exit  
 

Referenced by Exit, main, Read, and Write.

int getpagesize  
 

Referenced by AllocBoundedArray, and DeallocBoundedArray.

void InitSocketName struct sockaddr_un *    uname,
char *    name
[static]
 

Definition at line 306 of file sysdep.cc.

References strcpy.

Referenced by AssignNameToSocket, and SendToSocket.

00307 {
00308     uname->sun_family = AF_UNIX;
00309     strcpy(uname->sun_path, name);
00310 }

void Lseek int    fd,
int    offset,
int    whence
 

Definition at line 226 of file sysdep.cc.

References ASSERT, and lseek.

00227 {
00228     int retVal = lseek(fd, offset, whence);
00229     ASSERT(retVal >= 0);
00230 }

int lseek int    filedes,
int    offset,
int    whence
 

Referenced by Lseek, main, system_trap, and Tell.

int mprotect char *    addr,
int    len,
int    prot
 

Referenced by AllocBoundedArray, and DeallocBoundedArray.

int open const char *    name,
int    flags,
...   
 

Referenced by main, OpenForReadWrite, and OpenForWrite.

int OpenForReadWrite char *    name,
bool    crashOnError
 

Definition at line 175 of file sysdep.cc.

References ASSERT, and open.

00176 {
00177     int fd = open(name, O_RDWR, 0);
00178 
00179     ASSERT(!crashOnError || fd >= 0);
00180     return fd;
00181 }

int OpenForWrite char *    name
 

Definition at line 158 of file sysdep.cc.

References ASSERT, and open.

00159 {
00160     int fd = open(name, O_RDWR|O_CREAT|O_TRUNC, 0666);
00161 
00162     ASSERT(fd >= 0); 
00163     return fd;
00164 }

int OpenSocket  
 

Definition at line 279 of file sysdep.cc.

References ASSERT, and socket.

Referenced by Network::Network.

00280 {
00281     int sockID;
00282     
00283     sockID = socket(AF_UNIX, SOCK_DGRAM, 0);
00284     ASSERT(sockID >= 0);
00285 
00286     return sockID;
00287 }

bool PollFile int    fd
 

Definition at line 124 of file sysdep.cc.

References ASSERT, FALSE, IdleMode, select, and TRUE.

00125 {
00126     int rfd = (1 << fd), wfd = 0, xfd = 0, retVal;
00127     struct timeval pollTime;
00128 
00129 // decide how long to wait if there are no characters on the file
00130     pollTime.tv_sec = 0;
00131     if (interrupt->getStatus() == IdleMode)
00132         pollTime.tv_usec = 20000;               // delay to let other nachos run
00133     else
00134         pollTime.tv_usec = 0;                   // no delay
00135 
00136 // poll file or socket
00137 #if   defined(HOST_i386) || defined(HOST_SVR4)
00138     retVal = select(32, (fd_set*)&rfd, (fd_set*)&wfd, (fd_set*)&xfd, &pollTime);
00139 #else
00140     retVal = select(32, &rfd, &wfd, &xfd, &pollTime);
00141 #endif
00142 
00143     ASSERT((retVal == 0) || (retVal == 1));
00144     if (retVal == 0)
00145         return FALSE;                           // no char waiting to be read
00146     return TRUE;
00147 }

bool PollSocket int    sockID
 

Definition at line 348 of file sysdep.cc.

References PollFile.

00349 {
00350     return PollFile(sockID);    // on UNIX, socket ID's are just file ID's
00351 }

int rand void   
 

Referenced by Random.

int Random  
 

Definition at line 464 of file sysdep.cc.

References rand.

Referenced by Network::Send, and Timer::TimeOfNextInterrupt.

00465 {
00466     return rand();
00467 }

void RandomInit unsigned    seed
 

Definition at line 453 of file sysdep.cc.

References srand.

00454 {
00455     srand(seed);
00456 }

void Read int    fd,
char *    buffer,
int    nBytes
 

Definition at line 189 of file sysdep.cc.

References ASSERT, and read.

00190 {
00191     int retVal = read(fd, buffer, nBytes);
00192     ASSERT(retVal == nBytes);
00193 }

int read int    filedes,
char *    buf,
int    numBytes
 

Referenced by Read, and ReadPartial.

void ReadFromSocket int    sockID,
char *    buffer,
int    packetSize
 

Definition at line 358 of file sysdep.cc.

References ASSERT.

00359 {
00360     int retVal;
00361     extern int errno;
00362     struct sockaddr_un uName;
00363     int size = sizeof(uName);
00364    
00365     retVal = recvfrom(sockID, buffer, packetSize, 0,
00366                                    (struct sockaddr *) &uName, &size);
00367 
00368     if (retVal != packetSize) {
00369         perror("in recvfrom");
00370         printf("called: %x, got back %d, %d\n", (unsigned int) buffer, 
00371                retVal, errno);
00372     }
00373     ASSERT(retVal == packetSize);
00374 }

int ReadPartial int    fd,
char *    buffer,
int    nBytes
 

Definition at line 202 of file sysdep.cc.

References read.

00203 {
00204     return read(fd, buffer, nBytes);
00205 }

int select int    numBits,
void *    readFds,
void *    writeFds,
void *    exceptFds,
struct timeval *    timeout
 

Referenced by PollFile.

void SendToSocket int    sockID,
char *    buffer,
int    packetSize,
char *    toName
 

Definition at line 382 of file sysdep.cc.

References ASSERT, and InitSocketName.

00383 {
00384     struct sockaddr_un uName;
00385     int retVal;
00386 
00387     InitSocketName(&uName, toName);
00388     retVal = sendto(sockID, buffer, packetSize, 0,
00389 #ifdef        HOST_SVR4
00390                     (sockaddr *) &uName, 
00391 #else
00392                     (char *) &uName, 
00393 #endif        /* HOST_SVR4 */
00394                     sizeof(uName));
00395     ASSERT(retVal == packetSize);
00396 }

unsigned sleep unsigned   
 

Referenced by Delay.

int socket int   ,
int   ,
int   
 

Referenced by OpenSocket.

void srand unsigned    seed
 

Referenced by RandomInit.

int Tell int    fd
 

Definition at line 238 of file sysdep.cc.

References lseek, and tell.

00239 {
00240 #ifdef HOST_i386
00241     return lseek(fd,0,SEEK_CUR); // 386BSD doesn't have the tell() system call
00242 #else
00243     return tell(fd);
00244 #endif
00245 }

int tell int    filedes
 

Referenced by Tell.

bool Unlink char *    name
 

Definition at line 266 of file sysdep.cc.

References unlink.

00267 {
00268     return unlink(name);
00269 }

int unlink char *    name
 

Referenced by AssignNameToSocket, DeAssignNameToSocket, main, Read, Unlink, and Write.

int write int    filedes,
char *    buf,
int    numBytes
 

Referenced by Write, and WriteFile.

void WriteFile int    fd,
char *    buffer,
int    nBytes
 

Definition at line 214 of file sysdep.cc.

References ASSERT, and write.

00215 {
00216     int retVal = write(fd, buffer, nBytes);
00217     ASSERT(retVal == nBytes);
00218 }


Generated on Mon Feb 10 09:54:51 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees