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"
42 typedef struct Context {
49 static usec_t get_startup_time(Context *c) {
50 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
56 r = sd_bus_get_property_trivial(
58 "org.freedesktop.systemd1",
59 "/org/freedesktop/systemd1",
60 "org.freedesktop.systemd1.Manager",
65 log_error("Failed to get timestamp: %s", bus_error_message(&error, -r));
72 static int get_current_runlevel(Context *c) {
77 /* The first target of this list that is active or has
78 * a job scheduled wins. We prefer runlevels 5 and 3
79 * here over the others, since these are the main
80 * runlevels used on Fedora. It might make sense to
81 * change the order on some distributions. */
82 { '5', SPECIAL_RUNLEVEL5_TARGET },
83 { '3', SPECIAL_RUNLEVEL3_TARGET },
84 { '4', SPECIAL_RUNLEVEL4_TARGET },
85 { '2', SPECIAL_RUNLEVEL2_TARGET },
86 { '1', SPECIAL_RESCUE_TARGET },
89 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
95 for (i = 0; i < ELEMENTSOF(table); i++) {
96 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
97 _cleanup_free_ char *state = NULL;
98 const char *path = NULL;
100 r = sd_bus_call_method(
102 "org.freedesktop.systemd1",
103 "/org/freedesktop/systemd1",
104 "org.freedesktop.systemd1.Manager",
108 "s", table[i].special);
110 log_warning("Failed to get runlevel: %s", bus_error_message(&error, -r));
114 r = sd_bus_message_read(reply, "o", &path);
116 return bus_log_parse_error(r);
118 r = sd_bus_get_property_string(
120 "org.freedesktop.systemd1",
122 "org.freedesktop.systemd1.Unit",
127 log_warning("Failed to get state: %s", bus_error_message(&error, -r));
131 if (streq(state, "active") || streq(state, "reloading"))
132 return table[i].runlevel;
138 static int on_reboot(Context *c) {
144 /* We finished start-up, so let's write the utmp
145 * record and send the audit msg */
148 if (c->audit_fd >= 0)
149 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0 &&
151 log_error("Failed to send audit message: %m");
156 /* If this call fails it will return 0, which
157 * utmp_put_reboot() will then fix to the current time */
158 t = get_startup_time(c);
160 if ((q = utmp_put_reboot(t)) < 0) {
161 log_error("Failed to write utmp record: %s", strerror(-q));
168 static int on_shutdown(Context *c) {
173 /* We started shut-down, so let's write the utmp
174 * record and send the audit msg */
177 if (c->audit_fd >= 0)
178 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0 &&
180 log_error("Failed to send audit message: %m");
185 if ((q = utmp_put_shutdown()) < 0) {
186 log_error("Failed to write utmp record: %s", strerror(-q));
193 static int on_runlevel(Context *c) {
194 int r = 0, q, previous, runlevel;
198 /* We finished changing runlevel, so let's write the
199 * utmp record and send the audit msg */
201 /* First, get last runlevel */
202 if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
204 if (q != -ESRCH && q != -ENOENT) {
205 log_error("Failed to get current runlevel: %s", strerror(-q));
209 /* Hmm, we didn't find any runlevel, that means we
210 * have been rebooted */
215 /* Secondly, get new runlevel */
216 if ((runlevel = get_current_runlevel(c)) < 0)
219 if (previous == runlevel)
223 if (c->audit_fd >= 0) {
226 if (asprintf(&s, "old-level=%c new-level=%c",
227 previous > 0 ? previous : 'N',
228 runlevel > 0 ? runlevel : 'N') < 0)
231 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0 &&
233 log_error("Failed to send audit message: %m");
241 if ((q = utmp_put_runlevel(runlevel, previous)) < 0) {
242 if (q != -ESRCH && q != -ENOENT) {
243 log_error("Failed to write utmp record: %s", strerror(-q));
251 int main(int argc, char *argv[]) {
259 if (getppid() != 1) {
260 log_error("This program should be invoked by init only.");
265 log_error("This program requires one argument.");
269 log_set_target(LOG_TARGET_AUTO);
270 log_parse_environment();
276 if ((c.audit_fd = audit_open()) < 0 &&
277 /* If the kernel lacks netlink or audit support,
278 * don't worry about it. */
279 errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
280 log_error("Failed to connect to audit log: %m");
282 r = bus_open_system_systemd(&c.bus);
284 log_error("Failed to get D-Bus connection: %s", strerror(-r));
289 log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
291 if (streq(argv[1], "reboot"))
293 else if (streq(argv[1], "shutdown"))
295 else if (streq(argv[1], "runlevel"))
298 log_error("Unknown command %s", argv[1]);
302 log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
307 audit_close(c.audit_fd);
313 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;