Sanity checks for dump script
authorIan Barwick <ian@2ndquadrant.com>
Mon, 15 Dec 2014 00:14:12 +0000 (09:14 +0900)
committerIan Barwick <ian@2ndquadrant.com>
Mon, 15 Dec 2014 00:14:12 +0000 (09:14 +0900)
src/test/regress/ddl-deparse/dump_regress_db.pl

index 3e007b3f0adc05fc376632537e8610741641d656..de8fc7d165d8ce45d782699e852f930a25a8e394 100755 (executable)
@@ -12,7 +12,9 @@
 use strict;
 use warnings;
 
+use File::Temp qw(tempfile);
 use Getopt::Long;
+use IO::Socket::INET;
 
 our %options = (
     'pgdata' => undef,
@@ -36,22 +38,38 @@ if(!-d $options{'pgdata'}) {
     die(qq|Data directory $options{'pgdata'} not found or not a directory\n|);
 }
 
-# TODO: check port not in use
+# Check specified port not in use
+my $socket = IO::Socket::INET->new(
+    LocalAddr =>'localhost',
+    LocalPort => $options{'port'},
+    Proto     => 'tcp',
+    ReusePort => 1
+);
+
+if(!$socket) {
+    die qq|Port '$options{'port'}' appears to be in use\n|;
+}
+close($socket);
 
-# TODO: check for active instance
-# TODO: make unique log file
+my ($fh, $log_file) = tempfile( 'dump_regress_XXXXXX', TMPDIR => 1, SUFFIX => '.log', EXLOCK => 0);
+
+# check for extant pidfile in specified data directory
+# (not 100% foolproof but good enough for now)
+my $pidfile = qq|$options{'pgdata'}/postmaster.pid|;
+if(-e $pidfile) {
+    die qq|'$pidfile' exists - is another instance using the data directory?|;
+}
 
-my $log_dir = '/tmp/dump-regress.log';
 my $pg_ctl= sprintf(
     q|%s/src/bin/pg_ctl/pg_ctl -p %s/src/backend/postgres --pgdata %s -o '-p%i' -l %s -w|,
     $options{'top-builddir'},
     $options{'top-builddir'},
     $options{'pgdata'},
     $options{'port'},
-    $log_dir,
+    $log_file,
 );
 
-#print "$pg_ctl\n";exit;
+
 # TODO: check success
 `${pg_ctl} start`;