]>
Commit | Line | Data |
---|---|---|
e2fa5721 DM |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2015 Daniel Mack | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <pwd.h> | |
26 | ||
27 | #include "sd-messages.h" | |
28 | #include "strv.h" | |
29 | #include "special.h" | |
30 | #include "unit-name.h" | |
31 | #include "audit.h" | |
32 | #include "bus-util.h" | |
33 | #include "bus-error.h" | |
34 | #include "bus-common-errors.h" | |
35 | #include "logind.h" | |
36 | #include "formats-util.h" | |
37 | #include "utmp-wtmp.h" | |
38 | ||
39 | _const_ static usec_t when_wall(usec_t n, usec_t elapse) { | |
40 | ||
41 | usec_t left; | |
42 | unsigned int i; | |
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, | |
46 | }; | |
47 | ||
48 | /* If the time is already passed, then don't announce */ | |
49 | if (n >= elapse) | |
50 | return 0; | |
51 | ||
52 | left = elapse - n; | |
53 | ||
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; | |
57 | ||
58 | return left % USEC_PER_HOUR; | |
59 | } | |
60 | ||
61 | bool logind_wall_tty_filter(const char *tty, void *userdata) { | |
62 | ||
63 | Manager *m = userdata; | |
64 | ||
65 | assert(m); | |
66 | ||
67 | if (!startswith(tty, "/dev/")) | |
68 | return true; | |
69 | ||
70 | return !streq(tty + 5, m->scheduled_shutdown_tty); | |
71 | } | |
72 | ||
73 | static int warn_wall(Manager *m, usec_t n) { | |
74 | char date[FORMAT_TIMESTAMP_MAX] = {}; | |
75 | _cleanup_free_ char *l = NULL; | |
76 | usec_t left; | |
77 | int r; | |
78 | ||
79 | assert(m); | |
80 | ||
81 | if (!m->enable_wall_messages) | |
82 | return 0; | |
83 | ||
84 | left = m->scheduled_shutdown_timeout > n; | |
85 | ||
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, | |
90 | left ? "at " : "NOW", | |
91 | left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : ""); | |
92 | if (r < 0) { | |
93 | log_oom(); | |
94 | return 0; | |
95 | } | |
96 | ||
97 | utmp_wall(l, lookup_uid(m->scheduled_shutdown_uid), | |
98 | m->scheduled_shutdown_tty, logind_wall_tty_filter, m); | |
99 | ||
100 | return 1; | |
101 | } | |
102 | ||
103 | static int wall_message_timeout_handler( | |
104 | sd_event_source *s, | |
105 | uint64_t usec, | |
106 | void *userdata) { | |
107 | ||
108 | Manager *m = userdata; | |
109 | usec_t n, next; | |
110 | int r; | |
111 | ||
112 | assert(m); | |
113 | assert(s == m->wall_message_timeout_source); | |
114 | ||
115 | n = now(CLOCK_REALTIME); | |
116 | ||
117 | r = warn_wall(m, n); | |
118 | if (r == 0) | |
119 | return 0; | |
120 | ||
121 | next = when_wall(n, m->scheduled_shutdown_timeout); | |
122 | if (next > 0) { | |
123 | r = sd_event_source_set_time(s, n + next); | |
124 | if (r < 0) | |
c2a23db0 | 125 | return log_error_errno(r, "sd_event_source_set_time() failed. %m"); |
e2fa5721 DM |
126 | |
127 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); | |
128 | if (r < 0) | |
c2a23db0 | 129 | return log_error_errno(r, "sd_event_source_set_enabled() failed. %m"); |
e2fa5721 DM |
130 | } |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
135 | int manager_setup_wall_message_timer(Manager *m) { | |
136 | ||
137 | usec_t n, elapse; | |
138 | int r; | |
139 | ||
140 | assert(m); | |
141 | ||
142 | n = now(CLOCK_REALTIME); | |
143 | elapse = m->scheduled_shutdown_timeout; | |
144 | ||
145 | /* wall message handling */ | |
146 | ||
147 | if (isempty(m->scheduled_shutdown_type)) { | |
148 | warn_wall(m, n); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | if (elapse < n) | |
153 | return 0; | |
154 | ||
155 | /* Warn immediately if less than 15 minutes are left */ | |
156 | if (elapse - n < 15 * USEC_PER_MINUTE) { | |
157 | r = warn_wall(m, n); | |
158 | if (r == 0) | |
159 | return 0; | |
160 | } | |
161 | ||
162 | elapse = when_wall(n, elapse); | |
163 | if (elapse == 0) | |
164 | return 0; | |
165 | ||
166 | if (m->wall_message_timeout_source) { | |
167 | r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse); | |
168 | if (r < 0) | |
c2a23db0 | 169 | return log_error_errno(r, "sd_event_source_set_time() failed. %m"); |
e2fa5721 DM |
170 | |
171 | r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT); | |
172 | if (r < 0) | |
c2a23db0 | 173 | return log_error_errno(r, "sd_event_source_set_enabled() failed. %m"); |
e2fa5721 DM |
174 | } else { |
175 | r = sd_event_add_time(m->event, &m->wall_message_timeout_source, | |
176 | CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m); | |
177 | if (r < 0) | |
c2a23db0 | 178 | return log_error_errno(r, "sd_event_add_time() failed. %m"); |
e2fa5721 DM |
179 | } |
180 | ||
181 | return 0; | |
182 | } |