1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "alloc-util.h"
32 #include "bus-error.h"
34 #include "format-util.h"
37 #include "process-util.h"
40 #include "unit-name.h"
42 #include "utmp-wtmp.h"
44 typedef struct Context
{
51 static usec_t
get_startup_time(Context
*c
) {
52 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
58 r
= sd_bus_get_property_trivial(
60 "org.freedesktop.systemd1",
61 "/org/freedesktop/systemd1",
62 "org.freedesktop.systemd1.Manager",
67 log_error_errno(r
, "Failed to get timestamp: %s", bus_error_message(&error
, r
));
74 static int get_current_runlevel(Context
*c
) {
79 /* The first target of this list that is active or has
80 * a job scheduled wins. We prefer runlevels 5 and 3
81 * here over the others, since these are the main
82 * runlevels used on Fedora. It might make sense to
83 * change the order on some distributions. */
84 { '5', SPECIAL_GRAPHICAL_TARGET
},
85 { '3', SPECIAL_MULTI_USER_TARGET
},
86 { '1', SPECIAL_RESCUE_TARGET
},
89 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
95 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
96 _cleanup_free_
char *state
= NULL
, *path
= NULL
;
98 path
= unit_dbus_path_from_name(table
[i
].special
);
102 r
= sd_bus_get_property_string(
104 "org.freedesktop.systemd1",
106 "org.freedesktop.systemd1.Unit",
111 return log_warning_errno(r
, "Failed to get state: %s", bus_error_message(&error
, r
));
113 if (STR_IN_SET(state
, "active", "reloading"))
114 return table
[i
].runlevel
;
120 static int on_reboot(Context
*c
) {
126 /* We finished start-up, so let's write the utmp
127 * record and send the audit msg */
130 if (c
->audit_fd
>= 0)
131 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_BOOT
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
133 r
= log_error_errno(errno
, "Failed to send audit message: %m");
137 /* If this call fails it will return 0, which
138 * utmp_put_reboot() will then fix to the current time */
139 t
= get_startup_time(c
);
141 q
= utmp_put_reboot(t
);
143 log_error_errno(q
, "Failed to write utmp record: %m");
150 static int on_shutdown(Context
*c
) {
155 /* We started shut-down, so let's write the utmp
156 * record and send the audit msg */
159 if (c
->audit_fd
>= 0)
160 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_SHUTDOWN
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
162 r
= log_error_errno(errno
, "Failed to send audit message: %m");
166 q
= utmp_put_shutdown();
168 log_error_errno(q
, "Failed to write utmp record: %m");
175 static int on_runlevel(Context
*c
) {
176 int r
= 0, q
, previous
, runlevel
;
180 /* We finished changing runlevel, so let's write the
181 * utmp record and send the audit msg */
183 /* First, get last runlevel */
184 q
= utmp_get_runlevel(&previous
, NULL
);
187 if (!IN_SET(q
, -ESRCH
, -ENOENT
))
188 return log_error_errno(q
, "Failed to get current runlevel: %m");
193 /* Secondly, get new runlevel */
194 runlevel
= get_current_runlevel(c
);
199 if (previous
== runlevel
)
203 if (c
->audit_fd
>= 0) {
204 _cleanup_free_
char *s
= NULL
;
206 if (asprintf(&s
, "old-level=%c new-level=%c",
207 previous
> 0 ? previous
: 'N',
208 runlevel
> 0 ? runlevel
: 'N') < 0)
211 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_RUNLEVEL
, s
, "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 && errno
!= EPERM
)
212 r
= log_error_errno(errno
, "Failed to send audit message: %m");
216 q
= utmp_put_runlevel(runlevel
, previous
);
217 if (q
< 0 && !IN_SET(q
, -ESRCH
, -ENOENT
)) {
218 log_error_errno(q
, "Failed to write utmp record: %m");
225 int main(int argc
, char *argv
[]) {
233 if (getppid() != 1) {
234 log_error("This program should be invoked by init only.");
239 log_error("This program requires one argument.");
243 log_set_target(LOG_TARGET_AUTO
);
244 log_parse_environment();
250 /* If the kernel lacks netlink or audit support,
251 * don't worry about it. */
252 c
.audit_fd
= audit_open();
253 if (c
.audit_fd
< 0 && !IN_SET(errno
, EAFNOSUPPORT
, EPROTONOSUPPORT
))
254 log_error_errno(errno
, "Failed to connect to audit log: %m");
256 r
= bus_connect_system_systemd(&c
.bus
);
258 log_error_errno(r
, "Failed to get D-Bus connection: %m");
263 log_debug("systemd-update-utmp running as pid "PID_FMT
, getpid_cached());
265 if (streq(argv
[1], "reboot"))
267 else if (streq(argv
[1], "shutdown"))
269 else if (streq(argv
[1], "runlevel"))
272 log_error("Unknown command %s", argv
[1]);
276 log_debug("systemd-update-utmp stopped as pid "PID_FMT
, getpid_cached());
281 audit_close(c
.audit_fd
);
284 sd_bus_flush_close_unref(c
.bus
);
285 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;