1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Daniel Mack
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
20 #include "format-util.h"
22 #include "path-util.h"
23 //#include "special.h"
25 #include "unit-name.h"
26 #include "user-util.h"
27 #include "utmp-wtmp.h"
29 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
33 static const int wall_timers[] = {
34 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
35 25, 40, 55, 70, 100, 130, 150, 180,
38 /* If the time is already passed, then don't announce */
44 for (i = 1; i < ELEMENTSOF(wall_timers); i++)
45 if (wall_timers[i] * USEC_PER_MINUTE >= left)
46 return left - wall_timers[i-1] * USEC_PER_MINUTE;
48 return left % USEC_PER_HOUR;
51 bool logind_wall_tty_filter(const char *tty, void *userdata) {
52 Manager *m = userdata;
57 if (!m->scheduled_shutdown_tty)
60 p = path_startswith(tty, "/dev/");
64 return !streq(p, m->scheduled_shutdown_tty);
67 static int warn_wall(Manager *m, usec_t n) {
68 char date[FORMAT_TIMESTAMP_MAX] = {};
69 _cleanup_free_ char *l = NULL;
75 if (!m->enable_wall_messages)
78 left = m->scheduled_shutdown_timeout > n;
80 r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
81 strempty(m->wall_message),
82 isempty(m->wall_message) ? "" : "\n",
83 m->scheduled_shutdown_type,
85 left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
91 utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
92 m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
97 static int wall_message_timeout_handler(
102 Manager *m = userdata;
107 assert(s == m->wall_message_timeout_source);
109 n = now(CLOCK_REALTIME);
115 next = when_wall(n, m->scheduled_shutdown_timeout);
117 r = sd_event_source_set_time(s, n + next);
119 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
121 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
123 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
129 int manager_setup_wall_message_timer(Manager *m) {
132 #if 1 /// let's fix double wall messages in elogind - they suck.
139 #if 1 /// Make elogind more aware of the possibility to disable wall messages
140 /* Do not do anything if wall messages aren't enabled */
141 if (!m->enable_wall_messages)
144 n = now(CLOCK_REALTIME);
145 elapse = m->scheduled_shutdown_timeout;
147 /* wall message handling */
149 if (isempty(m->scheduled_shutdown_type)) {
157 #if 0 /// let's fix double wall messages in elogind - they suck.
158 /* Warn immediately if less than 15 minutes are left */
159 if (elapse - n < 15 * USEC_PER_MINUTE) {
165 elapse = when_wall(n, elapse);
170 elapse = when_wall(n, elapse);
172 /* Warn immediately if less than 15 minutes are left, but not if
173 * there aren't more than three seconds to the next timeout. */
174 if ( (left < 15 * USEC_PER_MINUTE)
175 && (elapse > 3 * USEC_PER_SEC) ) {
181 /* If the next timeout is within on second, delay it by 3 seconds */
182 if (USEC_PER_SEC > elapse)
183 elapse = 3 * USEC_PER_SEC;
186 if (m->wall_message_timeout_source) {
187 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
189 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
191 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
193 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
195 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
196 CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
198 return log_error_errno(r, "sd_event_add_time() failed. %m");