1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
38 #include "utmp-wtmp.h"
40 #include "bus-error.h"
41 #include "unit-name.h"
43 typedef struct Context {
50 static usec_t get_startup_time(Context *c) {
51 _cleanup_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("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_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 log_warning("Failed to get state: %s", bus_error_message(&error, -r));
114 if (streq(state, "active") || streq(state, "reloading"))
115 return table[i].runlevel;
121 static int on_reboot(Context *c) {
127 /* We finished start-up, so let's write the utmp
128 * record and send the audit msg */
131 if (c->audit_fd >= 0)
132 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
134 log_error_errno(errno, "Failed to send audit message: %m");
139 /* If this call fails it will return 0, which
140 * utmp_put_reboot() will then fix to the current time */
141 t = get_startup_time(c);
143 q = utmp_put_reboot(t);
145 log_error_errno(q, "Failed to write utmp record: %m");
152 static int on_shutdown(Context *c) {
157 /* We started shut-down, so let's write the utmp
158 * record and send the audit msg */
161 if (c->audit_fd >= 0)
162 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
164 log_error_errno(errno, "Failed to send audit message: %m");
169 q = utmp_put_shutdown();
171 log_error_errno(q, "Failed to write utmp record: %m");
178 static int on_runlevel(Context *c) {
179 int r = 0, q, previous, runlevel;
183 /* We finished changing runlevel, so let's write the
184 * utmp record and send the audit msg */
186 /* First, get last runlevel */
187 q = utmp_get_runlevel(&previous, NULL);
190 if (q != -ESRCH && q != -ENOENT)
191 return log_error_errno(q, "Failed to get current runlevel: %m");
196 /* Secondly, get new runlevel */
197 runlevel = get_current_runlevel(c);
202 if (previous == runlevel)
206 if (c->audit_fd >= 0) {
207 _cleanup_free_ char *s = NULL;
209 if (asprintf(&s, "old-level=%c new-level=%c",
210 previous > 0 ? previous : 'N',
211 runlevel > 0 ? runlevel : 'N') < 0)
214 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
216 log_error_errno(errno, "Failed to send audit message: %m");
222 q = utmp_put_runlevel(runlevel, previous);
223 if (q < 0 && q != -ESRCH && q != -ENOENT) {
224 log_error_errno(q, "Failed to write utmp record: %m");
231 int main(int argc, char *argv[]) {
239 if (getppid() != 1) {
240 log_error("This program should be invoked by init only.");
245 log_error("This program requires one argument.");
249 log_set_target(LOG_TARGET_AUTO);
250 log_parse_environment();
256 /* If the kernel lacks netlink or audit support,
257 * don't worry about it. */
258 c.audit_fd = audit_open();
259 if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
260 log_error_errno(errno, "Failed to connect to audit log: %m");
262 r = bus_open_system_systemd(&c.bus);
264 log_error_errno(r, "Failed to get D-Bus connection: %m");
269 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
271 if (streq(argv[1], "reboot"))
273 else if (streq(argv[1], "shutdown"))
275 else if (streq(argv[1], "runlevel"))
278 log_error("Unknown command %s", argv[1]);
282 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
287 audit_close(c.audit_fd);
293 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;