with-authinfo-kludge: Add machinery to restore signals in child processes.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 20 Sep 2017 19:30:04 +0000 (20:30 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 20 Sep 2017 19:39:37 +0000 (20:39 +0100)
In particular, this fixes a minor bug which restores `SIGCHLD' to its
default disposition in children of `with-authinfo-kludge', even if it
was ignored initially (which it probably shouldn't have been).

with-authinfo-kludge

index 3cdefe3..4317d8d 100755 (executable)
@@ -173,10 +173,20 @@ sub write_to_file ($$) {
   rename $new, $file or sysfail "failed to rename `$new' to `$file': $!";
 }
 
+my %OLDSIGS;
+sub set_sighandler ($$) {
+  my ($sig, $handler) = @_;
+  unless (exists $OLDSIGS{$sig}) { $OLDSIGS{$sig} = $SIG{$sig}; }
+  $SIG{$sig} = $handler;
+}
+
 my $INKIDP = 0;
 sub myfork () {
   my $kid = fork;
-  if (defined $kid && !$kid) { $INKIDP = 1; }
+  if (defined $kid && !$kid) {
+    $INKIDP = 1;
+    for my $sig (keys %OLDSIGS) { $SIG{$sig} = $OLDSIGS{$sig}; }
+  }
   return $kid;
 }
 
@@ -747,7 +757,7 @@ sub wait_for_ssh () {
   else { inform "  all tunnels started ok"; }
 }
 
-$SIG{"CHLD"} = sub {
+set_sighandler "CHLD", sub {
   KID: for (;;) {
     defined (my $kid = waitpid -1, WNOHANG)
       or sysfail "failed to reap child: $!";