[Previous]
[Contents]
[Next]

setuid()

set the real, effective and saved user IDs

Synopsis:

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

Description:

The setuid() function allows the calling process to set the real, effective and saved user IDs based on the following:

If you wish to change only the effective user ID, even if you have appropriate privileges, you should consider using the seteuid() function.

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error.

Errors:

EINVAL
The value of uid is invalid.
EPERM
The process doesn't have appropriate privileges, and uid doesn't match the real user ID.

Examples:

/*
 * this process sets its userid to 0 (root)
 */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

void main()
  {
    uid_t ouid;

    ouid = getuid();
    if( setuid( 0 ) == -1 ) {
      perror( "setuid" );
      exit( EXIT_FAILURE );
    }
    printf( "userid %d switched to 0\n", ouid );
    exit( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.1

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

errno, setegid(), seteuid(), setgid()


[Previous]
[Contents]
[Next]