2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "alloc-util.h"
31 #include "bus-error.h"
33 #include "format-util.h"
36 #include "process-util.h"
39 #include "unit-name.h"
41 #include "utmp-wtmp.h"
43 typedef struct Context
{
50 static usec_t
get_startup_time(Context
*c
) {
51 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
57 r
= sd_bus_get_property_trivial(
59 "org.freedesktop.systemd1",
60 "/org/freedesktop/systemd1",
61 "org.freedesktop.systemd1.Manager",
66 log_error_errno(r
, "Failed to get timestamp: %s", bus_error_message(&error
, r
));
73 static int get_current_runlevel(Context
*c
) {
78 /* The first target of this list that is active or has
79 * a job scheduled wins. We prefer runlevels 5 and 3
80 * here over the others, since these are the main
81 * runlevels used on Fedora. It might make sense to
82 * change the order on some distributions. */
83 { '5', SPECIAL_GRAPHICAL_TARGET
},
84 { '3', SPECIAL_MULTI_USER_TARGET
},
85 { '1', SPECIAL_RESCUE_TARGET
},
88 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
94 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
95 _cleanup_free_
char *state
= NULL
, *path
= NULL
;
97 path
= unit_dbus_path_from_name(table
[i
].special
);
101 r
= sd_bus_get_property_string(
103 "org.freedesktop.systemd1",
105 "org.freedesktop.systemd1.Unit",
110 return log_warning_errno(r
, "Failed to get state: %s", bus_error_message(&error
, r
));
112 if (STR_IN_SET(state
, "active", "reloading"))
113 return table
[i
].runlevel
;
119 static int on_reboot(Context
*c
) {
125 /* We finished start-up, so let's write the utmp
126 * record and send the audit msg */
129 if (c
->audit_fd
>= 0)
130 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_BOOT
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
132 r
= log_error_errno(errno
, "Failed to send audit message: %m");
136 /* If this call fails it will return 0, which
137 * utmp_put_reboot() will then fix to the current time */
138 t
= get_startup_time(c
);
140 q
= utmp_put_reboot(t
);
142 log_error_errno(q
, "Failed to write utmp record: %m");
149 static int on_shutdown(Context
*c
) {
154 /* We started shut-down, so let's write the utmp
155 * record and send the audit msg */
158 if (c
->audit_fd
>= 0)
159 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_SHUTDOWN
, "", "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 &&
161 r
= log_error_errno(errno
, "Failed to send audit message: %m");
165 q
= utmp_put_shutdown();
167 log_error_errno(q
, "Failed to write utmp record: %m");
174 static int on_runlevel(Context
*c
) {
175 int r
= 0, q
, previous
, runlevel
;
179 /* We finished changing runlevel, so let's write the
180 * utmp record and send the audit msg */
182 /* First, get last runlevel */
183 q
= utmp_get_runlevel(&previous
, NULL
);
186 if (!IN_SET(q
, -ESRCH
, -ENOENT
))
187 return log_error_errno(q
, "Failed to get current runlevel: %m");
192 /* Secondly, get new runlevel */
193 runlevel
= get_current_runlevel(c
);
198 if (previous
== runlevel
)
202 if (c
->audit_fd
>= 0) {
203 _cleanup_free_
char *s
= NULL
;
205 if (asprintf(&s
, "old-level=%c new-level=%c",
206 previous
> 0 ? previous
: 'N',
207 runlevel
> 0 ? runlevel
: 'N') < 0)
210 if (audit_log_user_comm_message(c
->audit_fd
, AUDIT_SYSTEM_RUNLEVEL
, s
, "systemd-update-utmp", NULL
, NULL
, NULL
, 1) < 0 && errno
!= EPERM
)
211 r
= log_error_errno(errno
, "Failed to send audit message: %m");
215 q
= utmp_put_runlevel(runlevel
, previous
);
216 if (q
< 0 && !IN_SET(q
, -ESRCH
, -ENOENT
)) {
217 log_error_errno(q
, "Failed to write utmp record: %m");
224 int main(int argc
, char *argv
[]) {
232 if (getppid() != 1) {
233 log_error("This program should be invoked by init only.");
238 log_error("This program requires one argument.");
242 log_set_target(LOG_TARGET_AUTO
);
243 log_parse_environment();
249 /* If the kernel lacks netlink or audit support,
250 * don't worry about it. */
251 c
.audit_fd
= audit_open();
252 if (c
.audit_fd
< 0 && !IN_SET(errno
, EAFNOSUPPORT
, EPROTONOSUPPORT
))
253 log_error_errno(errno
, "Failed to connect to audit log: %m");
255 r
= bus_connect_system_systemd(&c
.bus
);
257 log_error_errno(r
, "Failed to get D-Bus connection: %m");
262 log_debug("systemd-update-utmp running as pid "PID_FMT
, getpid_cached());
264 if (streq(argv
[1], "reboot"))
266 else if (streq(argv
[1], "shutdown"))
268 else if (streq(argv
[1], "runlevel"))
271 log_error("Unknown command %s", argv
[1]);
275 log_debug("systemd-update-utmp stopped as pid "PID_FMT
, getpid_cached());
280 audit_close(c
.audit_fd
);
283 sd_bus_flush_close_unref(c
.bus
);
284 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;