1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
16 //#include "alloc-util.h"
17 #include "bus-error.h"
18 //#include "bus-util.h"
19 //#include "format-util.h"
22 #include "process-util.h"
23 //#include "special.h"
25 //#include "unit-name.h"
27 #include "utmp-wtmp.h"
29 /// Additional includes needed by elogind
30 #include "string-util.h"
31 #include "time-util.h"
32 #include "update-utmp.h"
33 typedef struct Context {
40 #if 0 /// UNNEEDED by elogind
41 static usec_t get_startup_time(Context *c) {
42 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
48 r = sd_bus_get_property_trivial(
50 "org.freedesktop.systemd1",
51 "/org/freedesktop/systemd1",
52 "org.freedesktop.systemd1.Manager",
57 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
64 static int get_current_runlevel(Context *c) {
69 /* The first target of this list that is active or has
70 * a job scheduled wins. We prefer runlevels 5 and 3
71 * here over the others, since these are the main
72 * runlevels used on Fedora. It might make sense to
73 * change the order on some distributions. */
74 { '5', SPECIAL_GRAPHICAL_TARGET },
75 { '3', SPECIAL_MULTI_USER_TARGET },
76 { '1', SPECIAL_RESCUE_TARGET },
79 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
85 for (i = 0; i < ELEMENTSOF(table); i++) {
86 _cleanup_free_ char *state = NULL, *path = NULL;
88 path = unit_dbus_path_from_name(table[i].special);
92 r = sd_bus_get_property_string(
94 "org.freedesktop.systemd1",
96 "org.freedesktop.systemd1.Unit",
101 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
103 if (STR_IN_SET(state, "active", "reloading"))
104 return table[i].runlevel;
111 static int on_reboot(Context *c) {
117 /* We finished start-up, so let's write the utmp
118 * record and send the audit msg */
121 if (c->audit_fd >= 0)
122 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
124 r = log_error_errno(errno, "Failed to send audit message: %m");
128 #if 0 /// systemd hasn't started the system, so elogind always uses NOW()
129 /* If this call fails it will return 0, which
130 * utmp_put_reboot() will then fix to the current time */
131 t = get_startup_time(c);
133 t = now(CLOCK_REALTIME);
136 q = utmp_put_reboot(t);
138 log_error_errno(q, "Failed to write utmp record: %m");
145 static int on_shutdown(Context *c) {
150 /* We started shut-down, so let's write the utmp
151 * record and send the audit msg */
154 if (c->audit_fd >= 0)
155 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
157 r = log_error_errno(errno, "Failed to send audit message: %m");
161 q = utmp_put_shutdown();
163 log_error_errno(q, "Failed to write utmp record: %m");
170 #if 0 /// UNNEEDED by elogind
171 static int on_runlevel(Context *c) {
172 int r = 0, q, previous, runlevel;
176 /* We finished changing runlevel, so let's write the
177 * utmp record and send the audit msg */
179 /* First, get last runlevel */
180 q = utmp_get_runlevel(&previous, NULL);
183 if (!IN_SET(q, -ESRCH, -ENOENT))
184 return log_error_errno(q, "Failed to get current runlevel: %m");
189 /* Secondly, get new runlevel */
190 runlevel = get_current_runlevel(c);
195 if (previous == runlevel)
199 if (c->audit_fd >= 0) {
200 _cleanup_free_ char *s = NULL;
202 if (asprintf(&s, "old-level=%c new-level=%c",
203 previous > 0 ? previous : 'N',
204 runlevel > 0 ? runlevel : 'N') < 0)
207 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
208 r = log_error_errno(errno, "Failed to send audit message: %m");
212 q = utmp_put_runlevel(runlevel, previous);
213 if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) {
214 log_error_errno(q, "Failed to write utmp record: %m");
222 #if 0 /// elogind needs this to be a callable function
223 int main(int argc, char *argv[]) {
225 void update_utmp(int argc, char* argv[]) {
232 #if 0 /// UNNEEDED by elogind
235 if (getppid() != 1) {
236 log_error("This program should be invoked by init only.");
241 log_error("This program requires one argument.");
245 log_set_target(LOG_TARGET_AUTO);
246 log_parse_environment();
256 /* If the kernel lacks netlink or audit support,
257 * don't worry about it. */
258 c.audit_fd = audit_open();
259 if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
260 log_error_errno(errno, "Failed to connect to audit log: %m");
262 #if 0 /// UNNEEDED by elogind
263 r = bus_connect_system_systemd(&c.bus);
265 log_error_errno(r, "Failed to get D-Bus connection: %m");
270 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
272 if (streq(argv[1], "reboot"))
274 else if (streq(argv[1], "shutdown"))
276 else if (streq(argv[1], "runlevel"))
279 log_error("Unknown command %s", argv[1]);
283 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
287 if (streq(argv[1], "reboot"))
289 else if (streq(argv[1], "shutdown"))
290 (void)on_shutdown(&c);
294 audit_close(c.audit_fd);
297 #if 0 /// UNNEEDED by elogind
298 sd_bus_flush_close_unref(c.bus);
299 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;