]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
4927fcae 2
ca78ad1d 3#include <sys/stat.h>
4927fcae 4
89456fce
TG
5#include "sd-bus.h"
6
b5efdb8a
LP
7#include "alloc-util.h"
8#include "bus-error.h"
b7147168 9#include "bus-locator.h"
b5efdb8a 10#include "bus-util.h"
c992250c 11#include "libaudit-util.h"
4927fcae 12#include "log.h"
72682957 13#include "main-func.h"
fcb23431 14#include "random-util.h"
4927fcae 15#include "special.h"
ecfd9a99 16#include "stdio-util.h"
dfc7f594 17#include "strv.h"
8857aa74
DDM
18#include "time-util.h"
19#include "unit-def.h"
b5efdb8a 20#include "utmp-wtmp.h"
230f663e 21#include "verbs.h"
4927fcae
LP
22
23typedef struct Context {
89456fce 24 sd_bus *bus;
4927fcae 25 int audit_fd;
4927fcae
LP
26} Context;
27
3664cbab
YW
28static void context_clear(Context *c) {
29 assert(c);
30
31 c->bus = sd_bus_flush_close_unref(c->bus);
9c6afab6 32 c->audit_fd = close_audit_fd(c->audit_fd);
3664cbab
YW
33}
34
3950d8f6 35static int get_startup_monotonic_time(Context *c, usec_t *ret) {
4afd3348 36 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
89456fce 37 int r;
4927fcae 38
4927fcae 39 assert(c);
3950d8f6 40 assert(ret);
4927fcae 41
85d040da
YW
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
3950d8f6
YW
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));
4927fcae 56
3950d8f6 57 return 0;
4927fcae
LP
58}
59
f27ae592
YW
60#define MAX_ATTEMPTS 64u
61
4927fcae
LP
62static int get_current_runlevel(Context *c) {
63 static const struct {
64 const int runlevel;
65 const char *special;
66 } table[] = {
309491e4
YW
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. */
d5d8429a
LP
70 { '5', SPECIAL_GRAPHICAL_TARGET },
71 { '3', SPECIAL_MULTI_USER_TARGET },
72 { '1', SPECIAL_RESCUE_TARGET },
4927fcae 73 };
89456fce 74 int r;
4927fcae
LP
75
76 assert(c);
77
fcb23431 78 for (unsigned n_attempts = 0;;) {
ec9fd0d4
DDM
79 if (n_attempts++ > 0) {
80 /* systemd might have dropped off momentarily, let's not make this an error,
f27ae592 81 * and wait some random time. Let's pick a random time in the range 100ms…2000ms,
ec9fd0d4
DDM
82 * linearly scaled by the number of failed attempts. */
83 c->bus = sd_bus_flush_close_unref(c->bus);
84
f27ae592
YW
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);
ec9fd0d4 88 (void) usleep_safe(usec);
85d040da 89 }
ec9fd0d4 90
85d040da 91 if (!c->bus) {
ec9fd0d4
DDM
92 r = bus_connect_system_systemd(&c->bus);
93 if (r == -ECONNREFUSED && n_attempts < 64) {
85d040da
YW
94 log_debug_errno(r, "Failed to %s to system bus, retrying after a slight delay: %m",
95 n_attempts <= 1 ? "connect" : "reconnect");
ec9fd0d4
DDM
96 continue;
97 }
98 if (r < 0)
99 return log_error_errno(r, "Failed to reconnect to system bus: %m");
100 }
101
85471164 102 FOREACH_ELEMENT(e, table) {
fcb23431
YW
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);
ac004f4c 118 if ((r == -ENOTCONN || bus_error_is_connection(&error)) &&
f27ae592 119 n_attempts < MAX_ATTEMPTS) {
ec9fd0d4
DDM
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;
fcb23431
YW
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 }
ec9fd0d4
DDM
130 if (r >= 0)
131 return 0;
4927fcae 132 }
4927fcae
LP
133}
134
230f663e
YW
135static int on_reboot(int argc, char *argv[], void *userdata) {
136 Context *c = ASSERT_PTR(userdata);
3950d8f6 137 usec_t t = 0, boottime;
4c40ab20 138 int r, q = 0;
4927fcae 139
309491e4 140 /* We finished start-up, so let's write the utmp record and send the audit msg. */
4927fcae 141
349cc4a5 142#if HAVE_AUDIT
4927fcae 143 if (c->audit_fd >= 0)
0aa281df 144 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
38cd55b0 145 errno != EPERM)
4c40ab20 146 q = log_error_errno(errno, "Failed to send audit message: %m");
4927fcae
LP
147#endif
148
3950d8f6
YW
149 /* If this call fails, then utmp_put_reboot() will fix to the current time. */
150 (void) get_startup_monotonic_time(c, &t);
f813b623 151 boottime = map_clock_usec(t, CLOCK_MONOTONIC, CLOCK_REALTIME);
309491e4
YW
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. */
f813b623 156
4c40ab20
YW
157 r = utmp_put_reboot(boottime);
158 if (r < 0)
159 return log_error_errno(r, "Failed to write utmp record: %m");
4927fcae 160
4c40ab20 161 return q;
4927fcae
LP
162}
163
230f663e 164static int on_shutdown(int argc, char *argv[], void *userdata) {
4c40ab20 165 int r, q = 0;
4927fcae 166
309491e4 167 /* We started shut-down, so let's write the utmp record and send the audit msg. */
4927fcae 168
349cc4a5 169#if HAVE_AUDIT
230f663e
YW
170 Context *c = ASSERT_PTR(userdata);
171
4927fcae 172 if (c->audit_fd >= 0)
0aa281df 173 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
38cd55b0 174 errno != EPERM)
4c40ab20 175 q = log_error_errno(errno, "Failed to send audit message: %m");
4927fcae
LP
176#endif
177
4c40ab20
YW
178 r = utmp_put_shutdown();
179 if (r < 0)
180 return log_error_errno(r, "Failed to write utmp record: %m");
4927fcae 181
4c40ab20 182 return q;
4927fcae
LP
183}
184
230f663e
YW
185static int on_runlevel(int argc, char *argv[], void *userdata) {
186 Context *c = ASSERT_PTR(userdata);
4c40ab20 187 int r, q = 0, previous, runlevel;
4927fcae 188
309491e4 189 /* We finished changing runlevel, so let's write the utmp record and send the audit msg. */
4927fcae
LP
190
191 /* First, get last runlevel */
4c40ab20 192 r = utmp_get_runlevel(&previous, NULL);
4c40ab20
YW
193 if (r < 0) {
194 if (!IN_SET(r, -ESRCH, -ENOENT))
87ae5453 195 return log_error_errno(r, "Failed to get the last runlevel from utmp: %m");
4927fcae 196
4927fcae
LP
197 previous = 0;
198 }
199
830f6caa 200 /* Secondly, get new runlevel */
1cea22a5
LP
201 runlevel = get_current_runlevel(c);
202 if (runlevel < 0)
4927fcae 203 return runlevel;
c413bb28 204 if (runlevel == 0) {
87ae5453 205 log_warning("Failed to get the current runlevel, utmp update skipped.");
c413bb28
ZJS
206 return 0;
207 }
e57cd3fb 208
4927fcae
LP
209 if (previous == runlevel)
210 return 0;
211
349cc4a5 212#if HAVE_AUDIT
4927fcae 213 if (c->audit_fd >= 0) {
ecfd9a99 214 char s[STRLEN("old-level=_ new-level=_") + 1];
4927fcae 215
ecfd9a99
ZJS
216 xsprintf(s, "old-level=%c new-level=%c",
217 previous > 0 ? previous : 'N',
218 runlevel);
4927fcae 219
ecfd9a99
ZJS
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)
4c40ab20 222 q = log_error_errno(errno, "Failed to send audit message: %m");
4927fcae
LP
223 }
224#endif
225
4c40ab20
YW
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");
4927fcae 229
4c40ab20 230 return q;
4927fcae
LP
231}
232
72682957 233static int run(int argc, char *argv[]) {
230f663e
YW
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
3664cbab 241 _cleanup_(context_clear) Context c = {
254d1313 242 .audit_fd = -EBADF,
1cea22a5 243 };
4927fcae 244
d2acb93d 245 log_setup();
4927fcae 246
4c12626c
LP
247 umask(0022);
248
9c6afab6 249 c.audit_fd = open_audit_fd_or_warn();
4927fcae 250
230f663e 251 return dispatch_verb(argc, argv, verbs, &c);
4927fcae 252}
72682957
ZJS
253
254DEFINE_MAIN_FUNCTION(run);