blob: ddc2caee46e355a0ff9d056e3007766f1510904c [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* vi: set sw=4 ts=4: */
2/*
3 * getgroups() 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
16#if defined(__NR_getgroups32)
17# undef __NR_getgroups
18# define __NR_getgroups __NR_getgroups32
19_syscall2(int, getgroups, int, size, gid_t *, list)
20
21#elif __WORDSIZE == 64
22_syscall2(int, getgroups, int, size, gid_t *, list)
23
24#else
25
26#define MIN(a,b) (((a)<(b))?(a):(b))
27
28#define __NR___syscall_getgroups __NR_getgroups
29static __inline__ _syscall2(int, __syscall_getgroups,
30 int, size, __kernel_gid_t *, list)
31
32int getgroups(int size, gid_t groups[])
33{
34 if (unlikely(size < 0)) {
35ret_error:
36 __set_errno(EINVAL);
37 return -1;
38 } else {
39 int i, ngids;
40 __kernel_gid_t *kernel_groups;
41
42 size = MIN(size, sysconf(_SC_NGROUPS_MAX));
43 kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
44 if (size && kernel_groups == NULL)
45 goto ret_error;
46
47 ngids = __syscall_getgroups(size, kernel_groups);
48 if (size != 0 && ngids > 0) {
49 for (i = 0; i < ngids; i++) {
50 groups[i] = kernel_groups[i];
51 }
52 }
53
54 free(kernel_groups);
55 return ngids;
56 }
57}
58#endif
59
60libc_hidden_def(getgroups)