4 * Copyright (C) 2017 Denys Vlasenko
6 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //kbuild:lib-y += bb_getgroups.o
13 gid_t* FAST_FUNC bb_getgroups(int *ngroups, gid_t *group_array)
15 int n = ngroups ? *ngroups : 0;
17 /* getgroups may be a bit expensive, try to use it only once */
22 // FIXME: ash tries so hard to not die on OOM (when we are called from test),
23 // and we spoil it with just one xrealloc here
24 group_array = xrealloc(group_array, (n+1) * sizeof(group_array[0]));
25 n = getgroups(n, group_array);
27 * If buffer is too small, kernel does not return new_n > n.
28 * It returns -1 and EINVAL:
31 /* Terminator for bb_getgroups(NULL, NULL) usage */
32 group_array[n] = (gid_t) -1;
35 if (errno == EINVAL) { /* too small? */
36 /* This is the way to ask kernel how big the array is */
37 n = getgroups(0, group_array);
40 /* Some other error (should never happen on Linux) */
41 bb_simple_perror_msg_and_die("getgroups");