1 /* SPDX-License-Identifier: LGPL-2.1+ */
14 #include "alloc-util.h"
15 #include "bus-error.h"
17 #include "format-util.h"
20 #include "main-func.h"
21 #include "process-util.h"
23 #include "stdio-util.h"
25 #include "unit-name.h"
27 #include "utmp-wtmp.h"
29 typedef struct Context
{
36 static void context_clear(Context
*c
) {
39 c
->bus
= sd_bus_flush_close_unref(c
->bus
);
42 audit_close(c
->audit_fd
);
47 static usec_t
get_startup_time(Context
*c
) {
48 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
54 r
= sd_bus_get_property_trivial(
56 "org.freedesktop.systemd1",
57 "/org/freedesktop/systemd1",
58 "org.freedesktop.systemd1.Manager",
63 log_error_errno(r
, "Failed to get timestamp: %s", bus_error_message(&error
, r
));
70 static int get_current_runlevel(Context
*c
) {
75 /* The first target of this list that is active or has
76 * a job scheduled wins. We prefer runlevels 5 and 3
77 * here over the others, since these are the main
78 * runlevels used on Fedora. It might make sense to
79 * change the order on some distributions. */
80 { '5', SPECIAL_GRAPHICAL_TARGET
},
81 { '3', SPECIAL_MULTI_USER_TARGET
},
82 { '1', SPECIAL_RESCUE_TARGET
},
85 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
91 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
92 _cleanup_free_
char *state
= NULL
, *path
= NULL
;
94 path
= unit_dbus_path_from_name(table
[i
].special
);
98 r
= sd_bus_get_property_string(
100 "org.freedesktop.systemd1",
102 "org.freedesktop.systemd1.Unit",
107 return log_warning_errno(r
, "Failed to get state: %s", bus_error_message(&error
, r
));
109 if (STR_IN_SET(state
, "active", "reloading"))
110 return table
[i
].runlevel
;
116 static int on_reboot(Context
*c
) {
122 /* We finished start-up, so let's write the utmp
123 * record and send the audit msg */
126 if (c
->audit_fd
>= 0)
127 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_BOOT
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
129 r
= log_error_errno(errno
, "Failed to send audit message: %m");
133 /* If this call fails it will return 0, which
134 * utmp_put_reboot() will then fix to the current time */
135 t
= get_startup_time(c
);
137 q
= utmp_put_reboot(t
);
139 log_error_errno(q
, "Failed to write utmp record: %m");
146 static int on_shutdown(Context
*c
) {
151 /* We started shut-down, so let's write the utmp
152 * record and send the audit msg */
155 if (c
->audit_fd
>= 0)
156 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_SHUTDOWN
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
158 r
= log_error_errno(errno
, "Failed to send audit message: %m");
162 q
= utmp_put_shutdown();
164 log_error_errno(q
, "Failed to write utmp record: %m");
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
);
194 return log_warning("Failed to get new runlevel, utmp update skipped.");
196 if (previous
== runlevel
)
200 if (c
->audit_fd
>= 0) {
201 char s
[STRLEN("old-level=_ new-level=_") + 1];
203 xsprintf(s
, "old-level=%c new-level=%c",
204 previous
> 0 ? previous
: 'N',
207 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_RUNLEVEL
, s
,
208 "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 && errno
!= EPERM
)
209 r
= log_error_errno(errno
, "Failed to send audit message: %m");
213 q
= utmp_put_runlevel(runlevel
, previous
);
214 if (q
< 0 && !IN_SET(q
, -ESRCH
, -ENOENT
))
215 return log_error_errno(q
, "Failed to write utmp record: %m");
220 static int run(int argc
, char *argv
[]) {
221 _cleanup_(context_clear
) Context c
= {
229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
230 "This program should be invoked by init only.");
232 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
233 "This program requires one argument.");
240 /* If the kernel lacks netlink or audit support, don't worry about it. */
241 c
.audit_fd
= audit_open();
243 log_full_errno(IN_SET(errno
, EAFNOSUPPORT
, EPROTONOSUPPORT
) ? LOG_DEBUG
: LOG_ERR
,
244 errno
, "Failed to connect to audit log: %m");
246 r
= bus_connect_system_systemd(&c
.bus
);
248 return log_error_errno(r
, "Failed to get D-Bus connection: %m");
250 if (streq(argv
[1], "reboot"))
251 return on_reboot(&c
);
252 if (streq(argv
[1], "shutdown"))
253 return on_shutdown(&c
);
254 if (streq(argv
[1], "runlevel"))
255 return on_runlevel(&c
);
256 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown command %s", argv
[1]);
259 DEFINE_MAIN_FUNCTION(run
);