1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-21 02:50:18 +03:00

Merge pull request #23574 from keszybz/logind-pty-wall

Do not print logind wall message to local terminals
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-06-01 16:26:03 +02:00 committed by GitHub
commit d5254fa2f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 20 deletions

View File

@ -2342,8 +2342,6 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
reset_scheduled_shutdown(m);
if (m->enable_wall_messages) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
_cleanup_free_ char *username = NULL;
@ -2361,6 +2359,8 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd
username, tty, logind_wall_tty_filter, m);
}
reset_scheduled_shutdown(m);
return sd_bus_reply_method_return(message, "b", true);
}

View File

@ -42,20 +42,28 @@ _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
return left % USEC_PER_HOUR;
}
bool logind_wall_tty_filter(const char *tty, void *userdata) {
Manager *m = userdata;
const char *p;
bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
assert(m);
assert(m->scheduled_shutdown_action);
if (!m->scheduled_shutdown_tty)
return true;
p = path_startswith(tty, "/dev/");
const char *p = path_startswith(tty, "/dev/");
if (!p)
return true;
return !streq(p, m->scheduled_shutdown_tty);
/* Do not send information about events which do not destroy local sessions to local terminals. We
* can assume that if the system enters sleep or hibernation, this will be visible in an obvious way
* for any local user. And once the systems exits sleep or hibernation, the notication would be just
* noise, in particular for auto-suspend. */
if (is_local &&
IN_SET(m->scheduled_shutdown_action->handle,
HANDLE_SUSPEND,
HANDLE_HIBERNATE,
HANDLE_HYBRID_SLEEP,
HANDLE_SUSPEND_THEN_HIBERNATE))
return false;
return !streq_ptr(p, m->scheduled_shutdown_tty);
}
static int warn_wall(Manager *m, usec_t n) {

View File

@ -180,6 +180,6 @@ CONFIG_PARSER_PROTOTYPE(config_parse_n_autovts);
CONFIG_PARSER_PROTOTYPE(config_parse_tmpfs_size);
int manager_setup_wall_message_timer(Manager *m);
bool logind_wall_tty_filter(const char *tty, void *userdata);
bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata);
int manager_read_efi_boot_loader_entries(Manager *m);

View File

@ -337,7 +337,7 @@ int utmp_wall(
const char *message,
const char *username,
const char *origin_tty,
bool (*match_tty)(const char *tty, void *userdata),
bool (*match_tty)(const char *tty, bool is_local, void *userdata),
void *userdata) {
_unused_ _cleanup_(utxent_cleanup) bool utmpx = false;
@ -381,17 +381,20 @@ int utmp_wall(
if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
continue;
/* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
/* This access is fine, because strlen("/dev/") < 32 (UT_LINESIZE) */
if (path_startswith(u->ut_line, "/dev/"))
path = u->ut_line;
else {
if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
return -ENOMEM;
path = buf;
}
if (!match_tty || match_tty(path, userdata)) {
/* It seems that the address field is always set for remote logins.
* For local logins and other local entries, we get [0,0,0,0]. */
bool is_local = memeqzero(u->ut_addr_v6, sizeof(u->ut_addr_v6));
if (!match_tty || match_tty(path, is_local, userdata)) {
q = write_to_terminal(path, text);
if (q < 0)
r = q;

View File

@ -23,7 +23,7 @@ int utmp_wall(
const char *message,
const char *username,
const char *origin_tty,
bool (*match_tty)(const char *tty, void *userdata),
bool (*match_tty)(const char *tty, bool is_local, void *userdata),
void *userdata);
static inline bool utxent_start(void) {

View File

@ -614,6 +614,8 @@ tests += [
[files('test-journal-importer.c')],
[files('test-utmp.c')],
[files('test-udev.c'),
[libudevd_core,
libshared],

61
src/test/test-utmp.c Normal file
View File

@ -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);

View File

@ -94,13 +94,12 @@ static int send_passwords(const char *socket_name, char **passwords) {
return (int) n;
}
static bool wall_tty_match(const char *path, void *userdata) {
static bool wall_tty_match(const char *path, bool is_local, void *userdata) {
_cleanup_free_ char *p = NULL;
_cleanup_close_ int fd = -1;
struct stat st;
if (!path_is_absolute(path))
path = strjoina("/dev/", path);
assert(path_is_absolute(path));
if (lstat(path, &st) < 0) {
log_debug_errno(errno, "Failed to stat %s: %m", path);