]> git.ipfire.org Git - thirdparty/util-linux.git/commit
lib/timer.c: prevent pathological race condition
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>
Wed, 26 Apr 2017 11:03:10 +0000 (13:03 +0200)
committerRasmus Villemoes <rasmus.villemoes@prevas.dk>
Wed, 26 Apr 2017 11:35:26 +0000 (13:35 +0200)
commite2cd1072b44fe4fe269ee0e9ee51e05e946a011d
tree5b696b708dc1f8beb4b491310d99e80e9a028899
parentc6a9147fd8b21bdbdaa0ea363d3db9a819d2b5f7
lib/timer.c: prevent pathological race condition

flock(1) uses the timer facility to interrupt a blocking flock(2)
call. However, in a pathological case (or with a sufficiently short
timeout), the timer may fire and the signal be delivered after the
timer is set up, but before we get around to doing the flock(2)
call. In that case, we'd block forever. Checking timeout_expired right
before calling flock(2) does not eliminate that race, so the only
option is to make the timer fire repeatedly. Having the timer fire
after we've returned from flock(2) is not a problem, since we only
check timeout_expired in case of EINTR (also, this firing after return
could also happen with the current code).

There is currently one other user of setup_timer (misc-utils/uuidd.c),
but in that case the signal handler simply exits. Future users of
setup_timer obviously need to ensure that they can tolerate multiple
signal deliveries.

Choosing 1% of the initial timeout as the repeating interval is
somewhat arbitrary. However, I put a lower bound of 0.01s, since
setting the interval much smaller than this may end up effectively
live-locking the process, handling a never-ending stream of signals.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
lib/timer.c