1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include "alloc-util.h"
14 #include "bus-error.h"
16 #include "format-util.h"
19 #include "process-util.h"
22 #include "unit-name.h"
24 #include "utmp-wtmp.h"
26 typedef struct Context
{
33 static usec_t
get_startup_time(Context
*c
) {
34 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
40 r
= sd_bus_get_property_trivial(
42 "org.freedesktop.systemd1",
43 "/org/freedesktop/systemd1",
44 "org.freedesktop.systemd1.Manager",
49 log_error_errno(r
, "Failed to get timestamp: %s", bus_error_message(&error
, r
));
56 static int get_current_runlevel(Context
*c
) {
61 /* The first target of this list that is active or has
62 * a job scheduled wins. We prefer runlevels 5 and 3
63 * here over the others, since these are the main
64 * runlevels used on Fedora. It might make sense to
65 * change the order on some distributions. */
66 { '5', SPECIAL_GRAPHICAL_TARGET
},
67 { '3', SPECIAL_MULTI_USER_TARGET
},
68 { '1', SPECIAL_RESCUE_TARGET
},
71 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
77 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
78 _cleanup_free_
char *state
= NULL
, *path
= NULL
;
80 path
= unit_dbus_path_from_name(table
[i
].special
);
84 r
= sd_bus_get_property_string(
86 "org.freedesktop.systemd1",
88 "org.freedesktop.systemd1.Unit",
93 return log_warning_errno(r
, "Failed to get state: %s", bus_error_message(&error
, r
));
95 if (STR_IN_SET(state
, "active", "reloading"))
96 return table
[i
].runlevel
;
102 static int on_reboot(Context
*c
) {
108 /* We finished start-up, so let's write the utmp
109 * record and send the audit msg */
112 if (c
->audit_fd
>= 0)
113 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_BOOT
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
115 r
= log_error_errno(errno
, "Failed to send audit message: %m");
119 /* If this call fails it will return 0, which
120 * utmp_put_reboot() will then fix to the current time */
121 t
= get_startup_time(c
);
123 q
= utmp_put_reboot(t
);
125 log_error_errno(q
, "Failed to write utmp record: %m");
132 static int on_shutdown(Context
*c
) {
137 /* We started shut-down, so let's write the utmp
138 * record and send the audit msg */
141 if (c
->audit_fd
>= 0)
142 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_SHUTDOWN
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
144 r
= log_error_errno(errno
, "Failed to send audit message: %m");
148 q
= utmp_put_shutdown();
150 log_error_errno(q
, "Failed to write utmp record: %m");
157 static int on_runlevel(Context
*c
) {
158 int r
= 0, q
, previous
, runlevel
;
162 /* We finished changing runlevel, so let's write the
163 * utmp record and send the audit msg */
165 /* First, get last runlevel */
166 q
= utmp_get_runlevel(&previous
, NULL
);
169 if (!IN_SET(q
, -ESRCH
, -ENOENT
))
170 return log_error_errno(q
, "Failed to get current runlevel: %m");
175 /* Secondly, get new runlevel */
176 runlevel
= get_current_runlevel(c
);
181 if (previous
== runlevel
)
185 if (c
->audit_fd
>= 0) {
186 _cleanup_free_
char *s
= NULL
;
188 if (asprintf(&s
, "old-level=%c new-level=%c",
189 previous
> 0 ? previous
: 'N',
190 runlevel
> 0 ? runlevel
: 'N') < 0)
193 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_RUNLEVEL
, s
, "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 && errno
!= EPERM
)
194 r
= log_error_errno(errno
, "Failed to send audit message: %m");
198 q
= utmp_put_runlevel(runlevel
, previous
);
199 if (q
< 0 && !IN_SET(q
, -ESRCH
, -ENOENT
)) {
200 log_error_errno(q
, "Failed to write utmp record: %m");
207 int main(int argc
, char *argv
[]) {
215 if (getppid() != 1) {
216 log_error("This program should be invoked by init only.");
221 log_error("This program requires one argument.");
225 log_set_target(LOG_TARGET_AUTO
);
226 log_parse_environment();
232 /* If the kernel lacks netlink or audit support,
233 * don't worry about it. */
234 c
.audit_fd
= audit_open();
235 if (c
.audit_fd
< 0 && !IN_SET(errno
, EAFNOSUPPORT
, EPROTONOSUPPORT
))
236 log_error_errno(errno
, "Failed to connect to audit log: %m");
238 r
= bus_connect_system_systemd(&c
.bus
);
240 log_error_errno(r
, "Failed to get D-Bus connection: %m");
245 log_debug("systemd-update-utmp running as pid "PID_FMT
, getpid_cached());
247 if (streq(argv
[1], "reboot"))
249 else if (streq(argv
[1], "shutdown"))
251 else if (streq(argv
[1], "runlevel"))
254 log_error("Unknown command %s", argv
[1]);
258 log_debug("systemd-update-utmp stopped as pid "PID_FMT
, getpid_cached());
263 audit_close(c
.audit_fd
);
266 sd_bus_flush_close_unref(c
.bus
);
267 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;