]> git.proxmox.com Git - systemd.git/blob - src/login/logind-utmp.c
3f1503ad4842fdf6db35fb98e982f96edd1128fc
[systemd.git] / src / login / logind-utmp.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <unistd.h>
5
6 #include "sd-messages.h"
7
8 #include "alloc-util.h"
9 #include "audit-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-error.h"
12 #include "bus-util.h"
13 #include "event-util.h"
14 #include "format-util.h"
15 #include "logind.h"
16 #include "path-util.h"
17 #include "special.h"
18 #include "strv.h"
19 #include "unit-name.h"
20 #include "user-util.h"
21 #include "utmp-wtmp.h"
22
23 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
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
33 usec_t left = elapse - n;
34
35 for (unsigned i = 1; i < ELEMENTSOF(wall_timers); i++)
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
42 bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) {
43 Manager *m = ASSERT_PTR(userdata);
44
45 assert(m->scheduled_shutdown_action);
46
47 const char *p = path_startswith(tty, "/dev/");
48 if (!p)
49 return true;
50
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
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. */
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);
64 }
65
66 static int warn_wall(Manager *m, usec_t n) {
67 assert(m);
68
69 if (!m->scheduled_shutdown_action)
70 return 0;
71
72 bool left = m->scheduled_shutdown_timeout > n;
73
74 _cleanup_free_ char *l = NULL;
75 if (asprintf(&l, "%s%sThe system will %s %s%s!",
76 strempty(m->wall_message),
77 isempty(m->wall_message) ? "" : "\n",
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
82 log_oom();
83 return 1; /* We're out-of-memory for now, but let's try to print the message later */
84 }
85
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);
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
107 Manager *m = ASSERT_PTR(userdata);
108 int r;
109
110 assert(s == m->wall_message_timeout_source);
111
112 usec_t n = now(CLOCK_REALTIME);
113
114 r = warn_wall(m, n);
115 if (r == 0)
116 return 0;
117
118 usec_t next = when_wall(n, m->scheduled_shutdown_timeout);
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
132 int manager_setup_wall_message_timer(Manager *m) {
133 int r;
134
135 assert(m);
136
137 usec_t n = now(CLOCK_REALTIME);
138 usec_t elapse = m->scheduled_shutdown_timeout;
139
140 /* wall message handling */
141
142 if (!m->scheduled_shutdown_action)
143 return 0;
144
145 if (elapse > 0 && elapse < n)
146 return 0;
147
148 /* Warn immediately if less than 15 minutes are left */
149 if (elapse == 0 || elapse - n < 15 * USEC_PER_MINUTE) {
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
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);
164
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");
168 }
169
170 return 0;
171 }