]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-wall.c
polkit: simplify bus_verify_polkit_async() + drop auth-by-cap dbus feature
[thirdparty/systemd.git] / src / login / logind-wall.c
CommitLineData
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"
53c0397b 21#include "wall.h"
e2fa5721 22
d1e8e8b5 23static 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 42bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) {
51a2b575 43 Manager *m = ASSERT_PTR(userdata);
e2fa5721 44
c794877f 45 assert(handle_action_valid(m->scheduled_shutdown_action));
ea74f39c 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. */
c794877f 55 if (is_local && HANDLE_ACTION_IS_SLEEP(m->scheduled_shutdown_action))
51a2b575
ZJS
56 return false;
57
58 return !streq_ptr(p, m->scheduled_shutdown_tty);
e2fa5721
DM
59}
60
61static int warn_wall(Manager *m, usec_t n) {
e2fa5721
DM
62 assert(m);
63
c794877f 64 if (!handle_action_valid(m->scheduled_shutdown_action))
e2fa5721
DM
65 return 0;
66
90750669 67 bool left = m->scheduled_shutdown_timeout > n;
e2fa5721 68
90750669
ZJS
69 _cleanup_free_ char *l = NULL;
70 if (asprintf(&l, "%s%sThe system will %s %s%s!",
e2fa5721
DM
71 strempty(m->wall_message),
72 isempty(m->wall_message) ? "" : "\n",
c794877f 73 handle_action_verb_to_string(m->scheduled_shutdown_action),
3dbb9bc5 74 left ? "at " : "now",
90750669
ZJS
75 left ? FORMAT_TIMESTAMP(m->scheduled_shutdown_timeout) : "") < 0) {
76
e2fa5721 77 log_oom();
90750669 78 return 1; /* We're out-of-memory for now, but let's try to print the message later */
e2fa5721
DM
79 }
80
90750669
ZJS
81 _cleanup_free_ char *username = uid_to_name(m->scheduled_shutdown_uid);
82
83 int level = left ? LOG_INFO : LOG_NOTICE;
84
85 log_struct(level,
86 LOG_MESSAGE("%s", l),
c794877f 87 "ACTION=%s", handle_action_to_string(m->scheduled_shutdown_action),
87305b0f 88 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_SCHEDULED_STR,
90750669
ZJS
89 username ? "OPERATOR=%s" : NULL, username);
90
91 if (m->enable_wall_messages)
53c0397b 92 (void) wall(l, username, m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
e2fa5721
DM
93
94 return 1;
95}
96
97static int wall_message_timeout_handler(
98 sd_event_source *s,
99 uint64_t usec,
100 void *userdata) {
101
b2800613 102 Manager *m = ASSERT_PTR(userdata);
e2fa5721
DM
103 int r;
104
e2fa5721
DM
105 assert(s == m->wall_message_timeout_source);
106
b2800613 107 usec_t n = now(CLOCK_REALTIME);
e2fa5721
DM
108
109 r = warn_wall(m, n);
110 if (r == 0)
111 return 0;
112
b2800613 113 usec_t next = when_wall(n, m->scheduled_shutdown_timeout);
e2fa5721
DM
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
127int manager_setup_wall_message_timer(Manager *m) {
e2fa5721
DM
128 int r;
129
130 assert(m);
131
b2800613
ZJS
132 usec_t n = now(CLOCK_REALTIME);
133 usec_t elapse = m->scheduled_shutdown_timeout;
e2fa5721
DM
134
135 /* wall message handling */
136
c794877f 137 if (!handle_action_valid(m->scheduled_shutdown_action))
e2fa5721 138 return 0;
e2fa5721 139
90b1ec03 140 if (elapse > 0 && elapse < n)
e2fa5721
DM
141 return 0;
142
143 /* Warn immediately if less than 15 minutes are left */
90b1ec03 144 if (elapse == 0 || elapse - n < 15 * USEC_PER_MINUTE) {
e2fa5721
DM
145 r = warn_wall(m, n);
146 if (r == 0)
147 return 0;
148 }
149
150 elapse = when_wall(n, elapse);
151 if (elapse == 0)
152 return 0;
153
aa6123e8
LN
154 r = event_reset_time(m->event, &m->wall_message_timeout_source,
155 CLOCK_REALTIME,
156 n + elapse, 0,
157 wall_message_timeout_handler, m,
158 0, "wall-message-timer", true);
e2fa5721 159
aa6123e8
LN
160 if (r < 0) {
161 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
162 return log_error_errno(r, "Failed to set up wall message timer: %m");
e2fa5721
DM
163 }
164
165 return 0;
166}