]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/update-utmp/update-utmp.c
io.systemd.Unit.List fix context/runtime split (#38172)
[thirdparty/systemd.git] / src / update-utmp / update-utmp.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <sys/stat.h>
4
5#include "sd-bus.h"
6
7#include "alloc-util.h"
8#include "bus-error.h"
9#include "bus-locator.h"
10#include "bus-util.h"
11#include "libaudit-util.h"
12#include "log.h"
13#include "main-func.h"
14#include "random-util.h"
15#include "special.h"
16#include "stdio-util.h"
17#include "strv.h"
18#include "time-util.h"
19#include "unit-def.h"
20#include "utmp-wtmp.h"
21#include "verbs.h"
22
23typedef struct Context {
24 sd_bus *bus;
25 int audit_fd;
26} Context;
27
28static void context_clear(Context *c) {
29 assert(c);
30
31 c->bus = sd_bus_flush_close_unref(c->bus);
32 c->audit_fd = close_audit_fd(c->audit_fd);
33}
34
35static int get_startup_monotonic_time(Context *c, usec_t *ret) {
36 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
37 int r;
38
39 assert(c);
40 assert(ret);
41
42 if (!c->bus) {
43 r = bus_connect_system_systemd(&c->bus);
44 if (r < 0)
45 return log_warning_errno(r, "Failed to get D-Bus connection, ignoring: %m");
46 }
47
48 r = bus_get_property_trivial(
49 c->bus,
50 bus_systemd_mgr,
51 "UserspaceTimestampMonotonic",
52 &error,
53 't', ret);
54 if (r < 0)
55 return log_warning_errno(r, "Failed to get timestamp, ignoring: %s", bus_error_message(&error, r));
56
57 return 0;
58}
59
60#define MAX_ATTEMPTS 64u
61
62static int get_current_runlevel(Context *c) {
63 static const struct {
64 const int runlevel;
65 const char *special;
66 } table[] = {
67 /* The first target of this list that is active or has a job scheduled wins. We prefer
68 * runlevels 5 and 3 here over the others, since these are the main runlevels used on Fedora.
69 * It might make sense to change the order on some distributions. */
70 { '5', SPECIAL_GRAPHICAL_TARGET },
71 { '3', SPECIAL_MULTI_USER_TARGET },
72 { '1', SPECIAL_RESCUE_TARGET },
73 };
74 int r;
75
76 assert(c);
77
78 for (unsigned n_attempts = 0;;) {
79 if (n_attempts++ > 0) {
80 /* systemd might have dropped off momentarily, let's not make this an error,
81 * and wait some random time. Let's pick a random time in the range 100ms…2000ms,
82 * linearly scaled by the number of failed attempts. */
83 c->bus = sd_bus_flush_close_unref(c->bus);
84
85 usec_t usec =
86 UINT64_C(100) * USEC_PER_MSEC +
87 random_u64_range(UINT64_C(1900) * USEC_PER_MSEC * n_attempts / MAX_ATTEMPTS);
88 (void) usleep_safe(usec);
89 }
90
91 if (!c->bus) {
92 r = bus_connect_system_systemd(&c->bus);
93 if (r == -ECONNREFUSED && n_attempts < 64) {
94 log_debug_errno(r, "Failed to %s to system bus, retrying after a slight delay: %m",
95 n_attempts <= 1 ? "connect" : "reconnect");
96 continue;
97 }
98 if (r < 0)
99 return log_error_errno(r, "Failed to reconnect to system bus: %m");
100 }
101
102 FOREACH_ELEMENT(e, table) {
103 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
104 _cleanup_free_ char *state = NULL, *path = NULL;
105
106 path = unit_dbus_path_from_name(e->special);
107 if (!path)
108 return log_oom();
109
110 r = sd_bus_get_property_string(
111 c->bus,
112 "org.freedesktop.systemd1",
113 path,
114 "org.freedesktop.systemd1.Unit",
115 "ActiveState",
116 &error,
117 &state);
118 if ((r == -ENOTCONN || bus_error_is_connection(&error)) &&
119 n_attempts < MAX_ATTEMPTS) {
120 log_debug_errno(r, "Failed to get state of %s, retrying after a slight delay: %s",
121 e->special, bus_error_message(&error, r));
122 break;
123 }
124 if (r < 0)
125 return log_warning_errno(r, "Failed to get state of %s: %s", e->special, bus_error_message(&error, r));
126
127 if (STR_IN_SET(state, "active", "reloading"))
128 return e->runlevel;
129 }
130 if (r >= 0)
131 return 0;
132 }
133}
134
135static int on_reboot(int argc, char *argv[], void *userdata) {
136 Context *c = ASSERT_PTR(userdata);
137 usec_t t = 0, boottime;
138 int r, q = 0;
139
140 /* We finished start-up, so let's write the utmp record and send the audit msg. */
141
142#if HAVE_AUDIT
143 if (c->audit_fd >= 0)
144 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
145 errno != EPERM)
146 q = log_error_errno(errno, "Failed to send audit message: %m");
147#endif
148
149 /* If this call fails, then utmp_put_reboot() will fix to the current time. */
150 (void) get_startup_monotonic_time(c, &t);
151 boottime = map_clock_usec(t, CLOCK_MONOTONIC, CLOCK_REALTIME);
152 /* We query the recorded monotonic time here (instead of the system clock CLOCK_REALTIME), even
153 * though we actually want the system clock time. That's because there's a likely chance that the
154 * system clock wasn't set right during early boot. By manually converting the monotonic clock to the
155 * system clock here we can compensate for incorrectly set clocks during early boot. */
156
157 r = utmp_put_reboot(boottime);
158 if (r < 0)
159 return log_error_errno(r, "Failed to write utmp record: %m");
160
161 return q;
162}
163
164static int on_shutdown(int argc, char *argv[], void *userdata) {
165 int r, q = 0;
166
167 /* We started shut-down, so let's write the utmp record and send the audit msg. */
168
169#if HAVE_AUDIT
170 Context *c = ASSERT_PTR(userdata);
171
172 if (c->audit_fd >= 0)
173 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
174 errno != EPERM)
175 q = log_error_errno(errno, "Failed to send audit message: %m");
176#endif
177
178 r = utmp_put_shutdown();
179 if (r < 0)
180 return log_error_errno(r, "Failed to write utmp record: %m");
181
182 return q;
183}
184
185static int on_runlevel(int argc, char *argv[], void *userdata) {
186 Context *c = ASSERT_PTR(userdata);
187 int r, q = 0, previous, runlevel;
188
189 /* We finished changing runlevel, so let's write the utmp record and send the audit msg. */
190
191 /* First, get last runlevel */
192 r = utmp_get_runlevel(&previous, NULL);
193 if (r < 0) {
194 if (!IN_SET(r, -ESRCH, -ENOENT))
195 return log_error_errno(r, "Failed to get the last runlevel from utmp: %m");
196
197 previous = 0;
198 }
199
200 /* Secondly, get new runlevel */
201 runlevel = get_current_runlevel(c);
202 if (runlevel < 0)
203 return runlevel;
204 if (runlevel == 0) {
205 log_warning("Failed to get the current runlevel, utmp update skipped.");
206 return 0;
207 }
208
209 if (previous == runlevel)
210 return 0;
211
212#if HAVE_AUDIT
213 if (c->audit_fd >= 0) {
214 char s[STRLEN("old-level=_ new-level=_") + 1];
215
216 xsprintf(s, "old-level=%c new-level=%c",
217 previous > 0 ? previous : 'N',
218 runlevel);
219
220 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s,
221 "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
222 q = log_error_errno(errno, "Failed to send audit message: %m");
223 }
224#endif
225
226 r = utmp_put_runlevel(runlevel, previous);
227 if (r < 0 && !IN_SET(r, -ESRCH, -ENOENT))
228 return log_error_errno(r, "Failed to write utmp record: %m");
229
230 return q;
231}
232
233static int run(int argc, char *argv[]) {
234 static const Verb verbs[] = {
235 { "reboot", 1, 1, 0, on_reboot },
236 { "shutdown", 1, 1, 0, on_shutdown },
237 { "runlevel", 1, 1, 0, on_runlevel },
238 {}
239 };
240
241 _cleanup_(context_clear) Context c = {
242 .audit_fd = -EBADF,
243 };
244
245 log_setup();
246
247 umask(0022);
248
249 c.audit_fd = open_audit_fd_or_warn();
250
251 return dispatch_verb(argc, argv, verbs, &c);
252}
253
254DEFINE_MAIN_FUNCTION(run);