From 378543e13f93c522b976efddd1610a19a6601b1c Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Mon, 9 Feb 2015 23:18:09 +0000 Subject: flock: improve timeout handling Signal ALRM raised by the timer, and the timer only, will be considered as a timeout criteria. Secondly time interval is made to use monotonic clock. Documentation of ITIMER_REAL is unclear whether that time is affected various sources of clock skew, or does it even tick when system is suspended. Reviewed-by: Karel Zak Signed-off-by: Sami Kerola --- sys-utils/flock.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'sys-utils/flock.c') 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; -- cgit v1.2.3-55-g7522