From 92d1ceffe8da58eaa0910ed9bcc17faab7416a43 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 24 Sep 2021 14:36:18 -0700 Subject: [PATCH] macOS: add a timer_create() and timer_settimer() to fix the build. Note that these aren't equivalent to the CLOCK_MONOTONIC timer we have on Linux, but short of spinning up another thread ourselves I can't see how to implement that on macOS at present. --- lib/portability.c | 24 ++++++++++++++++++++++++ lib/portability.h | 10 ++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/portability.c b/lib/portability.c index 14de4222..ef9cc008 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -655,3 +655,27 @@ long long sendfile_len(int in, int out, long long bytes, long long *consumed) return total; } +#if __APPLE__ +// The absolute minimum POSIX timer implementation to build timeout(1). +// Note that although timeout(1) uses POSIX timers to get the monotonic clock, +// that doesn't seem to be an option on macOS (without using other libraries), +// so we just mangle that back into a regular setitimer(ITIMER_REAL) call. +int timer_create(clock_t c, struct sigevent *se, timer_t *t) +{ + if (se->sigev_notify != SIGEV_SIGNAL || se->sigev_signo != SIGALRM) + error_exit("unimplemented"); + *t = 1; + return 0; +} + +int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old) +{ + struct itimerval mangled; + + if (flags != 0 || old != 0) error_exit("unimplemented"); + memset(&mangled, 0, sizeof(mangled)); + mangled.it_value.tv_sec = new->it_value.tv_sec; + mangled.it_value.tv_usec = new->it_value.tv_nsec / 1000; + return setitimer(ITIMER_REAL, &mangled, NULL); +} +#endif diff --git a/lib/portability.h b/lib/portability.h index bb792f10..9fc0c9b8 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -371,3 +371,13 @@ int dev_makedev(int major, int minor); char *fs_type_name(struct statfs *statfs); int get_block_device_size(int fd, unsigned long long *size); + +#if __APPLE__ +// Apple doesn't have POSIX timers; this is "just enough" for timeout(1). +typedef int timer_t; +struct itimerspec { + struct timespec it_value; +}; +int timer_create(clock_t c, struct sigevent *se, timer_t *t); +int timer_settime(timer_t t, int flags, struct itimerspec *new, void *old); +#endif -- 2.39.2