user-util: sysconf_ngroups_max

This hides the iffy interface of sysconf(_SC_NGROUPS_MAX) to simplify
the calllers.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-07-02 17:19:18 +02:00
parent aac88fd8ab
commit 2c01b4895e
6 changed files with 23 additions and 14 deletions
+2 -1
View File
@@ -39,6 +39,7 @@
#include "strv.h"
#include "sysctl-util.h"
#include "tmpfile-util.h"
#include "user-util.h"
#include "xattr-util.h"
#if ENABLE_IDN
@@ -914,7 +915,7 @@ int getpeergroups(int fd, gid_t **ret) {
assert(fd >= 0);
assert(ret);
long ngroups_max = sysconf(_SC_NGROUPS_MAX);
int ngroups_max = sysconf_ngroups_max();
if (ngroups_max > 0)
n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
+13
View File
@@ -1353,6 +1353,19 @@ int lookup_grent_in_files(
return -ESRCH;
}
int sysconf_ngroups_max(void) {
/* Query sysconf _SC_NGROUPS_MAX. Returns an int because the expected value is 64k
* and later on this is used as an int with various glibc consumers. */
errno = 0;
long ngroups_max = sysconf(_SC_NGROUPS_MAX);
if (ngroups_max <= 0)
return errno_or_else(EOPNOTSUPP);
if (ngroups_max > INT_MAX)
return -ERANGE;
return ngroups_max;
}
static size_t getgr_buffer_size(void) {
long bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
return bufsize <= 0 ? 4096U : (size_t) bufsize;
+2
View File
@@ -173,6 +173,8 @@ static inline bool hashed_password_is_locked_or_invalid(const char *password) {
*/
#define PASSWORD_UNPROVISIONED "!unprovisioned"
int sysconf_ngroups_max(void);
int lookup_pwent_in_files(
char * const *files,
const char *name,
+3 -8
View File
@@ -917,14 +917,9 @@ static int get_supplementary_groups(
return 0;
}
/*
* If SupplementaryGroups= was passed then NGROUPS_MAX has to
* be positive, otherwise fail.
*/
errno = 0;
long ngroups_max = sysconf(_SC_NGROUPS_MAX);
if (ngroups_max <= 0)
return errno_or_else(EOPNOTSUPP);
int ngroups_max = sysconf_ngroups_max();
if (ngroups_max < 0)
return ngroups_max;
_cleanup_free_ gid_t *l_gids = new(gid_t, ngroups_max);
if (!l_gids)
+2 -2
View File
@@ -1038,7 +1038,7 @@ TEST(condition_test_group) {
char gid[DECIMAL_STR_MAX(uint32_t)];
gid_t *gids, max_gid;
int ngroups, r, i;
long ngroups_max;
int ngroups_max;
xsprintf(gid, "%u", UINT32_C(0xFFFF));
ASSERT_NOT_NULL((condition = condition_new(CONDITION_GROUP, gid, false, false)));
@@ -1054,7 +1054,7 @@ TEST(condition_test_group) {
ASSERT_OK_POSITIVE(r);
condition_free(condition);
ngroups_max = ASSERT_OK_ERRNO(sysconf(_SC_NGROUPS_MAX));
ngroups_max = ASSERT_OK(sysconf_ngroups_max());
ASSERT_GT(ngroups_max, 0);
gids = newa(gid_t, ngroups_max);
+1 -3
View File
@@ -175,12 +175,10 @@ TEST(getpeercred_getpeergroups) {
assert_se(fully_set_uid_gid(test_uid, test_gid, test_gids, n_test_gids) >= 0);
} else {
long ngroups_max;
test_uid = getuid();
test_gid = getgid();
ngroups_max = sysconf(_SC_NGROUPS_MAX);
int ngroups_max = sysconf_ngroups_max();
assert_se(ngroups_max > 0);
test_gids = newa(gid_t, ngroups_max);