]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
agetty: add cred_read_str() for credential string parsing
authorKarel Zak <kzak@redhat.com>
Wed, 22 Apr 2026 10:18:08 +0000 (12:18 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 22 Apr 2026 10:18:08 +0000 (12:18 +0200)
Introduce cred_read_str() helper that reads a systemd credential
file and stores the string value into a struct member addressed
via offsetof(). This replaces the open-coded string credential
handling and prepares for adding more credential types.

Addresses: https://github.com/util-linux/util-linux/issues/2255
Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/agetty.c

index a63caa935fce3ff9d8803a9839e94c61d7eeac2b..de5215d484ab268ee38863268e674de58575d771 100644 (file)
@@ -3094,6 +3094,20 @@ static void reload_agettys(void)
 #endif
 }
 
+static int cred_read_str(struct path_cxt *pc, const char *name,
+                        struct 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 void load_credentials(struct options *op)
 {
        char *env;
@@ -3118,13 +3132,9 @@ static void load_credentials(struct options *op)
        }
 
        while ((d = xreaddir(dir))) {
-               char *str;
-
-               if (strcmp(d->d_name, "agetty.autologin") == 0) {
-                       ul_path_read_string(pc, &str, d->d_name);
-                       free(op->autolog);
-                       op->autolog = str;
-               }
+               if (strcmp(d->d_name, "agetty.autologin") == 0)
+                       cred_read_str(pc, d->d_name, op,
+                                     offsetof(struct options, autolog));
        }
        closedir(dir);
 }