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