General System Call Issues - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

General System Call Issues

Description:

... a system call, type 'man syscall', where syscall is the name of the system call ... must have execute permission in directory to open file ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 13
Provided by: davidlo6
Category:

less

Transcript and Presenter's Notes

Title: General System Call Issues


1
General System Call Issues
  • Return values must be checked most often, a
    negative return value indicates an error
  • If system call parameters are pointers, you must
    make sure to pass a pointer
  • cant use reference parameters, because all
    system calls have a C interface.
  • so, if a system call requires a pointer to an
    integer, either pass x if x is an integer, or
    just y if y is an int

2
General System Call Issues (cont)
  • Passing incorrect parameters (like an int instead
    of a pointer) leads to seg fault
  • cant debug inside system call, so make sure to
    break in the debugger before the call, and check
    to see if your parameters are correct
  • Man pages
  • for info on a system call, type man syscall,
    where syscall is the name of the system call
    this will tell you what files to include, as well
    as give information on how to use the call
  • man pages are somewhat terse and sometimes hard
    to understand, but still are worth it to read
  • Bitwise or
  • this is the character, and is used to set
    multiple flags

3
General System Call Issues (cont)
  • Typedefs
  • you will see them all over the place
  • e.g., for the read system call, there is a
    typedef
  • typedef int ssize_t
  • creates a new type
  • to understand complex typedefs, put your hand
    over the typedef
  • e.g., typedef struct foo bar
  • means that if I now say bar x, that is the same
    as saying struct foo x

4
UNIX File I/O
  • File descriptors
  • Users way to reference files
  • 0 defined to be standard input, 1 standard
    output, and 2 standard error
  • When read/write a file, we use the file
    descriptor to determine the file

5
Open
  • include ltsys/types.hgt
  • include ltsys/stat.hgt
  • include ltfcntl.hgt
  • int open(const char pathname, int oflag)
  • Opens file pathname, with flags oflag

6
Open flags
  • O_RDONLY open for reading
  • O_WRONLY open for writing
  • O_RDWR open for reading and writing
  • O_APPEND write at end
  • O_CREAT create file if doesnt exist
  • See text for others
  • or flags together for both, i.e.
  • O_WRONLY O_APPEND

7
More on Open
  • If O_CREAT specified, need to specify a mode
    (described later)
  • Open returns a legitimate file descriptor on
    success, and -1 on error
  • e.g. error occurs if file does not exist, or is
    not permissible to open

8
Other file system calls
  • int close(int filedes)
  • closes file
  • int lseek(int filedes, off_t offset, int whence)
  • set file position of filedes to offset (or
    current position plus offset, depending on whence
  • ssize_t read (int fd, void buf, size_t nbytes)
  • read nbytes into memory pointed to by buf, from
    file fd returns number of bytes actually read
    (which can be less than the requested amount)
  • ssize_t write (int fd, const void buf, size_t
    nbytes)
  • same as read, except that data is written to file
    fd, from memory pointed to by buf

9
Other file system calls
  • int dup2(fd, fd2)
  • first, closes file fd2 if it is open
  • second, associates fd with what fd2 used to be
    associated with
  • e.g. dup2(fd, 1) will cause all input from
    standard input to instead be taken from fd
  • useful for implementing redirection in the shell
  • e.g. command gt outputfile

10
Other file system calls
  • int stat(const char filename, struct stat buf)
  • for a given filename, returns information about
    that file into buf
  • need to look at the man page to see what the
    fields of the stat struct are
  • Example
  • include ltsys/types.hgt
  • include ltsys/stat.hgt
  • main ( )
  • int err struct stat buf
  • err stat(foo, buf)
  • if (err ! 0)
  • cout ltlt Error invoking stat ltlt endl
  • exit(1)
  • cout ltlt Size of file is ltlt buf.st_size

11
File Permissions
  • Each file in UNIX has 9 bits for permissions
  • 3 for user, 3 for group, 3 for other
  • each group of three is RWX
  • read, write, execute (try ls l to see these)
  • Many rules (see text for all)
  • must have execute permission in directory to open
    file
  • read permission says if we can read, write if we
    can write
  • user bits checked if user owns file, group bits
    checked if not owner but in group, and other bits
    checked otherwise

12
Other file system calls
  • int chmod(char filename, mode_t mode)
  • for a given filename, changes its access
    permissions to mode
  • some flags for mode
  • S_IRUSR readable by user
  • S_IRWXU RWX by user
  • S_IRWXO RWX by other
  • or these flags together
Write a Comment
User Comments (0)
About PowerShow.com