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 q = utmp_put_reboot(t);
162 log_error("Failed to write utmp record: %s", strerror(-q));
169 static int on_shutdown(Context *c) {
174 /* We started shut-down, so let's write the utmp
175 * record and send the audit msg */
178 if (c->audit_fd >= 0)
179 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0 &&
181 log_error("Failed to send audit message: %m");
186 q = utmp_put_shutdown();
188 log_error("Failed to write utmp record: %s", strerror(-q));
195 static int on_runlevel(Context *c) {
196 int r = 0, q, previous, runlevel;
200 /* We finished changing runlevel, so let's write the
201 * utmp record and send the audit msg */
203 /* First, get last runlevel */
204 q = utmp_get_runlevel(&previous, NULL);
207 if (q != -ESRCH && q != -ENOENT) {
208 log_error("Failed to get current runlevel: %s", strerror(-q));
212 /* Hmm, we didn't find any runlevel, that means we
213 * have been rebooted */
218 /* Secondly, get new runlevel */
219 runlevel = get_current_runlevel(c);
223 if (previous == runlevel)
227 if (c->audit_fd >= 0) {
228 _cleanup_free_ char *s = NULL;
230 if (asprintf(&s, "old-level=%c new-level=%c",
231 previous > 0 ? previous : 'N',
232 runlevel > 0 ? runlevel : 'N') < 0)
235 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0 &&
237 log_error("Failed to send audit message: %m");
243 q = utmp_put_runlevel(runlevel, previous);
244 if (q < 0 && q != -ESRCH && q != -ENOENT) {
245 log_error("Failed to write utmp record: %s", strerror(-q));
252 int main(int argc, char *argv[]) {
260 if (getppid() != 1) {
261 log_error("This program should be invoked by init only.");
266 log_error("This program requires one argument.");
270 log_set_target(LOG_TARGET_AUTO);
271 log_parse_environment();
277 /* If the kernel lacks netlink or audit support,
278 * don't worry about it. */
279 c.audit_fd = audit_open();
280 if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
281 log_error("Failed to connect to audit log: %m");
283 r = bus_open_system_systemd(&c.bus);
285 log_error("Failed to get D-Bus connection: %s", strerror(-r));
290 log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
292 if (streq(argv[1], "reboot"))
294 else if (streq(argv[1], "shutdown"))
296 else if (streq(argv[1], "runlevel"))
299 log_error("Unknown command %s", argv[1]);
303 log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
308 audit_close(c.audit_fd);
314 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;