0% found this document useful (0 votes)
28 views46 pages

03 ch3 ELEC462

Uploaded by

이정화
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views46 pages

03 ch3 ELEC462

Uploaded by

이정화
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

System Programming

(ELEC462)
Directories and File Properties:
Looking through ls

Dukyun Nam
HPC Lab@KNU
Contents
● Introduction
● Project: ls command
● Brief Review of the File System Tree
● Converting File Mode to a String
● The Three Special Bits
● Setting and Modifying the Properties of a File
● Summary
● Appendix: gdb Debugger
2
Introduction
● Recall
○ Last time, we looked at ways to operate on the CONTENTS of a file:
open, read, write, lseek, close
● But, there is more to a file than just contents:
○ 1) Properties
■ Size, owner, permission, type, etc.
○ 2) Location in a directory tree
● To learn about these:
< Data flow in the who command >
○ We shall write a version of ls
3
What Does ls Do?
● Type the command ls to see what it does:

● The default action of ‘ls’:


○ To list the names of the files in the “current” directory

4
What Does ls Do? (cont.)
● When given the ‘-l’ command-line option
○ ls present information about each file and its properties using the long
format

5
What Does ls Do? (cont.)
● Listing other directories

○ If an argument is a “directory,” ls lists its contents


○ If an argument is a “file,” ls lists its name or attributes
■ What command line option is specified affects what ls does and what it shows

6
What Does ls Do? (cont.)
● Popular command-line options

○ Use man to see more options for ls


○ Why don’t we run ls with different options? What can you see?

7
Brief Review of the File System Tree

< A tree of directories >

● Listing other directories


○ A disk is organized as a tree of directories, each of which contains files or
directories
○ In Unix/Linux, every file on the system is located somewhere in a single tree of
directories
○ This simplicity simplifies writing ls: only thinking about directories and files
8
How Does ls Work?
● What is a directory?

○ A directory is a kind of file that contains a list of names of files and


directories
○ Unlike a regular file, a directory is never empty
○ Every directory contains two specific items
■ dot (.) is the name of the current directory
■ dotdot (..) is the name of the directory one level up

9
How Does ls Work? (cont.)
● Reading the contents of a directory

< Reading entries from a directory >

10
How Does ls Work? (cont.)
● The dirent structure
○ Typically defined /usr/include/dirent.h

■ An inode (index node) serves as a unique identifier for a specific piece of metadata on
a given filesystem
■ d_name contains the null-terminated filename

11
Can I Write ls? (ls1.c)
● Logic for listing a directory:

12
Writing ls -l
● ls -l operation
○ Mode: type of file (-, d) + permissions (user, group, everyone (other))
○ Links: reference to a file (often used in Linux)
○ Owner
○ Group
○ Size
○ Last-modified
○ Name

13
Writing ls -l (cont.)
● Needs a system call, called “stat”:

< Reading file properties using stat >

○ Obtains the information about a given file


○ Interface: stat(name, ptr)
14
Writing ls -l (cont.)
● System call “stat”:

15
Writing ls -l (cont.)
● ‘statinfo.c’ : to show file properties via stat()

16
Writing ls -l (cont.)
● Results of statinfo

17
Converting File Mode to a String

● st_mode includes field type and permission bits


● We convert file mode into a 16-bit value, called subfield coding
● To read subfields, we need to use a technique, called masking
○ Zeroing out digits (making zeros), only subfield show through
○ Set marks to translate st_mode into string displayed by ls -l

18
Converting File Mode to a String (cont.)
● Subfield coding

● Masking

○ By masking a bit, focus will be reduced; unnecessary information is hidden.

19
Converting File Mode to a String (cont.)
● Using masking to decode file type and permission bits

20
Converting User/Group ID to Strings
● Users
○ /etc/passwd is the list of users
■ But that does not always present a complete list of users
○ A library function getpwuid provides access to the complete list of users.
● Groups
○ /etc/group is the list of group
○ A user can belong to more than one group
○ Function getgrgid provides access to list of group

21
Putting It All Together: ls2.c

22
Putting It All Together: ls2.c (cont.)

23
Putting It All Together: ls2.c (cont.)

24
Putting It All Together: ls2.c (cont.)

25
The Three Special Bits
● The st_mode member of the stat structure contains 16 bits

● The three special bits


○ The Set-User-ID bit
○ The Set-Group-ID bit
○ The Sticky bit

26
1. The Set-User-ID Bit

● That SUID bit tells the kernel to run the program as though it were being
run by the owner of the program: let’s type ‘passwd.’
○ Note that this program belongs to “root” as owner and “root” as group.
● The name ‘set user ID’ comes from the fact that the bit causes the kernel
to set the effective user ID to the user ID of the owner of the program
○ User root owns /etc/passwd, so a program running as root can modify the file

27
2. The Set-Group-ID Bit

● The second special bit sets the effective group ID of a program


○ Effective group ID: a number assigned by the kernel to a group
○ If a program belongs to group g and the set group ID bit is set, then the
program runs as though it were being run by a member of g
○ This bit grants the program the access rights of members of that group

28
3. The Sticky Bit
● For files
○ The sticky bit tells the kernel to keep the program on the “swap device”
(somewhere on disk) even if nobody was using it right now
■ The program gets never fragmented on the swap device on the disk
○ If the bit is “unset,” then the program might be split into many small
sections scattered across the disk
■ So, the loading time of the program may get longer than when the bit is unset
○ Typically, loading a program from the swap device is faster than that from
the regular section of the disk
■ By having the bit set to the file associated with the program, we can load the program
quicker than otherwise

29
3. The Sticky Bit (cont.)

● For directories
○ Some directories are designed to hold “temporary” files
○ These scratch (temp) directories, notably /tmp, are publicly writable, allowing
any user to create and delete any files there
○ Setting the sticky bit overrides the publicly writable attribute for a directory
■ To avoid deletion of a folder and its content by other users
○ Files in the directory with the sticky bit set may only be deleted by their owners
■ “No one can delete things in my directory!”

30
Setting and Modifying the Properties of a File
● 1) Type of a file
○ A file has a type
■ It can be a regular file(-), a directory(d), a device file(b), a socket(s), a symbolic link(l),
or a named pipe(p) (connected processes by a pipe)
○ Establishing the “type” of a file
■ The type of the file is determined when the file is created
● For example, the creat system call creates a regular file
● Different system calls are used to create directories, devices, and the like

○ Changing the type of a file is impossible unless the file is recreated

31
Setting and Modifying the Properties of a File (cont.)
● 2) Permission bits and special bits
○ Establishing the mode of a file
○ Changing the mode of a file

32
Setting and Modifying the Properties of a File (cont.)
● 3) Number of links to a file
○ Represents the number of times the file is referenced in directories (or
somewhere in the filesystem)

33
Setting and Modifying the Properties of a File (cont.)
● 4) Changing the owner and group of a file
○ A program can modify the owner and group of a file by making the chown
system call

34
Setting and Modifying the Properties of a File (cont.)
● 5) Size of a file
○ The size of a file, directory, and named pipe represents the number of bytes
stored
● 6) Modification and access time

35
Setting and Modifying the Properties of a File (cont.)
● 7) Name of a file
○ Changing the name of a file is the ‘rename’ system call

36
Summary
● Chapter 3 continues the discussion
about files, showing there is more to
files than their contents.
● Files are stored in directories, and files
have attributes.
● Chapter 3 shows how to read a
directory, and explains how to read,
modify, and understand file attributes.
● The project for the chapter is to
understand and write a version of the ls
command.
37
Appendix
gdb Debugger

38
What is gdb?
● “GNU Debugger”
● A debugger for several languages, including C and C++
● It allows you to inspect what the program is doing at a certain point
during execution
● Errors like segmentation faults may be easier to find with the help of
gdb
● Online manual
○ https://sourceware.org/gdb/current/onlinedocs/gdb/

39
gdb Debugger
● GDB: interactive debugger
○ Allows the user to run a program and interactively examine its execution.
○ Features include:
■ breakpoints (“run until control reaches here, then prompt user”)
■ stack backtrace (chain of calls leading to some point in the code)
■ examination of program variables

● Compile option for debug


○ You have to use “–g” option when you compile a code
○ The (g)cc -g flag tells (g)cc to generate and embed debug info.
● Running gdb
○ $ gdb program
○ Caution: argument(program) is a program NOT a source code
40
gdb Commands
● run [arglist]
○ Run a program with a new argument (abbreviated command: r)
● break {[file:] function | line number }
○ Stop at a line that a user specifies for break. (abbreviated command: b)
● continue
○ Continue to run a program (abbreviated command: c)
● quit
○ Quit gdb (abbreviated command: q)

41
gdb Commands (cont.)
● step
○ Execute a line; if the current line has a function call, then run into the function and execute each
line of the function at a time (abbreviated command: s)
● next
○ Continue to the next source line; if that line has a function call, then execute the function without
stopping (abbreviated command: n)
● print expr
○ Perform the ‘expr’ expression and then print its value (outcome) (abbreviated: p).
● display
○ Enable automatic displaying of certain expressions each time GDB stops at a breakpoint or after a
step
● list [first, last]
○ Print lines from first to last: 10 lines by default
42
gdb Hands-on with buggy
● Run the following code
#include <stdio.h>
#include "string.h"

int tester();

int main(int argc, char* argv[]) {


int i, j, k;
int x[1000];

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


x[i] = i;
}

printf("Enter integer in 0..9999: ");


scanf("%d", &k);

tester(x, k);
}

int tester(int* c, int k) {


printf("x[%d] = %d\n", k, c[k]);
}
43
gdb Hands-on with buggy (cont.)
● Execute the following commands:
$ gcc buggy.c
$ ./a.out
Segmentation fault (core dumped)

● The program crashes


$ gdb a.out
...
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
0x00005555555551ce in main ()

44
gdb Hands-on with buggy (cont.)
● Enable debugging information
$ gcc -g buggy.c
$ ./a.out
Segmentation fault (core dumped)
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
0x00005555555551ce in main (argc=1, argv=0x7fffffffe4b8) at buggy.c:9
9 x[i] = i;

45
gdb Hands-on with buggy (cont.)
● Execute inside gdb
$ gcc -g buggy.c
$ ./a.out
Segmentation fault (core dumped)
(gdb) break buggy.c:8
Breakpoint 1 at 0x5555555551b4: file buggy.c, line 8.
(gdb) step
...
(gdb) print i
(gdb) print x[i]
(gdb) display i
(gdb) display x[i]
(gdb) list

(gdb) continue
...
(gdb) quit
46

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy