summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-03-03 12:56:52 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-03-05 07:10:08 +0900
commit496db330041151138809c8f8579fa91d7897aabe (patch)
tree865ff3fde1989e4bf931da2563dfd3315401f3e9 /src
parente6283cbf48a3821d03ec73252620fc1b04bd4588 (diff)
downloadsystemd-496db330041151138809c8f8579fa91d7897aabe.tar.gz
tree-wide: use usec_add() and usec_sub_unsigned()
Diffstat (limited to 'src')
-rw-r--r--src/ask-password/ask-password.c2
-rw-r--r--src/basic/process-util.c2
-rw-r--r--src/basic/ratelimit.c2
-rw-r--r--src/basic/terminal-util.c6
-rw-r--r--src/cgtop/cgtop.c15
-rw-r--r--src/cryptsetup/cryptsetup.c5
-rw-r--r--src/journal/journald-server.c4
-rw-r--r--src/libsystemd/sd-bus/sd-bus.c6
-rw-r--r--src/libsystemd/sd-event/sd-event.c5
-rw-r--r--src/libsystemd/sd-journal/sd-journal.c5
-rw-r--r--src/libsystemd/sd-netlink/sd-netlink.c2
-rw-r--r--src/login/logind-dbus.c2
-rw-r--r--src/network/networkd-ndisc.c8
-rw-r--r--src/network/wait-online/manager.c2
14 files changed, 30 insertions, 36 deletions
diff --git a/src/ask-password/ask-password.c b/src/ask-password/ask-password.c
index 599ffcdf09..6b89f57e1b 100644
--- a/src/ask-password/ask-password.c
+++ b/src/ask-password/ask-password.c
@@ -166,7 +166,7 @@ static int run(int argc, char *argv[]) {
return r;
if (arg_timeout > 0)
- timeout = now(CLOCK_MONOTONIC) + arg_timeout;
+ timeout = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
else
timeout = 0;
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 304b03960a..4eb524d211 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -756,7 +756,7 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
/* Drop into a sigtimewait-based timeout. Waiting for the
* pid to exit. */
- until = now(CLOCK_MONOTONIC) + timeout;
+ until = usec_add(now(CLOCK_MONOTONIC), timeout);
for (;;) {
usec_t n;
siginfo_t status = {};
diff --git a/src/basic/ratelimit.c b/src/basic/ratelimit.c
index bae2ec3ffc..005bf31dc7 100644
--- a/src/basic/ratelimit.c
+++ b/src/basic/ratelimit.c
@@ -19,7 +19,7 @@ bool ratelimit_below(RateLimit *r) {
ts = now(CLOCK_MONOTONIC);
if (r->begin <= 0 ||
- ts - r->begin > r->interval) {
+ usec_sub_unsigned(ts, r->begin) > r->interval) {
r->begin = ts;
/* Reset counter */
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index fc63e134c3..3617a04d3b 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -440,11 +440,11 @@ int acquire_terminal(
assert(ts != USEC_INFINITY);
- n = now(CLOCK_MONOTONIC);
- if (ts + timeout < n)
+ n = usec_sub_unsigned(now(CLOCK_MONOTONIC), ts);
+ if (n >= timeout)
return -ETIMEDOUT;
- r = fd_wait_for_event(notify, POLLIN, ts + timeout - n);
+ r = fd_wait_for_event(notify, POLLIN, usec_sub_unsigned(timeout, n));
if (r < 0)
return r;
if (r == 0)
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index cbae9897a5..29817224ad 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -953,7 +953,7 @@ static int run(int argc, char *argv[]) {
t = now(CLOCK_MONOTONIC);
- if (t >= last_refresh + arg_delay || immediate_refresh) {
+ if (t >= usec_add(last_refresh, arg_delay) || immediate_refresh) {
r = refresh(root, a, b, iteration++);
if (r < 0)
@@ -976,9 +976,9 @@ static int run(int argc, char *argv[]) {
fflush(stdout);
if (arg_batch)
- (void) usleep(last_refresh + arg_delay - t);
+ (void) usleep(usec_add(usec_sub_unsigned(last_refresh, t), arg_delay));
else {
- r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL);
+ r = read_one_char(stdin, &key, usec_add(usec_sub_unsigned(last_refresh, t), arg_delay), NULL);
if (r == -ETIMEDOUT)
continue;
if (r < 0)
@@ -1053,10 +1053,7 @@ static int run(int argc, char *argv[]) {
break;
case '+':
- if (arg_delay < USEC_PER_SEC)
- arg_delay += USEC_PER_MSEC*250;
- else
- arg_delay += USEC_PER_SEC;
+ arg_delay = usec_add(arg_delay, arg_delay < USEC_PER_SEC ? USEC_PER_MSEC * 250 : USEC_PER_SEC);
fprintf(stdout, "\nIncreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0));
fflush(stdout);
@@ -1066,10 +1063,8 @@ static int run(int argc, char *argv[]) {
case '-':
if (arg_delay <= USEC_PER_MSEC*500)
arg_delay = USEC_PER_MSEC*250;
- else if (arg_delay < USEC_PER_MSEC*1250)
- arg_delay -= USEC_PER_MSEC*250;
else
- arg_delay -= USEC_PER_SEC;
+ arg_delay = usec_sub_unsigned(arg_delay, arg_delay < USEC_PER_MSEC * 1250 ? USEC_PER_MSEC * 250 : USEC_PER_SEC);
fprintf(stdout, "\nDecreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0));
fflush(stdout);
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
index 65e328389d..00de920d09 100644
--- a/src/cryptsetup/cryptsetup.c
+++ b/src/cryptsetup/cryptsetup.c
@@ -1503,10 +1503,9 @@ static int run(int argc, char *argv[]) {
flags = determine_flags();
- if (arg_timeout == USEC_INFINITY)
+ until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
+ if (until == USEC_INFINITY)
until = 0;
- else
- until = now(CLOCK_MONOTONIC) + arg_timeout;
arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8));
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 5cad374083..4234ab2e61 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -144,7 +144,7 @@ static int cache_space_refresh(Server *s, JournalStorage *storage) {
ts = now(CLOCK_MONOTONIC);
- if (space->timestamp != 0 && space->timestamp + RECHECK_SPACE_USEC > ts)
+ if (space->timestamp != 0 && usec_add(space->timestamp, RECHECK_SPACE_USEC) > ts)
return 0;
r = determine_path_usage(s, storage->path, &vfs_used, &vfs_avail);
@@ -1206,7 +1206,7 @@ finish:
server_driver_message(s, 0, NULL,
LOG_MESSAGE("Time spent on flushing to %s is %s for %u entries.",
s->system_storage.path,
- format_timespan(ts, sizeof(ts), now(CLOCK_MONOTONIC) - start, 0),
+ format_timespan(ts, sizeof(ts), usec_sub_unsigned(now(CLOCK_MONOTONIC), start), 0),
n),
NULL);
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index d593002712..9d1ef93716 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -2205,7 +2205,9 @@ _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destinat
static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
assert(bus);
- if (usec == (uint64_t) -1)
+ assert_cc(sizeof(usec_t) == sizeof(uint64_t));
+
+ if (usec == USEC_INFINITY)
return 0;
/* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
@@ -2215,7 +2217,7 @@ static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
if (IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING))
return usec;
else
- return now(CLOCK_MONOTONIC) + usec;
+ return usec_add(now(CLOCK_MONOTONIC), usec);
}
static int timeout_compare(const void *a, const void *b) {
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
index 35c4cb67eb..17ac659ec5 100644
--- a/src/libsystemd/sd-event/sd-event.c
+++ b/src/libsystemd/sd-event/sd-event.c
@@ -2594,10 +2594,11 @@ _public_ int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec
if (r < 0)
return r;
- if (usec >= USEC_INFINITY - t)
+ usec = usec_add(t, usec);
+ if (usec == USEC_INFINITY)
return -EOVERFLOW;
- return sd_event_source_set_time(s, t + usec);
+ return sd_event_source_set_time(s, usec);
}
_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c
index ff26f5d7e7..aabbfd90e5 100644
--- a/src/libsystemd/sd-journal/sd-journal.c
+++ b/src/libsystemd/sd-journal/sd-journal.c
@@ -2725,10 +2725,7 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
return r;
if (t != (uint64_t) -1) {
- usec_t n;
-
- n = now(CLOCK_MONOTONIC);
- t = t > n ? t - n : 0;
+ t = usec_sub_unsigned(t, now(CLOCK_MONOTONIC));
if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
timeout_usec = t;
diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c
index f754d08ef4..4ceb64cb23 100644
--- a/src/libsystemd/sd-netlink/sd-netlink.c
+++ b/src/libsystemd/sd-netlink/sd-netlink.c
@@ -518,7 +518,7 @@ static usec_t calc_elapse(uint64_t usec) {
if (usec == 0)
usec = RTNL_DEFAULT_TIMEOUT;
- return now(CLOCK_MONOTONIC) + usec;
+ return usec_add(now(CLOCK_MONOTONIC), usec);
}
static int rtnl_poll(sd_netlink *rtnl, bool need_more, uint64_t timeout_usec) {
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
index b9b2524822..d32ff8aa3b 100644
--- a/src/login/logind-dbus.c
+++ b/src/login/logind-dbus.c
@@ -1637,7 +1637,7 @@ static int execute_shutdown_or_sleep(
m->action_what = w;
/* Make sure the lid switch is ignored for a while */
- manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
+ manager_set_lid_switch_ignore(m, usec_add(now(CLOCK_MONOTONIC), m->holdoff_timeout_usec));
return 0;
diff --git a/src/network/networkd-ndisc.c b/src/network/networkd-ndisc.c
index bb62ddd93f..035e80dab0 100644
--- a/src/network/networkd-ndisc.c
+++ b/src/network/networkd-ndisc.c
@@ -535,7 +535,7 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
route->pref = preference;
route->gw_family = AF_INET6;
route->gw = gateway;
- route->lifetime = time_now + lifetime * USEC_PER_SEC;
+ route->lifetime = usec_add(time_now, lifetime * USEC_PER_SEC);
route->mtu = mtu;
r = ndisc_route_configure(route, link, rt);
@@ -559,7 +559,7 @@ static int ndisc_router_process_default(Link *link, sd_ndisc_router *rt) {
route_gw->protocol = RTPROT_RA;
if (!route_gw->pref_set)
route->pref = preference;
- route_gw->lifetime = time_now + lifetime * USEC_PER_SEC;
+ route_gw->lifetime = usec_add(time_now, lifetime * USEC_PER_SEC);
if (route_gw->mtu == 0)
route_gw->mtu = mtu;
@@ -818,7 +818,7 @@ static int ndisc_router_process_onlink_prefix(Link *link, sd_ndisc_router *rt) {
route->protocol = RTPROT_RA;
route->flags = RTM_F_PREFIX;
route->dst_prefixlen = prefixlen;
- route->lifetime = time_now + lifetime * USEC_PER_SEC;
+ route->lifetime = usec_add(time_now, lifetime * USEC_PER_SEC);
r = sd_ndisc_router_prefix_get_address(rt, &route->dst.in6);
if (r < 0)
@@ -906,7 +906,7 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
route->gw_family = AF_INET6;
route->dst = dst;
route->dst_prefixlen = prefixlen;
- route->lifetime = time_now + lifetime * USEC_PER_SEC;
+ route->lifetime = usec_add(time_now, lifetime * USEC_PER_SEC);
r = ndisc_route_configure(route, link, rt);
if (r < 0)
diff --git a/src/network/wait-online/manager.c b/src/network/wait-online/manager.c
index f6c8cf909f..9710742123 100644
--- a/src/network/wait-online/manager.c
+++ b/src/network/wait-online/manager.c
@@ -325,7 +325,7 @@ int manager_new(Manager **ret, Hashmap *interfaces, char **ignore,
if (timeout > 0) {
usec_t usec;
- usec = now(clock_boottime_or_monotonic()) + timeout;
+ usec = usec_add(now(clock_boottime_or_monotonic()), timeout);
r = sd_event_add_time(m->event, NULL, clock_boottime_or_monotonic(), usec, 0, NULL, INT_TO_PTR(-ETIMEDOUT));
if (r < 0)