1 contents 1. preface/introduction 2. standardization and implementation 3. file i/o 4. standard i/o...

35
1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control 9. Signals

Upload: toby-poole

Post on 21-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

1

Contents

1. Preface/Introduction

2. Standardization and Implementation

3. File I/O

4. Standard I/O Library

5. Files and Directories

6. System Data Files and Information

7. Environment of a Unix Process

8. Process Control

9. Signals

10.Inter-process Communication

Page 2: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

2

Files and Directories

Objectives

• Additional Features of the File System

• Properties of a File.

Three major functions that return file information:

#include <sys/types.h>

#include <sys/stat.h>

int stat(const char *pathname, struct stat *buf);

int fstat(int filedes, struct stat *buf);

int lstat(const char *pathname, struct stat *buf);

Page 3: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

3

Files and Directories

Differences on stat(), fstat(), lstat(). lstat() similar to stat(), except that, when file is a symlink, it returns info regarding the symbolic link itself, not the referenced file. File info is returned in buf defined as the following structure:

struct stat {

mode_t st_mode; /* type & mode */

ino_t st_ino; /* i-node number */

dev_t st_dev; /* device no (filesystem) */

dev_t st_rdev; /* device no for special file */

nlink_t st_nlink; /* # of links */

uid_t st_uid; gid_t st_gid;

off_t st_size; /* sizes in bytes */

time_t st_atime; /* last access time */

time_t st_mtime; /* last modification time */

time_t st_ctime; /* time for last status change */

long st_blksize; /* best I/O block size */

long st_blocks; /* number of 512-byte blocks allocated */

};

Page 4: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

4

File Types

Regular Files: text, binary, etc.

Directory Files: Only Kernel can update these files – { (filename, i-node pointer) }.

Character Special Files, e.g., tty, audio, etc.

Block Special Files, e.g., disks, etc.

FIFO – named pipes

Sockets

Symbolic Links – not POSIX.1 or SVR4

Page 5: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

5

Macro Type of File

S_ISREG() Regular file

S_ISDIR() Directory

S_ISCHR() Character special file

S_ISBLK() Block special file

S_ISFIFO() FIFO or PIPE

S_ISLNK() Symbolic link

S_ISSOCK() Socket

File types can be determined using following macros; argument of macro is st_mode (which encodes file type) member from the stat structure.

Fig. 4.1

Page 6: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

6

File Types

Program 4.1: takes command-line argument and prints the file type for each command-line argument.

Example of execution:

$ a.out /vmunix /etc /dev/ttya /dev/hd0a gives:

/vmunix: regular file

/etc: directory

/dev/ttya: character special device

/dev/hd0a: block special device

<sys/stat.h> contains macro definitions shown in fig 4.1 in the form

#define S_ISDIR(mode) (((mode)&0xF000) == 0x4000)

Page 7: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

7

#include <sys/types.h>#include <sys/stat.h>#include "ourhdr.h"int main(int argc, char *argv[]){

int i;struct stat buf;char *ptr;for (i = 1; i < argc; i++) { printf("%s: ", argv[i]);

if (lstat(argv[i], &buf) < 0) { err_ret(“lstat error"); continue;

}if (S_ISREG(buf.st_mode)) ptr = "regular";else if (S_ISDIR(buf.st_mode)) ptr = "directory";else if (S_ISCHR(buf.st_mode)) ptr = "character special";else if (S_ISBLK(buf.st_mode)) ptr = "block special";else if (S_ISFIFO(buf.st_mode)) ptr = "fifo";

#ifdef S_ISLNK /* only for BSD */else if (S_ISLNK(buf.st_mode)) ptr = "symbolic link";

#endif#ifdef S_ISSOCK /* only for BSD */

else if (S_ISSOCK(buf.st_mode)) ptr = "socket";#endif

else ptr = "** unknown mode **";printf("%s\n", ptr);

}exit(0);

}

Program 4.1

Page 8: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

8

Set-User-ID and Set-Group-ID

Every process has 6 or more IDs associated with it.

Real user ID

Real group ID who we really are

Effective user ID

Effective group ID

Supplementary group ID

Used for file access permission checks

Saved set-user-ID

Saved set-group-ID Saved by exec function

– Real user ID and real group ID are taken from user entry in passwd file when user logs in. No change during a login session.

– Effective IDs and supplementary IDs control user file access permissions.

– Saved IDs contain copies of effective ids when program is executed (through exec function). Used by setuid function.

Page 9: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

9

Set-User-ID and Set-Group-ID

- Normally effective IDs are equal to real IDs .- Every file has an owner (st_uid of stat structure) and a group owner (st_gid).- On program execution (from an executable file), effective IDs of the initiated process = real IDs. BUT!!!, we can set a special flag in the executable file mode word (st_mode) to force setting the the effective uid of the process to the OWNER of the executable file (the st_uid). The same can be done for group ID. These 2 bits in st_mode are called set-user-ID and set-group-ID bits.

Example: when owner of a program file is superuser, and set-user-ID bit is set, it implies that when this program file is running as process, it has ROOT privileges! regardless of the real user ID of the process. This facility is required for the command passwd that allows any user to change his/her password: this is the only way for the command passwd to modify the /etc/passwd file ON BEHALF of the user. (/etc/passwd file is normally writable only by superuser.)

Because a process running set-user-ID for other users assumes extra permissions for file access, it must be written very carefully!!!

set-user-ID and set-group-ID bits contained in the st_mode word can be tested through stat function using constants S_ISUID and S_ISGID.

Page 10: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

10

File Access Permissions

st_mode word encodes access permissions for files and directories as Permission Bits (fig. 4.2)

According to permissions, allowed operations are:

For a Directory

* X – pass through the dir (search bit). Example: to open file /usr/student/ali, we need execute permissions in /usr and in /usr/student.

* R – list of files under the directory (different from X!)

* W – update the dir, e.g., delete or create a file (both need also X bit).

For a File

* X – execute a file (which must be a regular file)

* R – can open file for reading. O_RDONLY or O_RDWR

* W – can open file for writing. O_WRONLY, O_RDWR, or O_TRUNC

Page 11: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

11

st_mode mask meaning

S_IRUSR

S_IWUSR

S_IXUSR

User ( i.e OWNER) read

User ( i.e OWNER) write

User ( i.e OWNER) execute

S_IRGRP

S_IWGRP

S_IXGRP

Group read

Group write

Group execute

S_IROTH

S_IWOTH

S_IXOTH

Other read

Other write

Other execute

Fig. 4.2

Permission bits

Page 12: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

12

Access Permissions & UID/GID

File Access Test: Tests performed by OS when process opens, creates or deletes file. Depend on the owners of the file (st_uid, st_gid), the effective IDs of the process (EUID, EGID) and the supplementary groups.

1. If the effective UID == 0 superuser! access allowed alatool!

2. If the effective UID == owner UID of the file

If appropriate access permissions, access allowed, otherwise denied

3. If the effective GID == owner GID of the file

If appropriate access permissions, access allowed, otherwise denied

4. Check appropriate access permissions for others same way.

These 4 steps are tried in sequence. (e.g. at step 2, only UID is important regarding user permissions and not group permissions)

Related Commands: chmod & umask

Page 13: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

13

Ownership of a New File

Upon file creation (using open or creat), no mention of values of UID of GID. Rules are:

UID of a file = the effective UID of the creating process

GID of a file – 2 options defined by POSIX:

1. GID of the file = the effective GID of the process

2. GID of the file = the GID of the directory in which the file is created.

Page 14: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

14

Function – access: file accessibility tests

Normally, accessibility tests are performed by OS on open operation, based on effective UID and GID.

Sometimes, need to test accessibility for a file based on real UID and GID (e.g. we are running with set-user-ID).

#include <unistd.h>

int access(const char *pathname, int mode);

• Check the real UID/GID!

• Mode is bitwise OR of any of the constants: R_OK, W_OK, X_OK, F_OK (file existence)

• Program 4.2

Page 15: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

15

#include <sys/types.h>#include <fcntl.h>#include "ourhdr.h"int main(int argc, char *argv[]){

if (argc != 2)err_quit("usage: a.out <pathname>");

if (access(argv[1], R_OK) < 0)err_ret("access error for %s", argv[1]);

elseprintf("read access OK\n");

if (open(argv[1], O_RDONLY) < 0)err_ret("open error for %s", argv[1]);

elseprintf("open for reading OK\n");

exit(0);}

Example of execution:$ a.out a.outread access OKopen for reading OK

PROGRAM 4.2

Page 16: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

16

Function – umask

Defines the file mode CREATION MASK. The permissions defined in the mask will be used as default permissions for newly created files. Related to file permissions.Function umask sets the file mode creation mask for the process and returns the previous value.

#include <sys/types.h>#include <sys/stat.h>mode_t umask(mode_t cmask);

cmask = bitwise-OR of any of file permissions (Figure 4.2). Any bits set in cmask are turned OFF in file mode.

The mask is defined on a per-process basis.

• Inherited from the parent!

Example: Program 4.3 creates 2 files: one with a umask of 0 and one with a umask that disables all group/other permission bits.

Remark: in many case, users do not deal with umask values ; it is set once by the shell’s startup file (default value), on log in, and never changed. Used only if we create files with non-default permissions.

Page 17: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

17

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include "ourhdr.h"int main(void){

umask(0);if (creat("foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |

S_IROTH | S_IWOTH) < 0)err_sys("creat error for foo");

umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);if (creat("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |

S_IROTH | S_IWOTH) < 0)err_sys("creat error for bar");

exit(0);}

PROGRAM 4.3

Example of execution:$ umask 02$ a.out $ ls –l foo bar-rw------- bar-rw-rw-rw- foo

Page 18: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

18

Function – chmod & fchmodChange file access permissions for an existing file.

#include <sys/types.h>

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);

int fchmod(int filedes, mode_t mode);

• fchmod() applies to an OPEN file.

• Callers must be a superuser or effective UID = file UID (file owner).

• Mode = bitwise-OR of constants defined in Fig 4.3.

Example given in Program 4.4. Modifies modes of file foo and bar created in Program 4.3.

Execution example:Before change$ ls –l foo bar-rw------- bar-rw-rw-rw- foo

After change$ ls –l foo bar-rw-r--r-- bar-rw-rwxrw- foo

Page 19: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

19

mode description

S_ISUID

S_ISGID

S_ISVTX

set-user-ID on execution

set-group-ID on execution

saved-text (sticky bit)

S_IRWXUS_IRUSR

S_IWUSR

S_IXUSR

R/W/E by user (owner)Read by user

Write

execute

S_IRWXGS_IRGRP

S_IWGRP

S_IXGRP

R/W/E by groupRead by group

Write

execute

S_IRWXOS_IROTH

S_IWOTH

S_IXOTH

R/W/E by others (world)Read by others

Write

execute

Fig. 4.3

Page 20: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

20

#include <sys/types.h>#include <sys/stat.h>#include "ourhdr.h"

int main(void){

struct stat statbuf;

/* turn on group-execute relative to current mode*/

if (stat("foo", &statbuf) < 0) err_sys("stat error for foo");if (chmod("foo", (statbuf.st_mode & S_IXGRP) < 0)

err_sys("chmod error for foo");

/* set absolute mode to "rw-r--r--" */

if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)err_sys("chmod error for bar");

exit(0);}

Program 4.4

Page 21: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

21

Function – chmod & fchmod – STICKY BIT

S_ISVTX– saved-text bit (also known as Sticky Bit). Text=machine instructions part of a program. Applies only to executable file

• Used to save a copy of an executable file in the swap area to speed up its execution next time: on 1rst process execution, a copy of the program is saved in the swap area when process terminates. Allows program to be loaded faster on next execution (swap area is a continuous space). Usually set for frequently used applications (text editor, compiler,…)

Only superusers can set it!

Page 22: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

22

Function – chown, fchown, lchown

#include <sys/types.h>

#include <unistd.h>

int chown(const char *pathname, uid_t owner, gid_t, grp);

int fchown(int filedes, uid_t owner, gid_t, grp);

Only owner of a file can change file ownership.

Returns 0 if OK and -1 on error.

Page 23: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

23

File Systems

A hierarchical arrangement of directories and files – starting in root /.

Typical physical organization of disks and file systems is as follows:

Page 24: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

24

File Systems

i-node: fixed length entries that contain most of file info. Most info in stat structure comes from i-node. Version 7: 64Bytes, 4.4 BSD:128B File type, access permission, file size, data blocks, link count (hard links) – see fig above with 2 directories pointing to same i-node. File can be deleted only when link count=0.

UNLINK a file does not mean always DELETE a file!!! link count is contained in st_nlink (in stat). LINK_MAX (POSIX.1) defines max number of links.

Page 25: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

25

Problems • Infinite loop in tracing a path name with symbolic links• Dangling pointers

File System: hard links and soft links

Page 26: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

26

File System: example of directory creation

Page 27: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

27

Functions – link, unlink

File can have multiple dir entries pointing to it.

#include <unistd.h>int link(const char *existingpath, const char *newpath);Creates a new dir entry newpath that references an existing file existingpath.

int unlink(const char *pathname);Removes an existing directory entry and decrements the link count of the file referenced by pathname.

•Atomic action for link creation and link count incrementation– hard link• POSIX.1 allows linking across file systems• Only superusers could create a link to a dir (to avoid loops!!!)

• On link operation, error if newpath exists• Unlink – We must have WX right for the residing dir of the link

• Remove the dir entry & delete the file if link count=0 and no other process has the file still open. • If pathname is a symbolic link, unlink references the symb. Link itself, not the file pointed to by the symb. link

Page 28: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

28

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include "ourhdr.h"intmain(void){

if (open("tempfile", O_RDWR) < 0) err_sys("open error");if (unlink("tempfile") < 0) err_sys("unlink error");printf("file unlinked\n");sleep(15);printf("done\n");exit(0);

}

Example below shows how a program uses UNLINK function to make sure a temporary file is DELETED in case of program crash.Program creates a file (using open), and then unlink it immediately. The file is NOT deleted, because it is still open. But, when process closesthe file (using close) or terminates (OS will automatically close the file),the file is deleted (link count=0, and file is not open)

Page 29: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

29

Functions – rename, remove#include <stdio.h>int remove(const char *pathname);int rename(const char *oldname, const char *newname);

• remove = rmdir if pathname is a dir. (ANSI C)• Rename – ANSI C

• File: both files, newname is removed first, WX permission for both residing directories• Directory: both dir, newname must be empty, newname could not contain oldname.

Page 30: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

30

Symbolic LinksSymbolic link is an indirect pointer to a file. Different from hard link which points directly to i-node

Goal: Extends scope and limitations of hard links: (a) file system boundary, (b) link to a dir.

When using a function, it is important to know if the function follows a symlink of not: if function follows symlink, pathname argument refers to the FILE pointed to by the link, otherwise it refers to the LINK itself.

Example of functions:chdir, mkdir, open: follow symlinkremove, rename, unlink do not follow symlink

Page 31: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

31

Example – loops in symlinks

$ mkdir foo$ touch foo/a create a file with length=0$ ln -s ../foo foo/testdir create a symlink$ ls –l foo -rw-rw-r-- a lrwxrwxrwx testdir ../foo

Effect of these operations is shown on figure: a function that follows the link will loop. Many functions would return an error in this case (errno=ELOOP).

unlink(testdir) would work perfectly because it does not follow links. This is how loop can be removed.

Symbolic Links: loop problem

Page 32: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

32

Symbolic Links: symlink, readlink

#include <unistd.h>/* Create a new symbolic link: */int symlink(const char *actualpath, const char *sympath);

/* read symbolic link itself into buf*/int readlink(const char *pathname, char *buf, int bufsize); • actualpath does not need to exist!

• actualpath and symbpath do not need to be in the same file system.

• readlink is an action consisting of open, read, and close of a symbolic link. Returns symlink as non-null terminated string.

Page 33: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

33

Functions – mkdir and rmdir

#include <sys/types.h>#include <sys/stat.h>int mkdir(const char *pathname, mode_t mode); creates a new empty directory. Access permissions mode modified according to umask. UID/GID set as defined in slide 13. #include <unistd.h>int rmdir(const char *pathname);

• An empty dir is deleted.• Condition: link count reaches zero, and no one still opens the dir.

Page 34: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

34

Functions – opendir, readdir, rewinddir, closedir

Directories can be read by anyone with proper access rights. But only OS can write to directories to maintain file system consistency.

#include <sys/types.h>#include <dirent.h>DIR *opendir(const char *pathname); ret ptr if OK, NULL on err

struct dirent *readdir(DIR *dp); ret ptr if OK, NULL at end of dir or err

void rewinddir(DIR *dp); ret 0 if OK, -1 if error

int closedir(DIR *dp); ret 0 if OK, -1 if error

• Must have WX rights for creating/deleting a file!• Actual format of directory is implementation-dependent!

Page 35: 1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data

35

Functions – opendir, readdir, rewinddir, closedir dirent struct is very much implementation dependent, but contains at least following:

struct dirent {ino_t d_ino;char d_name[NAME_MAX+1];

}

The DIR structure is an internal structure used by above functions. It has similar role to FILE structure for buffered IO. Pointer to DIR structure returned by opendir is used with all other functions. opendir makes necessary initializations of DIR struct so that readdir reads 1rst entry of directory.