chiark / gitweb /
journald: adjust permissions for rotated files
[elogind.git] / src / shutdownd.c
index 59439bca850cb2d719ac5307aff5d6abe59b98cd..b4052d4933b94b241d5927fa41934e07abc30e68 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 
+#include <systemd/sd-daemon.h>
+
 #include "shutdownd.h"
 #include "log.h"
 #include "macro.h"
 #include "util.h"
-#include "sd-daemon.h"
 #include "utmp-wtmp.h"
 
 static int read_packet(int fd, struct shutdownd_command *_c) {
@@ -100,6 +101,9 @@ static int read_packet(int fd, struct shutdownd_command *_c) {
 }
 
 static void warn_wall(usec_t n, struct shutdownd_command *c) {
+        char date[FORMAT_TIMESTAMP_MAX];
+        const char *prefix;
+        char *l = NULL;
 
         assert(c);
         assert(c->warn_wall);
@@ -107,28 +111,21 @@ static void warn_wall(usec_t n, struct shutdownd_command *c) {
         if (n >= c->elapse)
                 return;
 
-        if (c->wall_message[0])
-                utmp_wall(c->wall_message);
+        if (c->mode == 'H')
+                prefix = "The system is going down for system halt at ";
+        else if (c->mode == 'P')
+                prefix = "The system is going down for power-off at ";
+        else if (c->mode == 'r')
+                prefix = "The system is going down for reboot at ";
+        else
+                assert_not_reached("Unknown mode!");
+
+        if (asprintf(&l, "%s%s%s%s!", c->wall_message, c->wall_message[0] ? "\n" : "",
+                     prefix, format_timestamp(date, sizeof(date), c->elapse)) < 0)
+                log_error("Failed to allocate wall message");
         else {
-                char date[FORMAT_TIMESTAMP_MAX];
-                const char* prefix;
-                char *l = NULL;
-
-                if (c->mode == 'H')
-                        prefix = "The system is going down for system halt at ";
-                else if (c->mode == 'P')
-                        prefix = "The system is going down for power-off at ";
-                else if (c->mode == 'r')
-                        prefix = "The system is going down for reboot at ";
-                else
-                        assert_not_reached("Unknown mode!");
-
-                if (asprintf(&l, "%s%s!", prefix, format_timestamp(date, sizeof(date), c->elapse)) < 0)
-                        log_error("Failed to allocate wall message");
-                else {
-                        utmp_wall(l);
-                        free(l);
-                }
+                utmp_wall(l, NULL);
+                free(l);
         }
 }
 
@@ -176,8 +173,7 @@ int main(int argc, char *argv[]) {
                 _FD_MAX
         };
 
-        int r = 4, n_fds;
-        int one = 1;
+        int r = EXIT_FAILURE, n_fds;
         struct shutdownd_command c;
         struct pollfd pollfd[_FD_MAX];
         bool exec_shutdown = false, unlink_nologin = false, failed = false;
@@ -185,31 +181,28 @@ int main(int argc, char *argv[]) {
 
         if (getppid() != 1) {
                 log_error("This program should be invoked by init only.");
-                return 1;
+                return EXIT_FAILURE;
         }
 
         if (argc > 1) {
                 log_error("This program does not take arguments.");
-                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);
+
         if ((n_fds = sd_listen_fds(true)) < 0) {
                 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
-                return 1;
+                return EXIT_FAILURE;
         }
 
         if (n_fds != 1) {
                 log_error("Need exactly one file descriptor.");
-                return 2;
-        }
-
-        if (setsockopt(SD_LISTEN_FDS_START, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
-                log_error("SO_PASSCRED failed: %m");
-                return 3;
+                return EXIT_FAILURE;
         }
 
         zero(c);
@@ -227,7 +220,7 @@ int main(int argc, char *argv[]) {
 
                 if ((pollfd[i].fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) {
                         log_error("timerfd_create(): %m");
-                        failed = false;
+                        failed = true;
                 }
         }
 
@@ -318,10 +311,10 @@ int main(int argc, char *argv[]) {
                 if (pollfd[FD_NOLOGIN_TIMER].revents) {
                         int e;
 
-                        log_info("Creating /etc/nologin, blocking further logins...");
+                        log_info("Creating /run/nologin, blocking further logins...");
 
-                        if ((e = touch("/etc/nologin")) < 0)
-                                log_error("Failed to create /etc/nologin: %s", strerror(-e));
+                        if ((e = write_one_line_file_atomic("/run/nologin", "System is going down.")) < 0)
+                                log_error("Failed to create /run/nologin: %s", strerror(-e));
                         else
                                 unlink_nologin = true;
 
@@ -335,7 +328,7 @@ int main(int argc, char *argv[]) {
 
         } while (c.elapse > 0);
 
-        r = 0;
+        r = EXIT_SUCCESS;
 
         log_debug("systemd-shutdownd stopped as pid %lu", (unsigned long) getpid());
 
@@ -345,7 +338,10 @@ finish:
                 if (pollfd[i].fd >= 0)
                         close_nointr_nofail(pollfd[i].fd);
 
-        if (exec_shutdown) {
+        if (unlink_nologin)
+                unlink("/run/nologin");
+
+        if (exec_shutdown && !c.dry_run) {
                 char sw[3];
 
                 sw[0] = '-';
@@ -363,9 +359,6 @@ finish:
                 log_error("Failed to execute /sbin/shutdown: %m");
         }
 
-        if (unlink_nologin)
-                unlink("/etc/nologin");
-
         sd_notify(false,
                   "STATUS=Exiting...");