setegid.c 753 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. #ifdef __NR_setresgid
  14. libc_hidden_proto(setresgid)
  15. #endif
  16. libc_hidden_proto(setregid)
  17. int setegid(gid_t gid)
  18. {
  19. int result;
  20. if (gid == (gid_t) ~0)
  21. {
  22. __set_errno (EINVAL);
  23. return -1;
  24. }
  25. #ifdef __NR_setresgid
  26. result = setresgid(-1, gid, -1);
  27. if (result == -1 && errno == ENOSYS)
  28. /* Will also set the saved group ID if egid != gid,
  29. * making it impossible to switch back...*/
  30. #endif
  31. result = setregid(-1, gid);
  32. return result;
  33. }