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 2084 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().

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

Definition at line 1923 of file pg_ctl.c.

References _, progname, and write_stderr().

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

1924 {
1925  write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
1926 }
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 1931 of file pg_ctl.c.

References _, and progname.

Referenced by main().

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

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

838 {
839  char cmd[MAXPGPATH];
840 
841  if (exec_path == NULL)
842  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
843 
844  if (pgdata_opt == NULL)
845  pgdata_opt = "";
846 
847  if (post_opts == NULL)
848  post_opts = "";
849 
850  if (!silent_mode)
851  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
853  else
854  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
856 
857  if (system(cmd) != 0)
858  {
859  write_stderr(_("%s: database system initialization failed\n"), progname);
860  exit(1);
861  }
862 }
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:226
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:805
static bool silent_mode
Definition: pg_ctl.c:74
static void do_kill ( pgpid_t  pid)
static

Definition at line 1344 of file pg_ctl.c.

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

Referenced by main().

1345 {
1346  if (kill((pid_t) pid, sig) != 0)
1347  {
1348  write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
1349  progname, sig, pid, strerror(errno));
1350  exit(1);
1351  }
1352 }
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 1166 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().

1167 {
1168  FILE *prmfile;
1169  pgpid_t pid;
1170 
1171  pid = get_pgpid(false);
1172 
1173  if (pid == 0) /* no pid file */
1174  {
1175  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1176  write_stderr(_("Is server running?\n"));
1177  exit(1);
1178  }
1179  else if (pid < 0) /* standalone backend, not postmaster */
1180  {
1181  pid = -pid;
1182  write_stderr(_("%s: cannot promote server; "
1183  "single-user server is running (PID: %ld)\n"),
1184  progname, pid);
1185  exit(1);
1186  }
1187 
1189  {
1190  write_stderr(_("%s: cannot promote server; "
1191  "server is not in standby mode\n"),
1192  progname);
1193  exit(1);
1194  }
1195 
1196  /*
1197  * For 9.3 onwards, "fast" promotion is performed. Promotion with a full
1198  * checkpoint is still possible by writing a file called
1199  * "fallback_promote" instead of "promote"
1200  */
1201  snprintf(promote_file, MAXPGPATH, "%s/promote", pg_data);
1202 
1203  if ((prmfile = fopen(promote_file, "w")) == NULL)
1204  {
1205  write_stderr(_("%s: could not create promote signal file \"%s\": %s\n"),
1206  progname, promote_file, strerror(errno));
1207  exit(1);
1208  }
1209  if (fclose(prmfile))
1210  {
1211  write_stderr(_("%s: could not write promote signal file \"%s\": %s\n"),
1212  progname, promote_file, strerror(errno));
1213  exit(1);
1214  }
1215 
1216  sig = SIGUSR1;
1217  if (kill((pid_t) pid, sig) != 0)
1218  {
1219  write_stderr(_("%s: could not send promote signal (PID: %ld): %s\n"),
1220  progname, pid, strerror(errno));
1221  if (unlink(promote_file) != 0)
1222  write_stderr(_("%s: could not remove promote signal file \"%s\": %s\n"),
1223  progname, promote_file, strerror(errno));
1224  exit(1);
1225  }
1226 
1227  if (do_wait)
1228  {
1230 
1231  print_msg(_("waiting for server to promote..."));
1232  while (wait_seconds > 0)
1233  {
1234  state = get_control_dbstate();
1235  if (state == DB_IN_PRODUCTION)
1236  break;
1237 
1238  print_msg(".");
1239  pg_usleep(1000000); /* 1 sec */
1240  wait_seconds--;
1241  }
1242  if (state == DB_IN_PRODUCTION)
1243  {
1244  print_msg(_(" done\n"));
1245  print_msg(_("server promoted\n"));
1246  }
1247  else
1248  {
1249  print_msg(_(" stopped waiting\n"));
1250  print_msg(_("server is still promoting\n"));
1251  }
1252  }
1253  else
1254  print_msg(_("server promoting\n"));
1255 }
#define SIGUSR1
Definition: win32.h:211
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:82
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:226
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:2143
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 1129 of file pg_ctl.c.

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

Referenced by main().

1130 {
1131  pgpid_t pid;
1132 
1133  pid = get_pgpid(false);
1134  if (pid == 0) /* no pid file */
1135  {
1136  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1137  write_stderr(_("Is server running?\n"));
1138  exit(1);
1139  }
1140  else if (pid < 0) /* standalone backend, not postmaster */
1141  {
1142  pid = -pid;
1143  write_stderr(_("%s: cannot reload server; "
1144  "single-user server is running (PID: %ld)\n"),
1145  progname, pid);
1146  write_stderr(_("Please terminate the single-user server and try again.\n"));
1147  exit(1);
1148  }
1149 
1150  if (kill((pid_t) pid, sig) != 0)
1151  {
1152  write_stderr(_("%s: could not send reload signal (PID: %ld): %s\n"),
1153  progname, pid, strerror(errno));
1154  exit(1);
1155  }
1156 
1157  print_msg(_("server signaled\n"));
1158 }
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 1036 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().

1037 {
1038  int cnt;
1039  pgpid_t pid;
1040  struct stat statbuf;
1041 
1042  pid = get_pgpid(false);
1043 
1044  if (pid == 0) /* no pid file */
1045  {
1046  write_stderr(_("%s: PID file \"%s\" does not exist\n"),
1047  progname, pid_file);
1048  write_stderr(_("Is server running?\n"));
1049  write_stderr(_("starting server anyway\n"));
1050  do_start();
1051  return;
1052  }
1053  else if (pid < 0) /* standalone backend, not postmaster */
1054  {
1055  pid = -pid;
1056  if (postmaster_is_alive((pid_t) pid))
1057  {
1058  write_stderr(_("%s: cannot restart server; "
1059  "single-user server is running (PID: %ld)\n"),
1060  progname, pid);
1061  write_stderr(_("Please terminate the single-user server and try again.\n"));
1062  exit(1);
1063  }
1064  }
1065 
1066  if (postmaster_is_alive((pid_t) pid))
1067  {
1068  if (kill((pid_t) pid, sig) != 0)
1069  {
1070  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
1071  strerror(errno));
1072  exit(1);
1073  }
1074 
1075  /*
1076  * If backup_label exists, an online backup is running. Warn the user
1077  * that smart shutdown will wait for it to finish. However, if
1078  * the server is in archive recovery, we're recovering from an online
1079  * backup instead of performing one.
1080  */
1081  if (shutdown_mode == SMART_MODE &&
1082  stat(backup_file, &statbuf) == 0 &&
1084  {
1085  print_msg(_("WARNING: online backup mode is active\n"
1086  "Shutdown will not complete until pg_stop_backup() is called.\n\n"));
1087  }
1088 
1089  print_msg(_("waiting for server to shut down..."));
1090 
1091  /* always wait for restart */
1092 
1093  for (cnt = 0; cnt < wait_seconds; cnt++)
1094  {
1095  if ((pid = get_pgpid(false)) != 0)
1096  {
1097  print_msg(".");
1098  pg_usleep(1000000); /* 1 sec */
1099  }
1100  else
1101  break;
1102  }
1103 
1104  if (pid != 0) /* pid file still exists */
1105  {
1106  print_msg(_(" failed\n"));
1107 
1108  write_stderr(_("%s: server does not shut down\n"), progname);
1109  if (shutdown_mode == SMART_MODE)
1110  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1111  "waiting for session-initiated disconnection.\n"));
1112  exit(1);
1113  }
1114 
1115  print_msg(_(" done\n"));
1116  print_msg(_("server stopped\n"));
1117  }
1118  else
1119  {
1120  write_stderr(_("%s: old server process (PID: %ld) seems to be gone\n"),
1121  progname, pid);
1122  write_stderr(_("starting server anyway\n"));
1123  }
1124 
1125  do_start();
1126 }
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:865
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:1263
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:2143
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 865 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().

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

Definition at line 1287 of file pg_ctl.c.

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

Referenced by main().

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

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

951 {
952  int cnt;
953  pgpid_t pid;
954  struct stat statbuf;
955 
956  pid = get_pgpid(false);
957 
958  if (pid == 0) /* no pid file */
959  {
960  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
961  write_stderr(_("Is server running?\n"));
962  exit(1);
963  }
964  else if (pid < 0) /* standalone backend, not postmaster */
965  {
966  pid = -pid;
967  write_stderr(_("%s: cannot stop server; "
968  "single-user server is running (PID: %ld)\n"),
969  progname, pid);
970  exit(1);
971  }
972 
973  if (kill((pid_t) pid, sig) != 0)
974  {
975  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"), progname, pid,
976  strerror(errno));
977  exit(1);
978  }
979 
980  if (!do_wait)
981  {
982  print_msg(_("server shutting down\n"));
983  return;
984  }
985  else
986  {
987  /*
988  * If backup_label exists, an online backup is running. Warn the user
989  * that smart shutdown will wait for it to finish. However, if
990  * the server is in archive recovery, we're recovering from an online
991  * backup instead of performing one.
992  */
993  if (shutdown_mode == SMART_MODE &&
994  stat(backup_file, &statbuf) == 0 &&
996  {
997  print_msg(_("WARNING: online backup mode is active\n"
998  "Shutdown will not complete until pg_stop_backup() is called.\n\n"));
999  }
1000 
1001  print_msg(_("waiting for server to shut down..."));
1002 
1003  for (cnt = 0; cnt < wait_seconds; cnt++)
1004  {
1005  if ((pid = get_pgpid(false)) != 0)
1006  {
1007  print_msg(".");
1008  pg_usleep(1000000); /* 1 sec */
1009  }
1010  else
1011  break;
1012  }
1013 
1014  if (pid != 0) /* pid file still exists */
1015  {
1016  print_msg(_(" failed\n"));
1017 
1018  write_stderr(_("%s: server does not shut down\n"), progname);
1019  if (shutdown_mode == SMART_MODE)
1020  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1021  "waiting for session-initiated disconnection.\n"));
1022  exit(1);
1023  }
1024  print_msg(_(" done\n"));
1025 
1026  print_msg(_("server stopped\n"));
1027  }
1028 }
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:2143
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 805 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().

806 {
807  int ret;
808  char *found_path;
809 
810  found_path = pg_malloc(MAXPGPATH);
811 
812  if ((ret = find_other_exec(argv0, target, versionstr, found_path)) < 0)
813  {
814  char full_path[MAXPGPATH];
815 
816  if (find_my_exec(argv0, full_path) < 0)
817  strlcpy(full_path, progname, sizeof(full_path));
818 
819  if (ret == -1)
820  write_stderr(_("The program \"%s\" is needed by %s "
821  "but was not found in the\n"
822  "same directory as \"%s\".\n"
823  "Check your installation.\n"),
824  target, progname, full_path);
825  else
826  write_stderr(_("The program \"%s\" was found by \"%s\"\n"
827  "but was not the same version as %s.\n"
828  "Check your installation.\n"),
829  target, full_path, progname);
830  exit(1);
831  }
832 
833  return found_path;
834 }
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 396 of file pg_ctl.c.

References free, i, and NULL.

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

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

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

2144 {
2145  DBState ret;
2146  bool crc_ok;
2147  ControlFileData *control_file_data = get_controlfile(pg_data, progname, &crc_ok);
2148 
2149  if (!crc_ok)
2150  {
2151  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2152  exit(1);
2153  }
2154 
2155  ret = control_file_data->state;
2156  pfree(control_file_data);
2157  return ret;
2158 }
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:992
DBState
Definition: pg_control.h:82
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-g
261  * eneric/iniscrptact.html
262  */
263  exit(is_status_request ? 4 : 1);
264  }
265 
266  if (stat(version_file, &statbuf) != 0 && errno == ENOENT)
267  {
268  write_stderr(_("%s: directory \"%s\" is not a database cluster directory\n"),
269  progname, pg_data);
270  exit(is_status_request ? 4 : 1);
271  }
272 
273  pidf = fopen(pid_file, "r");
274  if (pidf == NULL)
275  {
276  /* No pid file, not an error on startup */
277  if (errno == ENOENT)
278  return 0;
279  else
280  {
281  write_stderr(_("%s: could not open PID file \"%s\": %s\n"),
282  progname, pid_file, strerror(errno));
283  exit(1);
284  }
285  }
286  if (fscanf(pidf, "%ld", &pid) != 1)
287  {
288  /* Is the file empty? */
289  if (ftell(pidf) == 0 && feof(pidf))
290  write_stderr(_("%s: the PID file \"%s\" is empty\n"),
291  progname, pid_file);
292  else
293  write_stderr(_("%s: invalid data in PID file \"%s\"\n"),
294  progname, pid_file);
295  exit(1);
296  }
297  fclose(pidf);
298  return (pgpid_t) pid;
299 }
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:226
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 2162 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().

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

Referenced by do_restart(), and do_status().

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

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

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

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

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

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

2004 {
2005  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
2006  {
2008  sig = SIGTERM;
2009  }
2010  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
2011  {
2013  sig = SIGINT;
2014  }
2015  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
2016  {
2018  sig = SIGQUIT;
2019  }
2020  else
2021  {
2022  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2023  do_advice();
2024  exit(1);
2025  }
2026 }
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:75
#define SIGQUIT
Definition: win32.h:197
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:1923
#define _(x)
Definition: elog.c:84
static void set_sig ( char *  signame)
static

Definition at line 2031 of file pg_ctl.c.

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

Referenced by main().

2032 {
2033  if (strcmp(signame, "HUP") == 0)
2034  sig = SIGHUP;
2035  else if (strcmp(signame, "INT") == 0)
2036  sig = SIGINT;
2037  else if (strcmp(signame, "QUIT") == 0)
2038  sig = SIGQUIT;
2039  else if (strcmp(signame, "ABRT") == 0)
2040  sig = SIGABRT;
2041 #if 0
2042  /* probably should NOT provide SIGKILL */
2043  else if (strcmp(signame, "KILL") == 0)
2044  sig = SIGKILL;
2045 #endif
2046  else if (strcmp(signame, "TERM") == 0)
2047  sig = SIGTERM;
2048  else if (strcmp(signame, "USR1") == 0)
2049  sig = SIGUSR1;
2050  else if (strcmp(signame, "USR2") == 0)
2051  sig = SIGUSR2;
2052  else
2053  {
2054  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2055  do_advice();
2056  exit(1);
2057  }
2058 }
#define SIGUSR1
Definition: win32.h:211
#define SIGQUIT
Definition: win32.h:197
static int sig
Definition: pg_ctl.c:76
#define SIGHUP
Definition: win32.h:196
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:1923
#define SIGKILL
Definition: win32.h:200
#define SIGABRT
Definition: win32.h:199
#define _(x)
Definition: elog.c:84
#define SIGUSR2
Definition: win32.h:212
static pgpid_t start_postmaster ( void  )
static

Definition at line 428 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(), main(), and setup().

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

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