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 "common/file_perm.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 70 of file pg_ctl.c.

Referenced by do_kill().

◆ USEC_PER_SEC

#define USEC_PER_SEC   1000000

Definition at line 72 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 74 of file pg_ctl.c.

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

Typedef Documentation

◆ pgpid_t

Definition at line 37 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 54 of file pg_ctl.c.

◆ ShutdownMode

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 40 of file pg_ctl.c.

41 {
42  SMART_MODE,
43  FAST_MODE,
45 } ShutdownMode;
ShutdownMode
Definition: pg_ctl.c:40

◆ WaitPMResult

Enumerator
POSTMASTER_READY 
POSTMASTER_STILL_STARTING 
POSTMASTER_FAILED 

Definition at line 47 of file pg_ctl.c.

Function Documentation

◆ adjust_data_dir()

static void adjust_data_dir ( void  )
static

Definition at line 2060 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().

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

◆ do_advice()

static void do_advice ( void  )
static

Definition at line 1902 of file pg_ctl.c.

References _, progname, and write_stderr().

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

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

◆ do_help()

static void do_help ( void  )
static

Definition at line 1910 of file pg_ctl.c.

References _, and progname.

Referenced by main().

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

◆ do_init()

static void do_init ( void  )
static

Definition at line 752 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().

753 {
754  char cmd[MAXPGPATH];
755 
756  if (exec_path == NULL)
757  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
758 
759  if (pgdata_opt == NULL)
760  pgdata_opt = "";
761 
762  if (post_opts == NULL)
763  post_opts = "";
764 
765  if (!silent_mode)
766  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
768  else
769  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
771 
772  if (system(cmd) != 0)
773  {
774  write_stderr(_("%s: database system initialization failed\n"), progname);
775  exit(1);
776  }
777 }
static char * argv0
Definition: pg_ctl.c:94
static char * post_opts
Definition: pg_ctl.c:86
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:85
static char * exec_path
Definition: pg_ctl.c:89
static const char * progname
Definition: pg_ctl.c:87
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:204
#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:720
static bool silent_mode
Definition: pg_ctl.c:79

◆ do_kill()

static void do_kill ( pgpid_t  pid)
static

Definition at line 1261 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().

1262 {
1263  if (kill((pid_t) pid, sig) != 0)
1264  {
1265  write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
1266  progname, sig, pid, strerror(errno));
1267  exit(1);
1268  }
1269 }
#define kill(pid, sig)
Definition: win32_port.h:437
static int sig
Definition: pg_ctl.c:81
static const char * progname
Definition: pg_ctl.c:87
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:204
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 1080 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().

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

◆ do_reload()

static void do_reload ( void  )
static

Definition at line 1043 of file pg_ctl.c.

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

Referenced by main().

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

◆ do_restart()

static void do_restart ( void  )
static

Definition at line 949 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().

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

◆ do_start()

static void do_start ( void  )
static

Definition at line 780 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().

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

◆ do_status()

static void do_status ( void  )
static

Definition at line 1204 of file pg_ctl.c.

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

Referenced by main().

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

◆ do_stop()

static void do_stop ( void  )
static

Definition at line 862 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().

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

◆ 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 720 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().

721 {
722  int ret;
723  char *found_path;
724 
725  found_path = pg_malloc(MAXPGPATH);
726 
727  if ((ret = find_other_exec(argv0, target, versionstr, found_path)) < 0)
728  {
729  char full_path[MAXPGPATH];
730 
731  if (find_my_exec(argv0, full_path) < 0)
732  strlcpy(full_path, progname, sizeof(full_path));
733 
734  if (ret == -1)
735  write_stderr(_("The program \"%s\" is needed by %s "
736  "but was not found in the\n"
737  "same directory as \"%s\".\n"
738  "Check your installation.\n"),
739  target, progname, full_path);
740  else
741  write_stderr(_("The program \"%s\" was found by \"%s\"\n"
742  "but was not the same version as %s.\n"
743  "Check your installation.\n"),
744  target, full_path, progname);
745  exit(1);
746  }
747 
748  return found_path;
749 }
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:94
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:87
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:204
#define _(x)
Definition: elog.c:84

◆ free_readfile()

static void free_readfile ( char **  optlines)
static

Definition at line 412 of file pg_ctl.c.

References free, and i.

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

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

◆ get_control_dbstate()

static DBState get_control_dbstate ( void  )
static

Definition at line 2119 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().

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

◆ get_pgpid()

static pgpid_t get_pgpid ( bool  is_status_request)
static

Definition at line 248 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().

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 2138 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(), GetDataDirectoryCreatePerm(), getopt_long(), INIT_COMMAND, KILL_COMMAND, log_file, MAXPGPATH, no_argument, NO_COMMAND, optarg, optind, pg_config, pg_data, pg_mode_mask, PG_MODE_MASK_OWNER, 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, 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().

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

◆ postmaster_is_alive()

static bool postmaster_is_alive ( pid_t  pid)
static

Definition at line 1180 of file pg_ctl.c.

References kill.

Referenced by do_restart(), and do_status().

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

◆ print_msg()

static void print_msg ( const char *  msg)
static

Definition at line 238 of file pg_ctl.c.

References silent_mode.

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

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

◆ read_post_opts()

static void read_post_opts ( void  )
static

Definition at line 671 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().

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

◆ readfile()

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

Definition at line 316 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().

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

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

◆ set_sig()

static void set_sig ( char *  signame)
static

Definition at line 2010 of file pg_ctl.c.

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

Referenced by main().

2011 {
2012  if (strcmp(signame, "HUP") == 0)
2013  sig = SIGHUP;
2014  else if (strcmp(signame, "INT") == 0)
2015  sig = SIGINT;
2016  else if (strcmp(signame, "QUIT") == 0)
2017  sig = SIGQUIT;
2018  else if (strcmp(signame, "ABRT") == 0)
2019  sig = SIGABRT;
2020  else if (strcmp(signame, "KILL") == 0)
2021  sig = SIGKILL;
2022  else if (strcmp(signame, "TERM") == 0)
2023  sig = SIGTERM;
2024  else if (strcmp(signame, "USR1") == 0)
2025  sig = SIGUSR1;
2026  else if (strcmp(signame, "USR2") == 0)
2027  sig = SIGUSR2;
2028  else
2029  {
2030  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2031  do_advice();
2032  exit(1);
2033  }
2034 }
#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:81
static const char * progname
Definition: pg_ctl.c:87
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:204
static void static void do_advice(void)
Definition: pg_ctl.c:1902
#define _(x)
Definition: elog.c:84

◆ start_postmaster()

static pgpid_t start_postmaster ( void  )
static

Definition at line 444 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().

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

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

◆ write_stderr()

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

Definition at line 204 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().

205 {
206  va_list ap;
207 
208  va_start(ap, fmt);
209 #ifndef WIN32
210  /* On Unix, we just fprintf to stderr */
211  vfprintf(stderr, fmt, ap);
212 #else
213 
214  /*
215  * On Win32, we print to stderr if running on a console, or write to
216  * eventlog if running as a service
217  */
218  if (pgwin32_is_service()) /* Running as a service */
219  {
220  char errbuf[2048]; /* Arbitrary size? */
221 
222  vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
223 
224  write_eventlog(EVENTLOG_ERROR_TYPE, errbuf);
225  }
226  else
227  /* Not running as service, write to stderr */
228  vfprintf(stderr, fmt, ap);
229 #endif
230  va_end(ap);
231 }
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 95 of file pg_ctl.c.

Referenced by do_start(), and main().

◆ argv0

char* argv0 = NULL
static

Definition at line 94 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 101 of file pg_ctl.c.

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

◆ ctl_command

CtlCommand ctl_command = NO_COMMAND
static

Definition at line 82 of file pg_ctl.c.

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

◆ do_wait

bool do_wait = true
static

Definition at line 76 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 90 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 88 of file pg_ctl.c.

Referenced by main(), and start_postmaster().

◆ pg_config

char* pg_config = NULL
static

Definition at line 84 of file pg_ctl.c.

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

◆ pg_data

char* pg_data = NULL
static

Definition at line 83 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 85 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 100 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 86 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 98 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 102 of file pg_ctl.c.

Referenced by do_promote().

◆ register_password

char* register_password = NULL
static

Definition at line 93 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ register_servicename

char* register_servicename = "PostgreSQL"
static

Definition at line 91 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ register_username

char* register_username = NULL
static

Definition at line 92 of file pg_ctl.c.

Referenced by do_kill(), and main().

◆ shutdown_mode

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 80 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 79 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 99 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 78 of file pg_ctl.c.

Referenced by do_kill(), and main().