There are four process-related user IDs: Real, valid, saved, and file system user IDs.
- A real ID is the ID of the user who starts the process.
- A valid user ID is the user ID of a running process.
- The saved ID is the initial valid user ID of the process.
A valid user ID is the most important. It is the user ID that is checked during the process creden。. The real ID and saved ID can be used as a substitute. Root users can provide UID with any value, but common users can only provide real ID or saved user ID. The following are methods related to various IDS, which are not detailed:
int main(){
printf("uid:%d\n", (int)getuid());
printf("pid:%d\n", (int)getpid());
printf("sid:%d\n", (int)getsid(getpid()));
return 0;
}
This is used when creating a daemon. An example of creating a daemon is given below:
Int main (){
Pid_t PID;
Int I;
PID = fork ();
If (pid =-1 ){
Return-1;
} Else if (PID! = 0 ){
Exit (exit_success );
}
// Create a new session and Process Group
If (setsid () =-1 ){
Return-1;
}
// Set the working directory as the root directory
If (chdir ("/") =-1 ){
Return-1;
}
// Close all opened files
For (I = 0; I <= 2; I ++)
Close (I );
// Redirect FD 0, 1, 2 to/dev/null
Open ("/dev/null", o_rdwr );
DUP (0 );
DUP (0 );
Printf ("123123123123 ");
Return 0;
}