setegid.c 517 B

12345678910111213141516171819202122232425262728
  1. #define _GNU_SOURCE
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <grp.h>
  6. #include <sys/types.h>
  7. #include <sys/syscall.h>
  8. int setegid(gid_t gid)
  9. {
  10. int result;
  11. if (gid == (gid_t) ~0)
  12. {
  13. __set_errno (EINVAL);
  14. return -1;
  15. }
  16. #ifdef __NR_setresgid
  17. result = setresgid(-1, gid, -1);
  18. if (result == -1 && errno == ENOSYS)
  19. /* Will also set the saved group ID if egid != gid,
  20. * making it impossible to switch back...*/
  21. #endif
  22. result = setregid(-1, gid);
  23. return result;
  24. }