1:255.16-alt1
[systemd_ALT.git] / src / update-utmp / update-utmp.c
blob4ee935e0441335914f6fb2c871543c75b7fde718
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
8 #if HAVE_AUDIT
9 #include <libaudit.h>
10 #endif
12 #include "sd-bus.h"
14 #include "alloc-util.h"
15 #include "bus-error.h"
16 #include "bus-locator.h"
17 #include "bus-util.h"
18 #include "format-util.h"
19 #include "log.h"
20 #include "macro.h"
21 #include "main-func.h"
22 #include "process-util.h"
23 #include "random-util.h"
24 #include "special.h"
25 #include "stdio-util.h"
26 #include "strv.h"
27 #include "unit-name.h"
28 #include "utmp-wtmp.h"
29 #include "verbs.h"
31 typedef struct Context {
32 sd_bus *bus;
33 #if HAVE_AUDIT
34 int audit_fd;
35 #endif
36 } Context;
38 static void context_clear(Context *c) {
39 assert(c);
41 c->bus = sd_bus_flush_close_unref(c->bus);
42 #if HAVE_AUDIT
43 if (c->audit_fd >= 0)
44 audit_close(c->audit_fd);
45 c->audit_fd = -EBADF;
46 #endif
49 static int get_startup_monotonic_time(Context *c, usec_t *ret) {
50 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
51 int r;
53 assert(c);
54 assert(ret);
56 r = bus_get_property_trivial(
57 c->bus,
58 bus_systemd_mgr,
59 "UserspaceTimestampMonotonic",
60 &error,
61 't', ret);
62 if (r < 0)
63 return log_warning_errno(r, "Failed to get timestamp, ignoring: %s", bus_error_message(&error, r));
65 return 0;
68 static int get_current_runlevel(Context *c) {
69 static const struct {
70 const int runlevel;
71 const char *special;
72 } table[] = {
73 /* The first target of this list that is active or has a job scheduled wins. We prefer
74 * runlevels 5 and 3 here over the others, since these are the main runlevels used on Fedora.
75 * It might make sense to change the order on some distributions. */
76 { '5', SPECIAL_GRAPHICAL_TARGET },
77 { '3', SPECIAL_MULTI_USER_TARGET },
78 { '1', SPECIAL_RESCUE_TARGET },
80 int r;
82 assert(c);
84 for (unsigned n_attempts = 0;;) {
85 FOREACH_ARRAY(e, table, ELEMENTSOF(table)) {
86 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
87 _cleanup_free_ char *state = NULL, *path = NULL;
89 path = unit_dbus_path_from_name(e->special);
90 if (!path)
91 return log_oom();
93 r = sd_bus_get_property_string(
94 c->bus,
95 "org.freedesktop.systemd1",
96 path,
97 "org.freedesktop.systemd1.Unit",
98 "ActiveState",
99 &error,
100 &state);
101 if ((r == -ENOTCONN ||
102 sd_bus_error_has_names(&error,
103 SD_BUS_ERROR_NO_REPLY,
104 SD_BUS_ERROR_DISCONNECTED)) &&
105 ++n_attempts < 64) {
107 /* systemd might have dropped off momentarily, let's not make this an error,
108 * and wait some random time. Let's pick a random time in the range 0ms…250ms,
109 * linearly scaled by the number of failed attempts. */
111 usec_t usec = random_u64_range(UINT64_C(10) * USEC_PER_MSEC +
112 UINT64_C(240) * USEC_PER_MSEC * n_attempts/64);
113 log_debug_errno(r, "Failed to get state of %s, retrying after %s: %s",
114 e->special, FORMAT_TIMESPAN(usec, USEC_PER_MSEC), bus_error_message(&error, r));
115 (void) usleep_safe(usec);
116 goto reconnect;
118 if (r < 0)
119 return log_warning_errno(r, "Failed to get state of %s: %s", e->special, bus_error_message(&error, r));
121 if (STR_IN_SET(state, "active", "reloading"))
122 return e->runlevel;
125 return 0;
127 reconnect:
128 c->bus = sd_bus_flush_close_unref(c->bus);
129 r = bus_connect_system_systemd(&c->bus);
130 if (r < 0)
131 return log_error_errno(r, "Failed to reconnect to system bus: %m");
135 static 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;
140 /* We finished start-up, so let's write the utmp record and send the audit msg. */
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
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. */
157 r = utmp_put_reboot(boottime);
158 if (r < 0)
159 return log_error_errno(r, "Failed to write utmp record: %m");
161 return q;
164 static int on_shutdown(int argc, char *argv[], void *userdata) {
165 int r, q = 0;
167 /* We started shut-down, so let's write the utmp record and send the audit msg. */
169 #if HAVE_AUDIT
170 Context *c = ASSERT_PTR(userdata);
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
178 r = utmp_put_shutdown();
179 if (r < 0)
180 return log_error_errno(r, "Failed to write utmp record: %m");
182 return q;
185 static int on_runlevel(int argc, char *argv[], void *userdata) {
186 Context *c = ASSERT_PTR(userdata);
187 int r, q = 0, previous, runlevel;
189 /* We finished changing runlevel, so let's write the utmp record and send the audit msg. */
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");
197 previous = 0;
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;
209 if (previous == runlevel)
210 return 0;
212 #if HAVE_AUDIT
213 if (c->audit_fd >= 0) {
214 char s[STRLEN("old-level=_ new-level=_") + 1];
216 xsprintf(s, "old-level=%c new-level=%c",
217 previous > 0 ? previous : 'N',
218 runlevel);
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");
224 #endif
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");
230 return q;
233 static 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 },
241 _cleanup_(context_clear) Context c = {
242 #if HAVE_AUDIT
243 .audit_fd = -EBADF,
244 #endif
246 int r;
248 log_setup();
250 umask(0022);
252 #if HAVE_AUDIT
253 /* If the kernel lacks netlink or audit support, don't worry about it. */
254 c.audit_fd = audit_open();
255 if (c.audit_fd < 0)
256 log_full_errno(IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT) ? LOG_DEBUG : LOG_WARNING,
257 errno, "Failed to connect to audit log, ignoring: %m");
258 #endif
259 r = bus_connect_system_systemd(&c.bus);
260 if (r < 0)
261 return log_error_errno(r, "Failed to get D-Bus connection: %m");
263 return dispatch_verb(argc, argv, verbs, &c);
266 DEFINE_MAIN_FUNCTION(run);