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

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

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

Referenced by verify_directories().

◆ check_data_dir()

static void check_data_dir ( ClusterInfo cluster)
static

Definition at line 343 of file exec.c.

344 {
345  const char *pg_data = cluster->pgdata;
346 
347  /* get the cluster version */
348  cluster->major_version = get_major_server_version(cluster);
349 
351  check_single_dir(pg_data, "base");
352  check_single_dir(pg_data, "global");
353  check_single_dir(pg_data, "pg_multixact");
354  check_single_dir(pg_data, "pg_subtrans");
355  check_single_dir(pg_data, "pg_tblspc");
356  check_single_dir(pg_data, "pg_twophase");
357 
358  /* pg_xlog has been renamed to pg_wal in v10 */
359  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
360  check_single_dir(pg_data, "pg_xlog");
361  else
362  check_single_dir(pg_data, "pg_wal");
363 
364  /* pg_clog has been renamed to pg_xact in v10 */
365  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
366  check_single_dir(pg_data, "pg_clog");
367  else
368  check_single_dir(pg_data, "pg_xact");
369 }
static void check_single_dir(const char *pg_data, const char *subdir)
Definition: exec.c:314
static char * pg_data
Definition: initdb.c:127
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 431 of file exec.c.

432 {
433  char path[MAXPGPATH];
434  char line[MAXPGPATH];
435  char cmd[MAXPGPATH];
436  char versionstr[128];
437 
438  snprintf(path, sizeof(path), "%s/%s", dir, program);
439 
440  if (validate_exec(path) != 0)
441  pg_fatal("check for \"%s\" failed: %m", path);
442 
443  snprintf(cmd, sizeof(cmd), "\"%s\" -V", path);
444 
445  if (!pipe_read_line(cmd, line, sizeof(line)))
446  pg_fatal("check for \"%s\" failed: cannot execute",
447  path);
448 
449  if (check_version)
450  {
451  pg_strip_crlf(line);
452 
453  snprintf(versionstr, sizeof(versionstr), "%s (PostgreSQL) " PG_VERSION, program);
454 
455  if (strcmp(line, versionstr) != 0)
456  pg_fatal("check for \"%s\" failed: incorrect version: found \"%s\", expected \"%s\"",
457  path, line, versionstr);
458  }
459 }
int validate_exec(const char *path)
Definition: exec.c:80
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:387
#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_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 314 of file exec.c.

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

References MAXPGPATH, pg_data, PG_FATAL, report_status(), S_ISDIR, snprintf, stat::st_mode, stat, and strerror.

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

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

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: %s",
48  cmd, strerror(errno));
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  if (v1 < 10)
59  {
60  /* old style, e.g. 9.6.1 */
61  cluster->bin_version = v1 * 10000 + v2 * 100;
62  }
63  else
64  {
65  /* new style, e.g. 10.1 */
66  cluster->bin_version = v1 * 10000;
67  }
68 }
static void output(uint64 loop_count)
#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, strerror, 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 234 of file exec.c.

235 {
236  char path[MAXPGPATH];
237  int fd;
238 
239  snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
240 
241  if ((fd = open(path, O_RDONLY, 0)) < 0)
242  {
243  /* ENOTDIR means we will throw a more useful error later */
244  if (errno != ENOENT && errno != ENOTDIR)
245  pg_fatal("could not open file \"%s\" for reading: %s",
246  path, strerror(errno));
247 
248  return false;
249  }
250 
251  close(fd);
252  return true;
253 }
#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, snprintf, and strerror.

Referenced by setup().

◆ verify_directories()

void verify_directories ( void  )

Definition at line 265 of file exec.c.

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

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

Referenced by setup().