1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "audit-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
15 #include "format-util.h"
17 #include "path-util.h"
18 //#include "special.h"
20 #include "unit-name.h"
21 #include "user-util.h"
22 #include "utmp-wtmp.h"
24 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
28 static const int wall_timers[] = {
29 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
30 25, 40, 55, 70, 100, 130, 150, 180,
33 /* If the time is already passed, then don't announce */
39 for (i = 1; i < ELEMENTSOF(wall_timers); i++)
40 if (wall_timers[i] * USEC_PER_MINUTE >= left)
41 return left - wall_timers[i-1] * USEC_PER_MINUTE;
43 return left % USEC_PER_HOUR;
46 bool logind_wall_tty_filter(const char *tty, void *userdata) {
47 Manager *m = userdata;
52 if (!m->scheduled_shutdown_tty)
55 p = path_startswith(tty, "/dev/");
59 return !streq(p, m->scheduled_shutdown_tty);
62 static int warn_wall(Manager *m, usec_t n) {
63 char date[FORMAT_TIMESTAMP_MAX] = {};
64 _cleanup_free_ char *l = NULL;
70 if (!m->enable_wall_messages)
73 left = m->scheduled_shutdown_timeout > n;
75 r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
76 strempty(m->wall_message),
77 isempty(m->wall_message) ? "" : "\n",
78 m->scheduled_shutdown_type,
80 left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
86 utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
87 m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
92 static int wall_message_timeout_handler(
97 Manager *m = userdata;
102 assert(s == m->wall_message_timeout_source);
104 n = now(CLOCK_REALTIME);
110 next = when_wall(n, m->scheduled_shutdown_timeout);
112 r = sd_event_source_set_time(s, n + next);
114 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
116 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
118 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
124 int manager_setup_wall_message_timer(Manager *m) {
127 #if 1 /// let's fix double wall messages in elogind - they suck.
134 #if 1 /// Make elogind more aware of the possibility to disable wall messages
135 /* Do not do anything if wall messages aren't enabled */
136 if (!m->enable_wall_messages)
139 n = now(CLOCK_REALTIME);
140 elapse = m->scheduled_shutdown_timeout;
142 /* wall message handling */
144 if (isempty(m->scheduled_shutdown_type)) {
152 #if 0 /// let's fix double wall messages in elogind - they suck.
153 /* Warn immediately if less than 15 minutes are left */
154 if (elapse - n < 15 * USEC_PER_MINUTE) {
160 elapse = when_wall(n, elapse);
165 elapse = when_wall(n, elapse);
167 /* Warn immediately if less than 15 minutes are left, but not if
168 * there aren't more than three seconds to the next timeout. */
169 if ( (left < 15 * USEC_PER_MINUTE)
170 && (elapse > 3 * USEC_PER_SEC) ) {
176 /* If the next timeout is within on second, delay it by 3 seconds */
177 if (USEC_PER_SEC > elapse)
178 elapse = 3 * USEC_PER_SEC;
181 if (m->wall_message_timeout_source) {
182 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
184 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
186 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
188 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
190 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
191 CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
193 return log_error_errno(r, "sd_event_add_time() failed. %m");