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>
subordinateio.h \
subordinateio.c \
sulog.c \
+ sysconf.c \
+ sysconf.h \
time/day_to_str.c \
time/day_to_str.h \
ttytype.c \
#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)
{
#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);
--- /dev/null
+// 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;
+}
--- /dev/null
+// 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
#include "string/strdup/strdup.h"
#include "string/strerrno.h"
#include "string/strftime.h"
+#include "sysconf.h"
#ifdef USE_PAM
#include "string/strdup/strdup.h"
#include "string/strerrno.h"
#include "string/strtok/stpsep.h"
+#include "sysconf.h"
#ifndef SKEL_DIR
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;
#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 *);
{
struct group *grp;
bool errors = false;
- int ngroups = 0;
+ size_t ngroups = 0;
bool process_selinux;
process_selinux = !flags->chroot && !flags->prefix;
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;
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
#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
*/
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 = "";
{
struct group *grp;
bool errors = false;
- int ngroups = 0;
+ size_t ngroups = 0;
/*
* Initialize the list to be empty
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;
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;
test_chkname_SOURCES = \
../../lib/chkname.c \
+ ../../lib/sysconf.c \
test_chkname.c \
$(NULL)
test_chkname_CFLAGS = \