1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Daniel Mack
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "sd-messages.h"
30 #include "unit-name.h"
33 #include "bus-error.h"
34 #include "bus-common-errors.h"
36 #include "formats-util.h"
37 #include "utmp-wtmp.h"
39 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
43 static const int wall_timers[] = {
44 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
45 25, 40, 55, 70, 100, 130, 150, 180,
48 /* If the time is already passed, then don't announce */
54 for (i = 1; i < ELEMENTSOF(wall_timers); i++)
55 if (wall_timers[i] * USEC_PER_MINUTE >= left)
56 return left - wall_timers[i-1] * USEC_PER_MINUTE;
58 return left % USEC_PER_HOUR;
61 bool logind_wall_tty_filter(const char *tty, void *userdata) {
63 Manager *m = userdata;
67 if (!startswith(tty, "/dev/"))
70 return !streq(tty + 5, m->scheduled_shutdown_tty);
73 static int warn_wall(Manager *m, usec_t n) {
74 char date[FORMAT_TIMESTAMP_MAX] = {};
75 _cleanup_free_ char *l = NULL;
81 if (!m->enable_wall_messages)
84 left = m->scheduled_shutdown_timeout > n;
86 r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
87 strempty(m->wall_message),
88 isempty(m->wall_message) ? "" : "\n",
89 m->scheduled_shutdown_type,
91 left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
97 utmp_wall(l, lookup_uid(m->scheduled_shutdown_uid),
98 m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
103 static int wall_message_timeout_handler(
108 Manager *m = userdata;
113 assert(s == m->wall_message_timeout_source);
115 n = now(CLOCK_REALTIME);
121 next = when_wall(n, m->scheduled_shutdown_timeout);
123 r = sd_event_source_set_time(s, n + next);
125 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
127 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
129 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
135 int manager_setup_wall_message_timer(Manager *m) {
142 n = now(CLOCK_REALTIME);
143 elapse = m->scheduled_shutdown_timeout;
145 /* wall message handling */
147 if (isempty(m->scheduled_shutdown_type)) {
155 /* Warn immediately if less than 15 minutes are left */
156 if (elapse - n < 15 * USEC_PER_MINUTE) {
162 elapse = when_wall(n, elapse);
166 if (m->wall_message_timeout_source) {
167 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
169 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
171 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
173 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
175 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
176 CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
178 return log_error_errno(r, "sd_event_add_time() failed. %m");