1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
18 //#include "alloc-util.h"
19 #include "bus-error.h"
20 //#include "bus-util.h"
21 //#include "format-util.h"
24 #include "process-util.h"
25 //#include "special.h"
27 //#include "unit-name.h"
29 #include "utmp-wtmp.h"
31 /// Additional includes needed by elogind
32 #include "string-util.h"
33 #include "time-util.h"
34 #include "update-utmp.h"
35 typedef struct Context {
42 #if 0 /// UNNEEDED by elogind
43 static usec_t get_startup_time(Context *c) {
44 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
50 r = sd_bus_get_property_trivial(
52 "org.freedesktop.systemd1",
53 "/org/freedesktop/systemd1",
54 "org.freedesktop.systemd1.Manager",
59 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
66 static int get_current_runlevel(Context *c) {
71 /* The first target of this list that is active or has
72 * a job scheduled wins. We prefer runlevels 5 and 3
73 * here over the others, since these are the main
74 * runlevels used on Fedora. It might make sense to
75 * change the order on some distributions. */
76 { '5', SPECIAL_GRAPHICAL_TARGET },
77 { '3', SPECIAL_MULTI_USER_TARGET },
78 { '1', SPECIAL_RESCUE_TARGET },
81 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
87 for (i = 0; i < ELEMENTSOF(table); i++) {
88 _cleanup_free_ char *state = NULL, *path = NULL;
90 path = unit_dbus_path_from_name(table[i].special);
94 r = sd_bus_get_property_string(
96 "org.freedesktop.systemd1",
98 "org.freedesktop.systemd1.Unit",
103 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
105 if (STR_IN_SET(state, "active", "reloading"))
106 return table[i].runlevel;
113 static int on_reboot(Context *c) {
119 /* We finished start-up, so let's write the utmp
120 * record and send the audit msg */
123 if (c->audit_fd >= 0)
124 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
126 r = log_error_errno(errno, "Failed to send audit message: %m");
130 #if 0 /// systemd hasn't started the system, so elogind always uses NOW()
131 /* If this call fails it will return 0, which
132 * utmp_put_reboot() will then fix to the current time */
133 t = get_startup_time(c);
135 t = now(CLOCK_REALTIME);
138 q = utmp_put_reboot(t);
140 log_error_errno(q, "Failed to write utmp record: %m");
147 static int on_shutdown(Context *c) {
152 /* We started shut-down, so let's write the utmp
153 * record and send the audit msg */
156 if (c->audit_fd >= 0)
157 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
159 r = log_error_errno(errno, "Failed to send audit message: %m");
163 q = utmp_put_shutdown();
165 log_error_errno(q, "Failed to write utmp record: %m");
172 #if 0 /// UNNEEDED by elogind
173 static int on_runlevel(Context *c) {
174 int r = 0, q, previous, runlevel;
178 /* We finished changing runlevel, so let's write the
179 * utmp record and send the audit msg */
181 /* First, get last runlevel */
182 q = utmp_get_runlevel(&previous, NULL);
185 if (!IN_SET(q, -ESRCH, -ENOENT))
186 return log_error_errno(q, "Failed to get current runlevel: %m");
191 /* Secondly, get new runlevel */
192 runlevel = get_current_runlevel(c);
197 if (previous == runlevel)
201 if (c->audit_fd >= 0) {
202 _cleanup_free_ char *s = NULL;
204 if (asprintf(&s, "old-level=%c new-level=%c",
205 previous > 0 ? previous : 'N',
206 runlevel > 0 ? runlevel : 'N') < 0)
209 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
210 r = log_error_errno(errno, "Failed to send audit message: %m");
214 q = utmp_put_runlevel(runlevel, previous);
215 if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) {
216 log_error_errno(q, "Failed to write utmp record: %m");
224 #if 0 /// elogind needs this to be a callable function
225 int main(int argc, char *argv[]) {
227 void update_utmp(int argc, char* argv[]) {
234 #if 0 /// UNNEEDED by elogind
237 if (getppid() != 1) {
238 log_error("This program should be invoked by init only.");
243 log_error("This program requires one argument.");
247 log_set_target(LOG_TARGET_AUTO);
248 log_parse_environment();
258 /* If the kernel lacks netlink or audit support,
259 * don't worry about it. */
260 c.audit_fd = audit_open();
261 if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
262 log_error_errno(errno, "Failed to connect to audit log: %m");
264 #if 0 /// UNNEEDED by elogind
265 r = bus_connect_system_systemd(&c.bus);
267 log_error_errno(r, "Failed to get D-Bus connection: %m");
272 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
274 if (streq(argv[1], "reboot"))
276 else if (streq(argv[1], "shutdown"))
278 else if (streq(argv[1], "runlevel"))
281 log_error("Unknown command %s", argv[1]);
285 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
289 if (streq(argv[1], "reboot"))
291 else if (streq(argv[1], "shutdown"))
292 (void)on_shutdown(&c);
296 audit_close(c.audit_fd);
299 #if 0 /// UNNEEDED by elogind
300 sd_bus_flush_close_unref(c.bus);
301 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;