1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
16 #include "alloc-util.h"
17 #include "bus-error.h"
19 #include "format-util.h"
22 #include "process-util.h"
25 #include "unit-name.h"
27 #include "utmp-wtmp.h"
29 typedef struct Context
{
36 static usec_t
get_startup_time(Context
*c
) {
37 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
43 r
= sd_bus_get_property_trivial(
45 "org.freedesktop.systemd1",
46 "/org/freedesktop/systemd1",
47 "org.freedesktop.systemd1.Manager",
52 log_error_errno(r
, "Failed to get timestamp: %s", bus_error_message(&error
, r
));
59 static int get_current_runlevel(Context
*c
) {
64 /* The first target of this list that is active or has
65 * a job scheduled wins. We prefer runlevels 5 and 3
66 * here over the others, since these are the main
67 * runlevels used on Fedora. It might make sense to
68 * change the order on some distributions. */
69 { '5', SPECIAL_GRAPHICAL_TARGET
},
70 { '3', SPECIAL_MULTI_USER_TARGET
},
71 { '1', SPECIAL_RESCUE_TARGET
},
74 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
80 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
81 _cleanup_free_
char *state
= NULL
, *path
= NULL
;
83 path
= unit_dbus_path_from_name(table
[i
].special
);
87 r
= sd_bus_get_property_string(
89 "org.freedesktop.systemd1",
91 "org.freedesktop.systemd1.Unit",
96 return log_warning_errno(r
, "Failed to get state: %s", bus_error_message(&error
, r
));
98 if (STR_IN_SET(state
, "active", "reloading"))
99 return table
[i
].runlevel
;
105 static int on_reboot(Context
*c
) {
111 /* We finished start-up, so let's write the utmp
112 * record and send the audit msg */
115 if (c
->audit_fd
>= 0)
116 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_BOOT
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
118 r
= log_error_errno(errno
, "Failed to send audit message: %m");
122 /* If this call fails it will return 0, which
123 * utmp_put_reboot() will then fix to the current time */
124 t
= get_startup_time(c
);
126 q
= utmp_put_reboot(t
);
128 log_error_errno(q
, "Failed to write utmp record: %m");
135 static int on_shutdown(Context
*c
) {
140 /* We started shut-down, so let's write the utmp
141 * record and send the audit msg */
144 if (c
->audit_fd
>= 0)
145 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_SHUTDOWN
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
147 r
= log_error_errno(errno
, "Failed to send audit message: %m");
151 q
= utmp_put_shutdown();
153 log_error_errno(q
, "Failed to write utmp record: %m");
160 static int on_runlevel(Context
*c
) {
161 int r
= 0, q
, previous
, runlevel
;
165 /* We finished changing runlevel, so let's write the
166 * utmp record and send the audit msg */
168 /* First, get last runlevel */
169 q
= utmp_get_runlevel(&previous
, NULL
);
172 if (!IN_SET(q
, -ESRCH
, -ENOENT
))
173 return log_error_errno(q
, "Failed to get current runlevel: %m");
178 /* Secondly, get new runlevel */
179 runlevel
= get_current_runlevel(c
);
184 if (previous
== runlevel
)
188 if (c
->audit_fd
>= 0) {
189 _cleanup_free_
char *s
= NULL
;
191 if (asprintf(&s
, "old-level=%c new-level=%c",
192 previous
> 0 ? previous
: 'N',
193 runlevel
> 0 ? runlevel
: 'N') < 0)
196 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_RUNLEVEL
, s
, "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 && errno
!= EPERM
)
197 r
= log_error_errno(errno
, "Failed to send audit message: %m");
201 q
= utmp_put_runlevel(runlevel
, previous
);
202 if (q
< 0 && !IN_SET(q
, -ESRCH
, -ENOENT
)) {
203 log_error_errno(q
, "Failed to write utmp record: %m");
210 int main(int argc
, char *argv
[]) {
218 if (getppid() != 1) {
219 log_error("This program should be invoked by init only.");
224 log_error("This program requires one argument.");
228 log_set_target(LOG_TARGET_AUTO
);
229 log_parse_environment();
235 /* If the kernel lacks netlink or audit support,
236 * don't worry about it. */
237 c
.audit_fd
= audit_open();
238 if (c
.audit_fd
< 0 && !IN_SET(errno
, EAFNOSUPPORT
, EPROTONOSUPPORT
))
239 log_error_errno(errno
, "Failed to connect to audit log: %m");
241 r
= bus_connect_system_systemd(&c
.bus
);
243 log_error_errno(r
, "Failed to get D-Bus connection: %m");
248 log_debug("systemd-update-utmp running as pid "PID_FMT
, getpid_cached());
250 if (streq(argv
[1], "reboot"))
252 else if (streq(argv
[1], "shutdown"))
254 else if (streq(argv
[1], "runlevel"))
257 log_error("Unknown command %s", argv
[1]);
261 log_debug("systemd-update-utmp stopped as pid "PID_FMT
, getpid_cached());
266 audit_close(c
.audit_fd
);
269 sd_bus_flush_close_unref(c
.bus
);
270 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;