]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: use usec_add() and usec_sub_unsigned()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 3 Mar 2021 03:56:52 +0000 (12:56 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Mar 2021 22:10:08 +0000 (07:10 +0900)
14 files changed:
src/ask-password/ask-password.c
src/basic/process-util.c
src/basic/ratelimit.c
src/basic/terminal-util.c
src/cgtop/cgtop.c
src/cryptsetup/cryptsetup.c
src/journal/journald-server.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-event/sd-event.c
src/libsystemd/sd-journal/sd-journal.c
src/libsystemd/sd-netlink/sd-netlink.c
src/login/logind-dbus.c
src/network/networkd-ndisc.c
src/network/wait-online/manager.c

index 599ffcdf09caee3fde9ecb57ab4335459587788c..6b89f57e1b71a8ebc9b5a4d71b62a85a71f8e518 100644 (file)
@@ -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;
 
index 304b03960a527469985e9b302e40346e1ecebfbd..4eb524d211279095f8eaefdf787e8647a27e7bd0 100644 (file)
@@ -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 = {};
index bae2ec3ffc5f9f6fc6f75839f8466638733ba492..005bf31dc7ceeb14821e3ec8d5c67df69486c406 100644 (file)
@@ -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 */
index fc63e134c38daee5e224c3db8b39ce7f1de59836..3617a04d3b7409a274786a1070371274ed4105c9 100644 (file)
@@ -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)
index cbae9897a507c0fc532d8285e4956bf24af8c189..29817224ad8e6cd5cbc426caf54490303110d8b0 100644 (file)
@@ -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);
index 65e328389dcfb4ef20bf6bb5daabf844f45cf519..00de920d091f9c300555e43d7b1f8f58f72f9726 100644 (file)
@@ -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));
 
index 5cad3740838fd13298f608cd650b5bfdc2d101c4..4234ab2e611aba6930f93aadcb2e5070575287c6 100644 (file)
@@ -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);
 
index d593002712998310d023c8b318d2a26830e2c11f..9d1ef937167748ee29f243521a901a1db5f4f360 100644 (file)
@@ -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) {
index 35c4cb67eb0e1e7ba729b6001a30fae722c3c78f..17ac659ec5637f0a97484308dd8f230efcdb6890 100644 (file)
@@ -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) {
index ff26f5d7e7929bad87ff19209253885f54c4fcb4..aabbfd90e576521387e9147d3c31167399f6925d 100644 (file)
@@ -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;
index f754d08ef44057706827e87484191cc15ceea382..4ceb64cb23119a3f193dfc80961a9d07279271bd 100644 (file)
@@ -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) {
index b9b2524822acabad36375fa25858b3edc9b46bd0..d32ff8aa3bd40b644bde684e2c48bfadaf02cfcd 100644 (file)
@@ -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;
 
index bb62ddd93fde2e84ab34ccf1437e6a12f90a5eb2..035e80dab0e7ed9dee9a1118f5704c99da05a25a 100644 (file)
@@ -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)
index f6c8cf909fa3633e2a67729ec47bfab6551ca899..97107421239a1eac98583f478efda72b9d879798 100644 (file)
@@ -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)