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,