Skip to content

Commit 9b9c039

Browse files
stepnemLeont
authored andcommitted
perlipc.pod: avoid race in "exec from signal handler" example
Instead of using SA_NODEFER, which makes the handler function itself interruptible by another SIGHUP, use a plain $SIG{HUP} handler and unblock SIGHUP using sigprocmask from the main program.
1 parent 6cc5fbb commit 9b9c039

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

pod/perlipc.pod

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,15 @@ info to show that it works; it should be replaced with the real code.
204204
my $script = File::Basename::basename($0);
205205
my $SELF = catfile($FindBin::Bin, $script);
206206

207-
# Use SA_NODEFER to prevent SIGHUP from being blocked in
208-
# the new process (due to signal mask preservation across exec).
209-
my $sigset = POSIX::SigSet->new();
210-
my $action = POSIX::SigAction->new("sigHUP_handler",
211-
$sigset,
212-
&POSIX::SA_NODEFER);
213-
POSIX::sigaction(&POSIX::SIGHUP, $action);
214-
215-
sub sigHUP_handler {
207+
$SIG{HUP} = sub {
216208
print "got SIGHUP\n";
217-
exec($SELF, @ARGV) || die "$0: couldn't restart: $!";
218-
}
209+
exec($SELF, @ARGV) || die "$0: couldn't restart: $!";
210+
};
211+
212+
# Make sure SIGHUP isn't blocked (as it normally would be, due to the
213+
# handler function calling exec rather than returning)
214+
POSIX::sigprocmask(&POSIX::SIG_UNBLOCK,
215+
POSIX::SigSet->new(&POSIX::SIGHUP));
219216

220217
code();
221218

0 commit comments

Comments
 (0)