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 2115 of file pg_ctl.c.

2116 {
2117  char filename[MAXPGPATH];
2118  char *my_exec_path,
2119  *cmd;
2120  FILE *fd;
2121 
2122  /* do nothing if we're working without knowledge of data dir */
2123  if (pg_config == NULL)
2124  return;
2125 
2126  /* If there is no postgresql.conf, it can't be a config-only dir */
2127  snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2128  if ((fd = fopen(filename, "r")) == NULL)
2129  return;
2130  fclose(fd);
2131 
2132  /* If PG_VERSION exists, it can't be a config-only dir */
2133  snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2134  if ((fd = fopen(filename, "r")) != NULL)
2135  {
2136  fclose(fd);
2137  return;
2138  }
2139 
2140  /* Must be a configuration directory, so find the data directory */
2141 
2142  /* we use a private my_exec_path to avoid interfering with later uses */
2143  if (exec_path == NULL)
2145  else
2147 
2148  /* it's important for -C to be the first option, see main.c */
2149  cmd = psprintf("\"%s\" -C data_directory %s%s",
2150  my_exec_path,
2151  pgdata_opt ? pgdata_opt : "",
2152  post_opts ? post_opts : "");
2153  fflush(NULL);
2154 
2155  fd = popen(cmd, "r");
2156  if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL || pclose(fd) != 0)
2157  {
2158  write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2159  exit(1);
2160  }
2161  free(my_exec_path);
2162 
2163  /* strip trailing newline and carriage return */
2164  (void) pg_strip_crlf(filename);
2165 
2166  free(pg_data);
2169 }
#define _(x)
Definition: elog.c:91
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char my_exec_path[MAXPGPATH]
Definition: globals.c:76
#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 1955 of file pg_ctl.c.

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

References _, progname, and write_stderr().

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

◆ do_help()

static void do_help ( void  )
static

Definition at line 1963 of file pg_ctl.c.

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

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

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

Referenced by main().

◆ do_logrotate()

static void do_logrotate ( void  )
static

Definition at line 1257 of file pg_ctl.c.

1258 {
1259  FILE *logrotatefile;
1260  pid_t pid;
1261 
1262  pid = get_pgpid(false);
1263 
1264  if (pid == 0) /* no pid file */
1265  {
1266  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1267  write_stderr(_("Is server running?\n"));
1268  exit(1);
1269  }
1270  else if (pid < 0) /* standalone backend, not postmaster */
1271  {
1272  pid = -pid;
1273  write_stderr(_("%s: cannot rotate log file; "
1274  "single-user server is running (PID: %d)\n"),
1275  progname, (int) pid);
1276  exit(1);
1277  }
1278 
1279  snprintf(logrotate_file, MAXPGPATH, "%s/logrotate", pg_data);
1280 
1281  if ((logrotatefile = fopen(logrotate_file, "w")) == NULL)
1282  {
1283  write_stderr(_("%s: could not create log rotation signal file \"%s\": %s\n"),
1284  progname, logrotate_file, strerror(errno));
1285  exit(1);
1286  }
1287  if (fclose(logrotatefile))
1288  {
1289  write_stderr(_("%s: could not write log rotation signal file \"%s\": %s\n"),
1290  progname, logrotate_file, strerror(errno));
1291  exit(1);
1292  }
1293 
1294  sig = SIGUSR1;
1295  if (kill(pid, sig) != 0)
1296  {
1297  write_stderr(_("%s: could not send log rotation signal (PID: %d): %s\n"),
1298  progname, (int) pid, strerror(errno));
1299  if (unlink(logrotate_file) != 0)
1300  write_stderr(_("%s: could not remove log rotation signal file \"%s\": %s\n"),
1301  progname, logrotate_file, strerror(errno));
1302  exit(1);
1303  }
1304 
1305  print_msg(_("server signaled to rotate log file\n"));
1306 }
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, strerror, and write_stderr().

Referenced by main().

◆ do_promote()

static void do_promote ( void  )
static

Definition at line 1176 of file pg_ctl.c.

1177 {
1178  FILE *prmfile;
1179  pid_t pid;
1180 
1181  pid = get_pgpid(false);
1182 
1183  if (pid == 0) /* no pid file */
1184  {
1185  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1186  write_stderr(_("Is server running?\n"));
1187  exit(1);
1188  }
1189  else if (pid < 0) /* standalone backend, not postmaster */
1190  {
1191  pid = -pid;
1192  write_stderr(_("%s: cannot promote server; "
1193  "single-user server is running (PID: %d)\n"),
1194  progname, (int) pid);
1195  exit(1);
1196  }
1197 
1199  {
1200  write_stderr(_("%s: cannot promote server; "
1201  "server is not in standby mode\n"),
1202  progname);
1203  exit(1);
1204  }
1205 
1206  snprintf(promote_file, MAXPGPATH, "%s/promote", pg_data);
1207 
1208  if ((prmfile = fopen(promote_file, "w")) == NULL)
1209  {
1210  write_stderr(_("%s: could not create promote signal file \"%s\": %s\n"),
1211  progname, promote_file, strerror(errno));
1212  exit(1);
1213  }
1214  if (fclose(prmfile))
1215  {
1216  write_stderr(_("%s: could not write promote signal file \"%s\": %s\n"),
1217  progname, promote_file, strerror(errno));
1218  exit(1);
1219  }
1220 
1221  sig = SIGUSR1;
1222  if (kill(pid, sig) != 0)
1223  {
1224  write_stderr(_("%s: could not send promote signal (PID: %d): %s\n"),
1225  progname, (int) pid, strerror(errno));
1226  if (unlink(promote_file) != 0)
1227  write_stderr(_("%s: could not remove promote signal file \"%s\": %s\n"),
1228  progname, promote_file, strerror(errno));
1229  exit(1);
1230  }
1231 
1232  if (do_wait)
1233  {
1234  print_msg(_("waiting for server to promote..."));
1236  {
1237  print_msg(_(" done\n"));
1238  print_msg(_("server promoted\n"));
1239  }
1240  else
1241  {
1242  print_msg(_(" stopped waiting\n"));
1243  write_stderr(_("%s: server did not promote in time\n"),
1244  progname);
1245  exit(1);
1246  }
1247  }
1248  else
1249  print_msg(_("server promoting\n"));
1250 }
@ 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:2173
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, strerror, wait_for_postmaster_promote(), and write_stderr().

Referenced by main().

◆ do_reload()

static void do_reload ( void  )
static

Definition at line 1139 of file pg_ctl.c.

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

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

Referenced by main().

◆ do_restart()

static void do_restart ( void  )
static

Definition at line 1074 of file pg_ctl.c.

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

1339 {
1340  pid_t pid;
1341 
1342  pid = get_pgpid(true);
1343  /* Is there a pid file? */
1344  if (pid != 0)
1345  {
1346  /* standalone backend? */
1347  if (pid < 0)
1348  {
1349  pid = -pid;
1350  if (postmaster_is_alive(pid))
1351  {
1352  printf(_("%s: single-user server is running (PID: %d)\n"),
1353  progname, (int) pid);
1354  return;
1355  }
1356  }
1357  else
1358  /* must be a postmaster */
1359  {
1360  if (postmaster_is_alive(pid))
1361  {
1362  char **optlines;
1363  char **curr_line;
1364  int numlines;
1365 
1366  printf(_("%s: server is running (PID: %d)\n"),
1367  progname, (int) pid);
1368 
1369  optlines = readfile(postopts_file, &numlines);
1370  if (optlines != NULL)
1371  {
1372  for (curr_line = optlines; *curr_line != NULL; curr_line++)
1373  puts(*curr_line);
1374 
1375  /* Free the results of readfile */
1376  free_readfile(optlines);
1377  }
1378  return;
1379  }
1380  }
1381  }
1382  printf(_("%s: no server running\n"), progname);
1383 
1384  /*
1385  * The Linux Standard Base Core Specification 3.1 says this should return
1386  * '3, program is not running'
1387  * https://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
1388  */
1389  exit(3);
1390 }
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): %s\n"), progname, (int) pid,
1039  strerror(errno));
1040  exit(1);
1041  }
1042 
1043  if (!do_wait)
1044  {
1045  print_msg(_("server shutting down\n"));
1046  return;
1047  }
1048  else
1049  {
1050  print_msg(_("waiting for server to shut down..."));
1051 
1052  if (!wait_for_postmaster_stop())
1053  {
1054  print_msg(_(" failed\n"));
1055 
1056  write_stderr(_("%s: server does not shut down\n"), progname);
1057  if (shutdown_mode == SMART_MODE)
1058  write_stderr(_("HINT: The \"-m fast\" option immediately disconnects sessions rather than\n"
1059  "waiting for session-initiated disconnection.\n"));
1060  exit(1);
1061  }
1062  print_msg(_(" done\n"));
1063 
1064  print_msg(_("server stopped\n"));
1065  }
1066 }

References _, do_wait, exit(), get_pgpid(), kill, pid_file, print_msg(), progname, shutdown_mode, sig, SMART_MODE, strerror, 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:158
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:327
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 2173 of file pg_ctl.c.

2174 {
2175  DBState ret;
2176  bool crc_ok;
2177  ControlFileData *control_file_data = get_controlfile(pg_data, &crc_ok);
2178 
2179  if (!crc_ok)
2180  {
2181  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2182  exit(1);
2183  }
2184 
2185  ret = control_file_data->state;
2186  pfree(control_file_data);
2187  return ret;
2188 }
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
void pfree(void *pointer)
Definition: mcxt.c:1456
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\": %s\n"), progname,
258  pg_data, strerror(errno));
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\": %s\n"),
284  progname, pid_file, strerror(errno));
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, strerror, 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 2192 of file pg_ctl.c.

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

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

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:1283
#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 2037 of file pg_ctl.c.

2038 {
2039  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
2040  {
2042  sig = SIGTERM;
2043  }
2044  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
2045  {
2047  sig = SIGINT;
2048  }
2049  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
2050  {
2052  sig = SIGQUIT;
2053  }
2054  else
2055  {
2056  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2057  do_advice();
2058  exit(1);
2059  }
2060 }
#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 2065 of file pg_ctl.c.

2066 {
2067  if (strcmp(signame, "HUP") == 0)
2068  sig = SIGHUP;
2069  else if (strcmp(signame, "INT") == 0)
2070  sig = SIGINT;
2071  else if (strcmp(signame, "QUIT") == 0)
2072  sig = SIGQUIT;
2073  else if (strcmp(signame, "ABRT") == 0)
2074  sig = SIGABRT;
2075  else if (strcmp(signame, "KILL") == 0)
2076  sig = SIGKILL;
2077  else if (strcmp(signame, "TERM") == 0)
2078  sig = SIGTERM;
2079  else if (strcmp(signame, "USR1") == 0)
2080  sig = SIGUSR1;
2081  else if (strcmp(signame, "USR2") == 0)
2082  sig = SIGUSR2;
2083  else
2084  {
2085  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2086  do_advice();
2087  exit(1);
2088  }
2089 }
#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: %s\n"),
458  progname, strerror(errno));
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: %s\n"),
478  progname, strerror(errno));
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: %s\n"),
500  progname, strerror(errno));
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\": %s\n"),
548  progname, log_file, strerror(errno));
549  exit(1);
550  }
551  }
552  else
553  close(fd);
554 
555  cmd = psprintf("\"%s\" /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
557  }
558  else
559  cmd = psprintf("\"%s\" /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(), strerror, 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): %s\n"),
855  progname, (int) postmasterPID, strerror(errno));
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, strerror, 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().