X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fupdate-utmp.c;h=0d177d61645d56f7ebd37ff8e78349cf2818bd2b;hp=e64a819aa4fed54af1675a218974a9d82b616860;hb=6bc1ce40e531257ae8dd9bbbbcb48e5de0a1187b;hpb=4927fcae48de061393b3ce9c12d49f80d73fbf1d diff --git a/src/update-utmp.c b/src/update-utmp.c index e64a819aa..0d177d616 100644 --- a/src/update-utmp.c +++ b/src/update-utmp.c @@ -1,4 +1,4 @@ -/*-*- Mode: C; c-basic-offset: 8 -*-*/ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ /*** This file is part of systemd. @@ -22,12 +22,15 @@ #include #include #include -#include #include #include #include +#ifdef HAVE_AUDIT +#include +#endif + #include "log.h" #include "macro.h" #include "util.h" @@ -74,7 +77,7 @@ static usec_t get_startup_time(Context *c) { } if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) { - log_error("Failed to send command: %s", error.message); + log_error("Failed to send command: %s", bus_error_message(&error)); goto finish; } @@ -111,14 +114,15 @@ static int get_current_runlevel(Context *c) { const char *special; } table[] = { /* The first target of this list that is active or has - * a job scheduled wins */ - { '0', SPECIAL_POWEROFF_TARGET }, - { '6', SPECIAL_REBOOT_TARGET }, + * a job scheduled wins. We prefer runlevels 5 and 3 + * here over the others, since these are the main + * runlevels used on Fedora. It might make sense to + * change the order on some distributions. */ { '5', SPECIAL_RUNLEVEL5_TARGET }, - { '4', SPECIAL_RUNLEVEL4_TARGET }, { '3', SPECIAL_RUNLEVEL3_TARGET }, + { '4', SPECIAL_RUNLEVEL4_TARGET }, { '2', SPECIAL_RUNLEVEL2_TARGET }, - { '1', SPECIAL_RESCUE_TARGET }, + { 'S', SPECIAL_RESCUE_TARGET }, }; const char *interface = "org.freedesktop.systemd1.Unit", @@ -163,7 +167,7 @@ static int get_current_runlevel(Context *c) { if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) { - log_error("Failed to parse reply: %s", error.message); + log_error("Failed to parse reply: %s", bus_error_message(&error)); r = -EIO; goto finish; } @@ -190,7 +194,7 @@ static int get_current_runlevel(Context *c) { dbus_message_unref(reply); if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) { - log_error("Failed to send command: %s", error.message); + log_error("Failed to send command: %s", bus_error_message(&error)); r = -EIO; goto finish; } @@ -246,7 +250,7 @@ static int on_reboot(Context *c) { #ifdef HAVE_AUDIT if (c->audit_fd >= 0) - if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", NULL, NULL, NULL, 1) < 0) { + if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0) { log_error("Failed to send audit message: %m"); r = -errno; } @@ -274,13 +278,13 @@ static int on_shutdown(Context *c) { #ifdef HAVE_AUDIT if (c->audit_fd >= 0) - if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", NULL, NULL, NULL, 1) < 0) { + if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0) { log_error("Failed to send audit message: %m"); r = -errno; } #endif - if ((q = utmp_put_shutdown(0)) < 0) { + if ((q = utmp_put_shutdown()) < 0) { log_error("Failed to write utmp record: %s", strerror(-q)); r = q; } @@ -310,7 +314,7 @@ static int on_runlevel(Context *c) { previous = 0; } - /* Second get new runlevel */ + /* Secondly, get new runlevel */ if ((runlevel = get_current_runlevel(c)) < 0) return runlevel; @@ -321,7 +325,9 @@ static int on_runlevel(Context *c) { if (c->audit_fd >= 0) { char *s = NULL; - if (asprintf(&s, "old-level=%c new-level=%c", previous, runlevel) < 0) + if (asprintf(&s, "old-level=%c new-level=%c", + previous > 0 ? previous : 'N', + runlevel > 0 ? runlevel : 'N') < 0) return -ENOMEM; if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) { @@ -333,7 +339,7 @@ static int on_runlevel(Context *c) { } #endif - if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) { + if ((q = utmp_put_runlevel(runlevel, previous)) < 0) { log_error("Failed to write utmp record: %s", strerror(-q)); r = q; } @@ -353,31 +359,37 @@ int main(int argc, char *argv[]) { c.audit_fd = -1; #endif - /* if (getppid() != 1) { */ - /* log_error("This program should be invoked by init only."); */ - /* return 1; */ - /* } */ + if (getppid() != 1) { + log_error("This program should be invoked by init only."); + return EXIT_FAILURE; + } if (argc != 2) { log_error("This program requires one argument."); - return 1; + return EXIT_FAILURE; } - log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); + log_set_target(LOG_TARGET_AUTO); log_parse_environment(); + log_open(); + + umask(0022); #ifdef HAVE_AUDIT - if ((c.audit_fd = audit_open()) < 0) + if ((c.audit_fd = audit_open()) < 0 && + /* If the kernel lacks netlink or audit support, + * don't worry about it. */ + errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT) log_error("Failed to connect to audit log: %m"); #endif if (bus_connect(DBUS_BUS_SYSTEM, &c.bus, NULL, &error) < 0) { - log_error("Failed to get D-Bus connection: %s", error.message); + log_error("Failed to get D-Bus connection: %s", bus_error_message(&error)); r = -EIO; goto finish; } - log_info("systemd-update-utmp running as pid %lu", (unsigned long) getpid()); + log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid()); if (streq(argv[1], "reboot")) r = on_reboot(&c); @@ -390,21 +402,22 @@ int main(int argc, char *argv[]) { r = -EINVAL; } - log_info("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid()); -finish: + log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid()); +finish: #ifdef HAVE_AUDIT if (c.audit_fd >= 0) audit_close(c.audit_fd); #endif if (c.bus) { - dbus_connection_close(c.bus); - dbus_connection_unref(c.bus); + dbus_connection_flush(c.bus); + dbus_connection_close(c.bus); + dbus_connection_unref(c.bus); } dbus_error_free(&error); dbus_shutdown(); - return r < 0 ? 1 : 0; + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }