setgroups.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. libc_hidden_proto(setgroups)
  15. #if defined(__NR_setgroups32)
  16. # undef __NR_setgroups
  17. # define __NR_setgroups __NR_setgroups32
  18. _syscall2(int, setgroups, size_t, size, const gid_t *, list)
  19. #elif __WORDSIZE == 64
  20. _syscall2(int, setgroups, size_t, size, const gid_t *, list)
  21. #else
  22. libc_hidden_proto(sysconf)
  23. #define __NR___syscall_setgroups __NR_setgroups
  24. static __inline__ _syscall2(int, __syscall_setgroups,
  25. size_t, size, const __kernel_gid_t *, list)
  26. int setgroups(size_t size, const gid_t *groups)
  27. {
  28. if (size > (size_t) sysconf(_SC_NGROUPS_MAX)) {
  29. ret_error:
  30. __set_errno(EINVAL);
  31. return -1;
  32. } else {
  33. size_t i;
  34. __kernel_gid_t *kernel_groups = NULL;
  35. if (size) {
  36. kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
  37. if (kernel_groups == NULL)
  38. goto ret_error;
  39. }
  40. for (i = 0; i < size; i++) {
  41. kernel_groups[i] = (groups)[i];
  42. if (groups[i] != (gid_t) ((__kernel_gid_t) groups[i])) {
  43. goto ret_error;
  44. }
  45. }
  46. i = __syscall_setgroups(size, kernel_groups);
  47. free(kernel_groups);
  48. return i;
  49. }
  50. }
  51. #endif
  52. libc_hidden_def(setgroups)
  53. #endif