Professional Documents
Culture Documents
Unit 2
Unit 2
File Mode File operations Querying and changing Inode information Manipulating Directory entries Manipulating file descriptors Creating Unnamed pipes
File Handling
Resources such as mem, disk space, devices, and IPC channels are represented as files Resources accessed thro file operations: Regular files grow arbitrarily large Allow random access Byte oriented Pipes IPC mechanism One process writes info and another reads from it Used to provide I/O redirection ls l | grep f*
Types: Unnamed pipes Named pipes Unnamed pipes: No file name Created when needed and disappear when read and write ends are closed Do not exist in file system. Named pipes: Have file names Names used by processes to communicate each other Also known as FIFOs
Directories Special file that consist of a list of files they contain Opened and closed like regular files Special set of system calls to provide directory manipulation
Devices
Two types of device files: Block device
Devices that must be read in multiples of block size Receive special handling from kernel Disk drives ROM drives and RAM disks
Character device
Read as single char at a time No caching or ordering facilities provided by kernel Modems, terminals, printers, soundcards and mice
Symbolic links
Contains path to another file When a symlink is opened, system recognizes it as a symlink, reads its value and opens the file it references. When the value is used, system is said to be following the symlink
Sockets
Provide an IPC channel Flexible than pipes Can create IPC channel between processes running on different machines
Inode
Abbreviation to information node Unique identity Contains info about a file like
Access permissions Current size no. of file names it has
Types:
in-core inode
every open file has one Kernel keeps in-core inodes in memory Structure same for all file-system types Keeps track of no. of processes currently using the file associated with inode
on-disk inode
Every files on file system has one Structure depends on file-system type When a process opens a file on file system, on-disk is loaded into memory and converted into an in-core inode
Most system calls end updates both inodes Some files (like pipes) do not have on-disk inode updates only in-core inode Link count: no. of file names that refer to an inode and is stored in on-disk inode File removed LC is 0 no processes using it disk space freed If processes using it, space freed when final process closes the file File name is a pointer to a files on-disk inode
File mode
File type and access permission 4+12 bits Modifiers allow the EUID and GIDs to change when file is executed Six octal digits eg: 041777 0100755 low-order 3 digits access bits Next digit file permission modifiers( 0- no modifiers set) High-order 2 digits file type
File Access Permission access permission First digit files owner Second users in the files group Last other users Each octal digit is made of three bits read, write and exec World permission: permission given to all three classes of users Chmod used to change access permission Eg: chmod 0644 filename ls l filename rw-r--r- 6(110) ,4(100) Directory access Read lists the directory content Write create and delete files Execute search a directory can access a file in that
File Permission Modifiers users to run an executable with the permissions of the executable's owner or group to run programs with temporarily elevated privileges in order to perform a specific task Setuid: EUID = owner ID Setgid: EGID = group ID Setuid and setgid ignored for scripts insecure Directorys setgid files owned by the group that owns the directory Sticky bit Leave program stuck in memory, when not running Directories sticky bit is set files removed only the owner and root user (eg. /tmp)
Umask Every process has a umask Specifies the permission bits to turn off when files are created For sensitive files more restrictive permissions can be set
#include <sys/stat.h> Int umask(int newmask);
Return old mask and set new value
touch create new files with 0666(World read and write) $umask 022 (off world and group write) $touch foo $ls l foo -rw-r--r-foo $umask 002 (off group write) $touch foo2 $ls l foo2 -rw-rw-r-foo $umask 077 (all files accessible only by owner) $touch foo3 $ls l foo3 -rw------foo