]> git.proxmox.com Git - systemd.git/blame - src/login/logind-utmp.c
bump version to 252.11-pve1
[systemd.git] / src / login / logind-utmp.c
CommitLineData
a032b68d 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
e3bff60a
MP
2
3#include <errno.h>
e3bff60a 4#include <unistd.h>
e3bff60a
MP
5
6#include "sd-messages.h"
db2df898
MP
7
8#include "alloc-util.h"
9#include "audit-util.h"
e3bff60a 10#include "bus-common-errors.h"
db2df898
MP
11#include "bus-error.h"
12#include "bus-util.h"
f5caa8fa 13#include "event-util.h"
2897b343 14#include "format-util.h"
db2df898 15#include "logind.h"
f5e65279 16#include "path-util.h"
db2df898
MP
17#include "special.h"
18#include "strv.h"
19#include "unit-name.h"
20#include "user-util.h"
e3bff60a
MP
21#include "utmp-wtmp.h"
22
23_const_ static usec_t when_wall(usec_t n, usec_t elapse) {
e3bff60a
MP
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
086111aa 33 usec_t left = elapse - n;
e3bff60a 34
086111aa 35 for (unsigned i = 1; i < ELEMENTSOF(wall_timers); i++)
e3bff60a
MP
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
cfd0f778
MB
42bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) {
43 Manager *m = ASSERT_PTR(userdata);
e3bff60a 44
cfd0f778 45 assert(m->scheduled_shutdown_action);
e3bff60a 46
cfd0f778 47 const char *p = path_startswith(tty, "/dev/");
f5e65279 48 if (!p)
e3bff60a
MP
49 return true;
50
cfd0f778
MB
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
086111aa
LB
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. */
cfd0f778
MB
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))
61 return false;
62
63 return !streq_ptr(p, m->scheduled_shutdown_tty);
e3bff60a
MP
64}
65
66static int warn_wall(Manager *m, usec_t n) {
e3bff60a
MP
67 assert(m);
68
086111aa 69 if (!m->scheduled_shutdown_action)
e3bff60a
MP
70 return 0;
71
086111aa 72 bool left = m->scheduled_shutdown_timeout > n;
e3bff60a 73
086111aa
LB
74 _cleanup_free_ char *l = NULL;
75 if (asprintf(&l, "%s%sThe system will %s %s%s!",
e3bff60a
MP
76 strempty(m->wall_message),
77 isempty(m->wall_message) ? "" : "\n",
086111aa
LB
78 handle_action_verb_to_string(m->scheduled_shutdown_action->handle),
79 left ? "at " : "now",
80 left ? FORMAT_TIMESTAMP(m->scheduled_shutdown_timeout) : "") < 0) {
81
e3bff60a 82 log_oom();
086111aa 83 return 1; /* We're out-of-memory for now, but let's try to print the message later */
e3bff60a
MP
84 }
85
086111aa
LB
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),
93 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_SCHEDULED_STR,
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);
e3bff60a
MP
98
99 return 1;
100}
101
102static int wall_message_timeout_handler(
103 sd_event_source *s,
104 uint64_t usec,
105 void *userdata) {
106
086111aa 107 Manager *m = ASSERT_PTR(userdata);
e3bff60a
MP
108 int r;
109
e3bff60a
MP
110 assert(s == m->wall_message_timeout_source);
111
086111aa 112 usec_t n = now(CLOCK_REALTIME);
e3bff60a
MP
113
114 r = warn_wall(m, n);
115 if (r == 0)
116 return 0;
117
086111aa 118 usec_t next = when_wall(n, m->scheduled_shutdown_timeout);
e3bff60a
MP
119 if (next > 0) {
120 r = sd_event_source_set_time(s, n + next);
121 if (r < 0)
122 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
123
124 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
125 if (r < 0)
126 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
127 }
128
129 return 0;
130}
131
132int manager_setup_wall_message_timer(Manager *m) {
e3bff60a
MP
133 int r;
134
135 assert(m);
136
086111aa
LB
137 usec_t n = now(CLOCK_REALTIME);
138 usec_t elapse = m->scheduled_shutdown_timeout;
e3bff60a
MP
139
140 /* wall message handling */
141
f5caa8fa 142 if (!m->scheduled_shutdown_action)
e3bff60a 143 return 0;
e3bff60a 144
f5caa8fa 145 if (elapse > 0 && elapse < n)
e3bff60a
MP
146 return 0;
147
148 /* Warn immediately if less than 15 minutes are left */
f5caa8fa 149 if (elapse == 0 || elapse - n < 15 * USEC_PER_MINUTE) {
e3bff60a
MP
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
f5caa8fa
MB
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);
e3bff60a 164
f5caa8fa
MB
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");
e3bff60a
MP
168 }
169
170 return 0;
171}