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 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/>.
25 #include <sys/types.h>
28 #include <dbus/dbus.h>
38 #include "utmp-wtmp.h"
39 #include "dbus-common.h"
41 typedef struct Context {
48 static usec_t get_startup_time(Context *c) {
50 *interface = "org.freedesktop.systemd1.Manager",
51 *property = "StartupTimestamp";
55 DBusMessage *m = NULL, *reply = NULL;
56 DBusMessageIter iter, sub;
58 dbus_error_init(&error);
62 if (!(m = dbus_message_new_method_call(
63 "org.freedesktop.systemd1",
64 "/org/freedesktop/systemd1",
65 "org.freedesktop.DBus.Properties",
67 log_error("Could not allocate message.");
71 if (!dbus_message_append_args(m,
72 DBUS_TYPE_STRING, &interface,
73 DBUS_TYPE_STRING, &property,
75 log_error("Could not append arguments to message.");
79 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
80 log_error("Failed to send command: %s", error.message);
84 if (!dbus_message_iter_init(reply, &iter) ||
85 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
86 log_error("Failed to parse reply.");
90 dbus_message_iter_recurse(&iter, &sub);
92 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT64) {
93 log_error("Failed to parse reply.");
97 dbus_message_iter_get_basic(&sub, &t);
101 dbus_message_unref(m);
104 dbus_message_unref(reply);
106 dbus_error_free(&error);
111 static int get_current_runlevel(Context *c) {
112 static const struct {
116 /* The first target of this list that is active or has
117 * a job scheduled wins */
118 { '5', SPECIAL_RUNLEVEL5_TARGET },
119 { '4', SPECIAL_RUNLEVEL4_TARGET },
120 { '3', SPECIAL_RUNLEVEL3_TARGET },
121 { '2', SPECIAL_RUNLEVEL2_TARGET },
122 { '1', SPECIAL_RESCUE_TARGET },
125 *interface = "org.freedesktop.systemd1.Unit",
126 *property = "ActiveState";
128 DBusMessage *m = NULL, *reply = NULL;
135 dbus_error_init(&error);
137 for (i = 0; i < ELEMENTSOF(table); i++) {
138 const char *path = NULL, *state;
139 DBusMessageIter iter, sub;
141 if (!(m = dbus_message_new_method_call(
142 "org.freedesktop.systemd1",
143 "/org/freedesktop/systemd1",
144 "org.freedesktop.systemd1.Manager",
146 log_error("Could not allocate message.");
151 if (!dbus_message_append_args(m,
152 DBUS_TYPE_STRING, &table[i].special,
153 DBUS_TYPE_INVALID)) {
154 log_error("Could not append arguments to message.");
159 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
160 dbus_error_free(&error);
164 if (!dbus_message_get_args(reply, &error,
165 DBUS_TYPE_OBJECT_PATH, &path,
166 DBUS_TYPE_INVALID)) {
167 log_error("Failed to parse reply: %s", error.message);
172 dbus_message_unref(m);
173 if (!(m = dbus_message_new_method_call(
174 "org.freedesktop.systemd1",
176 "org.freedesktop.DBus.Properties",
178 log_error("Could not allocate message.");
183 if (!dbus_message_append_args(m,
184 DBUS_TYPE_STRING, &interface,
185 DBUS_TYPE_STRING, &property,
186 DBUS_TYPE_INVALID)) {
187 log_error("Could not append arguments to message.");
192 dbus_message_unref(reply);
193 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
194 log_error("Failed to send command: %s", error.message);
199 if (!dbus_message_iter_init(reply, &iter) ||
200 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
201 log_error("Failed to parse reply.");
206 dbus_message_iter_recurse(&iter, &sub);
208 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
209 log_error("Failed to parse reply.");
214 dbus_message_iter_get_basic(&sub, &state);
216 if (streq(state, "active") || streq(state, "reloading"))
217 r = table[i].runlevel;
219 dbus_message_unref(m);
220 dbus_message_unref(reply);
229 dbus_message_unref(m);
232 dbus_message_unref(reply);
234 dbus_error_free(&error);
239 static int on_reboot(Context *c) {
245 /* We finished start-up, so let's write the utmp
246 * record and send the audit msg */
249 if (c->audit_fd >= 0)
250 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0) {
251 log_error("Failed to send audit message: %m");
256 /* If this call fails it will return 0, which
257 * utmp_put_reboot() will then fix to the current time */
258 t = get_startup_time(c);
260 if ((q = utmp_put_reboot(t)) < 0) {
261 log_error("Failed to write utmp record: %s", strerror(-q));
268 static int on_shutdown(Context *c) {
273 /* We started shut-down, so let's write the utmp
274 * record and send the audit msg */
277 if (c->audit_fd >= 0)
278 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0) {
279 log_error("Failed to send audit message: %m");
284 if ((q = utmp_put_shutdown(0)) < 0) {
285 log_error("Failed to write utmp record: %s", strerror(-q));
292 static int on_runlevel(Context *c) {
293 int r = 0, q, previous, runlevel;
297 /* We finished changing runlevel, so let's write the
298 * utmp record and send the audit msg */
300 /* First, get last runlevel */
301 if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
303 if (q != -ESRCH && q != -ENOENT) {
304 log_error("Failed to get current runlevel: %s", strerror(-q));
308 /* Hmm, we didn't find any runlevel, that means we
309 * have been rebooted */
314 /* Secondly, get new runlevel */
315 if ((runlevel = get_current_runlevel(c)) < 0)
318 if (previous == runlevel)
322 if (c->audit_fd >= 0) {
325 if (asprintf(&s, "old-level=%c new-level=%c",
326 previous > 0 ? previous : 'N',
327 runlevel > 0 ? runlevel : 'N') < 0)
330 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) {
331 log_error("Failed to send audit message: %m");
339 if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) {
340 log_error("Failed to write utmp record: %s", strerror(-q));
347 int main(int argc, char *argv[]) {
352 dbus_error_init(&error);
359 if (getppid() != 1) {
360 log_error("This program should be invoked by init only.");
365 log_error("This program requires one argument.");
369 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
370 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;