PostgreSQL Source Code  git master
pg_ctl.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "getopt_long.h"
#include "utils/pidfile.h"
Include dependency graph for pg_ctl.c:

Go to the source code of this file.

Macros

#define DEFAULT_WAIT   60
 
#define USEC_PER_SEC   1000000
 
#define WAITS_PER_SEC   10 /* should divide USEC_PER_SEC evenly */
 

Typedefs

typedef long pgpid_t
 

Enumerations

enum  ShutdownMode { SMART_MODE, FAST_MODE, IMMEDIATE_MODE }
 
enum  WaitPMResult { POSTMASTER_READY, POSTMASTER_STILL_STARTING, POSTMASTER_FAILED }
 
enum  CtlCommand {
  NO_COMMAND = 0, INIT_COMMAND, START_COMMAND, STOP_COMMAND,
  RESTART_COMMAND, RELOAD_COMMAND, STATUS_COMMAND, PROMOTE_COMMAND,
  KILL_COMMAND, REGISTER_COMMAND, UNREGISTER_COMMAND, RUN_AS_SERVICE_COMMAND
}
 

Functions

static void write_stderr (const char *fmt,...) pg_attribute_printf(1
 
static void static void do_advice (void)
 
static void do_help (void)
 
static void set_mode (char *modeopt)
 
static void set_sig (char *signame)
 
static void do_init (void)
 
static void do_start (void)
 
static void do_stop (void)
 
static void do_restart (void)
 
static void do_reload (void)
 
static void do_status (void)
 
static void do_promote (void)
 
static void do_kill (pgpid_t pid)
 
static void print_msg (const char *msg)
 
static void adjust_data_dir (void)
 
static pgpid_t get_pgpid (bool is_status_request)
 
static char ** readfile (const char *path, int *numlines)
 
static void free_readfile (char **optlines)
 
static pgpid_t start_postmaster (void)
 
static void read_post_opts (void)
 
static WaitPMResult wait_for_postmaster (pgpid_t pm_pid, bool do_checkpoint)
 
static bool postmaster_is_alive (pid_t pid)
 
static DBState get_control_dbstate (void)
 
static char * find_other_exec_or_die (const char *argv0, const char *target, const char *versionstr)
 
int main (int argc, char **argv)
 

Variables

static bool do_wait = true
 
static int wait_seconds = DEFAULT_WAIT
 
static bool wait_seconds_arg = false
 
static bool silent_mode = false
 
static ShutdownMode shutdown_mode = FAST_MODE
 
static int sig = SIGINT
 
static CtlCommand ctl_command = NO_COMMAND
 
static char * pg_data = NULL
 
static char * pg_config = NULL
 
static char * pgdata_opt = NULL
 
static char * post_opts = NULL
 
static const char * progname
 
static char * log_file = NULL
 
static char * exec_path = NULL
 
static char * event_source = NULL
 
static char * register_servicename = "PostgreSQL"
 
static char * register_username = NULL
 
static char * register_password = NULL
 
static char * argv0 = NULL
 
static bool allow_core_files = false
 
static time_t start_time
 
static char postopts_file [MAXPGPATH]
 
static char version_file [MAXPGPATH]
 
static char pid_file [MAXPGPATH]
 
static char backup_file [MAXPGPATH]
 
static char promote_file [MAXPGPATH]
 

Macro Definition Documentation

◆ DEFAULT_WAIT

#define DEFAULT_WAIT   60

Definition at line 69 of file pg_ctl.c.

Referenced by do_kill().

◆ USEC_PER_SEC

#define USEC_PER_SEC   1000000

Definition at line 71 of file pg_ctl.c.

Referenced by do_promote(), do_restart(), do_stop(), and wait_for_postmaster().

◆ WAITS_PER_SEC

#define WAITS_PER_SEC   10 /* should divide USEC_PER_SEC evenly */

Definition at line 73 of file pg_ctl.c.

Referenced by do_promote(), do_restart(), do_stop(), and wait_for_postmaster().

Typedef Documentation

◆ pgpid_t

Definition at line 36 of file pg_ctl.c.

Enumeration Type Documentation

◆ CtlCommand

enum CtlCommand
Enumerator
NO_COMMAND 
INIT_COMMAND 
START_COMMAND 
STOP_COMMAND 
RESTART_COMMAND 
RELOAD_COMMAND 
STATUS_COMMAND 
PROMOTE_COMMAND 
KILL_COMMAND 
REGISTER_COMMAND 
UNREGISTER_COMMAND 
RUN_AS_SERVICE_COMMAND 

Definition at line 53 of file pg_ctl.c.

◆ ShutdownMode

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 39 of file pg_ctl.c.

40 {
41  SMART_MODE,
42  FAST_MODE,
44 } ShutdownMode;
ShutdownMode
Definition: pg_ctl.c:39

◆ WaitPMResult

Enumerator
POSTMASTER_READY 
POSTMASTER_STILL_STARTING 
POSTMASTER_FAILED 

Definition at line 46 of file pg_ctl.c.

Function Documentation

◆ adjust_data_dir()

static void adjust_data_dir ( void  )
static

Definition at line 2059 of file pg_ctl.c.

References _, argv0, canonicalize_path(), exec_path, fd(), filename, find_other_exec_or_die(), free, MAXPGPATH, my_exec_path, PG_BACKEND_VERSIONSTR, pg_config, pg_data, pg_strdup(), pgdata_opt, post_opts, progname, snprintf(), and write_stderr().

Referenced by main().

2060 {
2061  char cmd[MAXPGPATH],
2063  *my_exec_path;
2064  FILE *fd;
2065 
2066  /* do nothing if we're working without knowledge of data dir */
2067  if (pg_config == NULL)
2068  return;
2069 
2070  /* If there is no postgresql.conf, it can't be a config-only dir */
2071  snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2072  if ((fd = fopen(filename, "r")) == NULL)
2073  return;
2074  fclose(fd);
2075 
2076  /* If PG_VERSION exists, it can't be a config-only dir */
2077  snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2078  if ((fd = fopen(filename, "r")) != NULL)
2079  {
2080  fclose(fd);
2081  return;
2082  }
2083 
2084  /* Must be a configuration directory, so find the data directory */
2085 
2086  /* we use a private my_exec_path to avoid interfering with later uses */
2087  if (exec_path == NULL)
2088  my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR);
2089  else
2090  my_exec_path = pg_strdup(exec_path);
2091 
2092  /* it's important for -C to be the first option, see main.c */
2093  snprintf(cmd, MAXPGPATH, "\"%s\" -C data_directory %s%s",
2094  my_exec_path,
2095  pgdata_opt ? pgdata_opt : "",
2096  post_opts ? post_opts : "");
2097 
2098  fd = popen(cmd, "r");
2099  if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)
2100  {
2101  write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2102  exit(1);
2103  }
2104  pclose(fd);
2105  free(my_exec_path);
2106 
2107  /* Remove trailing newline */
2108  if (strchr(filename, '\n') != NULL)
2109  *strchr(filename, '\n') = '\0';
2110 
2111  free(pg_data);
2112  pg_data = pg_strdup(filename);
2114 }
static char * argv0
Definition: pg_ctl.c:93
static char * post_opts
Definition: pg_ctl.c:85
void canonicalize_path(char *path)
Definition: path.c:254
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static char * pg_data
Definition: pg_ctl.c:82
#define MAXPGPATH
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
static char * pgdata_opt
Definition: pg_ctl.c:84
#define PG_BACKEND_VERSIONSTR
Definition: port.h:111
#define free(a)
Definition: header.h:65
static char * exec_path
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static char * filename
Definition: pg_dumpall.c:85
#define _(x)
Definition: elog.c:84
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
Definition: pg_ctl.c:719
static char * pg_config
Definition: pg_ctl.c:83

◆ do_advice()

static void do_advice ( void  )
static

Definition at line 1901 of file pg_ctl.c.

References _, progname, and write_stderr().

Referenced by main(), set_mode(), and set_sig().

1902 {
1903  write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
1904 }
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
#define _(x)
Definition: elog.c:84

◆ do_help()

static void do_help ( void  )
static

Definition at line 1909 of file pg_ctl.c.

References _, and progname.

Referenced by main().

1910 {
1911  printf(_("%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"), progname);
1912  printf(_("Usage:\n"));
1913  printf(_(" %s init[db] [-D DATADIR] [-s] [-o OPTIONS]\n"), progname);
1914  printf(_(" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
1915  " [-o OPTIONS] [-p PATH] [-c]\n"), progname);
1916  printf(_(" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"), progname);
1917  printf(_(" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
1918  " [-o OPTIONS] [-c]\n"), progname);
1919  printf(_(" %s reload [-D DATADIR] [-s]\n"), progname);
1920  printf(_(" %s status [-D DATADIR]\n"), progname);
1921  printf(_(" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"), progname);
1922  printf(_(" %s kill SIGNALNAME PID\n"), progname);
1923 #ifdef WIN32
1924  printf(_(" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
1925  " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"), progname);
1926  printf(_(" %s unregister [-N SERVICENAME]\n"), progname);
1927 #endif
1928 
1929  printf(_("\nCommon options:\n"));
1930  printf(_(" -D, --pgdata=DATADIR location of the database storage area\n"));
1931 #ifdef WIN32
1932  printf(_(" -e SOURCE event source for logging when running as a service\n"));
1933 #endif
1934  printf(_(" -s, --silent only print errors, no informational messages\n"));
1935  printf(_(" -t, --timeout=SECS seconds to wait when using -w option\n"));
1936  printf(_(" -V, --version output version information, then exit\n"));
1937  printf(_(" -w, --wait wait until operation completes (default)\n"));
1938  printf(_(" -W, --no-wait do not wait until operation completes\n"));
1939  printf(_(" -?, --help show this help, then exit\n"));
1940  printf(_("If the -D option is omitted, the environment variable PGDATA is used.\n"));
1941 
1942  printf(_("\nOptions for start or restart:\n"));
1943 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
1944  printf(_(" -c, --core-files allow postgres to produce core files\n"));
1945 #else
1946  printf(_(" -c, --core-files not applicable on this platform\n"));
1947 #endif
1948  printf(_(" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
1949  printf(_(" -o, --options=OPTIONS command line options to pass to postgres\n"
1950  " (PostgreSQL server executable) or initdb\n"));
1951  printf(_(" -p PATH-TO-POSTGRES normally not necessary\n"));
1952  printf(_("\nOptions for stop or restart:\n"));
1953  printf(_(" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
1954 
1955  printf(_("\nShutdown modes are:\n"));
1956  printf(_(" smart quit after all clients have disconnected\n"));
1957  printf(_(" fast quit directly, with proper shutdown (default)\n"));
1958  printf(_(" immediate quit without complete shutdown; will lead to recovery on restart\n"));
1959 
1960  printf(_("\nAllowed signal names for kill:\n"));
1961  printf(" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
1962 
1963 #ifdef WIN32
1964  printf(_("\nOptions for register and unregister:\n"));
1965  printf(_(" -N SERVICENAME service name with which to register PostgreSQL server\n"));
1966  printf(_(" -P PASSWORD password of account to register PostgreSQL server\n"));
1967  printf(_(" -U USERNAME user name of account to register PostgreSQL server\n"));
1968  printf(_(" -S START-TYPE service start type to register PostgreSQL server\n"));
1969 
1970  printf(_("\nStart types are:\n"));
1971  printf(_(" auto start service automatically during system startup (default)\n"));
1972  printf(_(" demand start service on demand\n"));
1973 #endif
1974 
1975  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
1976 }
static const char * progname
Definition: pg_ctl.c:86
#define _(x)
Definition: elog.c:84

◆ do_init()

static void do_init ( void  )
static

Definition at line 751 of file pg_ctl.c.

References _, argv0, DEVNULL, exec_path, find_other_exec_or_die(), MAXPGPATH, pgdata_opt, post_opts, progname, silent_mode, snprintf(), and write_stderr().

Referenced by main().

752 {
753  char cmd[MAXPGPATH];
754 
755  if (exec_path == NULL)
756  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
757 
758  if (pgdata_opt == NULL)
759  pgdata_opt = "";
760 
761  if (post_opts == NULL)
762  post_opts = "";
763 
764  if (!silent_mode)
765  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
767  else
768  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
770 
771  if (system(cmd) != 0)
772  {
773  write_stderr(_("%s: database system initialization failed\n"), progname);
774  exit(1);
775  }
776 }
static char * argv0
Definition: pg_ctl.c:93
static char * post_opts
Definition: pg_ctl.c:85
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXPGPATH
#define DEVNULL
Definition: port.h:123
static char * pgdata_opt
Definition: pg_ctl.c:84
static char * exec_path
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
#define _(x)
Definition: elog.c:84
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
Definition: pg_ctl.c:719
static bool silent_mode
Definition: pg_ctl.c:78

◆ do_kill()

static void do_kill ( pgpid_t  pid)
static

Definition at line 1260 of file pg_ctl.c.

References _, AddUserToTokenDacl(), appendPQExpBuffer(), argv0, createPQExpBuffer(), PQExpBufferData::data, DEFAULT_WAIT, do_wait, event_source, FALSE, find_my_exec(), find_other_exec(), free, i, kill, length(), make_absolute_path(), make_native_path(), malloc, MAXPGPATH, PG_BACKEND_VERSIONSTR, pg_config, pg_strcasecmp(), post_opts, POSTMASTER_READY, progname, read_post_opts(), register_password, register_servicename, register_username, sig, SIGHUP, silent_mode, snprintf(), status(), strerror(), TRUE, wait_for_postmaster(), wait_seconds, wait_seconds_arg, and write_stderr().

Referenced by main().

1261 {
1262  if (kill((pid_t) pid, sig) != 0)
1263  {
1264  write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
1265  progname, sig, pid, strerror(errno));
1266  exit(1);
1267  }
1268 }
#define kill(pid, sig)
Definition: win32_port.h:437
static int sig
Definition: pg_ctl.c:80
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ do_promote()

static void do_promote ( void  )
static

Definition at line 1079 of file pg_ctl.c.

References _, DB_IN_ARCHIVE_RECOVERY, DB_IN_PRODUCTION, DB_STARTUP, do_wait, get_control_dbstate(), get_pgpid(), kill, MAXPGPATH, pg_data, pg_usleep(), pid_file, print_msg(), progname, promote_file, sig, SIGUSR1, snprintf(), strerror(), USEC_PER_SEC, wait_seconds, WAITS_PER_SEC, and write_stderr().

Referenced by main().

1080 {
1081  FILE *prmfile;
1082  pgpid_t pid;
1083 
1084  pid = get_pgpid(false);
1085 
1086  if (pid == 0) /* no pid file */
1087  {
1088  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1089  write_stderr(_("Is server running?\n"));
1090  exit(1);
1091  }
1092  else if (pid < 0) /* standalone backend, not postmaster */
1093  {
1094  pid = -pid;
1095  write_stderr(_("%s: cannot promote server; "
1096  "single-user server is running (PID: %ld)\n"),
1097  progname, pid);
1098  exit(1);
1099  }
1100 
1102  {
1103  write_stderr(_("%s: cannot promote server; "
1104  "server is not in standby mode\n"),
1105  progname);
1106  exit(1);
1107  }
1108 
1109  /*
1110  * For 9.3 onwards, "fast" promotion is performed. Promotion with a full
1111  * checkpoint is still possible by writing a file called
1112  * "fallback_promote" instead of "promote"
1113  */
1114  snprintf(promote_file, MAXPGPATH, "%s/promote", pg_data);
1115 
1116  if ((prmfile = fopen(promote_file, "w")) == NULL)
1117  {
1118  write_stderr(_("%s: could not create promote signal file \"%s\": %s\n"),
1119  progname, promote_file, strerror(errno));
1120  exit(1);
1121  }
1122  if (fclose(prmfile))
1123  {
1124  write_stderr(_("%s: could not write promote signal file \"%s\": %s\n"),
1125  progname, promote_file, strerror(errno));
1126  exit(1);
1127  }
1128 
1129  sig = SIGUSR1;
1130  if (kill((pid_t) pid, sig) != 0)
1131  {
1132  write_stderr(_("%s: could not send promote signal (PID: %ld): %s\n"),
1133  progname, pid, strerror(errno));
1134  if (unlink(promote_file) != 0)
1135  write_stderr(_("%s: could not remove promote signal file \"%s\": %s\n"),
1136  progname, promote_file, strerror(errno));
1137  exit(1);
1138  }
1139 
1140  if (do_wait)
1141  {
1143  int cnt;
1144 
1145  print_msg(_("waiting for server to promote..."));
1146  for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
1147  {
1148  state = get_control_dbstate();
1149  if (state == DB_IN_PRODUCTION)
1150  break;
1151 
1152  if (cnt % WAITS_PER_SEC == 0)
1153  print_msg(".");
1154  pg_usleep(USEC_PER_SEC / WAITS_PER_SEC);
1155  }
1156  if (state == DB_IN_PRODUCTION)
1157  {
1158  print_msg(_(" done\n"));
1159  print_msg(_("server promoted\n"));
1160  }
1161  else
1162  {
1163  print_msg(_(" stopped waiting\n"));
1164  write_stderr(_("%s: server did not promote in time\n"),
1165  progname);
1166  exit(1);
1167  }
1168  }
1169  else
1170  print_msg(_("server promoting\n"));
1171 }
static int wait_seconds
Definition: pg_ctl.c:76
#define SIGUSR1
Definition: win32_port.h:177
#define kill(pid, sig)
Definition: win32_port.h:437
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * pg_data
Definition: pg_ctl.c:82
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
#define MAXPGPATH
#define USEC_PER_SEC
Definition: pg_ctl.c:71
DBState
Definition: pg_control.h:85
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:247
static int sig
Definition: pg_ctl.c:80
Definition: regguts.h:298
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:101
static bool do_wait
Definition: pg_ctl.c:75
const char * strerror(int errnum)
Definition: strerror.c:19
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2118
static void print_msg(const char *msg)
Definition: pg_ctl.c:237
#define _(x)
Definition: elog.c:84
#define WAITS_PER_SEC
Definition: pg_ctl.c:73

◆ do_reload()

static void do_reload ( void  )
static

Definition at line 1042 of file pg_ctl.c.

References _, get_pgpid(), kill, pid_file, print_msg(), progname, sig, strerror(), and write_stderr().

Referenced by main().

1043 {
1044  pgpid_t pid;
1045 
1046  pid = get_pgpid(false);
1047  if (pid == 0) /* no pid file */
1048  {
1049  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1050  write_stderr(_("Is server running?\n"));
1051  exit(1);
1052  }
1053  else if (pid < 0) /* standalone backend, not postmaster */
1054  {
1055  pid = -pid;
1056  write_stderr(_("%s: cannot reload server; "
1057  "single-user server is running (PID: %ld)\n"),
1058  progname, pid);
1059  write_stderr(_("Please terminate the single-user server and try again.\n"));
1060  exit(1);
1061  }
1062 
1063  if (kill((pid_t) pid, sig) != 0)
1064  {
1065  write_stderr(_("%s: could not send reload signal (PID: %ld): %s\n"),
1066  progname, pid, strerror(errno));
1067  exit(1);
1068  }
1069 
1070  print_msg(_("server signaled\n"));
1071 }
#define kill(pid, sig)
Definition: win32_port.h:437
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:247
static int sig
Definition: pg_ctl.c:80
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
const char * strerror(int errnum)
Definition: strerror.c:19
static void print_msg(const char *msg)
Definition: pg_ctl.c:237
#define _(x)
Definition: elog.c:84

◆ do_restart()

static void do_restart ( void  )
static

Definition at line 948 of file pg_ctl.c.

References _, backup_file, DB_IN_ARCHIVE_RECOVERY, do_start(), get_control_dbstate(), get_pgpid(), kill, pg_usleep(), pid_file, postmaster_is_alive(), print_msg(), progname, shutdown_mode, sig, SMART_MODE, stat, strerror(), USEC_PER_SEC, wait_seconds, WAITS_PER_SEC, and write_stderr().

Referenced by main().

949 {
950  int cnt;
951  pgpid_t pid;
952  struct stat statbuf;
953 
954  pid = get_pgpid(false);
955 
956  if (pid == 0) /* no pid file */
957  {
958  write_stderr(_("%s: PID file \"%s\" does not exist\n"),
959  progname, pid_file);
960  write_stderr(_("Is server running?\n"));
961  write_stderr(_("trying to start server anyway\n"));
962  do_start();
963  return;
964  }
965  else if (pid < 0) /* standalone backend, not postmaster */
966  {
967  pid = -pid;
968  if (postmaster_is_alive((pid_t) pid))
969  {
970  write_stderr(_("%s: cannot restart server; "
971  "single-user server is running (PID: %ld)\n"),
972  progname, pid);
973  write_stderr(_("Please terminate the single-user server and try again.\n"));
974  exit(1);
975  }
976  }
977 
978  if (postmaster_is_alive((pid_t) pid))
979  {
980  if (kill((pid_t) pid, sig) != 0)
981  {
982  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
983  strerror(errno));
984  exit(1);
985  }
986 
987  /*
988  * If backup_label exists, an online backup is running. Warn the user
989  * that smart shutdown will wait for it to finish. However, if the
990  * server is in archive recovery, we're recovering from an online
991  * backup instead of performing one.
992  */
993  if (shutdown_mode == SMART_MODE &&
994  stat(backup_file, &statbuf) == 0 &&
996  {
997  print_msg(_("WARNING: online backup mode is active\n"
998  "Shutdown will not complete until pg_stop_backup() is called.\n\n"));
999  }
1000 
1001  print_msg(_("waiting for server to shut down..."));
1002 
1003  /* always wait for restart */
1004 
1005  for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
1006  {
1007  if ((pid = get_pgpid(false)) != 0)
1008  {
1009  if (cnt % WAITS_PER_SEC == 0)
1010  print_msg(".");
1011  pg_usleep(USEC_PER_SEC / WAITS_PER_SEC);
1012  }
1013  else
1014  break;
1015  }
1016 
1017  if (pid != 0) /* pid file still exists */
1018  {
1019  print_msg(_(" failed\n"));
1020 
1021  write_stderr(_("%s: server does not shut down\n"), progname);
1022  if (shutdown_mode == SMART_MODE)
1023  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1024  "waiting for session-initiated disconnection.\n"));
1025  exit(1);
1026  }
1027 
1028  print_msg(_(" done\n"));
1029  print_msg(_("server stopped\n"));
1030  }
1031  else
1032  {
1033  write_stderr(_("%s: old server process (PID: %ld) seems to be gone\n"),
1034  progname, pid);
1035  write_stderr(_("starting server anyway\n"));
1036  }
1037 
1038  do_start();
1039 }
static int wait_seconds
Definition: pg_ctl.c:76
#define kill(pid, sig)
Definition: win32_port.h:437
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:79
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:100
static void do_start(void)
Definition: pg_ctl.c:779
#define USEC_PER_SEC
Definition: pg_ctl.c:71
#define stat(a, b)
Definition: win32_port.h:266
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:247
static int sig
Definition: pg_ctl.c:80
static bool postmaster_is_alive(pid_t pid)
Definition: pg_ctl.c:1179
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
const char * strerror(int errnum)
Definition: strerror.c:19
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2118
static void print_msg(const char *msg)
Definition: pg_ctl.c:237
#define _(x)
Definition: elog.c:84
#define WAITS_PER_SEC
Definition: pg_ctl.c:73

◆ do_start()

static void do_start ( void  )
static

Definition at line 779 of file pg_ctl.c.

References _, allow_core_files, argv0, ctl_command, do_wait, exec_path, find_other_exec_or_die(), get_pgpid(), PG_BACKEND_VERSIONSTR, pgdata_opt, POSTMASTER_FAILED, POSTMASTER_READY, POSTMASTER_STILL_STARTING, print_msg(), progname, putenv, read_post_opts(), RESTART_COMMAND, snprintf(), start_postmaster(), wait_for_postmaster(), and write_stderr().

Referenced by do_restart(), and main().

780 {
781  pgpid_t old_pid = 0;
782  pgpid_t pm_pid;
783 
785  {
786  old_pid = get_pgpid(false);
787  if (old_pid != 0)
788  write_stderr(_("%s: another server might be running; "
789  "trying to start server anyway\n"),
790  progname);
791  }
792 
793  read_post_opts();
794 
795  /* No -D or -D already added during server start */
796  if (ctl_command == RESTART_COMMAND || pgdata_opt == NULL)
797  pgdata_opt = "";
798 
799  if (exec_path == NULL)
801 
802 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
803  if (allow_core_files)
804  unlimit_core_size();
805 #endif
806 
807  /*
808  * If possible, tell the postmaster our parent shell's PID (see the
809  * comments in CreateLockFile() for motivation). Windows hasn't got
810  * getppid() unfortunately.
811  */
812 #ifndef WIN32
813  {
814  static char env_var[32];
815 
816  snprintf(env_var, sizeof(env_var), "PG_GRANDPARENT_PID=%d",
817  (int) getppid());
818  putenv(env_var);
819  }
820 #endif
821 
822  pm_pid = start_postmaster();
823 
824  if (do_wait)
825  {
826  print_msg(_("waiting for server to start..."));
827 
828  switch (wait_for_postmaster(pm_pid, false))
829  {
830  case POSTMASTER_READY:
831  print_msg(_(" done\n"));
832  print_msg(_("server started\n"));
833  break;
835  print_msg(_(" stopped waiting\n"));
836  write_stderr(_("%s: server did not start in time\n"),
837  progname);
838  exit(1);
839  break;
840  case POSTMASTER_FAILED:
841  print_msg(_(" stopped waiting\n"));
842  write_stderr(_("%s: could not start server\n"
843  "Examine the log output.\n"),
844  progname);
845  exit(1);
846  break;
847  }
848  }
849  else
850  print_msg(_("server starting\n"));
851 
852 #ifdef WIN32
853  /* Now we don't need the handle to the shell process anymore */
854  CloseHandle(postmasterProcess);
855  postmasterProcess = INVALID_HANDLE_VALUE;
856 #endif
857 }
static char * argv0
Definition: pg_ctl.c:93
static void read_post_opts(void)
Definition: pg_ctl.c:670
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
long pgpid_t
Definition: pg_ctl.c:36
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:247
static char * pgdata_opt
Definition: pg_ctl.c:84
#define PG_BACKEND_VERSIONSTR
Definition: port.h:111
static WaitPMResult wait_for_postmaster(pgpid_t pm_pid, bool do_checkpoint)
Definition: pg_ctl.c:538
static bool allow_core_files
Definition: pg_ctl.c:94
static char * exec_path
Definition: pg_ctl.c:88
#define putenv(x)
Definition: win32_port.h:487
static const char * progname
Definition: pg_ctl.c:86
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:443
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static bool do_wait
Definition: pg_ctl.c:75
static CtlCommand ctl_command
Definition: pg_ctl.c:81
static void print_msg(const char *msg)
Definition: pg_ctl.c:237
#define _(x)
Definition: elog.c:84
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
Definition: pg_ctl.c:719

◆ do_status()

static void do_status ( void  )
static

Definition at line 1203 of file pg_ctl.c.

References _, free_readfile(), get_pgpid(), postmaster_is_alive(), postopts_file, progname, and readfile().

Referenced by main().

1204 {
1205  pgpid_t pid;
1206 
1207  pid = get_pgpid(true);
1208  /* Is there a pid file? */
1209  if (pid != 0)
1210  {
1211  /* standalone backend? */
1212  if (pid < 0)
1213  {
1214  pid = -pid;
1215  if (postmaster_is_alive((pid_t) pid))
1216  {
1217  printf(_("%s: single-user server is running (PID: %ld)\n"),
1218  progname, pid);
1219  return;
1220  }
1221  }
1222  else
1223  /* must be a postmaster */
1224  {
1225  if (postmaster_is_alive((pid_t) pid))
1226  {
1227  char **optlines;
1228  char **curr_line;
1229  int numlines;
1230 
1231  printf(_("%s: server is running (PID: %ld)\n"),
1232  progname, pid);
1233 
1234  optlines = readfile(postopts_file, &numlines);
1235  if (optlines != NULL)
1236  {
1237  for (curr_line = optlines; *curr_line != NULL; curr_line++)
1238  puts(*curr_line);
1239 
1240  /* Free the results of readfile */
1241  free_readfile(optlines);
1242  }
1243  return;
1244  }
1245  }
1246  }
1247  printf(_("%s: no server running\n"), progname);
1248 
1249  /*
1250  * The Linux Standard Base Core Specification 3.1 says this should return
1251  * '3, program is not running'
1252  * https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1253  */
1254  exit(3);
1255 }
static char ** readfile(const char *path, int *numlines)
Definition: pg_ctl.c:315
long pgpid_t
Definition: pg_ctl.c:36
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:247
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:97
static void free_readfile(char **optlines)
Definition: pg_ctl.c:411
static bool postmaster_is_alive(pid_t pid)
Definition: pg_ctl.c:1179
static const char * progname
Definition: pg_ctl.c:86
#define _(x)
Definition: elog.c:84

◆ do_stop()

static void do_stop ( void  )
static

Definition at line 861 of file pg_ctl.c.

References _, backup_file, DB_IN_ARCHIVE_RECOVERY, do_wait, get_control_dbstate(), get_pgpid(), kill, pg_usleep(), pid_file, print_msg(), progname, shutdown_mode, sig, SMART_MODE, stat, strerror(), USEC_PER_SEC, wait_seconds, WAITS_PER_SEC, and write_stderr().

Referenced by main().

862 {
863  int cnt;
864  pgpid_t pid;
865  struct stat statbuf;
866 
867  pid = get_pgpid(false);
868 
869  if (pid == 0) /* no pid file */
870  {
871  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
872  write_stderr(_("Is server running?\n"));
873  exit(1);
874  }
875  else if (pid < 0) /* standalone backend, not postmaster */
876  {
877  pid = -pid;
878  write_stderr(_("%s: cannot stop server; "
879  "single-user server is running (PID: %ld)\n"),
880  progname, pid);
881  exit(1);
882  }
883 
884  if (kill((pid_t) pid, sig) != 0)
885  {
886  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
887  strerror(errno));
888  exit(1);
889  }
890 
891  if (!do_wait)
892  {
893  print_msg(_("server shutting down\n"));
894  return;
895  }
896  else
897  {
898  /*
899  * If backup_label exists, an online backup is running. Warn the user
900  * that smart shutdown will wait for it to finish. However, if the
901  * server is in archive recovery, we're recovering from an online
902  * backup instead of performing one.
903  */
904  if (shutdown_mode == SMART_MODE &&
905  stat(backup_file, &statbuf) == 0 &&
907  {
908  print_msg(_("WARNING: online backup mode is active\n"
909  "Shutdown will not complete until pg_stop_backup() is called.\n\n"));
910  }
911 
912  print_msg(_("waiting for server to shut down..."));
913 
914  for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
915  {
916  if ((pid = get_pgpid(false)) != 0)
917  {
918  if (cnt % WAITS_PER_SEC == 0)
919  print_msg(".");
920  pg_usleep(USEC_PER_SEC / WAITS_PER_SEC);
921  }
922  else
923  break;
924  }
925 
926  if (pid != 0) /* pid file still exists */
927  {
928  print_msg(_(" failed\n"));
929 
930  write_stderr(_("%s: server does not shut down\n"), progname);
931  if (shutdown_mode == SMART_MODE)
932  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
933  "waiting for session-initiated disconnection.\n"));
934  exit(1);
935  }
936  print_msg(_(" done\n"));
937 
938  print_msg(_("server stopped\n"));
939  }
940 }
static int wait_seconds
Definition: pg_ctl.c:76
#define kill(pid, sig)
Definition: win32_port.h:437
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:79
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:100
#define USEC_PER_SEC
Definition: pg_ctl.c:71
#define stat(a, b)
Definition: win32_port.h:266
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:247
static int sig
Definition: pg_ctl.c:80
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static bool do_wait
Definition: pg_ctl.c:75
const char * strerror(int errnum)
Definition: strerror.c:19
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2118
static void print_msg(const char *msg)
Definition: pg_ctl.c:237
#define _(x)
Definition: elog.c:84
#define WAITS_PER_SEC
Definition: pg_ctl.c:73

◆ find_other_exec_or_die()

static char* find_other_exec_or_die ( const char *  argv0,
const char *  target,
const char *  versionstr 
)
static

Definition at line 719 of file pg_ctl.c.

References _, find_my_exec(), find_other_exec(), MAXPGPATH, pg_malloc(), progname, strlcpy(), and write_stderr().

Referenced by adjust_data_dir(), do_init(), and do_start().

720 {
721  int ret;
722  char *found_path;
723 
724  found_path = pg_malloc(MAXPGPATH);
725 
726  if ((ret = find_other_exec(argv0, target, versionstr, found_path)) < 0)
727  {
728  char full_path[MAXPGPATH];
729 
730  if (find_my_exec(argv0, full_path) < 0)
731  strlcpy(full_path, progname, sizeof(full_path));
732 
733  if (ret == -1)
734  write_stderr(_("The program \"%s\" is needed by %s "
735  "but was not found in the\n"
736  "same directory as \"%s\".\n"
737  "Check your installation.\n"),
738  target, progname, full_path);
739  else
740  write_stderr(_("The program \"%s\" was found by \"%s\"\n"
741  "but was not the same version as %s.\n"
742  "Check your installation.\n"),
743  target, full_path, progname);
744  exit(1);
745  }
746 
747  return found_path;
748 }
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:307
static char * argv0
Definition: pg_ctl.c:93
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:119
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
#define _(x)
Definition: elog.c:84

◆ free_readfile()

static void free_readfile ( char **  optlines)
static

Definition at line 411 of file pg_ctl.c.

References free, and i.

Referenced by do_status(), read_post_opts(), and wait_for_postmaster().

412 {
413  char *curr_line = NULL;
414  int i = 0;
415 
416  if (!optlines)
417  return;
418 
419  while ((curr_line = optlines[i++]))
420  free(curr_line);
421 
422  free(optlines);
423 
424  return;
425 }
#define free(a)
Definition: header.h:65
int i

◆ get_control_dbstate()

static DBState get_control_dbstate ( void  )
static

Definition at line 2118 of file pg_ctl.c.

References _, get_controlfile(), pfree(), pg_data, progname, ControlFileData::state, and write_stderr().

Referenced by do_promote(), do_restart(), and do_stop().

2119 {
2120  DBState ret;
2121  bool crc_ok;
2122  ControlFileData *control_file_data = get_controlfile(pg_data, progname, &crc_ok);
2123 
2124  if (!crc_ok)
2125  {
2126  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2127  exit(1);
2128  }
2129 
2130  ret = control_file_data->state;
2131  pfree(control_file_data);
2132  return ret;
2133 }
ControlFileData * get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
static char * pg_data
Definition: pg_ctl.c:82
void pfree(void *pointer)
Definition: mcxt.c:936
DBState
Definition: pg_control.h:85
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
#define _(x)
Definition: elog.c:84

◆ get_pgpid()

static pgpid_t get_pgpid ( bool  is_status_request)
static

Definition at line 247 of file pg_ctl.c.

References _, pg_data, pid_file, progname, stat, strerror(), version_file, and write_stderr().

Referenced by do_promote(), do_reload(), do_restart(), do_start(), do_status(), and do_stop().

248 {
249  FILE *pidf;
250  long pid;
251  struct stat statbuf;
252 
253  if (stat(pg_data, &statbuf) != 0)
254  {
255  if (errno == ENOENT)
256  write_stderr(_("%s: directory \"%s\" does not exist\n"), progname,
257  pg_data);
258  else
259  write_stderr(_("%s: could not access directory \"%s\": %s\n"), progname,
260  pg_data, strerror(errno));
261 
262  /*
263  * The Linux Standard Base Core Specification 3.1 says this should
264  * return '4, program or service status is unknown'
265  * https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
266  */
267  exit(is_status_request ? 4 : 1);
268  }
269 
270  if (stat(version_file, &statbuf) != 0 && errno == ENOENT)
271  {
272  write_stderr(_("%s: directory \"%s\" is not a database cluster directory\n"),
273  progname, pg_data);
274  exit(is_status_request ? 4 : 1);
275  }
276 
277  pidf = fopen(pid_file, "r");
278  if (pidf == NULL)
279  {
280  /* No pid file, not an error on startup */
281  if (errno == ENOENT)
282  return 0;
283  else
284  {
285  write_stderr(_("%s: could not open PID file \"%s\": %s\n"),
286  progname, pid_file, strerror(errno));
287  exit(1);
288  }
289  }
290  if (fscanf(pidf, "%ld", &pid) != 1)
291  {
292  /* Is the file empty? */
293  if (ftell(pidf) == 0 && feof(pidf))
294  write_stderr(_("%s: the PID file \"%s\" is empty\n"),
295  progname, pid_file);
296  else
297  write_stderr(_("%s: invalid data in PID file \"%s\"\n"),
298  progname, pid_file);
299  exit(1);
300  }
301  fclose(pidf);
302  return (pgpid_t) pid;
303 }
static char * pg_data
Definition: pg_ctl.c:82
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
#define stat(a, b)
Definition: win32_port.h:266
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
const char * strerror(int errnum)
Definition: strerror.c:19
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:98
#define _(x)
Definition: elog.c:84

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 2137 of file pg_ctl.c.

References _, adjust_data_dir(), allow_core_files, argv0, backup_file, canonicalize_path(), ctl_command, do_advice(), do_help(), do_init(), do_kill(), do_promote(), do_reload(), do_restart(), do_start(), do_status(), do_stop(), do_wait, event_source, exec_path, free, get_progname(), getopt_long(), INIT_COMMAND, KILL_COMMAND, log_file, MAXPGPATH, no_argument, NO_COMMAND, optarg, optind, pg_config, pg_data, pg_strdup(), PG_TEXTDOMAIN, pgdata_opt, pid_file, post_opts, postopts_file, progname, PROMOTE_COMMAND, psprintf(), putenv, REGISTER_COMMAND, register_password, register_servicename, register_username, RELOAD_COMMAND, required_argument, RESTART_COMMAND, RUN_AS_SERVICE_COMMAND, S_IRWXG, S_IRWXO, set_mode(), set_pglocale_pgservice(), set_sig(), sig, SIGHUP, silent_mode, snprintf(), START_COMMAND, start_time, STATUS_COMMAND, STOP_COMMAND, UNREGISTER_COMMAND, version_file, wait_seconds, wait_seconds_arg, and write_stderr().

2138 {
2139  static struct option long_options[] = {
2140  {"help", no_argument, NULL, '?'},
2141  {"version", no_argument, NULL, 'V'},
2142  {"log", required_argument, NULL, 'l'},
2143  {"mode", required_argument, NULL, 'm'},
2144  {"pgdata", required_argument, NULL, 'D'},
2145  {"options", required_argument, NULL, 'o'},
2146  {"silent", no_argument, NULL, 's'},
2147  {"timeout", required_argument, NULL, 't'},
2148  {"core-files", no_argument, NULL, 'c'},
2149  {"wait", no_argument, NULL, 'w'},
2150  {"no-wait", no_argument, NULL, 'W'},
2151  {NULL, 0, NULL, 0}
2152  };
2153 
2154  char *env_wait;
2155  int option_index;
2156  int c;
2157  pgpid_t killproc = 0;
2158 
2159 #ifdef WIN32
2160  setvbuf(stderr, NULL, _IONBF, 0);
2161 #endif
2162 
2163  progname = get_progname(argv[0]);
2164  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));
2165  start_time = time(NULL);
2166 
2167  /*
2168  * save argv[0] so do_start() can look for the postmaster if necessary. we
2169  * don't look for postmaster here because in many cases we won't need it.
2170  */
2171  argv0 = argv[0];
2172 
2173  umask(S_IRWXG | S_IRWXO);
2174 
2175  /* support --help and --version even if invoked as root */
2176  if (argc > 1)
2177  {
2178  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2179  {
2180  do_help();
2181  exit(0);
2182  }
2183  else if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2184  {
2185  puts("pg_ctl (PostgreSQL) " PG_VERSION);
2186  exit(0);
2187  }
2188  }
2189 
2190  /*
2191  * Disallow running as root, to forestall any possible security holes.
2192  */
2193 #ifndef WIN32
2194  if (geteuid() == 0)
2195  {
2196  write_stderr(_("%s: cannot be run as root\n"
2197  "Please log in (using, e.g., \"su\") as the "
2198  "(unprivileged) user that will\n"
2199  "own the server process.\n"),
2200  progname);
2201  exit(1);
2202  }
2203 #endif
2204 
2205  env_wait = getenv("PGCTLTIMEOUT");
2206  if (env_wait != NULL)
2207  wait_seconds = atoi(env_wait);
2208 
2209  /*
2210  * 'Action' can be before or after args so loop over both. Some
2211  * getopt_long() implementations will reorder argv[] to place all flags
2212  * first (GNU?), but we don't rely on it. Our /port version doesn't do
2213  * that.
2214  */
2215  optind = 1;
2216 
2217  /* process command-line options */
2218  while (optind < argc)
2219  {
2220  while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW",
2221  long_options, &option_index)) != -1)
2222  {
2223  switch (c)
2224  {
2225  case 'D':
2226  {
2227  char *pgdata_D;
2228  char *env_var;
2229 
2230  pgdata_D = pg_strdup(optarg);
2231  canonicalize_path(pgdata_D);
2232  env_var = psprintf("PGDATA=%s", pgdata_D);
2233  putenv(env_var);
2234 
2235  /*
2236  * We could pass PGDATA just in an environment
2237  * variable but we do -D too for clearer postmaster
2238  * 'ps' display
2239  */
2240  pgdata_opt = psprintf("-D \"%s\" ", pgdata_D);
2241  break;
2242  }
2243  case 'e':
2245  break;
2246  case 'l':
2248  break;
2249  case 'm':
2250  set_mode(optarg);
2251  break;
2252  case 'N':
2254  break;
2255  case 'o':
2256  /* append option? */
2257  if (!post_opts)
2259  else
2260  {
2261  char *old_post_opts = post_opts;
2262 
2263  post_opts = psprintf("%s %s", old_post_opts, optarg);
2264  free(old_post_opts);
2265  }
2266  break;
2267  case 'p':
2269  break;
2270  case 'P':
2272  break;
2273  case 's':
2274  silent_mode = true;
2275  break;
2276  case 'S':
2277 #ifdef WIN32
2278  set_starttype(optarg);
2279 #else
2280  write_stderr(_("%s: -S option not supported on this platform\n"),
2281  progname);
2282  exit(1);
2283 #endif
2284  break;
2285  case 't':
2286  wait_seconds = atoi(optarg);
2287  wait_seconds_arg = true;
2288  break;
2289  case 'U':
2290  if (strchr(optarg, '\\'))
2292  else
2293  /* Prepend .\ for local accounts */
2294  register_username = psprintf(".\\%s", optarg);
2295  break;
2296  case 'w':
2297  do_wait = true;
2298  break;
2299  case 'W':
2300  do_wait = false;
2301  break;
2302  case 'c':
2303  allow_core_files = true;
2304  break;
2305  default:
2306  /* getopt_long already issued a suitable error message */
2307  do_advice();
2308  exit(1);
2309  }
2310  }
2311 
2312  /* Process an action */
2313  if (optind < argc)
2314  {
2315  if (ctl_command != NO_COMMAND)
2316  {
2317  write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]);
2318  do_advice();
2319  exit(1);
2320  }
2321 
2322  if (strcmp(argv[optind], "init") == 0
2323  || strcmp(argv[optind], "initdb") == 0)
2325  else if (strcmp(argv[optind], "start") == 0)
2327  else if (strcmp(argv[optind], "stop") == 0)
2329  else if (strcmp(argv[optind], "restart") == 0)
2331  else if (strcmp(argv[optind], "reload") == 0)
2333  else if (strcmp(argv[optind], "status") == 0)
2335  else if (strcmp(argv[optind], "promote") == 0)
2337  else if (strcmp(argv[optind], "kill") == 0)
2338  {
2339  if (argc - optind < 3)
2340  {
2341  write_stderr(_("%s: missing arguments for kill mode\n"), progname);
2342  do_advice();
2343  exit(1);
2344  }
2346  set_sig(argv[++optind]);
2347  killproc = atol(argv[++optind]);
2348  }
2349 #ifdef WIN32
2350  else if (strcmp(argv[optind], "register") == 0)
2352  else if (strcmp(argv[optind], "unregister") == 0)
2354  else if (strcmp(argv[optind], "runservice") == 0)
2356 #endif
2357  else
2358  {
2359  write_stderr(_("%s: unrecognized operation mode \"%s\"\n"), progname, argv[optind]);
2360  do_advice();
2361  exit(1);
2362  }
2363  optind++;
2364  }
2365  }
2366 
2367  if (ctl_command == NO_COMMAND)
2368  {
2369  write_stderr(_("%s: no operation specified\n"), progname);
2370  do_advice();
2371  exit(1);
2372  }
2373 
2374  /* Note we put any -D switch into the env var above */
2375  pg_config = getenv("PGDATA");
2376  if (pg_config)
2377  {
2381  }
2382 
2383  /* -D might point at config-only directory; if so find the real PGDATA */
2384  adjust_data_dir();
2385 
2386  /* Complain if -D needed and not provided */
2387  if (pg_config == NULL &&
2389  {
2390  write_stderr(_("%s: no database directory specified and environment variable PGDATA unset\n"),
2391  progname);
2392  do_advice();
2393  exit(1);
2394  }
2395 
2396  if (ctl_command == RELOAD_COMMAND)
2397  {
2398  sig = SIGHUP;
2399  do_wait = false;
2400  }
2401 
2402  if (pg_data)
2403  {
2404  snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data);
2405  snprintf(version_file, MAXPGPATH, "%s/PG_VERSION", pg_data);
2406  snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pg_data);
2407  snprintf(backup_file, MAXPGPATH, "%s/backup_label", pg_data);
2408  }
2409 
2410  switch (ctl_command)
2411  {
2412  case INIT_COMMAND:
2413  do_init();
2414  break;
2415  case STATUS_COMMAND:
2416  do_status();
2417  break;
2418  case START_COMMAND:
2419  do_start();
2420  break;
2421  case STOP_COMMAND:
2422  do_stop();
2423  break;
2424  case RESTART_COMMAND:
2425  do_restart();
2426  break;
2427  case RELOAD_COMMAND:
2428  do_reload();
2429  break;
2430  case PROMOTE_COMMAND:
2431  do_promote();
2432  break;
2433  case KILL_COMMAND:
2434  do_kill(killproc);
2435  break;
2436 #ifdef WIN32
2437  case REGISTER_COMMAND:
2438  pgwin32_doRegister();
2439  break;
2440  case UNREGISTER_COMMAND:
2441  pgwin32_doUnregister();
2442  break;
2444  pgwin32_doRunAsService();
2445  break;
2446 #endif
2447  default:
2448  break;
2449  }
2450 
2451  exit(0);
2452 }
static char * argv0
Definition: pg_ctl.c:93
static int wait_seconds
Definition: pg_ctl.c:76
const char * get_progname(const char *argv0)
Definition: path.c:453
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static char * log_file
Definition: pg_ctl.c:87
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void do_help(void)
Definition: pg_ctl.c:1909
static char * post_opts
Definition: pg_ctl.c:85
static void do_stop(void)
Definition: pg_ctl.c:861
void canonicalize_path(char *path)
Definition: path.c:254
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static void adjust_data_dir(void)
Definition: pg_ctl.c:2059
static char * pg_data
Definition: pg_ctl.c:82
static time_t start_time
Definition: pg_ctl.c:95
static char * register_password
Definition: pg_ctl.c:92
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:100
#define MAXPGPATH
static char * register_username
Definition: pg_ctl.c:91
static char * register_servicename
Definition: pg_ctl.c:90
char * c
static void do_start(void)
Definition: pg_ctl.c:779
#define S_IRWXG
Definition: win32_port.h:292
#define SIGHUP
Definition: win32_port.h:163
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void do_kill(pgpid_t pid)
Definition: pg_ctl.c:1260
static void do_restart(void)
Definition: pg_ctl.c:948
static void do_status(void)
Definition: pg_ctl.c:1203
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1043
static char * pgdata_opt
Definition: pg_ctl.c:84
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:97
static int sig
Definition: pg_ctl.c:80
#define free(a)
Definition: header.h:65
static void do_promote(void)
Definition: pg_ctl.c:1079
static bool allow_core_files
Definition: pg_ctl.c:94
static char * exec_path
Definition: pg_ctl.c:88
static void do_init(void)
Definition: pg_ctl.c:751
#define putenv(x)
Definition: win32_port.h:487
static char * event_source
Definition: pg_ctl.c:89
static const char * progname
Definition: pg_ctl.c:86
static bool wait_seconds_arg
Definition: pg_ctl.c:77
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
static bool do_wait
Definition: pg_ctl.c:75
static void set_mode(char *modeopt)
Definition: pg_ctl.c:1981
static void do_reload(void)
Definition: pg_ctl.c:1042
char * optarg
Definition: getopt.c:53
static void static void do_advice(void)
Definition: pg_ctl.c:1901
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:98
static CtlCommand ctl_command
Definition: pg_ctl.c:81
#define _(x)
Definition: elog.c:84
static void set_sig(char *signame)
Definition: pg_ctl.c:2009
static bool silent_mode
Definition: pg_ctl.c:78
#define S_IRWXO
Definition: win32_port.h:304
static char * pg_config
Definition: pg_ctl.c:83

◆ postmaster_is_alive()

static bool postmaster_is_alive ( pid_t  pid)
static

Definition at line 1179 of file pg_ctl.c.

References kill.

Referenced by do_restart(), and do_status().

1180 {
1181  /*
1182  * Test to see if the process is still there. Note that we do not
1183  * consider an EPERM failure to mean that the process is still there;
1184  * EPERM must mean that the given PID belongs to some other userid, and
1185  * considering the permissions on $PGDATA, that means it's not the
1186  * postmaster we are after.
1187  *
1188  * Don't believe that our own PID or parent shell's PID is the postmaster,
1189  * either. (Windows hasn't got getppid(), though.)
1190  */
1191  if (pid == getpid())
1192  return false;
1193 #ifndef WIN32
1194  if (pid == getppid())
1195  return false;
1196 #endif
1197  if (kill(pid, 0) == 0)
1198  return true;
1199  return false;
1200 }
#define kill(pid, sig)
Definition: win32_port.h:437

◆ print_msg()

static void print_msg ( const char *  msg)
static

Definition at line 237 of file pg_ctl.c.

References silent_mode.

Referenced by do_promote(), do_reload(), do_restart(), do_start(), do_stop(), and wait_for_postmaster().

238 {
239  if (!silent_mode)
240  {
241  fputs(msg, stdout);
242  fflush(stdout);
243  }
244 }
static bool silent_mode
Definition: pg_ctl.c:78

◆ read_post_opts()

static void read_post_opts ( void  )
static

Definition at line 670 of file pg_ctl.c.

References _, ctl_command, exec_path, free_readfile(), pg_strdup(), post_opts, postopts_file, progname, readfile(), RESTART_COMMAND, and write_stderr().

Referenced by do_kill(), and do_start().

671 {
672  if (post_opts == NULL)
673  {
674  post_opts = ""; /* default */
676  {
677  char **optlines;
678  int numlines;
679 
680  optlines = readfile(postopts_file, &numlines);
681  if (optlines == NULL)
682  {
683  write_stderr(_("%s: could not read file \"%s\"\n"), progname, postopts_file);
684  exit(1);
685  }
686  else if (numlines != 1)
687  {
688  write_stderr(_("%s: option file \"%s\" must have exactly one line\n"),
690  exit(1);
691  }
692  else
693  {
694  char *optline;
695  char *arg1;
696 
697  optline = optlines[0];
698 
699  /*
700  * Are we at the first option, as defined by space and
701  * double-quote?
702  */
703  if ((arg1 = strstr(optline, " \"")) != NULL)
704  {
705  *arg1 = '\0'; /* terminate so we get only program name */
706  post_opts = pg_strdup(arg1 + 1); /* point past whitespace */
707  }
708  if (exec_path == NULL)
709  exec_path = pg_strdup(optline);
710  }
711 
712  /* Free the results of readfile. */
713  free_readfile(optlines);
714  }
715  }
716 }
static char * post_opts
Definition: pg_ctl.c:85
static char ** readfile(const char *path, int *numlines)
Definition: pg_ctl.c:315
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:97
static void free_readfile(char **optlines)
Definition: pg_ctl.c:411
static char * exec_path
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static CtlCommand ctl_command
Definition: pg_ctl.c:81
#define _(x)
Definition: elog.c:84

◆ readfile()

static char ** readfile ( const char *  path,
int *  numlines 
)
static

Definition at line 315 of file pg_ctl.c.

References buffer, close, fd(), free, i, PG_BINARY, pg_malloc(), read, and stat.

Referenced by do_status(), read_post_opts(), and wait_for_postmaster().

316 {
317  int fd;
318  int nlines;
319  char **result;
320  char *buffer;
321  char *linebegin;
322  int i;
323  int n;
324  int len;
325  struct stat statbuf;
326 
327  *numlines = 0; /* in case of failure or empty file */
328 
329  /*
330  * Slurp the file into memory.
331  *
332  * The file can change concurrently, so we read the whole file into memory
333  * with a single read() call. That's not guaranteed to get an atomic
334  * snapshot, but in practice, for a small file, it's close enough for the
335  * current use.
336  */
337  fd = open(path, O_RDONLY | PG_BINARY, 0);
338  if (fd < 0)
339  return NULL;
340  if (fstat(fd, &statbuf) < 0)
341  {
342  close(fd);
343  return NULL;
344  }
345  if (statbuf.st_size == 0)
346  {
347  /* empty file */
348  close(fd);
349  result = (char **) pg_malloc(sizeof(char *));
350  *result = NULL;
351  return result;
352  }
353  buffer = pg_malloc(statbuf.st_size + 1);
354 
355  len = read(fd, buffer, statbuf.st_size + 1);
356  close(fd);
357  if (len != statbuf.st_size)
358  {
359  /* oops, the file size changed between fstat and read */
360  free(buffer);
361  return NULL;
362  }
363 
364  /*
365  * Count newlines. We expect there to be a newline after each full line,
366  * including one at the end of file. If there isn't a newline at the end,
367  * any characters after the last newline will be ignored.
368  */
369  nlines = 0;
370  for (i = 0; i < len; i++)
371  {
372  if (buffer[i] == '\n')
373  nlines++;
374  }
375 
376  /* set up the result buffer */
377  result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
378  *numlines = nlines;
379 
380  /* now split the buffer into lines */
381  linebegin = buffer;
382  n = 0;
383  for (i = 0; i < len; i++)
384  {
385  if (buffer[i] == '\n')
386  {
387  int slen = &buffer[i] - linebegin;
388  char *linebuf = pg_malloc(slen + 1);
389 
390  memcpy(linebuf, linebegin, slen);
391  /* we already dropped the \n, but get rid of any \r too */
392  if (slen > 0 && linebuf[slen - 1] == '\r')
393  slen--;
394  linebuf[slen] = '\0';
395  result[n++] = linebuf;
396  linebegin = &buffer[i + 1];
397  }
398  }
399  result[n] = NULL;
400 
401  free(buffer);
402 
403  return result;
404 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1069
#define stat(a, b)
Definition: win32_port.h:266
#define free(a)
Definition: header.h:65
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
int i
#define close(a)
Definition: win32.h:12
#define read(a, b, c)
Definition: win32.h:13

◆ set_mode()

static void set_mode ( char *  modeopt)
static

Definition at line 1981 of file pg_ctl.c.

References _, do_advice(), FAST_MODE, IMMEDIATE_MODE, progname, shutdown_mode, sig, SIGQUIT, SMART_MODE, and write_stderr().

Referenced by main().

1982 {
1983  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
1984  {
1986  sig = SIGTERM;
1987  }
1988  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
1989  {
1991  sig = SIGINT;
1992  }
1993  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
1994  {
1996  sig = SIGQUIT;
1997  }
1998  else
1999  {
2000  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2001  do_advice();
2002  exit(1);
2003  }
2004 }
#define SIGQUIT
Definition: win32_port.h:164
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:79
static int sig
Definition: pg_ctl.c:80
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static void static void do_advice(void)
Definition: pg_ctl.c:1901
#define _(x)
Definition: elog.c:84

◆ set_sig()

static void set_sig ( char *  signame)
static

Definition at line 2009 of file pg_ctl.c.

References _, do_advice(), progname, sig, SIGABRT, SIGHUP, SIGKILL, SIGQUIT, SIGUSR1, SIGUSR2, and write_stderr().

Referenced by main().

2010 {
2011  if (strcmp(signame, "HUP") == 0)
2012  sig = SIGHUP;
2013  else if (strcmp(signame, "INT") == 0)
2014  sig = SIGINT;
2015  else if (strcmp(signame, "QUIT") == 0)
2016  sig = SIGQUIT;
2017  else if (strcmp(signame, "ABRT") == 0)
2018  sig = SIGABRT;
2019  else if (strcmp(signame, "KILL") == 0)
2020  sig = SIGKILL;
2021  else if (strcmp(signame, "TERM") == 0)
2022  sig = SIGTERM;
2023  else if (strcmp(signame, "USR1") == 0)
2024  sig = SIGUSR1;
2025  else if (strcmp(signame, "USR2") == 0)
2026  sig = SIGUSR2;
2027  else
2028  {
2029  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2030  do_advice();
2031  exit(1);
2032  }
2033 }
#define SIGQUIT
Definition: win32_port.h:164
#define SIGUSR1
Definition: win32_port.h:177
#define SIGUSR2
Definition: win32_port.h:178
#define SIGABRT
Definition: win32_port.h:166
#define SIGKILL
Definition: win32_port.h:167
#define SIGHUP
Definition: win32_port.h:163
static int sig
Definition: pg_ctl.c:80
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static void static void do_advice(void)
Definition: pg_ctl.c:1901
#define _(x)
Definition: elog.c:84

◆ start_postmaster()

static pgpid_t start_postmaster ( void  )
static

Definition at line 443 of file pg_ctl.c.

References _, DEVNULL, exec_path, log_file, MAXPGPATH, pgdata_opt, post_opts, progname, snprintf(), strerror(), and write_stderr().

Referenced by check_and_dump_old_cluster(), do_start(), issue_warnings_and_set_wal_level(), main(), and setup().

444 {
445  char cmd[MAXPGPATH];
446 
447 #ifndef WIN32
448  pgpid_t pm_pid;
449 
450  /* Flush stdio channels just before fork, to avoid double-output problems */
451  fflush(stdout);
452  fflush(stderr);
453 
454  pm_pid = fork();
455  if (pm_pid < 0)
456  {
457  /* fork failed */
458  write_stderr(_("%s: could not start server: %s\n"),
459  progname, strerror(errno));
460  exit(1);
461  }
462  if (pm_pid > 0)
463  {
464  /* fork succeeded, in parent */
465  return pm_pid;
466  }
467 
468  /* fork succeeded, in child */
469 
470  /*
471  * Since there might be quotes to handle here, it is easier simply to pass
472  * everything to a shell to process them. Use exec so that the postmaster
473  * has the same PID as the current child process.
474  */
475  if (log_file != NULL)
476  snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
478  DEVNULL, log_file);
479  else
480  snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" 2>&1",
482 
483  (void) execl("/bin/sh", "/bin/sh", "-c", cmd, (char *) NULL);
484 
485  /* exec failed */
486  write_stderr(_("%s: could not start server: %s\n"),
487  progname, strerror(errno));
488  exit(1);
489 
490  return 0; /* keep dumb compilers quiet */
491 
492 #else /* WIN32 */
493 
494  /*
495  * As with the Unix case, it's easiest to use the shell (CMD.EXE) to
496  * handle redirection etc. Unfortunately CMD.EXE lacks any equivalent of
497  * "exec", so we don't get to find out the postmaster's PID immediately.
498  */
499  PROCESS_INFORMATION pi;
500 
501  if (log_file != NULL)
502  snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
504  else
505  snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
507 
508  if (!CreateRestrictedProcess(cmd, &pi, false))
509  {
510  write_stderr(_("%s: could not start server: error code %lu\n"),
511  progname, (unsigned long) GetLastError());
512  exit(1);
513  }
514  /* Don't close command process handle here; caller must do so */
515  postmasterProcess = pi.hProcess;
516  CloseHandle(pi.hThread);
517  return pi.dwProcessId; /* Shell's PID, not postmaster's! */
518 #endif /* WIN32 */
519 }
static char * log_file
Definition: pg_ctl.c:87
static char * post_opts
Definition: pg_ctl.c:85
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
long pgpid_t
Definition: pg_ctl.c:36
#define MAXPGPATH
#define DEVNULL
Definition: port.h:123
static char * pgdata_opt
Definition: pg_ctl.c:84
static char * exec_path
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:86
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ wait_for_postmaster()

static WaitPMResult wait_for_postmaster ( pgpid_t  pm_pid,
bool  do_checkpoint 
)
static

Definition at line 538 of file pg_ctl.c.

References _, free_readfile(), i, LOCK_FILE_LINE_PID, LOCK_FILE_LINE_PM_STATUS, LOCK_FILE_LINE_START_TIME, pg_usleep(), pid_file, PM_STATUS_READY, PM_STATUS_STANDBY, POSTMASTER_FAILED, POSTMASTER_READY, POSTMASTER_STILL_STARTING, print_msg(), progname, readfile(), start_time, status(), USEC_PER_SEC, wait_seconds, WAITS_PER_SEC, and write_stderr().

Referenced by do_kill(), and do_start().

539 {
540  int i;
541 
542  for (i = 0; i < wait_seconds * WAITS_PER_SEC; i++)
543  {
544  char **optlines;
545  int numlines;
546 
547  /*
548  * Try to read the postmaster.pid file. If it's not valid, or if the
549  * status line isn't there yet, just keep waiting.
550  */
551  if ((optlines = readfile(pid_file, &numlines)) != NULL &&
552  numlines >= LOCK_FILE_LINE_PM_STATUS)
553  {
554  /* File is complete enough for us, parse it */
555  pgpid_t pmpid;
556  time_t pmstart;
557 
558  /*
559  * Make sanity checks. If it's for the wrong PID, or the recorded
560  * start time is before pg_ctl started, then either we are looking
561  * at the wrong data directory, or this is a pre-existing pidfile
562  * that hasn't (yet?) been overwritten by our child postmaster.
563  * Allow 2 seconds slop for possible cross-process clock skew.
564  */
565  pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]);
566  pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]);
567  if (pmstart >= start_time - 2 &&
568 #ifndef WIN32
569  pmpid == pm_pid
570 #else
571  /* Windows can only reject standalone-backend PIDs */
572  pmpid > 0
573 #endif
574  )
575  {
576  /*
577  * OK, seems to be a valid pidfile from our child. Check the
578  * status line (this assumes a v10 or later server).
579  */
580  char *pmstatus = optlines[LOCK_FILE_LINE_PM_STATUS - 1];
581 
582  if (strcmp(pmstatus, PM_STATUS_READY) == 0 ||
583  strcmp(pmstatus, PM_STATUS_STANDBY) == 0)
584  {
585  /* postmaster is done starting up */
586  free_readfile(optlines);
587  return POSTMASTER_READY;
588  }
589  }
590  }
591 
592  /*
593  * Free the results of readfile.
594  *
595  * This is safe to call even if optlines is NULL.
596  */
597  free_readfile(optlines);
598 
599  /*
600  * Check whether the child postmaster process is still alive. This
601  * lets us exit early if the postmaster fails during startup.
602  *
603  * On Windows, we may be checking the postmaster's parent shell, but
604  * that's fine for this purpose.
605  */
606 #ifndef WIN32
607  {
608  int exitstatus;
609 
610  if (waitpid((pid_t) pm_pid, &exitstatus, WNOHANG) == (pid_t) pm_pid)
611  return POSTMASTER_FAILED;
612  }
613 #else
614  if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0)
615  return POSTMASTER_FAILED;
616 #endif
617 
618  /* Startup still in process; wait, printing a dot once per second */
619  if (i % WAITS_PER_SEC == 0)
620  {
621 #ifdef WIN32
622  if (do_checkpoint)
623  {
624  /*
625  * Increment the wait hint by 6 secs (connection timeout +
626  * sleep). We must do this to indicate to the SCM that our
627  * startup time is changing, otherwise it'll usually send a
628  * stop signal after 20 seconds, despite incrementing the
629  * checkpoint counter.
630  */
631  status.dwWaitHint += 6000;
632  status.dwCheckPoint++;
633  SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
634  }
635  else
636 #endif
637  print_msg(".");
638  }
639 
640  pg_usleep(USEC_PER_SEC / WAITS_PER_SEC);
641  }
642 
643  /* out of patience; report that postmaster is still starting up */
645 }
#define LOCK_FILE_LINE_PID
Definition: pidfile.h:36
static int wait_seconds
Definition: pg_ctl.c:76
static char ** readfile(const char *path, int *numlines)
Definition: pg_ctl.c:315
#define LOCK_FILE_LINE_START_TIME
Definition: pidfile.h:38
static time_t start_time
Definition: pg_ctl.c:95
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:99
long pgpid_t
Definition: pg_ctl.c:36
#define PM_STATUS_READY
Definition: pidfile.h:52
#define USEC_PER_SEC
Definition: pg_ctl.c:71
static void free_readfile(char **optlines)
Definition: pg_ctl.c:411
#define PM_STATUS_STANDBY
Definition: pidfile.h:53
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define LOCK_FILE_LINE_PM_STATUS
Definition: pidfile.h:43
static void print_msg(const char *msg)
Definition: pg_ctl.c:237
#define WAITS_PER_SEC
Definition: pg_ctl.c:73

◆ write_stderr()

static void write_stderr ( const char *  fmt,
  ... 
)
static

Definition at line 203 of file pg_ctl.c.

References pgwin32_is_service(), and vsnprintf().

Referenced by adjust_data_dir(), do_advice(), do_init(), do_kill(), do_promote(), do_reload(), do_restart(), do_start(), do_stop(), find_other_exec_or_die(), get_control_dbstate(), get_pgpid(), main(), read_post_opts(), set_mode(), set_sig(), start_postmaster(), and wait_for_postmaster().

204 {
205  va_list ap;
206 
207  va_start(ap, fmt);
208 #ifndef WIN32
209  /* On Unix, we just fprintf to stderr */
210  vfprintf(stderr, fmt, ap);
211 #else
212 
213  /*
214  * On Win32, we print to stderr if running on a console, or write to
215  * eventlog if running as a service
216  */
217  if (pgwin32_is_service()) /* Running as a service */
218  {
219  char errbuf[2048]; /* Arbitrary size? */
220 
221  vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
222 
223  write_eventlog(EVENTLOG_ERROR_TYPE, errbuf);
224  }
225  else
226  /* Not running as service, write to stderr */
227  vfprintf(stderr, fmt, ap);
228 #endif
229  va_end(ap);
230 }
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
int pgwin32_is_service(void)

Variable Documentation

◆ allow_core_files

bool allow_core_files = false
static

Definition at line 94 of file pg_ctl.c.

Referenced by do_start(), and main().

◆ argv0

char* argv0 = NULL
static

Definition at line 93 of file pg_ctl.c.

Referenced by adjust_data_dir(), do_init(), do_kill(), do_start(), and main().

◆ backup_file

char backup_file[MAXPGPATH]
static

Definition at line 100 of file pg_ctl.c.

Referenced by do_restart(), do_stop(), and main().

◆ ctl_command

CtlCommand ctl_command = NO_COMMAND
static

Definition at line 81 of file pg_ctl.c.

Referenced by do_start(), main(), and read_post_opts().

◆ do_wait

bool do_wait = true
static

Definition at line 75 of file pg_ctl.c.

Referenced by do_kill(), do_promote(), do_start(), do_stop(), main(), and WaitForWorkers().

◆ event_source

char* event_source = NULL
static

Definition at line 89 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ exec_path

char* exec_path = NULL
static

◆ log_file

char* log_file = NULL
static

Definition at line 87 of file pg_ctl.c.

Referenced by main(), and start_postmaster().

◆ pg_config

char* pg_config = NULL
static

Definition at line 83 of file pg_ctl.c.

Referenced by adjust_data_dir(), do_kill(), and main().

◆ pg_data

char* pg_data = NULL
static

Definition at line 82 of file pg_ctl.c.

Referenced by adjust_data_dir(), do_promote(), get_control_dbstate(), get_pgpid(), and main().

◆ pgdata_opt

char* pgdata_opt = NULL
static

Definition at line 84 of file pg_ctl.c.

Referenced by adjust_data_dir(), do_init(), do_start(), main(), and start_postmaster().

◆ pid_file

char pid_file[MAXPGPATH]
static

Definition at line 99 of file pg_ctl.c.

Referenced by do_promote(), do_reload(), do_restart(), do_stop(), get_pgpid(), main(), and wait_for_postmaster().

◆ post_opts

char* post_opts = NULL
static

Definition at line 85 of file pg_ctl.c.

Referenced by adjust_data_dir(), do_init(), do_kill(), main(), read_post_opts(), and start_postmaster().

◆ postopts_file

char postopts_file[MAXPGPATH]
static

Definition at line 97 of file pg_ctl.c.

Referenced by do_status(), main(), and read_post_opts().

◆ progname

◆ promote_file

char promote_file[MAXPGPATH]
static

Definition at line 101 of file pg_ctl.c.

Referenced by do_promote().

◆ register_password

char* register_password = NULL
static

Definition at line 92 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ register_servicename

char* register_servicename = "PostgreSQL"
static

Definition at line 90 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ register_username

char* register_username = NULL
static

Definition at line 91 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ shutdown_mode

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 79 of file pg_ctl.c.

Referenced by do_restart(), do_stop(), and set_mode().

◆ sig

◆ silent_mode

bool silent_mode = false
static

Definition at line 78 of file pg_ctl.c.

Referenced by do_init(), do_kill(), main(), and print_msg().

◆ start_time

◆ version_file

char version_file[MAXPGPATH]
static

Definition at line 98 of file pg_ctl.c.

Referenced by get_pgpid(), main(), and write_version_file().

◆ wait_seconds

int wait_seconds = DEFAULT_WAIT
static

◆ wait_seconds_arg

bool wait_seconds_arg = false
static

Definition at line 77 of file pg_ctl.c.

Referenced by do_kill(), and main().