From: Evgeny Grin (Karlson2k) Date: Wed, 16 Jul 2025 15:50:53 +0000 (+0200) Subject: lib/utmp.c: Align generated "ut_id" with modern software X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59c5acb5dee310c3909a348a557224119f7d345f;p=thirdparty%2Fshadow.git lib/utmp.c: Align generated "ut_id" with modern software Modern software (systemd, utemper) usually use full "ut_line" as "ut_id" if string is up to four chars or last four chars if "ut_line" is longer. For reference: * libutemper (used by many graphical terminal emulator applications (konsole, xterm, others) to deal with utmp file): https://github.com/altlinux/libutempter/blob/4caa8ab94ff8b207228fa723a89214bf5e929321/libutempter/utempter.c#L99-L103 * systemd: uses full name of the device: https://github.com/systemd/systemd/blob/8013beb4a2221680b2c741bac6b3a33fe66406e1/units/getty%40.service.in#L41-L44 or **last** four characters: https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#UtmpIdentifier= The code in the commit is optimised for visual C code size. Signed-off-by: Evgeny Grin (Karlson2k) Reviewed-by: Alejandro Colomar --- diff --git a/lib/utmp.c b/lib/utmp.c index 11e6b190d..fade895f2 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -26,6 +26,7 @@ #include "alloc/x/xcalloc.h" #include "alloc/x/xmalloc.h" #include "sizeof.h" +#include "string/strchr/strnul.h" #include "string/strcmp/streq.h" #include "string/strcmp/strprefix.h" #include "string/strcpy/strncpy.h" @@ -274,7 +275,7 @@ prepare_utmp(const char *name, const char *line, const char *host, if (NULL != ut) { STRNCPY(utent->ut_id, ut->ut_id); } else { - STRNCPY(utent->ut_id, strprefix(line, "tty") ?: line); + STRNCPY(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id))); } #if defined(HAVE_STRUCT_UTMPX_UT_NAME) STRNCPY(utent->ut_name, name);