setgroups.c 1.3 KB

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