0
  7   /ˆl–Ãa¾”*i?u8 Y¸î41hß�þ[ŒYÇ 'št’ºÈüÿÙ„,�ÏØÄÖ ?÷     /#!/usr/bin/perl -wT

# multiversion/cluster aware pg_ctl wrapper; this also supplies the correct
# configuration parameters to 'start', and makes sure that postgres really
# stops on 'stop'.
#
# (C) 2005-2009 Martin Pitt <mpitt@debian.org>
# (C) 2009 Cyril Bouthors <cyril@bouthors.org>
# (C) 2013-2021 Christoph Berg <myon@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

use strict;
use warnings;
use Getopt::Long;
use POSIX qw/setsid dup2 :sys_wait_h/;
use PgCommon;
use Fcntl qw(SEEK_SET O_RDWR O_CREAT O_EXCL);
use POSIX qw(lchown);

my ($version, $cluster, $pg_ctl, $force);
my (@postgres_auxoptions, @pg_ctl_opts_from_cli);
my (%postgresql_conf, %info);
my $mode = 'fast'; # default shutdown mode
my $foreground = 0; # don't daemonize, use postgres instead of pg_ctl
my $stdlog = 0; # when run in foreground, still log to the default logfile

# untaint environment
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
chdir '/';

# 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 postgres.
sub start_check_pid_file {
    my $pidfile = $info{'pgdata'}.'/postmaster.pid';
    if (PgCommon::check_pidfile_running $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 = read_pidfile $pidfile;
    return if (defined $pid && PgCommon::check_pidfile_running $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
    my $nopwoption = $v >= 8.4 ? '-w' : '';
    if ($v < 8.4) {
        $ENV{'PGPASSWORD'} = 'foo'; # prevent hangs if superuser cannot connect without password
    }
    my $out;
    while ($n < ($result ? 10 : 3)) {
        select undef, undef, undef, 0.5;
        $out = `LC_MESSAGES=C $psql -h '$sd' --port $p $nopwoption -Xc '' template1 2>&1 >/dev/null`;

        if ($? == $result) {
            $n++;
        } else {
            $n = 0;
        }
        $result = $?;
    }

    if ($out =~ /FATAL:|no password supplied/) {
	print STDERR "Warning: connection to the database failed, disabling startup checks:\n$out\n";
	return cluster_port_running $v, $c, $p;
    }
    return !$result;
}

sub start {
    start_check_pid_file;

    # check conflicting port
    if (cluster_port_running $version, $cluster, $info{'port'}) {
        my $sockdir = get_cluster_socketdir $version, $cluster;
        error("Port conflict: another instance is already running on $sockdir with port $info{'port'}");
    }

    # prepare environment (empty except for content of 'environment', and LANG)
    %ENV = read_cluster_conf_file $version, $cluster, 'environment';
    # 