PostgreSQL Source Code git master
Loading...
Searching...
No Matches
exec.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include "common/string.h"
#include "fe_utils/version.h"
#include "pg_upgrade.h"
Include dependency graph for exec.c:

Go to the source code of this file.

Macros

#define MAXCMDLEN   (2 * MAXPGPATH)
 

Functions

static void check_data_dir (ClusterInfo *cluster)
 
static void check_bin_dir (ClusterInfo *cluster, bool check_versions)
 
static void get_bin_version (ClusterInfo *cluster)
 
static void check_exec (const char *dir, const char *program, bool check_version)
 
bool exec_prog (const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
 
bool pid_lock_file_exists (const char *datadir)
 
void verify_directories (void)
 
static void check_single_dir (const char *pg_data, const char *subdir)
 

Macro Definition Documentation

◆ MAXCMDLEN

#define MAXCMDLEN   (2 * MAXPGPATH)

Function Documentation

◆ check_bin_dir()

static void check_bin_dir ( ClusterInfo cluster,
bool  check_versions 
)
static

Definition at line 365 of file exec.c.

366{
367 struct stat statBuf;
368
369 /* check bindir */
370 if (stat(cluster->bindir, &statBuf) != 0)
371 report_status(PG_FATAL, "check for \"%s\" failed: %m",
372 cluster->bindir);
373 else if (!S_ISDIR(statBuf.st_mode))
374 report_status(PG_FATAL, "\"%s\" is not a directory",
375 cluster->bindir);
376
377 check_exec(cluster->bindir, "postgres", check_versions);
378 check_exec(cluster->bindir, "pg_controldata", check_versions);
379 check_exec(cluster->bindir, "pg_ctl", check_versions);
380
381 /*
382 * Fetch the binary version after checking for the existence of pg_ctl.
383 * This way we report a useful error if the pg_ctl binary used for version
384 * fetching is missing/broken.
385 */
387
388 check_exec(cluster->bindir, "pg_resetwal", check_versions);
389
390 if (cluster == &new_cluster)
391 {
392 /*
393 * These binaries are only needed for the target version. pg_dump and
394 * pg_dumpall are used to dump the old cluster, but must be of the
395 * target version.
396 */
397 check_exec(cluster->bindir, "initdb", check_versions);
398 check_exec(cluster->bindir, "pg_dump", check_versions);
399 check_exec(cluster->bindir, "pg_dumpall", check_versions);
400 check_exec(cluster->bindir, "pg_restore", check_versions);
401 check_exec(cluster->bindir, "psql", check_versions);
402 check_exec(cluster->bindir, "vacuumdb", check_versions);
403 }
404}
static void check_exec(const char *dir, const char *program, bool check_version)
Definition exec.c:407
static void get_bin_version(ClusterInfo *cluster)
Definition exec.c:34
ClusterInfo new_cluster
Definition pg_upgrade.c:74
@ PG_FATAL
Definition pg_upgrade.h:261
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
static int fb(int x)
#define stat
Definition win32_port.h:74
#define S_ISDIR(m)
Definition win32_port.h:332

References check_exec(), fb(), get_bin_version(), new_cluster, PG_FATAL, report_status(), S_ISDIR, and stat.

Referenced by verify_directories().

◆ check_data_dir()

static void check_data_dir ( ClusterInfo cluster)
static

Definition at line 333 of file exec.c.

334{
335 const char *pg_data = cluster->pgdata;
336
337 /* get the cluster version */
338 cluster->major_version = get_pg_version(cluster->pgdata,
339 &cluster->major_version_str);
341 check_single_dir(pg_data, "base");
342 check_single_dir(pg_data, "global");
343 check_single_dir(pg_data, "pg_multixact");
344 check_single_dir(pg_data, "pg_subtrans");
346 check_single_dir(pg_data, "pg_twophase");
347 check_single_dir(pg_data, "pg_wal");
348 check_single_dir(pg_data, "pg_xact");
349}
static void check_single_dir(const char *pg_data, const char *subdir)
Definition exec.c:304
uint32 get_pg_version(const char *datadir, char **version_str)
Definition version.c:44
static char * pg_data
Definition initdb.c:138
#define PG_TBLSPC_DIR
Definition relpath.h:41

References check_single_dir(), fb(), get_pg_version(), pg_data, and PG_TBLSPC_DIR.

Referenced by verify_directories().

◆ check_exec()

static void check_exec ( const char dir,
const char program,
bool  check_version 
)
static

Definition at line 407 of file exec.c.

408{
409 char path[MAXPGPATH];
410 char *line;
411 char cmd[MAXPGPATH];
412 char versionstr[128];
413
414 snprintf(path, sizeof(path), "%s/%s", dir, program);
415
416 if (validate_exec(path) != 0)
417 pg_fatal("check for \"%s\" failed: %m", path);
418
419 snprintf(cmd, sizeof(cmd), "\"%s\" -V", path);
420
421 if ((line = pipe_read_line(cmd)) == NULL)
422 pg_fatal("check for \"%s\" failed: cannot execute",
423 path);
424
425 if (check_version)
426 {
427 pg_strip_crlf(line);
428
429 snprintf(versionstr, sizeof(versionstr), "%s (PostgreSQL) " PG_VERSION, program);
430
431 if (strcmp(line, versionstr) != 0)
432 pg_fatal("check for \"%s\" failed: incorrect version: found \"%s\", expected \"%s\"",
433 path, line, versionstr);
434 }
435
436 pg_free(line);
437}
int validate_exec(const char *path)
Definition exec.c:89
char * pipe_read_line(char *cmd)
Definition exec.c:353
void pg_free(void *ptr)
#define pg_fatal(...)
#define MAXPGPATH
#define snprintf
Definition port.h:261
int pg_strip_crlf(char *str)
Definition string.c:154

References fb(), MAXPGPATH, pg_fatal, pg_free(), pg_strip_crlf(), pipe_read_line(), snprintf, and validate_exec().

Referenced by check_bin_dir().

◆ check_single_dir()

static void check_single_dir ( const char pg_data,
const char subdir 
)
static

Definition at line 304 of file exec.c.

305{
306 struct stat statBuf;
307 char subDirName[MAXPGPATH];
308
309 snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
310 /* Win32 can't stat() a directory with a trailing slash. */
311 *subdir ? "/" : "",
312 subdir);
313
314 if (stat(subDirName, &statBuf) != 0)
315 report_status(PG_FATAL, "check for \"%s\" failed: %m",
316 subDirName);
317 else if (!S_ISDIR(statBuf.st_mode))
318 report_status(PG_FATAL, "\"%s\" is not a directory",
319 subDirName);
320}

References fb(), MAXPGPATH, pg_data, PG_FATAL, report_status(), S_ISDIR, snprintf, and stat.

Referenced by check_data_dir().

◆ exec_prog()

bool exec_prog ( const char log_filename,
const char opt_log_file,
bool  report_error,
bool  exit_on_error,
const char fmt,
  ... 
)

Definition at line 77 of file exec.c.

79{
80 int result = 0;
81 int written;
82 char log_file[MAXPGPATH];
83
84#define MAXCMDLEN (2 * MAXPGPATH)
85 char cmd[MAXCMDLEN];
86 FILE *log;
87 va_list ap;
88
89#ifdef WIN32
90 static DWORD mainThreadId = 0;
91
92 /* We assume we are called from the primary thread first */
93 if (mainThreadId == 0)
95#endif
96
98
99 written = 0;
100 va_start(ap, fmt);
102 va_end(ap);
103 if (written >= MAXCMDLEN)
104 pg_fatal("command too long");
106 " >> \"%s\" 2>&1", log_file);
107 if (written >= MAXCMDLEN)
108 pg_fatal("command too long");
109
110 pg_log(PG_VERBOSE, "%s", cmd);
111
112#ifdef WIN32
113
114 /*
115 * For some reason, Windows issues a file-in-use error if we write data to
116 * the log file from a non-primary thread just before we create a
117 * subprocess that also writes to the same log file. One fix is to sleep
118 * for 100ms. A cleaner fix is to write to the log file _after_ the
119 * subprocess has completed, so we do this only when writing from a
120 * non-primary thread. fflush(), running system() twice, and pre-creating
121 * the file do not see to help.
122 */
124 {
125 fflush(NULL);
126 result = system(cmd);
127 }
128#endif
129
130 log = fopen(log_file, "a");
131
132#ifdef WIN32
133 {
134 /*
135 * "pg_ctl -w stop" might have reported that the server has stopped
136 * because the postmaster.pid file has been removed, but "pg_ctl -w
137 * start" might still be in the process of closing and might still be
138 * holding its stdout and -l log file descriptors open. Therefore,
139 * try to open the log file a few more times.
140 */
141 int iter;
142
143 for (iter = 0; iter < 4 && log == NULL; iter++)
144 {
145 pg_usleep(1000000); /* 1 sec */
146 log = fopen(log_file, "a");
147 }
148 }
149#endif
150
151 if (log == NULL)
152 pg_fatal("could not open log file \"%s\": %m", log_file);
153
154#ifdef WIN32
155 /* Are we printing "command:" before its output? */
157 fprintf(log, "\n\n");
158#endif
159 fprintf(log, "command: %s\n", cmd);
160#ifdef WIN32
161 /* Are we printing "command:" after its output? */
163 fprintf(log, "\n\n");
164#endif
165
166 /*
167 * In Windows, we must close the log file at this point so the file is not
168 * open while the command is running, or we get a share violation.
169 */
170 fclose(log);
171
172#ifdef WIN32
173 /* see comment above */
175#endif
176 {
177 fflush(NULL);
178 result = system(cmd);
179 }
180
181 if (result != 0 && report_error)
182 {
183 /* we might be in on a progress status line, so go to the next line */
184 report_status(PG_REPORT, "\n*failure*");
185 fflush(stdout);
186
187 pg_log(PG_VERBOSE, "There were problems executing \"%s\"", cmd);
188 if (opt_log_file)
189 pg_log(exit_on_error ? PG_FATAL : PG_REPORT,
190 "Consult the last few lines of \"%s\" or \"%s\" for\n"
191 "the probable cause of the failure.",
193 else
194 pg_log(exit_on_error ? PG_FATAL : PG_REPORT,
195 "Consult the last few lines of \"%s\" for\n"
196 "the probable cause of the failure.",
197 log_file);
198 }
199
200#ifndef WIN32
201
202 /*
203 * We can't do this on Windows because it will keep the "pg_ctl start"
204 * output filename open until the server stops, so we do the \n\n above on
205 * that platform. We use a unique filename for "pg_ctl start" that is
206 * never reused while the server is running, so it works fine. We could
207 * log these commands to a third file, but that just adds complexity.
208 */
209 if ((log = fopen(log_file, "a")) == NULL)
210 pg_fatal("could not write to log file \"%s\": %m", log_file);
211 fprintf(log, "\n\n");
212 fclose(log);
213#endif
214
215 return result == 0;
216}
#define MAXCMDLEN
uint32 result
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
static char * log_file
Definition pg_ctl.c:88
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
LogOpts log_opts
Definition util.c:17
@ PG_VERBOSE
Definition pg_upgrade.h:256
@ PG_REPORT
Definition pg_upgrade.h:259
#define vsnprintf
Definition port.h:260
void pg_usleep(long microsec)
Definition signal.c:53
char * logdir
Definition pg_upgrade.h:307

References fb(), fprintf, log_file, log_opts, LogOpts::logdir, MAXCMDLEN, MAXPGPATH, pg_fatal, PG_FATAL, pg_log(), PG_REPORT, pg_usleep(), PG_VERBOSE, report_status(), result, snprintf, and vsnprintf.

Referenced by copy_subdir_files(), copy_xact_xlog_xid(), create_new_objects(), generate_old_dump(), main(), parallel_exec_prog(), prepare_new_cluster(), prepare_new_globals(), set_new_cluster_char_signedness(), start_postmaster(), and stop_postmaster().

◆ get_bin_version()

static void get_bin_version ( ClusterInfo cluster)
static

Definition at line 34 of file exec.c.

35{
36 char cmd[MAXPGPATH],
38 FILE *output;
39 int rc;
40 int v1 = 0,
41 v2 = 0;
42
43 snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
44 fflush(NULL);
45
46 if ((output = popen(cmd, "r")) == NULL ||
48 pg_fatal("could not get pg_ctl version data using %s: %m", cmd);
49
50 rc = pclose(output);
51 if (rc != 0)
52 pg_fatal("could not get pg_ctl version data using %s: %s",
53 cmd, wait_result_to_str(rc));
54
55 if (sscanf(cmd_output, "%*s %*s %d.%d", &v1, &v2) < 1)
56 pg_fatal("could not get pg_ctl version output from %s", cmd);
57
58 cluster->bin_version = v1 * 10000;
59}
FILE * output
#define MAX_STRING
Definition pg_upgrade.h:22
char * wait_result_to_str(int exitstatus)
Definition wait_error.c:33

References fb(), MAX_STRING, MAXPGPATH, output, pg_fatal, snprintf, and wait_result_to_str().

Referenced by check_bin_dir().

◆ pid_lock_file_exists()

bool pid_lock_file_exists ( const char datadir)

Definition at line 225 of file exec.c.

226{
227 char path[MAXPGPATH];
228 int fd;
229
230 snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
231
232 if ((fd = open(path, O_RDONLY, 0)) < 0)
233 {
234 /* ENOTDIR means we will throw a more useful error later */
235 if (errno != ENOENT && errno != ENOTDIR)
236 pg_fatal("could not open file \"%s\" for reading: %m", path);
237
238 return false;
239 }
240
241 close(fd);
242 return true;
243}
#define close(a)
Definition win32.h:12
char * datadir
static int fd(const char *x, int i)

References close, datadir, fb(), fd(), MAXPGPATH, pg_fatal, and snprintf.

Referenced by setup().

◆ verify_directories()

void verify_directories ( void  )

Definition at line 255 of file exec.c.

256{
257#ifndef WIN32
258 if (access(".", R_OK | W_OK | X_OK) != 0)
259#else
261#endif
262 pg_fatal("You must have read and write access in the current directory.");
263
264 check_bin_dir(&old_cluster, false);
268}
static void check_bin_dir(ClusterInfo *cluster, bool check_versions)
Definition exec.c:365
static void check_data_dir(ClusterInfo *cluster)
Definition exec.c:333
ClusterInfo old_cluster
Definition pg_upgrade.c:73
short access

References check_bin_dir(), check_data_dir(), fb(), new_cluster, old_cluster, and pg_fatal.

Referenced by setup().