PostgreSQL Source Code  git master
exec.c
Go to the documentation of this file.
1 /*
2  * exec.c
3  *
4  * execution functions
5  *
6  * Copyright (c) 2010-2022, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/exec.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include <fcntl.h>
13 
14 #include "common/string.h"
15 #include "pg_upgrade.h"
16 
17 static void check_data_dir(ClusterInfo *cluster);
18 static void check_bin_dir(ClusterInfo *cluster, bool check_versions);
19 static void get_bin_version(ClusterInfo *cluster);
20 static void check_exec(const char *dir, const char *program, bool check_version);
21 
22 #ifdef WIN32
23 static int win32_check_directory_write_permissions(void);
24 #endif
25 
26 
27 /*
28  * get_bin_version
29  *
30  * Fetch major version of binaries for cluster.
31  */
32 static void
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 }
64 
65 
66 /*
67  * exec_prog()
68  * Execute an external program with stdout/stderr redirected, and report
69  * errors
70  *
71  * Formats a command from the given argument list, logs it to the log file,
72  * and attempts to execute that command. If the command executes
73  * successfully, exec_prog() returns true.
74  *
75  * If the command fails, an error message is optionally written to the specified
76  * log_file, and the program optionally exits.
77  *
78  * The code requires it be called first from the primary thread on Windows.
79  */
80 bool
81 exec_prog(const char *log_filename, const char *opt_log_file,
82  bool report_error, bool exit_on_error, const char *fmt,...)
83 {
84  int result = 0;
85  int written;
86  char log_file[MAXPGPATH];
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  snprintf(log_file, MAXPGPATH, "%s/%s", log_opts.logdir, log_filename);
102 
103  written = 0;
104  va_start(ap, fmt);
105  written += vsnprintf(cmd + written, MAXCMDLEN - written, fmt, ap);
106  va_end(ap);
107  if (written >= MAXCMDLEN)
108  pg_fatal("command too long\n");
109  written += snprintf(cmd + written, MAXCMDLEN - written,
110  " >> \"%s\" 2>&1", log_file);
111  if (written >= MAXCMDLEN)
112  pg_fatal("command too long\n");
113 
114  pg_log(PG_VERBOSE, "%s\n", cmd);
115 
116 #ifdef WIN32
117 
118  /*
119  * For some reason, Windows issues a file-in-use error if we write data to
120  * the log file from a non-primary thread just before we create a
121  * subprocess that also writes to the same log file. One fix is to sleep
122  * for 100ms. A cleaner fix is to write to the log file _after_ the
123  * subprocess has completed, so we do this only when writing from a
124  * non-primary thread. fflush(), running system() twice, and pre-creating
125  * the file do not see to help.
126  */
127  if (mainThreadId != GetCurrentThreadId())
128  result = system(cmd);
129 #endif
130 
131  log = fopen(log_file, "a");
132 
133 #ifdef WIN32
134  {
135  /*
136  * "pg_ctl -w stop" might have reported that the server has stopped
137  * because the postmaster.pid file has been removed, but "pg_ctl -w
138  * start" might still be in the process of closing and might still be
139  * holding its stdout and -l log file descriptors open. Therefore,
140  * try to open the log file a few more times.
141  */
142  int iter;
143 
144  for (iter = 0; iter < 4 && log == NULL; iter++)
145  {
146  pg_usleep(1000000); /* 1 sec */
147  log = fopen(log_file, "a");
148  }
149  }
150 #endif
151 
152  if (log == NULL)
153  pg_fatal("could not open log file \"%s\": %m\n", log_file);
154 
155 #ifdef WIN32
156  /* Are we printing "command:" before its output? */
157  if (mainThreadId == GetCurrentThreadId())
158  fprintf(log, "\n\n");
159 #endif
160  fprintf(log, "command: %s\n", cmd);
161 #ifdef WIN32
162  /* Are we printing "command:" after its output? */
163  if (mainThreadId != GetCurrentThreadId())
164  fprintf(log, "\n\n");
165 #endif
166 
167  /*
168  * In Windows, we must close the log file at this point so the file is not
169  * open while the command is running, or we get a share violation.
170  */
171  fclose(log);
172 
173 #ifdef WIN32
174  /* see comment above */
175  if (mainThreadId == GetCurrentThreadId())
176 #endif
177  result = system(cmd);
178 
179  if (result != 0 && report_error)
180  {
181  /* we might be in on a progress status line, so go to the next line */
182  report_status(PG_REPORT, "\n*failure*");
183  fflush(stdout);
184 
185  pg_log(PG_VERBOSE, "There were problems executing \"%s\"\n", cmd);
186  if (opt_log_file)
187  pg_log(exit_on_error ? PG_FATAL : PG_REPORT,
188  "Consult the last few lines of \"%s\" or \"%s\" for\n"
189  "the probable cause of the failure.\n",
190  log_file, opt_log_file);
191  else
192  pg_log(exit_on_error ? PG_FATAL : PG_REPORT,
193  "Consult the last few lines of \"%s\" for\n"
194  "the probable cause of the failure.\n",
195  log_file);
196  }
197 
198 #ifndef WIN32
199 
200  /*
201  * We can't do this on Windows because it will keep the "pg_ctl start"
202  * output filename open until the server stops, so we do the \n\n above on
203  * that platform. We use a unique filename for "pg_ctl start" that is
204  * never reused while the server is running, so it works fine. We could
205  * log these commands to a third file, but that just adds complexity.
206  */
207  if ((log = fopen(log_file, "a")) == NULL)
208  pg_fatal("could not write to log file \"%s\": %m\n", log_file);
209  fprintf(log, "\n\n");
210  fclose(log);
211 #endif
212 
213  return result == 0;
214 }
215 
216 
217 /*
218  * pid_lock_file_exists()
219  *
220  * Checks whether the postmaster.pid file exists.
221  */
222 bool
224 {
225  char path[MAXPGPATH];
226  int fd;
227 
228  snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
229 
230  if ((fd = open(path, O_RDONLY, 0)) < 0)
231  {
232  /* ENOTDIR means we will throw a more useful error later */
233  if (errno != ENOENT && errno != ENOTDIR)
234  pg_fatal("could not open file \"%s\" for reading: %s\n",
235  path, strerror(errno));
236 
237  return false;
238  }
239 
240  close(fd);
241  return true;
242 }
243 
244 
245 /*
246  * verify_directories()
247  *
248  * does all the hectic work of verifying directories and executables
249  * of old and new server.
250  *
251  * NOTE: May update the values of all parameters
252  */
253 void
255 {
256 #ifndef WIN32
257  if (access(".", R_OK | W_OK | X_OK) != 0)
258 #else
259  if (win32_check_directory_write_permissions() != 0)
260 #endif
261  pg_fatal("You must have read and write access in the current directory.\n");
262 
263  check_bin_dir(&old_cluster, false);
265  check_bin_dir(&new_cluster, true);
267 }
268 
269 
270 #ifdef WIN32
271 /*
272  * win32_check_directory_write_permissions()
273  *
274  * access() on WIN32 can't check directory permissions, so we have to
275  * optionally create, then delete a file to check.
276  * http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx
277  */
278 static int
279 win32_check_directory_write_permissions(void)
280 {
281  int fd;
282 
283  /*
284  * We open a file we would normally create anyway. We do this even in
285  * 'check' mode, which isn't ideal, but this is the best we can do.
286  */
287  if ((fd = open(GLOBALS_DUMP_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
288  return -1;
289  close(fd);
290 
291  return unlink(GLOBALS_DUMP_FILE);
292 }
293 #endif
294 
295 
296 /*
297  * check_single_dir()
298  *
299  * Check for the presence of a single directory in PGDATA, and fail if
300  * is it missing or not accessible.
301  */
302 static void
303 check_single_dir(const char *pg_data, const char *subdir)
304 {
305  struct stat statBuf;
306  char subDirName[MAXPGPATH];
307 
308  snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
309  /* Win32 can't stat() a directory with a trailing slash. */
310  *subdir ? "/" : "",
311  subdir);
312 
313  if (stat(subDirName, &statBuf) != 0)
314  report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
315  subDirName, strerror(errno));
316  else if (!S_ISDIR(statBuf.st_mode))
317  report_status(PG_FATAL, "\"%s\" is not a directory\n",
318  subDirName);
319 }
320 
321 
322 /*
323  * check_data_dir()
324  *
325  * This function validates the given cluster directory - we search for a
326  * small set of subdirectories that we expect to find in a valid $PGDATA
327  * directory. If any of the subdirectories are missing (or secured against
328  * us) we display an error message and exit()
329  *
330  */
331 static void
333 {
334  const char *pg_data = cluster->pgdata;
335 
336  /* get the cluster version */
337  cluster->major_version = get_major_server_version(cluster);
338 
340  check_single_dir(pg_data, "base");
341  check_single_dir(pg_data, "global");
342  check_single_dir(pg_data, "pg_multixact");
343  check_single_dir(pg_data, "pg_subtrans");
344  check_single_dir(pg_data, "pg_tblspc");
345  check_single_dir(pg_data, "pg_twophase");
346 
347  /* pg_xlog has been renamed to pg_wal in v10 */
348  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
349  check_single_dir(pg_data, "pg_xlog");
350  else
351  check_single_dir(pg_data, "pg_wal");
352 
353  /* pg_clog has been renamed to pg_xact in v10 */
354  if (GET_MAJOR_VERSION(cluster->major_version) <= 906)
355  check_single_dir(pg_data, "pg_clog");
356  else
357  check_single_dir(pg_data, "pg_xact");
358 }
359 
360 
361 /*
362  * check_bin_dir()
363  *
364  * This function searches for the executables that we expect to find
365  * in the binaries directory. If we find that a required executable
366  * is missing (or secured against us), we display an error message and
367  * exit().
368  *
369  * If check_versions is true, then the versions of the binaries are checked
370  * against the version of this pg_upgrade. This is for checking the target
371  * bindir.
372  */
373 static void
374 check_bin_dir(ClusterInfo *cluster, bool check_versions)
375 {
376  struct stat statBuf;
377 
378  /* check bindir */
379  if (stat(cluster->bindir, &statBuf) != 0)
380  report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
381  cluster->bindir, strerror(errno));
382  else if (!S_ISDIR(statBuf.st_mode))
383  report_status(PG_FATAL, "\"%s\" is not a directory\n",
384  cluster->bindir);
385 
386  check_exec(cluster->bindir, "postgres", check_versions);
387  check_exec(cluster->bindir, "pg_controldata", check_versions);
388  check_exec(cluster->bindir, "pg_ctl", check_versions);
389 
390  /*
391  * Fetch the binary version after checking for the existence of pg_ctl.
392  * This way we report a useful error if the pg_ctl binary used for version
393  * fetching is missing/broken.
394  */
396 
397  /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
398  if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
399  check_exec(cluster->bindir, "pg_resetxlog", check_versions);
400  else
401  check_exec(cluster->bindir, "pg_resetwal", check_versions);
402 
403  if (cluster == &new_cluster)
404  {
405  /*
406  * These binaries are only needed for the target version. pg_dump and
407  * pg_dumpall are used to dump the old cluster, but must be of the
408  * target version.
409  */
410  check_exec(cluster->bindir, "initdb", check_versions);
411  check_exec(cluster->bindir, "pg_dump", check_versions);
412  check_exec(cluster->bindir, "pg_dumpall", check_versions);
413  check_exec(cluster->bindir, "pg_restore", check_versions);
414  check_exec(cluster->bindir, "psql", check_versions);
415  check_exec(cluster->bindir, "vacuumdb", check_versions);
416  }
417 }
418 
419 static void
420 check_exec(const char *dir, const char *program, bool check_version)
421 {
422  char path[MAXPGPATH];
423  char line[MAXPGPATH];
424  char cmd[MAXPGPATH];
425  char versionstr[128];
426  int ret;
427 
428  snprintf(path, sizeof(path), "%s/%s", dir, program);
429 
430  ret = validate_exec(path);
431 
432  if (ret == -1)
433  pg_fatal("check for \"%s\" failed: not a regular file\n",
434  path);
435  else if (ret == -2)
436  pg_fatal("check for \"%s\" failed: cannot execute (permission denied)\n",
437  path);
438 
439  snprintf(cmd, sizeof(cmd), "\"%s\" -V", path);
440 
441  if (!pipe_read_line(cmd, line, sizeof(line)))
442  pg_fatal("check for \"%s\" failed: cannot execute\n",
443  path);
444 
445  if (check_version)
446  {
447  pg_strip_crlf(line);
448 
449  snprintf(versionstr, sizeof(versionstr), "%s (PostgreSQL) " PG_VERSION, program);
450 
451  if (strcmp(line, versionstr) != 0)
452  pg_fatal("check for \"%s\" failed: incorrect version: found \"%s\", expected \"%s\"\n",
453  path, line, versionstr);
454  }
455 }
bool exec_prog(const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:81
#define MAXCMDLEN
static void check_exec(const char *dir, const char *program, bool check_version)
Definition: exec.c:420
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:223
static void check_single_dir(const char *pg_data, const char *subdir)
Definition: exec.c:303
static void check_bin_dir(ClusterInfo *cluster, bool check_versions)
Definition: exec.c:374
static void check_data_dir(ClusterInfo *cluster)
Definition: exec.c:332
void verify_directories(void)
Definition: exec.c:254
static void get_bin_version(ClusterInfo *cluster)
Definition: exec.c:33
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
int validate_exec(const char *path)
Definition: exec.c:79
char * pipe_read_line(char *cmd, char *line, int maxsize)
Definition: exec.c:371
static char * pg_data
Definition: initdb.c:126
#define close(a)
Definition: win32.h:12
static void const char fflush(stdout)
va_end(args)
static void const char * fmt
va_start(args, fmt)
#define pg_fatal(...)
#define MAXPGPATH
static char * log_file
Definition: pg_ctl.c:91
char * datadir
static void output(uint64 loop_count)
ClusterInfo new_cluster
Definition: pg_upgrade.c:63
ClusterInfo old_cluster
Definition: pg_upgrade.c:62
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:29
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
LogOpts log_opts
Definition: util.c:17
@ PG_FATAL
Definition: pg_upgrade.h:241
@ PG_VERBOSE
Definition: pg_upgrade.h:237
@ PG_REPORT
Definition: pg_upgrade.h:239
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:26
#define MAX_STRING
Definition: pg_upgrade.h:21
uint32 get_major_server_version(ClusterInfo *cluster)
Definition: server.c:159
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define vsnprintf
Definition: port.h:224
#define strerror
Definition: port.h:238
#define snprintf
Definition: port.h:225
#define fprintf
Definition: port.h:229
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void pg_usleep(long microsec)
Definition: signal.c:53
int pg_strip_crlf(char *str)
Definition: string.c:121
char * logdir
Definition: pg_upgrade.h:284
unsigned short st_mode
Definition: win32_port.h:268
#define stat
Definition: win32_port.h:283
#define S_ISDIR(m)
Definition: win32_port.h:324
#define S_IRUSR
Definition: win32_port.h:288
#define S_IWUSR
Definition: win32_port.h:291