Atexit function and two special file permission bits. atexit function permission
The prototype of the atexit function is void atexit (void (* func) (void ))It is a parameterThe return value and parameters are empty.Function pointer FunctionIt indicates the function pointed to by the parameter function pointer executed before the current process ends. When using the function, you must register it in main. Many functions can be registered at one time, the execution sequence of functions is related to the order of registration,The opposite is true,The first registered function is the last execution. The following is the execution instance.
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 5 void func1() 6 { 7 printf("this is func1\n"); 8 } 9 10 void func4()11 {12 printf("this is func4\n");13 14 }15 16 17 void func3()18 {19 printf("this is func3\n");20 }21 22 23 void func2()24 {25 printf("this is func2\n");26 }27 28 void func0()29 {30 printf("this is func0\n");31 }32 void func6()33 {34 printf("this is func6\n");35 }36 void a()37 {38 printf("this is a\n");39 }40 void z()41 {42 printf("this is z\n");43 }44 45 46 int main()47 {48 atexit(func3);49 atexit(func1);50 atexit(func2);51 52 atexit(func4);53 atexit(func0);54 atexit(func6);55 atexit(a);56 atexit(z);57 return 0;58 }
This is the execution result
You can see some information by running the ps-l command. PID and PPID are not detailed about the idnumbers of the process and the parent process, respectively, what are the meanings of UID and related EUID and SUID?
UID: indicates the actual user ID, which is also displayed. Currently, the UID displayed by the ROOT user is 0, which is usually larger than 0, my general user UID is 1000
EUID: valid user ID. Generally, the uid and euid are the same...
SUID: Indicates setting the user ID (it can only be used in executable programs, because the x-bit of the permission will change to s after use). This is a different time! After you set this bit, your euid will be promoted to the owner of the file when you execute the file. In this example, the passwd command is used. It is reasonable to say that only root has the permission to change the user password, but common users can also change the password by using passwd, that is, setting the user ID.
The implementation method is as follows: first, create a log file without any Permissions
Obviously, it is illegal to access it, and it is not possible to write something into it. Now I try to open it with a file pointer and write something into it.
1 #include<stdio.h> 2 3 4 #include<string.h> 5 6 7 int main() 8 { 9 FILE *fp;10 printf("uid:%d euid:%d",getuid(),geteuid());11 fp=fopen("log","w");12 if(fp==NULL)13 {14 printf("fopen error");15 }16 char *buffer="hello world";17 18 fwrite(buffer,1,strlen(buffer),fp);19 20 return 0;21 }
Run the program as a common user
Error reported directly, rejected
Set User ID
Run the command again and write the data successfully. We found that the printed uid is different from the euid. Although the actual user identity is a common user, the current valid user identity is root! In this way, euid is easy to understand. It is just an identity and cannot represent actual characters, just like a fake name.
What is the sticky position of a file? What is the function?
The sticky bits of common files are ignored by the operating system kernel. After a directory file is set, the files in this directory can only be deleted by the owner and root., The sticky position appears on the executable position, represented by t,After this bit is set, you cannot delete directories and files that do not belong to it.
For example, the tmp directory is set to a sticky bit. I created a subdirectory file in the/tmp directory.
Look! The tmp directory file has a sticky bit.
Creates a common test file and a dirtest directory file, and deletes the file.
The request is rejected... Try to use-rf force Delete
The command to set the sticky bit is also rejected.
chmod 777 dirtestchmod +t dirtest
This can also be used.
chmod 1777 dirtest
Setting a sticky bit for a common file is useless. It turns to T and is finally ignored.