setegid() — Set the effective group ID

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1a
Single UNIX Specification, Version 3

both  

Format

#define _POSIX1_SOURCE 2
#include <unistd.h>

int setegid(gid_t gid);

General description

Sets the effective group ID (GID) of a process to gid, if gid is equal to the real GID or the saved set GID of the calling process, or if the process has appropriate privileges. The real GID, the saved set GID, and any supplementary GIDs are not changed.

Returned value

If successful, setegid() returns 0.

If unsuccessful, setegid() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
The value specified for gid is incorrect and is not supported by the implementation.
EPERM
The process does not have appropriate privileges, and gid does not match the real GID or the saved set GID.

Example

CELEBS02
⁄* CELEBS02

   This example changes your effective GID.

 *⁄
#define _POSIX1_SOURCE 2
#include <unistd.h>
#include <stdio.h>

main() {
  printf("your effective group id is %d\n", (int) getegid());
  if (setegid(500) != 0)
    perror("setegid() error");
  else
    printf("your effective group id was changed to %d\n",
           (int) getegid());
}
Output
your effective group id is 512
your effective group id was changed to 500

Related information