setegid.c 576 B

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