fork, exec, and signal handling

From: Todd Pytel (tppytel_at_sophrosune.REMOVE.org)
Date: 12/31/03


Date: Wed, 31 Dec 2003 12:16:51 -0600

Hi all,

An infrequent perl coder here, having some issues with signal handling.
I'm not understanding something about how forking works with signals, I
think. The script is supposed to loop, running tcpdump until it exits
(after a certain number of packets are captured), rotating the dump output
and analyzing the output with tcpstat. Since the tcpdump can be rather
lengthy, I'd like to catch TERM signals and have the output analyzed so as
not to lose data. Here's some of what I've come up with - the use of pid
files is rather ghetto, I think, so suggestions are welcome:

$SIG{'TERM'} = \&term_handler;

sub main {
    while ( 1 ) {
        ( $stop ) && exit;
        defined (my $pid = fork) or die "Cannot fork: $!";
        unless ($pid) {
            exec "tcpdump -c $packet_count -i $interface -w $output > /dev/null 2>&1";
        }
        open PIDFILE, "> $dumppid";
        print PIDFILE "$pid\n";
        close PIDFILE;
        waitpid($pid, 0);
        unlink $dumppid;
        &rotate;
        &analyze; # Should fork this, too - don't want to wait on analysis
    }
}

sub term_handler {
    print "Running term_handler...\n";
    $stop = 1;
    if ( -e $dumppid ) {
        open PIDFILE, "$dumppid";
        while (<PIDFILE>) {
            chomp;
            kill 15, $_;
        }
        close PIDFILE;
    }
}

The problem is that the signal never gets caught, so the tcpdump keeps
going, and the cleanup at the end of main never occurs. From what I've
seen searching, this has something to do with the parent waiting on the
child process, but I'm not experienced enough to see exactly what the
problem is. Is there a solution or alternate approach that I can use here?

Thanks,
Todd Pytel



Relevant Pages

  • Re: Circular dependencies between modules
    ... Public Sub SetData ... MODULE" is set, by VBA, to "yes". ... >> AddedEmpsignals that a new employee has been added ... >> to a new employee in its underlying file ...
    (microsoft.public.access.formscoding)
  • Re: OpenVMS 8.1 ships
    ... Perl has about ... only modifications to one DCL script and one Perl script ... signals with a home-grown implementation based on the undocumented ... CRTL bug on 7.x systems that precludes the ability to call ...
    (comp.os.vms)
  • How to propagate signal down to child? trap erratic; wait not waiting
    ... I am having trouble propagating signals down to a child. ... I have a script s.sh that calls a program p ... trap killchild SIGTERM ...
    (comp.unix.shell)
  • Re: logrotate error
    ... logrotate -bash-3.00# logrotate -f logrotate.conf error: ... running script with arg /var/log/servicelog: " /bin/killall -s HUP ntpd; ... If you have not used strace before, its output is voluminous enouhg to bring ... to see what they do when they receive signals. ...
    (comp.os.linux.setup)
  • Re: Ctrl-C in script causing started process to terminate
    ... I ran the script and waited a bit for my process to finish its ... SIGINT and 2 of the child processes just die at the same time. ... disposition for signals handled in the parent is set to SIG_DFL. ... pressing C-C) are sent to the foreground process group, ...
    (comp.unix.programmer)