struct stat { dev_t st_dev; ino_t st_ino; mode_t st_mode; nlink_t st_nlink; Struct stat uid_t st_uid; gid_t st_gid; dev_t st_rdev; off_t st_size; /* SysV/sco doesn't have the rest... But Solaris, eabi does. */ #if defined(__svr4__) && !defined(__PPC__) && !defined(__sun__) Files and Directories time_t st_atime; time_t st_mtime; time_t st_ctime; #else time_t st_atime; Marco Gruteser long st_spare1; time_t st_mtime; Advanced Programming for Linux long st_spare2; time_t st_ctime; long st_spare3; long st_blksize; long st_blocks; long st_spare4[2]; #endif 1 }; 2
Why does the operating system File types (mode) need to distinguish these? • Regular file • Directory file • Character special file (device access, e.g. serial port) • Block special file (device access, e.g., disk) •FIFO (pipe) • Socket (network connection) • Symbolic link (pointer to another file)
3 4 Some operations only valid on File Access control list certain files • No lseek on Fifo or socket • Every file (includes directories, device • No write on directory files) has • Open on symlink requires redirection – owner user and owner group – Permissions (-rwxr-x---) •…
5 6
Process User- and Group-IDs File access checks
• Real user ID • Automatically invoked on file open • Real group ID –Uses effective uid/gid • Effective user ID used for file • Manual invocation through access function possible • Effective group ID access checks What for? –Uses real uid/gid • Saved set-user-ID • Saved set-group-ID saved by exec
7 8 File access checks Setuid / setgid
• Requires x permission on all directories in • New processes inherit user ids from pathname parent • If uid==0 – allow access • If setuid files system flag is set • If uid==owner – Effective uid set to executable’s owner – if owner permission allow (else deny) • If setgid file system flag is set • If gid (or supplementary)==group – Effective gid set to executable’s group – if group permission allow (else deny) owner •Else – if others permission allow (else deny • Programs can selectively raise privileges
9 10
Sticky bit New Files
• A user with directory write permissions • Inherit effective user and group ID from can rename and remove any files creatingmode creation process mask • If sticky bit is set, only the owner of the – Incompatibility: Some systems set gid to directory or owner of the file can do so gid of the parent directory • Permissions are set according to the file of the process – Umask system call
11 12 #include
/* turn on set-group-ID and turn off group-execute */ • Keeping secure log files
if (stat("foo", &statbuf) < 0) • “Secure printer” err_sys("stat error for foo"); if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 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); 13 14 }
Unix Filesystem Structure? Hard links
• Every directory entry points to an i-node (which represents a file) – Multiple entries can point to the same file (hard links) • Link(existingpath, newpath) creates additional directory entry to existing file • Unlink removes link and deletes files if no other links exist • Linking to directory files also possible
15 16 Problem: Loops Symbolic links / • Recursive listing of files does usr • Can span filesystems, user’s can create links not work to directories • No easy way to fix Hello.c • Represented by special files – Unlink does not remove links mydir to directories • Loops are easier to remove – Rmdir removes links to • Symlink(actualpath, sympath) – creates link directories only when they are empty • Readlink(pathname, …) – reads sympath • Only root can create hard – These files cannot be read with “open” ! links to directories
17 18
Which file? Which file?
•Access • Access follows link •Chdir • Chdir follows link •Chown •Chown not •Open • Open follows link •Opendir • Opendir follows link •Remove • Remove not •rename •Rename not
19 20 Directory manipulation File Times
•Mkdir, rmdir • Three time entries – create and remove – (Content) Access time • Opendir, readdir, rewinddir, closedir – (Content) Modification time – Read directory entries – Changed status time (i-node modification • There is no direct write time) – Implicit in ‘create file’ and ‘mkdir’ • E.g., link, chmod, chown affect this •Chdir, getcwd • Utime sets access and modification time – Set and get working directory for current process – Changed status time only updated by kernel
21 22
Special Device Files Sync
• All files store the file system’s device •Sync number (st_dev) – Flush entire kernel buffer cache • Special device files (character and •Fsync block) store their device’s number in – Flush one file in the buffer cache (st_rdev) • O_SYNC flag • Need to use major and minor macros – Flush every write to this file
23 24 Next Class
• Environment of a Unix Process • Read Chapter 7 (and 5)
25