From: Karel Zak Date: Wed, 22 Apr 2026 10:18:08 +0000 (+0200) Subject: agetty: add cred_read_str() for credential string parsing X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=5c587edd616c5fd0daf36e33c2d3c76e17fa9ba2;p=thirdparty%2Futil-linux.git agetty: add cred_read_str() for credential string parsing 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 --- diff --git a/term-utils/agetty.c b/term-utils/agetty.c index a63caa935..de5215d48 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -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); }