+sub setup_signals {
+ my ($cb, $arg) = @_; # optional
+
+ # we call exit() here instead of _exit() so DESTROY methods
+ # get called (e.g. File::Temp::Dir and PublicInbox::Msgmap)
+ $SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = sub {
+ my ($sig) = @_;
+ # https://www.tldp.org/LDP/abs/html/exitcodes.html
+ eval { $cb->($sig, $arg) } if $cb;
+ $sig = 'SIG'.$sig;
+ exit(128 + POSIX->$sig);
+ };
+}
+