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 "utils/pidfile.h"
Include dependency graph for pg_ctl.c:

Go to the source code of this file.

Macros

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

Typedefs

typedef long pgpid_t
 

Enumerations

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

Functions

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

Variables

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

Macro Definition Documentation

#define DEFAULT_WAIT   60

Definition at line 77 of file pg_ctl.c.

#define USEC_PER_SEC   1000000

Definition at line 79 of file pg_ctl.c.

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

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

Definition at line 81 of file pg_ctl.c.

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

Typedef Documentation

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

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 47 of file pg_ctl.c.

48 {
49  SMART_MODE,
50  FAST_MODE,
52 } ShutdownMode;
ShutdownMode
Definition: pg_ctl.c:47
Enumerator
POSTMASTER_READY 
POSTMASTER_STILL_STARTING 
POSTMASTER_FAILED 

Definition at line 54 of file pg_ctl.c.

Function Documentation

static void adjust_data_dir ( void  )
static

Definition at line 2004 of file pg_ctl.c.

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

Referenced by main().

2005 {
2006  char cmd[MAXPGPATH],
2008  *my_exec_path;
2009  FILE *fd;
2010 
2011  /* do nothing if we're working without knowledge of data dir */
2012  if (pg_config == NULL)
2013  return;
2014 
2015  /* If there is no postgresql.conf, it can't be a config-only dir */
2016  snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2017  if ((fd = fopen(filename, "r")) == NULL)
2018  return;
2019  fclose(fd);
2020 
2021  /* If PG_VERSION exists, it can't be a config-only dir */
2022  snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2023  if ((fd = fopen(filename, "r")) != NULL)
2024  {
2025  fclose(fd);
2026  return;
2027  }
2028 
2029  /* Must be a configuration directory, so find the data directory */
2030 
2031  /* we use a private my_exec_path to avoid interfering with later uses */
2032  if (exec_path == NULL)
2033  my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR);
2034  else
2035  my_exec_path = pg_strdup(exec_path);
2036 
2037  /* it's important for -C to be the first option, see main.c */
2038  snprintf(cmd, MAXPGPATH, "\"%s\" -C data_directory %s%s",
2039  my_exec_path,
2040  pgdata_opt ? pgdata_opt : "",
2041  post_opts ? post_opts : "");
2042 
2043  fd = popen(cmd, "r");
2044  if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)
2045  {
2046  write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2047  exit(1);
2048  }
2049  pclose(fd);
2050  free(my_exec_path);
2051 
2052  /* Remove trailing newline */
2053  if (strchr(filename, '\n') != NULL)
2054  *strchr(filename, '\n') = '\0';
2055 
2056  free(pg_data);
2057  pg_data = pg_strdup(filename);
2059 }
static char * argv0
Definition: pg_ctl.c:101
static char * post_opts
Definition: pg_ctl.c:93
void canonicalize_path(char *path)
Definition: path.c:254
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static char * pg_data
Definition: pg_ctl.c:90
#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:92
#define PG_BACKEND_VERSIONSTR
Definition: port.h:102
#define free(a)
Definition: header.h:65
static char * exec_path
Definition: pg_ctl.c:96
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
static char * filename
Definition: pg_dumpall.c:90
#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:726
static char * pg_config
Definition: pg_ctl.c:91
static void do_advice ( void  )
static

Definition at line 1846 of file pg_ctl.c.

References _, progname, and write_stderr().

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

1847 {
1848  write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
1849 }
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
#define _(x)
Definition: elog.c:84
static void do_help ( void  )
static

Definition at line 1854 of file pg_ctl.c.

References _, and progname.

Referenced by main().

1855 {
1856  printf(_("%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"), progname);
1857  printf(_("Usage:\n"));
1858  printf(_(" %s init[db] [-D DATADIR] [-s] [-o OPTIONS]\n"), progname);
1859  printf(_(" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
1860  " [-o OPTIONS] [-p PATH] [-c]\n"), progname);
1861  printf(_(" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"), progname);
1862  printf(_(" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
1863  " [-o OPTIONS] [-c]\n"), progname);
1864  printf(_(" %s reload [-D DATADIR] [-s]\n"), progname);
1865  printf(_(" %s status [-D DATADIR]\n"), progname);
1866  printf(_(" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"), progname);
1867  printf(_(" %s kill SIGNALNAME PID\n"), progname);
1868 #ifdef WIN32
1869  printf(_(" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
1870  " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"), progname);
1871  printf(_(" %s unregister [-N SERVICENAME]\n"), progname);
1872 #endif
1873 
1874  printf(_("\nCommon options:\n"));
1875  printf(_(" -D, --pgdata=DATADIR location of the database storage area\n"));
1876 #ifdef WIN32
1877  printf(_(" -e SOURCE event source for logging when running as a service\n"));
1878 #endif
1879  printf(_(" -s, --silent only print errors, no informational messages\n"));
1880  printf(_(" -t, --timeout=SECS seconds to wait when using -w option\n"));
1881  printf(_(" -V, --version output version information, then exit\n"));
1882  printf(_(" -w, --wait wait until operation completes (default)\n"));
1883  printf(_(" -W, --no-wait do not wait until operation completes\n"));
1884  printf(_(" -?, --help show this help, then exit\n"));
1885  printf(_("If the -D option is omitted, the environment variable PGDATA is used.\n"));
1886 
1887  printf(_("\nOptions for start or restart:\n"));
1888 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
1889  printf(_(" -c, --core-files allow postgres to produce core files\n"));
1890 #else
1891  printf(_(" -c, --core-files not applicable on this platform\n"));
1892 #endif
1893  printf(_(" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
1894  printf(_(" -o, --options=OPTIONS command line options to pass to postgres\n"
1895  " (PostgreSQL server executable) or initdb\n"));
1896  printf(_(" -p PATH-TO-POSTGRES normally not necessary\n"));
1897  printf(_("\nOptions for stop or restart:\n"));
1898  printf(_(" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
1899 
1900  printf(_("\nShutdown modes are:\n"));
1901  printf(_(" smart quit after all clients have disconnected\n"));
1902  printf(_(" fast quit directly, with proper shutdown (default)\n"));
1903  printf(_(" immediate quit without complete shutdown; will lead to recovery on restart\n"));
1904 
1905  printf(_("\nAllowed signal names for kill:\n"));
1906  printf(" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
1907 
1908 #ifdef WIN32
1909  printf(_("\nOptions for register and unregister:\n"));
1910  printf(_(" -N SERVICENAME service name with which to register PostgreSQL server\n"));
1911  printf(_(" -P PASSWORD password of account to register PostgreSQL server\n"));
1912  printf(_(" -U USERNAME user name of account to register PostgreSQL server\n"));
1913  printf(_(" -S START-TYPE service start type to register PostgreSQL server\n"));
1914 
1915  printf(_("\nStart types are:\n"));
1916  printf(_(" auto start service automatically during system startup (default)\n"));
1917  printf(_(" demand start service on demand\n"));
1918 #endif
1919 
1920  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
1921 }
static const char * progname
Definition: pg_ctl.c:94
#define _(x)
Definition: elog.c:84
static void do_init ( void  )
static

Definition at line 758 of file pg_ctl.c.

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

Referenced by main().

759 {
760  char cmd[MAXPGPATH];
761 
762  if (exec_path == NULL)
763  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
764 
765  if (pgdata_opt == NULL)
766  pgdata_opt = "";
767 
768  if (post_opts == NULL)
769  post_opts = "";
770 
771  if (!silent_mode)
772  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
774  else
775  snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
777 
778  if (system(cmd) != 0)
779  {
780  write_stderr(_("%s: database system initialization failed\n"), progname);
781  exit(1);
782  }
783 }
static char * argv0
Definition: pg_ctl.c:101
static char * post_opts
Definition: pg_ctl.c:93
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXPGPATH
#define DEVNULL
Definition: port.h:119
static char * pgdata_opt
Definition: pg_ctl.c:92
static char * exec_path
Definition: pg_ctl.c:96
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
#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:726
static bool silent_mode
Definition: pg_ctl.c:86
static void do_kill ( pgpid_t  pid)
static

Definition at line 1267 of file pg_ctl.c.

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

Referenced by main().

1268 {
1269  if (kill((pid_t) pid, sig) != 0)
1270  {
1271  write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
1272  progname, sig, pid, strerror(errno));
1273  exit(1);
1274  }
1275 }
static int sig
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
static void do_promote ( void  )
static

Definition at line 1086 of file pg_ctl.c.

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

Referenced by main().

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

Definition at line 1049 of file pg_ctl.c.

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

Referenced by main().

1050 {
1051  pgpid_t pid;
1052 
1053  pid = get_pgpid(false);
1054  if (pid == 0) /* no pid file */
1055  {
1056  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1057  write_stderr(_("Is server running?\n"));
1058  exit(1);
1059  }
1060  else if (pid < 0) /* standalone backend, not postmaster */
1061  {
1062  pid = -pid;
1063  write_stderr(_("%s: cannot reload server; "
1064  "single-user server is running (PID: %ld)\n"),
1065  progname, pid);
1066  write_stderr(_("Please terminate the single-user server and try again.\n"));
1067  exit(1);
1068  }
1069 
1070  if (kill((pid_t) pid, sig) != 0)
1071  {
1072  write_stderr(_("%s: could not send reload signal (PID: %ld): %s\n"),
1073  progname, pid, strerror(errno));
1074  exit(1);
1075  }
1076 
1077  print_msg(_("server signaled\n"));
1078 }
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:107
long pgpid_t
Definition: pg_ctl.c:44
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:254
static int sig
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
const char * strerror(int errnum)
Definition: strerror.c:19
static void print_msg(const char *msg)
Definition: pg_ctl.c:244
#define _(x)
Definition: elog.c:84
static void do_restart ( void  )
static

Definition at line 955 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(), USEC_PER_SEC, wait_seconds, WAITS_PER_SEC, and write_stderr().

Referenced by main().

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

Definition at line 786 of file pg_ctl.c.

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

Referenced by do_restart(), and main().

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

Definition at line 1210 of file pg_ctl.c.

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

Referenced by main().

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

Definition at line 868 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(), USEC_PER_SEC, wait_seconds, WAITS_PER_SEC, and write_stderr().

Referenced by main().

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

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

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

Definition at line 418 of file pg_ctl.c.

References free, and i.

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

419 {
420  char *curr_line = NULL;
421  int i = 0;
422 
423  if (!optlines)
424  return;
425 
426  while ((curr_line = optlines[i++]))
427  free(curr_line);
428 
429  free(optlines);
430 
431  return;
432 }
#define free(a)
Definition: header.h:65
int i
static DBState get_control_dbstate ( void  )
static

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

2064 {
2065  DBState ret;
2066  bool crc_ok;
2067  ControlFileData *control_file_data = get_controlfile(pg_data, progname, &crc_ok);
2068 
2069  if (!crc_ok)
2070  {
2071  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2072  exit(1);
2073  }
2074 
2075  ret = control_file_data->state;
2076  pfree(control_file_data);
2077  return ret;
2078 }
ControlFileData * get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
static char * pg_data
Definition: pg_ctl.c:90
void pfree(void *pointer)
Definition: mcxt.c:949
DBState
Definition: pg_control.h:85
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
#define _(x)
Definition: elog.c:84
static pgpid_t get_pgpid ( bool  is_status_request)
static

Definition at line 254 of file pg_ctl.c.

References _, 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().

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

Definition at line 2082 of file pg_ctl.c.

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

2083 {
2084  static struct option long_options[] = {
2085  {"help", no_argument, NULL, '?'},
2086  {"version", no_argument, NULL, 'V'},
2087  {"log", required_argument, NULL, 'l'},
2088  {"mode", required_argument, NULL, 'm'},
2089  {"pgdata", required_argument, NULL, 'D'},
2090  {"options", required_argument, NULL, 'o'},
2091  {"silent", no_argument, NULL, 's'},
2092  {"timeout", required_argument, NULL, 't'},
2093  {"core-files", no_argument, NULL, 'c'},
2094  {"wait", no_argument, NULL, 'w'},
2095  {"no-wait", no_argument, NULL, 'W'},
2096  {NULL, 0, NULL, 0}
2097  };
2098 
2099  char *env_wait;
2100  int option_index;
2101  int c;
2102  pgpid_t killproc = 0;
2103 
2104 #ifdef WIN32
2105  setvbuf(stderr, NULL, _IONBF, 0);
2106 #endif
2107 
2108  progname = get_progname(argv[0]);
2109  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));
2110  start_time = time(NULL);
2111 
2112  /*
2113  * save argv[0] so do_start() can look for the postmaster if necessary. we
2114  * don't look for postmaster here because in many cases we won't need it.
2115  */
2116  argv0 = argv[0];
2117 
2118  umask(S_IRWXG | S_IRWXO);
2119 
2120  /* support --help and --version even if invoked as root */
2121  if (argc > 1)
2122  {
2123  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2124  {
2125  do_help();
2126  exit(0);
2127  }
2128  else if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2129  {
2130  puts("pg_ctl (PostgreSQL) " PG_VERSION);
2131  exit(0);
2132  }
2133  }
2134 
2135  /*
2136  * Disallow running as root, to forestall any possible security holes.
2137  */
2138 #ifndef WIN32
2139  if (geteuid() == 0)
2140  {
2141  write_stderr(_("%s: cannot be run as root\n"
2142  "Please log in (using, e.g., \"su\") as the "
2143  "(unprivileged) user that will\n"
2144  "own the server process.\n"),
2145  progname);
2146  exit(1);
2147  }
2148 #endif
2149 
2150  env_wait = getenv("PGCTLTIMEOUT");
2151  if (env_wait != NULL)
2152  wait_seconds = atoi(env_wait);
2153 
2154  /*
2155  * 'Action' can be before or after args so loop over both. Some
2156  * getopt_long() implementations will reorder argv[] to place all flags
2157  * first (GNU?), but we don't rely on it. Our /port version doesn't do
2158  * that.
2159  */
2160  optind = 1;
2161 
2162  /* process command-line options */
2163  while (optind < argc)
2164  {
2165  while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW",
2166  long_options, &option_index)) != -1)
2167  {
2168  switch (c)
2169  {
2170  case 'D':
2171  {
2172  char *pgdata_D;
2173  char *env_var;
2174 
2175  pgdata_D = pg_strdup(optarg);
2176  canonicalize_path(pgdata_D);
2177  env_var = psprintf("PGDATA=%s", pgdata_D);
2178  putenv(env_var);
2179 
2180  /*
2181  * We could pass PGDATA just in an environment
2182  * variable but we do -D too for clearer postmaster
2183  * 'ps' display
2184  */
2185  pgdata_opt = psprintf("-D \"%s\" ", pgdata_D);
2186  break;
2187  }
2188  case 'e':
2190  break;
2191  case 'l':
2193  break;
2194  case 'm':
2195  set_mode(optarg);
2196  break;
2197  case 'N':
2199  break;
2200  case 'o':
2201  /* append option? */
2202  if (!post_opts)
2204  else
2205  {
2206  char *old_post_opts = post_opts;
2207 
2208  post_opts = psprintf("%s %s", old_post_opts, optarg);
2209  free(old_post_opts);
2210  }
2211  break;
2212  case 'p':
2214  break;
2215  case 'P':
2217  break;
2218  case 's':
2219  silent_mode = true;
2220  break;
2221  case 'S':
2222 #ifdef WIN32
2223  set_starttype(optarg);
2224 #else
2225  write_stderr(_("%s: -S option not supported on this platform\n"),
2226  progname);
2227  exit(1);
2228 #endif
2229  break;
2230  case 't':
2231  wait_seconds = atoi(optarg);
2232  wait_seconds_arg = true;
2233  break;
2234  case 'U':
2235  if (strchr(optarg, '\\'))
2237  else
2238  /* Prepend .\ for local accounts */
2239  register_username = psprintf(".\\%s", optarg);
2240  break;
2241  case 'w':
2242  do_wait = true;
2243  break;
2244  case 'W':
2245  do_wait = false;
2246  break;
2247  case 'c':
2248  allow_core_files = true;
2249  break;
2250  default:
2251  /* getopt_long already issued a suitable error message */
2252  do_advice();
2253  exit(1);
2254  }
2255  }
2256 
2257  /* Process an action */
2258  if (optind < argc)
2259  {
2260  if (ctl_command != NO_COMMAND)
2261  {
2262  write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]);
2263  do_advice();
2264  exit(1);
2265  }
2266 
2267  if (strcmp(argv[optind], "init") == 0
2268  || strcmp(argv[optind], "initdb") == 0)
2270  else if (strcmp(argv[optind], "start") == 0)
2272  else if (strcmp(argv[optind], "stop") == 0)
2274  else if (strcmp(argv[optind], "restart") == 0)
2276  else if (strcmp(argv[optind], "reload") == 0)
2278  else if (strcmp(argv[optind], "status") == 0)
2280  else if (strcmp(argv[optind], "promote") == 0)
2282  else if (strcmp(argv[optind], "kill") == 0)
2283  {
2284  if (argc - optind < 3)
2285  {
2286  write_stderr(_("%s: missing arguments for kill mode\n"), progname);
2287  do_advice();
2288  exit(1);
2289  }
2291  set_sig(argv[++optind]);
2292  killproc = atol(argv[++optind]);
2293  }
2294 #ifdef WIN32
2295  else if (strcmp(argv[optind], "register") == 0)
2297  else if (strcmp(argv[optind], "unregister") == 0)
2299  else if (strcmp(argv[optind], "runservice") == 0)
2301 #endif
2302  else
2303  {
2304  write_stderr(_("%s: unrecognized operation mode \"%s\"\n"), progname, argv[optind]);
2305  do_advice();
2306  exit(1);
2307  }
2308  optind++;
2309  }
2310  }
2311 
2312  if (ctl_command == NO_COMMAND)
2313  {
2314  write_stderr(_("%s: no operation specified\n"), progname);
2315  do_advice();
2316  exit(1);
2317  }
2318 
2319  /* Note we put any -D switch into the env var above */
2320  pg_config = getenv("PGDATA");
2321  if (pg_config)
2322  {
2326  }
2327 
2328  /* -D might point at config-only directory; if so find the real PGDATA */
2329  adjust_data_dir();
2330 
2331  /* Complain if -D needed and not provided */
2332  if (pg_config == NULL &&
2334  {
2335  write_stderr(_("%s: no database directory specified and environment variable PGDATA unset\n"),
2336  progname);
2337  do_advice();
2338  exit(1);
2339  }
2340 
2341  if (ctl_command == RELOAD_COMMAND)
2342  {
2343  sig = SIGHUP;
2344  do_wait = false;
2345  }
2346 
2347  if (pg_data)
2348  {
2349  snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data);
2350  snprintf(version_file, MAXPGPATH, "%s/PG_VERSION", pg_data);
2351  snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pg_data);
2352  snprintf(backup_file, MAXPGPATH, "%s/backup_label", pg_data);
2353  }
2354 
2355  switch (ctl_command)
2356  {
2357  case INIT_COMMAND:
2358  do_init();
2359  break;
2360  case STATUS_COMMAND:
2361  do_status();
2362  break;
2363  case START_COMMAND:
2364  do_start();
2365  break;
2366  case STOP_COMMAND:
2367  do_stop();
2368  break;
2369  case RESTART_COMMAND:
2370  do_restart();
2371  break;
2372  case RELOAD_COMMAND:
2373  do_reload();
2374  break;
2375  case PROMOTE_COMMAND:
2376  do_promote();
2377  break;
2378  case KILL_COMMAND:
2379  do_kill(killproc);
2380  break;
2381 #ifdef WIN32
2382  case REGISTER_COMMAND:
2383  pgwin32_doRegister();
2384  break;
2385  case UNREGISTER_COMMAND:
2386  pgwin32_doUnregister();
2387  break;
2389  pgwin32_doRunAsService();
2390  break;
2391 #endif
2392  default:
2393  break;
2394  }
2395 
2396  exit(0);
2397 }
static char * argv0
Definition: pg_ctl.c:101
static int wait_seconds
Definition: pg_ctl.c:84
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:95
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void do_help(void)
Definition: pg_ctl.c:1854
static char * post_opts
Definition: pg_ctl.c:93
static void do_stop(void)
Definition: pg_ctl.c:868
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:2004
static char * pg_data
Definition: pg_ctl.c:90
static time_t start_time
Definition: pg_ctl.c:103
static char * register_password
Definition: pg_ctl.c:100
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:107
long pgpid_t
Definition: pg_ctl.c:44
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:108
#define MAXPGPATH
static char * register_username
Definition: pg_ctl.c:99
static char * register_servicename
Definition: pg_ctl.c:98
char * c
static void do_start(void)
Definition: pg_ctl.c:786
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void do_kill(pgpid_t pid)
Definition: pg_ctl.c:1267
static void do_restart(void)
Definition: pg_ctl.c:955
static void do_status(void)
Definition: pg_ctl.c:1210
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1018
static char * pgdata_opt
Definition: pg_ctl.c:92
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:105
static int sig
Definition: pg_ctl.c:88
#define S_IRWXO
Definition: win32.h:455
#define SIGHUP
Definition: win32.h:188
#define free(a)
Definition: header.h:65
static void do_promote(void)
Definition: pg_ctl.c:1086
static bool allow_core_files
Definition: pg_ctl.c:102
static char * exec_path
Definition: pg_ctl.c:96
static void do_init(void)
Definition: pg_ctl.c:758
static char * event_source
Definition: pg_ctl.c:97
static const char * progname
Definition: pg_ctl.c:94
static bool wait_seconds_arg
Definition: pg_ctl.c:85
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
#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:83
static void set_mode(char *modeopt)
Definition: pg_ctl.c:1926
static void do_reload(void)
Definition: pg_ctl.c:1049
char * optarg
Definition: getopt.c:53
static void static void do_advice(void)
Definition: pg_ctl.c:1846
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:106
static CtlCommand ctl_command
Definition: pg_ctl.c:89
#define _(x)
Definition: elog.c:84
static void set_sig(char *signame)
Definition: pg_ctl.c:1954
static bool silent_mode
Definition: pg_ctl.c:86
static char * pg_config
Definition: pg_ctl.c:91
static bool postmaster_is_alive ( pid_t  pid)
static

Definition at line 1186 of file pg_ctl.c.

Referenced by do_restart(), and do_status().

1187 {
1188  /*
1189  * Test to see if the process is still there. Note that we do not
1190  * consider an EPERM failure to mean that the process is still there;
1191  * EPERM must mean that the given PID belongs to some other userid, and
1192  * considering the permissions on $PGDATA, that means it's not the
1193  * postmaster we are after.
1194  *
1195  * Don't believe that our own PID or parent shell's PID is the postmaster,
1196  * either. (Windows hasn't got getppid(), though.)
1197  */
1198  if (pid == getpid())
1199  return false;
1200 #ifndef WIN32
1201  if (pid == getppid())
1202  return false;
1203 #endif
1204  if (kill(pid, 0) == 0)
1205  return true;
1206  return false;
1207 }
static void print_msg ( const char *  msg)
static

Definition at line 244 of file pg_ctl.c.

References silent_mode.

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

245 {
246  if (!silent_mode)
247  {
248  fputs(msg, stdout);
249  fflush(stdout);
250  }
251 }
static bool silent_mode
Definition: pg_ctl.c:86
static void read_post_opts ( void  )
static

Definition at line 677 of file pg_ctl.c.

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

Referenced by do_start().

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

Definition at line 322 of file pg_ctl.c.

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

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

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

1927 {
1928  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
1929  {
1931  sig = SIGTERM;
1932  }
1933  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
1934  {
1936  sig = SIGINT;
1937  }
1938  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
1939  {
1941  sig = SIGQUIT;
1942  }
1943  else
1944  {
1945  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
1946  do_advice();
1947  exit(1);
1948  }
1949 }
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:87
#define SIGQUIT
Definition: win32.h:189
static int sig
Definition: pg_ctl.c:88
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
static void static void do_advice(void)
Definition: pg_ctl.c:1846
#define _(x)
Definition: elog.c:84
static void set_sig ( char *  signame)
static

Definition at line 1954 of file pg_ctl.c.

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

Referenced by main().

1955 {
1956  if (strcmp(signame, "HUP") == 0)
1957  sig = SIGHUP;
1958  else if (strcmp(signame, "INT") == 0)
1959  sig = SIGINT;
1960  else if (strcmp(signame, "QUIT") == 0)
1961  sig = SIGQUIT;
1962  else if (strcmp(signame, "ABRT") == 0)
1963  sig = SIGABRT;
1964  else if (strcmp(signame, "KILL") == 0)
1965  sig = SIGKILL;
1966  else if (strcmp(signame, "TERM") == 0)
1967  sig = SIGTERM;
1968  else if (strcmp(signame, "USR1") == 0)
1969  sig = SIGUSR1;
1970  else if (strcmp(signame, "USR2") == 0)
1971  sig = SIGUSR2;
1972  else
1973  {
1974  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
1975  do_advice();
1976  exit(1);
1977  }
1978 }
#define SIGUSR1
Definition: win32.h:202
#define SIGQUIT
Definition: win32.h:189
static int sig
Definition: pg_ctl.c:88
#define SIGHUP
Definition: win32.h:188
static const char * progname
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:210
static void static void do_advice(void)
Definition: pg_ctl.c:1846
#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 450 of file pg_ctl.c.

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

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

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

Definition at line 545 of file pg_ctl.c.

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

Referenced by do_start().

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

Definition at line 210 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(), and start_postmaster().

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

Referenced by do_start(), and main().

char* argv0 = NULL
static

Definition at line 101 of file pg_ctl.c.

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

char backup_file[MAXPGPATH]
static

Definition at line 108 of file pg_ctl.c.

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

CtlCommand ctl_command = NO_COMMAND
static

Definition at line 89 of file pg_ctl.c.

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

bool do_wait = true
static

Definition at line 83 of file pg_ctl.c.

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

char* event_source = NULL
static

Definition at line 97 of file pg_ctl.c.

Referenced by main().

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

Definition at line 95 of file pg_ctl.c.

Referenced by main(), and start_postmaster().

char* pg_config = NULL
static

Definition at line 91 of file pg_ctl.c.

Referenced by adjust_data_dir(), and main().

char* pg_data = NULL
static

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

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

char pid_file[MAXPGPATH]
static

Definition at line 107 of file pg_ctl.c.

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

char* post_opts = NULL
static

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

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

char promote_file[MAXPGPATH]
static

Definition at line 109 of file pg_ctl.c.

Referenced by do_promote().

char* register_password = NULL
static

Definition at line 100 of file pg_ctl.c.

Referenced by main().

char* register_servicename = "PostgreSQL"
static

Definition at line 98 of file pg_ctl.c.

Referenced by main().

char* register_username = NULL
static

Definition at line 99 of file pg_ctl.c.

Referenced by main().

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 87 of file pg_ctl.c.

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

bool silent_mode = false
static

Definition at line 86 of file pg_ctl.c.

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

char version_file[MAXPGPATH]
static

Definition at line 106 of file pg_ctl.c.

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

int wait_seconds = DEFAULT_WAIT
static

Definition at line 84 of file pg_ctl.c.

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

bool wait_seconds_arg = false
static

Definition at line 85 of file pg_ctl.c.

Referenced by main().