User and group ID

Source: Internet
Author: User

There are four process-related user IDs: Real, valid, saved, and file system user IDs.

  1. A real ID is the ID of the user who starts the process.
  2. A valid user ID is the user ID of a running process.
  3. 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;
}

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.