From f3389fffd6c91ea518b9f2572d9184f4eeb0259f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 6 Sep 2024 13:20:11 +0200 Subject: [PATCH 1/4] user-util: switch from utmp to utmpx We generally use utmpx instead of utmp (both are actually identical on Linux, but utmpx is POSIX, while utmp is not). Let's fix one left-over case. UT_NAMESIZE does not exist in utmpx world, it has no direct counterpart, hence let's just sizeof_field() to determine the size of the actual field. (which comes to the same result of course: 32). --- src/basic/user-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/basic/user-util.c b/src/basic/user-util.c index 858d7121896..6de5e4705eb 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include "sd-messages.h" @@ -802,7 +802,7 @@ bool valid_user_group_name(const char *u, ValidUserFlags flags) { return false; if (l > NAME_MAX) /* must fit in a filename: 255 */ return false; - if (l > UT_NAMESIZE - 1) /* must fit in utmp: 31 */ + if (l > sizeof_field(struct utmpx, ut_user) - 1) /* must fit in utmp: 31 */ return false; } From 983fee2e9491c0bbec92722f8612cb51229cd2b1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 6 Sep 2024 13:22:06 +0200 Subject: [PATCH 2/4] tree-wide: drop unnecessary utmp includes --- src/sysusers/sysusers.c | 1 - src/userdb/userdbctl.c | 1 - 2 files changed, 2 deletions(-) diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index a1ccc1f2b4b..8ec13734798 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include -#include #include "alloc-util.h" #include "build.h" diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 9a7aab3857b..1346e2de012 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include -#include #include "build.h" #include "dirent-util.h" From 98dc726902eed6755fd087eb9b18f60228293efb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 6 Sep 2024 13:22:32 +0200 Subject: [PATCH 3/4] test-utmp: replace UT_LINESIZE/UT_NAMESIZE/UT_HOSTSIZE with sizeof_field() utmpx doesn't know these defines, hence fix them. --- src/test/test-utmp.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/test/test-utmp.c b/src/test/test-utmp.c index 06a0fce764a..e94ee03250e 100644 --- a/src/test/test-utmp.c +++ b/src/test/test-utmp.c @@ -7,15 +7,9 @@ #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 +#define UTX_LINESIZE sizeof_field(struct utmpx, ut_line) +#define UTX_NAMESIZE sizeof_field(struct utmpx, ut_user) +#define UTX_HOSTSIZE sizeof_field(struct utmpx, ut_host) TEST(dump_run_utmp) { _unused_ _cleanup_(utxent_cleanup) bool utmpx = false; @@ -46,11 +40,11 @@ TEST(dump_run_utmp) { 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, + (int) UTX_LINESIZE, u->ut_line, u->ut_id, - UT_NAMESIZE, u->ut_user, + (int) UTX_NAMESIZE, u->ut_user, (long unsigned) u->ut_session, - UT_HOSTSIZE, u->ut_host, + (int) UTX_HOSTSIZE, u->ut_host, IN_ADDR_TO_STRING(is_ipv4 ? AF_INET : AF_INET6, &addr)); } } From 51da613bc49e7839017296b30b00aa3c45ea9a6d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 6 Sep 2024 13:51:51 +0200 Subject: [PATCH 4/4] tree-wide: use UTMPX_FILE rather than _PATH_UTMPX Apparently _PATH_UTMPX is a glibc'ism. UTMPX_FILE is the same thing and what everyone else uses. Since they are otherwise equivalent, let's just switch. --- src/login/logind-core.c | 12 ++++++------ src/shared/utmp-wtmp.c | 4 ++-- src/shared/wall.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 8742fe6a97c..fad276f195e 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -710,8 +710,8 @@ int manager_read_utmp(Manager *m) { assert(m); - if (utmpxname(_PATH_UTMPX) < 0) - return log_error_errno(errno, "Failed to set utmp path to " _PATH_UTMPX ": %m"); + if (utmpxname(UTMPX_FILE) < 0) + return log_error_errno(errno, "Failed to set utmp path to " UTMPX_FILE ": %m"); utmpx = utxent_start(); @@ -725,9 +725,9 @@ int manager_read_utmp(Manager *m) { u = getutxent(); if (!u) { if (errno == ENOENT) - log_debug_errno(errno, _PATH_UTMPX " does not exist, ignoring."); + log_debug_errno(errno, UTMPX_FILE " does not exist, ignoring."); else if (errno != 0) - log_warning_errno(errno, "Failed to read " _PATH_UTMPX ", ignoring: %m"); + log_warning_errno(errno, "Failed to read " UTMPX_FILE ", ignoring: %m"); return 0; } @@ -808,9 +808,9 @@ void manager_connect_utmp(Manager *m) { * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle * detection (which, for tty sessions, relies on the TTY used) */ - r = sd_event_add_inotify(m->event, &s, _PATH_UTMPX, IN_MODIFY|IN_MOVE_SELF|IN_DELETE_SELF|IN_ATTRIB, manager_dispatch_utmp, m); + r = sd_event_add_inotify(m->event, &s, UTMPX_FILE, IN_MODIFY|IN_MOVE_SELF|IN_DELETE_SELF|IN_ATTRIB, manager_dispatch_utmp, m); if (r < 0) - log_full_errno(r == -ENOENT ? LOG_DEBUG: LOG_WARNING, r, "Failed to create inotify watch on " _PATH_UTMPX ", ignoring: %m"); + log_full_errno(r == -ENOENT ? LOG_DEBUG: LOG_WARNING, r, "Failed to create inotify watch on " UTMPX_FILE ", ignoring: %m"); else { r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IDLE); if (r < 0) diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 267b350276c..e399b1d5d77 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -42,7 +42,7 @@ int utmp_get_runlevel(int *runlevel, int *previous) { return 0; } - if (utmpxname(_PATH_UTMPX) < 0) + if (utmpxname(UTMPX_FILE) < 0) return -errno; utmpx = utxent_start(); @@ -91,7 +91,7 @@ static int write_entry_utmp(const struct utmpx *store) { * each entry type resp. user; i.e. basically a key/value * table. */ - if (utmpxname(_PATH_UTMPX) < 0) + if (utmpxname(UTMPX_FILE) < 0) return -errno; utmpx = utxent_start(); diff --git a/src/shared/wall.c b/src/shared/wall.c index 119d18f5a46..b28c04cd8b3 100644 --- a/src/shared/wall.c +++ b/src/shared/wall.c @@ -51,7 +51,7 @@ static int wall_utmp( /* libc's setutxent() unfortunately doesn't inform us about success, i.e. whether /var/run/utmp * exists. Hence we have to check manually first. */ - if (access(_PATH_UTMPX, F_OK) < 0) { + if (access(UTMPX_FILE, F_OK) < 0) { if (errno == ENOENT) return -ENOPROTOOPT;