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>
39 #include "utmp-wtmp.h"
40 #include "dbus-common.h"
42 typedef struct Context {
49 static usec_t get_startup_time(Context *c) {
51 *interface = "org.freedesktop.systemd1.Manager",
52 *property = "StartupTimestamp";
56 DBusMessage *m = NULL, *reply = NULL;
57 DBusMessageIter iter, sub;
59 dbus_error_init(&error);
63 if (!(m = dbus_message_new_method_call(
64 "org.freedesktop.systemd1",
65 "/org/freedesktop/systemd1",
66 "org.freedesktop.DBus.Properties",
68 log_error("Could not allocate message.");
72 if (!dbus_message_append_args(m,
73 DBUS_TYPE_STRING, &interface,
74 DBUS_TYPE_STRING, &property,
76 log_error("Could not append arguments to message.");
80 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
81 log_error("Failed to send command: %s", error.message);
85 if (!dbus_message_iter_init(reply, &iter) ||
86 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
87 log_error("Failed to parse reply.");
91 dbus_message_iter_recurse(&iter, &sub);
93 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT64) {
94 log_error("Failed to parse reply.");
98 dbus_message_iter_get_basic(&sub, &t);
102 dbus_message_unref(m);
105 dbus_message_unref(reply);
107 dbus_error_free(&error);
112 static int get_current_runlevel(Context *c) {
113 static const struct {
117 /* The first target of this list that is active or has
118 * a job scheduled wins */
119 { '5', SPECIAL_RUNLEVEL5_TARGET },
120 { '4', SPECIAL_RUNLEVEL4_TARGET },
121 { '3', SPECIAL_RUNLEVEL3_TARGET },
122 { '2', SPECIAL_RUNLEVEL2_TARGET },
123 { '1', SPECIAL_RESCUE_TARGET },
126 *interface = "org.freedesktop.systemd1.Unit",
127 *property = "ActiveState";
129 DBusMessage *m = NULL, *reply = NULL;
136 dbus_error_init(&error);
138 for (i = 0; i < ELEMENTSOF(table); i++) {
139 const char *path = NULL, *state;
140 DBusMessageIter iter, sub;
142 if (!(m = dbus_message_new_method_call(
143 "org.freedesktop.systemd1",
144 "/org/freedesktop/systemd1",
145 "org.freedesktop.systemd1.Manager",
147 log_error("Could not allocate message.");
152 if (!dbus_message_append_args(m,
153 DBUS_TYPE_STRING, &table[i].special,
154 DBUS_TYPE_INVALID)) {
155 log_error("Could not append arguments to message.");
160 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
161 dbus_error_free(&error);
165 if (!dbus_message_get_args(reply, &error,
166 DBUS_TYPE_OBJECT_PATH, &path,
167 DBUS_TYPE_INVALID)) {
168 log_error("Failed to parse reply: %s", error.message);
173 dbus_message_unref(m);
174 if (!(m = dbus_message_new_method_call(
175 "org.freedesktop.systemd1",
177 "org.freedesktop.DBus.Properties",
179 log_error("Could not allocate message.");
184 if (!dbus_message_append_args(m,
185 DBUS_TYPE_STRING, &interface,
186 DBUS_TYPE_STRING, &property,
187 DBUS_TYPE_INVALID)) {
188 log_error("Could not append arguments to message.");
193 dbus_message_unref(reply);
194 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
195 log_error("Failed to send command: %s", error.message);
200 if (!dbus_message_iter_init(reply, &iter) ||
201 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
202 log_error("Failed to parse reply.");
207 dbus_message_iter_recurse(&iter, &sub);
209 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
210 log_error("Failed to parse reply.");
215 dbus_message_iter_get_basic(&sub, &state);
217 if (streq(state, "active") || streq(state, "reloading"))
218 r = table[i].runlevel;
220 dbus_message_unref(m);
221 dbus_message_unref(reply);
230 dbus_message_unref(m);
233 dbus_message_unref(reply);
235 dbus_error_free(&error);
240 static int on_reboot(Context *c) {
246 /* We finished start-up, so let's write the utmp
247 * record and send the audit msg */
250 if (c->audit_fd >= 0)
251 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0) {
252 log_error("Failed to send audit message: %m");
257 /* If this call fails it will return 0, which
258 * utmp_put_reboot() will then fix to the current time */
259 t = get_startup_time(c);
261 if ((q = utmp_put_reboot(t)) < 0) {
262 log_error("Failed to write utmp record: %s", strerror(-q));
269 static int on_shutdown(Context *c) {
274 /* We started shut-down, so let's write the utmp
275 * record and send the audit msg */
278 if (c->audit_fd >= 0)
279 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0) {
280 log_error("Failed to send audit message: %m");
285 if ((q = utmp_put_shutdown(0)) < 0) {
286 log_error("Failed to write utmp record: %s", strerror(-q));
293 static int on_runlevel(Context *c) {
294 int r = 0, q, previous, runlevel;
298 /* We finished changing runlevel, so let's write the
299 * utmp record and send the audit msg */
301 /* First, get last runlevel */
302 if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
304 if (q != -ESRCH && q != -ENOENT) {
305 log_error("Failed to get current runlevel: %s", strerror(-q));
309 /* Hmm, we didn't find any runlevel, that means we
310 * have been rebooted */
315 /* Secondly, get new runlevel */
316 if ((runlevel = get_current_runlevel(c)) < 0)
319 if (previous == runlevel)
323 if (c->audit_fd >= 0) {
326 if (asprintf(&s, "old-level=%c new-level=%c",
327 previous > 0 ? previous : 'N',
328 runlevel > 0 ? runlevel : 'N') < 0)
331 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) {
332 log_error("Failed to send audit message: %m");
340 if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) {
341 log_error("Failed to write utmp record: %s", strerror(-q));
348 int main(int argc, char *argv[]) {
353 dbus_error_init(&error);
360 if (getppid() != 1) {
361 log_error("This program should be invoked by init only.");
366 log_error("This program requires one argument.");
370 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
371 log_parse_environment();
374 if ((c.audit_fd = audit_open()) < 0)
375 log_error("Failed to connect to audit log: %m");
378 if (bus_connect(DBUS_BUS_SYSTEM, &c.bus, NULL, &error) < 0) {
379 log_error("Failed to get D-Bus connection: %s", error.message);
384 log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
386 if (streq(argv[1], "reboot"))
388 else if (streq(argv[1], "shutdown"))
390 else if (streq(argv[1], "runlevel"))
393 log_error("Unknown command %s", argv[1]);
397 log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
402 audit_close(c.audit_fd);
406 dbus_connection_close(c.bus);
407 dbus_connection_unref(c.bus);
410 dbus_error_free(&error);
413 return r < 0 ? 1 : 0;