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 { '0', SPECIAL_POWEROFF_TARGET },
116 { '6', SPECIAL_REBOOT_TARGET },
117 { '5', SPECIAL_RUNLEVEL5_TARGET },
118 { '4', SPECIAL_RUNLEVEL4_TARGET },
119 { '3', SPECIAL_RUNLEVEL3_TARGET },
120 { '2', SPECIAL_RUNLEVEL2_TARGET },
121 { '1', SPECIAL_RESCUE_TARGET },
124 *interface = "org.freedesktop.systemd1.Unit",
125 *property = "ActiveState";
127 DBusMessage *m = NULL, *reply = NULL;
134 dbus_error_init(&error);
136 for (i = 0; i < ELEMENTSOF(table); i++) {
137 const char *path = NULL, *state;
138 DBusMessageIter iter, sub;
140 if (!(m = dbus_message_new_method_call(
141 "org.freedesktop.systemd1",
142 "/org/freedesktop/systemd1",
143 "org.freedesktop.systemd1.Manager",
145 log_error("Could not allocate message.");
150 if (!dbus_message_append_args(m,
151 DBUS_TYPE_STRING, &table[i].special,
152 DBUS_TYPE_INVALID)) {
153 log_error("Could not append arguments to message.");
158 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
159 dbus_error_free(&error);
163 if (!dbus_message_get_args(reply, &error,
164 DBUS_TYPE_OBJECT_PATH, &path,
165 DBUS_TYPE_INVALID)) {
166 log_error("Failed to parse reply: %s", error.message);
171 dbus_message_unref(m);
172 if (!(m = dbus_message_new_method_call(
173 "org.freedesktop.systemd1",
175 "org.freedesktop.DBus.Properties",
177 log_error("Could not allocate message.");
182 if (!dbus_message_append_args(m,
183 DBUS_TYPE_STRING, &interface,
184 DBUS_TYPE_STRING, &property,
185 DBUS_TYPE_INVALID)) {
186 log_error("Could not append arguments to message.");
191 dbus_message_unref(reply);
192 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
193 log_error("Failed to send command: %s", error.message);
198 if (!dbus_message_iter_init(reply, &iter) ||
199 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
200 log_error("Failed to parse reply.");
205 dbus_message_iter_recurse(&iter, &sub);
207 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
208 log_error("Failed to parse reply.");
213 dbus_message_iter_get_basic(&sub, &state);
215 if (streq(state, "active") || streq(state, "reloading"))
216 r = table[i].runlevel;
218 dbus_message_unref(m);
219 dbus_message_unref(reply);
228 dbus_message_unref(m);
231 dbus_message_unref(reply);
233 dbus_error_free(&error);
238 static int on_reboot(Context *c) {
244 /* We finished start-up, so let's write the utmp
245 * record and send the audit msg */
248 if (c->audit_fd >= 0)
249 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", NULL, NULL, NULL, 1) < 0) {
250 log_error("Failed to send audit message: %m");
255 /* If this call fails it will return 0, which
256 * utmp_put_reboot() will then fix to the current time */
257 t = get_startup_time(c);
259 if ((q = utmp_put_reboot(t)) < 0) {
260 log_error("Failed to write utmp record: %s", strerror(-q));
267 static int on_shutdown(Context *c) {
272 /* We started shut-down, so let's write the utmp
273 * record and send the audit msg */
276 if (c->audit_fd >= 0)
277 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", NULL, NULL, NULL, 1) < 0) {
278 log_error("Failed to send audit message: %m");
283 if ((q = utmp_put_shutdown(0)) < 0) {
284 log_error("Failed to write utmp record: %s", strerror(-q));
291 static int on_runlevel(Context *c) {
292 int r = 0, q, previous, runlevel;
296 /* We finished changing runlevel, so let's write the
297 * utmp record and send the audit msg */
299 /* First, get last runlevel */
300 if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
302 if (q != -ESRCH && q != -ENOENT) {
303 log_error("Failed to get current runlevel: %s", strerror(-q));
307 /* Hmm, we didn't find any runlevel, that means we
308 * have been rebooted */
313 /* Second get new runlevel */
314 if ((runlevel = get_current_runlevel(c)) < 0)
317 if (previous == runlevel)
321 if (c->audit_fd >= 0) {
324 if (asprintf(&s, "old-level=%c new-level=%c", previous, runlevel) < 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_info("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_info("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;