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"
21 #include "format-util.h"
24 #include "process-util.h"
27 #include "unit-name.h"
29 #include "utmp-wtmp.h"
31 typedef struct Context
{
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
;
107 static int on_reboot(Context
*c
) {
113 /* We finished start-up, so let's write the utmp
114 * record and send the audit msg */
117 if (c
->audit_fd
>= 0)
118 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_BOOT
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
120 r
= log_error_errno(errno
, "Failed to send audit message: %m");
124 /* If this call fails it will return 0, which
125 * utmp_put_reboot() will then fix to the current time */
126 t
= get_startup_time(c
);
128 q
= utmp_put_reboot(t
);
130 log_error_errno(q
, "Failed to write utmp record: %m");
137 static int on_shutdown(Context
*c
) {
142 /* We started shut-down, so let's write the utmp
143 * record and send the audit msg */
146 if (c
->audit_fd
>= 0)
147 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_SHUTDOWN
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
149 r
= log_error_errno(errno
, "Failed to send audit message: %m");
153 q
= utmp_put_shutdown();
155 log_error_errno(q
, "Failed to write utmp record: %m");
162 static int on_runlevel(Context
*c
) {
163 int r
= 0, q
, previous
, runlevel
;
167 /* We finished changing runlevel, so let's write the
168 * utmp record and send the audit msg */
170 /* First, get last runlevel */
171 q
= utmp_get_runlevel(&previous
, NULL
);
174 if (!IN_SET(q
, -ESRCH
, -ENOENT
))
175 return log_error_errno(q
, "Failed to get current runlevel: %m");
180 /* Secondly, get new runlevel */
181 runlevel
= get_current_runlevel(c
);
186 if (previous
== runlevel
)
190 if (c
->audit_fd
>= 0) {
191 _cleanup_free_
char *s
= NULL
;
193 if (asprintf(&s
, "old-level=%c new-level=%c",
194 previous
> 0 ? previous
: 'N',
195 runlevel
> 0 ? runlevel
: 'N') < 0)
198 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_RUNLEVEL
, s
, "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 && errno
!= EPERM
)
199 r
= log_error_errno(errno
, "Failed to send audit message: %m");
203 q
= utmp_put_runlevel(runlevel
, previous
);
204 if (q
< 0 && !IN_SET(q
, -ESRCH
, -ENOENT
)) {
205 log_error_errno(q
, "Failed to write utmp record: %m");
212 int main(int argc
, char *argv
[]) {
220 if (getppid() != 1) {
221 log_error("This program should be invoked by init only.");
226 log_error("This program requires one argument.");
230 log_set_target(LOG_TARGET_AUTO
);
231 log_parse_environment();
237 /* If the kernel lacks netlink or audit support,
238 * don't worry about it. */
239 c
.audit_fd
= audit_open();
240 if (c
.audit_fd
< 0 && !IN_SET(errno
, EAFNOSUPPORT
, EPROTONOSUPPORT
))
241 log_error_errno(errno
, "Failed to connect to audit log: %m");
243 r
= bus_connect_system_systemd(&c
.bus
);
245 log_error_errno(r
, "Failed to get D-Bus connection: %m");
250 log_debug("systemd-update-utmp running as pid "PID_FMT
, getpid_cached());
252 if (streq(argv
[1], "reboot"))
254 else if (streq(argv
[1], "shutdown"))
256 else if (streq(argv
[1], "runlevel"))
259 log_error("Unknown command %s", argv
[1]);
263 log_debug("systemd-update-utmp stopped as pid "PID_FMT
, getpid_cached());
268 audit_close(c
.audit_fd
);
271 sd_bus_flush_close_unref(c
.bus
);
272 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;