chiark / gitweb /
util: add a bit of syntactic sugar to run short code fragments with a different umask
[elogind.git] / src / core / manager.c
index a3eeb4afc10c9837c238bc4e1c9d739e6b9cda25..e26522a4dd0944460c9e3baca37ee05522eace40 100644 (file)
@@ -95,7 +95,7 @@ static int manager_setup_notify(Manager *m) {
                 struct sockaddr_un un;
         } sa;
         struct epoll_event ev;
-        int one = 1;
+        int one = 1, r;
 
         assert(m);
 
@@ -116,12 +116,15 @@ static int manager_setup_notify(Manager *m) {
 
         sa.un.sun_path[0] = 0;
 
-        if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
+        r = bind(m->notify_watch.fd, &sa.sa,
+                 offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
+        if (r < 0) {
                 log_error("bind() failed: %m");
                 return -errno;
         }
 
-        if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
+        r = setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
+        if (r < 0) {
                 log_error("SO_PASSCRED failed: %m");
                 return -errno;
         }
@@ -130,7 +133,8 @@ static int manager_setup_notify(Manager *m) {
         ev.events = EPOLLIN;
         ev.data.ptr = &m->notify_watch;
 
-        if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0) {
+        r = epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev);
+        if (r < 0) {
                 log_error("Failed to add notification socket fd to epoll: %m");
                 return -errno;
         }
@@ -226,10 +230,8 @@ static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned po
         assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */
 
         if (pos > 1) {
-                if (pos > 2) {
-                        memset(p, ' ', pos-2);
-                        p += pos-2;
-                }
+                if (pos > 2)
+                        p = mempset(p, ' ', pos-2);
                 p = stpcpy(p, ANSI_RED_ON);
                 *p++ = '*';
         }
@@ -244,10 +246,8 @@ static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned po
         if (pos < width) {
                 p = stpcpy(p, ANSI_RED_ON);
                 *p++ = '*';
-                if (pos < width-1) {
-                        memset(p, ' ', width-1-pos);
-                        p += width-1-pos;
-                }
+                if (pos < width-1)
+                        p = mempset(p, ' ', width-1-pos);
                 p = stpcpy(p, ANSI_HIGHLIGHT_OFF);
         }
 }
@@ -1192,7 +1192,7 @@ static int manager_process_notify_fd(Manager *m) {
                         uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
                 } control;
                 Unit *u;
-                char **tags;
+                char _cleanup_strv_free_ **tags = NULL;
 
                 zero(iovec);
                 iovec.iov_base = buf;
@@ -1245,8 +1245,6 @@ static int manager_process_notify_fd(Manager *m) {
 
                 if (UNIT_VTABLE(u)->notify_message)
                         UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
-
-                strv_free(tags);
         }
 
         return 0;
@@ -1902,7 +1900,8 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
 
         /* We set SOCK_NONBLOCK here so that we rather drop the
          * message then wait for plymouth */
-        if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
+        fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
+        if (fd < 0) {
                 log_error("socket() failed: %m");
                 return;
         }
@@ -1985,7 +1984,6 @@ void manager_dispatch_bus_query_pid_done(
 
 int manager_open_serialization(Manager *m, FILE **_f) {
         char *path = NULL;
-        mode_t saved_umask;
         int fd;
         FILE *f;
 
@@ -1999,9 +1997,9 @@ int manager_open_serialization(Manager *m, FILE **_f) {
         if (!path)
                 return -ENOMEM;
 
-        saved_umask = umask(0077);
-        fd = mkostemp(path, O_RDWR|O_CLOEXEC);
-        umask(saved_umask);
+        RUN_WITH_UMASK(0077) {
+                fd = mkostemp(path, O_RDWR|O_CLOEXEC);
+        }
 
         if (fd < 0) {
                 free(path);
@@ -2337,6 +2335,12 @@ static bool manager_is_booting_or_shutting_down(Manager *m) {
         return false;
 }
 
+bool manager_is_reloading_or_reexecuting(Manager *m) {
+        assert(m);
+
+        return m->n_reloading != 0;
+}
+
 void manager_reset_failed(Manager *m) {
         Unit *u;
         Iterator i;
@@ -2410,10 +2414,10 @@ void manager_check_finished(Manager *m) {
                                            "INITRD_USEC=%llu", (unsigned long long) initrd_usec,
                                            "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
                                            "MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
-                                           format_timespan(kernel, sizeof(kernel), kernel_usec),
-                                           format_timespan(initrd, sizeof(initrd), initrd_usec),
-                                           format_timespan(userspace, sizeof(userspace), userspace_usec),
-                                           format_timespan(sum, sizeof(sum), total_usec),
+                                           format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
+                                           format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC),
+                                           format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),
+                                           format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
                                            NULL);
                 } else {
                         kernel_usec = m->userspace_timestamp.monotonic - m->kernel_timestamp.monotonic;
@@ -2425,9 +2429,9 @@ void manager_check_finished(Manager *m) {
                                            "KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
                                            "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
                                            "MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.",
-                                           format_timespan(kernel, sizeof(kernel), kernel_usec),
-                                           format_timespan(userspace, sizeof(userspace), userspace_usec),
-                                           format_timespan(sum, sizeof(sum), total_usec),
+                                           format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
+                                           format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),
+                                           format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
                                            NULL);
                 }
         } else {
@@ -2439,7 +2443,7 @@ void manager_check_finished(Manager *m) {
                                    MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
                                    "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
                                    "MESSAGE=Startup finished in %s.",
-                                   format_timespan(sum, sizeof(sum), total_usec),
+                                   format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
                                    NULL);
         }
 
@@ -2447,7 +2451,7 @@ void manager_check_finished(Manager *m) {
 
         sd_notifyf(false,
                    "READY=1\nSTATUS=Startup finished in %s.",
-                   format_timespan(sum, sizeof(sum), total_usec));
+                   format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC));
 }
 
 static int create_generator_dir(Manager *m, char **generator, const char *name) {
@@ -2510,7 +2514,6 @@ void manager_run_generators(Manager *m) {
         DIR *d = NULL;
         const char *generator_path;
         const char *argv[5];
-        mode_t u;
         int r;
 
         assert(m);
@@ -2544,9 +2547,9 @@ void manager_run_generators(Manager *m) {
         argv[3] = m->generator_unit_path_late;
         argv[4] = NULL;
 
-        u = umask(0022);
-        execute_directory(generator_path, d, (char**) argv);
-        umask(u);
+        RUN_WITH_UMASK(0022) {
+                execute_directory(generator_path, d, (char**) argv);
+        }
 
         trim_generator_dir(m, &m->generator_unit_path);
         trim_generator_dir(m, &m->generator_unit_path_early);