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