diff options
-rw-r--r-- | configure.ac | 14 | ||||
-rw-r--r-- | include/Makemodule.am | 1 | ||||
-rw-r--r-- | include/monotonic.h | 4 | ||||
-rw-r--r-- | include/timer.h | 31 | ||||
-rw-r--r-- | lib/monotonic.c | 34 | ||||
-rw-r--r-- | sys-utils/Makemodule.am | 2 | ||||
-rw-r--r-- | sys-utils/flock.c | 17 |
7 files changed, 59 insertions, 44 deletions
diff --git a/configure.ac b/configure.ac index 5d2b68503..9dec9d897 100644 --- a/configure.ac +++ b/configure.ac @@ -371,10 +371,16 @@ AC_CHECK_FUNCS([ioperm iopl], [have_io=yes]) AC_CHECK_FUNCS([futimens], [have_futimens=yes]) AC_CHECK_FUNCS([inotify_init1], [have_inotify_init1=yes]) -dnl Old glibc requires -lrt -AC_CHECK_FUNCS(clock_gettime, [], [ - AC_CHECK_LIB(rt, clock_gettime, [ - AC_DEFINE(HAVE_CLOCK_GETTIME, 1) +dnl lib/mononotic.c may require -lrt +AC_CHECK_FUNCS([clock_gettime], + [AC_CHECK_FUNCS([timer_create], [], [ + AC_CHECK_LIB([rt], [timer_create], [ + AC_DEFINE([HAVE_CLOCK_GETTIME], [1]) + CLOCKGETTIME_LIBS="-lrt" + ]) + ])], + [AC_CHECK_LIB([rt], [clock_gettime], [ + AC_DEFINE([HAVE_CLOCK_GETTIME], [1]) CLOCKGETTIME_LIBS="-lrt" ]) ]) diff --git a/include/Makemodule.am b/include/Makemodule.am index c4a52e4cf..8d7c881d5 100644 --- a/include/Makemodule.am +++ b/include/Makemodule.am @@ -44,7 +44,6 @@ dist_noinst_HEADERS += \ include/swapprober.h \ include/swapheader.h \ include/sysfs.h \ - include/timer.h \ include/timeutils.h \ include/ttyutils.h \ include/widechar.h \ diff --git a/include/monotonic.h b/include/monotonic.h index f3b03d3d0..d5ff7c8e5 100644 --- a/include/monotonic.h +++ b/include/monotonic.h @@ -8,4 +8,8 @@ extern int get_boot_time(struct timeval *boot_time); extern int gettime_monotonic(struct timeval *tv); +extern int setup_timer(timer_t * t_id, struct itimerval *timeout, + void (*timeout_handler)(void)); +extern void cancel_timer(timer_t * t_id); + #endif /* UTIL_LINUX_BOOTTIME_H */ diff --git a/include/timer.h b/include/timer.h deleted file mode 100644 index 79ef64919..000000000 --- a/include/timer.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef UTIL_LINUX_TIMER_H -#define UTIL_LINUX_TIMER_H - -#include <signal.h> -#include <sys/time.h> - -static inline int setup_timer( - struct itimerval *timer, - struct itimerval *old_timer, - struct sigaction *old_sa, - void (*timeout_handler)(int)) -{ - struct sigaction sa; - - memset(&sa, 0, sizeof sa); - sa.sa_handler = timeout_handler; - sa.sa_flags = SA_RESETHAND; - sigaction(SIGALRM, &sa, old_sa); - - return setitimer(ITIMER_REAL, timer, old_timer); -} - -static inline void cancel_timer( - struct itimerval *old_timer, - struct sigaction *old_sa) -{ - setitimer(ITIMER_REAL, old_timer, NULL); - sigaction(SIGALRM, old_sa, NULL); -} - -#endif diff --git a/lib/monotonic.c b/lib/monotonic.c index 3d4a4438e..a124debba 100644 --- a/lib/monotonic.c +++ b/lib/monotonic.c @@ -3,6 +3,7 @@ * -lrt on systems with old libc. */ #include <time.h> +#include <signal.h> #include <sys/sysinfo.h> #include <sys/time.h> @@ -66,3 +67,36 @@ int gettime_monotonic(struct timeval *tv) return gettimeofday(tv, NULL); #endif } + +int setup_timer(timer_t * t_id, struct itimerval *timeout, + void (*timeout_handler)(void)) +{ + struct sigaction sig_a; + static struct sigevent sig_e = { + .sigev_notify = SIGEV_SIGNAL, + .sigev_signo = SIGALRM + }; + struct itimerspec val = { + .it_value.tv_sec = timeout->it_value.tv_sec, + .it_value.tv_nsec = timeout->it_value.tv_usec * 1000, + .it_interval.tv_sec = 0, + .it_interval.tv_nsec = 0 + }; + + if (sigemptyset(&sig_a.sa_mask)) + return 1; + sig_a.sa_flags = SA_SIGINFO; + sig_a.sa_handler = timeout_handler; + if (sigaction(SIGALRM, &sig_a, 0)) + return 1; + if (timer_create(CLOCK_MONOTONIC, &sig_e, t_id)) + return 1; + if (timer_settime(*t_id, SA_SIGINFO, &val, NULL)) + return 1; + return 0; +} + +void cancel_timer(timer_t *t_id) +{ + timer_delete(*t_id); +} diff --git a/sys-utils/Makemodule.am b/sys-utils/Makemodule.am index b6b0f91ac..bd1b681e8 100644 --- a/sys-utils/Makemodule.am +++ b/sys-utils/Makemodule.am @@ -2,7 +2,7 @@ if BUILD_FLOCK usrbin_exec_PROGRAMS += flock dist_man_MANS += sys-utils/flock.1 flock_SOURCES = sys-utils/flock.c lib/monotonic.c -flock_LDADD = $(LDADD) libcommon.la $(CLOCKGETTIME_LIBS) +flock_LDADD = $(LDADD) libcommon.la -lrt endif if BUILD_IPCMK diff --git a/sys-utils/flock.c b/sys-utils/flock.c index 707c5992b..368ad1a9f 100644 --- a/sys-utils/flock.c +++ b/sys-utils/flock.c @@ -43,7 +43,6 @@ #include "nls.h" #include "strutils.h" #include "closestream.h" -#include "timer.h" #include "monotonic.h" static void __attribute__((__noreturn__)) usage(int ex) @@ -77,9 +76,12 @@ static void __attribute__((__noreturn__)) usage(int ex) static sig_atomic_t timeout_expired = 0; -static void timeout_handler(int sig __attribute__((__unused__))) +static void timeout_handler(int sig __attribute__((__unused__)), + siginfo_t *info, + void *context __attribute__((__unused__))) { - timeout_expired = 1; + if (info->si_code == SI_TIMER) + timeout_expired = 1; } static int open_file(const char *filename, int *flags) @@ -113,7 +115,8 @@ static int open_file(const char *filename, int *flags) int main(int argc, char *argv[]) { - struct itimerval timeout, old_timer; + static timer_t t_id; + struct itimerval timeout; int have_timeout = 0; int type = LOCK_EX; int block = 0; @@ -131,7 +134,6 @@ int main(int argc, char *argv[]) int conflict_exit_code = 1; char **cmd_argv = NULL, *sh_c_argv[4]; const char *filename = NULL; - struct sigaction old_sa; enum { OPT_VERBOSE = CHAR_MAX + 1 }; @@ -246,7 +248,8 @@ int main(int argc, char *argv[]) have_timeout = 0; block = LOCK_NB; } else - setup_timer(&timeout, &old_timer, &old_sa, timeout_handler); + if (setup_timer(&t_id, &timeout, &timeout_handler)) + err(EX_OSERR, _("cannot not setup timer")); } if (verbose) @@ -298,7 +301,7 @@ int main(int argc, char *argv[]) } if (have_timeout) - cancel_timer(&old_timer, &old_sa); + cancel_timer(&t_id); if (verbose) { struct timeval delta; |