Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created September 10, 2010 12:05
Show Gist options
  • Save kamipo/573517 to your computer and use it in GitHub Desktop.
Save kamipo/573517 to your computer and use it in GitHub Desktop.
socketpair
#!/usr/bin/perl
use strict;
use warnings;
use Socket;
use IO::Handle;
my @callback;
for my $host (@ARGV) {
socketpair(my $child, my $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die $!;
$child->autoflush(1);
$parent->autoflush(1);
if (my $pid = fork) {
close $parent;
push @callback, sub {
local $/ = undef;
chomp(my $line = <$child>);
print "$host: $line";
close $child;
waitpid($pid, 0);
};
} elsif (defined $pid) {
close $child;
close STDOUT;
open STDOUT, '>&' . fileno($parent);
close $parent;
exec "squidclient -h $host -p 8080 mgr:5min | grep 'syscalls.sock.writes'";
exit;
} else {
die $!;
}
}
$_->() for @callback;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment