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_file, 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)

Referenced by exec_prog().

Function Documentation

◆ check_bin_dir()

static void check_bin_dir ( ClusterInfo cluster,
bool  check_versions 
)
static

Definition at line 371 of file exec.c.

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

Referenced by verify_directories().

372 {
373  struct stat statBuf;
374 
375  /* check bindir */
376  if (stat(cluster->bindir, &statBuf) != 0)
377  report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
378  cluster->bindir, strerror(errno));
379  else if (!S_ISDIR(statBuf.st_mode))
380  report_status(PG_FATAL, "\"%s\" is not a directory\n",
381  cluster->bindir);
382 
383  check_exec(cluster->bindir, "postgres", check_versions);
384  check_exec(cluster->bindir, "pg_controldata", check_versions);
385  check_exec(cluster->bindir, "pg_ctl", check_versions);
386 
387  /*
388  * Fetch the binary version after checking for the existence of pg_ctl.
389  * This way we report a useful error if the pg_ctl binary used for version
390  * fetching is missing/broken.
391  */
392  get_bin_version(cluster);
393 
394  /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
395  if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
396  check_exec(cluster->bindir, "pg_resetxlog", check_versions);
397  else
398  check_exec(cluster->bindir, "pg_resetwal", check_versions);
399 
400  if (cluster == &new_cluster)
401  {
402  /*
403  * These binaries are only needed for the target version. pg_dump and
404  * pg_dumpall are used to dump the old cluster, but must be of the
405  * target version.
406  */
407  check_exec(cluster->bindir, "initdb", check_versions);
408  check_exec(cluster->bindir, "pg_dump", check_versions);
409  check_exec(cluster->bindir, "pg_dumpall", check_versions);
410  check_exec(cluster->bindir, "pg_restore", check_versions);
411  check_exec(cluster->bindir, "psql", check_versions);
412  check_exec(cluster->bindir, "vacuumdb", check_versions);
413  }
414 }
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
static void check_exec(const char *dir, const char *program, bool check_version)
Definition: exec.c:417
char * bindir
Definition: pg_upgrade.h:264
uint32 bin_version
Definition: pg_upgrade.h:271
#define strerror
Definition: port.h:229
#define S_ISDIR(m)
Definition: win32_port.h:316
static void get_bin_version(ClusterInfo *cluster)
Definition: exec.c:33
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define stat
Definition: win32_port.h:275

◆ check_data_dir()

static void check_data_dir ( ClusterInfo cluster)
static

Definition at line 329 of file exec.c.

References check_single_dir(), get_major_server_version(), GET_MAJOR_VERSION, ClusterInfo::major_version, pg_data, and ClusterInfo::pgdata.

Referenced by verify_directories().

330 {
331  const char *pg_data = cluster->pgdata;
332 
333  /* get the cluster version */
334  cluster->major_version = get_major_server_version(cluster);
335 
336  check_single_dir(pg_data, "");
337  check_single_dir(pg_data, "base");
338  check_single_dir(pg_data, "global");
339  check_single_dir(pg_data, "pg_multixact");
340  check_single_dir(pg_data, "pg_subtrans");
341  check_single_dir(pg_data, "pg_tblspc");
342  check_single_dir(pg_data, "pg_twophase");
343 
344  /* pg_xlog has been renamed to pg_wal in v10 */
345  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
346  check_single_dir(pg_data, "pg_xlog");
347  else
348  check_single_dir(pg_data, "pg_wal");
349 
350  /* pg_clog has been renamed to pg_xact in v10 */
351  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
352  check_single_dir(pg_data, "pg_clog");
353  else
354  check_single_dir(pg_data, "pg_xact");
355 }
uint32 get_major_server_version(ClusterInfo *cluster)
Definition: server.c:158
uint32 major_version
Definition: pg_upgrade.h:269
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
static char * pg_data
Definition: initdb.c:124
static void check_single_dir(const char *pg_data, const char *subdir)
Definition: exec.c:300
char * pgdata
Definition: pg_upgrade.h:261

◆ check_exec()

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

Definition at line 417 of file exec.c.

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

Referenced by check_bin_dir().

418 {
419  char path[MAXPGPATH];
420  char line[MAXPGPATH];
421  char cmd[MAXPGPATH];
422  char versionstr[128];
423  int ret;
424 
425  snprintf(path, sizeof(path), "%s/%s", dir, program);
426 
427  ret = validate_exec(path);
428 
429  if (ret == -1)
430  pg_fatal("check for \"%s\" failed: not a regular file\n",
431  path);
432  else if (ret == -2)
433  pg_fatal("check for \"%s\" failed: cannot execute (permission denied)\n",
434  path);
435 
436  snprintf(cmd, sizeof(cmd), "\"%s\" -V", path);
437 
438  if (!pipe_read_line(cmd, line, sizeof(line)))
439  pg_fatal("check for \"%s\" failed: cannot execute\n",
440  path);
441 
442  if (check_version)
443  {
444  pg_strip_crlf(line);
445 
446  snprintf(versionstr, sizeof(versionstr), "%s (PostgreSQL) " PG_VERSION, program);
447 
448  if (strcmp(line, versionstr) != 0)
449  pg_fatal("check for \"%s\" failed: incorrect version: found \"%s\", expected \"%s\"\n",
450  path, line, versionstr);
451  }
452 }
int pg_strip_crlf(char *str)
Definition: string.c:121
#define pg_fatal(...)
Definition: pg_rewind.h:37
#define MAXPGPATH
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:358
#define snprintf
Definition: port.h:216
int validate_exec(const char *path)
Definition: exec.c:66

◆ check_single_dir()

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

Definition at line 300 of file exec.c.

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

Referenced by check_data_dir().

301 {
302  struct stat statBuf;
303  char subDirName[MAXPGPATH];
304 
305  snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
306  /* Win32 can't stat() a directory with a trailing slash. */
307  *subdir ? "/" : "",
308  subdir);
309 
310  if (stat(subDirName, &statBuf) != 0)
311  report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
312  subDirName, strerror(errno));
313  else if (!S_ISDIR(statBuf.st_mode))
314  report_status(PG_FATAL, "\"%s\" is not a directory\n",
315  subDirName);
316 }
#define MAXPGPATH
static char * pg_data
Definition: initdb.c:124
#define strerror
Definition: port.h:229
#define S_ISDIR(m)
Definition: win32_port.h:316
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define snprintf
Definition: port.h:216
#define stat
Definition: win32_port.h:275

◆ exec_prog()

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

Definition at line 81 of file exec.c.

References fprintf, MAXCMDLEN, pg_fatal, PG_FATAL, pg_log(), PG_REPORT, pg_usleep(), PG_VERBOSE, report_status(), snprintf, generate_unaccent_rules::stdout, 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().

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

◆ get_bin_version()

static void get_bin_version ( ClusterInfo cluster)
static

Definition at line 33 of file exec.c.

References ClusterInfo::bin_version, ClusterInfo::bindir, MAX_STRING, MAXPGPATH, output(), pg_fatal, snprintf, and strerror.

Referenced by check_bin_dir().

34 {
35  char cmd[MAXPGPATH],
36  cmd_output[MAX_STRING];
37  FILE *output;
38  int v1 = 0,
39  v2 = 0;
40 
41  snprintf(cmd, sizeof(cmd), "\"%s/pg_ctl\" --version", cluster->bindir);
42 
43  if ((output = popen(cmd, "r")) == NULL ||
44  fgets(cmd_output, sizeof(cmd_output), output) == NULL)
45  pg_fatal("could not get pg_ctl version data using %s: %s\n",
46  cmd, strerror(errno));
47 
48  pclose(output);
49 
50  if (sscanf(cmd_output, "%*s %*s %d.%d", &v1, &v2) < 1)
51  pg_fatal("could not get pg_ctl version output from %s\n", cmd);
52 
53  if (v1 < 10)
54  {
55  /* old style, e.g. 9.6.1 */
56  cluster->bin_version = v1 * 10000 + v2 * 100;
57  }
58  else
59  {
60  /* new style, e.g. 10.1 */
61  cluster->bin_version = v1 * 10000;
62  }
63 }
static void output(uint64 loop_count)
#define pg_fatal(...)
Definition: pg_rewind.h:37
#define MAXPGPATH
char * bindir
Definition: pg_upgrade.h:264
#define MAX_STRING
Definition: pg_upgrade.h:18
uint32 bin_version
Definition: pg_upgrade.h:271
#define strerror
Definition: port.h:229
#define snprintf
Definition: port.h:216

◆ pid_lock_file_exists()

bool pid_lock_file_exists ( const char *  datadir)

Definition at line 220 of file exec.c.

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

Referenced by setup().

221 {
222  char path[MAXPGPATH];
223  int fd;
224 
225  snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
226 
227  if ((fd = open(path, O_RDONLY, 0)) < 0)
228  {
229  /* ENOTDIR means we will throw a more useful error later */
230  if (errno != ENOENT && errno != ENOTDIR)
231  pg_fatal("could not open file \"%s\" for reading: %s\n",
232  path, strerror(errno));
233 
234  return false;
235  }
236 
237  close(fd);
238  return true;
239 }
#define pg_fatal(...)
Definition: pg_rewind.h:37
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define MAXPGPATH
char * datadir
#define strerror
Definition: port.h:229
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:216

◆ verify_directories()

void verify_directories ( void  )

Definition at line 251 of file exec.c.

References check_bin_dir(), check_data_dir(), close, fd(), GLOBALS_DUMP_FILE, new_cluster, old_cluster, pg_fatal, S_IRUSR, and S_IWUSR.

Referenced by setup().

252 {
253 #ifndef WIN32
254  if (access(".", R_OK | W_OK | X_OK) != 0)
255 #else
256  if (win32_check_directory_write_permissions() != 0)
257 #endif
258  pg_fatal("You must have read and write access in the current directory.\n");
259 
260  check_bin_dir(&old_cluster, false);
262  check_bin_dir(&new_cluster, true);
264 }
#define pg_fatal(...)
Definition: pg_rewind.h:37
static void check_bin_dir(ClusterInfo *cluster, bool check_versions)
Definition: exec.c:371
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void check_data_dir(ClusterInfo *cluster)
Definition: exec.c:329