Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got an issue where I'm calling an external program (tetheral) to parse some QoS bits, but the problem is occasionally the IP i'm keying on doesn't respond, which causes the tethereal process to hang. since this check runs every few mins, if the remote host is down it can chew up resources on the box checking. I've gotten around this by programming the check with fork() and exec() so I can keep track of the pid of tethereal when it gets spawned, and kill it appropriately if it hasn't terminated in a given time frame.

so, that's all well and good, except the tethereal process I call has some rather complex arguments, so it spawns a shell to interpret them. My script can kill the tethereal process if it hangs, but now I have a duplicate shell running the tetheral process that doesn't get killed, and I can't keep track of. I've found on this site and others that you can avoid having exec() parse through a shell by giving the arguments as an array. the trouble is, I haven't yet figured out how to properly format these arguments into an array so that tethereal sees them correctly. this is what I need help with. here's the bit of code I'm calling now, that spawns the shell process (that I want to avoid):
exec("/usr/bin/tethereal -c 10 -i eth1 -f \"(! vlan 0) and src host $I +P and udp and port 5060\" -x | grep ^0010 > /var/tmp/tosbits.txt");

20070608 Janitored by Corion: Changed PRE to CODE tags

Replies are listed 'Best First'.
Re: avoiding shell escapes with exec()
by runrig (Abbot) on Jun 08, 2007 at 00:53 UTC
    When you have pipes and file redirection, using 'exec @array' is not straightforward. I wrote some code that may help get you started.

    Update: and as for passing your args to exec, it depends on how you would normally run tetheral on the command line. If you would normall run:

    command arg1 arg2 "Quoted stuff as arg3"
    Then you would pass to system or exec:
    system('command', 'arg1', 'arg2', 'Quoted stuff as arg3');
      thanks, that worked! the only issue is that @pids doesn't seem to be globally referenced, so even though it gets populated with the child pids, the other fork of the program (that is watching to see if they hang) sees that array as empty. So I just had to export them to a file and then read them in when the timeout happened.
        ...the only issue is that @pids doesn't seem to be globally referenced...
        I'm not sure what you mean by that. This seems to work:
        my ($fh, @pids) = pipeline( [qw(ls -1)], [qw(sed -e s/t/a/g)], [qw(sed -e s/z/b/g)], # Let's "hang" for 20 seconds [qw(sleep 20)], ); eval { local $SIG{ALRM} = sub { die "alarm\n" }; # Assume we're "hung" after 5 seconds alarm 5; while (<$fh>) { print "$.:$_"; } }; if ($@) { die unless $@ eq "alarm\n"; # Kill the sleep process kill 1, $pids[0]; # timed out } else { close $fh; } print "Pids: @pids\n";
        Sorry if I'm misunderstanding, but glad you got it working.
Re: avoiding shell escapes with exec()
by Argel (Prior) on Jun 08, 2007 at 23:09 UTC
    Regarding your use of fork/exec couldn't you use alarm instead? Soemthing like:
    sub cmd_wrapper { my( $cmd ) = @_; my( $rc , $out ); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 15; $out = (`$cmd`)[0]; # Get first line of output $rc = $?; alarm 0; }; if( $@ ) { die "Eval failed for $cmd but not because of alarm" if $@ ne " +alarm\n"; # Propogate unexpected errors die "Eval failed for $cmd because alarm timed out"; } die "Return code undefined for $cmd" unless defined $rc; return $rc, $out if wantarray; return $rc; }
      I did try using alarm, and that works great for keeping the parent process from hanging, but it does not help with keeping track of the child pids that hang, which is the main issue I have
        I was under the impression that the only reason you were using the fork was to help deal with the hanging issue. I take it you need to do some batch processing instead?
Re: avoiding shell escapes with exec()
by chunter (Initiate) on Jun 08, 2007 at 00:36 UTC
    This was my question, didn't see I wasn't logged in.