]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e2fa5721 DM |
2 | |
3 | #include <errno.h> | |
e2fa5721 | 4 | #include <unistd.h> |
e2fa5721 DM |
5 | |
6 | #include "sd-messages.h" | |
b1d4f8e1 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
430f0182 | 9 | #include "audit-util.h" |
e2fa5721 | 10 | #include "bus-common-errors.h" |
b1d4f8e1 LP |
11 | #include "bus-error.h" |
12 | #include "bus-util.h" | |
aa6123e8 | 13 | #include "event-util.h" |
f97b34a6 | 14 | #include "format-util.h" |
b1d4f8e1 | 15 | #include "logind.h" |
27458ed6 | 16 | #include "path-util.h" |
b1d4f8e1 LP |
17 | #include "special.h" |
18 | #include "strv.h" | |
19 | #include "unit-name.h" | |
20 | #include "user-util.h" | |
e2fa5721 DM |
21 | #include "utmp-wtmp.h" |
22 | ||
d1e8e8b5 | 23 | static usec_t when_wall(usec_t n, usec_t elapse) { |
e2fa5721 DM |
24 | static const int wall_timers[] = { |
25 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | |
26 | 25, 40, 55, 70, 100, 130, 150, 180, | |
27 | }; | |
28 | ||
29 | /* If the time is already passed, then don't announce */ | |
30 | if (n >= elapse) | |
31 | return 0; | |
32 | ||
b2800613 | 33 | usec_t left = elapse - n; |
e2fa5721 | 34 | |
b2800613 | 35 | for (unsigned i = 1; i < ELEMENTSOF(wall_timers); i++) |
e2fa5721 DM |
36 | if (wall_timers[i] * USEC_PER_MINUTE >= left) |
37 | return left - wall_timers[i-1] * USEC_PER_MINUTE; | |
38 | ||
39 | return left % USEC_PER_HOUR; | |
40 | } | |
41 | ||
16618332 | 42 | bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) { |
51a2b575 | 43 | Manager *m = ASSERT_PTR(userdata); |
e2fa5721 | 44 | |
ea74f39c CG |
45 | assert(m->scheduled_shutdown_action); |
46 | ||
51a2b575 | 47 | const char *p = path_startswith(tty, "/dev/"); |
27458ed6 | 48 | if (!p) |
e2fa5721 DM |
49 | return true; |
50 | ||
ea74f39c CG |
51 | /* Do not send information about events which do not destroy local sessions to local terminals. We |
52 | * can assume that if the system enters sleep or hibernation, this will be visible in an obvious way | |
0a113662 YW |
53 | * for any local user. And once the systems exits sleep or hibernation, the notification would be |
54 | * just noise, in particular for auto-suspend. */ | |
ea74f39c CG |
55 | if (is_local && |
56 | IN_SET(m->scheduled_shutdown_action->handle, | |
57 | HANDLE_SUSPEND, | |
58 | HANDLE_HIBERNATE, | |
59 | HANDLE_HYBRID_SLEEP, | |
60 | HANDLE_SUSPEND_THEN_HIBERNATE)) | |
51a2b575 ZJS |
61 | return false; |
62 | ||
63 | return !streq_ptr(p, m->scheduled_shutdown_tty); | |
e2fa5721 DM |
64 | } |
65 | ||
66 | static int warn_wall(Manager *m, usec_t n) { | |
e2fa5721 DM |
67 | assert(m); |
68 | ||
90750669 | 69 | if (!m->scheduled_shutdown_action) |
e2fa5721 DM |
70 | return 0; |
71 | ||
90750669 | 72 | bool left = m->scheduled_shutdown_timeout > n; |
e2fa5721 | 73 | |
90750669 ZJS |
74 | _cleanup_free_ char *l = NULL; |
75 | if (asprintf(&l, "%s%sThe system will %s %s%s!", | |
e2fa5721 DM |
76 | strempty(m->wall_message), |
77 | isempty(m->wall_message) ? "" : "\n", | |
3dbb9bc5 ZJS |
78 | handle_action_verb_to_string(m->scheduled_shutdown_action->handle), |
79 | left ? "at " : "now", | |
90750669 ZJS |
80 | left ? FORMAT_TIMESTAMP(m->scheduled_shutdown_timeout) : "") < 0) { |
81 | ||
e2fa5721 | 82 | log_oom(); |
90750669 | 83 | return 1; /* We're out-of-memory for now, but let's try to print the message later */ |
e2fa5721 DM |
84 | } |
85 | ||
90750669 ZJS |
86 | _cleanup_free_ char *username = uid_to_name(m->scheduled_shutdown_uid); |
87 | ||
88 | int level = left ? LOG_INFO : LOG_NOTICE; | |
89 | ||
90 | log_struct(level, | |
91 | LOG_MESSAGE("%s", l), | |
92 | "ACTION=%s", handle_action_to_string(m->scheduled_shutdown_action->handle), | |
87305b0f | 93 | "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_SCHEDULED_STR, |
90750669 ZJS |
94 | username ? "OPERATOR=%s" : NULL, username); |
95 | ||
96 | if (m->enable_wall_messages) | |
97 | utmp_wall(l, username, m->scheduled_shutdown_tty, logind_wall_tty_filter, m); | |
e2fa5721 DM |
98 | |
99 | return 1; | |
100 | } | |
101 | ||
102 | static int wall_message_timeout_handler( | |
103 | sd_event_source *s, | |
104 | uint64_t usec, | |
105 | void *userdata) { | |
106 | ||
b2800613 | 107 | Manager *m = ASSERT_PTR(userdata); |
e2fa5721 DM |
108 | int r; |
109 | ||
e2fa5721 DM |
110 | assert(s == m->wall_message_timeout_source); |
111 | ||
b2800613 | 112 | usec_t n = now(CLOCK_REALTIME); |
e2fa5721 DM |
113 | |
114 | r = warn_wall(m, n); | |
115 | if (r == 0) | |
116 | return 0; | |
117 | ||
b2800613 | 118 | usec_t next = when_wall(n, m->scheduled_shutdown_timeout); |
e2fa5721 DM |
119 | if (next > 0) { |
120 | r = sd_event_source_set_time(s, n + next); | |
121 | if (r < 0) | |
c2a23db0 | 122 | return log_error_errno(r, "sd_event_source_set_time() failed. %m"); |
e2fa5721 DM |
123 | |
124 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); | |
125 | if (r < 0) | |
c2a23db0 | 126 | return log_error_errno(r, "sd_event_source_set_enabled() failed. %m"); |
e2fa5721 DM |
127 | } |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | int manager_setup_wall_message_timer(Manager *m) { | |
e2fa5721 DM |
133 | int r; |
134 | ||
135 | assert(m); | |
136 | ||
b2800613 ZJS |
137 | usec_t n = now(CLOCK_REALTIME); |
138 | usec_t elapse = m->scheduled_shutdown_timeout; | |
e2fa5721 DM |
139 | |
140 | /* wall message handling */ | |
141 | ||
a3ddcc11 | 142 | if (!m->scheduled_shutdown_action) |
e2fa5721 | 143 | return 0; |
e2fa5721 | 144 | |
90b1ec03 | 145 | if (elapse > 0 && elapse < n) |
e2fa5721 DM |
146 | return 0; |
147 | ||
148 | /* Warn immediately if less than 15 minutes are left */ | |
90b1ec03 | 149 | if (elapse == 0 || elapse - n < 15 * USEC_PER_MINUTE) { |
e2fa5721 DM |
150 | r = warn_wall(m, n); |
151 | if (r == 0) | |
152 | return 0; | |
153 | } | |
154 | ||
155 | elapse = when_wall(n, elapse); | |
156 | if (elapse == 0) | |
157 | return 0; | |
158 | ||
aa6123e8 LN |
159 | r = event_reset_time(m->event, &m->wall_message_timeout_source, |
160 | CLOCK_REALTIME, | |
161 | n + elapse, 0, | |
162 | wall_message_timeout_handler, m, | |
163 | 0, "wall-message-timer", true); | |
e2fa5721 | 164 | |
aa6123e8 LN |
165 | if (r < 0) { |
166 | m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); | |
167 | return log_error_errno(r, "Failed to set up wall message timer: %m"); | |
e2fa5721 DM |
168 | } |
169 | ||
170 | return 0; | |
171 | } |