setgid() — Set the group ID
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
Format
#define _POSIX1_SOURCE 2
#include <unistd.h>
int setgid(gid_t gid);
General description
Sets one or more of the group IDs (GIDs) for the current process to gid.
If gid is the same as the process's real GID or the saved set-group-ID, setgid() always succeeds and sets the effective GID to gid.
If gid is not the same as the process's real GID, setgid() succeeds only if the process has appropriate privileges. If the process has such privileges, setgid() sets the real GID, the effective GID, and saved set GID to gid.
setgid() does not change any supplementary GIDs of the calling process.
Returned value
If successful, setgid() returns 0.
If unsuccessful, setgid() returns -1 and sets errno
to one of the following values:
- Error Code
- Description
- EINVAL
- The value of gid is incorrect.
- EPERM
- The process does not have appropriate privileges to set the GID.
Example
CELEBS06
/* CELEBS06
This example sets your GID.
*/
#define _POSIX_SOURCE 1
#include <unistd.h>
#include <stdio.h>
main() {
printf("your group id is %d\n", (int) getgid());
if (setgid(500) != 0)
perror("setgid() error");
else
printf("your group id was changed to %d\n",
(int) getgid());
}
Output
your group id is 512
your group id was changed to 500