chiark / gitweb /
shared: introduce cmsg_close_all() call
[elogind.git] / src / shutdownd / shutdownd.c
index 4a1719849ba2aec5e57da674909ce84e6f2b201e..60a646878e501725459aa1fbe971dba37f9b3084 100644 (file)
@@ -20,7 +20,7 @@
 ***/
 
 #include <sys/socket.h>
-#include <sys/poll.h>
+#include <poll.h>
 #include <sys/types.h>
 #include <sys/timerfd.h>
 #include <assert.h>
@@ -30,8 +30,8 @@
 #include <fcntl.h>
 #include <stddef.h>
 
-#include <systemd/sd-daemon.h>
-#include <systemd/sd-shutdown.h>
+#include "systemd/sd-daemon.h"
+#include "systemd/sd-shutdown.h"
 
 #include "log.h"
 #include "macro.h"
@@ -52,8 +52,8 @@ static int read_packet(int fd, union shutdown_buffer *_b) {
         union shutdown_buffer b; /* We maintain our own copy here, in
                                   * order not to corrupt the last message */
         struct iovec iovec = {
-                iovec.iov_base = &b,
-                iovec.iov_len = sizeof(b) - 1,
+                .iov_base = &b,
+                .iov_len = sizeof(b) - 1,
         };
         union {
                 struct cmsghdr cmsghdr;
@@ -61,28 +61,30 @@ static int read_packet(int fd, union shutdown_buffer *_b) {
         } control = {};
         struct msghdr msghdr = {
                 .msg_iov = &iovec,
-                msghdr.msg_iovlen = 1,
-                msghdr.msg_control = &control,
-                msghdr.msg_controllen = sizeof(control),
+                .msg_iovlen = 1,
+                .msg_control = &control,
+                .msg_controllen = sizeof(control),
         };
 
         assert(fd >= 0);
         assert(_b);
 
         n = recvmsg(fd, &msghdr, MSG_DONTWAIT);
-        if (n <= 0) {
-                if (n == 0) {
-                        log_error("Short read");
-                        return -EIO;
-                }
-
+        if (n < 0) {
                 if (errno == EAGAIN || errno == EINTR)
                         return 0;
 
-                log_error("recvmsg(): %m");
+                log_error_errno(errno, "recvmsg(): %m");
                 return -errno;
         }
 
+        cmsg_close_all(&msghdr);
+
+        if (n == 0) {
+                log_error("Short read");
+                return -EIO;
+        }
+
         if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
             control.cmsghdr.cmsg_level != SOL_SOCKET ||
             control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
@@ -120,7 +122,7 @@ static int read_packet(int fd, union shutdown_buffer *_b) {
 static void warn_wall(usec_t n, struct sd_shutdown_command *c) {
         char date[FORMAT_TIMESTAMP_MAX];
         const char *prefix;
-        char *l = NULL;
+        _cleanup_free_ char *l = NULL;
 
         assert(c);
         assert(c->warn_wall);
@@ -142,12 +144,10 @@ static void warn_wall(usec_t n, struct sd_shutdown_command *c) {
                 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->usec)) < 0)
+                     prefix, format_timestamp(date, sizeof(date), c->usec)) >= 0)
+                utmp_wall(l, NULL, NULL);
+        else
                 log_error("Failed to allocate wall message");
-        else {
-                utmp_wall(l, NULL);
-                free(l);
-        }
 }
 
 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
@@ -199,27 +199,22 @@ static const char *mode_to_string(enum sd_shutdown_mode m) {
 
 static int update_schedule_file(struct sd_shutdown_command *c) {
         int r;
-        FILE *f;
-        char *temp_path, *t;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *t = NULL, *temp_path = NULL;
 
         assert(c);
 
         r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0);
-        if (r < 0) {
-                log_error("Failed to create shutdown subdirectory: %s", strerror(-r));
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
 
         t = cescape(c->wall_message);
         if (!t)
                 return log_oom();
 
         r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
-        if (r < 0) {
-                log_error("Failed to save information about scheduled shutdowns: %s", strerror(-r));
-                free(t);
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
 
         fchmod(fileno(f), 0644);
 
@@ -237,21 +232,16 @@ static int update_schedule_file(struct sd_shutdown_command *c) {
         if (!isempty(t))
                 fprintf(f, "WALL_MESSAGE=%s\n", t);
 
-        free(t);
-
         fflush(f);
 
         if (ferror(f) || rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
-                log_error("Failed to write information about scheduled shutdowns: %m");
+                log_error_errno(errno, "Failed to write information about scheduled shutdowns: %m");
                 r = -errno;
 
                 unlink(temp_path);
                 unlink("/run/systemd/shutdown/scheduled");
         }
 
-        fclose(f);
-        free(temp_path);
-
         return r;
 }
 
@@ -292,7 +282,7 @@ int main(int argc, char *argv[]) {
 
         n_fds = sd_listen_fds(true);
         if (n_fds < 0) {
-                log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
+                log_error_errno(r, "Failed to read listening file descriptors from environment: %m");
                 return EXIT_FAILURE;
         }
 
@@ -308,12 +298,12 @@ int main(int argc, char *argv[]) {
                 pollfd[i].events = POLLIN;
                 pollfd[i].fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
                 if (pollfd[i].fd < 0) {
-                        log_error("timerfd_create(): %m");
+                        log_error_errno(errno, "timerfd_create(): %m");
                         goto finish;
                 }
         }
 
-        log_debug("systemd-shutdownd running as pid %lu", (unsigned long) getpid());
+        log_debug("systemd-shutdownd running as pid "PID_FMT, getpid());
 
         sd_notify(false,
                   "READY=1\n"
@@ -329,7 +319,7 @@ int main(int argc, char *argv[]) {
                         if (errno == EAGAIN || errno == EINTR)
                                 continue;
 
-                        log_error("poll(): %m");
+                        log_error_errno(errno, "poll(): %m");
                         goto finish;
                 }
 
@@ -366,7 +356,7 @@ int main(int argc, char *argv[]) {
                                                 warn_wall(n, &b.command);
                                 }
                                 if (timerfd_settime(pollfd[FD_WALL_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
-                                        log_error("timerfd_settime(): %m");
+                                        log_error_errno(errno, "timerfd_settime(): %m");
                                         goto finish;
                                 }
 
@@ -374,7 +364,7 @@ int main(int argc, char *argv[]) {
                                 zero(its);
                                 timespec_store(&its.it_value, when_nologin(b.command.usec));
                                 if (timerfd_settime(pollfd[FD_NOLOGIN_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
-                                        log_error("timerfd_settime(): %m");
+                                        log_error_errno(errno, "timerfd_settime(): %m");
                                         goto finish;
                                 }
 
@@ -382,7 +372,7 @@ int main(int argc, char *argv[]) {
                                 zero(its);
                                 timespec_store(&its.it_value, b.command.usec);
                                 if (timerfd_settime(pollfd[FD_SHUTDOWN_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
-                                        log_error("timerfd_settime(): %m");
+                                        log_error_errno(errno, "timerfd_settime(): %m");
                                         goto finish;
                                 }
 
@@ -406,7 +396,7 @@ int main(int argc, char *argv[]) {
                         /* Restart timer */
                         timespec_store(&its.it_value, when_wall(n, b.command.usec));
                         if (timerfd_settime(pollfd[FD_WALL_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
-                                log_error("timerfd_settime(): %m");
+                                log_error_errno(errno, "timerfd_settime(): %m");
                                 goto finish;
                         }
                 }
@@ -418,7 +408,7 @@ int main(int argc, char *argv[]) {
 
                         e = write_string_file_atomic("/run/nologin", "System is going down.");
                         if (e < 0)
-                                log_error("Failed to create /run/nologin: %s", strerror(-e));
+                                log_error_errno(e, "Failed to create /run/nologin: %m");
                         else
                                 unlink_nologin = true;
 
@@ -433,13 +423,12 @@ int main(int argc, char *argv[]) {
 
         r = EXIT_SUCCESS;
 
-        log_debug("systemd-shutdownd stopped as pid %lu", (unsigned long) getpid());
+        log_debug("systemd-shutdownd stopped as pid "PID_FMT, getpid());
 
 finish:
 
         for (i = 0; i < _FD_MAX; i++)
-                if (pollfd[i].fd >= 0)
-                        close_nointr_nofail(pollfd[i].fd);
+                safe_close(pollfd[i].fd);
 
         if (unlink_nologin)
                 unlink("/run/nologin");
@@ -461,10 +450,11 @@ finish:
                       (b.command.warn_wall ? NULL : "--no-wall"),
                       NULL);
 
-                log_error("Failed to execute /sbin/shutdown: %m");
+                log_error_errno(errno, "Failed to execute /sbin/shutdown: %m");
         }
 
         sd_notify(false,
+                  "STOPPING=\n"
                   "STATUS=Exiting...");
 
         return r;