From 3a43da2832dc5360a638d043f469a6dcbe025582 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Tue, 29 Jul 2014 12:23:31 +0200 Subject: time-util: add and use USEC/NSEC_INFINIY --- src/shared/logs-show.c | 2 +- src/shared/spawn-polkit-agent.c | 2 +- src/shared/time-util.c | 28 ++++++++++++++-------------- src/shared/time-util.h | 27 +++++++++++++++------------ src/shared/util.c | 26 +++++++++++++------------- src/shared/watchdog.c | 6 +++--- 6 files changed, 47 insertions(+), 44 deletions(-) (limited to 'src/shared') diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 8edf4087c6..5538dd3b9e 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -1032,7 +1032,7 @@ static int show_journal(FILE *f, if (!(flags & OUTPUT_FOLLOW)) break; - r = sd_journal_wait(j, (usec_t) -1); + r = sd_journal_wait(j, USEC_INFINITY); if (r < 0) goto finish; diff --git a/src/shared/spawn-polkit-agent.c b/src/shared/spawn-polkit-agent.c index fccf1e9173..29b01db19a 100644 --- a/src/shared/spawn-polkit-agent.c +++ b/src/shared/spawn-polkit-agent.c @@ -67,7 +67,7 @@ int polkit_agent_open(void) { log_error("Failed to fork TTY ask password agent: %s", strerror(-r)); else /* Wait until the agent closes the fd */ - fd_wait_for_event(pipe_fd[0], POLLHUP, (usec_t) -1); + fd_wait_for_event(pipe_fd[0], POLLHUP, USEC_INFINITY); safe_close(pipe_fd[0]); diff --git a/src/shared/time-util.c b/src/shared/time-util.c index 76ab143f34..2dc01e6ed3 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -49,8 +49,8 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { int64_t delta; assert(ts); - if (u == (usec_t) -1) { - ts->realtime = ts->monotonic = (usec_t) -1; + if (u == USEC_INFINITY) { + ts->realtime = ts->monotonic = USEC_INFINITY; return ts; } @@ -76,8 +76,8 @@ dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) { int64_t delta; assert(ts); - if (u == (usec_t) -1) { - ts->realtime = ts->monotonic = (usec_t) -1; + if (u == USEC_INFINITY) { + ts->realtime = ts->monotonic = USEC_INFINITY; return ts; } @@ -98,10 +98,10 @@ usec_t timespec_load(const struct timespec *ts) { if (ts->tv_sec == (time_t) -1 && ts->tv_nsec == (long) -1) - return (usec_t) -1; + return USEC_INFINITY; if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC) - return (usec_t) -1; + return USEC_INFINITY; return (usec_t) ts->tv_sec * USEC_PER_SEC + @@ -111,7 +111,7 @@ usec_t timespec_load(const struct timespec *ts) { struct timespec *timespec_store(struct timespec *ts, usec_t u) { assert(ts); - if (u == (usec_t) -1) { + if (u == USEC_INFINITY) { ts->tv_sec = (time_t) -1; ts->tv_nsec = (long) -1; return ts; @@ -128,10 +128,10 @@ usec_t timeval_load(const struct timeval *tv) { if (tv->tv_sec == (time_t) -1 && tv->tv_usec == (suseconds_t) -1) - return (usec_t) -1; + return USEC_INFINITY; if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC) - return (usec_t) -1; + return USEC_INFINITY; return (usec_t) tv->tv_sec * USEC_PER_SEC + @@ -141,7 +141,7 @@ usec_t timeval_load(const struct timeval *tv) { struct timeval *timeval_store(struct timeval *tv, usec_t u) { assert(tv); - if (u == (usec_t) -1) { + if (u == USEC_INFINITY) { tv->tv_sec = (time_t) -1; tv->tv_usec = (suseconds_t) -1; } else { @@ -159,7 +159,7 @@ char *format_timestamp(char *buf, size_t l, usec_t t) { assert(buf); assert(l > 0); - if (t <= 0 || t == (usec_t) -1) + if (t <= 0 || t == USEC_INFINITY) return NULL; sec = (time_t) (t / USEC_PER_SEC); @@ -177,7 +177,7 @@ char *format_timestamp_us(char *buf, size_t l, usec_t t) { assert(buf); assert(l > 0); - if (t <= 0 || t == (usec_t) -1) + if (t <= 0 || t == USEC_INFINITY) return NULL; sec = (time_t) (t / USEC_PER_SEC); @@ -198,7 +198,7 @@ char *format_timestamp_relative(char *buf, size_t l, usec_t t) { n = now(CLOCK_REALTIME); - if (t <= 0 || (t == (usec_t) -1)) + if (t <= 0 || (t == USEC_INFINITY)) return NULL; if (n > t) { @@ -279,7 +279,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { assert(buf); assert(l > 0); - if (t == (usec_t) -1) + if (t == USEC_INFINITY) return NULL; if (t <= 0) { diff --git a/src/shared/time-util.h b/src/shared/time-util.h index 69a48c686b..8ba1cfee8e 100644 --- a/src/shared/time-util.h +++ b/src/shared/time-util.h @@ -37,25 +37,28 @@ typedef struct dual_timestamp { usec_t monotonic; } dual_timestamp; -#define MSEC_PER_SEC ((usec_t) 1000ULL) +#define USEC_INFINITY ((usec_t) -1) +#define NSEC_INFINITY ((nsec_t) -1) + +#define MSEC_PER_SEC 1000ULL #define USEC_PER_SEC ((usec_t) 1000000ULL) #define USEC_PER_MSEC ((usec_t) 1000ULL) -#define NSEC_PER_SEC ((usec_t) 1000000000ULL) -#define NSEC_PER_MSEC ((usec_t) 1000000ULL) -#define NSEC_PER_USEC ((usec_t) 1000ULL) +#define NSEC_PER_SEC ((nsec_t) 1000000000ULL) +#define NSEC_PER_MSEC ((nsec_t) 1000000ULL) +#define NSEC_PER_USEC ((nsec_t) 1000ULL) #define USEC_PER_MINUTE ((usec_t) (60ULL*USEC_PER_SEC)) -#define NSEC_PER_MINUTE ((usec_t) (60ULL*NSEC_PER_SEC)) +#define NSEC_PER_MINUTE ((nsec_t) (60ULL*NSEC_PER_SEC)) #define USEC_PER_HOUR ((usec_t) (60ULL*USEC_PER_MINUTE)) -#define NSEC_PER_HOUR ((usec_t) (60ULL*NSEC_PER_MINUTE)) +#define NSEC_PER_HOUR ((nsec_t) (60ULL*NSEC_PER_MINUTE)) #define USEC_PER_DAY ((usec_t) (24ULL*USEC_PER_HOUR)) -#define NSEC_PER_DAY ((usec_t) (24ULL*NSEC_PER_HOUR)) +#define NSEC_PER_DAY ((nsec_t) (24ULL*NSEC_PER_HOUR)) #define USEC_PER_WEEK ((usec_t) (7ULL*USEC_PER_DAY)) -#define NSEC_PER_WEEK ((usec_t) (7ULL*NSEC_PER_DAY)) +#define NSEC_PER_WEEK ((nsec_t) (7ULL*NSEC_PER_DAY)) #define USEC_PER_MONTH ((usec_t) (2629800ULL*USEC_PER_SEC)) -#define NSEC_PER_MONTH ((usec_t) (2629800ULL*NSEC_PER_SEC)) +#define NSEC_PER_MONTH ((nsec_t) (2629800ULL*NSEC_PER_SEC)) #define USEC_PER_YEAR ((usec_t) (31557600ULL*USEC_PER_SEC)) -#define NSEC_PER_YEAR ((usec_t) (31557600ULL*NSEC_PER_SEC)) +#define NSEC_PER_YEAR ((nsec_t) (31557600ULL*NSEC_PER_SEC)) #define FORMAT_TIMESTAMP_MAX ((4*4+1)+11+9+4+1) /* weekdays can be unicode */ #define FORMAT_TIMESTAMP_WIDTH 28 /* when outputting, assume this width */ @@ -71,8 +74,8 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u); dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u); static inline bool dual_timestamp_is_set(dual_timestamp *ts) { - return ((ts->realtime > 0 && ts->realtime != (usec_t) -1) || - (ts->monotonic > 0 && ts->monotonic != (usec_t) -1)); + return ((ts->realtime > 0 && ts->realtime != USEC_INFINITY) || + (ts->monotonic > 0 && ts->monotonic != USEC_INFINITY)); } usec_t timespec_load(const struct timespec *ts) _pure_; diff --git a/src/shared/util.c b/src/shared/util.c index 49c17eff85..b1689e651f 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -1581,7 +1581,7 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) { if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) { size_t k; - if (t != (usec_t) -1) { + if (t != USEC_INFINITY) { if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) { tcsetattr(fileno(f), TCSADRAIN, &old_termios); return -ETIMEDOUT; @@ -1603,7 +1603,7 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) { } } - if (t != (usec_t) -1) { + if (t != USEC_INFINITY) { if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) return -ETIMEDOUT; } @@ -1648,7 +1648,7 @@ int ask_char(char *ret, const char *replies, const char *text, ...) { fflush(stdout); - r = read_one_char(stdin, &c, (usec_t) -1, &need_nl); + r = read_one_char(stdin, &c, USEC_INFINITY, &need_nl); if (r < 0) { if (r == -EBADMSG) { @@ -1898,11 +1898,11 @@ int acquire_terminal( * on the same tty as an untrusted user this should not be a * problem. (Which he probably should not do anyway.) */ - if (timeout != (usec_t) -1) + if (timeout != USEC_INFINITY) ts = now(CLOCK_MONOTONIC); if (!fail && !force) { - notify = inotify_init1(IN_CLOEXEC | (timeout != (usec_t) -1 ? IN_NONBLOCK : 0)); + notify = inotify_init1(IN_CLOEXEC | (timeout != USEC_INFINITY ? IN_NONBLOCK : 0)); if (notify < 0) { r = -errno; goto fail; @@ -1966,7 +1966,7 @@ int acquire_terminal( ssize_t l; struct inotify_event *e; - if (timeout != (usec_t) -1) { + if (timeout != USEC_INFINITY) { usec_t n; n = now(CLOCK_MONOTONIC); @@ -2148,7 +2148,7 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { * and expect that any error/EOF is reported * via read() */ - fd_wait_for_event(fd, POLLIN, (usec_t) -1); + fd_wait_for_event(fd, POLLIN, USEC_INFINITY); continue; } @@ -2183,7 +2183,7 @@ ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { * and expect that any error/EOF is reported * via write() */ - fd_wait_for_event(fd, POLLOUT, (usec_t) -1); + fd_wait_for_event(fd, POLLOUT, USEC_INFINITY); continue; } @@ -3428,7 +3428,7 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi return -errno; } - if (stamp != (usec_t) -1) { + if (stamp != USEC_INFINITY) { struct timespec ts[2]; timespec_store(&ts[0], stamp); @@ -3443,7 +3443,7 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi } int touch(const char *path) { - return touch_file(path, false, (usec_t) -1, (uid_t) -1, (gid_t) -1, 0); + return touch_file(path, false, USEC_INFINITY, (uid_t) -1, (gid_t) -1, 0); } char *unquote(const char *s, const char* quotes) { @@ -3904,7 +3904,7 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv * timout. We simply rely on SIGALRM as default action * terminating the process, and turn on alarm(). */ - if (timeout != (usec_t) -1) + if (timeout != USEC_INFINITY) alarm((timeout + USEC_PER_SEC - 1) / USEC_PER_SEC); while (!hashmap_isempty(pids)) { @@ -4074,7 +4074,7 @@ int fd_wait_for_event(int fd, int event, usec_t t) { struct timespec ts; int r; - r = ppoll(&pollfd, 1, t == (usec_t) -1 ? NULL : timespec_store(&ts, t), NULL); + r = ppoll(&pollfd, 1, t == USEC_INFINITY ? NULL : timespec_store(&ts, t), NULL); if (r < 0) return -errno; @@ -5273,7 +5273,7 @@ int make_console_stdio(void) { /* Make /dev/console the controlling terminal and stdin/stdout/stderr */ - fd = acquire_terminal("/dev/console", false, true, true, (usec_t) -1); + fd = acquire_terminal("/dev/console", false, true, true, USEC_INFINITY); if (fd < 0) { log_error("Failed to acquire terminal: %s", strerror(-fd)); return fd; diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index ba9ad9be97..7d188d98e8 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -29,7 +29,7 @@ #include "log.h" static int watchdog_fd = -1; -static usec_t watchdog_timeout = (usec_t) -1; +static usec_t watchdog_timeout = USEC_INFINITY; static int update_timeout(void) { int r; @@ -37,7 +37,7 @@ static int update_timeout(void) { if (watchdog_fd < 0) return 0; - if (watchdog_timeout == (usec_t) -1) + if (watchdog_timeout == USEC_INFINITY) return 0; else if (watchdog_timeout == 0) { int flags; @@ -104,7 +104,7 @@ int watchdog_set_timeout(usec_t *usec) { /* If we didn't open the watchdog yet and didn't get any * explicit timeout value set, don't do anything */ - if (watchdog_fd < 0 && watchdog_timeout == (usec_t) -1) + if (watchdog_fd < 0 && watchdog_timeout == USEC_INFINITY) return 0; if (watchdog_fd < 0) -- cgit v1.2.3-54-g00ecf