]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
agetty: split out credential loading to credentials.c
authorKarel Zak <kzak@redhat.com>
Thu, 14 May 2026 07:47:38 +0000 (09:47 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 14 May 2026 11:14:57 +0000 (13:14 +0200)
Move agetty_load_credentials() and its helpers (cred_read_str,
cred_read_num, cred_read_bool) to a new credentials.c file.

Signed-off-by: Karel Zak <kzak@redhat.com>
agetty-cmd/Makemodule.am
agetty-cmd/agetty.c
agetty-cmd/agetty.h
agetty-cmd/credentials.c [new file with mode: 0644]
agetty-cmd/meson.build

index d040aee7d3ea5088cc741d905582063137b215e6..97e067095aaf44c098b0b2912fdf135743a74057 100644 (file)
@@ -5,6 +5,7 @@ dist_noinst_DATA += term-utils/agetty.8.adoc
 
 agetty_SOURCES = agetty-cmd/agetty.c \
                 agetty-cmd/agetty.h \
+                agetty-cmd/credentials.c \
                 agetty-cmd/utils.c
 
 if USE_PLYMOUTH_SUPPORT
index dff0ed2bd4060f7dbe741e683022a45aec237200..863de79652399e2dd3ad2f8e6b31cd976e7f1875 100644 (file)
@@ -277,7 +277,6 @@ static void reload_agettys(void);
 static void print_issue_file(struct issue *ie, struct agetty_options *op, struct termios *tp);
 static void eval_issue_file(struct issue *ie, struct agetty_options *op, struct termios *tp);
 static void show_issue(struct agetty_options *op);
-static void load_credentials(struct agetty_options *op);
 
 
 /* Fake hostname for ut_host specified on command line. */
@@ -342,7 +341,7 @@ int main(int argc, char **argv)
 #endif                         /* DEBUGGING */
 
        /* Load systemd credentials. */
-       load_credentials(&options);
+       agetty_load_credentials(&options);
 
        /* Parse command-line arguments. */
        parse_args(argc, argv, &options);
@@ -3004,127 +3003,3 @@ static void reload_agettys(void)
 #endif
 }
 
-static int cred_read_str(struct path_cxt *pc, const char *name,
-                        struct agetty_options *op, size_t offset)
-{
-       char *str = NULL, **dest;
-
-       if (ul_path_read_string(pc, &str, name) < 0)
-               return -1;
-
-       dest = (char **) ((char *) op + offset);
-       free(*dest);
-       *dest = str;
-       return 0;
-}
-
-static int cred_read_num(struct path_cxt *pc, const char *name,
-                        struct agetty_options *op, size_t offset, int type)
-{
-       char *str = NULL;
-       int rc;
-
-       if (ul_path_read_string(pc, &str, name) < 0)
-               return -1;
-
-       switch (type) {
-       case 'u':
-       {
-               uint32_t num;
-               rc = ul_strtou32(str, &num, 10);
-               if (rc == 0)
-                       *((unsigned int *) ((char *) op + offset)) = num;
-               break;
-       }
-       case 'd':
-       {
-               int32_t num;
-               rc = ul_strtos32(str, &num, 10);
-               if (rc == 0)
-                       *((int *) ((char *) op + offset)) = num;
-               break;
-       }
-       default:
-               rc = -EINVAL;
-               break;
-       }
-
-       if (rc)
-               agetty_log_warn(_("invalid '%s' credential value"), name);
-       free(str);
-       return rc;
-}
-
-static int cred_read_bool(struct path_cxt *pc, const char *name,
-                         int *flags, int flag, int invert)
-{
-       char *str = NULL;
-       bool res;
-       int rc;
-
-       if (ul_path_read_string(pc, &str, name) < 0)
-               return -1;
-
-       rc = ul_strtobool(str, &res);
-       if (rc)
-               agetty_log_warn(_("invalid '%s' credential value"), name);
-       else if (res != invert)
-               *flags |= flag;
-       else
-               *flags &= ~flag;
-
-       free(str);
-       return rc;
-}
-
-static void load_credentials(struct agetty_options *op)
-{
-       char *env;
-       DIR *dir;
-       struct dirent *d;
-       struct path_cxt *pc;
-
-       env = safe_getenv("CREDENTIALS_DIRECTORY");
-       if (!env)
-               return;
-
-       pc = ul_new_path("%s", env);
-       if (!pc) {
-               agetty_log_warn(_("failed to initialize path context"));
-               return;
-       }
-
-       dir = ul_path_opendir(pc, NULL);
-       if (!dir) {
-               agetty_log_warn(_("failed to open credentials directory"));
-               return;
-       }
-
-       while ((d = xreaddir(dir))) {
-               if (strcmp(d->d_name, "agetty.autologin") == 0)
-                       cred_read_str(pc, d->d_name, op,
-                                     offsetof(struct agetty_options, autolog));
-               else if (strcmp(d->d_name, "agetty.delay") == 0)
-                       cred_read_num(pc, d->d_name, op,
-                                     offsetof(struct agetty_options, delay), 'u');
-               else if (strcmp(d->d_name, "agetty.nice") == 0)
-                       cred_read_num(pc, d->d_name, op,
-                                     offsetof(struct agetty_options, nice), 'd');
-               else if (strcmp(d->d_name, "agetty.hangup") == 0)
-                       cred_read_bool(pc, d->d_name,
-                                      &op->flags, F_HANGUP, 0);
-               else if (strcmp(d->d_name, "agetty.noclear") == 0)
-                       cred_read_bool(pc, d->d_name,
-                                      &op->flags, F_NOCLEAR, 0);
-               else if (strcmp(d->d_name, "agetty.nohints") == 0)
-                       cred_read_bool(pc, d->d_name,
-                                      &op->flags, F_NOHINTS, 0);
-               else if (strcmp(d->d_name, "agetty.nohostname") == 0)
-                       cred_read_bool(pc, d->d_name,
-                                      &op->flags, F_NOHOSTNAME, 0);
-               else if (strcmp(d->d_name, "agetty.noissue") == 0)
-                       cred_read_bool(pc, d->d_name,
-                                      &op->flags, F_ISSUE, 1);
-       }
-       closedir(dir);
-}
index ca0687c1e0ab9515b66db746de8715ec04259600..2bf455e349a24afeb26288f2c2c767a3cbb7312a 100644 (file)
@@ -61,4 +61,6 @@ extern void agetty_log_err(const char *, ...) __attribute__((__noreturn__))
 extern void agetty_log_warn(const char *, ...)
                                __attribute__((__format__(printf, 1, 2)));
 
+extern void agetty_load_credentials(struct agetty_options *op);
+
 #endif /* UTIL_LINUX_AGETTY_H */
diff --git a/agetty-cmd/credentials.c b/agetty-cmd/credentials.c
new file mode 100644 (file)
index 0000000..89a25d6
--- /dev/null
@@ -0,0 +1,138 @@
+#include <dirent.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "agetty.h"
+#include "env.h"
+#include "fileutils.h"
+#include "nls.h"
+#include "path.h"
+#include "strutils.h"
+
+static int cred_read_str(struct path_cxt *pc, const char *name,
+                        struct agetty_options *op, size_t offset)
+{
+       char *str = NULL, **dest;
+
+       if (ul_path_read_string(pc, &str, name) < 0)
+               return -1;
+
+       dest = (char **) ((char *) op + offset);
+       free(*dest);
+       *dest = str;
+       return 0;
+}
+
+static int cred_read_num(struct path_cxt *pc, const char *name,
+                        struct agetty_options *op, size_t offset, int type)
+{
+       char *str = NULL;
+       int rc;
+
+       if (ul_path_read_string(pc, &str, name) < 0)
+               return -1;
+
+       switch (type) {
+       case 'u':
+       {
+               uint32_t num;
+               rc = ul_strtou32(str, &num, 10);
+               if (rc == 0)
+                       *((unsigned int *) ((char *) op + offset)) = num;
+               break;
+       }
+       case 'd':
+       {
+               int32_t num;
+               rc = ul_strtos32(str, &num, 10);
+               if (rc == 0)
+                       *((int *) ((char *) op + offset)) = num;
+               break;
+       }
+       default:
+               rc = -EINVAL;
+               break;
+       }
+
+       if (rc)
+               agetty_log_warn(_("invalid '%s' credential value"), name);
+       free(str);
+       return rc;
+}
+
+static int cred_read_bool(struct path_cxt *pc, const char *name,
+                         int *flags, int flag, int invert)
+{
+       char *str = NULL;
+       bool res;
+       int rc;
+
+       if (ul_path_read_string(pc, &str, name) < 0)
+               return -1;
+
+       rc = ul_strtobool(str, &res);
+       if (rc)
+               agetty_log_warn(_("invalid '%s' credential value"), name);
+       else if (res != invert)
+               *flags |= flag;
+       else
+               *flags &= ~flag;
+
+       free(str);
+       return rc;
+}
+
+void agetty_load_credentials(struct agetty_options *op)
+{
+       char *env;
+       DIR *dir;
+       struct dirent *d;
+       struct path_cxt *pc;
+
+       env = safe_getenv("CREDENTIALS_DIRECTORY");
+       if (!env)
+               return;
+
+       pc = ul_new_path("%s", env);
+       if (!pc) {
+               agetty_log_warn(_("failed to initialize path context"));
+               return;
+       }
+
+       dir = ul_path_opendir(pc, NULL);
+       if (!dir) {
+               agetty_log_warn(_("failed to open credentials directory"));
+               return;
+       }
+
+       while ((d = xreaddir(dir))) {
+               if (strcmp(d->d_name, "agetty.autologin") == 0)
+                       cred_read_str(pc, d->d_name, op,
+                                     offsetof(struct agetty_options, autolog));
+               else if (strcmp(d->d_name, "agetty.delay") == 0)
+                       cred_read_num(pc, d->d_name, op,
+                                     offsetof(struct agetty_options, delay), 'u');
+               else if (strcmp(d->d_name, "agetty.nice") == 0)
+                       cred_read_num(pc, d->d_name, op,
+                                     offsetof(struct agetty_options, nice), 'd');
+               else if (strcmp(d->d_name, "agetty.hangup") == 0)
+                       cred_read_bool(pc, d->d_name,
+                                      &op->flags, F_HANGUP, 0);
+               else if (strcmp(d->d_name, "agetty.noclear") == 0)
+                       cred_read_bool(pc, d->d_name,
+                                      &op->flags, F_NOCLEAR, 0);
+               else if (strcmp(d->d_name, "agetty.nohints") == 0)
+                       cred_read_bool(pc, d->d_name,
+                                      &op->flags, F_NOHINTS, 0);
+               else if (strcmp(d->d_name, "agetty.nohostname") == 0)
+                       cred_read_bool(pc, d->d_name,
+                                      &op->flags, F_NOHOSTNAME, 0);
+               else if (strcmp(d->d_name, "agetty.noissue") == 0)
+                       cred_read_bool(pc, d->d_name,
+                                      &op->flags, F_ISSUE, 1);
+       }
+       closedir(dir);
+}
index 4274b309f611e11763a29bc9e6df1499369cf829..bd6519b767c4a8cf57f363a8eee3952448f945b9 100644 (file)
@@ -1,6 +1,7 @@
 agetty_sources = files(
   'agetty.c',
   'agetty.h',
+  'credentials.c',
   'utils.c',
 )