SlideShare a Scribd company logo
Linux System
Programming
Working with Files
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://youtube.com/rfchishti
https://sites.google.com/site/chishti
 In Linux system, almost every thing is treated like a file.
 All input and output are done by reading or writing files, because all
peripheral devices, even keyboard and screen are files in the file system.
 This means that a single homogeneous interface handles all communication
between a program and peripheral devices.
 so we can use five basic functions: open, close, read, write, and ioctl for disk
files, serial ports, sockets, printers, etc.
 Directories are also a special sort of files.
 Every Directory or a file has a name and some properties like creation date,
user permissions, size of the file, and its location on the disk.
 All these properties are stored against the file’s inode number (this no. is
unique only with in a disk partition).
Linux File Structure
 A system uses a file’s inode number, while the directory structure names that
file for our benefit.
 A directory is a file that holds the inode numbers and names of other files.
Removing the filename also removes the inode number from the directory.
 To see the inode number type this command $ls -i
 Using ln -s hello.c soft.c command you can make soft link to the same
file/directory in different directories.
 Using ln hello.c hard.c you can make hard links to the same file in different
directories on the same partition.
 Hard link his not allowed for a directory.
 Hard links have the same inode number as the original file.
Linux File Structure
 Symbolic (Soft) links (they are like short cuts in Windows) have their own
unique inode number.
 You can’t create a hard link to a file on a different partition but you can create
a soft link on a different partition.
 ls -l command also indicates the type of file,
 The first hyphen (-) symbol indicates a regular file
 l indicates a Soft Link, d indicates a Directory
 p Indicates a FIFO or a Pipe
 s Indicates a Unix Domain Socket.
 c Indicates a Character Device (such as a terminal),
 b Indicates a Block Device (such as a hard disk). Try this ls -l /dev | less
Linux File Structure
 To mount IDE cdrom type following command
# mount -t iso9660 /dev/cdrom /mnt/cdrom
# cd /home/cdrom
 To mount .iso file type following command
# mount -t iso9660 –o loop /home/file.iso /mnt/cdrom1
# cd /home/cdrom1
 To mount a FAT32 partition
# mount -t vfat /dev/hda1 /mnt/winfiles
 To mount a NTFS partition
# mount -t ntfs /dev/hda4 /mnt/myntfs
 To unmount use # umount /mnt/cdrom
Files and Devices
To open cdrom # eject –v
To close cdrom # eject -t
Three important File Devices in Linux are
 /dev/console: This device represents the system console (screen). Error
messages and diagnostics are often sent to this device.
 /dev/tty: This device is used as virtual terminal for the user.
There’s only one /dev/console device, there are many different terminals like
tty1(first virtual terminal), tty2 etc.
 /dev/null
This file is the null device. All output written to this device is discarded e.g.
$ echo do not want to see this > /dev/null
$ cp /dev/null empty_file
$ echo do not want to see this >> msg.txt
File Devices in Linux
Linux System
A hierarchy view of Unix system, where
various file functions exist in user space and
kernel space in the different levels, and
complete different jobs.
 System calls: The Linux system provides its service through a set of functions
called as system calls. open, read, write, close, ioctl are the examples of
system calls.
 Device drivers: The Kernel uses device drivers to interact with the hardware.
Device drivers are placed at /dev location.
 Library functions: To provide a higher level interface to devices and disk files,
Linux provides a number of standard libraries, e.g.
< stdio.h >; < time.h >; < stdlib.h >; < math.h >, etc.
 Using Library functions, we can avoid the penalty in making system calls.
 Linux has to switch from running user code to executing the kernel code and
back again, so the system calls are more expensive than function calls.
Linux System
What is file descriptor?
 If you read and write a file, the system will check:
1. Does this file exit?
2. Do you have permission to access it?
 If all is well, it will return to the program a small non-negative integer, and this
is a file descriptor.
 When a program starts, it usually has three of these descriptors already
opened. These are:
0: Standard input
1: Standard output
2: Standard error
Linux System
 You can associate other file descriptors with files and devices by using the
open system call.
write
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
 The write system call arranges for the first nbytes bytes from buf to be written
to the file associated with the file descriptor fildes.
 It returns the number of bytes actually written. If the function returns 0, it
means no data was written.
 if it returns –1, there has been an error in the write call, and the error will
be specified in the errno global variable.
Low Level File Access
 Now write your first program
 This program simply prints a message to the standard output. When a
program exits, all open file descriptors are automatically closed, so you don’t
need to close them explicitly.
 This won’t be the case, however, when you’re dealing with buffered output.
 Now replace the code != with == and run the program again
Low Level File Access
#include <unistd.h>
#include <stdlib.h>
int main(){
if ((write(1, "Here is some datan", 18)) != 18)
write(2, "A write error has occurred on file descriptor 1 n",46);
exit(0);
}
simple_write.c
read
#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes);
 The read system call reads up to nbytes bytes of data from the file associated
with the file descriptor fildes and places them in the data area buf.
 It returns the number of data bytes actually read, which may be less than the
number requested.
 If a read call returns 0, it had nothing to read; it reached the end of the file.
Again, an error on the call will cause it to return –1.
 The Next program, simple_read.c, copies the first 128 bytes of the standard
input to the standard output. It copies all of the even input if there are fewer
than 128 bytes.
Low Level File Access
 Create some input for the program using echo, which is piped to your
program. $ echo hello there | ./simple_read
 Redirect input from a file. Now first part of the file draft1.txt is appearing on
the standard output. $ ./simple_read < draft1.txt
Low Level File Access
#include <unistd.h>
#include <stdlib.h>
int main(){
char buffer[128]; int nread;
nread = read(0, buffer, 128);
if (nread == -1)
write(2, "A read error has occurredn", 26);
if ((write(1,buffer,nread)) != nread)
write(2, "A write error has occurredn",27);
exit(0);
}
simple_read.c
open
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
 To create a new file descriptor, you need to use the open system call. In simple
terms, open establishes an access path to a file or device. If successful, it
returns a file descriptor that can be used in read, write, and other system calls.
 The name of the file or device to be opened is passed as a parameter, path;
the oflags parameter is used to specify actions to be taken on opening the file.
 The open call must specify one of the file access modes shown in the following
table:
Low Level File Access
 To The call may also include a combination
(using a bitwise OR) of the following optional
modes in the oflags parameter:
 O_APPEND: Place written data at the end of the file.
 O_TRUNC: Set the length of the file to zero, discarding existing contents.
 O_CREAT: Creates the file, if necessary, with permissions given in mode.
 O_EXCL: Used with O_CREAT, ensures that the caller creates the file. This
protects against two programs creating the file at the same time.
 If the file already exists, open will fail.
Low Level File Access
Mode Description
O_RDONLY Open for read-only
O_WRONLY Open for write-only
O_RDWR Open for reading and writing
Initial Permissions
 When you create a file using the O_CREAT flag with open, you must use the
three-parameter form. mode, the third parameter, is made from a bitwise OR
of the flags defined in the header file sys/stat.h. These are:
 S_IRUSR: Read permission, owner
 S_IWUSR: Write permission, owner
 S_IXUSR: Execute permission, owner
 S_IRGRP: Read permission, group
 S_IWGRP: Write permission, group
 S_IXGRP: Execute permission, group
 S_IROTH: Read permission, others
 S_IWOTH: Write permission, others
 S_IXOTH: Execute permission, others
Low Level File Access
Permission Bits and Their Values
Bit Octal value Text value Corresponding permission
8 4 0 0 - r-- --- --- Owner may read
7 2 0 0 - -w- --- --- Owner may write
6 1 0 0 - --x --- --- Owner may execute
5 0 4 0 - --- r-- --- Group may read
4 0 2 0 - --- -w- --- Group may write
3 0 1 0 - --- --x --- Group may execute
2 0 0 4 - --- --- r-- Everyone else may read
1 0 0 2 - --- --- -w- Everyone else may write
0 0 0 1 - --- --- --x Everyone else may execute
Difference between File and Directory Permissions
Directory Permissions
read We can list the files and subdirectories contained in that
directory.
write We can create and remove files and subdirectories within it.
execute We can enter the directory using the cd command
File Permissions
read We can read and copy a file but can not modify or execute
write We can modify that file
execute We can execute but can’t read or modify
 For example open ("myfile", O_CREAT, S_IRUSR|S_IXOTH);
has the effect of creating a file called myfile, with read permission for the
owner and execute permission for others, and only those permissions.
$ ls -ls myfile
0 -r-------x 1 neil software 0 Sep 22 08:11 myfile*
 The umask is a system variable that encodes a mask for file permissions to be
used when a file is created.
 You can change the variable by executing the umask command to supply a
new value. The value is a three-digit octal value.
 Each digit is the result of ORing values from 1, 2, or 4; the meanings are
shown in the following table. The separate digits refer to “user,” “group,” and
“other” permissions, respectively.
Low Level File Access
Low Level File Access
Digit Value Meaning
1 0 No user (owner) permissions are to be disallowed.
4 User (owner) read permission is disallowed.
2 User (owner) write permission is disallowed.
1 User (owner) execute permission is disallowed.
2 0 No group permissions are to be disallowed.
4 Group read permission is disallowed.
2 Group write permission is disallowed.
1 Group execute permission is disallowed.
3 0 No other permissions are to be disallowed.
4 Other read permission is disallowed.
2 Other write permission is disallowed.
1 Other execute permission is disallowed.
 For example, to block “group” write and execute, and “other” write, the
umask would be
 Values for each digit are ORed together; so the second digit will need to be 2
| 1, giving 3. The resulting umask is 032.
 You can also use chmod command to set the permissions of a file or a folder.
# chmod 755 a.out
 You can use chown command to set the ownership of a file or a folder.
# chown nobody:nobody widgets
Low Level File Access
Digit Value Description
1 0 Owner can read = 4, write = 2 and execute = 1
2 2
1
Group can read = 4 only
3 2 Others can read = 4 and execute = 1
close #include <unistd.h>
int close(int fildes);
 You use close to terminate the association between a file descriptor, fildes, and its file. The
file descriptor becomes available for reuse. It returns 0 if successful and –1 on error.
Ioctl #include <unistd.h>
int ioctl(int fildes, int cmd, ...);
 It provides an interface for controlling the behavior of devices and their descriptors and
configuring underlying services.
 ioctl performs the function indicated by cmd on the object referenced by the descriptor
fields. It may take an optional third argument, depending on the functions supported by a
particular device.
 For example, the following call to ioctl on Linux turns on the keyboard LEDs:
 ioctl(tty_fd, KDSETLED, LED_NUM|LED_CAP|LED_SCR);
Low Level File Access
Try It Out A File Copy Program
 Following program, copies one file to another, character by character.
 First Make a test input file, say 1Mb in size, and name it file.in then compile
copy_system.c
Low Level File Access
#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);
}
copy_system.c
 Running the program will give something like the following
 TIMEFORMAT="" time ./copy_system ls -ls file.in file.out
 A Second File Copy Program (copy_block.c)
 $ rm file.out $ TIMEFORMAT="" time ./copy_block
Low Level File Access
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(){
char block[1024]; int in, out, 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);
}
copy_block.c
 A Third File Copy Program (copy_stdio.c)
$ TIMEFORMAT="" time ./copy_stdio
Low Level File Access
#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);
}
copy_stdio.c
 Linux provides a special file system, procfs, that is usually made available as the directory
/proc.
 It contains many special files that allow higher-level access to driver and kernel information.
Type this command $ ls /proc
 $ cat /proc/cpuinfo // gives details of the processors available:
 $ cat /proc/meminfo // gives information about memory usage
 $ cat /proc/version // gives information about kernel version
 $ cat /proc/net/sockstat // gives network socket usage statistics
 $ cat /proc/sys/fs/file-max // tells total no of files that all running programs can open at the
same time this can be changed directly using
 # echo 80000 >/proc/sys/fs/file-max
 $ cat /proc/sys/fs/file-max
 $ cat /proc/sys/net/ipv4/ip_forward // should it work as a router
/proc File System
 Each process in Linux has a unique identifier: a number between 1 and about 32,000.
 The ps command provides a list of currently running processes. ps -aux
 PID TTY TIME CMD
 9118 pts/1 00:00:00 ftp
 9230 pts/1 00:00:00 ps
 10689 pts/1 00:00:01 bash
 The process identifier for ftp here is given as 9118, so you need to look in /proc/9118 for
details about it:
 $ ls -l /proc/9118
 You can tell that the program /usr/bin/pftp is running and that its current working directory
is /home/neil/BLP4e/chapter03.
 $ od -c /proc/9118/cmdline
 Here, you can see that ftp was started with the command line ftp 192.168.0.12
 $ ls /proc/9118/fd // you can see that
 ftp has open descriptors 0, 1, 2, and 3, as we might expect. These are the standard input,
output, and error descriptors plus a connection to the remote server.
/proc File System

More Related Content

Similar to Linux Systems Programming: File Handling

Linux introductory-course-day-1
Linux introductory-course-day-1Linux introductory-course-day-1
Linux introductory-course-day-1
Julio Pulido
 

Similar to Linux Systems Programming: File Handling (20)

Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)
 
Unix
UnixUnix
Unix
 
Module 3 Using Linux Softwares.
Module 3 Using Linux Softwares.Module 3 Using Linux Softwares.
Module 3 Using Linux Softwares.
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
Linux Basics
Linux BasicsLinux Basics
Linux Basics
 
Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
 
Linux
LinuxLinux
Linux
 
Edubooktraining
EdubooktrainingEdubooktraining
Edubooktraining
 
File management
File managementFile management
File management
 
Assignment OS LAB 2022
Assignment OS LAB 2022Assignment OS LAB 2022
Assignment OS LAB 2022
 
Linux basic
Linux basicLinux basic
Linux basic
 
84640411 study-of-unix-os
84640411 study-of-unix-os84640411 study-of-unix-os
84640411 study-of-unix-os
 
Linux introductory-course-day-1
Linux introductory-course-day-1Linux introductory-course-day-1
Linux introductory-course-day-1
 
Unix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basiUnix_Introduction_BCA.pptx the very basi
Unix_Introduction_BCA.pptx the very basi
 
Nguyễn Vũ Hưng: Basic Linux Power Tools
Nguyễn Vũ Hưng: Basic Linux Power Tools Nguyễn Vũ Hưng: Basic Linux Power Tools
Nguyễn Vũ Hưng: Basic Linux Power Tools
 
Unix Administration
Unix AdministrationUnix Administration
Unix Administration
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
 
beginner.en.print
beginner.en.printbeginner.en.print
beginner.en.print
 
beginner.en.print
beginner.en.printbeginner.en.print
beginner.en.print
 
beginner.en.print
beginner.en.printbeginner.en.print
beginner.en.print
 

More from RashidFaridChishti

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
Atif Razi
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 

Recently uploaded (20)

Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
retail automation billing system ppt.pptx
retail automation billing system ppt.pptxretail automation billing system ppt.pptx
retail automation billing system ppt.pptx
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
AI for workflow automation Use cases applications benefits and development.pdf
AI for workflow automation Use cases applications benefits and development.pdfAI for workflow automation Use cases applications benefits and development.pdf
AI for workflow automation Use cases applications benefits and development.pdf
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 

Linux Systems Programming: File Handling

  • 1. Linux System Programming Working with Files Engr. Rashid Farid Chishti chishti@iiu.edu.pk https://youtube.com/rfchishti https://sites.google.com/site/chishti
  • 2.  In Linux system, almost every thing is treated like a file.  All input and output are done by reading or writing files, because all peripheral devices, even keyboard and screen are files in the file system.  This means that a single homogeneous interface handles all communication between a program and peripheral devices.  so we can use five basic functions: open, close, read, write, and ioctl for disk files, serial ports, sockets, printers, etc.  Directories are also a special sort of files.  Every Directory or a file has a name and some properties like creation date, user permissions, size of the file, and its location on the disk.  All these properties are stored against the file’s inode number (this no. is unique only with in a disk partition). Linux File Structure
  • 3.  A system uses a file’s inode number, while the directory structure names that file for our benefit.  A directory is a file that holds the inode numbers and names of other files. Removing the filename also removes the inode number from the directory.  To see the inode number type this command $ls -i  Using ln -s hello.c soft.c command you can make soft link to the same file/directory in different directories.  Using ln hello.c hard.c you can make hard links to the same file in different directories on the same partition.  Hard link his not allowed for a directory.  Hard links have the same inode number as the original file. Linux File Structure
  • 4.  Symbolic (Soft) links (they are like short cuts in Windows) have their own unique inode number.  You can’t create a hard link to a file on a different partition but you can create a soft link on a different partition.  ls -l command also indicates the type of file,  The first hyphen (-) symbol indicates a regular file  l indicates a Soft Link, d indicates a Directory  p Indicates a FIFO or a Pipe  s Indicates a Unix Domain Socket.  c Indicates a Character Device (such as a terminal),  b Indicates a Block Device (such as a hard disk). Try this ls -l /dev | less Linux File Structure
  • 5.  To mount IDE cdrom type following command # mount -t iso9660 /dev/cdrom /mnt/cdrom # cd /home/cdrom  To mount .iso file type following command # mount -t iso9660 –o loop /home/file.iso /mnt/cdrom1 # cd /home/cdrom1  To mount a FAT32 partition # mount -t vfat /dev/hda1 /mnt/winfiles  To mount a NTFS partition # mount -t ntfs /dev/hda4 /mnt/myntfs  To unmount use # umount /mnt/cdrom Files and Devices To open cdrom # eject –v To close cdrom # eject -t
  • 6. Three important File Devices in Linux are  /dev/console: This device represents the system console (screen). Error messages and diagnostics are often sent to this device.  /dev/tty: This device is used as virtual terminal for the user. There’s only one /dev/console device, there are many different terminals like tty1(first virtual terminal), tty2 etc.  /dev/null This file is the null device. All output written to this device is discarded e.g. $ echo do not want to see this > /dev/null $ cp /dev/null empty_file $ echo do not want to see this >> msg.txt File Devices in Linux
  • 7. Linux System A hierarchy view of Unix system, where various file functions exist in user space and kernel space in the different levels, and complete different jobs.
  • 8.  System calls: The Linux system provides its service through a set of functions called as system calls. open, read, write, close, ioctl are the examples of system calls.  Device drivers: The Kernel uses device drivers to interact with the hardware. Device drivers are placed at /dev location.  Library functions: To provide a higher level interface to devices and disk files, Linux provides a number of standard libraries, e.g. < stdio.h >; < time.h >; < stdlib.h >; < math.h >, etc.  Using Library functions, we can avoid the penalty in making system calls.  Linux has to switch from running user code to executing the kernel code and back again, so the system calls are more expensive than function calls. Linux System
  • 9. What is file descriptor?  If you read and write a file, the system will check: 1. Does this file exit? 2. Do you have permission to access it?  If all is well, it will return to the program a small non-negative integer, and this is a file descriptor.  When a program starts, it usually has three of these descriptors already opened. These are: 0: Standard input 1: Standard output 2: Standard error Linux System
  • 10.  You can associate other file descriptors with files and devices by using the open system call. write #include <unistd.h> size_t write(int fildes, const void *buf, size_t nbytes);  The write system call arranges for the first nbytes bytes from buf to be written to the file associated with the file descriptor fildes.  It returns the number of bytes actually written. If the function returns 0, it means no data was written.  if it returns –1, there has been an error in the write call, and the error will be specified in the errno global variable. Low Level File Access
  • 11.  Now write your first program  This program simply prints a message to the standard output. When a program exits, all open file descriptors are automatically closed, so you don’t need to close them explicitly.  This won’t be the case, however, when you’re dealing with buffered output.  Now replace the code != with == and run the program again Low Level File Access #include <unistd.h> #include <stdlib.h> int main(){ if ((write(1, "Here is some datan", 18)) != 18) write(2, "A write error has occurred on file descriptor 1 n",46); exit(0); } simple_write.c
  • 12. read #include <unistd.h> size_t read(int fildes, void *buf, size_t nbytes);  The read system call reads up to nbytes bytes of data from the file associated with the file descriptor fildes and places them in the data area buf.  It returns the number of data bytes actually read, which may be less than the number requested.  If a read call returns 0, it had nothing to read; it reached the end of the file. Again, an error on the call will cause it to return –1.  The Next program, simple_read.c, copies the first 128 bytes of the standard input to the standard output. It copies all of the even input if there are fewer than 128 bytes. Low Level File Access
  • 13.  Create some input for the program using echo, which is piped to your program. $ echo hello there | ./simple_read  Redirect input from a file. Now first part of the file draft1.txt is appearing on the standard output. $ ./simple_read < draft1.txt Low Level File Access #include <unistd.h> #include <stdlib.h> int main(){ char buffer[128]; int nread; nread = read(0, buffer, 128); if (nread == -1) write(2, "A read error has occurredn", 26); if ((write(1,buffer,nread)) != nread) write(2, "A write error has occurredn",27); exit(0); } simple_read.c
  • 14. open #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int open(const char *path, int oflags); int open(const char *path, int oflags, mode_t mode);  To create a new file descriptor, you need to use the open system call. In simple terms, open establishes an access path to a file or device. If successful, it returns a file descriptor that can be used in read, write, and other system calls.  The name of the file or device to be opened is passed as a parameter, path; the oflags parameter is used to specify actions to be taken on opening the file.  The open call must specify one of the file access modes shown in the following table: Low Level File Access
  • 15.  To The call may also include a combination (using a bitwise OR) of the following optional modes in the oflags parameter:  O_APPEND: Place written data at the end of the file.  O_TRUNC: Set the length of the file to zero, discarding existing contents.  O_CREAT: Creates the file, if necessary, with permissions given in mode.  O_EXCL: Used with O_CREAT, ensures that the caller creates the file. This protects against two programs creating the file at the same time.  If the file already exists, open will fail. Low Level File Access Mode Description O_RDONLY Open for read-only O_WRONLY Open for write-only O_RDWR Open for reading and writing
  • 16. Initial Permissions  When you create a file using the O_CREAT flag with open, you must use the three-parameter form. mode, the third parameter, is made from a bitwise OR of the flags defined in the header file sys/stat.h. These are:  S_IRUSR: Read permission, owner  S_IWUSR: Write permission, owner  S_IXUSR: Execute permission, owner  S_IRGRP: Read permission, group  S_IWGRP: Write permission, group  S_IXGRP: Execute permission, group  S_IROTH: Read permission, others  S_IWOTH: Write permission, others  S_IXOTH: Execute permission, others Low Level File Access
  • 17. Permission Bits and Their Values Bit Octal value Text value Corresponding permission 8 4 0 0 - r-- --- --- Owner may read 7 2 0 0 - -w- --- --- Owner may write 6 1 0 0 - --x --- --- Owner may execute 5 0 4 0 - --- r-- --- Group may read 4 0 2 0 - --- -w- --- Group may write 3 0 1 0 - --- --x --- Group may execute 2 0 0 4 - --- --- r-- Everyone else may read 1 0 0 2 - --- --- -w- Everyone else may write 0 0 0 1 - --- --- --x Everyone else may execute
  • 18. Difference between File and Directory Permissions Directory Permissions read We can list the files and subdirectories contained in that directory. write We can create and remove files and subdirectories within it. execute We can enter the directory using the cd command File Permissions read We can read and copy a file but can not modify or execute write We can modify that file execute We can execute but can’t read or modify
  • 19.  For example open ("myfile", O_CREAT, S_IRUSR|S_IXOTH); has the effect of creating a file called myfile, with read permission for the owner and execute permission for others, and only those permissions. $ ls -ls myfile 0 -r-------x 1 neil software 0 Sep 22 08:11 myfile*  The umask is a system variable that encodes a mask for file permissions to be used when a file is created.  You can change the variable by executing the umask command to supply a new value. The value is a three-digit octal value.  Each digit is the result of ORing values from 1, 2, or 4; the meanings are shown in the following table. The separate digits refer to “user,” “group,” and “other” permissions, respectively. Low Level File Access
  • 20. Low Level File Access Digit Value Meaning 1 0 No user (owner) permissions are to be disallowed. 4 User (owner) read permission is disallowed. 2 User (owner) write permission is disallowed. 1 User (owner) execute permission is disallowed. 2 0 No group permissions are to be disallowed. 4 Group read permission is disallowed. 2 Group write permission is disallowed. 1 Group execute permission is disallowed. 3 0 No other permissions are to be disallowed. 4 Other read permission is disallowed. 2 Other write permission is disallowed. 1 Other execute permission is disallowed.
  • 21.  For example, to block “group” write and execute, and “other” write, the umask would be  Values for each digit are ORed together; so the second digit will need to be 2 | 1, giving 3. The resulting umask is 032.  You can also use chmod command to set the permissions of a file or a folder. # chmod 755 a.out  You can use chown command to set the ownership of a file or a folder. # chown nobody:nobody widgets Low Level File Access Digit Value Description 1 0 Owner can read = 4, write = 2 and execute = 1 2 2 1 Group can read = 4 only 3 2 Others can read = 4 and execute = 1
  • 22. close #include <unistd.h> int close(int fildes);  You use close to terminate the association between a file descriptor, fildes, and its file. The file descriptor becomes available for reuse. It returns 0 if successful and –1 on error. Ioctl #include <unistd.h> int ioctl(int fildes, int cmd, ...);  It provides an interface for controlling the behavior of devices and their descriptors and configuring underlying services.  ioctl performs the function indicated by cmd on the object referenced by the descriptor fields. It may take an optional third argument, depending on the functions supported by a particular device.  For example, the following call to ioctl on Linux turns on the keyboard LEDs:  ioctl(tty_fd, KDSETLED, LED_NUM|LED_CAP|LED_SCR); Low Level File Access
  • 23. Try It Out A File Copy Program  Following program, copies one file to another, character by character.  First Make a test input file, say 1Mb in size, and name it file.in then compile copy_system.c Low Level File Access #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); } copy_system.c
  • 24.  Running the program will give something like the following  TIMEFORMAT="" time ./copy_system ls -ls file.in file.out  A Second File Copy Program (copy_block.c)  $ rm file.out $ TIMEFORMAT="" time ./copy_block Low Level File Access #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main(){ char block[1024]; int in, out, 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); } copy_block.c
  • 25.  A Third File Copy Program (copy_stdio.c) $ TIMEFORMAT="" time ./copy_stdio Low Level File Access #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); } copy_stdio.c
  • 26.  Linux provides a special file system, procfs, that is usually made available as the directory /proc.  It contains many special files that allow higher-level access to driver and kernel information. Type this command $ ls /proc  $ cat /proc/cpuinfo // gives details of the processors available:  $ cat /proc/meminfo // gives information about memory usage  $ cat /proc/version // gives information about kernel version  $ cat /proc/net/sockstat // gives network socket usage statistics  $ cat /proc/sys/fs/file-max // tells total no of files that all running programs can open at the same time this can be changed directly using  # echo 80000 >/proc/sys/fs/file-max  $ cat /proc/sys/fs/file-max  $ cat /proc/sys/net/ipv4/ip_forward // should it work as a router /proc File System
  • 27.  Each process in Linux has a unique identifier: a number between 1 and about 32,000.  The ps command provides a list of currently running processes. ps -aux  PID TTY TIME CMD  9118 pts/1 00:00:00 ftp  9230 pts/1 00:00:00 ps  10689 pts/1 00:00:01 bash  The process identifier for ftp here is given as 9118, so you need to look in /proc/9118 for details about it:  $ ls -l /proc/9118  You can tell that the program /usr/bin/pftp is running and that its current working directory is /home/neil/BLP4e/chapter03.  $ od -c /proc/9118/cmdline  Here, you can see that ftp was started with the command line ftp 192.168.0.12  $ ls /proc/9118/fd // you can see that  ftp has open descriptors 0, 1, 2, and 3, as we might expect. These are the standard input, output, and error descriptors plus a connection to the remote server. /proc File System