setgroups.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 "syscalls.h"
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <grp.h>
  13. libc_hidden_proto(setgroups)
  14. libc_hidden_proto(sysconf)
  15. #define __NR___syscall_setgroups __NR_setgroups
  16. static inline _syscall2(int, __syscall_setgroups,
  17. size_t, size, const __kernel_gid_t *, list);
  18. int setgroups(size_t size, const gid_t *groups)
  19. {
  20. if (size > (size_t) sysconf(_SC_NGROUPS_MAX)) {
  21. ret_error:
  22. __set_errno(EINVAL);
  23. return -1;
  24. } else {
  25. size_t i;
  26. __kernel_gid_t *kernel_groups = NULL;
  27. if (size) {
  28. kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
  29. if (kernel_groups == NULL)
  30. goto ret_error;
  31. }
  32. for (i = 0; i < size; i++) {
  33. kernel_groups[i] = (groups)[i];
  34. if (groups[i] != (gid_t) ((__kernel_gid_t) groups[i])) {
  35. goto ret_error;
  36. }
  37. }
  38. i = __syscall_setgroups(size, kernel_groups);
  39. if (kernel_groups)
  40. free(kernel_groups);
  41. return i;
  42. }
  43. }
  44. libc_hidden_def(setgroups)