Re: "Misses" in a loop



Jean-Charles Ripault wrote:
Hi all,

For a customer project I'm writing a "firewall proof" script.
This script, between two systems tries systematically all tcp ports (up to a given parameter) of all target network.

Now, I've made a double loop (one for the IP addresses, one for the ports) to browse this). For speed reasons, I fork all the network "clients" and they all individually log their results in a global log file opened before the fork.

Despite this, and the fact that I log both success and failure and that I check that all children successfully died, I get some holes the log files, means that some tests do not get logged (they most of the times do show up in the firewall log) and I get no error message telling me that a child unexpectedly died or crashed.
[...]

I think your children are clobbering on each other's output.

My advice is to use syslog since it's designed to receive log messages from multiple asynchronously run applications.

If you don't want to do that, try to organize things so that the children never have to open the log file themselves. Instead, they should just write to a file-handle you've opened or redirected.

This is what I'm thinking:

#!/usr/bin/perl

use strict;
use warnings;
use constant EXIT_OK => 0;

local $\ = "\n";
exit (@ARGV < 1 ? parent() : child() );

###############################

sub parent {
close STDOUT;
open (STDOUT, '>', 'logfile') or die("Couldn't re-open STDOUT\n");

for my $id (2..10) {
system("$0 child=$id &");
}
EXIT_OK;
}

sub child {
my ($id) = $ARGV[0] =~ m/(\d+)/;
print "I'm a child, and my id is $id.";
EXIT_OK;
}


__END__

The "&" in the 'system' command allows of asynchronous running of the child processes, and the children inherit the redirected STDOUT.

.



Relevant Pages

  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • Re: Questions about perl daemons with child processes and open files / signals
    ... running as daemon which launch a child process. ... I need to run a perl script as a daemon. ... tcpdump output, but I decided to use a pipe ... the parent process. ...
    (comp.lang.perl.misc)
  • Heirarchical path to the net
    ... traced the path of a net from child or leaf cell to the parent. ... script was run and it used .sch files(we use Calibre, ... Why the script traced up is because some signals have fan outs. ... hiCreateAppForm( ...
    (comp.cad.cadence)
  • Re: New Security Features, Please Comment
    ... then call setuid/gid and then launch the script ... language. ... The idea is to not allow the child to change its own uid, ...
    (Linux-Kernel)
  • Re: ("Scripting.Dictionary") buffer
    ... RAM I managed to populate a dictionary object with 18,000 Distinguished ... My test script was slow, ... ' Sub to enumerate users in OU/Container and child containers. ...
    (microsoft.public.scripting.vbscript)