Skip to content

Commit

Permalink
Fix signal handler behavior
Browse files Browse the repository at this point in the history
I mean, I understand why the original was ok in some sense, but was
never correct.
  • Loading branch information
tokenrove committed Nov 23, 2023
1 parent aa8f77e commit 0609da5
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions helpers/echo-signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exec ${CC:-cc} ${CFLAGS:--Wall -Wextra -g} $0 -o $1
#include <string.h>
#include <unistd.h>

static char *awaited;
static sig_atomic_t got;

static int int_of_signal_name(char *name)
{
Expand All @@ -23,20 +23,16 @@ static int int_of_signal_name(char *name)
abort();
}

static void echo(int _)
{
(void)_;
puts(awaited); /* never do stuff like this in signal handlers. */
}
static void handler(int n) { got = n; }

int main(int argc, char **argv)
{
struct sigaction action = { .sa_handler = echo };
struct sigaction action = { .sa_handler = handler };
if (2 != argc) abort();
int sig = int_of_signal_name(argv[1]);
awaited = argv[1];
sigaction(sig, &action, NULL);
puts("ready");
pause();
do { pause(); } while (sig != got);
puts(argv[1]);
return 0;
}

0 comments on commit 0609da5

Please sign in to comment.