initgroups.c\misc\src - musl - musl - an implementation of the standard library for Linux-based systems

index : musl
musl - an implementation of the standard library for Linux-based systems
summary refs log tree commit diff
path: root/src/misc/initgroups.c
blob: 101f5c7b0363a0d8a71dad1fd12d0c6668ea449a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define _GNU_SOURCE
#include <grp.h>
#include <limits.h>
#include <stdlib.h>
int initgroups(const char *user, gid_t gid)
{
	gid_t buf[32], *groups = buf;
	int count = sizeof buf / sizeof *buf, prev_count = count;
	while (getgrouplist(user, gid, groups, &count) < 0) {
		if (groups != buf) free(groups);
		/* Return if failure isn't buffer size */
		if (count <= prev_count)
			return -1;
		/* Always increase by at least 50% to limit to
		 * logarithmically many retries on TOCTOU races. */
		if (count < prev_count + (prev_count>>1))
			count = prev_count + (prev_count>>1);
		groups = calloc(count, sizeof *groups);
		if (!groups) return -1;
		prev_count = count;
	}
	int ret = setgroups(count, groups);
	if (groups != buf) free(groups);
	return ret;
}
generated by cgit v1.2.1 (git 2.18.0) at 2025年09月08日 22:27:24 +0000

AltStyle によって変換されたページ (->オリジナル) /