blob: 72c987837ffee5c8ce5af2aa3d1f74670b198144 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* 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#include <stdlib.h>
12#include <unistd.h>
13#include <grp.h>
14
15#ifdef __USE_BSD
16
17
18#if defined(__NR_setgroups32)
19# undef __NR_setgroups
20# define __NR_setgroups __NR_setgroups32
21_syscall2(int, setgroups, size_t, size, const gid_t *, list)
22
23#elif __WORDSIZE == 64
24_syscall2(int, setgroups, size_t, size, const gid_t *, list)
25
26#else
27
28
29#define __NR___syscall_setgroups __NR_setgroups
30static __inline__ _syscall2(int, __syscall_setgroups,
31 size_t, size, const __kernel_gid_t *, list)
32
33int setgroups(size_t size, const gid_t *groups)
34{
35 if (size > (size_t) sysconf(_SC_NGROUPS_MAX)) {
36ret_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
63libc_hidden_def(setgroups)
64#endif