]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/utmp.c: Align generated "ut_id" with modern software
authorEvgeny Grin (Karlson2k) <k2k@drgrin.dev>
Wed, 16 Jul 2025 15:50:53 +0000 (17:50 +0200)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Fri, 18 Jul 2025 09:52:19 +0000 (11:52 +0200)
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) <k2k@drgrin.dev>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
lib/utmp.c

index 11e6b190d11cd535132e5ee5318aeae4e5cd1fc4..fade895f288e1205f98ea934f3f772079dca6fe9 100644 (file)
@@ -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);