PostgreSQL Source Code  git master
pg_ctl.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "common/string.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 */
 

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 ,
  LOGROTATE_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_logrotate (void)
 
static void do_kill (pid_t pid)
 
static void print_msg (const char *msg)
 
static void adjust_data_dir (void)
 
static pid_t get_pgpid (bool is_status_request)
 
static char ** readfile (const char *path, int *numlines)
 
static void free_readfile (char **optlines)
 
static pid_t start_postmaster (void)
 
static void read_post_opts (void)
 
static WaitPMResult wait_for_postmaster_start (pid_t pm_pid, bool do_checkpoint)
 
static bool wait_for_postmaster_stop (void)
 
static bool wait_for_postmaster_promote (void)
 
static bool postmaster_is_alive (pid_t pid)
 
static void unlimit_core_size (void)
 
static DBState get_control_dbstate (void)
 
static void trap_sigint_during_startup (SIGNAL_ARGS)
 
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 promote_file [MAXPGPATH]
 
static char logrotate_file [MAXPGPATH]
 
static volatile pid_t postmasterPID = -1
 

Macro Definition Documentation

◆ DEFAULT_WAIT

#define DEFAULT_WAIT   60

Definition at line 68 of file pg_ctl.c.

◆ USEC_PER_SEC

#define USEC_PER_SEC   1000000

Definition at line 70 of file pg_ctl.c.

◆ WAITS_PER_SEC

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

Definition at line 72 of file pg_ctl.c.

Enumeration Type Documentation

◆ CtlCommand

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

Definition at line 51 of file pg_ctl.c.

52 {
53  NO_COMMAND = 0,
66 } CtlCommand;
CtlCommand
Definition: pg_ctl.c:52
@ REGISTER_COMMAND
Definition: pg_ctl.c:63
@ RUN_AS_SERVICE_COMMAND
Definition: pg_ctl.c:65
@ LOGROTATE_COMMAND
Definition: pg_ctl.c:61
@ RESTART_COMMAND
Definition: pg_ctl.c:57
@ UNREGISTER_COMMAND
Definition: pg_ctl.c:64
@ START_COMMAND
Definition: pg_ctl.c:55
@ KILL_COMMAND
Definition: pg_ctl.c:62
@ INIT_COMMAND
Definition: pg_ctl.c:54
@ STOP_COMMAND
Definition: pg_ctl.c:56
@ STATUS_COMMAND
Definition: pg_ctl.c:59
@ NO_COMMAND
Definition: pg_ctl.c:53
@ PROMOTE_COMMAND
Definition: pg_ctl.c:60
@ RELOAD_COMMAND
Definition: pg_ctl.c:58

◆ ShutdownMode

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 37 of file pg_ctl.c.

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

◆ WaitPMResult

Enumerator
POSTMASTER_READY 
POSTMASTER_STILL_STARTING 
POSTMASTER_FAILED 

Definition at line 44 of file pg_ctl.c.

45 {
49 } WaitPMResult;
WaitPMResult
Definition: pg_ctl.c:45
@ POSTMASTER_FAILED
Definition: pg_ctl.c:48
@ POSTMASTER_READY
Definition: pg_ctl.c:46
@ POSTMASTER_STILL_STARTING
Definition: pg_ctl.c:47

Function Documentation

◆ adjust_data_dir()

static void adjust_data_dir ( void  )
static

Definition at line 2113 of file pg_ctl.c.

2114 {
2115  char filename[MAXPGPATH];
2116  char *my_exec_path,
2117  *cmd;
2118  FILE *fd;
2119 
2120  /* do nothing if we're working without knowledge of data dir */
2121  if (pg_config == NULL)
2122  return;
2123 
2124  /* If there is no postgresql.conf, it can't be a config-only dir */
2125  snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2126  if ((fd = fopen(filename, "r")) == NULL)
2127  return;
2128  fclose(fd);
2129 
2130  /* If PG_VERSION exists, it can't be a config-only dir */
2131  snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2132  if ((fd = fopen(filename, "r")) != NULL)
2133  {
2134  fclose(fd);
2135  return;
2136  }
2137 
2138  /* Must be a configuration directory, so find the data directory */
2139 
2140  /* we use a private my_exec_path to avoid interfering with later uses */
2141  if (exec_path == NULL)
2143  else
2145 
2146  /* it's important for -C to be the first option, see main.c */
2147  cmd = psprintf("\"%s\" -C data_directory %s%s",
2148  my_exec_path,
2149  pgdata_opt ? pgdata_opt : "",
2150  post_opts ? post_opts : "");
2151  fflush(NULL);
2152 
2153  fd = popen(cmd, "r");
2154  if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL || pclose(fd) != 0)
2155  {
2156  write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2157  exit(1);
2158  }
2159  free(my_exec_path);
2160 
2161  /* strip trailing newline and carriage return */
2162  (void) pg_strip_crlf(filename);
2163 
2164  free(pg_data);
2167 }
#define _(x)
Definition: elog.c:90
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char my_exec_path[MAXPGPATH]
Definition: globals.c:78
#define free(a)
Definition: header.h:65
static void const char fflush(stdout)
exit(1)
#define MAXPGPATH
static char * post_opts
Definition: pg_ctl.c:84
static char * pgdata_opt
Definition: pg_ctl.c:83
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
Definition: pg_ctl.c:867
static char * pg_config
Definition: pg_ctl.c:82
static char * pg_data
Definition: pg_ctl.c:81
static char * argv0
Definition: pg_ctl.c:92
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:201
static const char * progname
Definition: pg_ctl.c:85
static char * exec_path
Definition: pg_ctl.c:87
static char * filename
Definition: pg_dumpall.c:121
void canonicalize_path(char *path)
Definition: path.c:264
#define snprintf
Definition: port.h:238
#define PG_BACKEND_VERSIONSTR
Definition: port.h:143
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int pg_strip_crlf(char *str)
Definition: string.c:155

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

Referenced by main().

◆ do_advice()

static void do_advice ( void  )
static

Definition at line 1953 of file pg_ctl.c.

1954 {
1955  write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
1956 }

References _, progname, and write_stderr().

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

◆ do_help()

static void do_help ( void  )
static

Definition at line 1961 of file pg_ctl.c.

1962 {
1963  printf(_("%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"), progname);
1964  printf(_("Usage:\n"));
1965  printf(_(" %s init[db] [-D DATADIR] [-s] [-o OPTIONS]\n"), progname);
1966  printf(_(" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
1967  " [-o OPTIONS] [-p PATH] [-c]\n"), progname);
1968  printf(_(" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"), progname);
1969  printf(_(" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
1970  " [-o OPTIONS] [-c]\n"), progname);
1971  printf(_(" %s reload [-D DATADIR] [-s]\n"), progname);
1972  printf(_(" %s status [-D DATADIR]\n"), progname);
1973  printf(_(" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"), progname);
1974  printf(_(" %s logrotate [-D DATADIR] [-s]\n"), progname);
1975  printf(_(" %s kill SIGNALNAME PID\n"), progname);
1976 #ifdef WIN32
1977  printf(_(" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
1978  " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"), progname);
1979  printf(_(" %s unregister [-N SERVICENAME]\n"), progname);
1980 #endif
1981 
1982  printf(_("\nCommon options:\n"));
1983  printf(_(" -D, --pgdata=DATADIR location of the database storage area\n"));
1984 #ifdef WIN32
1985  printf(_(" -e SOURCE event source for logging when running as a service\n"));
1986 #endif
1987  printf(_(" -s, --silent only print errors, no informational messages\n"));
1988  printf(_(" -t, --timeout=SECS seconds to wait when using -w option\n"));
1989  printf(_(" -V, --version output version information, then exit\n"));
1990  printf(_(" -w, --wait wait until operation completes (default)\n"));
1991  printf(_(" -W, --no-wait do not wait until operation completes\n"));
1992  printf(_(" -?, --help show this help, then exit\n"));
1993  printf(_("If the -D option is omitted, the environment variable PGDATA is used.\n"));
1994 
1995  printf(_("\nOptions for start or restart:\n"));
1996 #if defined(HAVE_GETRLIMIT)
1997  printf(_(" -c, --core-files allow postgres to produce core files\n"));
1998 #else
1999  printf(_(" -c, --core-files not applicable on this platform\n"));
2000 #endif
2001  printf(_(" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
2002  printf(_(" -o, --options=OPTIONS command line options to pass to postgres\n"
2003  " (PostgreSQL server executable) or initdb\n"));
2004  printf(_(" -p PATH-TO-POSTGRES normally not necessary\n"));
2005  printf(_("\nOptions for stop or restart:\n"));
2006  printf(_(" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
2007 
2008  printf(_("\nShutdown modes are:\n"));
2009  printf(_(" smart quit after all clients have disconnected\n"));
2010  printf(_(" fast quit directly, with proper shutdown (default)\n"));
2011  printf(_(" immediate quit without complete shutdown; will lead to recovery on restart\n"));
2012 
2013  printf(_("\nAllowed signal names for kill:\n"));
2014  printf(" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
2015 
2016 #ifdef WIN32
2017  printf(_("\nOptions for register and unregister:\n"));
2018  printf(_(" -N SERVICENAME service name with which to register PostgreSQL server\n"));
2019  printf(_(" -P PASSWORD password of account to register PostgreSQL server\n"));
2020  printf(_(" -U USERNAME user name of account to register PostgreSQL server\n"));
2021  printf(_(" -S START-TYPE service start type to register PostgreSQL server\n"));
2022 
2023  printf(_("\nStart types are:\n"));
2024  printf(_(" auto start service automatically during system startup (default)\n"));
2025  printf(_(" demand start service on demand\n"));
2026 #endif
2027 
2028  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2029  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2030 }
#define printf(...)
Definition: port.h:244

References _, printf, and progname.

Referenced by main().

◆ do_init()

static void do_init ( void  )
static

Definition at line 894 of file pg_ctl.c.

895 {
896  char *cmd;
897 
898  if (exec_path == NULL)
899  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
900 
901  if (pgdata_opt == NULL)
902  pgdata_opt = "";
903 
904  if (post_opts == NULL)
905  post_opts = "";
906 
907  if (!silent_mode)
908  cmd = psprintf("\"%s\" %s%s",
910  else
911  cmd = psprintf("\"%s\" %s%s > \"%s\"",
913 
914  fflush(NULL);
915  if (system(cmd) != 0)
916  {
917  write_stderr(_("%s: database system initialization failed\n"), progname);
918  exit(1);
919  }
920 }
static bool silent_mode
Definition: pg_ctl.c:77
#define DEVNULL
Definition: port.h:160

References _, argv0, DEVNULL, exec_path, exit(), fflush(), find_other_exec_or_die(), pgdata_opt, post_opts, progname, psprintf(), silent_mode, and write_stderr().

Referenced by main(), and PQinitSSL().

◆ do_kill()

static void do_kill ( pid_t  pid)
static

Definition at line 1393 of file pg_ctl.c.

1394 {
1395  if (kill(pid, sig) != 0)
1396  {
1397  write_stderr(_("%s: could not send signal %d (PID: %d): %m\n"),
1398  progname, sig, (int) pid);
1399  exit(1);
1400  }
1401 }
static int sig
Definition: pg_ctl.c:79
#define kill(pid, sig)
Definition: win32_port.h:485

References _, exit(), kill, progname, sig, and write_stderr().

Referenced by main().

◆ do_logrotate()

static void do_logrotate ( void  )
static

Definition at line 1255 of file pg_ctl.c.

1256 {
1257  FILE *logrotatefile;
1258  pid_t pid;
1259 
1260  pid = get_pgpid(false);
1261 
1262  if (pid == 0) /* no pid file */
1263  {
1264  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1265  write_stderr(_("Is server running?\n"));
1266  exit(1);
1267  }
1268  else if (pid < 0) /* standalone backend, not postmaster */
1269  {
1270  pid = -pid;
1271  write_stderr(_("%s: cannot rotate log file; "
1272  "single-user server is running (PID: %d)\n"),
1273  progname, (int) pid);
1274  exit(1);
1275  }
1276 
1277  snprintf(logrotate_file, MAXPGPATH, "%s/logrotate", pg_data);
1278 
1279  if ((logrotatefile = fopen(logrotate_file, "w")) == NULL)
1280  {
1281  write_stderr(_("%s: could not create log rotation signal file \"%s\": %m\n"),
1283  exit(1);
1284  }
1285  if (fclose(logrotatefile))
1286  {
1287  write_stderr(_("%s: could not write log rotation signal file \"%s\": %m\n"),
1289  exit(1);
1290  }
1291 
1292  sig = SIGUSR1;
1293  if (kill(pid, sig) != 0)
1294  {
1295  write_stderr(_("%s: could not send log rotation signal (PID: %d): %m\n"),
1296  progname, (int) pid);
1297  if (unlink(logrotate_file) != 0)
1298  write_stderr(_("%s: could not remove log rotation signal file \"%s\": %m\n"),
1300  exit(1);
1301  }
1302 
1303  print_msg(_("server signaled to rotate log file\n"));
1304 }
static pid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:245
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:98
static void print_msg(const char *msg)
Definition: pg_ctl.c:235
static char logrotate_file[MAXPGPATH]
Definition: pg_ctl.c:100
#define SIGUSR1
Definition: win32_port.h:180

References _, exit(), get_pgpid(), kill, logrotate_file, MAXPGPATH, pg_data, pid_file, print_msg(), progname, sig, SIGUSR1, snprintf, and write_stderr().

Referenced by main().

◆ do_promote()

static void do_promote ( void  )
static

Definition at line 1174 of file pg_ctl.c.

1175 {
1176  FILE *prmfile;
1177  pid_t pid;
1178 
1179  pid = get_pgpid(false);
1180 
1181  if (pid == 0) /* no pid file */
1182  {
1183  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1184  write_stderr(_("Is server running?\n"));
1185  exit(1);
1186  }
1187  else if (pid < 0) /* standalone backend, not postmaster */
1188  {
1189  pid = -pid;
1190  write_stderr(_("%s: cannot promote server; "
1191  "single-user server is running (PID: %d)\n"),
1192  progname, (int) pid);
1193  exit(1);
1194  }
1195 
1197  {
1198  write_stderr(_("%s: cannot promote server; "
1199  "server is not in standby mode\n"),
1200  progname);
1201  exit(1);
1202  }
1203 
1204  snprintf(promote_file, MAXPGPATH, "%s/promote", pg_data);
1205 
1206  if ((prmfile = fopen(promote_file, "w")) == NULL)
1207  {
1208  write_stderr(_("%s: could not create promote signal file \"%s\": %m\n"),
1210  exit(1);
1211  }
1212  if (fclose(prmfile))
1213  {
1214  write_stderr(_("%s: could not write promote signal file \"%s\": %m\n"),
1216  exit(1);
1217  }
1218 
1219  sig = SIGUSR1;
1220  if (kill(pid, sig) != 0)
1221  {
1222  write_stderr(_("%s: could not send promote signal (PID: %d): %m\n"),
1223  progname, (int) pid);
1224  if (unlink(promote_file) != 0)
1225  write_stderr(_("%s: could not remove promote signal file \"%s\": %m\n"),
1227  exit(1);
1228  }
1229 
1230  if (do_wait)
1231  {
1232  print_msg(_("waiting for server to promote..."));
1234  {
1235  print_msg(_(" done\n"));
1236  print_msg(_("server promoted\n"));
1237  }
1238  else
1239  {
1240  print_msg(_(" stopped waiting\n"));
1241  write_stderr(_("%s: server did not promote in time\n"),
1242  progname);
1243  exit(1);
1244  }
1245  }
1246  else
1247  print_msg(_("server promoting\n"));
1248 }
@ DB_IN_ARCHIVE_RECOVERY
Definition: pg_control.h:95
static bool wait_for_postmaster_promote(void)
Definition: pg_ctl.c:746
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:99
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2171
static bool do_wait
Definition: pg_ctl.c:74

References _, DB_IN_ARCHIVE_RECOVERY, do_wait, exit(), get_control_dbstate(), get_pgpid(), kill, MAXPGPATH, pg_data, pid_file, print_msg(), progname, promote_file, sig, SIGUSR1, snprintf, wait_for_postmaster_promote(), and write_stderr().

Referenced by main().

◆ do_reload()

static void do_reload ( void  )
static

Definition at line 1137 of file pg_ctl.c.

1138 {
1139  pid_t pid;
1140 
1141  pid = get_pgpid(false);
1142  if (pid == 0) /* no pid file */
1143  {
1144  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1145  write_stderr(_("Is server running?\n"));
1146  exit(1);
1147  }
1148  else if (pid < 0) /* standalone backend, not postmaster */
1149  {
1150  pid = -pid;
1151  write_stderr(_("%s: cannot reload server; "
1152  "single-user server is running (PID: %d)\n"),
1153  progname, (int) pid);
1154  write_stderr(_("Please terminate the single-user server and try again.\n"));
1155  exit(1);
1156  }
1157 
1158  if (kill(pid, sig) != 0)
1159  {
1160  write_stderr(_("%s: could not send reload signal (PID: %d): %m\n"),
1161  progname, (int) pid);
1162  exit(1);
1163  }
1164 
1165  print_msg(_("server signaled\n"));
1166 }

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

Referenced by main().

◆ do_restart()

static void do_restart ( void  )
static

Definition at line 1073 of file pg_ctl.c.

1074 {
1075  pid_t pid;
1076 
1077  pid = get_pgpid(false);
1078 
1079  if (pid == 0) /* no pid file */
1080  {
1081  write_stderr(_("%s: PID file \"%s\" does not exist\n"),
1082  progname, pid_file);
1083  write_stderr(_("Is server running?\n"));
1084  write_stderr(_("trying to start server anyway\n"));
1085  do_start();
1086  return;
1087  }
1088  else if (pid < 0) /* standalone backend, not postmaster */
1089  {
1090  pid = -pid;
1091  if (postmaster_is_alive(pid))
1092  {
1093  write_stderr(_("%s: cannot restart server; "
1094  "single-user server is running (PID: %d)\n"),
1095  progname, (int) pid);
1096  write_stderr(_("Please terminate the single-user server and try again.\n"));
1097  exit(1);
1098  }
1099  }
1100 
1101  if (postmaster_is_alive(pid))
1102  {
1103  if (kill(pid, sig) != 0)
1104  {
1105  write_stderr(_("%s: could not send stop signal (PID: %d): %m\n"), progname, (int) pid);
1106  exit(1);
1107  }
1108 
1109  print_msg(_("waiting for server to shut down..."));
1110 
1111  /* always wait for restart */
1112  if (!wait_for_postmaster_stop())
1113  {
1114  print_msg(_(" failed\n"));
1115 
1116  write_stderr(_("%s: server does not shut down\n"), progname);
1117  if (shutdown_mode == SMART_MODE)
1118  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1119  "waiting for session-initiated disconnection.\n"));
1120  exit(1);
1121  }
1122 
1123  print_msg(_(" done\n"));
1124  print_msg(_("server stopped\n"));
1125  }
1126  else
1127  {
1128  write_stderr(_("%s: old server process (PID: %d) seems to be gone\n"),
1129  progname, (int) pid);
1130  write_stderr(_("starting server anyway\n"));
1131  }
1132 
1133  do_start();
1134 }
static void do_start(void)
Definition: pg_ctl.c:923
static bool postmaster_is_alive(pid_t pid)
Definition: pg_ctl.c:1312
static bool wait_for_postmaster_stop(void)
Definition: pg_ctl.c:709
static ShutdownMode shutdown_mode
Definition: pg_ctl.c:78

References _, do_start(), exit(), get_pgpid(), kill, pid_file, postmaster_is_alive(), print_msg(), progname, shutdown_mode, sig, SMART_MODE, wait_for_postmaster_stop(), and write_stderr().

Referenced by main().

◆ do_start()

static void do_start ( void  )
static

Definition at line 923 of file pg_ctl.c.

924 {
925  pid_t old_pid = 0;
926  pid_t pm_pid;
927 
929  {
930  old_pid = get_pgpid(false);
931  if (old_pid != 0)
932  write_stderr(_("%s: another server might be running; "
933  "trying to start server anyway\n"),
934  progname);
935  }
936 
937  read_post_opts();
938 
939  /* No -D or -D already added during server start */
940  if (ctl_command == RESTART_COMMAND || pgdata_opt == NULL)
941  pgdata_opt = "";
942 
943  if (exec_path == NULL)
945 
946 #if defined(HAVE_GETRLIMIT)
947  if (allow_core_files)
949 #endif
950 
951  /*
952  * If possible, tell the postmaster our parent shell's PID (see the
953  * comments in CreateLockFile() for motivation). Windows hasn't got
954  * getppid() unfortunately.
955  */
956 #ifndef WIN32
957  {
958  char env_var[32];
959 
960  snprintf(env_var, sizeof(env_var), "%d", (int) getppid());
961  setenv("PG_GRANDPARENT_PID", env_var, 1);
962  }
963 #endif
964 
965  pm_pid = start_postmaster();
966 
967  if (do_wait)
968  {
969  /*
970  * If the user interrupts the startup (e.g. with CTRL-C), we'd like to
971  * abort the server launch. Install a signal handler that will
972  * forward SIGINT to the postmaster process, while we wait.
973  *
974  * (We don't bother to reset the signal handler after the launch, as
975  * we're about to exit, anyway.)
976  */
977  postmasterPID = pm_pid;
979 
980  print_msg(_("waiting for server to start..."));
981 
982  switch (wait_for_postmaster_start(pm_pid, false))
983  {
984  case POSTMASTER_READY:
985  print_msg(_(" done\n"));
986  print_msg(_("server started\n"));
987  break;
989  print_msg(_(" stopped waiting\n"));
990  write_stderr(_("%s: server did not start in time\n"),
991  progname);
992  exit(1);
993  break;
994  case POSTMASTER_FAILED:
995  print_msg(_(" stopped waiting\n"));
996  write_stderr(_("%s: could not start server\n"
997  "Examine the log output.\n"),
998  progname);
999  exit(1);
1000  break;
1001  }
1002  }
1003  else
1004  print_msg(_("server starting\n"));
1005 
1006 #ifdef WIN32
1007  /* Now we don't need the handle to the shell process anymore */
1008  CloseHandle(postmasterProcess);
1009  postmasterProcess = INVALID_HANDLE_VALUE;
1010 #endif
1011 }
static void read_post_opts(void)
Definition: pg_ctl.c:794
static WaitPMResult wait_for_postmaster_start(pid_t pm_pid, bool do_checkpoint)
Definition: pg_ctl.c:592
static void trap_sigint_during_startup(SIGNAL_ARGS)
Definition: pg_ctl.c:849
static volatile pid_t postmasterPID
Definition: pg_ctl.c:102
static bool allow_core_files
Definition: pg_ctl.c:93
static void unlimit_core_size(void)
Definition: pg_ctl.c:774
static CtlCommand ctl_command
Definition: pg_ctl.c:80
static pid_t start_postmaster(void)
Definition: pg_ctl.c:439
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define setenv(x, y, z)
Definition: win32_port.h:537

References _, allow_core_files, argv0, ctl_command, do_wait, exec_path, exit(), find_other_exec_or_die(), get_pgpid(), PG_BACKEND_VERSIONSTR, pgdata_opt, POSTMASTER_FAILED, POSTMASTER_READY, POSTMASTER_STILL_STARTING, postmasterPID, pqsignal(), print_msg(), progname, read_post_opts(), RESTART_COMMAND, setenv, snprintf, start_postmaster(), trap_sigint_during_startup(), unlimit_core_size(), wait_for_postmaster_start(), and write_stderr().

Referenced by do_restart(), and main().

◆ do_status()

static void do_status ( void  )
static

Definition at line 1336 of file pg_ctl.c.

1337 {
1338  pid_t pid;
1339 
1340  pid = get_pgpid(true);
1341  /* Is there a pid file? */
1342  if (pid != 0)
1343  {
1344  /* standalone backend? */
1345  if (pid < 0)
1346  {
1347  pid = -pid;
1348  if (postmaster_is_alive(pid))
1349  {
1350  printf(_("%s: single-user server is running (PID: %d)\n"),
1351  progname, (int) pid);
1352  return;
1353  }
1354  }
1355  else
1356  /* must be a postmaster */
1357  {
1358  if (postmaster_is_alive(pid))
1359  {
1360  char **optlines;
1361  char **curr_line;
1362  int numlines;
1363 
1364  printf(_("%s: server is running (PID: %d)\n"),
1365  progname, (int) pid);
1366 
1367  optlines = readfile(postopts_file, &numlines);
1368  if (optlines != NULL)
1369  {
1370  for (curr_line = optlines; *curr_line != NULL; curr_line++)
1371  puts(*curr_line);
1372 
1373  /* Free the results of readfile */
1374  free_readfile(optlines);
1375  }
1376  return;
1377  }
1378  }
1379  }
1380  printf(_("%s: no server running\n"), progname);
1381 
1382  /*
1383  * The Linux Standard Base Core Specification 3.1 says this should return
1384  * '3, program is not running'
1385  * https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1386  */
1387  exit(3);
1388 }
static void free_readfile(char **optlines)
Definition: pg_ctl.c:409
static char ** readfile(const char *path, int *numlines)
Definition: pg_ctl.c:313
static char postopts_file[MAXPGPATH]
Definition: pg_ctl.c:96

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

Referenced by main().

◆ do_stop()

static void do_stop ( void  )
static

Definition at line 1015 of file pg_ctl.c.

1016 {
1017  pid_t pid;
1018 
1019  pid = get_pgpid(false);
1020 
1021  if (pid == 0) /* no pid file */
1022  {
1023  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1024  write_stderr(_("Is server running?\n"));
1025  exit(1);
1026  }
1027  else if (pid < 0) /* standalone backend, not postmaster */
1028  {
1029  pid = -pid;
1030  write_stderr(_("%s: cannot stop server; "
1031  "single-user server is running (PID: %d)\n"),
1032  progname, (int) pid);
1033  exit(1);
1034  }
1035 
1036  if (kill(pid, sig) != 0)
1037  {
1038  write_stderr(_("%s: could not send stop signal (PID: %d): %m\n"), progname, (int) pid);
1039  exit(1);
1040  }
1041 
1042  if (!do_wait)
1043  {
1044  print_msg(_("server shutting down\n"));
1045  return;
1046  }
1047  else
1048  {
1049  print_msg(_("waiting for server to shut down..."));
1050 
1051  if (!wait_for_postmaster_stop())
1052  {
1053  print_msg(_(" failed\n"));
1054 
1055  write_stderr(_("%s: server does not shut down\n"), progname);
1056  if (shutdown_mode == SMART_MODE)
1057  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1058  "waiting for session-initiated disconnection.\n"));
1059  exit(1);
1060  }
1061  print_msg(_(" done\n"));
1062 
1063  print_msg(_("server stopped\n"));
1064  }
1065 }

References _, do_wait, exit(), get_pgpid(), kill, pid_file, print_msg(), progname, shutdown_mode, sig, SMART_MODE, wait_for_postmaster_stop(), and write_stderr().

Referenced by main().

◆ find_other_exec_or_die()

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

Definition at line 867 of file pg_ctl.c.

868 {
869  int ret;
870  char *found_path;
871 
872  found_path = pg_malloc(MAXPGPATH);
873 
874  if ((ret = find_other_exec(argv0, target, versionstr, found_path)) < 0)
875  {
876  char full_path[MAXPGPATH];
877 
878  if (find_my_exec(argv0, full_path) < 0)
879  strlcpy(full_path, progname, sizeof(full_path));
880 
881  if (ret == -1)
882  write_stderr(_("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n"),
883  target, progname, full_path);
884  else
885  write_stderr(_("program \"%s\" was found by \"%s\" but was not the same version as %s\n"),
886  target, full_path, progname);
887  exit(1);
888  }
889 
890  return found_path;
891 }
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:329
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

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

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

◆ free_readfile()

static void free_readfile ( char **  optlines)
static

Definition at line 409 of file pg_ctl.c.

410 {
411  char *curr_line = NULL;
412  int i = 0;
413 
414  if (!optlines)
415  return;
416 
417  while ((curr_line = optlines[i++]))
418  free(curr_line);
419 
420  free(optlines);
421 }
int i
Definition: isn.c:73

References free, and i.

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

◆ get_control_dbstate()

static DBState get_control_dbstate ( void  )
static

Definition at line 2171 of file pg_ctl.c.

2172 {
2173  DBState ret;
2174  bool crc_ok;
2175  ControlFileData *control_file_data = get_controlfile(pg_data, &crc_ok);
2176 
2177  if (!crc_ok)
2178  {
2179  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2180  exit(1);
2181  }
2182 
2183  ret = control_file_data->state;
2184  pfree(control_file_data);
2185  return ret;
2186 }
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
void pfree(void *pointer)
Definition: mcxt.c:1508
DBState
Definition: pg_control.h:89

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

Referenced by do_promote(), and wait_for_postmaster_promote().

◆ get_pgpid()

static pid_t get_pgpid ( bool  is_status_request)
static

Definition at line 245 of file pg_ctl.c.

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

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

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 2190 of file pg_ctl.c.

2191 {
2192  static struct option long_options[] = {
2193  {"help", no_argument, NULL, '?'},
2194  {"version", no_argument, NULL, 'V'},
2195  {"log", required_argument, NULL, 'l'},
2196  {"mode", required_argument, NULL, 'm'},
2197  {"pgdata", required_argument, NULL, 'D'},
2198  {"options", required_argument, NULL, 'o'},
2199  {"silent", no_argument, NULL, 's'},
2200  {"timeout", required_argument, NULL, 't'},
2201  {"core-files", no_argument, NULL, 'c'},
2202  {"wait", no_argument, NULL, 'w'},
2203  {"no-wait", no_argument, NULL, 'W'},
2204  {NULL, 0, NULL, 0}
2205  };
2206 
2207  char *env_wait;
2208  int option_index;
2209  int c;
2210  pid_t killproc = 0;
2211 
2212  pg_logging_init(argv[0]);
2213  progname = get_progname(argv[0]);
2214  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));
2215  start_time = time(NULL);
2216 
2217  /*
2218  * save argv[0] so do_start() can look for the postmaster if necessary. we
2219  * don't look for postmaster here because in many cases we won't need it.
2220  */
2221  argv0 = argv[0];
2222 
2223  /* Set restrictive mode mask until PGDATA permissions are checked */
2224  umask(PG_MODE_MASK_OWNER);
2225 
2226  /* support --help and --version even if invoked as root */
2227  if (argc > 1)
2228  {
2229  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2230  {
2231  do_help();
2232  exit(0);
2233  }
2234  else if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2235  {
2236  puts("pg_ctl (PostgreSQL) " PG_VERSION);
2237  exit(0);
2238  }
2239  }
2240 
2241  /*
2242  * Disallow running as root, to forestall any possible security holes.
2243  */
2244 #ifndef WIN32
2245  if (geteuid() == 0)
2246  {
2247  write_stderr(_("%s: cannot be run as root\n"
2248  "Please log in (using, e.g., \"su\") as the "
2249  "(unprivileged) user that will\n"
2250  "own the server process.\n"),
2251  progname);
2252  exit(1);
2253  }
2254 #endif
2255 
2256  env_wait = getenv("PGCTLTIMEOUT");
2257  if (env_wait != NULL)
2258  wait_seconds = atoi(env_wait);
2259 
2260  /* process command-line options */
2261  while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW",
2262  long_options, &option_index)) != -1)
2263  {
2264  switch (c)
2265  {
2266  case 'D':
2267  {
2268  char *pgdata_D;
2269 
2270  pgdata_D = pg_strdup(optarg);
2271  canonicalize_path(pgdata_D);
2272  setenv("PGDATA", pgdata_D, 1);
2273 
2274  /*
2275  * We could pass PGDATA just in an environment variable
2276  * but we do -D too for clearer postmaster 'ps' display
2277  */
2278  pgdata_opt = psprintf("-D \"%s\" ", pgdata_D);
2279  free(pgdata_D);
2280  break;
2281  }
2282  case 'e':
2284  break;
2285  case 'l':
2287  break;
2288  case 'm':
2289  set_mode(optarg);
2290  break;
2291  case 'N':
2293  break;
2294  case 'o':
2295  /* append option? */
2296  if (!post_opts)
2298  else
2299  {
2300  char *old_post_opts = post_opts;
2301 
2302  post_opts = psprintf("%s %s", old_post_opts, optarg);
2303  free(old_post_opts);
2304  }
2305  break;
2306  case 'p':
2308  break;
2309  case 'P':
2311  break;
2312  case 's':
2313  silent_mode = true;
2314  break;
2315  case 'S':
2316 #ifdef WIN32
2317  set_starttype(optarg);
2318 #else
2319  write_stderr(_("%s: -S option not supported on this platform\n"),
2320  progname);
2321  exit(1);
2322 #endif
2323  break;
2324  case 't':
2325  wait_seconds = atoi(optarg);
2326  wait_seconds_arg = true;
2327  break;
2328  case 'U':
2329  if (strchr(optarg, '\\'))
2331  else
2332  /* Prepend .\ for local accounts */
2333  register_username = psprintf(".\\%s", optarg);
2334  break;
2335  case 'w':
2336  do_wait = true;
2337  break;
2338  case 'W':
2339  do_wait = false;
2340  break;
2341  case 'c':
2342  allow_core_files = true;
2343  break;
2344  default:
2345  /* getopt_long already issued a suitable error message */
2346  do_advice();
2347  exit(1);
2348  }
2349  }
2350 
2351  /* Process an action */
2352  if (optind < argc)
2353  {
2354  if (strcmp(argv[optind], "init") == 0
2355  || strcmp(argv[optind], "initdb") == 0)
2357  else if (strcmp(argv[optind], "start") == 0)
2359  else if (strcmp(argv[optind], "stop") == 0)
2361  else if (strcmp(argv[optind], "restart") == 0)
2363  else if (strcmp(argv[optind], "reload") == 0)
2365  else if (strcmp(argv[optind], "status") == 0)
2367  else if (strcmp(argv[optind], "promote") == 0)
2369  else if (strcmp(argv[optind], "logrotate") == 0)
2371  else if (strcmp(argv[optind], "kill") == 0)
2372  {
2373  if (argc - optind < 3)
2374  {
2375  write_stderr(_("%s: missing arguments for kill mode\n"), progname);
2376  do_advice();
2377  exit(1);
2378  }
2380  set_sig(argv[++optind]);
2381  killproc = atol(argv[++optind]);
2382  }
2383 #ifdef WIN32
2384  else if (strcmp(argv[optind], "register") == 0)
2386  else if (strcmp(argv[optind], "unregister") == 0)
2388  else if (strcmp(argv[optind], "runservice") == 0)
2390 #endif
2391  else
2392  {
2393  write_stderr(_("%s: unrecognized operation mode \"%s\"\n"), progname, argv[optind]);
2394  do_advice();
2395  exit(1);
2396  }
2397  optind++;
2398  }
2399 
2400  if (optind < argc)
2401  {
2402  write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]);
2403  do_advice();
2404  exit(1);
2405  }
2406 
2407  if (ctl_command == NO_COMMAND)
2408  {
2409  write_stderr(_("%s: no operation specified\n"), progname);
2410  do_advice();
2411  exit(1);
2412  }
2413 
2414  /* Note we put any -D switch into the env var above */
2415  pg_config = getenv("PGDATA");
2416  if (pg_config)
2417  {
2421  }
2422 
2423  /* -D might point at config-only directory; if so find the real PGDATA */
2424  adjust_data_dir();
2425 
2426  /* Complain if -D needed and not provided */
2427  if (pg_config == NULL &&
2429  {
2430  write_stderr(_("%s: no database directory specified and environment variable PGDATA unset\n"),
2431  progname);
2432  do_advice();
2433  exit(1);
2434  }
2435 
2436  if (ctl_command == RELOAD_COMMAND)
2437  {
2438  sig = SIGHUP;
2439  do_wait = false;
2440  }
2441 
2442  if (pg_data)
2443  {
2444  snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data);
2445  snprintf(version_file, MAXPGPATH, "%s/PG_VERSION", pg_data);
2446  snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pg_data);
2447 
2448  /*
2449  * Set mask based on PGDATA permissions,
2450  *
2451  * Don't error here if the data directory cannot be stat'd. This is
2452  * handled differently based on the command and we don't want to
2453  * interfere with that logic.
2454  */
2456  umask(pg_mode_mask);
2457  }
2458 
2459  switch (ctl_command)
2460  {
2461  case INIT_COMMAND:
2462  do_init();
2463  break;
2464  case STATUS_COMMAND:
2465  do_status();
2466  break;
2467  case START_COMMAND:
2468  do_start();
2469  break;
2470  case STOP_COMMAND:
2471  do_stop();
2472  break;
2473  case RESTART_COMMAND:
2474  do_restart();
2475  break;
2476  case RELOAD_COMMAND:
2477  do_reload();
2478  break;
2479  case PROMOTE_COMMAND:
2480  do_promote();
2481  break;
2482  case LOGROTATE_COMMAND:
2483  do_logrotate();
2484  break;
2485  case KILL_COMMAND:
2486  do_kill(killproc);
2487  break;
2488 #ifdef WIN32
2489  case REGISTER_COMMAND:
2490  pgwin32_doRegister();
2491  break;
2492  case UNREGISTER_COMMAND:
2493  pgwin32_doUnregister();
2494  break;
2496  pgwin32_doRunAsService();
2497  break;
2498 #endif
2499  default:
2500  break;
2501  }
2502 
2503  exit(0);
2504 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1201
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:448
int pg_mode_mask
Definition: file_perm.c:25
bool GetDataDirectoryCreatePerm(const char *dataDir)
#define PG_MODE_MASK_OWNER
Definition: file_perm.h:24
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
void pg_logging_init(const char *argv0)
Definition: logging.c:83
static void static void do_advice(void)
Definition: pg_ctl.c:1953
static void do_reload(void)
Definition: pg_ctl.c:1137
static void do_help(void)
Definition: pg_ctl.c:1961
static char * register_username
Definition: pg_ctl.c:90
static char * event_source
Definition: pg_ctl.c:88
static void adjust_data_dir(void)
Definition: pg_ctl.c:2113
static char * register_servicename
Definition: pg_ctl.c:89
static void set_mode(char *modeopt)
Definition: pg_ctl.c:2035
static void do_logrotate(void)
Definition: pg_ctl.c:1255
static void do_stop(void)
Definition: pg_ctl.c:1015
static char * register_password
Definition: pg_ctl.c:91
static void do_init(void)
Definition: pg_ctl.c:894
static int wait_seconds
Definition: pg_ctl.c:75
static void do_restart(void)
Definition: pg_ctl.c:1073
static bool wait_seconds_arg
Definition: pg_ctl.c:76
static char * log_file
Definition: pg_ctl.c:86
static void do_promote(void)
Definition: pg_ctl.c:1174
static time_t start_time
Definition: pg_ctl.c:94
static void set_sig(char *signame)
Definition: pg_ctl.c:2063
static void do_status(void)
Definition: pg_ctl.c:1336
static void do_kill(pid_t pid)
Definition: pg_ctl.c:1393
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
const char * get_progname(const char *argv0)
Definition: path.c:574
char * c
#define SIGHUP
Definition: win32_port.h:168

References _, adjust_data_dir(), allow_core_files, argv0, canonicalize_path(), ctl_command, do_advice(), do_help(), do_init(), do_kill(), do_logrotate(), do_promote(), do_reload(), do_restart(), do_start(), do_status(), do_stop(), do_wait, event_source, exec_path, exit(), free, get_progname(), GetDataDirectoryCreatePerm(), getopt_long(), INIT_COMMAND, KILL_COMMAND, log_file, LOGROTATE_COMMAND, MAXPGPATH, no_argument, NO_COMMAND, optarg, optind, pg_config, pg_data, pg_logging_init(), pg_mode_mask, PG_MODE_MASK_OWNER, pg_strdup(), PG_TEXTDOMAIN, pgdata_opt, pid_file, post_opts, postopts_file, progname, PROMOTE_COMMAND, psprintf(), REGISTER_COMMAND, register_password, register_servicename, register_username, RELOAD_COMMAND, required_argument, RESTART_COMMAND, RUN_AS_SERVICE_COMMAND, set_mode(), set_pglocale_pgservice(), set_sig(), setenv, 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().

◆ postmaster_is_alive()

static bool postmaster_is_alive ( pid_t  pid)
static

Definition at line 1312 of file pg_ctl.c.

1313 {
1314  /*
1315  * Test to see if the process is still there. Note that we do not
1316  * consider an EPERM failure to mean that the process is still there;
1317  * EPERM must mean that the given PID belongs to some other userid, and
1318  * considering the permissions on $PGDATA, that means it's not the
1319  * postmaster we are after.
1320  *
1321  * Don't believe that our own PID or parent shell's PID is the postmaster,
1322  * either. (Windows hasn't got getppid(), though.)
1323  */
1324  if (pid == getpid())
1325  return false;
1326 #ifndef WIN32
1327  if (pid == getppid())
1328  return false;
1329 #endif
1330  if (kill(pid, 0) == 0)
1331  return true;
1332  return false;
1333 }

References kill.

Referenced by do_restart(), and do_status().

◆ print_msg()

static void print_msg ( const char *  msg)
static

◆ read_post_opts()

static void read_post_opts ( void  )
static

Definition at line 794 of file pg_ctl.c.

795 {
796  if (post_opts == NULL)
797  {
798  post_opts = ""; /* default */
800  {
801  char **optlines;
802  int numlines;
803 
804  optlines = readfile(postopts_file, &numlines);
805  if (optlines == NULL)
806  {
807  write_stderr(_("%s: could not read file \"%s\"\n"), progname, postopts_file);
808  exit(1);
809  }
810  else if (numlines != 1)
811  {
812  write_stderr(_("%s: option file \"%s\" must have exactly one line\n"),
814  exit(1);
815  }
816  else
817  {
818  char *optline;
819  char *arg1;
820 
821  optline = optlines[0];
822 
823  /*
824  * Are we at the first option, as defined by space and
825  * double-quote?
826  */
827  if ((arg1 = strstr(optline, " \"")) != NULL)
828  {
829  *arg1 = '\0'; /* terminate so we get only program name */
830  post_opts = pg_strdup(arg1 + 1); /* point past whitespace */
831  }
832  if (exec_path == NULL)
833  exec_path = pg_strdup(optline);
834  }
835 
836  /* Free the results of readfile. */
837  free_readfile(optlines);
838  }
839  }
840 }

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

Referenced by do_start().

◆ readfile()

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

Definition at line 313 of file pg_ctl.c.

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

References close, fd(), free, fstat, i, len, PG_BINARY, pg_malloc(), read, and stat::st_size.

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

◆ set_mode()

static void set_mode ( char *  modeopt)
static

Definition at line 2035 of file pg_ctl.c.

2036 {
2037  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
2038  {
2040  sig = SIGTERM;
2041  }
2042  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
2043  {
2045  sig = SIGINT;
2046  }
2047  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
2048  {
2050  sig = SIGQUIT;
2051  }
2052  else
2053  {
2054  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2055  do_advice();
2056  exit(1);
2057  }
2058 }
#define SIGQUIT
Definition: win32_port.h:169

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

Referenced by main().

◆ set_sig()

static void set_sig ( char *  signame)
static

Definition at line 2063 of file pg_ctl.c.

2064 {
2065  if (strcmp(signame, "HUP") == 0)
2066  sig = SIGHUP;
2067  else if (strcmp(signame, "INT") == 0)
2068  sig = SIGINT;
2069  else if (strcmp(signame, "QUIT") == 0)
2070  sig = SIGQUIT;
2071  else if (strcmp(signame, "ABRT") == 0)
2072  sig = SIGABRT;
2073  else if (strcmp(signame, "KILL") == 0)
2074  sig = SIGKILL;
2075  else if (strcmp(signame, "TERM") == 0)
2076  sig = SIGTERM;
2077  else if (strcmp(signame, "USR1") == 0)
2078  sig = SIGUSR1;
2079  else if (strcmp(signame, "USR2") == 0)
2080  sig = SIGUSR2;
2081  else
2082  {
2083  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2084  do_advice();
2085  exit(1);
2086  }
2087 }
#define SIGABRT
Definition: win32_port.h:171
#define SIGUSR2
Definition: win32_port.h:181
#define SIGKILL
Definition: win32_port.h:172

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

Referenced by main().

◆ start_postmaster()

static pid_t start_postmaster ( void  )
static

Definition at line 439 of file pg_ctl.c.

440 {
441  char *cmd;
442 
443 #ifndef WIN32
444  pid_t pm_pid;
445 
446  /* Flush stdio channels just before fork, to avoid double-output problems */
447  fflush(NULL);
448 
449 #ifdef EXEC_BACKEND
450  pg_disable_aslr();
451 #endif
452 
453  pm_pid = fork();
454  if (pm_pid < 0)
455  {
456  /* fork failed */
457  write_stderr(_("%s: could not start server: %m\n"),
458  progname);
459  exit(1);
460  }
461  if (pm_pid > 0)
462  {
463  /* fork succeeded, in parent */
464  return pm_pid;
465  }
466 
467  /* fork succeeded, in child */
468 
469  /*
470  * If possible, detach the postmaster process from the launching process
471  * group and make it a group leader, so that it doesn't get signaled along
472  * with the current group that launched it.
473  */
474 #ifdef HAVE_SETSID
475  if (setsid() < 0)
476  {
477  write_stderr(_("%s: could not start server due to setsid() failure: %m\n"),
478  progname);
479  exit(1);
480  }
481 #endif
482 
483  /*
484  * Since there might be quotes to handle here, it is easier simply to pass
485  * everything to a shell to process them. Use exec so that the postmaster
486  * has the same PID as the current child process.
487  */
488  if (log_file != NULL)
489  cmd = psprintf("exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
491  DEVNULL, log_file);
492  else
493  cmd = psprintf("exec \"%s\" %s%s < \"%s\" 2>&1",
495 
496  (void) execl("/bin/sh", "/bin/sh", "-c", cmd, (char *) NULL);
497 
498  /* exec failed */
499  write_stderr(_("%s: could not start server: %m\n"),
500  progname);
501  exit(1);
502 
503  return 0; /* keep dumb compilers quiet */
504 
505 #else /* WIN32 */
506 
507  /*
508  * As with the Unix case, it's easiest to use the shell (CMD.EXE) to
509  * handle redirection etc. Unfortunately CMD.EXE lacks any equivalent of
510  * "exec", so we don't get to find out the postmaster's PID immediately.
511  */
512  PROCESS_INFORMATION pi;
513  const char *comspec;
514 
515  /* Find CMD.EXE location using COMSPEC, if it's set */
516  comspec = getenv("COMSPEC");
517  if (comspec == NULL)
518  comspec = "CMD";
519 
520  if (log_file != NULL)
521  {
522  /*
523  * First, open the log file if it exists. The idea is that if the
524  * file is still locked by a previous postmaster run, we'll wait until
525  * it comes free, instead of failing with ERROR_SHARING_VIOLATION.
526  * (It'd be better to open the file in a sharing-friendly mode, but we
527  * can't use CMD.EXE to do that, so work around it. Note that the
528  * previous postmaster will still have the file open for a short time
529  * after removing postmaster.pid.)
530  *
531  * If the log file doesn't exist, we *must not* create it here. If we
532  * were launched with higher privileges than the restricted process
533  * will have, the log file might end up with permissions settings that
534  * prevent the postmaster from writing on it.
535  */
536  int fd = open(log_file, O_RDWR, 0);
537 
538  if (fd == -1)
539  {
540  /*
541  * ENOENT is expectable since we didn't use O_CREAT. Otherwise
542  * complain. We could just fall through and let CMD.EXE report
543  * the problem, but its error reporting is pretty miserable.
544  */
545  if (errno != ENOENT)
546  {
547  write_stderr(_("%s: could not open log file \"%s\": %m\n"),
548  progname, log_file);
549  exit(1);
550  }
551  }
552  else
553  close(fd);
554 
555  cmd = psprintf("\"%s\" /D /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
557  }
558  else
559  cmd = psprintf("\"%s\" /D /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
560  comspec, exec_path, pgdata_opt, post_opts, DEVNULL);
561 
562  if (!CreateRestrictedProcess(cmd, &pi, false))
563  {
564  write_stderr(_("%s: could not start server: error code %lu\n"),
565  progname, (unsigned long) GetLastError());
566  exit(1);
567  }
568  /* Don't close command process handle here; caller must do so */
569  postmasterProcess = pi.hProcess;
570  CloseHandle(pi.hThread);
571  return pi.dwProcessId; /* Shell's PID, not postmaster's! */
572 #endif /* WIN32 */
573 }

References _, close, DEVNULL, exec_path, exit(), fd(), fflush(), log_file, pgdata_opt, post_opts, progname, psprintf(), and write_stderr().

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

◆ trap_sigint_during_startup()

static void trap_sigint_during_startup ( SIGNAL_ARGS  )
static

Definition at line 849 of file pg_ctl.c.

850 {
851  if (postmasterPID != -1)
852  {
853  if (kill(postmasterPID, SIGINT) != 0)
854  write_stderr(_("%s: could not send stop signal (PID: %d): %m\n"),
855  progname, (int) postmasterPID);
856  }
857 
858  /*
859  * Clear the signal handler, and send the signal again, to terminate the
860  * process as normal.
861  */
862  pqsignal(postgres_signal_arg, SIG_DFL);
863  raise(postgres_signal_arg);
864 }
#define SIG_DFL
Definition: win32_port.h:163

References _, kill, postmasterPID, pqsignal(), progname, SIG_DFL, and write_stderr().

Referenced by do_start().

◆ unlimit_core_size()

static void unlimit_core_size ( void  )
static

Definition at line 774 of file pg_ctl.c.

775 {
776  struct rlimit lim;
777 
778  getrlimit(RLIMIT_CORE, &lim);
779  if (lim.rlim_max == 0)
780  {
781  write_stderr(_("%s: cannot set core file size limit; disallowed by hard limit\n"),
782  progname);
783  return;
784  }
785  else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
786  {
787  lim.rlim_cur = lim.rlim_max;
788  setrlimit(RLIMIT_CORE, &lim);
789  }
790 }

References _, progname, and write_stderr().

Referenced by do_start().

◆ wait_for_postmaster_promote()

static bool wait_for_postmaster_promote ( void  )
static

Definition at line 746 of file pg_ctl.c.

747 {
748  int cnt;
749 
750  for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
751  {
752  pid_t pid;
753  DBState state;
754 
755  if ((pid = get_pgpid(false)) == 0)
756  return false; /* pid file is gone */
757  if (kill(pid, 0) != 0)
758  return false; /* postmaster died */
759 
761  if (state == DB_IN_PRODUCTION)
762  return true; /* successful promotion */
763 
764  if (cnt % WAITS_PER_SEC == 0)
765  print_msg(".");
767  }
768  return false; /* timeout reached */
769 }
@ DB_IN_PRODUCTION
Definition: pg_control.h:96
#define WAITS_PER_SEC
Definition: pg_ctl.c:72
#define USEC_PER_SEC
Definition: pg_ctl.c:70
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: regguts.h:323

References DB_IN_PRODUCTION, get_control_dbstate(), get_pgpid(), kill, pg_usleep(), print_msg(), USEC_PER_SEC, wait_seconds, and WAITS_PER_SEC.

Referenced by do_promote().

◆ wait_for_postmaster_start()

static WaitPMResult wait_for_postmaster_start ( pid_t  pm_pid,
bool  do_checkpoint 
)
static

Definition at line 592 of file pg_ctl.c.

593 {
594  int i;
595 
596  for (i = 0; i < wait_seconds * WAITS_PER_SEC; i++)
597  {
598  char **optlines;
599  int numlines;
600 
601  /*
602  * Try to read the postmaster.pid file. If it's not valid, or if the
603  * status line isn't there yet, just keep waiting.
604  */
605  if ((optlines = readfile(pid_file, &numlines)) != NULL &&
606  numlines >= LOCK_FILE_LINE_PM_STATUS)
607  {
608  /* File is complete enough for us, parse it */
609  pid_t pmpid;
610  time_t pmstart;
611 
612  /*
613  * Make sanity checks. If it's for the wrong PID, or the recorded
614  * start time is before pg_ctl started, then either we are looking
615  * at the wrong data directory, or this is a pre-existing pidfile
616  * that hasn't (yet?) been overwritten by our child postmaster.
617  * Allow 2 seconds slop for possible cross-process clock skew.
618  */
619  pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]);
620  pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]);
621  if (pmstart >= start_time - 2 &&
622 #ifndef WIN32
623  pmpid == pm_pid
624 #else
625  /* Windows can only reject standalone-backend PIDs */
626  pmpid > 0
627 #endif
628  )
629  {
630  /*
631  * OK, seems to be a valid pidfile from our child. Check the
632  * status line (this assumes a v10 or later server).
633  */
634  char *pmstatus = optlines[LOCK_FILE_LINE_PM_STATUS - 1];
635 
636  if (strcmp(pmstatus, PM_STATUS_READY) == 0 ||
637  strcmp(pmstatus, PM_STATUS_STANDBY) == 0)
638  {
639  /* postmaster is done starting up */
640  free_readfile(optlines);
641  return POSTMASTER_READY;
642  }
643  }
644  }
645 
646  /*
647  * Free the results of readfile.
648  *
649  * This is safe to call even if optlines is NULL.
650  */
651  free_readfile(optlines);
652 
653  /*
654  * Check whether the child postmaster process is still alive. This
655  * lets us exit early if the postmaster fails during startup.
656  *
657  * On Windows, we may be checking the postmaster's parent shell, but
658  * that's fine for this purpose.
659  */
660 #ifndef WIN32
661  {
662  int exitstatus;
663 
664  if (waitpid(pm_pid, &exitstatus, WNOHANG) == pm_pid)
665  return POSTMASTER_FAILED;
666  }
667 #else
668  if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0)
669  return POSTMASTER_FAILED;
670 #endif
671 
672  /* Startup still in process; wait, printing a dot once per second */
673  if (i % WAITS_PER_SEC == 0)
674  {
675 #ifdef WIN32
676  if (do_checkpoint)
677  {
678  /*
679  * Increment the wait hint by 6 secs (connection timeout +
680  * sleep). We must do this to indicate to the SCM that our
681  * startup time is changing, otherwise it'll usually send a
682  * stop signal after 20 seconds, despite incrementing the
683  * checkpoint counter.
684  */
685  status.dwWaitHint += 6000;
686  status.dwCheckPoint++;
687  SetServiceStatus(hStatus, (LPSERVICE_STATUS) &status);
688  }
689  else
690 #endif
691  print_msg(".");
692  }
693 
695  }
696 
697  /* out of patience; report that postmaster is still starting up */
699 }
#define PM_STATUS_READY
Definition: pidfile.h:53
#define PM_STATUS_STANDBY
Definition: pidfile.h:54
#define LOCK_FILE_LINE_START_TIME
Definition: pidfile.h:39
#define LOCK_FILE_LINE_PM_STATUS
Definition: pidfile.h:44
#define LOCK_FILE_LINE_PID
Definition: pidfile.h:37

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, USEC_PER_SEC, wait_seconds, and WAITS_PER_SEC.

Referenced by do_start().

◆ wait_for_postmaster_stop()

static bool wait_for_postmaster_stop ( void  )
static

Definition at line 709 of file pg_ctl.c.

710 {
711  int cnt;
712 
713  for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
714  {
715  pid_t pid;
716 
717  if ((pid = get_pgpid(false)) == 0)
718  return true; /* pid file is gone */
719 
720  if (kill(pid, 0) != 0)
721  {
722  /*
723  * Postmaster seems to have died. Check the pid file once more to
724  * avoid a race condition, but give up waiting.
725  */
726  if (get_pgpid(false) == 0)
727  return true; /* pid file is gone */
728  return false; /* postmaster died untimely */
729  }
730 
731  if (cnt % WAITS_PER_SEC == 0)
732  print_msg(".");
734  }
735  return false; /* timeout reached */
736 }

References get_pgpid(), kill, pg_usleep(), print_msg(), USEC_PER_SEC, wait_seconds, and WAITS_PER_SEC.

Referenced by do_restart(), and do_stop().

◆ write_stderr()

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

Definition at line 201 of file pg_ctl.c.

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

References fmt, pgwin32_is_service(), va_end(), va_start(), vfprintf(), and vsnprintf.

Referenced by adjust_data_dir(), do_advice(), do_init(), do_kill(), do_logrotate(), 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(), trap_sigint_during_startup(), and unlimit_core_size().

Variable Documentation

◆ allow_core_files

bool allow_core_files = false
static

Definition at line 93 of file pg_ctl.c.

Referenced by do_start(), and main().

◆ argv0

◆ ctl_command

CtlCommand ctl_command = NO_COMMAND
static

Definition at line 80 of file pg_ctl.c.

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

◆ do_wait

bool do_wait = true
static

◆ event_source

char* event_source = NULL
static

Definition at line 88 of file pg_ctl.c.

Referenced by main().

◆ exec_path

char* exec_path = NULL
static

◆ log_file

char* log_file = NULL
static

Definition at line 86 of file pg_ctl.c.

Referenced by exec_prog(), main(), parallel_exec_prog(), and start_postmaster().

◆ logrotate_file

char logrotate_file[MAXPGPATH]
static

Definition at line 100 of file pg_ctl.c.

Referenced by do_logrotate().

◆ pg_config

char* pg_config = NULL
static

Definition at line 82 of file pg_ctl.c.

Referenced by adjust_data_dir(), and main().

◆ pg_data

char* pg_data = NULL
static

Definition at line 81 of file pg_ctl.c.

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

◆ pgdata_opt

char* pgdata_opt = NULL
static

Definition at line 83 of file pg_ctl.c.

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

◆ pid_file

char pid_file[MAXPGPATH]
static

◆ post_opts

char* post_opts = NULL
static

Definition at line 84 of file pg_ctl.c.

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

◆ postmasterPID

volatile pid_t postmasterPID = -1
static

Definition at line 102 of file pg_ctl.c.

Referenced by do_start(), and trap_sigint_during_startup().

◆ postopts_file

char postopts_file[MAXPGPATH]
static

Definition at line 96 of file pg_ctl.c.

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

◆ progname

◆ promote_file

char promote_file[MAXPGPATH]
static

Definition at line 99 of file pg_ctl.c.

Referenced by do_promote(), and pg_promote().

◆ register_password

char* register_password = NULL
static

Definition at line 91 of file pg_ctl.c.

Referenced by main().

◆ register_servicename

char* register_servicename = "PostgreSQL"
static

Definition at line 89 of file pg_ctl.c.

Referenced by main().

◆ register_username

char* register_username = NULL
static

Definition at line 90 of file pg_ctl.c.

Referenced by main().

◆ shutdown_mode

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 78 of file pg_ctl.c.

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

◆ sig

◆ silent_mode

bool silent_mode = false
static

Definition at line 77 of file pg_ctl.c.

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

◆ start_time

◆ version_file

char version_file[MAXPGPATH]
static

Definition at line 97 of file pg_ctl.c.

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

◆ wait_seconds

◆ wait_seconds_arg

bool wait_seconds_arg = false
static

Definition at line 76 of file pg_ctl.c.

Referenced by main().