Go to the previous, next section.

Setting the User ID

This section describes the functions for altering the user ID (real and/or effective) of a process. To use these facilities, you must include the header files `sys/types.h' and `unistd.h'.

Function: int setuid (uid_t newuid)

This function sets both the real and effective user ID of the process to newuid, provided that the process has appropriate privileges.

If the process is not privileged, then newuid must either be equal to the real user ID or the saved user ID (if the system supports the _POSIX_SAVED_IDS feature). In this case, setuid sets only the effective user ID and not the real user ID.

The setuid function returns a value of 0 to indicate successful completion, and a value of -1 to indicate an error. The following errno error conditions are defined for this function:

EINVAL
The value of the newuid argument is invalid.

EPERM
The process does not have the appropriate privileges; you do not have permission to change to the specified ID.

Function: int setreuid (uid_t ruid, uid_t euid)

This function sets the real user ID of the process to ruid and the effective user ID to euid. If ruid is -1, it means not to change the real user ID; likewise if euid is -1, it means not to change the effective user ID.

The setreuid function exists for compatibility with 4.3 BSD Unix, which does not support saved IDs. You can use this function to swap the effective and real user IDs of the process. (Privileged processes are not limited to this particular usage.) If saved IDs are supported, you should use that feature instead of this function. See section Enabling and Disabling Setuid Access.

The return value is 0 on success and -1 on failure. The following errno error conditions are defined for this function:

EPERM
The process does not have the appropriate privileges; you do not have permission to change to the specified ID.

Go to the previous, next section.