]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/sysconf: Move sysconf handling into own file
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 13 Mar 2026 16:23:45 +0000 (17:23 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Fri, 13 Mar 2026 20:15:39 +0000 (21:15 +0100)
Move sysconf() calls and error handling into its own file.

While at it, adjust the data type for ngroup max value.

Requested-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
lib/Makefile.am
lib/chkname.c
lib/chkname.h
lib/sysconf.c [new file with mode: 0644]
lib/sysconf.h [new file with mode: 0644]
src/login.c
src/useradd.c
src/usermod.c
tests/unit/Makefile.am

index 72a57ad94b7e11cbc19a0128c6e24576122451a4..0727548192715a352a7684de51a2554936ef34d8 100644 (file)
@@ -262,6 +262,8 @@ libshadow_la_SOURCES = \
        subordinateio.h \
        subordinateio.c \
        sulog.c \
+       sysconf.c \
+       sysconf.h \
        time/day_to_str.c \
        time/day_to_str.h \
        ttytype.c \
index 0abee4d2942aac9a7dee8a525a8fd2060000c518..0b6d9409cc9866a77a93878cc6c26db5b214ac4f 100644 (file)
 #include "string/ctype/strisascii/strisdigit.h"
 #include "string/strcmp/streq.h"
 #include "string/strcmp/strcaseeq.h"
-
-
-#ifndef  LOGIN_NAME_MAX
-# define LOGIN_NAME_MAX  256
-#endif
+#include "sysconf.h"
 
 
 int allow_bad_names = false;
 
 
-size_t
-login_name_max_size(void)
-{
-       long  conf;
-
-       conf = sysconf(_SC_LOGIN_NAME_MAX);
-       if (conf == -1)
-               return LOGIN_NAME_MAX;
-
-       return conf;
-}
-
-
 static bool
 is_valid_name(const char *name)
 {
index 40d004cc02c58379973dda5e38572714f5d9ea6a..1f8e4e08221ebd9e055c4c53b5df0a3eda9059ab 100644 (file)
 #include "config.h"
 
 #include <stdbool.h>
-#include <stddef.h>
 
 
-extern size_t login_name_max_size(void);
 extern bool is_valid_user_name (const char *name);
 extern bool is_valid_group_name (const char *name);
 
diff --git a/lib/sysconf.c b/lib/sysconf.c
new file mode 100644 (file)
index 0000000..a9517dd
--- /dev/null
@@ -0,0 +1,43 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2026, Tobias Stoeckmann <tobias@stoeckmann.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include "config.h"
+
+#include <limits.h>
+#include <unistd.h>
+
+
+#ifndef  LOGIN_NAME_MAX
+# define LOGIN_NAME_MAX  256
+#endif
+
+#ifndef  NGROUPS_MAX
+# define NGROUPS_MAX  65536
+#endif
+
+
+size_t
+login_name_max_size(void)
+{
+       long  conf;
+
+       conf = sysconf(_SC_LOGIN_NAME_MAX);
+       if (conf == -1)
+               return LOGIN_NAME_MAX;
+
+       return conf;
+}
+
+size_t
+ngroups_max_size(void)
+{
+       long  conf;
+
+       conf = sysconf(_SC_NGROUPS_MAX);
+       if (conf == -1)
+               conf = NGROUPS_MAX;
+
+       return conf;
+}
diff --git a/lib/sysconf.h b/lib/sysconf.h
new file mode 100644 (file)
index 0000000..467ef32
--- /dev/null
@@ -0,0 +1,18 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2026, Tobias Stoeckmann <tobias@stoeckmann.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef _SYSCONF_H_
+#define _SYSCONF_H_
+
+
+#include "config.h"
+
+#include <stddef.h>
+
+
+extern size_t login_name_max_size(void);
+extern size_t ngroups_max_size(void);
+
+#endif  // include guard
index b5b82f3a339422978c76db248f2efa7c5badedd3..87bfff69cf45480cad80b8123fde58f96d4851a2 100644 (file)
@@ -48,6 +48,7 @@
 #include "string/strdup/strdup.h"
 #include "string/strerrno.h"
 #include "string/strftime.h"
+#include "sysconf.h"
 
 
 #ifdef USE_PAM
index 5534e1d860ff5658d08f823a8e5ea8409714980d..397d45f46e5bf18311d54ff0e1c21330a2ace7a9 100644 (file)
@@ -72,6 +72,7 @@
 #include "string/strdup/strdup.h"
 #include "string/strerrno.h"
 #include "string/strtok/stpsep.h"
+#include "sysconf.h"
 
 
 #ifndef SKEL_DIR
@@ -153,7 +154,7 @@ static bool pw_locked = false;
 static bool gr_locked = false;
 static bool spw_locked = false;
 static char **user_groups;     /* NULL-terminated list */
-static long sys_ngroups;
+static size_t sys_ngroups;
 static bool do_grp_update = false;     /* group files need to be updated */
 
 extern int allow_bad_names;
@@ -221,10 +222,6 @@ static bool home_added = false;
 #define DBTRFS_SUBVOLUME_HOME  "BTRFS_SUBVOLUME_HOME"
 #define DLOG_INIT              "LOG_INIT"
 
-#ifndef NGROUPS_MAX
-#define NGROUPS_MAX            65536
-#endif
-
 /* local function prototypes */
 NORETURN static void fail_exit (int, bool);
 static void get_defaults(const struct option_flags *);
@@ -764,7 +761,7 @@ static int get_groups(char *list, const struct option_flags *flags)
 {
        struct group *grp;
        bool errors = false;
-       int ngroups = 0;
+       size_t ngroups = 0;
        bool process_selinux;
 
        process_selinux = !flags->chroot && !flags->prefix;
@@ -825,7 +822,7 @@ static int get_groups(char *list, const struct option_flags *flags)
 
                if (ngroups == sys_ngroups) {
                        fprintf (stderr,
-                                _("%s: too many groups specified (max %d).\n"),
+                                _("%s: too many groups specified (max %zu).\n"),
                                 Prog, ngroups);
                        gr_free(grp);
                        break;
@@ -2532,9 +2529,7 @@ int main (int argc, char **argv)
        audit_help_open ();
 #endif
 
-       sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       if (sys_ngroups == -1)
-               sys_ngroups = NGROUPS_MAX;
+       sys_ngroups = ngroups_max_size();
        user_groups = xmalloc_T(1 + sys_ngroups, char *);
        /*
         * Initialize the list to be empty
index af66d38c022cfa6b409a099774af5f3cf5d30e84..6dfe461a23b63b974478abc0ac7fe2771efacd45 100644 (file)
@@ -66,6 +66,7 @@
 #include "string/strdup/strdup.h"
 #include "string/strerrno.h"
 #include "string/strspn/stprspn.h"
+#include "sysconf.h"
 #include "time/day_to_str.h"
 #include "typetraits.h"
 
 /* invalid shadow time indicating missing entry */
 #define MISSING_TIME   -2
 
-#ifndef NGROUPS_MAX
-#define NGROUPS_MAX            65536
-#endif
-
 /*
  * Structures
  */
@@ -138,7 +135,7 @@ static long user_expire = MISSING_TIME;
 static long user_newexpire = MISSING_TIME;
 static long user_inactive = MISSING_TIME;
 static long user_newinactive = MISSING_TIME;
-static long sys_ngroups;
+static size_t sys_ngroups;
 static char **user_groups;     /* NULL-terminated list */
 
 static const char* prefix = "";
@@ -238,7 +235,7 @@ static int get_groups (char *list)
 {
        struct group *grp;
        bool errors = false;
-       int ngroups = 0;
+       size_t ngroups = 0;
 
        /*
         * Initialize the list to be empty
@@ -288,7 +285,7 @@ static int get_groups (char *list)
 
                if (ngroups == sys_ngroups) {
                        fprintf (stderr,
-                                _("%s: too many groups specified (max %d).\n"),
+                                _("%s: too many groups specified (max %zu).\n"),
                                 Prog, ngroups);
                        gr_free (grp);
                        break;
@@ -2206,9 +2203,7 @@ int main (int argc, char **argv)
        audit_help_open ();
 #endif
 
-       sys_ngroups = sysconf (_SC_NGROUPS_MAX);
-       if (sys_ngroups == -1)
-               sys_ngroups = NGROUPS_MAX;
+       sys_ngroups = ngroups_max_size();
        user_groups = xmalloc_T(sys_ngroups + 1, char *);
        user_groups[0] = NULL;
 
index 9c548240f231855e5d572711d9c34f361a63d6ff..74f6721107eeebe56e8f7f2d731b7c4ef22503d9 100644 (file)
@@ -65,6 +65,7 @@ test_chkhash_LDADD = \
 
 test_chkname_SOURCES = \
     ../../lib/chkname.c \
+    ../../lib/sysconf.c \
     test_chkname.c \
     $(NULL)
 test_chkname_CFLAGS = \