| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "sd-messages.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "event-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "logind.h" |
| 11 |
#include "path-util.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "user-util.h" |
| 14 |
#include "wall.h" |
| 15 |
|
| 16 |
static usec_t when_wall(usec_t n, usec_t elapse) { |
| 17 |
static const int wall_timers[] = { |
| 18 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 19 |
25, 40, 55, 70, 100, 130, 150, 180, |
| 20 |
}; |
| 21 |
|
| 22 |
/* If the time is already passed, then don't announce */ |
| 23 |
if (n >= elapse) |
| 24 |
return 0; |
| 25 |
|
| 26 |
usec_t left = elapse - n; |
| 27 |
|
| 28 |
for (unsigned i = 1; i < ELEMENTSOF(wall_timers); i++) |
| 29 |
if (wall_timers[i] * USEC_PER_MINUTE >= left) |
| 30 |
return left - wall_timers[i-1] * USEC_PER_MINUTE; |
| 31 |
|
| 32 |
return left % USEC_PER_HOUR; |
| 33 |
} |
| 34 |
|
| 35 |
bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) { |
| 36 |
Manager *m = ASSERT_PTR(userdata); |
| 37 |
|
| 38 |
assert(handle_action_valid(m->scheduled_shutdown_action)); |
| 39 |
|
| 40 |
const char *p = path_startswith(tty, "/dev/"); |
| 41 |
if (!p) |
| 42 |
return true; |
| 43 |
|
| 44 |
/* Do not send information about events which do not destroy local sessions to local terminals. We |
| 45 |
* can assume that if the system enters sleep or hibernation, this will be visible in an obvious way |
| 46 |
* for any local user. And once the systems exits sleep or hibernation, the notification would be |
| 47 |
* just noise, in particular for auto-suspend. */ |
| 48 |
if (is_local && HANDLE_ACTION_IS_SLEEP(m->scheduled_shutdown_action)) |
| 49 |
return false; |
| 50 |
|
| 51 |
return !streq_ptr(p, m->scheduled_shutdown_tty); |
| 52 |
} |
| 53 |
|
| 54 |
static int warn_wall(Manager *m, usec_t n) { |
| 55 |
assert(m); |
| 56 |
|
| 57 |
if (!handle_action_valid(m->scheduled_shutdown_action)) |
| 58 |
return 0; |
| 59 |
|
| 60 |
bool left = m->scheduled_shutdown_timeout > n; |
| 61 |
|
| 62 |
_cleanup_free_ char *l = NULL; |
| 63 |
if (asprintf(&l, "%s%sThe system will %s %s%s!", |
| 64 |
strempty(m->wall_message), |
| 65 |
isempty(m->wall_message) ? "" : "\n", |
| 66 |
handle_action_verb_to_string(m->scheduled_shutdown_action), |
| 67 |
left ? "at " : "now", |
| 68 |
left ? FORMAT_TIMESTAMP(m->scheduled_shutdown_timeout) : "") < 0) { |
| 69 |
|
| 70 |
log_oom(); |
| 71 |
return 1; /* We're out-of-memory for now, but let's try to print the message later */ |
| 72 |
} |
| 73 |
|
| 74 |
_cleanup_free_ char *username = uid_to_name(m->scheduled_shutdown_uid); |
| 75 |
|
| 76 |
int level = left ? LOG_INFO : LOG_NOTICE; |
| 77 |
|
| 78 |
log_struct(level, |
| 79 |
LOG_MESSAGE("%s", l), |
| 80 |
LOG_ITEM("ACTION=%s", handle_action_to_string(m->scheduled_shutdown_action)), |
| 81 |
LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN_SCHEDULED_STR), |
| 82 |
username ? "OPERATOR=%s" : NULL, username); |
| 83 |
|
| 84 |
if (m->wall_messages) |
| 85 |
(void) wall(l, username, m->scheduled_shutdown_tty, logind_wall_tty_filter, m); |
| 86 |
|
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
static int wall_message_timeout_handler( |
| 91 |
sd_event_source *s, |
| 92 |
uint64_t usec, |
| 93 |
void *userdata) { |
| 94 |
|
| 95 |
Manager *m = ASSERT_PTR(userdata); |
| 96 |
int r; |
| 97 |
|
| 98 |
assert(s == m->wall_message_timeout_source); |
| 99 |
|
| 100 |
usec_t n = now(CLOCK_REALTIME); |
| 101 |
|
| 102 |
r = warn_wall(m, n); |
| 103 |
if (r == 0) |
| 104 |
return 0; |
| 105 |
|
| 106 |
usec_t next = when_wall(n, m->scheduled_shutdown_timeout); |
| 107 |
if (next > 0) { |
| 108 |
r = sd_event_source_set_time(s, n + next); |
| 109 |
if (r < 0) |
| 110 |
return log_error_errno(r, "sd_event_source_set_time() failed: %m"); |
| 111 |
|
| 112 |
r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); |
| 113 |
if (r < 0) |
| 114 |
return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); |
| 115 |
} |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
int manager_setup_wall_message_timer(Manager *m) { |
| 121 |
int r; |
| 122 |
|
| 123 |
assert(m); |
| 124 |
|
| 125 |
usec_t n = now(CLOCK_REALTIME); |
| 126 |
usec_t elapse = m->scheduled_shutdown_timeout; |
| 127 |
|
| 128 |
/* wall message handling */ |
| 129 |
|
| 130 |
if (!handle_action_valid(m->scheduled_shutdown_action)) |
| 131 |
return 0; |
| 132 |
|
| 133 |
if (elapse > 0 && elapse < n) |
| 134 |
return 0; |
| 135 |
|
| 136 |
/* Warn immediately if less than 15 minutes are left */ |
| 137 |
if (elapse == 0 || elapse - n < 15 * USEC_PER_MINUTE) { |
| 138 |
r = warn_wall(m, n); |
| 139 |
if (r == 0) |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
elapse = when_wall(n, elapse); |
| 144 |
if (elapse == 0) |
| 145 |
return 0; |
| 146 |
|
| 147 |
r = event_reset_time(m->event, &m->wall_message_timeout_source, |
| 148 |
CLOCK_REALTIME, |
| 149 |
n + elapse, 0, |
| 150 |
wall_message_timeout_handler, m, |
| 151 |
0, "wall-message-timer", true); |
| 152 |
|
| 153 |
if (r < 0) { |
| 154 |
m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); |
| 155 |
return log_error_errno(r, "Failed to set up wall message timer: %m"); |
| 156 |
} |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |