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