setegid.c 724 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #define _GNU_SOURCE
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include <grp.h>
  11. #include <sys/types.h>
  12. #include <sys/syscall.h>
  13. libc_hidden_proto(setresgid)
  14. libc_hidden_proto(setregid)
  15. int setegid(gid_t gid)
  16. {
  17. int result;
  18. if (gid == (gid_t) ~0)
  19. {
  20. __set_errno (EINVAL);
  21. return -1;
  22. }
  23. #ifdef __NR_setresgid
  24. result = setresgid(-1, gid, -1);
  25. if (result == -1 && errno == ENOSYS)
  26. /* Will also set the saved group ID if egid != gid,
  27. * making it impossible to switch back...*/
  28. #endif
  29. result = setregid(-1, gid);
  30. return result;
  31. }