sub mping { my $mypid = $$; print "debug mping\nmy pid is $mypid\n"; {my @ls = `ls -1 /proc/$mypid/fd`; chomp @ls; printf "%s file descriptors:\n", scalar @ls; print join " ", sort {$a <=> $b} @ls; print "\n\n"; } my @hosts = @{$_[0]}; @hosts or die "no hosts to mping"; my ($pingcmd, $pingsize, $pingnumber, $pingtimeout) = @_[1,2,3,4]; croak "invalid mping arguments" unless $pingcmd and $pingnumber and $pingsize; my @pings; my %results; for ($i=0;$i<=$#hosts;$i++) { $pings[$i][0] = $hosts[$i]; my $pid; $pid = open $pings[$i][1], "-|", "$pingcmd $hosts[$i] $pingsize $pingnumber" or die "Error executing $pingcmd: $!\n";; my $old_flags = fcntl($pings[$i][1], F_GETFL, 0) or die "can't get flags: $!"; fcntl($pings[$i][1], F_SETFL, $old_flags | O_NONBLOCK) or die "can't set non blocking: $!"; $pings[$i][2] = $pid; printf "spawned ping, pid $pings[$i][2], fileno %s\n", fileno $pings[$i][1]; print "$pingcmd, $hosts[$i], $pingsize, $pingnumber failure\n" unless $pid; } printf "spawned %s ping commands\n", scalar @pings; my $start = time; my $j = 1; READ: while (@pings) { print "starting read loop $j\n"; printf "there are %s pings active\n", scalar @pings; print "fd status from /proc/$mypid/fd:\n"; {my @ls = `ls -1 /proc/$mypid/fd`; chomp @ls; print join " ", sort {$a <=> $b} @ls; print "\n"; } for ($i=$#pings;$i>=0;$i--) { my $buf; my $len = sysread $pings[$i][1], $buf, 580; if (not defined $len) { # loop } elsif ($len > 0) { # read, loop } elsif ($len == 0) { printf "done reading from pid $pings[$i][2] fd `%s`, closing\n", fileno $pings[$i][1]; my $rv; $rv = close $pings[$i][1]; printf "close $pings[$i][2] status: fd: `%s`; rv: `$rv`; \$!: `$!`; \$?: $?\n", fileno $pings[$i][2]; $results{$pings[$i][0]}{status} = $? >> 8; splice @pings, $i, 1; } else { die; } } sleep 2; print "end read loop $j\n"; printf "there are %s pings active\n", scalar @pings; print "fd status from /proc/$mypid/fd:\n"; {my @ls = `ls -1 /proc/$mypid/fd`; chomp @ls; print join " ", sort {$a <=> $b} @ls; print "\n"; } $j++; } return \%results; } #### pinging 6 IPs... debug mping my pid is 13884 7 file descriptors: 0 1 2 3 4 5 6 spawned ping, pid 13901, fileno 6 spawned ping, pid 13902, fileno 9 spawned ping, pid 13903, fileno 12 spawned ping, pid 13904, fileno 15 spawned ping, pid 13905, fileno 18 spawned ping, pid 13906, fileno 21 spawned 6 ping commands starting read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 end read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 starting read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 end read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 starting read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 end read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 starting read loop 4 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 done reading from pid 13906 fd `21`, closing close 13906 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13905 fd `18`, closing close 13905 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13904 fd `15`, closing close 13904 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13903 fd `12`, closing close 13903 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13902 fd `9`, closing close 13902 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13901 fd `6`, closing close 13901 status: fd: ``; rv: `1`; $!: ``; $?: 0 end read loop 4 there are 0 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 10 11 13 14 16 17 19 20 22 23 pinging 6 IPs... debug mping my pid is 13884 19 file descriptors: 0 1 2 3 4 5 6 7 8 10 11 13 14 16 17 19 20 22 23 spawned ping, pid 13920, fileno 6 spawned ping, pid 13921, fileno 15 spawned ping, pid 13922, fileno 24 spawned ping, pid 13923, fileno 27 spawned ping, pid 13924, fileno 30 spawned ping, pid 13925, fileno 33 spawned 6 ping commands starting read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 end read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 starting read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 end read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 starting read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 end read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 starting read loop 4 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 done reading from pid 13925 fd `33`, closing close 13925 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13924 fd `30`, closing close 13924 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13923 fd `27`, closing close 13923 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13922 fd `24`, closing close 13922 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13921 fd `15`, closing close 13921 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13920 fd `6`, closing close 13920 status: fd: ``; rv: `1`; $!: ``; $?: 0 end read loop 4 there are 0 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 25 26 28 29 31 32 34 35 pinging 6 IPs... debug mping my pid is 13884 31 file descriptors: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 20 21 22 23 25 26 28 29 31 32 34 35 spawned ping, pid 13936, fileno 6 spawned ping, pid 13937, fileno 27 spawned ping, pid 13938, fileno 36 spawned ping, pid 13939, fileno 39 spawned ping, pid 13940, fileno 42 spawned ping, pid 13941, fileno 45 spawned 6 ping commands starting read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 end read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 starting read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 end read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 starting read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 end read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 starting read loop 4 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 done reading from pid 13941 fd `45`, closing close 13941 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13940 fd `42`, closing close 13940 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13939 fd `39`, closing close 13939 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13938 fd `36`, closing close 13938 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13937 fd `27`, closing close 13937 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13936 fd `6`, closing close 13936 status: fd: ``; rv: `1`; $!: ``; $?: 0 end read loop 4 there are 0 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 37 38 40 41 43 44 46 47 pinging 6 IPs... debug mping my pid is 13884 43 file descriptors: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 37 38 40 41 43 44 46 47 spawned ping, pid 13952, fileno 6 spawned ping, pid 13953, fileno 39 spawned ping, pid 13954, fileno 48 spawned ping, pid 13955, fileno 51 spawned ping, pid 13956, fileno 54 spawned ping, pid 13957, fileno 57 spawned 6 ping commands starting read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 end read loop 1 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 starting read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 end read loop 2 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 starting read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 end read loop 3 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 starting read loop 4 there are 6 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 done reading from pid 13957 fd `57`, closing close 13957 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13956 fd `54`, closing close 13956 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13955 fd `51`, closing close 13955 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13954 fd `48`, closing close 13954 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13953 fd `39`, closing close 13953 status: fd: ``; rv: `1`; $!: ``; $?: 0 done reading from pid 13952 fd `6`, closing close 13952 status: fd: ``; rv: `1`; $!: ``; $?: 0 end read loop 4 there are 0 pings active fd status from /proc/13884/fd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 49 50 52 53 55 56 58 59 pinging 6 IPs... debug mping my pid is 13884 55 file descriptors: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 49 50 52 53 55 56 58 59 .....etc.....