From: Karel Zak Date: Wed, 22 Apr 2026 10:21:50 +0000 (+0200) Subject: agetty: add cred_read_num() for credential numeric parsing X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=82de7e2e30c7d8bc381ef8685414012d04deb18a;p=thirdparty%2Futil-linux.git agetty: add cred_read_num() for credential numeric parsing Introduce cred_read_num() helper that reads a systemd credential file and parses the value as a number. The type argument selects between unsigned ('u') and signed ('d') parsing, making it easy to extend for other numeric types in the future. 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 de5215d48..959386706 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -3108,6 +3108,43 @@ static int cred_read_str(struct path_cxt *pc, const char *name, return 0; } +static int cred_read_num(struct path_cxt *pc, const char *name, + struct 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) + log_warn(_("invalid '%s' credential value"), name); + free(str); + return rc; +} + static void load_credentials(struct options *op) { char *env;