Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / setgroups.c
blobdd74cd2864af1cff672e43b6202b90e09f03d67d
1 /* Copyright (C) 1997,1998,2000,2002,2004,2006,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <grp.h>
21 #include <unistd.h>
22 #include <sys/types.h>
24 #include <sysdep.h>
25 #include <sys/syscall.h>
26 #include <bp-checks.h>
28 #include <setxid.h>
29 #include <linux/posix_types.h>
30 #include <kernel-features.h>
33 #ifdef __NR_setgroups32
34 # if __ASSUME_32BITUIDS == 0
35 /* This variable is shared with all files that need to check for 32bit
36 uids. */
37 extern int __libc_missing_32bit_uids;
38 # endif
39 #endif /* __NR_setgroups32 */
41 /* Set the group set for the current user to GROUPS (N of them). For
42 Linux we must convert the array of groups into the format that the
43 kernel expects. */
44 int
45 setgroups (size_t n, const gid_t *groups)
47 #if __ASSUME_32BITUIDS > 0
48 return INLINE_SETXID_SYSCALL (setgroups32, 2, n, CHECK_N (groups, n));
49 #else
50 if (n > (size_t) __sysconf (_SC_NGROUPS_MAX))
52 __set_errno (EINVAL);
53 return -1;
55 else
57 size_t i;
58 __kernel_gid_t kernel_groups[n];
60 # ifdef __NR_setgroups32
61 if (__libc_missing_32bit_uids <= 0)
63 int result;
64 int saved_errno = errno;
66 result = INLINE_SETXID_SYSCALL (setgroups32, 2, n,
67 CHECK_N (groups, n));
68 if (result == 0 || errno != ENOSYS)
69 return result;
71 __set_errno (saved_errno);
72 __libc_missing_32bit_uids = 1;
74 # endif /* __NR_setgroups32 */
75 for (i = 0; i < n; i++)
77 kernel_groups[i] = (__ptrvalue (groups))[i];
78 if (groups[i] != (gid_t) ((__kernel_gid_t) groups[i]))
80 __set_errno (EINVAL);
81 return -1;
85 return INLINE_SETXID_SYSCALL (setgroups, 2, n,
86 CHECK_N (kernel_groups, n));
88 #endif
90 libc_hidden_def (setgroups)