Debian Codegraph @

src/test/test-utmp.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "format-util.h"
4 #include "in-addr-util.h"
5 #include "stdio-util.h"
6 #include "tests.h"
7 #include "utmp-wtmp.h"
8
9 #define UTX_LINESIZE sizeof_field(struct utmpx, ut_line)
10 #define UTX_NAMESIZE sizeof_field(struct utmpx, ut_user)
11 #define UTX_HOSTSIZE sizeof_field(struct utmpx, ut_host)
12
13 TEST(dump_run_utmp) {
14 _unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
15
16 utmpx = utxent_start();
17
18 for (struct utmpx *u; (u = getutxent()); ) {
19 char _type_buf[DECIMAL_STR_MAX(short)];
20 const char *type =
21 u->ut_type == EMPTY ? "EMPTY" :
22 u->ut_type == RUN_LVL ? "RUN_LVL" :
23 u->ut_type == BOOT_TIME ? "BOOT_TIME" :
24 u->ut_type == NEW_TIME ? "NEW_TIME" :
25 u->ut_type == OLD_TIME ? "OLD_TIME" :
26 u->ut_type == INIT_PROCESS ? "INIT_PROCESS" :
27 u->ut_type == LOGIN_PROCESS ? "LOGIN_PROCESS" :
28 u->ut_type == USER_PROCESS ? "USER_PROCESS" :
29 u->ut_type == DEAD_PROCESS ? "DEAD_PROCESS" :
30 u->ut_type == ACCOUNTING ? "ACCOUNTING" :
31 _type_buf;
32 if (type == _type_buf)
33 xsprintf(_type_buf, "%hd", u->ut_type);
34
35 union in_addr_union addr = {};
36 memcpy(&addr, u->ut_addr_v6, MIN(sizeof(addr), sizeof(u->ut_addr_v6)));
37 bool is_ipv4 = memeqzero((const uint8_t*) &addr + 4, sizeof(addr) - 4);
38
39 log_info("%14s %10"PID_PRI" line=%-7.*s id=%-4.4s name=%-8.*s session=%lu host=%.*s addr=%s",
40 type,
41 u->ut_pid,
42 (int) UTX_LINESIZE, u->ut_line,
43 u->ut_id,
44 (int) UTX_NAMESIZE, u->ut_user,
45 (long unsigned) u->ut_session,
46 (int) UTX_HOSTSIZE, u->ut_host,
47 IN_ADDR_TO_STRING(is_ipv4 ? AF_INET : AF_INET6, &addr));
48 }
49 }
50
51 DEFINE_TEST_MAIN(LOG_DEBUG);