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/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 */
 

Typedefs

typedef long pgpid_t
 

Enumerations

enum  ShutdownMode { SMART_MODE , FAST_MODE , IMMEDIATE_MODE }
 
enum  WaitPMResult { POSTMASTER_READY , POSTMASTER_STILL_STARTING , POSTMASTER_FAILED }
 
enum  CtlCommand {
  NO_COMMAND = 0 , INIT_COMMAND , START_COMMAND , STOP_COMMAND ,
  RESTART_COMMAND , RELOAD_COMMAND , STATUS_COMMAND , PROMOTE_COMMAND ,
  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 (pgpid_t pid)
 
static void print_msg (const char *msg)
 
static void adjust_data_dir (void)
 
static pgpid_t get_pgpid (bool is_status_request)
 
static char ** readfile (const char *path, int *numlines)
 
static void free_readfile (char **optlines)
 
static pgpid_t start_postmaster (void)
 
static void read_post_opts (void)
 
static WaitPMResult wait_for_postmaster_start (pgpid_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 DBState get_control_dbstate (void)
 
static void trap_sigint_during_startup (int sig)
 
static char * find_other_exec_or_die (const char *argv0, const char *target, const char *versionstr)
 
int main (int argc, char **argv)
 

Variables

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

Macro Definition Documentation

◆ DEFAULT_WAIT

#define DEFAULT_WAIT   60

Definition at line 73 of file pg_ctl.c.

◆ USEC_PER_SEC

#define USEC_PER_SEC   1000000

Definition at line 75 of file pg_ctl.c.

◆ WAITS_PER_SEC

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

Definition at line 77 of file pg_ctl.c.

Typedef Documentation

◆ pgpid_t

typedef long pgpid_t

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

57 {
58  NO_COMMAND = 0,
71 } CtlCommand;
CtlCommand
Definition: pg_ctl.c:57
@ REGISTER_COMMAND
Definition: pg_ctl.c:68
@ RUN_AS_SERVICE_COMMAND
Definition: pg_ctl.c:70
@ LOGROTATE_COMMAND
Definition: pg_ctl.c:66
@ RESTART_COMMAND
Definition: pg_ctl.c:62
@ UNREGISTER_COMMAND
Definition: pg_ctl.c:69
@ START_COMMAND
Definition: pg_ctl.c:60
@ KILL_COMMAND
Definition: pg_ctl.c:67
@ INIT_COMMAND
Definition: pg_ctl.c:59
@ STOP_COMMAND
Definition: pg_ctl.c:61
@ STATUS_COMMAND
Definition: pg_ctl.c:64
@ NO_COMMAND
Definition: pg_ctl.c:58
@ PROMOTE_COMMAND
Definition: pg_ctl.c:65
@ RELOAD_COMMAND
Definition: pg_ctl.c:63

◆ ShutdownMode

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 42 of file pg_ctl.c.

43 {
44  SMART_MODE,
45  FAST_MODE,
47 } ShutdownMode;
ShutdownMode
Definition: pg_ctl.c:43
@ FAST_MODE
Definition: pg_ctl.c:45
@ IMMEDIATE_MODE
Definition: pg_ctl.c:46
@ SMART_MODE
Definition: pg_ctl.c:44

◆ WaitPMResult

Enumerator
POSTMASTER_READY 
POSTMASTER_STILL_STARTING 
POSTMASTER_FAILED 

Definition at line 49 of file pg_ctl.c.

50 {
54 } WaitPMResult;
WaitPMResult
Definition: pg_ctl.c:50
@ POSTMASTER_FAILED
Definition: pg_ctl.c:53
@ POSTMASTER_READY
Definition: pg_ctl.c:51
@ POSTMASTER_STILL_STARTING
Definition: pg_ctl.c:52

Function Documentation

◆ adjust_data_dir()

static void adjust_data_dir ( void  )
static

Definition at line 2211 of file pg_ctl.c.

2212 {
2213  char filename[MAXPGPATH];
2214  char *my_exec_path,
2215  *cmd;
2216  FILE *fd;
2217 
2218  /* do nothing if we're working without knowledge of data dir */
2219  if (pg_config == NULL)
2220  return;
2221 
2222  /* If there is no postgresql.conf, it can't be a config-only dir */
2223  snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2224  if ((fd = fopen(filename, "r")) == NULL)
2225  return;
2226  fclose(fd);
2227 
2228  /* If PG_VERSION exists, it can't be a config-only dir */
2229  snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2230  if ((fd = fopen(filename, "r")) != NULL)
2231  {
2232  fclose(fd);
2233  return;
2234  }
2235 
2236  /* Must be a configuration directory, so find the data directory */
2237 
2238  /* we use a private my_exec_path to avoid interfering with later uses */
2239  if (exec_path == NULL)
2241  else
2243 
2244  /* it's important for -C to be the first option, see main.c */
2245  cmd = psprintf("\"%s\" -C data_directory %s%s",
2246  my_exec_path,
2247  pgdata_opt ? pgdata_opt : "",
2248  post_opts ? post_opts : "");
2249 
2250  fd = popen(cmd, "r");
2251  if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)
2252  {
2253  write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2254  exit(1);
2255  }
2256  pclose(fd);
2257  free(my_exec_path);
2258 
2259  /* strip trailing newline and carriage return */
2260  (void) pg_strip_crlf(filename);
2261 
2262  free(pg_data);
2265 }
#define _(x)
Definition: elog.c:89
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
exit(1)
#define MAXPGPATH
static char * post_opts
Definition: pg_ctl.c:89
static char * pgdata_opt
Definition: pg_ctl.c:88
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
Definition: pg_ctl.c:874
static char * pg_config
Definition: pg_ctl.c:87
static char * pg_data
Definition: pg_ctl.c:86
static char * argv0
Definition: pg_ctl.c:97
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:207
static const char * progname
Definition: pg_ctl.c:90
static char * exec_path
Definition: pg_ctl.c:92
static char * filename
Definition: pg_dumpall.c:94
void canonicalize_path(char *path)
Definition: path.c:264
#define snprintf
Definition: port.h:225
#define PG_BACKEND_VERSIONSTR
Definition: port.h:139
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:121

References _, argv0, canonicalize_path(), exec_path, exit(), fd(), 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 2051 of file pg_ctl.c.

2052 {
2053  write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
2054 }

References _, progname, and write_stderr().

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

◆ do_help()

static void do_help ( void  )
static

Definition at line 2059 of file pg_ctl.c.

2060 {
2061  printf(_("%s is a utility to initialize, start, stop, or control a PostgreSQL server.\n\n"), progname);
2062  printf(_("Usage:\n"));
2063  printf(_(" %s init[db] [-D DATADIR] [-s] [-o OPTIONS]\n"), progname);
2064  printf(_(" %s start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]\n"
2065  " [-o OPTIONS] [-p PATH] [-c]\n"), progname);
2066  printf(_(" %s stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"), progname);
2067  printf(_(" %s restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]\n"
2068  " [-o OPTIONS] [-c]\n"), progname);
2069  printf(_(" %s reload [-D DATADIR] [-s]\n"), progname);
2070  printf(_(" %s status [-D DATADIR]\n"), progname);
2071  printf(_(" %s promote [-D DATADIR] [-W] [-t SECS] [-s]\n"), progname);
2072  printf(_(" %s logrotate [-D DATADIR] [-s]\n"), progname);
2073  printf(_(" %s kill SIGNALNAME PID\n"), progname);
2074 #ifdef WIN32
2075  printf(_(" %s register [-D DATADIR] [-N SERVICENAME] [-U USERNAME] [-P PASSWORD]\n"
2076  " [-S START-TYPE] [-e SOURCE] [-W] [-t SECS] [-s] [-o OPTIONS]\n"), progname);
2077  printf(_(" %s unregister [-N SERVICENAME]\n"), progname);
2078 #endif
2079 
2080  printf(_("\nCommon options:\n"));
2081  printf(_(" -D, --pgdata=DATADIR location of the database storage area\n"));
2082 #ifdef WIN32
2083  printf(_(" -e SOURCE event source for logging when running as a service\n"));
2084 #endif
2085  printf(_(" -s, --silent only print errors, no informational messages\n"));
2086  printf(_(" -t, --timeout=SECS seconds to wait when using -w option\n"));
2087  printf(_(" -V, --version output version information, then exit\n"));
2088  printf(_(" -w, --wait wait until operation completes (default)\n"));
2089  printf(_(" -W, --no-wait do not wait until operation completes\n"));
2090  printf(_(" -?, --help show this help, then exit\n"));
2091  printf(_("If the -D option is omitted, the environment variable PGDATA is used.\n"));
2092 
2093  printf(_("\nOptions for start or restart:\n"));
2094 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
2095  printf(_(" -c, --core-files allow postgres to produce core files\n"));
2096 #else
2097  printf(_(" -c, --core-files not applicable on this platform\n"));
2098 #endif
2099  printf(_(" -l, --log=FILENAME write (or append) server log to FILENAME\n"));
2100  printf(_(" -o, --options=OPTIONS command line options to pass to postgres\n"
2101  " (PostgreSQL server executable) or initdb\n"));
2102  printf(_(" -p PATH-TO-POSTGRES normally not necessary\n"));
2103  printf(_("\nOptions for stop or restart:\n"));
2104  printf(_(" -m, --mode=MODE MODE can be \"smart\", \"fast\", or \"immediate\"\n"));
2105 
2106  printf(_("\nShutdown modes are:\n"));
2107  printf(_(" smart quit after all clients have disconnected\n"));
2108  printf(_(" fast quit directly, with proper shutdown (default)\n"));
2109  printf(_(" immediate quit without complete shutdown; will lead to recovery on restart\n"));
2110 
2111  printf(_("\nAllowed signal names for kill:\n"));
2112  printf(" ABRT HUP INT KILL QUIT TERM USR1 USR2\n");
2113 
2114 #ifdef WIN32
2115  printf(_("\nOptions for register and unregister:\n"));
2116  printf(_(" -N SERVICENAME service name with which to register PostgreSQL server\n"));
2117  printf(_(" -P PASSWORD password of account to register PostgreSQL server\n"));
2118  printf(_(" -U USERNAME user name of account to register PostgreSQL server\n"));
2119  printf(_(" -S START-TYPE service start type to register PostgreSQL server\n"));
2120 
2121  printf(_("\nStart types are:\n"));
2122  printf(_(" auto start service automatically during system startup (default)\n"));
2123  printf(_(" demand start service on demand\n"));
2124 #endif
2125 
2126  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2127  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2128 }
#define printf(...)
Definition: port.h:231

References _, printf, and progname.

Referenced by main().

◆ do_init()

static void do_init ( void  )
static

Definition at line 901 of file pg_ctl.c.

902 {
903  char *cmd;
904 
905  if (exec_path == NULL)
906  exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
907 
908  if (pgdata_opt == NULL)
909  pgdata_opt = "";
910 
911  if (post_opts == NULL)
912  post_opts = "";
913 
914  if (!silent_mode)
915  cmd = psprintf("\"%s\" %s%s",
917  else
918  cmd = psprintf("\"%s\" %s%s > \"%s\"",
920 
921  if (system(cmd) != 0)
922  {
923  write_stderr(_("%s: database system initialization failed\n"), progname);
924  exit(1);
925  }
926 }
static bool silent_mode
Definition: pg_ctl.c:82
#define DEVNULL
Definition: port.h:156

References _, argv0, DEVNULL, exec_path, exit(), 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 ( pgpid_t  pid)
static

Definition at line 1401 of file pg_ctl.c.

1402 {
1403  if (kill((pid_t) pid, sig) != 0)
1404  {
1405  write_stderr(_("%s: could not send signal %d (PID: %ld): %s\n"),
1406  progname, sig, pid, strerror(errno));
1407  exit(1);
1408  }
1409 }
static int sig
Definition: pg_ctl.c:84
#define strerror
Definition: port.h:238
#define kill(pid, sig)
Definition: win32_port.h:464

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

Referenced by main().

◆ do_logrotate()

static void do_logrotate ( void  )
static

Definition at line 1263 of file pg_ctl.c.

1264 {
1265  FILE *logrotatefile;
1266  pgpid_t pid;
1267 
1268  pid = get_pgpid(false);
1269 
1270  if (pid == 0) /* no pid file */
1271  {
1272  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1273  write_stderr(_("Is server running?\n"));
1274  exit(1);
1275  }
1276  else if (pid < 0) /* standalone backend, not postmaster */
1277  {
1278  pid = -pid;
1279  write_stderr(_("%s: cannot rotate log file; "
1280  "single-user server is running (PID: %ld)\n"),
1281  progname, pid);
1282  exit(1);
1283  }
1284 
1285  snprintf(logrotate_file, MAXPGPATH, "%s/logrotate", pg_data);
1286 
1287  if ((logrotatefile = fopen(logrotate_file, "w")) == NULL)
1288  {
1289  write_stderr(_("%s: could not create log rotation signal file \"%s\": %s\n"),
1290  progname, logrotate_file, strerror(errno));
1291  exit(1);
1292  }
1293  if (fclose(logrotatefile))
1294  {
1295  write_stderr(_("%s: could not write log rotation signal file \"%s\": %s\n"),
1296  progname, logrotate_file, strerror(errno));
1297  exit(1);
1298  }
1299 
1300  sig = SIGUSR1;
1301  if (kill((pid_t) pid, sig) != 0)
1302  {
1303  write_stderr(_("%s: could not send log rotation signal (PID: %ld): %s\n"),
1304  progname, pid, strerror(errno));
1305  if (unlink(logrotate_file) != 0)
1306  write_stderr(_("%s: could not remove log rotation signal file \"%s\": %s\n"),
1307  progname, logrotate_file, strerror(errno));
1308  exit(1);
1309  }
1310 
1311  print_msg(_("server signaled to rotate log file\n"));
1312 }
long pgpid_t
Definition: pg_ctl.c:39
static pgpid_t get_pgpid(bool is_status_request)
Definition: pg_ctl.c:251
static char pid_file[MAXPGPATH]
Definition: pg_ctl.c:103
static void print_msg(const char *msg)
Definition: pg_ctl.c:241
static char logrotate_file[MAXPGPATH]
Definition: pg_ctl.c:106
#define SIGUSR1
Definition: win32_port.h:179

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

1183 {
1184  FILE *prmfile;
1185  pgpid_t pid;
1186 
1187  pid = get_pgpid(false);
1188 
1189  if (pid == 0) /* no pid file */
1190  {
1191  write_stderr(_("%s: PID file \"%s\" does not exist\n"), progname, pid_file);
1192  write_stderr(_("Is server running?\n"));
1193  exit(1);
1194  }
1195  else if (pid < 0) /* standalone backend, not postmaster */
1196  {
1197  pid = -pid;
1198  write_stderr(_("%s: cannot promote server; "
1199  "single-user server is running (PID: %ld)\n"),
1200  progname, pid);
1201  exit(1);
1202  }
1203 
1205  {
1206  write_stderr(_("%s: cannot promote server; "
1207  "server is not in standby mode\n"),
1208  progname);
1209  exit(1);
1210  }
1211 
1212  snprintf(promote_file, MAXPGPATH, "%s/promote", pg_data);
1213 
1214  if ((prmfile = fopen(promote_file, "w")) == NULL)
1215  {
1216  write_stderr(_("%s: could not create promote signal file \"%s\": %s\n"),
1217  progname, promote_file, strerror(errno));
1218  exit(1);
1219  }
1220  if (fclose(prmfile))
1221  {
1222  write_stderr(_("%s: could not write promote signal file \"%s\": %s\n"),
1223  progname, promote_file, strerror(errno));
1224  exit(1);
1225  }
1226 
1227  sig = SIGUSR1;
1228  if (kill((pid_t) pid, sig) != 0)
1229  {
1230  write_stderr(_("%s: could not send promote signal (PID: %ld): %s\n"),
1231  progname, pid, strerror(errno));
1232  if (unlink(promote_file) != 0)
1233  write_stderr(_("%s: could not remove promote signal file \"%s\": %s\n"),
1234  progname, promote_file, strerror(errno));
1235  exit(1);
1236  }
1237 
1238  if (do_wait)
1239  {
1240  print_msg(_("waiting for server to promote..."));
1242  {
1243  print_msg(_(" done\n"));
1244  print_msg(_("server promoted\n"));
1245  }
1246  else
1247  {
1248  print_msg(_(" stopped waiting\n"));
1249  write_stderr(_("%s: server did not promote in time\n"),
1250  progname);
1251  exit(1);
1252  }
1253  }
1254  else
1255  print_msg(_("server promoting\n"));
1256 }
@ DB_IN_ARCHIVE_RECOVERY
Definition: pg_control.h:94
static bool wait_for_postmaster_promote(void)
Definition: pg_ctl.c:753
static char promote_file[MAXPGPATH]
Definition: pg_ctl.c:105
static DBState get_control_dbstate(void)
Definition: pg_ctl.c:2269
static bool do_wait
Definition: pg_ctl.c:79

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

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

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

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

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

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

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(), wait_for_postmaster_start(), and write_stderr().

Referenced by do_restart(), and main().

◆ do_status()

static void do_status ( void  )
static

Definition at line 1344 of file pg_ctl.c.

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

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

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

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

875 {
876  int ret;
877  char *found_path;
878 
879  found_path = pg_malloc(MAXPGPATH);
880 
881  if ((ret = find_other_exec(argv0, target, versionstr, found_path)) < 0)
882  {
883  char full_path[MAXPGPATH];
884 
885  if (find_my_exec(argv0, full_path) < 0)
886  strlcpy(full_path, progname, sizeof(full_path));
887 
888  if (ret == -1)
889  write_stderr(_("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"\n"),
890  target, progname, full_path);
891  else
892  write_stderr(_("program \"%s\" was found by \"%s\" but was not the same version as %s\n"),
893  target, full_path, progname);
894  exit(1);
895  }
896 
897  return found_path;
898 }
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:140
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:335
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 415 of file pg_ctl.c.

416 {
417  char *curr_line = NULL;
418  int i = 0;
419 
420  if (!optlines)
421  return;
422 
423  while ((curr_line = optlines[i++]))
424  free(curr_line);
425 
426  free(optlines);
427 }
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 2269 of file pg_ctl.c.

2270 {
2271  DBState ret;
2272  bool crc_ok;
2273  ControlFileData *control_file_data = get_controlfile(pg_data, &crc_ok);
2274 
2275  if (!crc_ok)
2276  {
2277  write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2278  exit(1);
2279  }
2280 
2281  ret = control_file_data->state;
2282  pfree(control_file_data);
2283  return ret;
2284 }
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
void pfree(void *pointer)
Definition: mcxt.c:1175
DBState
Definition: pg_control.h:88

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 pgpid_t get_pgpid ( bool  is_status_request)
static

Definition at line 251 of file pg_ctl.c.

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

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

2289 {
2290  static struct option long_options[] = {
2291  {"help", no_argument, NULL, '?'},
2292  {"version", no_argument, NULL, 'V'},
2293  {"log", required_argument, NULL, 'l'},
2294  {"mode", required_argument, NULL, 'm'},
2295  {"pgdata", required_argument, NULL, 'D'},
2296  {"options", required_argument, NULL, 'o'},
2297  {"silent", no_argument, NULL, 's'},
2298  {"timeout", required_argument, NULL, 't'},
2299  {"core-files", no_argument, NULL, 'c'},
2300  {"wait", no_argument, NULL, 'w'},
2301  {"no-wait", no_argument, NULL, 'W'},
2302  {NULL, 0, NULL, 0}
2303  };
2304 
2305  char *env_wait;
2306  int option_index;
2307  int c;
2308  pgpid_t killproc = 0;
2309 
2310  pg_logging_init(argv[0]);
2311  progname = get_progname(argv[0]);
2312  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_ctl"));
2313  start_time = time(NULL);
2314 
2315  /*
2316  * save argv[0] so do_start() can look for the postmaster if necessary. we
2317  * don't look for postmaster here because in many cases we won't need it.
2318  */
2319  argv0 = argv[0];
2320 
2321  /* Set restrictive mode mask until PGDATA permissions are checked */
2322  umask(PG_MODE_MASK_OWNER);
2323 
2324  /* support --help and --version even if invoked as root */
2325  if (argc > 1)
2326  {
2327  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2328  {
2329  do_help();
2330  exit(0);
2331  }
2332  else if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2333  {
2334  puts("pg_ctl (PostgreSQL) " PG_VERSION);
2335  exit(0);
2336  }
2337  }
2338 
2339  /*
2340  * Disallow running as root, to forestall any possible security holes.
2341  */
2342 #ifndef WIN32
2343  if (geteuid() == 0)
2344  {
2345  write_stderr(_("%s: cannot be run as root\n"
2346  "Please log in (using, e.g., \"su\") as the "
2347  "(unprivileged) user that will\n"
2348  "own the server process.\n"),
2349  progname);
2350  exit(1);
2351  }
2352 #endif
2353 
2354  env_wait = getenv("PGCTLTIMEOUT");
2355  if (env_wait != NULL)
2356  wait_seconds = atoi(env_wait);
2357 
2358  /*
2359  * 'Action' can be before or after args so loop over both. Some
2360  * getopt_long() implementations will reorder argv[] to place all flags
2361  * first (GNU?), but we don't rely on it. Our /port version doesn't do
2362  * that.
2363  */
2364  optind = 1;
2365 
2366  /* process command-line options */
2367  while (optind < argc)
2368  {
2369  while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW",
2370  long_options, &option_index)) != -1)
2371  {
2372  switch (c)
2373  {
2374  case 'D':
2375  {
2376  char *pgdata_D;
2377 
2378  pgdata_D = pg_strdup(optarg);
2379  canonicalize_path(pgdata_D);
2380  setenv("PGDATA", pgdata_D, 1);
2381 
2382  /*
2383  * We could pass PGDATA just in an environment
2384  * variable but we do -D too for clearer postmaster
2385  * 'ps' display
2386  */
2387  pgdata_opt = psprintf("-D \"%s\" ", pgdata_D);
2388  free(pgdata_D);
2389  break;
2390  }
2391  case 'e':
2393  break;
2394  case 'l':
2396  break;
2397  case 'm':
2398  set_mode(optarg);
2399  break;
2400  case 'N':
2402  break;
2403  case 'o':
2404  /* append option? */
2405  if (!post_opts)
2407  else
2408  {
2409  char *old_post_opts = post_opts;
2410 
2411  post_opts = psprintf("%s %s", old_post_opts, optarg);
2412  free(old_post_opts);
2413  }
2414  break;
2415  case 'p':
2417  break;
2418  case 'P':
2420  break;
2421  case 's':
2422  silent_mode = true;
2423  break;
2424  case 'S':
2425 #ifdef WIN32
2426  set_starttype(optarg);
2427 #else
2428  write_stderr(_("%s: -S option not supported on this platform\n"),
2429  progname);
2430  exit(1);
2431 #endif
2432  break;
2433  case 't':
2434  wait_seconds = atoi(optarg);
2435  wait_seconds_arg = true;
2436  break;
2437  case 'U':
2438  if (strchr(optarg, '\\'))
2440  else
2441  /* Prepend .\ for local accounts */
2442  register_username = psprintf(".\\%s", optarg);
2443  break;
2444  case 'w':
2445  do_wait = true;
2446  break;
2447  case 'W':
2448  do_wait = false;
2449  break;
2450  case 'c':
2451  allow_core_files = true;
2452  break;
2453  default:
2454  /* getopt_long already issued a suitable error message */
2455  do_advice();
2456  exit(1);
2457  }
2458  }
2459 
2460  /* Process an action */
2461  if (optind < argc)
2462  {
2463  if (ctl_command != NO_COMMAND)
2464  {
2465  write_stderr(_("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind]);
2466  do_advice();
2467  exit(1);
2468  }
2469 
2470  if (strcmp(argv[optind], "init") == 0
2471  || strcmp(argv[optind], "initdb") == 0)
2473  else if (strcmp(argv[optind], "start") == 0)
2475  else if (strcmp(argv[optind], "stop") == 0)
2477  else if (strcmp(argv[optind], "restart") == 0)
2479  else if (strcmp(argv[optind], "reload") == 0)
2481  else if (strcmp(argv[optind], "status") == 0)
2483  else if (strcmp(argv[optind], "promote") == 0)
2485  else if (strcmp(argv[optind], "logrotate") == 0)
2487  else if (strcmp(argv[optind], "kill") == 0)
2488  {
2489  if (argc - optind < 3)
2490  {
2491  write_stderr(_("%s: missing arguments for kill mode\n"), progname);
2492  do_advice();
2493  exit(1);
2494  }
2496  set_sig(argv[++optind]);
2497  killproc = atol(argv[++optind]);
2498  }
2499 #ifdef WIN32
2500  else if (strcmp(argv[optind], "register") == 0)
2502  else if (strcmp(argv[optind], "unregister") == 0)
2504  else if (strcmp(argv[optind], "runservice") == 0)
2506 #endif
2507  else
2508  {
2509  write_stderr(_("%s: unrecognized operation mode \"%s\"\n"), progname, argv[optind]);
2510  do_advice();
2511  exit(1);
2512  }
2513  optind++;
2514  }
2515  }
2516 
2517  if (ctl_command == NO_COMMAND)
2518  {
2519  write_stderr(_("%s: no operation specified\n"), progname);
2520  do_advice();
2521  exit(1);
2522  }
2523 
2524  /* Note we put any -D switch into the env var above */
2525  pg_config = getenv("PGDATA");
2526  if (pg_config)
2527  {
2531  }
2532 
2533  /* -D might point at config-only directory; if so find the real PGDATA */
2534  adjust_data_dir();
2535 
2536  /* Complain if -D needed and not provided */
2537  if (pg_config == NULL &&
2539  {
2540  write_stderr(_("%s: no database directory specified and environment variable PGDATA unset\n"),
2541  progname);
2542  do_advice();
2543  exit(1);
2544  }
2545 
2546  if (ctl_command == RELOAD_COMMAND)
2547  {
2548  sig = SIGHUP;
2549  do_wait = false;
2550  }
2551 
2552  if (pg_data)
2553  {
2554  snprintf(postopts_file, MAXPGPATH, "%s/postmaster.opts", pg_data);
2555  snprintf(version_file, MAXPGPATH, "%s/PG_VERSION", pg_data);
2556  snprintf(pid_file, MAXPGPATH, "%s/postmaster.pid", pg_data);
2557  snprintf(backup_file, MAXPGPATH, "%s/backup_label", pg_data);
2558 
2559  /*
2560  * Set mask based on PGDATA permissions,
2561  *
2562  * Don't error here if the data directory cannot be stat'd. This is
2563  * handled differently based on the command and we don't want to
2564  * interfere with that logic.
2565  */
2567  umask(pg_mode_mask);
2568  }
2569 
2570  switch (ctl_command)
2571  {
2572  case INIT_COMMAND:
2573  do_init();
2574  break;
2575  case STATUS_COMMAND:
2576  do_status();
2577  break;
2578  case START_COMMAND:
2579  do_start();
2580  break;
2581  case STOP_COMMAND:
2582  do_stop();
2583  break;
2584  case RESTART_COMMAND:
2585  do_restart();
2586  break;
2587  case RELOAD_COMMAND:
2588  do_reload();
2589  break;
2590  case PROMOTE_COMMAND:
2591  do_promote();
2592  break;
2593  case LOGROTATE_COMMAND:
2594  do_logrotate();
2595  break;
2596  case KILL_COMMAND:
2597  do_kill(killproc);
2598  break;
2599 #ifdef WIN32
2600  case REGISTER_COMMAND:
2601  pgwin32_doRegister();
2602  break;
2603  case UNREGISTER_COMMAND:
2604  pgwin32_doUnregister();
2605  break;
2607  pgwin32_doRunAsService();
2608  break;
2609 #endif
2610  default:
2611  break;
2612  }
2613 
2614  exit(0);
2615 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1212
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:446
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:57
#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:2051
static void do_reload(void)
Definition: pg_ctl.c:1145
static void do_help(void)
Definition: pg_ctl.c:2059
static char * register_username
Definition: pg_ctl.c:95
static char * event_source
Definition: pg_ctl.c:93
static void adjust_data_dir(void)
Definition: pg_ctl.c:2211
static char * register_servicename
Definition: pg_ctl.c:94
static void set_mode(char *modeopt)
Definition: pg_ctl.c:2133
static char backup_file[MAXPGPATH]
Definition: pg_ctl.c:104
static void do_logrotate(void)
Definition: pg_ctl.c:1263
static void do_stop(void)
Definition: pg_ctl.c:1021
static char * register_password
Definition: pg_ctl.c:96
static void do_init(void)
Definition: pg_ctl.c:901
static int wait_seconds
Definition: pg_ctl.c:80
static void do_kill(pgpid_t pid)
Definition: pg_ctl.c:1401
static void do_restart(void)
Definition: pg_ctl.c:1080
static bool wait_seconds_arg
Definition: pg_ctl.c:81
static char * log_file
Definition: pg_ctl.c:91
static void do_promote(void)
Definition: pg_ctl.c:1182
static time_t start_time
Definition: pg_ctl.c:99
static void set_sig(char *signame)
Definition: pg_ctl.c:2161
static void do_status(void)
Definition: pg_ctl.c:1344
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:167

References _, adjust_data_dir(), allow_core_files, argv0, backup_file, 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 1320 of file pg_ctl.c.

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

References kill.

Referenced by do_restart(), and do_status().

◆ print_msg()

static void print_msg ( const char *  msg)
static

Definition at line 241 of file pg_ctl.c.

242 {
243  if (!silent_mode)
244  {
245  fputs(msg, stdout);
246  fflush(stdout);
247  }
248 }
static void const char fflush(stdout)

References fflush(), silent_mode, and generate_unaccent_rules::stdout.

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

◆ read_post_opts()

static void read_post_opts ( void  )
static

Definition at line 801 of file pg_ctl.c.

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

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

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

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

2134 {
2135  if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
2136  {
2138  sig = SIGTERM;
2139  }
2140  else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
2141  {
2143  sig = SIGINT;
2144  }
2145  else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
2146  {
2148  sig = SIGQUIT;
2149  }
2150  else
2151  {
2152  write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2153  do_advice();
2154  exit(1);
2155  }
2156 }
#define SIGQUIT
Definition: win32_port.h:168

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

2162 {
2163  if (strcmp(signame, "HUP") == 0)
2164  sig = SIGHUP;
2165  else if (strcmp(signame, "INT") == 0)
2166  sig = SIGINT;
2167  else if (strcmp(signame, "QUIT") == 0)
2168  sig = SIGQUIT;
2169  else if (strcmp(signame, "ABRT") == 0)
2170  sig = SIGABRT;
2171  else if (strcmp(signame, "KILL") == 0)
2172  sig = SIGKILL;
2173  else if (strcmp(signame, "TERM") == 0)
2174  sig = SIGTERM;
2175  else if (strcmp(signame, "USR1") == 0)
2176  sig = SIGUSR1;
2177  else if (strcmp(signame, "USR2") == 0)
2178  sig = SIGUSR2;
2179  else
2180  {
2181  write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2182  do_advice();
2183  exit(1);
2184  }
2185 }
#define SIGABRT
Definition: win32_port.h:170
#define SIGUSR2
Definition: win32_port.h:180
#define SIGKILL
Definition: win32_port.h:171

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

Referenced by main().

◆ start_postmaster()

static pgpid_t start_postmaster ( void  )
static

Definition at line 445 of file pg_ctl.c.

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

References _, close, DEVNULL, exec_path, exit(), fd(), fflush(), log_file, pgdata_opt, post_opts, progname, psprintf(), generate_unaccent_rules::stdout, 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 ( int  sig)
static

Definition at line 856 of file pg_ctl.c.

857 {
858  if (postmasterPID != -1)
859  {
860  if (kill(postmasterPID, SIGINT) != 0)
861  write_stderr(_("%s: could not send stop signal (PID: %ld): %s\n"),
863  }
864 
865  /*
866  * Clear the signal handler, and send the signal again, to terminate the
867  * process as normal.
868  */
869  pqsignal(SIGINT, SIG_DFL);
870  raise(SIGINT);
871 }
#define SIG_DFL
Definition: win32_port.h:162

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

Referenced by do_start().

◆ wait_for_postmaster_promote()

static bool wait_for_postmaster_promote ( void  )
static

Definition at line 753 of file pg_ctl.c.

754 {
755  int cnt;
756 
757  for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
758  {
759  pgpid_t pid;
760  DBState state;
761 
762  if ((pid = get_pgpid(false)) == 0)
763  return false; /* pid file is gone */
764  if (kill((pid_t) pid, 0) != 0)
765  return false; /* postmaster died */
766 
768  if (state == DB_IN_PRODUCTION)
769  return true; /* successful promotion */
770 
771  if (cnt % WAITS_PER_SEC == 0)
772  print_msg(".");
774  }
775  return false; /* timeout reached */
776 }
@ DB_IN_PRODUCTION
Definition: pg_control.h:95
#define WAITS_PER_SEC
Definition: pg_ctl.c:77
#define USEC_PER_SEC
Definition: pg_ctl.c:75
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: regguts.h:318

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 ( pgpid_t  pm_pid,
bool  do_checkpoint 
)
static

Definition at line 599 of file pg_ctl.c.

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

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

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

208 {
209  va_list ap;
210 
211  va_start(ap, fmt);
212 #ifndef WIN32
213  /* On Unix, we just fprintf to stderr */
214  vfprintf(stderr, fmt, ap);
215 #else
216 
217  /*
218  * On Win32, we print to stderr if running on a console, or write to
219  * eventlog if running as a service
220  */
221  if (pgwin32_is_service()) /* Running as a service */
222  {
223  char errbuf[2048]; /* Arbitrary size? */
224 
225  vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
226 
227  write_eventlog(EVENTLOG_ERROR_TYPE, errbuf);
228  }
229  else
230  /* Not running as service, write to stderr */
231  vfprintf(stderr, fmt, ap);
232 #endif
233  va_end(ap);
234 }
va_end(args)
vfprintf(stderr, fmt, args)
static void const char * fmt
va_start(args, fmt)
#define vsnprintf
Definition: port.h:224
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(), and trap_sigint_during_startup().

Variable Documentation

◆ allow_core_files

bool allow_core_files = false
static

Definition at line 98 of file pg_ctl.c.

Referenced by do_start(), and main().

◆ argv0

◆ backup_file

char backup_file[MAXPGPATH]
static

Definition at line 104 of file pg_ctl.c.

Referenced by main().

◆ ctl_command

CtlCommand ctl_command = NO_COMMAND
static

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

Referenced by do_logrotate().

◆ pg_config

char* pg_config = NULL
static

Definition at line 87 of file pg_ctl.c.

Referenced by adjust_data_dir(), and main().

◆ pg_data

char* pg_data = NULL
static

Definition at line 86 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 88 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 89 of file pg_ctl.c.

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

◆ postmasterPID

volatile pgpid_t postmasterPID = -1
static

Definition at line 108 of file pg_ctl.c.

Referenced by do_start(), and trap_sigint_during_startup().

◆ postopts_file

char postopts_file[MAXPGPATH]
static

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

Referenced by do_promote(), and pg_promote().

◆ register_password

char* register_password = NULL
static

Definition at line 96 of file pg_ctl.c.

Referenced by main().

◆ register_servicename

char* register_servicename = "PostgreSQL"
static

Definition at line 94 of file pg_ctl.c.

Referenced by main().

◆ register_username

char* register_username = NULL
static

Definition at line 95 of file pg_ctl.c.

Referenced by main().

◆ shutdown_mode

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 83 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 82 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 102 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 81 of file pg_ctl.c.

Referenced by main().