We don't need to read and write on the same fd.
The real right way is to detect when the psql client exits - possible when
the perl program spawns it, like shown below.
As always, TIMTOWTDI
andrew
--------------------------
use strict;
use IO::Handle;
use POSIX ":sys_wait_h";
my $curpos;
my $fifofile = shift || usage();
my $database = shift || usage();
open(FILE,$fifofile) || die $!;
my $psqlpid = open(OUTPIPE,"|-");
unless (defined($psqlpid)) { die $!; }
if ($psqlpid == 0) { exec("psql -a $database") || die $!; }
# must be parent here
sub REAPER { my $waitedpid; while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {if ($waitedpid == $psqlpid) { exit
0;} } $SIG{CHLD} = \&REAPER; # loathe sysV }
$SIG{CHLD} = \&REAPER;
OUTPIPE->autoflush();
for (;;) { for ($curpos = tell(FILE); $_ = <FILE>; $curpos = tell(FILE)) { print OUTPIPE $_; }
sleep(1); seek(FILE, $curpos, 0); }
sub usage { print STDERR "usage: ",$0," fifofile database\n"; exit 1; }
----- Original Message -----
From: "PeterKorman" <calvin-pgsql-ml@eigenvision.com>
To: "Andrew Dunstan" <andrew@dunslane.net>
Cc: <pgsql-hackers@postgresql.org>
Sent: Sunday, June 29, 2003 11:40 AM
Subject: Re: [HACKERS] persistant psql feature suggestion
> On Sun, Jun 29, 2003 at 10:22:49AM -0400, Andrew Dunstan wrote:
> > OK, worked out the wrinkle. psql is behaving perfectly well, but the
shim
> > doesn't get a SIGPIPE until it tries to write to it after psql has
exited.
> >
> > A slightly hackish fix for this would be to put this line after the
"print
> > $_" line:
> >
> > if ($_ eq "\\q\n") { sleep 1; print " "; } # get SIGPIPE if client
gone
> >
> > cheers
> >
> > andrew
>
> NAME
> IPC::Open2, open2 - open a process for both reading and writing
>
> http://www.perl.com/doc/manual/html/lib/IPC/Open2.html
>
> Would this help?
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly