gtpc2m6gC/C++ Language Support User's Guide

setuid-Set the Real User ID

This function sets the real, effective, or saved set-user-IDs (UIDs) for the current process to uid.

Format

#include <unistd.h>
int setuid(uid_t uid);

uid
The intended new UID value.

Normal Return

If successful, the setuid function returns 0 and sets the real, effective, or saved set-user-IDs for the current process to uid.

Error Return

If unsuccessful, the setuid function returns -1 and sets errno to one of the following:

EINVAL
The value of uid is not valid.

EPERM
The process does not have appropriate privileges and uid does not match the real user ID or the saved set-user-ID.

Programming Considerations

Examples

The following example changes the real, effective, and saved-set-UIDs.

#include <unistd.h>
#include <stdio.h>
 
int main(void) {
 
  printf ( "prior to setuid(), uid = %d, effective uid = %d\n"
     (int) getuid(), (int) geteuid() );
   if ( setuid(25) != 0 )
        perror( "setuid() error" );
    else
      printf ( "after setuid(),   uid = %d, effective uid = %d\n",
        (int) getuid(), (int) geteuid() );
  return 0;
}

Related Information