diff mbox series

[v1,4/5] pb-discover: Fix signal handler

Message ID c79ad8475f0d30c81f45660db98dea237e187e26.1587958237.git.geoff@infradead.org
State New
Headers show
Series [v1,1/5] ncurses/nc-menu: Remove stray declaration | expand

Commit Message

Geoff Levand April 27, 2020, 3:38 a.m. UTC
Variables shared between signal handlers and the main program
need to be of type 'volatile sig_atomic_t'.  Also, after a
signal is serviced it should be re-enabled, so add a call to
signal() in the handler.

Signed-off-by: Geoff Levand <geoff@infradead.org>
---
 discover/pb-discover.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Jeremy Kerr May 3, 2020, 6 a.m. UTC | #1
Hi Geoff,

> -static int running;
> +static volatile sig_atomic_t running;

OK, looks good.
 
> -static void sigint_handler(int __attribute__((unused)) signum)
> +static void sigint_handler(int signum)
>  {
>  	running = 0;
> +	signal(signum, sigint_handler);

But this shouldn't be needed; invoking the signal shouldn't change the
handler - at least on Linux. Do you have a specific OS implementation
that requires the signal to be re-registered?

Cheers,


Jeremy
Geoff Levand May 3, 2020, 6:16 p.m. UTC | #2
Hi Jeremy,

On 5/2/20 11:00 PM, Jeremy Kerr wrote:
>> -static void sigint_handler(int __attribute__((unused)) signum)
>> +static void sigint_handler(int signum)
>>  {
>>  	running = 0;
>> +	signal(signum, sigint_handler);
> 
> But this shouldn't be needed; invoking the signal shouldn't change the
> handler - at least on Linux. Do you have a specific OS implementation
> that requires the signal to be re-registered?

I've never experienced needing to do this, but I though to add it in
to be more compatible.  I don't think it matters that much to have it
or not.  Can you think of some problem having it could cause?

-Geoff
diff mbox series

Patch

diff --git a/discover/pb-discover.c b/discover/pb-discover.c
index e2b36dd..f67eea5 100644
--- a/discover/pb-discover.c
+++ b/discover/pb-discover.c
@@ -113,11 +113,12 @@  static int opts_parse(struct opts *opts, int argc, char *argv[])
 	return optind != argc;
 }
 
-static int running;
+static volatile sig_atomic_t running;
 
-static void sigint_handler(int __attribute__((unused)) signum)
+static void sigint_handler(int signum)
 {
 	running = 0;
+	signal(signum, sigint_handler);
 }
 
 int main(int argc, char *argv[])