[Apue] Files and directories

Source: Internet
Author: User

One, apue the various obscure nouns in this chapter

I was reading this chapter encountered a variety of ID, according to the name is completely unclear what meaning, fortunately see this article, http://blog.csdn.net/ccjjnn19890720/article/details/6990656, summarize

Each process actually corresponds to more than 6 IDs, which are: the actual user ID, the actual group ID ( who we are actually, the users and groups executing the program ), the valid user ID, the valid group ID, the additional group ID ( for file access check ), Save set User ID, save setting Group ID ( saved by exec function )

  actual user ID/actual group IDs: The ID of the current execution of this process, such as I am now a orlion user, then I execute the Foo program, then the actual user ID of this foo process is orlion. Group similarly

  Valid userID: This ID is an ID that UNIX has been using, because even if you are just a very simple access file, that is to pass this valid user ID, because each file has a certain access rights, and a process or a program to access it, The operating system itself is given a certain permission based on your valid user ID.

  What is the difference between the actual user ID and the valid user ID ?! : These two IDs are the same in general, such as the current user is orlion, then its actual user ID is orlion, and the valid user ID is orlion. But in unusual circumstances then these two IDs may be different, then what kind of situation is not the same? That's what happens when a user wants to make a reasonable privilege, so what's the situation?

For example, we in the Linux system passwd This command or this passwd this program, a user to change their password is a very normal thing, but the password to save the file/etc/passwd is the root user can write such a right, So that is the use if you want to change the password, you have to change the root user to help you

The handling of this thing is so that when the user goes to run passwd this program, the OS gives the root user the right, then the user can modify their own password. Specifically, to allow users to run passwd this program, UNIX will be its valid user ID into the owner of the passwd user ID, that is, root, so you can modify the/etc/passwd this file.

  Save Settings User ID: This ID is used to save a copy of the valid ID, let us run the program is actually the OS call the EXEC series function to invoke the main function of our program, the EXEC function is kernel the only way to execute the program, Or so, no matter what the user program runs, in fact, the OS exec call process. and exec copies the program's valid user ID to the Save User ID during the call.

  File Settings User ID bit: Each file has a file mode word (St_mode), the word can be obtained through the stat function, and this pattern word contains many properties of the file, including the file type, as well as the access rights of the file, of course, set the user ID bit in it. By setting this bit, the process's valid ID is set to the user of the file itself when the file is executed, where the file can be considered an executable file, and when the file is run, the process changes its valid user ID to the ID of the file itself.

  In the terminal we view/usr/bin/passwd this file

$ ll/usr/bin/passwd130768 February   /usr/bin/passwd

You can see that there is an S permission, which is the flag that sets the user ID bit set.

  

#include <stdio.h>#include<stdlib.h>#include<sys/Stat.h>#include<unistd.h>intMain () {printf ("Real User ID =%d\n", Getuid ()); printf ("effect User ID =%d\n", Geteuid ()); Return0; }
Execution in Terminal: orlion$./main Real User ID= +Effect User ID= +orlion$suroot#ChownRoot main root#chmodu+S main root# ll main-rwsr-xr-x1Root Orlion9809 .- A- + A: -main root# exit orlion$ exit orlion$./main Real User ID= +Effect User ID=0

From the above you can see that the valid user ID has changed

Second, chmod and FCHMOD functions

These two functions allow us to change the access permission (permissions) of the existing file

#include <sys/types.h><sys/stat.h>int chmod (constChar *  Pathname, mode_t mode); int fchmod (int fileds, mode_t mode); Fileds is the file handle

Return Value: 0 if successful, or 1 if there is an error

In order to change the permissions of a file, the valid user ID of the process must be equal to the owner of the file, or have root permissions

The parameter mode is the bitwise OR operation shown below

  

mode                Description Ming s_isuid            -user-ids_isgid            -Group-ids_isvtx            save body s_irwxu          User (owner) read, Write and execute S_IRUSR user (owner)           read S_IWUSR user (owner)          write s_ixusr           User (owner) perform s_irwxg          group read, write, and execute S_IRGRP           Group Read s_iwgrp          Group write s_ixgrp           Group execution
S_irwxo Other read, write, and execute
S_iroth Other Reading
S_iwoth Other Write
S_ixoth Other executions

Instance:

#include <stdio.h>#include<sys/stat.h>intMainvoid){    structstat statbuf; if(Stat ("Foo", &statbuf) <0) {fprintf (stderr,"stat error for foo\n"); }    if(Chmod ("Foo", (Statbuf.st_mode &-S_IXGRP) | S_isgid) <0) {fprintf (stderr,"chmod error for foo\n"); }    /*set Absolute mode to "rw-r--r--"*/    if(Chmod ("Bar", S_IRUSR | S_IWUSR | S_irgrp | S_iroth) <0) {fprintf (stderr,"chmod error for bar\n"); }    return 0;}

Third, sticking bit
S_isvtx bit in the previous section, one of the earlier versions of Unix was called the Stick bit (sticky bit). If this bit of an executable program file is set, a text of the program body is saved in the swap area the first time the program executes and ends. (The body part of the program is the machine Instruction section.) This enables the next time the program executes, it can be loaded into the memory area more quickly. The reason: In the swap area, the file is stored continuously, and in the general U N I x File System, the file's data blocks are likely to be stored randomly. Common applications, such as text-editing programs and parts of the compiler, often set the stick bits of the files they reside in. Later versions of Unix are known as the Save-body bit (saved-text bit), so there is a constant S _ I s V T X. Most newer UNIX systems today have virtual storage systems and fast file systems, so it is no longer necessary to use this technology.
S V R 4 and 4. 3 + B S is the main target directory for sticking bits. If a stick bit is set on a directory, only the user who has write permission to the directory and one of the following conditions can delete or rename the file in that directory:
? owns this file.
? owns this directory.
? is super user.
The directory/TMP and/var/spool/uucppublic are candidates for setting the stick bit-both directories where any user can create files. These two directories are usually read, write, and execute for any user (user, group, and other) permissions. However, users should not be able to delete or rename files belonging to other people, and for this reason they have a sticky bit set in the file mode of both directories.

Iv. Chown, Fchown, and Lchown functions

#include <sys/types.h><unistd.h>int chown (constChar *pathname , uid_t owner, gid_t Group); int fchown (int  filedes, uid_t owner, gid_t Group); int lchown (constChar *Pathname, uid_t owner, gid_t Group); return value: Success 0, Failure -1 .

Lchown change the owner of the symbolic connection itself, not the file that the symbolic link points to

Depending on the value of _posix_chown_restricted, poisx.1 can choose that only superuser can change the owner of a file or any user can modify the owner of the file they own.

  If _posix_chown_restricted works on the specified file, the

(1) Only the Superuser process can change the user ID of the file.
(2) A non-superuser process can change the group ID of the file if the following conditions are true:
(a) The process owns this file (its valid user ID equals the user ID of the file).
(b) The parameter owner equals the user ID of the file, and the parameter group equals the valid group ID of the process or one of the added group IDs of the process.
This means that when _ P o s i X _ C H o W N _ r e S t r I C T e D is valid, the user ID of the other user's files cannot be changed. You can change the group ID of the file you're holding, but only change to the group you belong to.
If these functions are called by non-superuser processes, the file's settings-user-id bit and settings-group-id bits are cleared on successful return.

V. Length of files
The member of the stat structure st_size contains the length of the file in bytes. This field is only meaningful for normal files, directories, and symbolic connections.
For a directory file length is usually a number of the city, example 16 or 512 of an integer multiple;
For symbolic connections, the file length is the length of the actual file.

Vi. Document Truncation
Sometimes we need to truncate some data at the end of the file to shorten the file, and the truncated file can call the following function

#include <sys/types.h><unistd.h>int truncate (constChar *  Pathname, off_t length); int ftruncate (int  filedes, off_t length); return value: Success 0, Failure -1.

These two functions truncate the length of the file as long, and if the length of the file is longer than long, the data beyond the length is not accessed, and the result is system-related if the previous length is less than. If the processing of a system is to extend the file, then the end of the old file and the end of the new file will be read as 0.

Vii. File System

Traditional UNIX System V file system, you can divide a hard disk into multiple partitions, each partition can contain a file system

  

The I node is a fixed-length record entry that contains information about the file.

  

    • There are two directories pointing to the same I node. There is a connection count in each I node whose value is the number of directory entries that point to the I node. The file can be deleted only if the connection count is 0 o'clock (that is, the block of data that the file occupies). In the stat structure, the connection count is contained in St_nlink and its base system data type is nlink_t. This connection is called a hard connection. The POSIX.1 constant Link_max specifies the maximum value for a file connection.
    • Another connection is a symbolic connection (symbolic link). For such a connection, the actual content of the file (in the data block) contains the name of the file to which the symbolic connection points.
    • The I node contains all the information about the file: The file type, the file access License, the length of the file, and the pointer to the data block that the file occupies. Most of the information in the stat structure is taken from the I node. Only two items of data are stored in the catalog entry: The file name and the I node number. The data type of the I-node number is ino_t.
    • Because the number of I nodes in a catalog item points to the I node in the same file system, you cannot make one directory entry point to the I node of another file system. (So the ln command cannot cross the file system)
    • When renaming a file without changing the file system, the actual contents of the file are not moved, simply construct a new directory entry that points to the existing I node.

Connection count fields for catalog files: Suppose we create a TestDir directory:

mkdir TestDir

Shows the result, explicitly showing the. and. Catalog items

  

A 2549 I node whose Type field indicates that it is a directory, and its Type field indicates that it is a directory. And the connection count is 2. Any leaf directory (a directory that does not contain any directories) its connection count is always 2, and the value 2 comes from the directory that names the directory (TestDir) and the. Item in that directory. An I node numbered 1267, whose Type field indicates that it is a directory and its connection count is greater than or equal to 3. It is greater than or equal to 3 because there are at least three directory entries pointing to it: A directory entry that names it, a. Item in that directory, and a third in the TestDir from the subdirectory. Item

[Apue] files and directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.