]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: add macros around timespec_store() that operates on compund literal alloca...
authorLennart Poettering <lennart@poettering.net>
Fri, 18 Mar 2022 13:19:20 +0000 (14:19 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 18 Mar 2022 16:13:36 +0000 (17:13 +0100)
This way we can convert usec_t to timespec on-the-fly, without a buffer.

No actual behaviour change just some shortening of code.

src/basic/io-util.c
src/basic/process-util.c
src/basic/time-util.h
src/core/main.c
src/libsystemd/sd-event/sd-event.c
src/nspawn/nspawn-stub-pid1.c
src/shared/clock-util.c
src/timesync/timesyncd.c
src/tty-ask-password-agent/tty-ask-password-agent.c
src/userdb/userdbd-manager.c

index 783ca1309dede27c8f2245cc34c542f50ceeb1a4..a591a75c3741ad70afc5050bad593271059eb90a 100644 (file)
@@ -159,7 +159,6 @@ int pipe_eof(int fd) {
 }
 
 int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
-        struct timespec ts;
         int r;
 
         assert(fds || nfds == 0);
@@ -167,7 +166,7 @@ int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
         if (nfds == 0)
                 return 0;
 
-        r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : timespec_store(&ts, timeout), NULL);
+        r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), NULL);
         if (r < 0)
                 return -errno;
         if (r == 0)
index 369edce816b2dc341c7580cfac7f255ea44cbfb2..680900c7313e38af1af61cd7ace792fe3b1e0745 100644 (file)
@@ -820,13 +820,12 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
         for (;;) {
                 usec_t n;
                 siginfo_t status = {};
-                struct timespec ts;
 
                 n = now(CLOCK_MONOTONIC);
                 if (n >= until)
                         break;
 
-                r = RET_NERRNO(sigtimedwait(&mask, NULL, timespec_store(&ts, until - n)));
+                r = RET_NERRNO(sigtimedwait(&mask, NULL, TIMESPEC_STORE(until - n)));
                 /* Assuming we woke due to the child exiting. */
                 if (waitid(P_PID, pid, &status, WEXITED|WNOHANG) == 0) {
                         if (status.si_pid == pid) {
index 01a72026e395cdf60d950c700ed4d4d5e635207c..d3a88ed6e4bf274a1323fefcf694a52b52d7167f 100644 (file)
@@ -115,9 +115,13 @@ nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
 struct timespec* timespec_store(struct timespec *ts, usec_t u);
 struct timespec* timespec_store_nsec(struct timespec *ts, nsec_t n);
 
+#define TIMESPEC_STORE(u) timespec_store(&(struct timespec) {}, (u))
+
 usec_t timeval_load(const struct timeval *tv) _pure_;
 struct timeval* timeval_store(struct timeval *tv, usec_t u);
 
+#define TIMEVAL_STORE(u) timeval_store(&(struct timeval) {}, (u))
+
 char* format_timestamp_style(char *buf, size_t l, usec_t t, TimestampStyle style) _warn_unused_result_;
 char* format_timestamp_relative(char *buf, size_t l, usec_t t) _warn_unused_result_;
 char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) _warn_unused_result_;
index c13534e98a2c795f84dc8e8498cc5cee134a269c..be403dee7957b9554824518844d272eb4c03d67f 100644 (file)
@@ -1571,8 +1571,6 @@ static void initialize_clock(void) {
 }
 
 static void apply_clock_update(void) {
-        struct timespec ts;
-
         /* This is called later than initialize_clock(), i.e. after we parsed configuration files/kernel
          * command line and such. */
 
@@ -1582,7 +1580,7 @@ static void apply_clock_update(void) {
         if (getpid_cached() != 1)
                 return;
 
-        if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, arg_clock_usec)) < 0)
+        if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(arg_clock_usec)) < 0)
                 log_error_errno(errno, "Failed to set system clock to time specified on kernel command line: %m");
         else
                 log_info("Set system clock to %s, as specified on the kernel command line.",
index 82056998bd64cce6e17c84cd61ee6d1368060321..fcb242fefadd4d6b08dafd4f4b25a15c393ec084 100644 (file)
@@ -3906,6 +3906,7 @@ static int epoll_wait_usec(
         int msec;
 #if 0
         static bool epoll_pwait2_absent = false;
+        int r;
 
         /* A wrapper that uses epoll_pwait2() if available, and falls back to epoll_wait() if not.
          *
@@ -3914,12 +3915,10 @@ static int epoll_wait_usec(
          * https://github.com/systemd/systemd/issues/19052. */
 
         if (!epoll_pwait2_absent && timeout != USEC_INFINITY) {
-                struct timespec ts;
-
                 r = epoll_pwait2(fd,
                                  events,
                                  maxevents,
-                                 timespec_store(&ts, timeout),
+                                 TIMESPEC_STORE(timeout),
                                  NULL);
                 if (r >= 0)
                         return r;
index 6dbd6ba4c9e45983346ee5fdec46e3a2336eb62d..85c439815c6ddcb332f62fe98ef6841825b4ea7a 100644 (file)
@@ -142,10 +142,8 @@ int stub_pid1(sd_id128_t uuid) {
 
                 if (quit_usec == USEC_INFINITY)
                         r = sigwaitinfo(&waitmask, &si);
-                else {
-                        struct timespec ts;
-                        r = sigtimedwait(&waitmask, &si, timespec_store(&ts, quit_usec - current_usec));
-                }
+                else
+                        r = sigtimedwait(&waitmask, &si, TIMESPEC_STORE(quit_usec - current_usec));
                 if (r < 0) {
                         if (errno == EINTR) /* strace -p attach can result in EINTR, let's handle this nicely. */
                                 continue;
index febea8ea85463c9340c61042363391f2f4f0f53f..eb6f12a2556b028d29a1d234e344bc7d8e026503 100644 (file)
@@ -134,9 +134,8 @@ int clock_reset_timewarp(void) {
 #define EPOCH_FILE "/usr/lib/clock-epoch"
 
 int clock_apply_epoch(ClockChangeDirection *ret_attempted_change) {
-        struct stat st;
-        struct timespec ts;
         usec_t epoch_usec, now_usec;
+        struct stat st;
 
         /* NB: we update *ret_attempted_change in *all* cases, both
          * on success and failure, to indicate what we intended to do! */
@@ -161,7 +160,7 @@ int clock_apply_epoch(ClockChangeDirection *ret_attempted_change) {
                 return 0;
         }
 
-        if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, epoch_usec)) < 0)
+        if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(epoch_usec)) < 0)
                 return -errno;
 
         return 1;
index 6f316746f5889453c496b63845f7895167ae7516..471a63689ee6d25dfe3be7391f7869ff5c5143f0 100644 (file)
@@ -72,13 +72,12 @@ static int load_clock_timestamp(uid_t uid, gid_t gid) {
 settime:
         ct = now(CLOCK_REALTIME);
         if (ct < min) {
-                struct timespec ts;
                 char date[FORMAT_TIMESTAMP_MAX];
 
                 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
                          format_timestamp(date, sizeof(date), min));
 
-                if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
+                if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(min)) < 0)
                         log_error_errno(errno, "Failed to restore system clock, ignoring: %m");
         }
 
index e98b38510ebc43c227440e1250a919b186a11724..253559377556e9362473b759f322e73144e9627d 100644 (file)
@@ -581,8 +581,6 @@ static int ask_on_this_console(const char *tty, pid_t *ret_pid, char **arguments
 }
 
 static void terminate_agents(Set *pids) {
-        struct timespec ts;
-        siginfo_t status = {};
         sigset_t set;
         void *p;
         int r, signum;
@@ -599,11 +597,10 @@ static void terminate_agents(Set *pids) {
          */
         assert_se(sigemptyset(&set) >= 0);
         assert_se(sigaddset(&set, SIGCHLD) >= 0);
-        timespec_store(&ts, 50 * USEC_PER_MSEC);
 
         while (!set_isempty(pids)) {
+                siginfo_t status = {};
 
-                zero(status);
                 r = waitid(P_ALL, 0, &status, WEXITED|WNOHANG);
                 if (r < 0 && errno == EINTR)
                         continue;
@@ -613,7 +610,7 @@ static void terminate_agents(Set *pids) {
                         continue;
                 }
 
-                signum = sigtimedwait(&set, NULL, &ts);
+                signum = sigtimedwait(&set, NULL, TIMESPEC_STORE(50 * USEC_PER_MSEC));
                 if (signum < 0) {
                         if (errno != EAGAIN)
                                 log_error_errno(errno, "sigtimedwait() failed: %m");
index 0564840dbe8db1ba4fb64dae18fdb8aaa826031d..aabf8070d2a58366075c517e40a9a54e7dbe10ed 100644 (file)
@@ -251,7 +251,6 @@ static int start_workers(Manager *m, bool explicit_request) {
 }
 
 int manager_startup(Manager *m) {
-        struct timeval ts;
         int n, r;
 
         assert(m);
@@ -300,7 +299,7 @@ int manager_startup(Manager *m) {
 
         /* Let's make sure every accept() call on this socket times out after 25s. This allows workers to be
          * GC'ed on idle */
-        if (setsockopt(m->listen_fd, SOL_SOCKET, SO_RCVTIMEO, timeval_store(&ts, LISTEN_TIMEOUT_USEC), sizeof(ts)) < 0)
+        if (setsockopt(m->listen_fd, SOL_SOCKET, SO_RCVTIMEO, TIMEVAL_STORE(LISTEN_TIMEOUT_USEC), sizeof(struct timeval)) < 0)
                 return log_error_errno(errno, "Failed to se SO_RCVTIMEO: %m");
 
         return start_workers(m, false);