1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
#!/usr/bin/perl -wT
# multiversion/cluster aware pg_ctl wrapper; this also supplies the correct
# configuration parameters to 'start', and makes sure that a postmaster really
# stops on 'stop'.
#
# (C) 2005 Martin Pitt <mpitt@debian.org>
use lib '/usr/share/postgresql-common';
use Getopt::Long;
use POSIX qw/setsid dup2 setlocale LC_ALL :sys_wait_h/;
use PgCommon;
use Fcntl 'SEEK_SET';
# Check for known broken configurations of this cluster
sub check_valid_config {
my %postgresql_conf = read_cluster_conf_file $version, $cluster,
'postgresql.conf';
my $log_statement_stats = config_bool $postgresql_conf{'log_statement_stats'};
my $log_parser_stats = config_bool $postgresql_conf{'log_parser_stats'};
my $log_planner_stats = config_bool $postgresql_conf{'log_planner_stats'};
my $log_executor_stats = config_bool $postgresql_conf{'log_executor_stats'};
# statement and other statistics are mutually exclusive
if ($log_statement_stats && ($log_parser_stats || $log_planner_stats ||
$log_executor_stats)) {
error 'invalid postgresql.conf: log_statement_stats and the other log_*_stats options are mutually exclusive';
}
}
# Return the PID from an existing PID file or undef if it does not exist.
# Arguments: <pid file path>
sub get_running_pid {
return undef unless -e $_[0];
if (open PIDFILE, $_[0]) {
my $pid = <PIDFILE>;
close PIDFILE;
chomp $pid;
($pid) = $pid =~ /^(\d+)\s*$/; # untaint
return $pid;
} else {
return undef;
}
}
# Check whether a pid file is present and belongs to a running postmaster.
# Arguments: <pid file path>
sub check_running_postmaster {
my $pid = get_running_pid $_[0];
if (defined $pid) {
if (open PS, '-|', '/bin/ps', '-o', 'comm', 'h', 'p', $pid) {
my $process = <PS>;
chomp $process if defined $process;
close PS;
if (defined $process and ($process eq 'postmaster' or $process eq 'postgres')) {
return 1;
}
} else {
error "Could not exec /bin/ps";
}
}
return 0;
}
# If a pid file is already present, delete it if it is stale/invalid, or exit
# with a notice if it belongs to an already running postmaster.
sub start_check_pid_file {
my $pidfile = $info{'pgdata'}.'/postmaster.pid';
if (check_running_postmaster $pidfile) {
print "Cluster is already running.\n";
exit 2;
}
# Remove invalid or stale PID file
if (-e $pidfile) {
unlink $pidfile;
print "Removed stale pid file.\n";
}
}
# Check if a pid file is not present or it is invalid. If so, clean up/abort.
sub stop_check_pid_file {
my $pidfile = $info{'pgdata'}.'/postmaster.pid';
my $pid = get_running_pid $pidfile;
return if (defined $pid && check_running_postmaster $pidfile);
if ($info{'running'}) {
error 'pid file is invalid, please manually kill the stale server process.';
}
# Remove invalid or stale PID file
if (-e $pidfile) {
unlink $pidfile;
print "Removed stale pid file.\n";
}
print "Cluster is not running.\n";
exit 2;
}
# check if a cluster reliably connects or fails
# Arguments: <version> <cluster> <port> <socket dir>
sub cluster_port_ready {
my ($v, $c, $p, $sd) = @_;
my $psql = get_program_path 'psql', $v;
error 'cluster_port_ready: could not find psql binary' unless $psql;
my $n = 0;
my $result = 0;
# probe until we get three successful or failed connections in a row
$ENV{'PGPASSWORD'} = 'foo'; # prevent hangs if superuser cannot connect withouth password
my $out;
while ($n < ($result ? 10 : 3)) {
select undef, undef, undef, 0.5;
$out = `$psql -h '$sd' --port $p -l 2>&1 >/dev/null`;
if ($? == $result) {
$n++;
} else {
$n = 0;
}
$result = $?;
}
if ($out =~ 'FATAL:') {
print STDERR "WARNING: connection to the database failed, disabling startup checks:\n$out\n";
return cluster_port_running $v, $c, $p;
}
return !$result;
}
sub autovacuum_start {
if ($version ge '8.1') {
error 'PostgreSQL 8.1 and above has an integrated autovacuum daemon which cannot be controlled by this program.';
}
return unless $info{'avac_enable'};
error "pg_autovacuum not found. Install postgresql-contrib-$version to get it"
unless $pg_autovacuum;
return if -f $info{'pgdata'}.'/autovacuum.pid';
my %avac_flags = ('avac_sleep_base' => '-s', 'avac_sleep_scale' => '-S',
'avac_vac_base' => '-v', 'avac_vac_scale' => '-V',
'avac_anal_base' => '-a', 'avac_anal_scale' => '-A',
'avac_logfile' => '-L', 'avac_debug' => '-d');
@options = ('-p', $info{'port'}, '-H', $info{'socketdir'});
foreach (keys %info) {
if (my $option = $avac_flags{$_}) {
push (@options, $option, $info{$_}) if $info{$_};
}
}
# check that the cluster is actually running
my $running = 0;
for (my $attempt = 0; $attempt < 10; $attempt++) {
select (undef, undef, undef, 0.5);
if (cluster_port_running $version, $cluster, $info{'port'}) {
$running = 1;
last;
}
}
error "Cannot start autovacuum daemon: cluster is not running" unless $running;
# remember current size of the log
my $logsize = -1;
if (defined $info{'avac_logfile'}) {
$logsize = (stat $info{'avac_logfile'})[7] if -r $info{'avac_logfile'};
}
if (!($pid = fork)) {
setsid or die "setsid failed(): $!";
close $_ for(STDOUT, STDIN, STDERR);
chdir '/';
exec $pg_autovacuum, @options;
exit -1;
} else {
# wait a bit and check whether the daemon really started
select (undef, undef, undef, 0.5);
# clean up process if it exited
waitpid($pid, WNOHANG);
unless (kill 0, $pid) {
print STDERR "The PostgreSQL autovacuum daemon failed to start.\n";
if ($logsize >= 0) {
print STDERR "Please check the log output:\n";
open LOG, $info{'avac_logfile'} or
error "Could not open log file " . $info{'avac_logfile'};
seek LOG, $logsize, SEEK_SET;
print STDERR $_ while <LOG>;
} else {
print STDERR "/etc/postgresql/$version/$cluster/autovacuum_log is missing or a dangling symlink.\n\n";
}
exit 1;
}
# write PID file
if(open my $AVAC_PID, '>', $info{'pgdata'}.'/autovacuum.pid') {
print $AVAC_PID "$pid\n";
close $AVAC_PID;
}
exit 0;
}
}
sub autovacuum_stop {
if ($version ge '8.1') {
error 'PostgreSQL 8.1 and above has an integrated autovacuum daemon which cannot be controlled by this program.';
}
return unless $info{'avac_enable'};
error "pg_autovacuum not found. Install postgresql-contrib-$version to get it"
unless $pg_autovacuum;
if (open my $AVAC_PID, $info{'pgdata'}.'/autovacuum.pid') {
$pid = <$AVAC_PID>;
($pid) = $pid =~ /^(\d+)\s*$/; # untaint
kill ('TERM', $pid) if $pid;
close $AVAC_PID;
unlink $info{'pgdata'}.'/autovacuum.pid';
}
}
sub autovacuum_restart {
if ($version ge '8.1') {
error 'PostgreSQL 8.1 and above has an integrated autovacuum daemon which cannot be controlled by this program.';
}
if (-f $info{'pgdata'}.'/autovacuum.pid') {
autovacuum_stop;
autovacuum_start;
}
}
sub start {
my $cdir = $info{'configdir'};
check_valid_config;
start_check_pid_file;
# get locale used by initdb
my ($lc_ctype, $lc_collate) = get_cluster_locales $version, $cluster;
$lc_ctype or error ('Could not parse locale out of pg_controldata output');
# check validity of locale
unless (setlocale (LC_ALL, $lc_ctype)) {
error ("The server must be started under the locale $lc_ctype which does not exist any more.")
}
# prepare environment
%ENV = read_cluster_conf_file $version, $cluster, 'environment';
$ENV{'LC_CTYPE'} = $lc_ctype;
my $postmaster_opts = '';
if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'unix_socket_directory')) {
$postmaster_opts .= '-c unix_socket_directory="' . $info{'socketdir'} . '"';
}
# versions 8.0+ support configurable conffile locations and external PID files
if ($version ge '8.0') {
$postmaster_opts .= " -c config_file=\"$cdir/postgresql.conf\"";
if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'hba_file')) {
$postmaster_opts .= " -c hba_file=\"$cdir/pg_hba.conf\"";
}
if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'ident_file')) {
$postmaster_opts .= " -c ident_file=\"$cdir/pg_ident.conf\"";
}
if ((-d '/var/run/postgresql') && !defined (PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'external_pid_file')) {
# check whether /var/run/postgresql/ is writeable as the cluster owner
my $vrp_writable;
if ($> == 0) {
change_ugid $info{'owneruid'}, $info{'ownergid'};
$vrp_writable = -w '/var/run/postgresql';
$< = $> = 0;
$( = $) = 0;
} else {
$vrp_writable = -w '/var/run/postgresql';
}
if ($vrp_writable) {
$postmaster_opts .= " -c external_pid_file=\"/var/run/postgresql/$version-$cluster.pid\"";
}
}
}
$postmaster_opts .= ' ' . (join ' ', @postmaster_auxoptions);
($postmaster_opts) = $postmaster_opts =~ /(.*)/; # untaint
# only supply or default logfile path when none is given explicitly in
# postgresql.conf
@options = ($pg_ctl, 'start', '-D', $info{'pgdata'});
$logsize = 0;
if ($info{'logfile'}) {
push @options, ('-l', $info{'logfile'});
# remember current size of the log
unless (-e $info{'logfile'}) {
open L, '>', $info{'logfile'} or
error 'Could not create log file ' . $info{'logfile'};
chmod 0640, $info{'logfile'};
my $g;
if ($info{'owneruid'} < 500) {
$g = (getgrnam 'adm')[2];
} else {
$g = $info{'ownergid'};
}
chown $info{'owneruid'}, $g, $info{'logfile'};
close L;
}
$logsize = (stat $info{'logfile'})[7];
}
push @options, ('-s', '-o', $postmaster_opts);
if (fork) {
wait;
error "could not exec $pg_ctl @options: $!" if $?;
} else {
setsid or error "could not start session: $!";
dup2(POSIX::open('/dev/null', POSIX::O_WRONLY), 1);
dup2(POSIX::open('/dev/null', POSIX::O_WRONLY), 2);
exec $pg_ctl @options or error "could not exec $pg_ctl @options: $!";
}
# wait a bit until the socket exists
$success = 0;
$currlogsize = 0;
my $pidfile = $info{'pgdata'}.'/postmaster.pid';
for (my $attempt = 0; $attempt < 60; $attempt++) {
select (undef, undef, undef, 0.5);
$currlogsize = (stat $info{'logfile'})[7] if $info{'logfile'} && -r $info{'logfile'};
if (cluster_port_running $version, $cluster, $info{'port'}) {
$success = 1;
last;
}
# if the postmaster wrote something, but the process does not exist any
# more, there must be a problem and we can stop immediately
last if ($currlogsize > $logsize && !check_running_postmaster $pidfile);
}
# OK, the server runs, now wait until it stabilized
if ($success) {
$success = cluster_port_ready $version, $cluster, $info{'port'}, $info{'socketdir'};
}
if ($success) {
if ($version lt '8.1' && $info{'avac_enable'} && $pg_autovacuum) {
autovacuum_start;
}
} else {
if ($logsize) {
print STDERR "The PostgreSQL server failed to start. Please check the log output:\n";
open LOG, $info{'logfile'} or
error "Could not open log file " . $info{'logfile'};
seek LOG, $logsize, SEEK_SET;
print STDERR $_ while <LOG>;
} else {
print STDERR "The PostgreSQL server failed to start. Please check the log output.\n";
}
exit 1;
}
}
sub stop {
autovacuum_stop if $pg_autovacuum;
stop_check_pid_file;
if ($force) {
if (!fork()) {
close STDOUT;
exec $pg_ctl, '-D', $info{'pgdata'}, '-s', '-w', '-m', 'fast', 'stop';
} else {
wait;
}
# try harder if "fast" mode does not work
if (-f $info{'pgdata'}.'/postmaster.pid') {
print "(does not shutdown gracefully, now stopping immediately)";
system $pg_ctl, '-D', $info{'pgdata'}, '-s', '-w', '-m', 'immediate', 'stop';
}
# if that still not helps, use the big hammer
if (-f $info{'pgdata'}.'/postmaster.pid') {
print "(does not shutdown, killing the process)";
$pid = get_running_pid $info{'pgdata'}.'/postmaster.pid';
kill (9, $pid) if $pid;
unlink $info{'pgdata'}.'/postmaster.pid';
}
} else {
if (!fork()) {
close STDOUT;
exec $pg_ctl, '-D', $info{'pgdata'}, '-s', '-w', 'stop';
} else {
wait;
}
}
# external_pid_file files are currently not removed by postmaster itself
unlink "/var/run/postgresql/$version-$cluster.pid";
}
sub restart {
stop if $info{'running'};
start;
}
sub reload {
exec $pg_ctl, '-D', $info{'pgdata'}, '-s', 'reload';
}
#
# main
#
exit 1 unless GetOptions ('o|options=s' => \@postmaster_auxoptions,
'f|force' => \$force);
if ($#ARGV != 2) {
print "Usage: $0 <version> <cluster> <action>\n";
exit 1;
}
($version, $cluster, $action) = @ARGV;
($version) = $version =~ /^(\d+\.\d+)$/; # untaint
($cluster) = $cluster =~ /^([^'"\s]+)$/; # untaint
error 'specified cluster does not exist' unless $version && $cluster && cluster_exists $version, $cluster;
%info = cluster_info ($version, $cluster);
unless ($action eq 'stop' || $action eq 'autovac-stop') {
error 'Cluster is disabled' if $info{'start'} eq 'disabled';
}
# untaint environment
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
unless (-d $info{'pgdata'} && defined $info{'owneruid'}) {
error $info{'pgdata'} . ' is not accessible or does not exist';
}
# check that owner uig/gid is valid
unless (getpwuid $info{'owneruid'}) {
error 'The cluster is owned by user id '.$info{'owneruid'}.' which does not exist any more'
}
unless (getgrgid $info{'ownergid'}) {
error 'The cluster is owned by group id '.$info{'ownergid'}.' which does not exist any more'
}
if ($> == 0) {
chdir ('/var/lib/postgresql');
change_ugid $info{'owneruid'}, $info{'ownergid'};
}
if( $> != $info{'owneruid'} ) {
error 'You must run this program as the cluster owner ('.
(getpwuid $info{'owneruid'})[0].')';
}
$pg_ctl = get_program_path 'pg_ctl', $version;
$pg_autovacuum = get_program_path 'pg_autovacuum', $version;
%actions = ('start' => \&start, 'stop' => \&stop, 'reload' => \&reload,
'restart' => \&restart,
'autovac-start' => \&autovacuum_start,
'autovac-stop' => \&autovacuum_stop,
'autovac-restart' => \&autovacuum_restart);
if ($actions{$action}) {
$actions{$action}->();
} else {
error 'Error: invalid action (must be one of: '.
(join ', ', keys %actions);
}
__END__
=head1 NAME
pg_ctlcluster - start/stop/restart/reload a PostgreSQL cluster
=head1 SYNOPSIS
B<pg_ctlcluster> [I<options>] I<cluster-version> I<cluster-name> I<action>
where I<action> = B<start>|B<stop>|B<restart>|B<reload>|B<autovac-start>|B<autovac-stop>|B<autovac-restart>
=head1 DESCRIPTION
This program controls the B<postmaster> server for a particular cluster. It
essentially wraps the L<pg_ctl(1)> command. It determines the cluster version
and data path and calls the right version of B<pg_ctl> with appropriate
configuration parameters and paths.
You have to start this program as the user who owns the database cluster or as
root.
=head1 ACTIONS
=over 4
=item B<start>
A log file for this specific cluster is created if it does not exist yet (by
default,
C</var/log/postgresql/postgresql->I<cluster-version>C<->I<cluster-name>C<.log>),
and a PostreSQL server process (L<postmaster(1)>) is started on it. If the
package B<postgresql-contrib->I<version> is installed, a B<pg_autovacuum>
process is started as well (unless this gets disabled in
C</etc/postgresql-common/autovacuum.conf> or a cluster-specific
C<autovacuum.conf> file). Please note that server version 8.1 and above does
internal autovacuuming. Exits with 0 on success, with 2 if the server is
already running, and with 1 on other failure conditions.
=item B<stop>
Stops the L<postmaster(1)> server (and B<pg_autovacuum>, if running) of the
given cluster. By default, "smart" mode is used, which waits until all clients
disconnected.
With the B<--force> option the "fast" mode is used which rolls back all active
transactions, disconnects clients immediately and thus shuts down cleanly. If
that does not work, shutdown is attempted again in "immediate" mode, which can
leave the cluster in an inconsistent state and thus will lead to a recovery run
at the next start. If this still does not help, the B<postmaster> process is
killed. Exits with 0 on success, with 2 if the server is not running, and with
1 on other failure conditions. This mode should only be used when the machine
is about to be shut down.
=item B<restart>
Stops the server if it is running and starts it (again). If B<pg_autovacuum> is
running on the server, it is restarted as well.
=item B<reload>
Causes the configuration files to be re-read without a full shutdown of the
server.
=item B<autovac-start>
Starts a B<pg_autovacuum> process for an already running cluster. This normally
happens automatically along with B<start>. This command fails for PostgreSQL 8.1
and above since they do autovacuuming internally.
=item B<autovac-stop>
Stops the B<pg_autovacuum> process for a running cluster. This normally happens
automatically along with B<stop>. This command fails for PostgreSQL 8.1 and above
since they do autovacuuming internally.
=item B<autovac-restart>
Restarts a B<pg_autovacuum> process for an already running cluster. This
normally happens automatically along with B<restart>. This command fails for
PostgreSQL 8.1 and above since they do autovacuuming internally.
=back
=head1 OPTIONS
=over 4
=item B<-o> I<option>
Pass given I<option> as command line option to the C<postmaster> process. It is
possible to specify B<-o> multiple times. See L<postmaster(1)> for a
description of valid options.
=back
=head1 SEE ALSO
L<pg_ctl(1)>, L<pg_wrapper(1)>, L<pg_lsclusters(1)>, L<postmaster(1)>
=head1 AUTHOR
Martin Pitt L<E<lt>mpitt@debian.orgE<gt>>
|