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