#!/usr/bin/perl -w use Socket; use Carp; use Getopt::Std; sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n"; } sub REAPER { $SIG{CHLD}=\&REAPER; $waitedpid = wait; logmsg "reaped $waitedpid" if $opt_d; } $ENV{MGDATA}='/usr2/people/lis429/public_html/mgdata'; $ENV{MG_GETRC}='/usr2/people/lis429/public_html/.mg_getrc'; logmsg "Started"; getopts('ds:c:o:'); my $address = $opt_s || "/tmp/mgsock"; my $command = $opt_c || "/usr2/local/bin/mgquery"; my $options = $opt_o || "fna"; unlink $address or logmsg "failure removing $address"; socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!"; bind(Server,sockaddr_un($address)) or die "bind: $!"; listen(Server,SOMAXCONN) or die "listen: $!"; $SIG{CHLD}=\&REAPER; my $paddr; for ( ; $paddr = accept(Client,Server) ; close Client) { logmsg "connection on $address"; &spawn (sub { exec($command, $options); die "Can't exec $command!"; }); } sub spawn { my $coderef = shift; unless(@_ == 0 && $coderef && ref($coderef) eq 'CODE') { confess "usage: spawn CODEREF"; } my $pid; if (!defined ($pid = fork)) { logmsg "cannot fork: $!"; return; } elsif ($pid) { logmsg "begat $pid" if $opt_d; return; } select(Client); $|++; open(STDIN, "<&Client") or die "can't dup client to stdin"; open(STDOUT, ">&Client") or die "can't dup client to stdout"; exit &$coderef(); }