]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/common/setgroups.c
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / libc / sysdeps / linux / common / setgroups.c
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
10 #include <sys/syscall.h>
11
12 #ifdef __USE_BSD
13 #include <grp.h>
14
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
20 #elif __WORDSIZE == 64
21 _syscall2(int, setgroups, size_t, size, const gid_t *, list)
22
23 #else
24 # include <errno.h>
25 # include <stdlib.h>
26 # include <unistd.h>
27 # include <sys/types.h>
28
29 # define __NR___syscall_setgroups __NR_setgroups
30 static __always_inline _syscall2(int, __syscall_setgroups,
31                                  size_t, size, const __kernel_gid_t *, list)
32
33 int setgroups(size_t size, const gid_t *groups)
34 {
35         if (size > (size_t) sysconf(_SC_NGROUPS_MAX)) {
36 ret_error:
37                 __set_errno(EINVAL);
38                 return -1;
39         } else {
40                 size_t i;
41                 __kernel_gid_t *kernel_groups = NULL;
42
43                 if (size) {
44                         kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
45                         if (kernel_groups == NULL)
46                                 goto ret_error;
47                 }
48
49                 for (i = 0; i < size; i++) {
50                         kernel_groups[i] = (groups)[i];
51                         if (groups[i] != (gid_t) ((__kernel_gid_t) groups[i])) {
52                                 goto ret_error;
53                         }
54                 }
55
56                 i = __syscall_setgroups(size, kernel_groups);
57                 free(kernel_groups);
58                 return i;
59         }
60 }
61 #endif
62
63 libc_hidden_def(setgroups)
64 #endif