1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 //#include "alloc-util.h"
14 #include "bus-error.h"
15 //#include "bus-util.h"
16 //#include "format-util.h"
19 #include "process-util.h"
20 //#include "special.h"
22 //#include "unit-name.h"
24 #include "utmp-wtmp.h"
26 /// Additional includes needed by elogind
27 #include "string-util.h"
28 #include "time-util.h"
29 #include "update-utmp.h"
30 typedef struct Context {
37 #if 0 /// UNNEEDED by elogind
38 static usec_t get_startup_time(Context *c) {
39 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
45 r = sd_bus_get_property_trivial(
47 "org.freedesktop.systemd1",
48 "/org/freedesktop/systemd1",
49 "org.freedesktop.systemd1.Manager",
54 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
61 static int get_current_runlevel(Context *c) {
66 /* The first target of this list that is active or has
67 * a job scheduled wins. We prefer runlevels 5 and 3
68 * here over the others, since these are the main
69 * runlevels used on Fedora. It might make sense to
70 * change the order on some distributions. */
71 { '5', SPECIAL_GRAPHICAL_TARGET },
72 { '3', SPECIAL_MULTI_USER_TARGET },
73 { '1', SPECIAL_RESCUE_TARGET },
76 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
82 for (i = 0; i < ELEMENTSOF(table); i++) {
83 _cleanup_free_ char *state = NULL, *path = NULL;
85 path = unit_dbus_path_from_name(table[i].special);
89 r = sd_bus_get_property_string(
91 "org.freedesktop.systemd1",
93 "org.freedesktop.systemd1.Unit",
98 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
100 if (STR_IN_SET(state, "active", "reloading"))
101 return table[i].runlevel;
108 static int on_reboot(Context *c) {
114 /* We finished start-up, so let's write the utmp
115 * record and send the audit msg */
118 if (c->audit_fd >= 0)
119 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
121 r = log_error_errno(errno, "Failed to send audit message: %m");
125 #if 0 /// systemd hasn't started the system, so elogind always uses NOW()
126 /* If this call fails it will return 0, which
127 * utmp_put_reboot() will then fix to the current time */
128 t = get_startup_time(c);
130 t = now(CLOCK_REALTIME);
133 q = utmp_put_reboot(t);
135 log_error_errno(q, "Failed to write utmp record: %m");
142 static int on_shutdown(Context *c) {
147 /* We started shut-down, so let's write the utmp
148 * record and send the audit msg */
151 if (c->audit_fd >= 0)
152 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
154 r = log_error_errno(errno, "Failed to send audit message: %m");
158 q = utmp_put_shutdown();
160 log_error_errno(q, "Failed to write utmp record: %m");
167 #if 0 /// UNNEEDED by elogind
168 static int on_runlevel(Context *c) {
169 int r = 0, q, previous, runlevel;
173 /* We finished changing runlevel, so let's write the
174 * utmp record and send the audit msg */
176 /* First, get last runlevel */
177 q = utmp_get_runlevel(&previous, NULL);
180 if (!IN_SET(q, -ESRCH, -ENOENT))
181 return log_error_errno(q, "Failed to get current runlevel: %m");
186 /* Secondly, get new runlevel */
187 runlevel = get_current_runlevel(c);
192 if (previous == runlevel)
196 if (c->audit_fd >= 0) {
197 _cleanup_free_ char *s = NULL;
199 if (asprintf(&s, "old-level=%c new-level=%c",
200 previous > 0 ? previous : 'N',
201 runlevel > 0 ? runlevel : 'N') < 0)
204 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
205 r = log_error_errno(errno, "Failed to send audit message: %m");
209 q = utmp_put_runlevel(runlevel, previous);
210 if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) {
211 log_error_errno(q, "Failed to write utmp record: %m");
219 #if 0 /// elogind needs this to be a callable function
220 int main(int argc, char *argv[]) {
222 void update_utmp(int argc, char* argv[]) {
229 #if 0 /// UNNEEDED by elogind
232 if (getppid() != 1) {
233 log_error("This program should be invoked by init only.");
238 log_error("This program requires one argument.");
242 log_set_target(LOG_TARGET_AUTO);
243 log_parse_environment();
253 /* If the kernel lacks netlink or audit support,
254 * don't worry about it. */
255 c.audit_fd = audit_open();
256 if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
257 log_error_errno(errno, "Failed to connect to audit log: %m");
259 #if 0 /// UNNEEDED by elogind
260 r = bus_connect_system_systemd(&c.bus);
262 log_error_errno(r, "Failed to get D-Bus connection: %m");
267 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
269 if (streq(argv[1], "reboot"))
271 else if (streq(argv[1], "shutdown"))
273 else if (streq(argv[1], "runlevel"))
276 log_error("Unknown command %s", argv[1]);
280 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
284 if (streq(argv[1], "reboot"))
286 else if (streq(argv[1], "shutdown"))
287 (void)on_shutdown(&c);
291 audit_close(c.audit_fd);
294 #if 0 /// UNNEEDED by elogind
295 sd_bus_flush_close_unref(c.bus);
296 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;