PostgreSQL Source Code  git master
exec.c File Reference
#include "postgres_fe.h"
#include <fcntl.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)
 
static void get_bin_version (ClusterInfo *cluster)
 
static void validate_exec (const char *dir, const char *cmdName)
 
bool exec_prog (const char *log_file, const char *opt_log_file, bool throw_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)
static

Definition at line 368 of file exec.c.

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

Referenced by verify_directories().

369 {
370  struct stat statBuf;
371 
372  /* check bindir */
373  if (stat(cluster->bindir, &statBuf) != 0)
374  report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
375  cluster->bindir, strerror(errno));
376  else if (!S_ISDIR(statBuf.st_mode))
377  report_status(PG_FATAL, "\"%s\" is not a directory\n",
378  cluster->bindir);
379 
380  validate_exec(cluster->bindir, "postgres");
381  validate_exec(cluster->bindir, "pg_ctl");
382 
383  /*
384  * Fetch the binary version after checking for the existence of pg_ctl.
385  * This way we report a useful error if the pg_ctl binary used for version
386  * fetching is missing/broken.
387  */
388  get_bin_version(cluster);
389 
390  /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
391  if (GET_MAJOR_VERSION(cluster->bin_version) < 1000)
392  validate_exec(cluster->bindir, "pg_resetxlog");
393  else
394  validate_exec(cluster->bindir, "pg_resetwal");
395  if (cluster == &new_cluster)
396  {
397  /* these are only needed in the new cluster */
398  validate_exec(cluster->bindir, "psql");
399  validate_exec(cluster->bindir, "pg_dump");
400  validate_exec(cluster->bindir, "pg_dumpall");
401  }
402 }
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
#define stat(a, b)
Definition: win32_port.h:266
char * bindir
Definition: pg_upgrade.h:269
uint32 bin_version
Definition: pg_upgrade.h:276
static void validate_exec(const char *dir, const char *cmdName)
Definition: exec.c:411
#define S_ISDIR(m)
Definition: win32_port.h:307
static void get_bin_version(ClusterInfo *cluster)
Definition: exec.c:32
const char * strerror(int errnum)
Definition: strerror.c:19
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2

◆ check_data_dir()

static void check_data_dir ( ClusterInfo cluster)
static

Definition at line 330 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().

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

◆ check_single_dir()

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

Definition at line 301 of file exec.c.

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

Referenced by check_data_dir().

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

◆ exec_prog()

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

Definition at line 82 of file exec.c.

References MAXCMDLEN, PG_FATAL, pg_fatal(), pg_log(), PG_REPORT, pg_usleep(), PG_VERBOSE, report_status(), snprintf(), and vsnprintf().

Referenced by copy_subdir_files(), copy_xact_xlog_xid(), generate_old_dump(), main(), parallel_exec_prog(), prepare_new_cluster(), prepare_new_databases(), start_postmaster(), and stop_postmaster().

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

◆ get_bin_version()

static void get_bin_version ( ClusterInfo cluster)
static

Definition at line 32 of file exec.c.

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

Referenced by check_bin_dir().

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

◆ pid_lock_file_exists()

bool pid_lock_file_exists ( const char *  datadir)

Definition at line 221 of file exec.c.

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

Referenced by setup().

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

◆ validate_exec()

static void validate_exec ( const char *  dir,
const char *  cmdName 
)
static

Definition at line 411 of file exec.c.

References MAXPGPATH, pg_fatal(), pg_strcasecmp(), S_IRUSR, S_ISREG, S_IXUSR, snprintf(), stat, strerror(), and strlcat().

Referenced by check_bin_dir().

412 {
413  char path[MAXPGPATH];
414  struct stat buf;
415 
416  snprintf(path, sizeof(path), "%s/%s", dir, cmdName);
417 
418 #ifdef WIN32
419  /* Windows requires a .exe suffix for stat() */
420  if (strlen(path) <= strlen(EXE_EXT) ||
421  pg_strcasecmp(path + strlen(path) - strlen(EXE_EXT), EXE_EXT) != 0)
422  strlcat(path, EXE_EXT, sizeof(path));
423 #endif
424 
425  /*
426  * Ensure that the file exists and is a regular file.
427  */
428  if (stat(path, &buf) < 0)
429  pg_fatal("check for \"%s\" failed: %s\n",
430  path, strerror(errno));
431  else if (!S_ISREG(buf.st_mode))
432  pg_fatal("check for \"%s\" failed: not a regular file\n",
433  path);
434 
435  /*
436  * Ensure that the file is both executable and readable (required for
437  * dynamic loading).
438  */
439 #ifndef WIN32
440  if (access(path, R_OK) != 0)
441 #else
442  if ((buf.st_mode & S_IRUSR) == 0)
443 #endif
444  pg_fatal("check for \"%s\" failed: cannot read file (permission denied)\n",
445  path);
446 
447 #ifndef WIN32
448  if (access(path, X_OK) != 0)
449 #else
450  if ((buf.st_mode & S_IXUSR) == 0)
451 #endif
452  pg_fatal("check for \"%s\" failed: cannot execute (permission denied)\n",
453  path);
454 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define S_IXUSR
Definition: win32_port.h:277
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
#define S_ISREG(m)
Definition: win32_port.h:310
#define stat(a, b)
Definition: win32_port.h:266
#define S_IRUSR
Definition: win32_port.h:271
const char * strerror(int errnum)
Definition: strerror.c:19
size_t strlcat(char *dst, const char *src, size_t siz)
Definition: strlcat.c:33

◆ verify_directories()

void verify_directories ( void  )

Definition at line 252 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().

253 {
254 #ifndef WIN32
255  if (access(".", R_OK | W_OK | X_OK) != 0)
256 #else
257  if (win32_check_directory_write_permissions() != 0)
258 #endif
259  pg_fatal("You must have read and write access in the current directory.\n");
260 
265 }
static void check_bin_dir(ClusterInfo *cluster)
Definition: exec.c:368
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
static void check_data_dir(ClusterInfo *cluster)
Definition: exec.c:330