1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
29 #include <dbus/dbus.h>
35 #include "utmp-wtmp.h"
36 #include "dbus-common.h"
38 typedef struct Context {
45 static usec_t get_startup_time(Context *c) {
47 *interface = "org.freedesktop.systemd1.Manager",
48 *property = "StartupTimestamp";
52 DBusMessage *m = NULL, *reply = NULL;
53 DBusMessageIter iter, sub;
55 dbus_error_init(&error);
59 if (!(m = dbus_message_new_method_call(
60 "org.freedesktop.systemd1",
61 "/org/freedesktop/systemd1",
62 "org.freedesktop.DBus.Properties",
64 log_error("Could not allocate message.");
68 if (!dbus_message_append_args(m,
69 DBUS_TYPE_STRING, &interface,
70 DBUS_TYPE_STRING, &property,
72 log_error("Could not append arguments to message.");
76 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
77 log_error("Failed to send command: %s", error.message);
81 if (!dbus_message_iter_init(reply, &iter) ||
82 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
83 log_error("Failed to parse reply.");
87 dbus_message_iter_recurse(&iter, &sub);
89 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT64) {
90 log_error("Failed to parse reply.");
94 dbus_message_iter_get_basic(&sub, &t);
98 dbus_message_unref(m);
101 dbus_message_unref(reply);
103 dbus_error_free(&error);
108 static int get_current_runlevel(Context *c) {
109 static const struct {
113 /* The first target of this list that is active or has
114 * a job scheduled wins */
115 { '5', SPECIAL_RUNLEVEL5_TARGET },
116 { '4', SPECIAL_RUNLEVEL4_TARGET },
117 { '3', SPECIAL_RUNLEVEL3_TARGET },
118 { '2', SPECIAL_RUNLEVEL2_TARGET },
119 { '1', SPECIAL_RESCUE_TARGET },
122 *interface = "org.freedesktop.systemd1.Unit",
123 *property = "ActiveState";
125 DBusMessage *m = NULL, *reply = NULL;
132 dbus_error_init(&error);
134 for (i = 0; i < ELEMENTSOF(table); i++) {
135 const char *path = NULL, *state;
136 DBusMessageIter iter, sub;
138 if (!(m = dbus_message_new_method_call(
139 "org.freedesktop.systemd1",
140 "/org/freedesktop/systemd1",
141 "org.freedesktop.systemd1.Manager",
143 log_error("Could not allocate message.");
148 if (!dbus_message_append_args(m,
149 DBUS_TYPE_STRING, &table[i].special,
150 DBUS_TYPE_INVALID)) {
151 log_error("Could not append arguments to message.");
156 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
157 dbus_error_free(&error);
161 if (!dbus_message_get_args(reply, &error,
162 DBUS_TYPE_OBJECT_PATH, &path,
163 DBUS_TYPE_INVALID)) {
164 log_error("Failed to parse reply: %s", error.message);
169 dbus_message_unref(m);
170 if (!(m = dbus_message_new_method_call(
171 "org.freedesktop.systemd1",
173 "org.freedesktop.DBus.Properties",
175 log_error("Could not allocate message.");
180 if (!dbus_message_append_args(m,
181 DBUS_TYPE_STRING, &interface,
182 DBUS_TYPE_STRING, &property,
183 DBUS_TYPE_INVALID)) {
184 log_error("Could not append arguments to message.");
189 dbus_message_unref(reply);
190 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
191 log_error("Failed to send command: %s", error.message);
196 if (!dbus_message_iter_init(reply, &iter) ||
197 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
198 log_error("Failed to parse reply.");
203 dbus_message_iter_recurse(&iter, &sub);
205 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
206 log_error("Failed to parse reply.");
211 dbus_message_iter_get_basic(&sub, &state);
213 if (streq(state, "active") || streq(state, "reloading"))
214 r = table[i].runlevel;
216 dbus_message_unref(m);
217 dbus_message_unref(reply);
226 dbus_message_unref(m);
229 dbus_message_unref(reply);
231 dbus_error_free(&error);
236 static int on_reboot(Context *c) {
242 /* We finished start-up, so let's write the utmp
243 * record and send the audit msg */
246 if (c->audit_fd >= 0)
247 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", NULL, NULL, NULL, 1) < 0) {
248 log_error("Failed to send audit message: %m");
253 /* If this call fails it will return 0, which
254 * utmp_put_reboot() will then fix to the current time */
255 t = get_startup_time(c);
257 if ((q = utmp_put_reboot(t)) < 0) {
258 log_error("Failed to write utmp record: %s", strerror(-q));
265 static int on_shutdown(Context *c) {
270 /* We started shut-down, so let's write the utmp
271 * record and send the audit msg */
274 if (c->audit_fd >= 0)
275 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", NULL, NULL, NULL, 1) < 0) {
276 log_error("Failed to send audit message: %m");
281 if ((q = utmp_put_shutdown(0)) < 0) {
282 log_error("Failed to write utmp record: %s", strerror(-q));
289 static int on_runlevel(Context *c) {
290 int r = 0, q, previous, runlevel;
294 /* We finished changing runlevel, so let's write the
295 * utmp record and send the audit msg */
297 /* First, get last runlevel */
298 if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
300 if (q != -ESRCH && q != -ENOENT) {
301 log_error("Failed to get current runlevel: %s", strerror(-q));
305 /* Hmm, we didn't find any runlevel, that means we
306 * have been rebooted */
311 /* Second get new runlevel */
312 if ((runlevel = get_current_runlevel(c)) < 0)
315 if (previous == runlevel)
319 if (c->audit_fd >= 0) {
322 if (asprintf(&s, "old-level=%c new-level=%c",
323 previous > 0 ? previous : 'N',
324 runlevel > 0 ? runlevel : 'N') < 0)
327 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) {
328 log_error("Failed to send audit message: %m");
336 if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) {
337 log_error("Failed to write utmp record: %s", strerror(-q));
344 int main(int argc, char *argv[]) {
349 dbus_error_init(&error);
356 if (getppid() != 1) {
357 log_error("This program should be invoked by init only.");
362 log_error("This program requires one argument.");
366 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
367 log_parse_environment();
370 if ((c.audit_fd = audit_open()) < 0)
371 log_error("Failed to connect to audit log: %m");
374 if (bus_connect(DBUS_BUS_SYSTEM, &c.bus, NULL, &error) < 0) {
375 log_error("Failed to get D-Bus connection: %s", error.message);
380 log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
382 if (streq(argv[1], "reboot"))
384 else if (streq(argv[1], "shutdown"))
386 else if (streq(argv[1], "runlevel"))
389 log_error("Unknown command %s", argv[1]);
393 log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
398 audit_close(c.audit_fd);
402 dbus_connection_close(c.bus);
403 dbus_connection_unref(c.bus);
406 dbus_error_free(&error);
409 return r < 0 ? 1 : 0;