From 941f67ed09155011f13bb75db6aed621f94e2374 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Fri, 12 Mar 2021 11:18:53 +0100 Subject: [PATCH] signals: add interruptible usleep Add small wrapper that temporarily enables signal handling during usleep() and return '0' when interrupted. --- lib/misc/lvm-signal.c | 13 +++++++++++++ lib/misc/lvm-signal.h | 3 +++ 2 files changed, 16 insertions(+) diff --git a/lib/misc/lvm-signal.c b/lib/misc/lvm-signal.c index b90a2db81..b1653a85c 100644 --- a/lib/misc/lvm-signal.c +++ b/lib/misc/lvm-signal.c @@ -153,3 +153,16 @@ void unblock_signals(void) _signals_blocked = 0; } + +/* usleep with enabled signal handler. + * Returns 1 when there was interruption */ +int interruptible_usleep(useconds_t usec) +{ + int r; + + sigint_allow(); + r = usleep(usec); + sigint_restore(); + + return (sigint_caught() || r) ? 1 : 0; +} diff --git a/lib/misc/lvm-signal.h b/lib/misc/lvm-signal.h index 2f7309f5d..a39f77eb2 100644 --- a/lib/misc/lvm-signal.h +++ b/lib/misc/lvm-signal.h @@ -16,6 +16,8 @@ #ifndef _LVM_SIGNAL_H #define _LVM_SIGNAL_H +#include + void remove_ctrl_c_handler(void); void install_ctrl_c_handler(void); int init_signals(int suppress_messages); @@ -28,4 +30,5 @@ void sigint_clear(void); void block_signals(uint32_t flags); void unblock_signals(void); +int interruptible_usleep(useconds_t usec); #endif