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