Skip to content
 

Check signal handling of a process

Here's a quick and dirty program to check what a given process (specified by its PID) is doing with regard to signal handling. It gets its information from the virtual file /proc/<PID>/status, reading the SigBlk:, SigIgn:, and SigCgt: values. Each one is a 64 bit mask indicating which signals the process is blocking, ignoring and handling (catching) respectively.

#!/usr/bin/perl
 
# use as: $0 <PID>
 
use warnings;
use strict;
use bignum;
use Config;
 
defined $Config{sig_name} or die "Cannot find signal names in Config";
my @sigs = map { "SIG$_" } split(/ /, $Config{sig_name});  
 
my $statfile = "/proc/$ARGV[0]/status";
 
open(S, "<", $statfile) or die "Cannot open status file $statfile";
 
while(<S>) {
  chomp;
  if (/^Sig(Blk|Ign|Cgt):\s+(\S+)/) {
    if (my @list = grep { oct("0x$2") & (1 << ($_ - 1)) } (1..64) ) {
      print "$1: " . join(", ", map { "$sigs[$_] ($_)" } @list) . "\n";
    }
  }
}
 
close(S);

Sample usage:

$ checksignals.pl 1747
Ign: SIGPIPE (13)
Cgt: SIGHUP (1), SIGINT (2), SIGQUIT (3), SIGTERM (15), SIGCHLD (17), SIGWINCH (28), SIGNUM32 (32), SIGNUM33 (33)
$ checksignals.pl $$
Blk: SIGCHLD (17)
Ign: SIGQUIT (3), SIGTERM (15), SIGTSTP (20), SIGTTIN (21), SIGTTOU (22)
Cgt: SIGHUP (1), SIGINT (2), SIGILL (4), SIGTRAP (5), SIGABRT (6), SIGBUS (7), SIGFPE (8), SIGUSR1 (10), SIGSEGV (11), SIGUSR2 (12), SIGPIPE (13), SIGALRM (14), SIGCHLD (17), SIGXCPU (24), SIGXFSZ (25), SIGVTALRM (26), SIGWINCH (28), SIGSYS (31)

Of course it's racy, like almost anything that messes with files in /proc, but it may be good enough for most situations.