The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo...

50
The Linux File System

Transcript of The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo...

Page 1: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

The Linux File System

Page 2: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Everything is a file

• In Linux systems almost everything is a file.

– Regular Files

– Directories

– Hardware devices

• Why is it good?

Page 3: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Directories Structure

• The root directory - / contains all other directories in the system.

• The home directory - /home subdirectory of root and contains all users’ files.

– In the shell, the notation ~ getting straight to your home directory.

Page 4: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Special Directories

• /dev – file that represent physical devices and provide the interface to those devices.

• /bin – system’s programs (binaries).

• /lib – system’s libraries.

Page 5: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

/dev files

• /dev/console – This device represents the system console.

• /dev/tty – The special file for the controlling terminal.

• /dev/null – This file is a null device.

Page 6: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Empty file

• With the command cp, /dev/null file can used as source to empty file.

>cp /dev/null <filename>

• Another way to creating empty files is to use the touch command.

>touch <filename>

Page 7: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

- Example /dev/tty

#include <stdio.h>

int main() { FILE *input; FILE *output; int num,success; input = fopen("/dev/tty", "r"); output = fopen("/dev/tty", "w"); fprintf(output,"Enter a number:\n"); success = fscanf(input,"%d",&num); if(success) { fprintf(output,"Your number is: %d\n",num); } else { fprintf(stderr,"It isn't a number.\n"); }

}

Page 8: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Main File System Calls

• open: Open a file or device.

• read: Read from an open file or device.

• write: Write to a file or device.

• close: Close the file or device.

• ioctl: Pass control information to a device driver.

Page 9: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Library Functions – why?

• system calls are best avoided:

– Expensive performance.

– Limitations.

Page 10: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Library Functions

• collections of functions for user programs.

– stdio.h – for standard I/O

Page 11: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Low-Level File System Calls

Page 12: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Low-Level File Access

• Three descriptors are open automatically when each program starts:

– 0: Standard input

– 1: Standard output

– 2: Standard error

Page 13: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

write()

size_t write(int fildes, const void *buf, size_t nbytes)

• fildes – file descriptor number. • buf – buffer to write from. • nbytes – number of bytes to write from the

buffer.

• Returns the number of bytes actually written or -1 on error.

Page 14: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

read()

size_t read(int fildes, void *buf, size_t nbytes)

• fildes – file descriptor number to read from.

• buf – buffer to write the data.

• nbytes – number of bytes to read from the file.

• Returns the number of bytes actually read or -1 on error.

Page 15: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

open()

int open(const char *path, int oflags) int open(const char *path, int oflags, mode_t mode) Establishes an access path to a file or device. • path – Name of the file or device. • oflags – specifying actions to be taken on opening the file

(read, write, create etc...) • mode – specifying file Initial Permissions.

• Returns a file descriptor that can be used in read, write and

other system calls or -1 on error.

Page 16: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

close()

int close(int fildes)

Terminate the association between a file descriptor and its file.

• fildes – file descriptor.

• Returns the 0 on success or -1 on error.

Page 17: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

ioctl()

int ioctl(int fildes, int cmd, ...)

provides an interface for controlling the behavior of devices and their descriptors and configuring underlying services.

• fildes – object descriptor number.

• cmd – functions\command to perform

Page 18: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example 1 - A File Copy Program

#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main() { char c; int in, out; in = open(“file.in”, O_RDONLY); out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while(read(in,&c,1) == 1) write(out,&c,1); exit(0); }

Page 19: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example 2 - A Second File Copy Program

#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main() { char block[1024]; int in, out; int nread; in = open(“file.in”, O_RDONLY); out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while((nread = read(in,block,sizeof(block))) > 0) write(out,block,nread); exit(0); }

Page 20: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

ANSI-C I/O library functions

• fopen, fclose • fread, fwrite • fflush • fseek • fgetc, getc, getchar • fputc, putc, putchar • fgets, gets • printf, fprintf, and sprintf • scanf, fscanf, and sscanf

What is the difference between those and the system calls shown

?before

Page 21: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example 3 - A Third File Copy Program

#include <stdio.h> #include <stdlib.h> int main() { int c; FILE *in, *out; in = fopen(“file.in”,”r”); out = fopen(“file.out”,”w”); while((c = fgetc(in)) != EOF) fputc(c,out); exit(0); }

Page 22: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Directories manipulations

• The library for directories manipulations: dirent.h.

• The struct type, DIR*, is a directory stream.

• Some of directories functions: – opendir, closedir

– readdir

– telldir

– seekdir

– closedir

Page 23: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Directory scanning program

#include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> #include <stdlib.h> void printdir(char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr,”cannot open directory: %s\n”, dir); return; { …

Page 24: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Directory scanning program

chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { if(strcmp(“.”,entry->d_name) == 0 || strcmp(“..”,entry->d_name) == 0) continue; printf(“%*s%s/\n”,depth,”“,entry->d_name); printdir(entry->d_name,depth+4); {

else printf(“%*s%s\n”,depth,”“,entry->d_name); {

chdir(“..”); closedir(dp); {

Page 25: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Directory scanning program

int main()

{

printf(“Directory scan of /home:\n”);

printdir(“/home”,0);

printf(“done.\n”);

exit(0);

}

Page 26: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

The /proc File System

• The directory /proc contains many special files that allow higher-level access to driver and kernel information.

Page 27: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

The /proc File System – Example 1

$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R) Pentium(R) 4 CPU 2.66GHz stepping : 8 cpu MHz : 2665.923 cache size : 512 KB fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 2 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss up bogomips : 5413.47 clflush size : 64

Page 28: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

The /proc File System – Example 2

$ cat /proc/net/sockstat

sockets: used 285

TCP: inuse 4 orphan 0 tw 0 alloc 7 mem 1

UDP: inuse 3

UDPLITE: inuse 0

RAW: inuse 0

FRAG: inuse 0 memory 0

Page 29: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

fcntl()

#include <fcntl.h> int fcntl(int fildes, int cmd); int fcntl(int fildes, int cmd, long arg); Performing several operations on open file descriptors. (getting/setting flags etc…) • fildes – file descriptor number. • cmd – operation to perform. • arg – argument for the command.

Page 30: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

mmap()

void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

map files or device into memory.

Page 31: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Using mmap() #include <unistd.h> #include <stdio.h> #include <sys/mman.h> #include <fcntl.h> #include <stdlib.h> typedef struct { int integer; char string[24]; } RECORD; #define NRECORDS (100) int main() }

RECORD record, *mapped; int i, f; FILE *fp; fp = fopen(“records.dat”,”w+”);

Page 32: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Using mmap()

for(i=0; i<NRECORDS; i++) {

record.integer = i;

sprintf(record.string,”RECORD-%d”,i);

fwrite(&record,sizeof(record),1,fp);

}

fclose(fp);

Page 33: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Using mmap()

f = open(“records.dat”,O_RDWR); mapped = (RECORD *)mmap(0, NRECORDS*sizeof(record), PROT_READ|PROT_WRITE, MAP_SHARED, f, 0); mapped[43].integer = 243; sprintf(mapped[43].string,”RECORD-%d”,mapped[43].integer); msync((void *)mapped, NRECORDS*sizeof(record), MS_ASYNC); munmap((void *)mapped, NRECORDS*sizeof(record)); close(f); exit(0); {

Page 34: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Example - Using mmap()

fp = fopen(“records.dat”,”r+”); fseek(fp,43*sizeof(record),SEEK_SET); fread(&record,sizeof(record),1,fp); record.integer = 143; sprintf(record.string,”RECORD-%d”,record.integer); fseek(fp,43*sizeof(record),SEEK_SET); fwrite(&record,sizeof(record),1,fp); fclose(fp);

Page 35: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Files’ Metadata

• A set of data that describes and gives information about the file, stores in file’s i-node. – Access permissions

– Last access timestamp

– Owner

– Group

– Size

– And more…

Page 36: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

File permissions

• File permissions - Allowing\disallowing access to the file in order to read (r), write (w) or Execute (x).

• Can give permission to the user, group, or others.

• Example of file permission bits in the bash: >ls -l <filename>

-rwxr-xr--

Page 37: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Extended attributes

• provide a mechanism for associating key/value pairs with files.

• Extended attributes used to insert additional attributes besides the attributes in the file system.

Page 38: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

The Current Working Directory

• Every process has a current directory, which it initially inherits from its parent process.

• The current working directory is the starting point from which the kernel resolves relative pathnames.

• Example: if the process tries to open <filename>, the kernel will attempt to open: current directory/<filename>

• Get Current Working Directory:

char * getcwd (char *buf, size_t size);

Page 39: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Links and Hard links

• Link – connection between a file name and the actual data in the disk (i-node).

• Hard Link – a link between many files to the same i-node represents the file.

• Symbolic Link – a link between file and other file name in the file system.

Page 40: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

link() system call

int link (const char *oldpath, const char *newpath);

Creates a link (hard link) under the newpath for the existing file oldpath.

Returns 0 on success and -1 on error.

Page 41: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

symlink() system call

int symlink (const char *oldpath, const char *newpath);

Creates a link (symbolic link) under the newpath for the existing file oldpath.

Returns 0 on success and -1 on error.

Page 42: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

unlink() system call

int unlink (const char *pathname);

Deletes the pathname from the file system.

Returns 0 on success and -1 on error.

Page 43: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

File events - inotify

• Inotify – interface for monitoring file events to see when they are moved, read from, written to, or deleted.

The library - #include <sys/inotify.h>

Page 44: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

inotify - Example

#include <sys/inotify.h> #include <stdio.h> #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main() { int fd,length, i = 0; fd = inotify_init (); int wd; wd = inotify_add_watch (fd, ".", IN_MOVE); char buffer[BUF_LEN]; length = read( fd, buffer, BUF_LEN );

Page 45: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

inotify - Example

while ( i < length ) { struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; if (event->len) { if ( event->mask & IN_MOVE ) { printf( "The file %s was moved.\n", event->name ); } } i += EVENT_SIZE + event->len; } (void) inotify_rm_watch( fd, wd ); (void) close( fd ); return 0; }

Page 46: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Self studying

Error Handling:

• char *strerror(int errnum);

• void perror(const char *s);

Page 47: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Self studying

• Stream Errors:

– int ferror(FILE *stream);

– int feof(FILE *stream);

– void clearerr(FILE *stream);

• Streams and File Descriptors :

– int fileno(FILE *stream);

– FILE *fdopen(int fildes, const char *mode);

Page 48: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Self studying

• File and Directory Maintenance:

– int chmod(const char *path, mode_t mode);

– int chown(const char *path, uid_t owner, gid_t group);

– int unlink(const char *path);

– int link(const char *path1, const char *path2);

– int symlink(const char *path1, const char *path2);

Page 49: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Self studying

• mkdir and rmdir:

– int mkdir(const char *path, mode_t mode);

– int rmdir(const char *path);

• chdir and getcwd:

– int chdir(const char *path);

– char *getcwd(char *buf, size_t size);

Page 50: The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R)

Self studying

• File state:

– int stat (const char *path, struct stat *buf);

– int fstat (int fd, struct stat *buf);

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

• Permissions:

– int chmod (const char *path, mode_t mode);

– int fchmod (int fd, mode_t mode);