| 1 | package Test::Postgresql58; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Class::Accessor::Lite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use File::Temp qw(tempdir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use POSIX qw(SIGTERM SIGKILL WNOHANG setuid); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | our $VERSION = '2.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Various paths that Postgres gets installed under, sometimes with a version on the end, | ||||||||||||||||||||||||||||
| 16 | # in which case take the highest version. We append /bin/ and so forth to the path later. | ||||||||||||||||||||||||||||
| 17 | # Note that these are used only if the program isn't already in the path. | ||||||||||||||||||||||||||||
| 18 | our @SEARCH_PATHS = ( | ||||||||||||||||||||||||||||
| 19 | split(/:/, $ENV{PATH}), | ||||||||||||||||||||||||||||
| 20 | # popular installation dir? | ||||||||||||||||||||||||||||
| 21 | qw(/usr/local/pgsql), | ||||||||||||||||||||||||||||
| 22 | # ubuntu (maybe debian as well, find the newest version) | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | (sort { $b cmp $a } grep { -d $_ } glob "/usr/lib/postgresql/*"), | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||
| 24 | # macport | ||||||||||||||||||||||||||||
| 25 | 0 | (sort { $b cmp $a } grep { -d $_ } glob "/opt/local/lib/postgresql*"), | |||||||||||||||||||||||||||
| 26 | # Postgresapp.com | ||||||||||||||||||||||||||||
| 27 | 0 | (sort { $b cmp $a } grep { -d $_ } glob "/Applications/Postgres.app/Contents/Versions/*"), | |||||||||||||||||||||||||||
| 28 | # BSDs end up with it in /usr/local/bin which doesn't appear to be in the path sometimes: | ||||||||||||||||||||||||||||
| 29 | "/usr/local", | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # This environment variable is used to override the default, so it gets | ||||||||||||||||||||||||||||
| 33 | # prefixed to the start of the search paths. | ||||||||||||||||||||||||||||
| 34 | 7 | ▶ | if (defined $ENV{POSTGRES_HOME} and -d $ENV{POSTGRES_HOME}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ENV{"POSTGRES_HOME"} and -d $ENV{"POSTGRES_HOME"}
MC/DC0%0/2
defined $ENV{"POSTGRES_HOME"} and -d $ENV{"POSTGRES_HOME"}
defined $ENV{"POSTGRES_HOME"}
-d $ENV{"POSTGRES_HOME"}
Truth table33%1/3
A and B
A= defined $ENV{"POSTGRES_HOME"}B= -d $ENV{"POSTGRES_HOME"}
| |||||||||||||||||||||||||||||
| 35 | 0 | unshift @SEARCH_PATHS, $ENV{POSTGRES_HOME}; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 7 | our $errstr; | |||||||||||||||||||||||||||
| 39 | 7 | our $BASE_PORT = 15432; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 7 | our %Defaults = ( | |||||||||||||||||||||||||||
| 42 | auto_start => 2, | ||||||||||||||||||||||||||||
| 43 | base_dir => undef, | ||||||||||||||||||||||||||||
| 44 | initdb => undef, | ||||||||||||||||||||||||||||
| 45 | initdb_args => '-U postgres -A trust', | ||||||||||||||||||||||||||||
| 46 | pid => undef, | ||||||||||||||||||||||||||||
| 47 | port => undef, | ||||||||||||||||||||||||||||
| 48 | postmaster => undef, | ||||||||||||||||||||||||||||
| 49 | postmaster_args => '-h 127.0.0.1 -F', | ||||||||||||||||||||||||||||
| 50 | uid => undef, | ||||||||||||||||||||||||||||
| 51 | _owner_pid => undef, | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 7 | Class::Accessor::Lite->mk_accessors(keys %Defaults); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=15 SCAR=42.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 6 | my $klass = shift; | |||||||||||||||||||||||||||
| 58 | my $self = bless { | ||||||||||||||||||||||||||||
| 59 | %Defaults, | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | @_ == 1 ? %{$_[0]} : @_, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | _owner_pid => $$, | ||||||||||||||||||||||||||||
| 62 | }, $klass; | ||||||||||||||||||||||||||||
| 63 | 6 | ▶ | if (! defined $self->uid && $ENV{USER} eq 'root') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $self->uid and $ENV{"USER"} eq "root"
MC/DC0%0/2
not defined $self->uid and $ENV{"USER"} eq "root"
not defined $self->uid
$ENV{"USER"} eq "root"
Truth table33%1/3
not(A) and B
A= not defined $self->uidB= $ENV{"USER"} eq "root"
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my @a = getpwnam('nobody') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | or die "user nobody does not exist, use uid() to specify user:$!"; | ||||||||||||||||||||||||||||
| 66 | 0 | $self->uid($a[2]); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 6 | ▶ | if (defined $self->base_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | $self->base_dir(cwd . '/' . $self->base_dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | if $self->base_dir !~ m|^/|; | ||||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | $self->base_dir( | ||||||||||||||||||||||||||||
| 73 | tempdir( | ||||||||||||||||||||||||||||
| 74 | 5 | ▶ | CLEANUP => $ENV{TEST_POSTGRESQL_PRESERVE} ? undef : 1, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | ), | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | 5 | ▶ | chown $self->uid, -1, $self->base_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | if defined $self->uid; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 6 | ▶ | if (! defined $self->initdb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 6 | ▶ | my $prog = _find_program('initdb') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | or return; | ||||||||||||||||||||||||||||
| 83 | 0 | $self->initdb($prog); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (! defined $self->postmaster) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $prog = _find_program('postmaster') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or return; | ||||||||||||||||||||||||||||
| 88 | 0 | $self->postmaster($prog); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($self->auto_start) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $self->setup | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | if $self->auto_start >= 2; | ||||||||||||||||||||||||||||
| 93 | 0 | $self->start; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | $self; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 6 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 99 | 6 | local $?; | |||||||||||||||||||||||||||
| 100 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 6 | ▶ | $self->stop | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->pid and $$ == $self->_owner_pid
MC/DC0%0/2
defined $self->pid and $$ == $self->_owner_pid
defined $self->pid
$$ == $self->_owner_pid
Truth table33%1/3
A and B
A= defined $self->pidB= $$ == $self->_owner_pid
| |||||||||||||||||||||||||||||
| 102 | if defined $self->pid && $$ == $self->_owner_pid; | ||||||||||||||||||||||||||||
| 103 | 6 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub dsn { | ||||||||||||||||||||||||||
|
Subroutine
dsn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 0 | my %args = $self->_default_args(@_); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | return 'DBI:Pg:' . join(';', map { "$_=$args{$_}" } sort keys %args); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub _default_args { | ||||||||||||||||||||||||||
|
Subroutine
_default_args: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | $args{host} ||= '127.0.0.1'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"host"} ||= "127.0.0.1"
MC/DC0%0/1
$args{"host"} ||= "127.0.0.1"
$args{"host"}
Truth table0%0/2
A
A= $args{"host"}
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $args{port} ||= $self->port; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args{"port"} ||= $self->port
MC/DC0%0/2
$args{"port"} ||= $self->port
$args{"port"}
$self->port
Truth table0%0/3
A ||= B
A= $args{"port"}B= $self->port
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $args{user} ||= 'postgres'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"user"} ||= "postgres"
MC/DC0%0/1
$args{"user"} ||= "postgres"
$args{"user"}
Truth table0%0/2
A
A= $args{"user"}
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $args{dbname} ||= 'test'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"dbname"} ||= "test"
MC/DC0%0/1
$args{"dbname"} ||= "test"
$args{"dbname"}
Truth table0%0/2
A
A= $args{"dbname"}
| |||||||||||||||||||||||||||||
| 119 | 0 | return %args; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub uri { | ||||||||||||||||||||||||||
|
Subroutine
uri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 0 | my %args = $self->_default_args(@_); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | return sprintf('postgresql://%s@%s:%d/%s', @args{qw/user host port dbname/}); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | return | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if defined $self->pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | # start (or die) | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 135 | 0 | my $err; | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($self->port) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $err = $self->_try_start($self->port) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | or return; | ||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||
| 140 | # try by incrementing port no | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | for (my $port = $BASE_PORT; $port < $BASE_PORT + 100; $port++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $err = $self->_try_start($port) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | or return; | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | # failed | ||||||||||||||||||||||||||||
| 147 | 0 | die "failed to launch PostgreSQL:$!\n$err"; | |||||||||||||||||||||||||||
| 148 | 0 | }->(); | |||||||||||||||||||||||||||
| 149 | { # create "test" database | ||||||||||||||||||||||||||||
| 150 | 0 | my $tries = 5; | |||||||||||||||||||||||||||
| 151 | 0 | my $dbh; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | while ($tries) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $tries -= 1; | |||||||||||||||||||||||||||
| 154 | 0 | $dbh = DBI->connect($self->dsn(dbname => 'template1'), '', '', { | |||||||||||||||||||||||||||
| 155 | PrintError => 0, | ||||||||||||||||||||||||||||
| 156 | RaiseError => 0 | ||||||||||||||||||||||||||||
| 157 | }); | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | last if $dbh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # waiting for database to start up | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($DBI::errstr =~ /the database system is starting up/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$DBI::errstr =~ /the database system is starting up/ or $DBI::errstr =~ /Connection refused/
MC/DC0%0/2
$DBI::errstr =~ /the database system is starting up/ or $DBI::errstr =~ /Connection refused/
$DBI::errstr =~ /the database system is starting up/
$DBI::errstr =~ /Connection refused/
Truth table0%0/3
A or B
A= $DBI::errstr =~ /the database system is starting up/B= $DBI::errstr =~ /Connection refused/
| |||||||||||||||||||||||||||||
| 162 | || $DBI::errstr =~ /Connection refused/) { | ||||||||||||||||||||||||||||
| 163 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 164 | 0 | next; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | die $DBI::errstr; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | die "Connection to the database failed even after 5 tries" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | unless ($dbh); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if ($dbh->selectrow_arrayref(q{SELECT COUNT(*) FROM pg_database WHERE datname='test'})->[0] == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | $dbh->do('CREATE DATABASE test') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | or die $dbh->errstr; | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub _try_start { | ||||||||||||||||||||||||||
|
Subroutine
_try_start: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 180 | 0 | my ($self, $port) = @_; | |||||||||||||||||||||||||||
| 181 | # open log and fork | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | open my $logfh, '>>', $self->base_dir . '/postgres.log' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | or die 'failed to create log file:' . $self->base_dir | ||||||||||||||||||||||||||||
| 184 | . "/postgres.log:$!"; | ||||||||||||||||||||||||||||
| 185 | 0 | my $pid = fork; | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | die "fork(2) failed:$!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | unless defined $pid; | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | open STDOUT, '>>&', $logfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | or die "dup(2) failed:$!"; | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | open STDERR, '>>&', $logfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | or die "dup(2) failed:$!"; | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | chdir $self->base_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | or die "failed to chdir to:" . $self->base_dir . ":$!"; | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (defined $self->uid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | setuid($self->uid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | or die "setuid failed:$!"; | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 0 | my $cmd = join( | |||||||||||||||||||||||||||
| 200 | ' ', | ||||||||||||||||||||||||||||
| 201 | $self->postmaster, | ||||||||||||||||||||||||||||
| 202 | $self->postmaster_args, | ||||||||||||||||||||||||||||
| 203 | '-p', $port, | ||||||||||||||||||||||||||||
| 204 | '-D', $self->base_dir . '/data', | ||||||||||||||||||||||||||||
| 205 | '-k', $self->base_dir . '/tmp', | ||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||
| 207 | 0 | exec($cmd); | |||||||||||||||||||||||||||
| 208 | 0 | die "failed to launch postmaster:$?"; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 0 | close $logfh; | |||||||||||||||||||||||||||
| 211 | # wait until server becomes ready (or dies) | ||||||||||||||||||||||||||||
| 212 | 0 | ▶ | for (my $i = 0; $i < 100; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | open $logfh, '<', $self->base_dir . '/postgres.log' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | or die 'failed to open log file:' . $self->base_dir | ||||||||||||||||||||||||||||
| 215 | . "/postgres.log:$!"; | ||||||||||||||||||||||||||||
| 216 | 0 | my $lines = do { join '', <$logfh> }; | |||||||||||||||||||||||||||
| 217 | 0 | close $logfh; | |||||||||||||||||||||||||||
| 218 | last | ||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if $lines =~ /is ready to accept connections/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if (waitpid($pid, WNOHANG) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | # failed | ||||||||||||||||||||||||||||
| 222 | 0 | return $lines; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | # PostgreSQL is ready | ||||||||||||||||||||||||||||
| 227 | 0 | $self->pid($pid); | |||||||||||||||||||||||||||
| 228 | 0 | $self->port($port); | |||||||||||||||||||||||||||
| 229 | 0 | return; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 0 | my ($self, $sig) = @_; | |||||||||||||||||||||||||||
| 234 | 0 | ▶ | return unless defined $self->pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | $sig ||= SIGTERM; | ||||||||||||||||||||||||||
|
Condition0%0/2
$sig ||= 15
MC/DC0%0/1
$sig ||= 15
$sig
Truth table0%0/2
A
A= $sig
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | kill $sig, $self->pid; | |||||||||||||||||||||||||||
| 239 | 0 | my $timeout = 10; | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | while ($timeout > 0 and waitpid($self->pid, WNOHANG) <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$timeout > 0 and waitpid($self->pid, 1) <= 0
MC/DC0%0/2
$timeout > 0 and waitpid($self->pid, 1) <= 0
$timeout > 0
waitpid($self->pid, 1) <= 0
Truth table0%0/3
A and B
A= $timeout > 0B= waitpid($self->pid, 1) <= 0
| |||||||||||||||||||||||||||||
| 241 | 0 | $timeout -= sleep(1); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | if ($timeout <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | warn "Pg refused to die gracefully; killing it violently.\n"; | |||||||||||||||||||||||||||
| 246 | 0 | kill SIGKILL, $self->pid; | |||||||||||||||||||||||||||
| 247 | 0 | $timeout = 5; | |||||||||||||||||||||||||||
| 248 | 0 | ▶ | while ($timeout > 0 and waitpid($self->pid, WNOHANG) <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$timeout > 0 and waitpid($self->pid, 1) <= 0
MC/DC0%0/2
$timeout > 0 and waitpid($self->pid, 1) <= 0
$timeout > 0
waitpid($self->pid, 1) <= 0
Truth table0%0/3
A and B
A= $timeout > 0B= waitpid($self->pid, 1) <= 0
| |||||||||||||||||||||||||||||
| 249 | 0 | $timeout -= sleep(1); | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ($timeout <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | warn "Pg really didn't die.. WTF?\n"; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | $self->pid(undef); | |||||||||||||||||||||||||||
| 257 | 0 | return; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||
| 261 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 262 | # (re)create directory structure | ||||||||||||||||||||||||||||
| 263 | 0 | mkdir $self->base_dir; | |||||||||||||||||||||||||||
| 264 | 0 | ▶ | chmod 0755, $self->base_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | or die "failed to chmod 0755 dir:" . $self->base_dir . ":$!"; | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ($ENV{USER} eq 'root') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | chown $self->uid, -1, $self->base_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | or die "failed to chown dir:" . $self->base_dir . ":$!"; | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if (mkdir $self->base_dir . '/tmp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | ▶ | if ($self->uid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | chown $self->uid, -1, $self->base_dir . '/tmp' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | or die "failed to chown dir:" . $self->base_dir . "/tmp:$!"; | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | # initdb | ||||||||||||||||||||||||||||
| 277 | 0 | ▶ | if (! -d $self->base_dir . '/data') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 0 | ▶ | pipe my $rfh, my $wfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | or die "failed to create pipe:$!"; | ||||||||||||||||||||||||||||
| 280 | 0 | my $pid = fork; | |||||||||||||||||||||||||||
| 281 | 0 | ▶ | die "fork failed:$!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | unless defined $pid; | ||||||||||||||||||||||||||||
| 283 | 0 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | close $rfh; | |||||||||||||||||||||||||||
| 285 | 0 | ▶ | open STDOUT, '>&', $wfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | or die "dup(2) failed:$!"; | ||||||||||||||||||||||||||||
| 287 | 0 | ▶ | open STDERR, '>&', $wfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | or die "dup(2) failed:$!"; | ||||||||||||||||||||||||||||
| 289 | 0 | ▶ | chdir $self->base_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | or die "failed to chdir to:" . $self->base_dir . ":$!"; | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | if (defined $self->uid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | setuid($self->uid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | or die "setuid failed:$!"; | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 0 | my $cmd = join( | |||||||||||||||||||||||||||
| 296 | ' ', | ||||||||||||||||||||||||||||
| 297 | $self->initdb, | ||||||||||||||||||||||||||||
| 298 | $self->initdb_args, | ||||||||||||||||||||||||||||
| 299 | '-D', $self->base_dir . '/data', | ||||||||||||||||||||||||||||
| 300 | ); | ||||||||||||||||||||||||||||
| 301 | 0 | exec($cmd); | |||||||||||||||||||||||||||
| 302 | 0 | die "failed to exec:$cmd:$!"; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | 0 | close $wfh; | |||||||||||||||||||||||||||
| 305 | 0 | my $output = ''; | |||||||||||||||||||||||||||
| 306 | 0 | ▶ | while (my $l = <$rfh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 0 | $output .= $l; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | 0 | close $rfh; | |||||||||||||||||||||||||||
| 310 | 0 | ▶ | while (waitpid($pid, 0) <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | 0 | ▶ | die "*** initdb failed ***\n$output\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | if $? != 0; | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | # use postgres hard-coded configuration as some packagers mess | ||||||||||||||||||||||||||||
| 316 | # around with postgresql.conf.sample too much: | ||||||||||||||||||||||||||||
| 317 | 0 | truncate $self->base_dir . '/data/postgresql.conf', 0; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 6 | ▶ | sub _find_program { | ||||||||||||||||||||||||||
|
Subroutine
_find_program: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 322 | 6 | my $prog = shift; | |||||||||||||||||||||||||||
| 323 | 6 | undef $errstr; | |||||||||||||||||||||||||||
| 324 | 6 | for my $sp (@SEARCH_PATHS) { | |||||||||||||||||||||||||||
| 325 | 60 | ▶ | return "$sp/bin/$prog" if -x "$sp/bin/$prog"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 60 | ▶ | return "$sp/$prog" if -x "$sp/$prog"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | 6 | $errstr = "could not find $prog, please set appropriate PATH or POSTGRES_HOME"; | |||||||||||||||||||||||||||
| 329 | 6 | return; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 7 | 1; | |||||||||||||||||||||||||||
| 333 | __END__ | ||||||||||||||||||||||||||||