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 "datatype/timestamp.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 WAITS_PER_SEC   10
 

Enumerations

enum  ShutdownMode { SMART_MODE , FAST_MODE , IMMEDIATE_MODE }
 
enum  WaitPMResult { POSTMASTER_READY , POSTMASTER_STILL_STARTING , POSTMASTER_SHUTDOWN_IN_RECOVERY , 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

 StaticAssertDecl (USECS_PER_SEC % WAITS_PER_SEC==0, "WAITS_PER_SEC must divide USECS_PER_SEC evenly")
 
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 70 of file pg_ctl.c.

◆ WAITS_PER_SEC

#define WAITS_PER_SEC   10

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

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

◆ ShutdownMode

Enumerator
SMART_MODE 
FAST_MODE 
IMMEDIATE_MODE 

Definition at line 38 of file pg_ctl.c.

39{
ShutdownMode
Definition: pg_ctl.c:39
@ FAST_MODE
Definition: pg_ctl.c:41
@ IMMEDIATE_MODE
Definition: pg_ctl.c:42
@ SMART_MODE
Definition: pg_ctl.c:40

◆ WaitPMResult

Enumerator
POSTMASTER_READY 
POSTMASTER_STILL_STARTING 
POSTMASTER_SHUTDOWN_IN_RECOVERY 
POSTMASTER_FAILED 

Definition at line 45 of file pg_ctl.c.

46{
WaitPMResult
Definition: pg_ctl.c:46
@ POSTMASTER_SHUTDOWN_IN_RECOVERY
Definition: pg_ctl.c:49
@ POSTMASTER_FAILED
Definition: pg_ctl.c:50
@ POSTMASTER_READY
Definition: pg_ctl.c:47
@ POSTMASTER_STILL_STARTING
Definition: pg_ctl.c:48

Function Documentation

◆ adjust_data_dir()

static void adjust_data_dir ( void  )
static

Definition at line 2126 of file pg_ctl.c.

2127{
2128 char filename[MAXPGPATH];
2129 char *my_exec_path,
2130 *cmd;
2131 FILE *fd;
2132
2133 /* do nothing if we're working without knowledge of data dir */
2134 if (pg_config == NULL)
2135 return;
2136
2137 /* If there is no postgresql.conf, it can't be a config-only dir */
2138 snprintf(filename, sizeof(filename), "%s/postgresql.conf", pg_config);
2139 if ((fd = fopen(filename, "r")) == NULL)
2140 return;
2141 fclose(fd);
2142
2143 /* If PG_VERSION exists, it can't be a config-only dir */
2144 snprintf(filename, sizeof(filename), "%s/PG_VERSION", pg_config);
2145 if ((fd = fopen(filename, "r")) != NULL)
2146 {
2147 fclose(fd);
2148 return;
2149 }
2150
2151 /* Must be a configuration directory, so find the data directory */
2152
2153 /* we use a private my_exec_path to avoid interfering with later uses */
2154 if (exec_path == NULL)
2156 else
2158
2159 /* it's important for -C to be the first option, see main.c */
2160 cmd = psprintf("\"%s\" -C data_directory %s%s",
2162 pgdata_opt ? pgdata_opt : "",
2163 post_opts ? post_opts : "");
2164 fflush(NULL);
2165
2166 fd = popen(cmd, "r");
2167 if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL || pclose(fd) != 0)
2168 {
2169 write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
2170 exit(1);
2171 }
2173
2174 /* strip trailing newline and carriage return */
2175 (void) pg_strip_crlf(filename);
2176
2177 free(pg_data);
2180}
#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:81
#define free(a)
Definition: header.h:65
#define MAXPGPATH
static char * post_opts
Definition: pg_ctl.c:86
static char * pgdata_opt
Definition: pg_ctl.c:85
static char * find_other_exec_or_die(const char *argv0, const char *target, const char *versionstr)
Definition: pg_ctl.c:876
static char * pg_config
Definition: pg_ctl.c:84
static char * pg_data
Definition: pg_ctl.c:83
static char * argv0
Definition: pg_ctl.c:94
static void write_stderr(const char *fmt,...) pg_attribute_printf(1
Definition: pg_ctl.c:203
static const char * progname
Definition: pg_ctl.c:87
static char * exec_path
Definition: pg_ctl.c:89
static char * filename
Definition: pg_dumpall.c:120
void canonicalize_path(char *path)
Definition: path.c:337
#define snprintf
Definition: port.h:260
#define PG_BACKEND_VERSIONSTR
Definition: port.h:144
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
int pg_strip_crlf(char *str)
Definition: string.c:154

References _, argv0, canonicalize_path(), exec_path, fd(), filename, find_other_exec_or_die(), free, MAXPGPATH, my_exec_path, PG_BACKEND_VERSIONSTR, pg_config, pg_data, pg_strdup(), 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 1966 of file pg_ctl.c.

1967{
1968 write_stderr(_("Try \"%s --help\" for more information.\n"), progname);
1969}

References _, progname, and write_stderr().

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

◆ do_help()

static void do_help ( void  )
static

Definition at line 1974 of file pg_ctl.c.

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

References _, printf, and progname.

Referenced by main().

◆ do_init()

static void do_init ( void  )
static

Definition at line 903 of file pg_ctl.c.

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

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

Referenced by main().

◆ do_kill()

static void do_kill ( pid_t  pid)
static

Definition at line 1406 of file pg_ctl.c.

1407{
1408 if (kill(pid, sig) != 0)
1409 {
1410 write_stderr(_("%s: could not send signal %d (PID: %d): %m\n"),
1411 progname, sig, (int) pid);
1412 exit(1);
1413 }
1414}
static int sig
Definition: pg_ctl.c:81
#define kill(pid, sig)
Definition: win32_port.h:493

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

Referenced by main().

◆ do_logrotate()

static void do_logrotate ( void  )
static

Definition at line 1268 of file pg_ctl.c.

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

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

Referenced by main().

◆ do_promote()

static void do_promote ( void  )
static

Definition at line 1187 of file pg_ctl.c.

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

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

Referenced by main().

◆ do_reload()

static void do_reload ( void  )
static

Definition at line 1150 of file pg_ctl.c.

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

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

Referenced by main().

◆ do_restart()

static void do_restart ( void  )
static

Definition at line 1086 of file pg_ctl.c.

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

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

Referenced by main().

◆ do_start()

static void do_start ( void  )
static

Definition at line 932 of file pg_ctl.c.

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

References _, allow_core_files, argv0, ctl_command, do_wait, exec_path, find_other_exec_or_die(), get_pgpid(), PG_BACKEND_VERSIONSTR, pgdata_opt, POSTMASTER_FAILED, POSTMASTER_READY, POSTMASTER_SHUTDOWN_IN_RECOVERY, 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 1349 of file pg_ctl.c.

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

References _, 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 1028 of file pg_ctl.c.

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

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

Referenced by main().

◆ find_other_exec_or_die()

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

Definition at line 876 of file pg_ctl.c.

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

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

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

2185{
2186 DBState ret;
2187 bool crc_ok;
2188 ControlFileData *control_file_data = get_controlfile(pg_data, &crc_ok);
2189
2190 if (!crc_ok)
2191 {
2192 write_stderr(_("%s: control file appears to be corrupt\n"), progname);
2193 exit(1);
2194 }
2195
2196 ret = control_file_data->state;
2197 pfree(control_file_data);
2198 return ret;
2199}
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
void pfree(void *pointer)
Definition: mcxt.c:1594
DBState
Definition: pg_control.h:90

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

Referenced by do_promote(), wait_for_postmaster_promote(), and wait_for_postmaster_start().

◆ get_pgpid()

static pid_t get_pgpid ( bool  is_status_request)
static

Definition at line 247 of file pg_ctl.c.

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

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

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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 2203 of file pg_ctl.c.

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

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

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

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

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

References _, ctl_command, exec_path, 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 315 of file pg_ctl.c.

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

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

2049{
2050 if (strcmp(modeopt, "s") == 0 || strcmp(modeopt, "smart") == 0)
2051 {
2053 sig = SIGTERM;
2054 }
2055 else if (strcmp(modeopt, "f") == 0 || strcmp(modeopt, "fast") == 0)
2056 {
2058 sig = SIGINT;
2059 }
2060 else if (strcmp(modeopt, "i") == 0 || strcmp(modeopt, "immediate") == 0)
2061 {
2063 sig = SIGQUIT;
2064 }
2065 else
2066 {
2067 write_stderr(_("%s: unrecognized shutdown mode \"%s\"\n"), progname, modeopt);
2068 do_advice();
2069 exit(1);
2070 }
2071}
#define SIGQUIT
Definition: win32_port.h:159

References _, do_advice(), 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 2076 of file pg_ctl.c.

2077{
2078 if (strcmp(signame, "HUP") == 0)
2079 sig = SIGHUP;
2080 else if (strcmp(signame, "INT") == 0)
2081 sig = SIGINT;
2082 else if (strcmp(signame, "QUIT") == 0)
2083 sig = SIGQUIT;
2084 else if (strcmp(signame, "ABRT") == 0)
2085 sig = SIGABRT;
2086 else if (strcmp(signame, "KILL") == 0)
2087 sig = SIGKILL;
2088 else if (strcmp(signame, "TERM") == 0)
2089 sig = SIGTERM;
2090 else if (strcmp(signame, "USR1") == 0)
2091 sig = SIGUSR1;
2092 else if (strcmp(signame, "USR2") == 0)
2093 sig = SIGUSR2;
2094 else
2095 {
2096 write_stderr(_("%s: unrecognized signal name \"%s\"\n"), progname, signame);
2097 do_advice();
2098 exit(1);
2099 }
2100}
#define SIGABRT
Definition: win32_port.h:161
#define SIGUSR2
Definition: win32_port.h:171
#define SIGKILL
Definition: win32_port.h:162

References _, do_advice(), 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 441 of file pg_ctl.c.

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

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

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

◆ StaticAssertDecl()

StaticAssertDecl ( USECS_PER_SEC WAITS_PER_SEC = =0,
"WAITS_PER_SEC must divide USECS_PER_SEC evenly"   
)

◆ trap_sigint_during_startup()

static void trap_sigint_during_startup ( SIGNAL_ARGS  )
static

Definition at line 858 of file pg_ctl.c.

859{
860 if (postmasterPID != -1)
861 {
862 if (kill(postmasterPID, SIGINT) != 0)
863 write_stderr(_("%s: could not send stop signal (PID: %d): %m\n"),
864 progname, (int) postmasterPID);
865 }
866
867 /*
868 * Clear the signal handler, and send the signal again, to terminate the
869 * process as normal.
870 */
871 pqsignal(postgres_signal_arg, SIG_DFL);
872 raise(postgres_signal_arg);
873}

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

Referenced by do_start().

◆ unlimit_core_size()

static void unlimit_core_size ( void  )
static

Definition at line 783 of file pg_ctl.c.

784{
785 struct rlimit lim;
786
787 getrlimit(RLIMIT_CORE, &lim);
788 if (lim.rlim_max == 0)
789 {
790 write_stderr(_("%s: cannot set core file size limit; disallowed by hard limit\n"),
791 progname);
792 return;
793 }
794 else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
795 {
796 lim.rlim_cur = lim.rlim_max;
797 setrlimit(RLIMIT_CORE, &lim);
798 }
799}

References _, progname, and write_stderr().

Referenced by do_start().

◆ wait_for_postmaster_promote()

static bool wait_for_postmaster_promote ( void  )
static

Definition at line 755 of file pg_ctl.c.

756{
757 int cnt;
758
759 for (cnt = 0; cnt < wait_seconds * WAITS_PER_SEC; cnt++)
760 {
761 pid_t pid;
763
764 if ((pid = get_pgpid(false)) == 0)
765 return false; /* pid file is gone */
766 if (kill(pid, 0) != 0)
767 return false; /* postmaster died */
768
770 if (state == DB_IN_PRODUCTION)
771 return true; /* successful promotion */
772
773 if (cnt % WAITS_PER_SEC == 0)
774 print_msg(".");
776 }
777 return false; /* timeout reached */
778}
#define USECS_PER_SEC
Definition: timestamp.h:134
@ DB_IN_PRODUCTION
Definition: pg_control.h:97
#define WAITS_PER_SEC
Definition: pg_ctl.c:72
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(), USECS_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 594 of file pg_ctl.c.

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

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

References get_pgpid(), kill, pg_usleep(), print_msg(), USECS_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 203 of file pg_ctl.c.

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

References pgwin32_is_service(), 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 95 of file pg_ctl.c.

Referenced by do_start(), and main().

◆ argv0

◆ ctl_command

CtlCommand ctl_command = NO_COMMAND
static

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

Referenced by do_logrotate().

◆ pg_config

char* pg_config = NULL
static

Definition at line 84 of file pg_ctl.c.

Referenced by adjust_data_dir(), and main().

◆ pg_data

char* pg_data = NULL
static

Definition at line 83 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 85 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 86 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 104 of file pg_ctl.c.

Referenced by do_start(), and trap_sigint_during_startup().

◆ postopts_file

char postopts_file[MAXPGPATH]
static

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

Referenced by do_promote(), and pg_promote().

◆ register_password

char* register_password = NULL
static

Definition at line 93 of file pg_ctl.c.

Referenced by main().

◆ register_servicename

char* register_servicename = "PostgreSQL"
static

Definition at line 91 of file pg_ctl.c.

Referenced by main().

◆ register_username

char* register_username = NULL
static

Definition at line 92 of file pg_ctl.c.

Referenced by main().

◆ shutdown_mode

ShutdownMode shutdown_mode = FAST_MODE
static

Definition at line 80 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 79 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 99 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 78 of file pg_ctl.c.

Referenced by main().