setgroups.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * setgroups() for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include <sys/syscall.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <grp.h>
  13. #ifdef __USE_BSD
  14. #if defined(__NR_setgroups32)
  15. # undef __NR_setgroups
  16. # define __NR_setgroups __NR_setgroups32
  17. _syscall2(int, setgroups, size_t, size, const gid_t *, list)
  18. #elif __WORDSIZE == 64
  19. _syscall2(int, setgroups, size_t, size, const gid_t *, list)
  20. #else
  21. #define __NR___syscall_setgroups __NR_setgroups
  22. static __inline__ _syscall2(int, __syscall_setgroups,
  23. size_t, size, const __kernel_gid_t *, list)
  24. int setgroups(size_t size, const gid_t *groups)
  25. {
  26. if (size > (size_t) sysconf(_SC_NGROUPS_MAX)) {
  27. ret_error:
  28. __set_errno(EINVAL);
  29. return -1;
  30. } else {
  31. size_t i;
  32. __kernel_gid_t *kernel_groups = NULL;
  33. if (size) {
  34. kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
  35. if (kernel_groups == NULL)
  36. goto ret_error;
  37. }
  38. for (i = 0; i < size; i++) {
  39. kernel_groups[i] = (groups)[i];
  40. if (groups[i] != (gid_t) ((__kernel_gid_t) groups[i])) {
  41. goto ret_error;
  42. }
  43. }
  44. i = __syscall_setgroups(size, kernel_groups);
  45. free(kernel_groups);
  46. return i;
  47. }
  48. }
  49. #endif
  50. libc_hidden_def(setgroups)
  51. #endif