PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 "libpq-fe.h"
#include "miscadmin.h"
#include "pqexpbuffer.h"
Include dependency graph for pg_ctl.c:

Go to the source code of this file.

Macros

#define DEFAULT_WAIT   60
 

Typedefs

typedef long pgpid_t
 

Enumerations

enum  ShutdownMode { SMART_MODE, FAST_MODE, IMMEDIATE_MODE }
 
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)
 
static void free_readfile (char **optlines)
 
static pgpid_t start_postmaster (void)
 
static void read_post_opts (void)
 
static PGPing test_postmaster_connection (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

#define DEFAULT_WAIT   60

Definition at line 69 of file pg_ctl.c.

Typedef Documentation

Definition at line 42 of file pg_ctl.c.

Enumeration Type Documentation

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.

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 45 of file pg_ctl.c.

46 {
47  SMART_MODE,
48  FAST_MODE,
50 } ShutdownMode;
ShutdownMode
Definition: pg_ctl.c:45

Function Documentation

static void adjust_data_dir ( void  )
static

Definition at line 2081 of file pg_ctl.c.

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

Referenced by main().

2082 {
2083  char cmd[MAXPGPATH],
2085  *my_exec_path;
2086  FILE *fd;
2087 
2088  /* do nothing if we're working without knowledge of data dir */
2089  if (pg_config == NULL)
2090  return;
2091 
2092  /* If there is no postgresql.conf, it can't be a config-only dir */
2093  snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2094  if ((fd = fopen(filename, "r")) == NULL)
2095  return;
2096  fclose(fd);
2097 
2098  /* If PG_VERSION exists, it can't be a config-only dir */
2099  snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2100  if ((fd = fopen(filename, "r")) != NULL)
2101  {
2102  fclose(fd);
2103  return;
2104  }
2105 
2106  /* Must be a configuration directory, so find the data directory */
2107 
2108  /* we use a private my_exec_path to avoid interfering with later uses */
2109  if (exec_path == NULL)
2110  my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR);
2111  else
2112  my_exec_path = pg_strdup(exec_path);
2113 
2114  /* it's important for -C to be the first option, see main.c */
2115  snprintf(cmd, MAXPGPATH, "\"%s\" -C data_directory %s%s",
2116  my_exec_path,
2117  pgdata_opt ? pgdata_opt : "",
2118  post_opts ? post_opts : "");
2119 
2120  fd = popen(cmd, "r");
2121  if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)
2122  {
2123  write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2124  exit(1);
2125  }
2126  pclose(fd);
2127  free(my_exec_path);
2128 
2129  /* Remove trailing newline */
2130  if (strchr(filename, '\n') != NULL)
2131  *strchr(filename, '\n') = '\0';
2132 
2133  free(pg_data);
2134  pg_data = pg_strdup(filename);
2136 }
static char * argv0
Definition: pg_ctl.c:89
static char * post_opts
Definition: pg_ctl.c:81
void canonicalize_path(char *path)
Definition: path.c:254
#define PG_BACKEND_VERSIONSTR
Definition: miscadmin.h:31
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:78
#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:80
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static char * exec_path
Definition: pg_ctl.c:84
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static char * filename
Definition: pg_dumpall.c:89
#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:803
static char * pg_config
Definition: pg_ctl.c:79
static void do_advice ( void  )
static

Definition at line 1920 of file pg_ctl.c.

References _, progname, and write_stderr().

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

1921 {
1922  write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
1923 }
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
#define _(x)
Definition: elog.c:84
static void do_help ( void  )
static

Definition at line 1928 of file pg_ctl.c.

References _, and progname.

Referenced by main().

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

Definition at line 835 of file pg_ctl.c.

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

Referenced by main().

836 {
837  char cmd[MAXPGPATH];
838 
839  if (exec_path == NULL)
840  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
841 
842  if (pgdata_opt == NULL)
843  pgdata_opt = "";
844 
845  if (post_opts == NULL)
846  post_opts = "";
847 
848  if (!silent_mode)
849  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
851  else
852  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
854 
855  if (system(cmd) != 0)
856  {
857  write_stderr(_("%s: database system initialization failed\n"), progname);
858  exit(1);
859  }
860 }
static char * argv0
Definition: pg_ctl.c:89
static char * post_opts
Definition: pg_ctl.c:81
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXPGPATH
#define DEVNULL
Definition: port.h:116
static char * pgdata_opt
Definition: pg_ctl.c:80
#define NULL
Definition: c.h:229
static char * exec_path
Definition: pg_ctl.c:84
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
#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:803
static bool silent_mode
Definition: pg_ctl.c:74
static void do_kill ( pgpid_t  pid)
static

Definition at line 1341 of file pg_ctl.c.

References _, progname, sig, strerror(), and write_stderr().

Referenced by main().

1342 {
1343  if (kill((pid_t) pid, sig) != 0)
1344  {
1345  write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
1346  progname, sig, pid, strerror(errno));
1347  exit(1);
1348  }
1349 }
static int sig
Definition: pg_ctl.c:76
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
static void do_promote ( void  )
static

Definition at line 1164 of file pg_ctl.c.

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

Referenced by main().

1165 {
1166  FILE *prmfile;
1167  pgpid_t pid;
1168 
1169  pid = get_pgpid(false);
1170 
1171  if (pid == 0) /* no pid file */
1172  {
1173  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1174  write_stderr(_("Is server running?\n"));
1175  exit(1);
1176  }
1177  else if (pid < 0) /* standalone backend, not postmaster */
1178  {
1179  pid = -pid;
1180  write_stderr(_("%s: cannot promote server; "
1181  "single-user server is running (PID: %ld)\n"),
1182  progname, pid);
1183  exit(1);
1184  }
1185 
1187  {
1188  write_stderr(_("%s: cannot promote server; "
1189  "server is not in standby mode\n"),
1190  progname);
1191  exit(1);
1192  }
1193 
1194  /*
1195  * For 9.3 onwards, "fast" promotion is performed. Promotion with a full
1196  * checkpoint is still possible by writing a file called
1197  * "fallback_promote" instead of "promote"
1198  */
1199  snprintf(promote_file, MAXPGPATH, "%s/promote", pg_data);
1200 
1201  if ((prmfile = fopen(promote_file, "w")) == NULL)
1202  {
1203  write_stderr(_("%s: could not create promote signal file \"%s\": %s\n"),
1204  progname, promote_file, strerror(errno));
1205  exit(1);
1206  }
1207  if (fclose(prmfile))
1208  {
1209  write_stderr(_("%s: could not write promote signal file \"%s\": %s\n"),
1210  progname, promote_file, strerror(errno));
1211  exit(1);
1212  }
1213 
1214  sig = SIGUSR1;
1215  if (kill((pid_t) pid, sig) != 0)
1216  {
1217  write_stderr(_("%s: could not send promote signal (PID: %ld): %s\n"),
1218  progname, pid, strerror(errno));
1219  if (unlink(promote_file) != 0)
1220  write_stderr(_("%s: could not remove promote signal file \"%s\": %s\n"),
1221  progname, promote_file, strerror(errno));
1222  exit(1);
1223  }
1224 
1225  if (do_wait)
1226  {
1228 
1229  print_msg(_("waiting for server to promote..."));
1230  while (wait_seconds > 0)
1231  {
1232  state = get_control_dbstate();
1233  if (state == DB_IN_PRODUCTION)
1234  break;
1235 
1236  print_msg(".");
1237  pg_usleep(1000000); /* 1 sec */
1238  wait_seconds--;
1239  }
1240  if (state == DB_IN_PRODUCTION)
1241  {
1242  print_msg(_(" done\n"));
1243  print_msg(_("server promoted\n"));
1244  }
1245  else
1246  {
1247  print_msg(_(" stopped waiting\n"));
1248  print_msg(_("server is still promoting\n"));
1249  }
1250  }
1251  else
1252  print_msg(_("server promoting\n"));
1253 }
#define SIGUSR1
Definition: win32.h:202
static int wait_seconds
Definition: pg_ctl.c:72
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * pg_data
Definition: pg_ctl.c:78
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:95
long pgpid_t
Definition: pg_ctl.c:42
#define MAXPGPATH
int unlink(const char *filename)
DBState
Definition: pg_control.h:84
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:242
static int sig
Definition: pg_ctl.c:76
#define NULL
Definition: c.h:229
Definition: regguts.h:298
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:97
static bool do_wait
Definition: pg_ctl.c:71
const char * strerror(int errnum)
Definition: strerror.c:19
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2140
static void print_msg(const char *msg)
Definition: pg_ctl.c:232
#define _(x)
Definition: elog.c:84
static void do_reload ( void  )
static

Definition at line 1127 of file pg_ctl.c.

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

Referenced by main().

1128 {
1129  pgpid_t pid;
1130 
1131  pid = get_pgpid(false);
1132  if (pid == 0) /* no pid file */
1133  {
1134  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1135  write_stderr(_("Is server running?\n"));
1136  exit(1);
1137  }
1138  else if (pid < 0) /* standalone backend, not postmaster */
1139  {
1140  pid = -pid;
1141  write_stderr(_("%s: cannot reload server; "
1142  "single-user server is running (PID: %ld)\n"),
1143  progname, pid);
1144  write_stderr(_("Please terminate the single-user server and try again.\n"));
1145  exit(1);
1146  }
1147 
1148  if (kill((pid_t) pid, sig) != 0)
1149  {
1150  write_stderr(_("%s: could not send reload signal (PID: %ld): %s\n"),
1151  progname, pid, strerror(errno));
1152  exit(1);
1153  }
1154 
1155  print_msg(_("server signaled\n"));
1156 }
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:95
long pgpid_t
Definition: pg_ctl.c:42
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:242
static int sig
Definition: pg_ctl.c:76
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
const char * strerror(int errnum)
Definition: strerror.c:19
static void print_msg(const char *msg)
Definition: pg_ctl.c:232
#define _(x)
Definition: elog.c:84
static void do_restart ( void  )
static

Definition at line 1034 of file pg_ctl.c.

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

Referenced by main().

1035 {
1036  int cnt;
1037  pgpid_t pid;
1038  struct stat statbuf;
1039 
1040  pid = get_pgpid(false);
1041 
1042  if (pid == 0) /* no pid file */
1043  {
1044  write_stderr(_("%s: PID file \"%s\" does not exist\n"),
1045  progname, pid_file);
1046  write_stderr(_("Is server running?\n"));
1047  write_stderr(_("starting server anyway\n"));
1048  do_start();
1049  return;
1050  }
1051  else if (pid < 0) /* standalone backend, not postmaster */
1052  {
1053  pid = -pid;
1054  if (postmaster_is_alive((pid_t) pid))
1055  {
1056  write_stderr(_("%s: cannot restart 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 
1064  if (postmaster_is_alive((pid_t) pid))
1065  {
1066  if (kill((pid_t) pid, sig) != 0)
1067  {
1068  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
1069  strerror(errno));
1070  exit(1);
1071  }
1072 
1073  /*
1074  * If backup_label exists, an online backup is running. Warn the user
1075  * that smart shutdown will wait for it to finish. However, if the
1076  * server is in archive recovery, we're recovering from an online
1077  * backup instead of performing one.
1078  */
1079  if (shutdown_mode == SMART_MODE &&
1080  stat(backup_file, &statbuf) == 0 &&
1082  {
1083  print_msg(_("WARNING: online backup mode is active\n"
1084  "Shutdown will not complete until pg_stop_backup() is called.\n\n"));
1085  }
1086 
1087  print_msg(_("waiting for server to shut down..."));
1088 
1089  /* always wait for restart */
1090 
1091  for (cnt = 0; cnt < wait_seconds; cnt++)
1092  {
1093  if ((pid = get_pgpid(false)) != 0)
1094  {
1095  print_msg(".");
1096  pg_usleep(1000000); /* 1 sec */
1097  }
1098  else
1099  break;
1100  }
1101 
1102  if (pid != 0) /* pid file still exists */
1103  {
1104  print_msg(_(" failed\n"));
1105 
1106  write_stderr(_("%s: server does not shut down\n"), progname);
1107  if (shutdown_mode == SMART_MODE)
1108  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1109  "waiting for session-initiated disconnection.\n"));
1110  exit(1);
1111  }
1112 
1113  print_msg(_(" done\n"));
1114  print_msg(_("server stopped\n"));
1115  }
1116  else
1117  {
1118  write_stderr(_("%s: old server process (PID: %ld) seems to be gone\n"),
1119  progname, pid);
1120  write_stderr(_("starting server anyway\n"));
1121  }
1122 
1123  do_start();
1124 }
static int wait_seconds
Definition: pg_ctl.c:72
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:75
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:95
long pgpid_t
Definition: pg_ctl.c:42
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:96
static void do_start(void)
Definition: pg_ctl.c:863
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:242
static int sig
Definition: pg_ctl.c:76
static bool postmaster_is_alive(pid_t pid)
Definition: pg_ctl.c:1261
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
const char * strerror(int errnum)
Definition: strerror.c:19
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2140
static void print_msg(const char *msg)
Definition: pg_ctl.c:232
#define _(x)
Definition: elog.c:84
static void do_start ( void  )
static

Definition at line 863 of file pg_ctl.c.

References _, allow_core_files, argv0, ctl_command, do_wait, exec_path, find_other_exec_or_die(), get_pgpid(), NULL, PG_BACKEND_VERSIONSTR, pgdata_opt, PQPING_NO_ATTEMPT, PQPING_NO_RESPONSE, PQPING_OK, PQPING_REJECT, print_msg(), progname, putenv, read_post_opts(), RESTART_COMMAND, snprintf(), start_postmaster(), test_postmaster_connection(), and write_stderr().

Referenced by do_restart(), and main().

864 {
865  pgpid_t old_pid = 0;
866  pgpid_t pm_pid;
867 
869  {
870  old_pid = get_pgpid(false);
871  if (old_pid != 0)
872  write_stderr(_("%s: another server might be running; "
873  "trying to start server anyway\n"),
874  progname);
875  }
876 
877  read_post_opts();
878 
879  /* No -D or -D already added during server start */
881  pgdata_opt = "";
882 
883  if (exec_path == NULL)
885 
886 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
887  if (allow_core_files)
888  unlimit_core_size();
889 #endif
890 
891  /*
892  * If possible, tell the postmaster our parent shell's PID (see the
893  * comments in CreateLockFile() for motivation). Windows hasn't got
894  * getppid() unfortunately.
895  */
896 #ifndef WIN32
897  {
898  static char env_var[32];
899 
900  snprintf(env_var, sizeof(env_var), "PG_GRANDPARENT_PID=%d",
901  (int) getppid());
902  putenv(env_var);
903  }
904 #endif
905 
906  pm_pid = start_postmaster();
907 
908  if (do_wait)
909  {
910  print_msg(_("waiting for server to start..."));
911 
912  switch (test_postmaster_connection(pm_pid, false))
913  {
914  case PQPING_OK:
915  print_msg(_(" done\n"));
916  print_msg(_("server started\n"));
917  break;
918  case PQPING_REJECT:
919  print_msg(_(" stopped waiting\n"));
920  print_msg(_("server is still starting up\n"));
921  break;
922  case PQPING_NO_RESPONSE:
923  print_msg(_(" stopped waiting\n"));
924  write_stderr(_("%s: could not start server\n"
925  "Examine the log output.\n"),
926  progname);
927  exit(1);
928  break;
929  case PQPING_NO_ATTEMPT:
930  print_msg(_(" failed\n"));
931  write_stderr(_("%s: could not wait for server because of misconfiguration\n"),
932  progname);
933  exit(1);
934  }
935  }
936  else
937  print_msg(_("server starting\n"));
938 
939 #ifdef WIN32
940  /* Now we don't need the handle to the shell process anymore */
941  CloseHandle(postmasterProcess);
942  postmasterProcess = INVALID_HANDLE_VALUE;
943 #endif
944 }
static PGPing test_postmaster_connection(pgpid_t pm_pid, bool do_checkpoint)
Definition: pg_ctl.c:522
static char * argv0
Definition: pg_ctl.c:89
static void read_post_opts(void)
Definition: pg_ctl.c:751
#define PG_BACKEND_VERSIONSTR
Definition: miscadmin.h:31
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define putenv(x)
Definition: win32.h:411
long pgpid_t
Definition: pg_ctl.c:42
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:242
static char * pgdata_opt
Definition: pg_ctl.c:80
#define NULL
Definition: c.h:229
static bool allow_core_files
Definition: pg_ctl.c:90
static char * exec_path
Definition: pg_ctl.c:84
static const char * progname
Definition: pg_ctl.c:82
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:427
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static bool do_wait
Definition: pg_ctl.c:71
static CtlCommand ctl_command
Definition: pg_ctl.c:77
static void print_msg(const char *msg)
Definition: pg_ctl.c:232
#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:803
static void do_status ( void  )
static

Definition at line 1285 of file pg_ctl.c.

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

Referenced by main().

1286 {
1287  pgpid_t pid;
1288 
1289  pid = get_pgpid(true);
1290  /* Is there a pid file? */
1291  if (pid != 0)
1292  {
1293  /* standalone backend? */
1294  if (pid < 0)
1295  {
1296  pid = -pid;
1297  if (postmaster_is_alive((pid_t) pid))
1298  {
1299  printf(_("%s: single-user server is running (PID: %ld)\n"),
1300  progname, pid);
1301  return;
1302  }
1303  }
1304  else
1305  /* must be a postmaster */
1306  {
1307  if (postmaster_is_alive((pid_t) pid))
1308  {
1309  char **optlines;
1310  char **curr_line;
1311 
1312  printf(_("%s: server is running (PID: %ld)\n"),
1313  progname, pid);
1314 
1315  optlines = readfile(postopts_file);
1316  if (optlines != NULL)
1317  {
1318  for (curr_line = optlines; *curr_line != NULL; curr_line++)
1319  fputs(*curr_line, stdout);
1320 
1321  /* Free the results of readfile */
1322  free_readfile(optlines);
1323  }
1324  return;
1325  }
1326  }
1327  }
1328  printf(_("%s: no server running\n"), progname);
1329 
1330  /*
1331  * The Linux Standard Base Core Specification 3.1 says this should return
1332  * '3, program is not running'
1333  * https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1334  */
1335  exit(3);
1336 }
long pgpid_t
Definition: pg_ctl.c:42
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:242
static char ** readfile(const char *path)
Definition: pg_ctl.c:305
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:93
static void free_readfile(char **optlines)
Definition: pg_ctl.c:395
#define NULL
Definition: c.h:229
static bool postmaster_is_alive(pid_t pid)
Definition: pg_ctl.c:1261
static const char * progname
Definition: pg_ctl.c:82
#define _(x)
Definition: elog.c:84
static void do_stop ( void  )
static

Definition at line 948 of file pg_ctl.c.

References _, backup_file, DB_IN_ARCHIVE_RECOVERY, do_wait, get_control_dbstate(), get_pgpid(), pg_usleep(), pid_file, print_msg(), progname, shutdown_mode, sig, SMART_MODE, strerror(), wait_seconds, 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"), progname, pid_file);
959  write_stderr(_("Is server running?\n"));
960  exit(1);
961  }
962  else if (pid < 0) /* standalone backend, not postmaster */
963  {
964  pid = -pid;
965  write_stderr(_("%s: cannot stop server; "
966  "single-user server is running (PID: %ld)\n"),
967  progname, pid);
968  exit(1);
969  }
970 
971  if (kill((pid_t) pid, sig) != 0)
972  {
973  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
974  strerror(errno));
975  exit(1);
976  }
977 
978  if (!do_wait)
979  {
980  print_msg(_("server shutting down\n"));
981  return;
982  }
983  else
984  {
985  /*
986  * If backup_label exists, an online backup is running. Warn the user
987  * that smart shutdown will wait for it to finish. However, if the
988  * server is in archive recovery, we're recovering from an online
989  * backup instead of performing one.
990  */
991  if (shutdown_mode == SMART_MODE &&
992  stat(backup_file, &statbuf) == 0 &&
994  {
995  print_msg(_("WARNING: online backup mode is active\n"
996  "Shutdown will not complete until pg_stop_backup() is called.\n\n"));
997  }
998 
999  print_msg(_("waiting for server to shut down..."));
1000 
1001  for (cnt = 0; cnt < wait_seconds; cnt++)
1002  {
1003  if ((pid = get_pgpid(false)) != 0)
1004  {
1005  print_msg(".");
1006  pg_usleep(1000000); /* 1 sec */
1007  }
1008  else
1009  break;
1010  }
1011 
1012  if (pid != 0) /* pid file still exists */
1013  {
1014  print_msg(_(" failed\n"));
1015 
1016  write_stderr(_("%s: server does not shut down\n"), progname);
1017  if (shutdown_mode == SMART_MODE)
1018  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1019  "waiting for session-initiated disconnection.\n"));
1020  exit(1);
1021  }
1022  print_msg(_(" done\n"));
1023 
1024  print_msg(_("server stopped\n"));
1025  }
1026 }
static int wait_seconds
Definition: pg_ctl.c:72
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:75
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:95
long pgpid_t
Definition: pg_ctl.c:42
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:96
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:242
static int sig
Definition: pg_ctl.c:76
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static bool do_wait
Definition: pg_ctl.c:71
const char * strerror(int errnum)
Definition: strerror.c:19
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2140
static void print_msg(const char *msg)
Definition: pg_ctl.c:232
#define _(x)
Definition: elog.c:84
static char* find_other_exec_or_die ( const char *  argv0,
const char *  target,
const char *  versionstr 
)
static

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

804 {
805  int ret;
806  char *found_path;
807 
808  found_path = pg_malloc(MAXPGPATH);
809 
810  if ((ret = find_other_exec(argv0, target, versionstr, found_path)) < 0)
811  {
812  char full_path[MAXPGPATH];
813 
814  if (find_my_exec(argv0, full_path) < 0)
815  strlcpy(full_path, progname, sizeof(full_path));
816 
817  if (ret == -1)
818  write_stderr(_("The program \"%s\" is needed by %s "
819  "but was not found in the\n"
820  "same directory as \"%s\".\n"
821  "Check your installation.\n"),
822  target, progname, full_path);
823  else
824  write_stderr(_("The program \"%s\" was found by \"%s\"\n"
825  "but was not the same version as %s.\n"
826  "Check your installation.\n"),
827  target, full_path, progname);
828  exit(1);
829  }
830 
831  return found_path;
832 }
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:89
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:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
#define _(x)
Definition: elog.c:84
static void free_readfile ( char **  optlines)
static

Definition at line 395 of file pg_ctl.c.

References free, i, and NULL.

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

396 {
397  char *curr_line = NULL;
398  int i = 0;
399 
400  if (!optlines)
401  return;
402 
403  while ((curr_line = optlines[i++]))
404  free(curr_line);
405 
406  free(optlines);
407 
408  return;
409 }
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
int i
static DBState get_control_dbstate ( void  )
static

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

2141 {
2142  DBState ret;
2143  bool crc_ok;
2144  ControlFileData *control_file_data = get_controlfile(pg_data, progname, &crc_ok);
2145 
2146  if (!crc_ok)
2147  {
2148  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2149  exit(1);
2150  }
2151 
2152  ret = control_file_data->state;
2153  pfree(control_file_data);
2154  return ret;
2155 }
ControlFileData * get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
static char * pg_data
Definition: pg_ctl.c:78
void pfree(void *pointer)
Definition: mcxt.c:950
DBState
Definition: pg_control.h:84
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
#define _(x)
Definition: elog.c:84
static pgpid_t get_pgpid ( bool  is_status_request)
static

Definition at line 242 of file pg_ctl.c.

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

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

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

Definition at line 2159 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, NULL, 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().

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

Definition at line 1261 of file pg_ctl.c.

Referenced by do_restart(), and do_status().

1262 {
1263  /*
1264  * Test to see if the process is still there. Note that we do not
1265  * consider an EPERM failure to mean that the process is still there;
1266  * EPERM must mean that the given PID belongs to some other userid, and
1267  * considering the permissions on $PGDATA, that means it's not the
1268  * postmaster we are after.
1269  *
1270  * Don't believe that our own PID or parent shell's PID is the postmaster,
1271  * either. (Windows hasn't got getppid(), though.)
1272  */
1273  if (pid == getpid())
1274  return false;
1275 #ifndef WIN32
1276  if (pid == getppid())
1277  return false;
1278 #endif
1279  if (kill(pid, 0) == 0)
1280  return true;
1281  return false;
1282 }
static void print_msg ( const char *  msg)
static

Definition at line 232 of file pg_ctl.c.

References silent_mode.

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

233 {
234  if (!silent_mode)
235  {
236  fputs(msg, stdout);
237  fflush(stdout);
238  }
239 }
static bool silent_mode
Definition: pg_ctl.c:74
static void read_post_opts ( void  )
static

Definition at line 751 of file pg_ctl.c.

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

Referenced by do_start().

752 {
753  if (post_opts == NULL)
754  {
755  post_opts = ""; /* default */
757  {
758  char **optlines;
759 
760  optlines = readfile(postopts_file);
761  if (optlines == NULL)
762  {
763  write_stderr(_("%s: could not read file \"%s\"\n"), progname, postopts_file);
764  exit(1);
765  }
766  else if (optlines[0] == NULL || optlines[1] != NULL)
767  {
768  write_stderr(_("%s: option file \"%s\" must have exactly one line\n"),
770  exit(1);
771  }
772  else
773  {
774  int len;
775  char *optline;
776  char *arg1;
777 
778  optline = optlines[0];
779  /* trim off line endings */
780  len = strcspn(optline, "\r\n");
781  optline[len] = '\0';
782 
783  /*
784  * Are we at the first option, as defined by space and
785  * double-quote?
786  */
787  if ((arg1 = strstr(optline, " \"")) != NULL)
788  {
789  *arg1 = '\0'; /* terminate so we get only program name */
790  post_opts = pg_strdup(arg1 + 1); /* point past whitespace */
791  }
792  if (exec_path == NULL)
793  exec_path = pg_strdup(optline);
794  }
795 
796  /* Free the results of readfile. */
797  free_readfile(optlines);
798  }
799  }
800 }
static char * post_opts
Definition: pg_ctl.c:81
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char ** readfile(const char *path)
Definition: pg_ctl.c:305
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:93
static void free_readfile(char **optlines)
Definition: pg_ctl.c:395
#define NULL
Definition: c.h:229
static char * exec_path
Definition: pg_ctl.c:84
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static CtlCommand ctl_command
Definition: pg_ctl.c:77
#define _(x)
Definition: elog.c:84
static char ** readfile ( const char *  path)
static

Definition at line 305 of file pg_ctl.c.

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

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

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

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

2001 {
2002  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
2003  {
2005  sig = SIGTERM;
2006  }
2007  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
2008  {
2010  sig = SIGINT;
2011  }
2012  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
2013  {
2015  sig = SIGQUIT;
2016  }
2017  else
2018  {
2019  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2020  do_advice();
2021  exit(1);
2022  }
2023 }
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:75
#define SIGQUIT
Definition: win32.h:189
static int sig
Definition: pg_ctl.c:76
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static void static void do_advice(void)
Definition: pg_ctl.c:1920
#define _(x)
Definition: elog.c:84
static void set_sig ( char *  signame)
static

Definition at line 2028 of file pg_ctl.c.

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

Referenced by main().

2029 {
2030  if (strcmp(signame, "HUP") == 0)
2031  sig = SIGHUP;
2032  else if (strcmp(signame, "INT") == 0)
2033  sig = SIGINT;
2034  else if (strcmp(signame, "QUIT") == 0)
2035  sig = SIGQUIT;
2036  else if (strcmp(signame, "ABRT") == 0)
2037  sig = SIGABRT;
2038 #if 0
2039  /* probably should NOT provide SIGKILL */
2040  else if (strcmp(signame, "KILL") == 0)
2041  sig = SIGKILL;
2042 #endif
2043  else if (strcmp(signame, "TERM") == 0)
2044  sig = SIGTERM;
2045  else if (strcmp(signame, "USR1") == 0)
2046  sig = SIGUSR1;
2047  else if (strcmp(signame, "USR2") == 0)
2048  sig = SIGUSR2;
2049  else
2050  {
2051  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2052  do_advice();
2053  exit(1);
2054  }
2055 }
#define SIGUSR1
Definition: win32.h:202
#define SIGQUIT
Definition: win32.h:189
static int sig
Definition: pg_ctl.c:76
#define SIGHUP
Definition: win32.h:188
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
static void static void do_advice(void)
Definition: pg_ctl.c:1920
#define SIGKILL
Definition: win32.h:192
#define SIGABRT
Definition: win32.h:191
#define _(x)
Definition: elog.c:84
#define SIGUSR2
Definition: win32.h:203
static pgpid_t start_postmaster ( void  )
static

Definition at line 427 of file pg_ctl.c.

References _, DEVNULL, exec_path, log_file, MAXPGPATH, NULL, 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().

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

Definition at line 522 of file pg_ctl.c.

References _, connstr, free_readfile(), i, LOCK_FILE_LINE_LISTEN_ADDR, LOCK_FILE_LINE_PID, LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR, LOCK_FILE_LINE_START_TIME, MAXPGPATH, NULL, pg_usleep(), pid_file, PQping(), PQPING_NO_ATTEMPT, PQPING_NO_RESPONSE, PQPING_OK, PQPING_REJECT, print_msg(), progname, readfile(), snprintf(), sockdir, start_time, status(), strlcpy(), wait_seconds, and write_stderr().

Referenced by do_start().

523 {
525  char connstr[MAXPGPATH * 2 + 256];
526  int i;
527 
528  /* if requested wait time is zero, return "still starting up" code */
529  if (wait_seconds <= 0)
530  return PQPING_REJECT;
531 
532  connstr[0] = '\0';
533 
534  for (i = 0; i < wait_seconds; i++)
535  {
536  /* Do we need a connection string? */
537  if (connstr[0] == '\0')
538  {
539  /*----------
540  * The number of lines in postmaster.pid tells us several things:
541  *
542  * # of lines
543  * 0 lock file created but status not written
544  * 2 pre-9.1 server, shared memory not created
545  * 3 pre-9.1 server, shared memory created
546  * 5 9.1+ server, ports not opened
547  * 6 9.1+ server, shared memory not created
548  * 7 9.1+ server, shared memory created
549  *
550  * This code does not support pre-9.1 servers. On Unix machines
551  * we could consider extracting the port number from the shmem
552  * key, but that (a) is not robust, and (b) doesn't help with
553  * finding out the socket directory. And it wouldn't work anyway
554  * on Windows.
555  *
556  * If we see less than 6 lines in postmaster.pid, just keep
557  * waiting.
558  *----------
559  */
560  char **optlines;
561 
562  /* Try to read the postmaster.pid file */
563  if ((optlines = readfile(pid_file)) != NULL &&
564  optlines[0] != NULL &&
565  optlines[1] != NULL &&
566  optlines[2] != NULL)
567  {
568  if (optlines[3] == NULL)
569  {
570  /* File is exactly three lines, must be pre-9.1 */
571  write_stderr(_("\n%s: -w option is not supported when starting a pre-9.1 server\n"),
572  progname);
573  return PQPING_NO_ATTEMPT;
574  }
575  else if (optlines[4] != NULL &&
576  optlines[5] != NULL)
577  {
578  /* File is complete enough for us, parse it */
579  pgpid_t pmpid;
580  time_t pmstart;
581 
582  /*
583  * Make sanity checks. If it's for the wrong PID, or the
584  * recorded start time is before pg_ctl started, then
585  * either we are looking at the wrong data directory, or
586  * this is a pre-existing pidfile that hasn't (yet?) been
587  * overwritten by our child postmaster. Allow 2 seconds
588  * slop for possible cross-process clock skew.
589  */
590  pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]);
591  pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]);
592  if (pmstart >= start_time - 2 &&
593 #ifndef WIN32
594  pmpid == pm_pid
595 #else
596  /* Windows can only reject standalone-backend PIDs */
597  pmpid > 0
598 #endif
599  )
600  {
601  /*
602  * OK, seems to be a valid pidfile from our child.
603  */
604  int portnum;
605  char *sockdir;
606  char *hostaddr;
607  char host_str[MAXPGPATH];
608 
609  /*
610  * Extract port number and host string to use. Prefer
611  * using Unix socket if available.
612  */
613  portnum = atoi(optlines[LOCK_FILE_LINE_PORT - 1]);
614  sockdir = optlines[LOCK_FILE_LINE_SOCKET_DIR - 1];
615  hostaddr = optlines[LOCK_FILE_LINE_LISTEN_ADDR - 1];
616 
617  /*
618  * While unix_socket_directories can accept relative
619  * directories, libpq's host parameter must have a
620  * leading slash to indicate a socket directory. So,
621  * ignore sockdir if it's relative, and try to use TCP
622  * instead.
623  */
624  if (sockdir[0] == '/')
625  strlcpy(host_str, sockdir, sizeof(host_str));
626  else
627  strlcpy(host_str, hostaddr, sizeof(host_str));
628 
629  /* remove trailing newline */
630  if (strchr(host_str, '\n') != NULL)
631  *strchr(host_str, '\n') = '\0';
632 
633  /* Fail if couldn't get either sockdir or host addr */
634  if (host_str[0] == '\0')
635  {
636  write_stderr(_("\n%s: -w option cannot use a relative socket directory specification\n"),
637  progname);
638  return PQPING_NO_ATTEMPT;
639  }
640 
641  /*
642  * Map listen-only addresses to counterparts usable
643  * for establishing a connection. connect() to "::"
644  * or "0.0.0.0" is not portable to OpenBSD 5.0 or to
645  * Windows Server 2008, and connect() to "::" is
646  * additionally not portable to NetBSD 6.0. (Cygwin
647  * does handle both addresses, though.)
648  */
649  if (strcmp(host_str, "*") == 0)
650  strcpy(host_str, "localhost");
651  else if (strcmp(host_str, "0.0.0.0") == 0)
652  strcpy(host_str, "127.0.0.1");
653  else if (strcmp(host_str, "::") == 0)
654  strcpy(host_str, "::1");
655 
656  /*
657  * We need to set connect_timeout otherwise on Windows
658  * the Service Control Manager (SCM) will probably
659  * timeout first.
660  */
661  snprintf(connstr, sizeof(connstr),
662  "dbname=postgres port=%d host='%s' connect_timeout=5",
663  portnum, host_str);
664  }
665  }
666  }
667 
668  /*
669  * Free the results of readfile.
670  *
671  * This is safe to call even if optlines is NULL.
672  */
673  free_readfile(optlines);
674  }
675 
676  /* If we have a connection string, ping the server */
677  if (connstr[0] != '\0')
678  {
679  ret = PQping(connstr);
680  if (ret == PQPING_OK || ret == PQPING_NO_ATTEMPT)
681  break;
682  }
683 
684  /*
685  * Check whether the child postmaster process is still alive. This
686  * lets us exit early if the postmaster fails during startup.
687  *
688  * On Windows, we may be checking the postmaster's parent shell, but
689  * that's fine for this purpose.
690  */
691 #ifndef WIN32
692  {
693  int exitstatus;
694 
695  if (waitpid((pid_t) pm_pid, &exitstatus, WNOHANG) == (pid_t) pm_pid)
696  return PQPING_NO_RESPONSE;
697  }
698 #else
699  if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0)
700  return PQPING_NO_RESPONSE;
701 #endif
702 
703  /* No response, or startup still in process; wait */
704 #ifdef WIN32
705  if (do_checkpoint)
706  {
707  /*
708  * Increment the wait hint by 6 secs (connection timeout + sleep)
709  * We must do this to indicate to the SCM that our startup time is
710  * changing, otherwise it'll usually send a stop signal after 20
711  * seconds, despite incrementing the checkpoint counter.
712  */
713  status.dwWaitHint += 6000;
714  status.dwCheckPoint++;
715  SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
716  }
717  else
718 #endif
719  print_msg(".");
720 
721  pg_usleep(1000000); /* 1 sec */
722  }
723 
724  /* return result of last call to PQping */
725  return ret;
726 }
PGPing PQping(const char *conninfo)
Definition: fe-connect.c:582
static const char * sockdir
Definition: pg_regress.c:101
static int wait_seconds
Definition: pg_ctl.c:72
#define LOCK_FILE_LINE_LISTEN_ADDR
Definition: miscadmin.h:456
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define LOCK_FILE_LINE_PID
Definition: miscadmin.h:451
static time_t start_time
Definition: pg_ctl.c:91
void pg_usleep(long microsec)
Definition: signal.c:53
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:95
long pgpid_t
Definition: pg_ctl.c:42
#define MAXPGPATH
#define LOCK_FILE_LINE_SOCKET_DIR
Definition: miscadmin.h:455
static char ** readfile(const char *path)
Definition: pg_ctl.c:305
static void free_readfile(char **optlines)
Definition: pg_ctl.c:395
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:229
#define LOCK_FILE_LINE_START_TIME
Definition: miscadmin.h:453
#define LOCK_FILE_LINE_PORT
Definition: miscadmin.h:454
static const char * progname
Definition: pg_ctl.c:82
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:198
int i
PGPing
Definition: libpq-fe.h:129
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static void print_msg(const char *msg)
Definition: pg_ctl.c:232
#define _(x)
Definition: elog.c:84
static char * connstr
Definition: pg_dumpall.c:64
static void write_stderr ( const char *  fmt,
  ... 
)
static

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

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

Variable Documentation

bool allow_core_files = false
static

Definition at line 90 of file pg_ctl.c.

Referenced by do_start(), and main().

char* argv0 = NULL
static

Definition at line 89 of file pg_ctl.c.

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

char backup_file[MAXPGPATH]
static

Definition at line 96 of file pg_ctl.c.

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

CtlCommand ctl_command = NO_COMMAND
static

Definition at line 77 of file pg_ctl.c.

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

bool do_wait = true
static

Definition at line 71 of file pg_ctl.c.

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

char* event_source = NULL
static

Definition at line 85 of file pg_ctl.c.

Referenced by main().

char* exec_path = NULL
static
char* log_file = NULL
static

Definition at line 83 of file pg_ctl.c.

Referenced by main(), and start_postmaster().

char* pg_config = NULL
static

Definition at line 79 of file pg_ctl.c.

Referenced by adjust_data_dir(), and main().

char* pg_data = NULL
static

Definition at line 78 of file pg_ctl.c.

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

char* pgdata_opt = NULL
static

Definition at line 80 of file pg_ctl.c.

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

char pid_file[MAXPGPATH]
static
char* post_opts = NULL
static

Definition at line 81 of file pg_ctl.c.

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

char postopts_file[MAXPGPATH]
static

Definition at line 93 of file pg_ctl.c.

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

char promote_file[MAXPGPATH]
static

Definition at line 97 of file pg_ctl.c.

Referenced by do_promote().

char* register_password = NULL
static

Definition at line 88 of file pg_ctl.c.

Referenced by main().

char* register_servicename = "PostgreSQL"
static

Definition at line 86 of file pg_ctl.c.

Referenced by main().

char* register_username = NULL
static

Definition at line 87 of file pg_ctl.c.

Referenced by main().

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 75 of file pg_ctl.c.

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

bool silent_mode = false
static

Definition at line 74 of file pg_ctl.c.

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

char version_file[MAXPGPATH]
static

Definition at line 94 of file pg_ctl.c.

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

int wait_seconds = DEFAULT_WAIT
static
bool wait_seconds_arg = false
static

Definition at line 73 of file pg_ctl.c.

Referenced by main().