|
CS 456 - Systems Programming
Spring 2024
|
Displaying ./code/feb07/stMode.txt
Macros and how they work (and other things about inodes)
Reading: man 7 inode
https://man7.org/linux/man-pages/man7/inode.7.html
We were shown macros in Assignment h1
Where m represents some st_mode for a file:
S_ISREG(m) regular file?
S_ISDIR(m) directory?
S_ISLNK(m) symbolic link?
each macro returns either a 1 or 0, depending on what type of file it is.
But how do these work?
the st_mode is generally represented as a 7 digit octal number (base 8).
- The leftmost three digits define the file type
- The rightmost four define the file permissions
Note that in your program, leading zeroes aren't printed by default.
So an st_mode value of 100644 will be represented here as 0100644
Let's look at file types:
The following mask values (also octal numbers) are defined for the file type
S_IFMT 0170000 bit mask for the file type bit field
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
To determine the filetype, we need to AND our st_mode value with the bit mask, and check the value
AND - Both bits need to be a 1 to get a 1
So if our st_mode value was 0100644, it would go something like this
st_mode : 0100644
S_IFMT : 0170000
Result: : 0100000
We can probably see right away without needing to convert these octal characters to binary
that the result will be 0100000, which is a regular file.
Now suppose we get an st_mode value of 40755 (or 0040755)
st_mode : 0040755
S_IFMT : 0170000
It's not as obvious here, so to see it better we can convert this to binary, which is easy to do
as each octal character represents three binary bits.
st_mode : 0040755 000 000 100 000 111 101 101
S_IFMT : 0170000 000 001 111 000 000 000 000
Answer : 000 000 100 000 000 000 000 -> 0040000
We get a value of 0040000, whis cooresponds to a directory
Then, for the macros above, it checks that value produced with the value defined for the type of
file specified to see if these are equal. If it returns 1, then the answer is yes, otherwise it's no.
###### FILE PERMISSIONS ####
Finding file permissions work much the same way
File permissions have a setting for the file owner, the group owner, and all other users
The following mask values are defined for the file mode component
of the st_mode field:
Fourth digit:
S_ISUID 04000 set-user-ID bit (see execve(2))
S_ISGID 02000 set-group-ID bit (see below)
S_ISVTX 01000 sticky bit (see below)
Last three Digits:
3rd - Owner section:
S_IRWXU 00700 owner has read, write, and execute permission
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
2nd - Group Section:
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
1st - Others section:
S_IRWXO 00007 others (not in group) have read, write, and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
These are represented here as 5-digit octal numbers, the last 4 digits determine the file-ownership
We can determine permissions in our st_mode by the following
- If digit is a 7, we know we have read-write-execute
- All other digits, we look at the table and determine what two numbers out of 4, 2, or 1 add up
to our desired digit
The second digit in 0040755 (or the group digit) is 5, so we need to determine which two numbers
out of 4, 2, or 1 add to 5, which is 4 and 1. this indicates that the group has read or execute/
|