1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2015 Daniel Mack
11 #include "sd-messages.h"
13 #include "alloc-util.h"
14 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
18 #include "format-util.h"
20 #include "path-util.h"
21 //#include "special.h"
23 #include "unit-name.h"
24 #include "user-util.h"
25 #include "utmp-wtmp.h"
27 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
31 static const int wall_timers[] = {
32 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
33 25, 40, 55, 70, 100, 130, 150, 180,
36 /* If the time is already passed, then don't announce */
42 for (i = 1; i < ELEMENTSOF(wall_timers); i++)
43 if (wall_timers[i] * USEC_PER_MINUTE >= left)
44 return left - wall_timers[i-1] * USEC_PER_MINUTE;
46 return left % USEC_PER_HOUR;
49 bool logind_wall_tty_filter(const char *tty, void *userdata) {
50 Manager *m = userdata;
55 if (!m->scheduled_shutdown_tty)
58 p = path_startswith(tty, "/dev/");
62 return !streq(p, m->scheduled_shutdown_tty);
65 static int warn_wall(Manager *m, usec_t n) {
66 char date[FORMAT_TIMESTAMP_MAX] = {};
67 _cleanup_free_ char *l = NULL;
73 if (!m->enable_wall_messages)
76 left = m->scheduled_shutdown_timeout > n;
78 r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
79 strempty(m->wall_message),
80 isempty(m->wall_message) ? "" : "\n",
81 m->scheduled_shutdown_type,
83 left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
89 utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
90 m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
95 static int wall_message_timeout_handler(
100 Manager *m = userdata;
105 assert(s == m->wall_message_timeout_source);
107 n = now(CLOCK_REALTIME);
113 next = when_wall(n, m->scheduled_shutdown_timeout);
115 r = sd_event_source_set_time(s, n + next);
117 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
119 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
121 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
127 int manager_setup_wall_message_timer(Manager *m) {
130 #if 1 /// let's fix double wall messages in elogind - they suck.
137 #if 1 /// Make elogind more aware of the possibility to disable wall messages
138 /* Do not do anything if wall messages aren't enabled */
139 if (!m->enable_wall_messages)
142 n = now(CLOCK_REALTIME);
143 elapse = m->scheduled_shutdown_timeout;
145 /* wall message handling */
147 if (isempty(m->scheduled_shutdown_type)) {
155 #if 0 /// let's fix double wall messages in elogind - they suck.
156 /* Warn immediately if less than 15 minutes are left */
157 if (elapse - n < 15 * USEC_PER_MINUTE) {
163 elapse = when_wall(n, elapse);
168 elapse = when_wall(n, elapse);
170 /* Warn immediately if less than 15 minutes are left, but not if
171 * there aren't more than three seconds to the next timeout. */
172 if ( (left < 15 * USEC_PER_MINUTE)
173 && (elapse > 3 * USEC_PER_SEC) ) {
179 /* If the next timeout is within on second, delay it by 3 seconds */
180 if (USEC_PER_SEC > elapse)
181 elapse = 3 * USEC_PER_SEC;
184 if (m->wall_message_timeout_source) {
185 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
187 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
189 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
191 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
193 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
194 CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
196 return log_error_errno(r, "sd_event_add_time() failed. %m");