seteuid.c 516 B

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