]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: add a helper that dumps /run/utmp in detail
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 31 May 2022 13:23:35 +0000 (15:23 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 31 May 2022 20:30:08 +0000 (22:30 +0200)
utmpdump doesn't print all the details. Looking at the list if useful
when trying to tweak the wall filtering logic.

This doesn't do much, but at least it serves as a smoke test for the cleanup
functions.

src/test/meson.build
src/test/test-utmp.c [new file with mode: 0644]

index d9304d538b0848b485410172b7f41309c7b32cd8..d2bdd201ee3567130c7b13b80f0a4d26cf83c0ab 100644 (file)
@@ -614,6 +614,8 @@ tests += [
 
         [files('test-journal-importer.c')],
 
+        [files('test-utmp.c')],
+
         [files('test-udev.c'),
          [libudevd_core,
           libshared],
diff --git a/src/test/test-utmp.c b/src/test/test-utmp.c
new file mode 100644 (file)
index 0000000..2f66003
--- /dev/null
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "format-util.h"
+#include "socket-util.h"
+#include "stdio-util.h"
+#include "string-util.h"
+#include "utmp-wtmp.h"
+#include "tests.h"
+
+#ifndef UT_LINESIZE
+#  define UT_LINESIZE      32
+#endif
+#ifndef UT_NAMESIZE
+#  define UT_NAMESIZE      32
+#endif
+#ifndef UT_HOSTSIZE
+#  define UT_HOSTSIZE     256
+#endif
+
+TEST(dump_run_utmp) {
+        _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
+
+        utmpx = utxent_start();
+
+        for (struct utmpx *u; (u = getutxent()); ) {
+                char _type_buf[DECIMAL_STR_MAX(short)];
+                const char *type =
+                        u->ut_type == EMPTY         ? "EMPTY" :
+                        u->ut_type == RUN_LVL       ? "RUN_LVL" :
+                        u->ut_type == BOOT_TIME     ? "BOOT_TIME" :
+                        u->ut_type == NEW_TIME      ? "NEW_TIME" :
+                        u->ut_type == OLD_TIME      ? "OLD_TIME" :
+                        u->ut_type == INIT_PROCESS  ? "INIT_PROCESS" :
+                        u->ut_type == LOGIN_PROCESS ? "LOGIN_PROCESS" :
+                        u->ut_type == USER_PROCESS  ? "USER_PROCESS" :
+                        u->ut_type == DEAD_PROCESS  ? "DEAD_PROCESS" :
+                        u->ut_type == ACCOUNTING    ? "ACCOUNTING" :
+                        _type_buf;
+                if (type == _type_buf)
+                        xsprintf(_type_buf, "%hd", u->ut_type);
+
+                union in_addr_union addr = {};
+                memcpy(&addr, u->ut_addr_v6, MIN(sizeof(addr), sizeof(u->ut_addr_v6)));
+                _cleanup_free_ char *pretty = NULL;
+                bool is_ipv4 = memeqzero((const uint8_t*) &addr + 4, sizeof(addr) - 4);
+                (void) in_addr_to_string(is_ipv4 ? AF_INET : AF_INET6,
+                                         &addr, &pretty);
+
+                log_info("%14s %10"PID_PRI" line=%-7.*s id=%-4.4s name=%-8.*s session=%lu host=%.*s addr=%s",
+                         type,
+                         u->ut_pid,
+                         UT_LINESIZE, u->ut_line,
+                         u->ut_id,
+                         UT_NAMESIZE, u->ut_user,
+                         (long unsigned) u->ut_session,
+                         UT_HOSTSIZE, u->ut_host,
+                         strempty(pretty));
+        }
+}
+
+DEFINE_TEST_MAIN(LOG_DEBUG);