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) {
51 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
52 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
57 r = sd_bus_call_method(
59 "org.freedesktop.systemd1",
60 "/org/freedesktop/systemd1",
61 "org.freedesktop.DBus.Properties",
66 "org.freedesktop.systemd1.Manager",
67 "UserspaceTimestamp");
69 log_error("Failed to get timestamp: %s", bus_error_message(&error, -r));
73 r = sd_bus_message_read(reply, "v", "t", &t);
75 return bus_log_parse_error(r);
80 static int get_current_runlevel(Context *c) {
85 /* The first target of this list that is active or has
86 * a job scheduled wins. We prefer runlevels 5 and 3
87 * here over the others, since these are the main
88 * runlevels used on Fedora. It might make sense to
89 * change the order on some distributions. */
90 { '5', SPECIAL_RUNLEVEL5_TARGET },
91 { '3', SPECIAL_RUNLEVEL3_TARGET },
92 { '4', SPECIAL_RUNLEVEL4_TARGET },
93 { '2', SPECIAL_RUNLEVEL2_TARGET },
94 { '1', SPECIAL_RESCUE_TARGET },
97 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
103 for (i = 0; i < ELEMENTSOF(table); i++) {
104 _cleanup_bus_message_unref_ sd_bus_message *reply1 = NULL, *reply2 = NULL;
105 const char *path = NULL, *state;
107 r = sd_bus_call_method(
109 "org.freedesktop.systemd1",
110 "/org/freedesktop/systemd1",
111 "org.freedesktop.systemd1.Manager",
115 "s", table[i].special);
117 log_error("Failed to get runlevel: %s", bus_error_message(&error, -r));
124 r = sd_bus_message_read(reply1, "o", &path);
126 return bus_log_parse_error(r);
128 r = sd_bus_call_method(
130 "org.freedesktop.systemd1",
132 "org.freedesktop.DBus.Properties",
136 "ss", "org.freedesktop.systemd1.Unit", "ActiveState");
138 log_error("Failed to get state: %s", bus_error_message(&error, -r));
142 r = sd_bus_message_read(reply2, "v", "s", &state);
144 return bus_log_parse_error(r);
146 if (streq(state, "active") || streq(state, "reloading"))
147 return table[i].runlevel;
153 static int on_reboot(Context *c) {
159 /* We finished start-up, so let's write the utmp
160 * record and send the audit msg */
163 if (c->audit_fd >= 0)
164 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0 &&
166 log_error("Failed to send audit message: %m");
171 /* If this call fails it will return 0, which
172 * utmp_put_reboot() will then fix to the current time */
173 t = get_startup_time(c);
175 if ((q = utmp_put_reboot(t)) < 0) {
176 log_error("Failed to write utmp record: %s", strerror(-q));
183 static int on_shutdown(Context *c) {
188 /* We started shut-down, so let's write the utmp
189 * record and send the audit msg */
192 if (c->audit_fd >= 0)
193 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0 &&
195 log_error("Failed to send audit message: %m");
200 if ((q = utmp_put_shutdown()) < 0) {
201 log_error("Failed to write utmp record: %s", strerror(-q));
208 static int on_runlevel(Context *c) {
209 int r = 0, q, previous, runlevel;
213 /* We finished changing runlevel, so let's write the
214 * utmp record and send the audit msg */
216 /* First, get last runlevel */
217 if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
219 if (q != -ESRCH && q != -ENOENT) {
220 log_error("Failed to get current runlevel: %s", strerror(-q));
224 /* Hmm, we didn't find any runlevel, that means we
225 * have been rebooted */
230 /* Secondly, get new runlevel */
231 if ((runlevel = get_current_runlevel(c)) < 0)
234 if (previous == runlevel)
238 if (c->audit_fd >= 0) {
241 if (asprintf(&s, "old-level=%c new-level=%c",
242 previous > 0 ? previous : 'N',
243 runlevel > 0 ? runlevel : 'N') < 0)
246 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0 &&
248 log_error("Failed to send audit message: %m");
256 if ((q = utmp_put_runlevel(runlevel, previous)) < 0) {
257 if (q != -ESRCH && q != -ENOENT) {
258 log_error("Failed to write utmp record: %s", strerror(-q));
266 int main(int argc, char *argv[]) {
274 if (getppid() != 1) {
275 log_error("This program should be invoked by init only.");
280 log_error("This program requires one argument.");
284 log_set_target(LOG_TARGET_AUTO);
285 log_parse_environment();
291 if ((c.audit_fd = audit_open()) < 0 &&
292 /* If the kernel lacks netlink or audit support,
293 * don't worry about it. */
294 errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
295 log_error("Failed to connect to audit log: %m");
297 r = bus_open_system_systemd(&c.bus);
299 log_error("Failed to get D-Bus connection: %s", strerror(-r));
304 log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
306 if (streq(argv[1], "reboot"))
308 else if (streq(argv[1], "shutdown"))
310 else if (streq(argv[1], "runlevel"))
313 log_error("Unknown command %s", argv[1]);
317 log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
322 audit_close(c.audit_fd);
328 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;