CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problems terminating fork subprocess

 



eyncydious
New User

Oct 18, 2011, 12:10 PM

Post #1 of 1 (305 views)
Problems terminating fork subprocess Can't Post

Hi folks,

I have a bit of an issue with a fork function creating a child process. I'm using perl to open a child process and perform a tcpdump for a set of user-defined parameters. Everything works fine through the fork process and the termination of the child process. However, when the child actually opens the filehandle pipe, it does so in another subprocess. Now, I assume that's because I am redirecting stdout and stderr which somehow requires the child to spawn the actual tcpdump in a sub process. I have literally tried hundreds of different combinations of SIG{ALRM}, kills, and closed filehandles at different places. Everything I've tried closes both the child and the parent, but leaves the tcpdump process open to capture and chew up disk space until the disk fills up. If anyone has ANY experience with how to do this, I would forever be in your debt.

In the code snippet below, I've left in the commented lines to show where I've tried different combinations. Most of the advice I've read thus far from similiar issues, suggests using the child process within an eval {}; block, but to be perfectly honest, I don't think that will help. The eval block won't help me because the child isn't crashing; on the contrary, it's working just fine. It's the child's subprocess that I can't seem to handle. At any rate, I appreciate any help.

if( my $pid = fork() ) {
waitpid( $pid,0 );
}
elsif( $pid == 0 ) {
print "I'm going to try to kill $jpid\n";
kill $jpid;
$SIG{ALRM} = sub { my $now = &time(); print RVDATA "Packet capture terminated; PID = $jpid at $now\n"; die "graceful shutdown\n";};
open (RVDATA, ">>$$data") or die "Child 'time' can't open $$data -- $!\n";
alarm(10);
$jpid = open (CMD, "$$command |") or die "Child 'time' can't open $$log -- $!\n";
$$jjpid = $jpid;
print "child sees the filehandle process as $jpid\n";
while (<CMD>) {
print RVDATA "$_\n";
}
alarm(0);
# close(CMD);
exit(0);
}
#kill 15, $jpid;
close(CMD);
close(RVDATA);
# return($jpid);
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives