2 This file is part of systemd.
4 Copyright 2015 Daniel Mack
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "sd-messages.h"
27 #include "alloc-util.h"
28 #include "audit-util.h"
29 #include "bus-common-errors.h"
30 #include "bus-error.h"
32 #include "formats-util.h"
34 //#include "special.h"
36 #include "unit-name.h"
37 #include "user-util.h"
38 #include "utmp-wtmp.h"
40 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
44 static const int wall_timers[] = {
45 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
46 25, 40, 55, 70, 100, 130, 150, 180,
49 /* If the time is already passed, then don't announce */
55 for (i = 1; i < ELEMENTSOF(wall_timers); i++)
56 if (wall_timers[i] * USEC_PER_MINUTE >= left)
57 return left - wall_timers[i-1] * USEC_PER_MINUTE;
59 return left % USEC_PER_HOUR;
62 bool logind_wall_tty_filter(const char *tty, void *userdata) {
64 Manager *m = userdata;
68 if (!startswith(tty, "/dev/") || !m->scheduled_shutdown_tty)
71 return !streq(tty + 5, m->scheduled_shutdown_tty);
74 static int warn_wall(Manager *m, usec_t n) {
75 char date[FORMAT_TIMESTAMP_MAX] = {};
76 _cleanup_free_ char *l = NULL;
82 if (!m->enable_wall_messages)
85 left = m->scheduled_shutdown_timeout > n;
87 r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
88 strempty(m->wall_message),
89 isempty(m->wall_message) ? "" : "\n",
90 m->scheduled_shutdown_type,
92 left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
98 utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
99 m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
104 static int wall_message_timeout_handler(
109 Manager *m = userdata;
114 assert(s == m->wall_message_timeout_source);
116 n = now(CLOCK_REALTIME);
122 next = when_wall(n, m->scheduled_shutdown_timeout);
124 r = sd_event_source_set_time(s, n + next);
126 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
128 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
130 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
136 int manager_setup_wall_message_timer(Manager *m) {
143 n = now(CLOCK_REALTIME);
144 elapse = m->scheduled_shutdown_timeout;
146 /* wall message handling */
148 if (isempty(m->scheduled_shutdown_type)) {
156 /* Warn immediately if less than 15 minutes are left */
157 if (elapse - n < 15 * USEC_PER_MINUTE) {
163 elapse = when_wall(n, elapse);
167 if (m->wall_message_timeout_source) {
168 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
170 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
172 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
174 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
176 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
177 CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
179 return log_error_errno(r, "sd_event_add_time() failed. %m");