PostgreSQL Source Code  git master
exec.c File Reference
#include "postgres_fe.h"
#include <fcntl.h>
#include "common/string.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 383 of file exec.c.

384 {
385  struct stat statBuf;
386 
387  /* check bindir */
388  if (stat(cluster->bindir, &statBuf) != 0)
389  report_status(PG_FATAL, "check for \"%s\" failed: %m",
390  cluster->bindir);
391  else if (!S_ISDIR(statBuf.st_mode))
392  report_status(PG_FATAL, "\"%s\" is not a directory",
393  cluster->bindir);
394 
395  check_exec(cluster->bindir, "postgres", check_versions);
396  check_exec(cluster->bindir, "pg_controldata", check_versions);
397  check_exec(cluster->bindir, "pg_ctl", check_versions);
398 
399  /*
400  * Fetch the binary version after checking for the existence of pg_ctl.
401  * This way we report a useful error if the pg_ctl binary used for version
402  * fetching is missing/broken.
403  */
405 
406  /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
407  if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
408  check_exec(cluster->bindir, "pg_resetxlog", check_versions);
409  else
410  check_exec(cluster->bindir, "pg_resetwal", check_versions);
411 
412  if (cluster == &new_cluster)
413  {
414  /*
415  * These binaries are only needed for the target version. pg_dump and
416  * pg_dumpall are used to dump the old cluster, but must be of the
417  * target version.
418  */
419  check_exec(cluster->bindir, "initdb", check_versions);
420  check_exec(cluster->bindir, "pg_dump", check_versions);
421  check_exec(cluster->bindir, "pg_dumpall", check_versions);
422  check_exec(cluster->bindir, "pg_restore", check_versions);
423  check_exec(cluster->bindir, "psql", check_versions);
424  check_exec(cluster->bindir, "vacuumdb", check_versions);
425  }
426 }
static void check_exec(const char *dir, const char *program, bool check_version)
Definition: exec.c:429
static void get_bin_version(ClusterInfo *cluster)
Definition: exec.c:33
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
ClusterInfo new_cluster
Definition: pg_upgrade.c:65
@ PG_FATAL
Definition: pg_upgrade.h:273
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define stat
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:325

References check_exec(), cluster(), get_bin_version(), GET_MAJOR_VERSION, new_cluster, PG_FATAL, report_status(), S_ISDIR, stat::st_mode, and stat.

Referenced by verify_directories().

◆ check_data_dir()

static void check_data_dir ( ClusterInfo cluster)
static

Definition at line 341 of file exec.c.

342 {
343  const char *pg_data = cluster->pgdata;
344 
345  /* get the cluster version */
346  cluster->major_version = get_major_server_version(cluster);
347 
349  check_single_dir(pg_data, "base");
350  check_single_dir(pg_data, "global");
351  check_single_dir(pg_data, "pg_multixact");
352  check_single_dir(pg_data, "pg_subtrans");
353  check_single_dir(pg_data, "pg_tblspc");
354  check_single_dir(pg_data, "pg_twophase");
355 
356  /* pg_xlog has been renamed to pg_wal in v10 */
357  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
358  check_single_dir(pg_data, "pg_xlog");
359  else
360  check_single_dir(pg_data, "pg_wal");
361 
362  /* pg_clog has been renamed to pg_xact in v10 */
363  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
364  check_single_dir(pg_data, "pg_clog");
365  else
366  check_single_dir(pg_data, "pg_xact");
367 }
static void check_single_dir(const char *pg_data, const char *subdir)
Definition: exec.c:312
static char * pg_data
Definition: initdb.c:138
uint32 get_major_server_version(ClusterInfo *cluster)
Definition: server.c:159

References check_single_dir(), cluster(), get_major_server_version(), GET_MAJOR_VERSION, and pg_data.

Referenced by verify_directories().

◆ check_exec()

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

Definition at line 429 of file exec.c.

430 {
431  char path[MAXPGPATH];
432  char *line;
433  char cmd[MAXPGPATH];
434  char versionstr[128];
435 
436  snprintf(path, sizeof(path), "%s/%s", dir, program);
437 
438  if (validate_exec(path) != 0)
439  pg_fatal("check for \"%s\" failed: %m", path);
440 
441  snprintf(cmd, sizeof(cmd), "\"%s\" -V", path);
442 
443  if ((line = pipe_read_line(cmd)) == NULL)
444  pg_fatal("check for \"%s\" failed: cannot execute",
445  path);
446 
447  if (check_version)
448  {
449  pg_strip_crlf(line);
450 
451  snprintf(versionstr, sizeof(versionstr), "%s (PostgreSQL) " PG_VERSION, program);
452 
453  if (strcmp(line, versionstr) != 0)
454  pg_fatal("check for \"%s\" failed: incorrect version: found \"%s\", expected \"%s\"",
455  path, line, versionstr);
456  }
457 
458  pg_free(line);
459 }
char * pipe_read_line(char *cmd)
Definition: exec.c:371
int validate_exec(const char *path)
Definition: exec.c:88
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define pg_fatal(...)
#define MAXPGPATH
#define snprintf
Definition: port.h:238
int pg_strip_crlf(char *str)
Definition: string.c:155

References 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 312 of file exec.c.

313 {
314  struct stat statBuf;
315  char subDirName[MAXPGPATH];
316 
317  snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
318  /* Win32 can't stat() a directory with a trailing slash. */
319  *subdir ? "/" : "",
320  subdir);
321 
322  if (stat(subDirName, &statBuf) != 0)
323  report_status(PG_FATAL, "check for \"%s\" failed: %m",
324  subDirName);
325  else if (!S_ISDIR(statBuf.st_mode))
326  report_status(PG_FATAL, "\"%s\" is not a directory",
327  subDirName);
328 }

References MAXPGPATH, pg_data, PG_FATAL, report_status(), S_ISDIR, snprintf, stat::st_mode, 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 85 of file exec.c.

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

References fflush(), fmt, fprintf, log_file, log_opts, LogOpts::logdir, MAXCMDLEN, MAXPGPATH, pg_fatal, PG_FATAL, pg_log(), PG_REPORT, pg_usleep(), PG_VERBOSE, report_status(), snprintf, generate_unaccent_rules::stdout, va_end(), va_start(), 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(), start_postmaster(), and stop_postmaster().

◆ get_bin_version()

static void get_bin_version ( ClusterInfo cluster)
static

Definition at line 33 of file exec.c.

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

References cluster(), fflush(), 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 233 of file exec.c.

234 {
235  char path[MAXPGPATH];
236  int fd;
237 
238  snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
239 
240  if ((fd = open(path, O_RDONLY, 0)) < 0)
241  {
242  /* ENOTDIR means we will throw a more useful error later */
243  if (errno != ENOENT && errno != ENOTDIR)
244  pg_fatal("could not open file \"%s\" for reading: %m", path);
245 
246  return false;
247  }
248 
249  close(fd);
250  return true;
251 }
#define close(a)
Definition: win32.h:12
char * datadir
static int fd(const char *x, int i)
Definition: preproc-init.c:105

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

Referenced by setup().

◆ verify_directories()

void verify_directories ( void  )

Definition at line 263 of file exec.c.

264 {
265 #ifndef WIN32
266  if (access(".", R_OK | W_OK | X_OK) != 0)
267 #else
268  if (win32_check_directory_write_permissions() != 0)
269 #endif
270  pg_fatal("You must have read and write access in the current directory.");
271 
272  check_bin_dir(&old_cluster, false);
274  check_bin_dir(&new_cluster, true);
276 }
static void check_bin_dir(ClusterInfo *cluster, bool check_versions)
Definition: exec.c:383
static void check_data_dir(ClusterInfo *cluster)
Definition: exec.c:341
ClusterInfo old_cluster
Definition: pg_upgrade.c:64
short access
Definition: preproc-type.c:36

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

Referenced by setup().