time-util: add macros around timespec_store() that operates on compund literal allocated timespec struct

This way we can convert usec_t to timespec on-the-fly, without a buffer.

No actual behaviour change just some shortening of code.
This commit is contained in:
Lennart Poettering
2022-03-18 17:13:36 +01:00
parent de0988f9d2
commit 52bb308c13
10 changed files with 17 additions and 26 deletions
+1 -2
View 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)
+1 -2
View 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) {
+4
View 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_;
+1 -3
View 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.",
+2 -3
View 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;
+2 -4
View 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;
+2 -3
View 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;
+1 -2
View 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");
}
@@ -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");
+1 -2
View 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);