6 * Copyright (c) 2010-2012, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/exec.c
12 #include "pg_upgrade.h"
16 #include <sys/types.h>
18 static void check_data_dir(const char *pg_data);
19 static void check_bin_dir(ClusterInfo *cluster);
20 static void validate_exec(const char *dir, const char *cmdName);
23 static int win32_check_directory_write_permissions(void);
29 * Execute an external program with stdout/stderr redirected, and report
32 * Formats a command from the given argument list, logs it to the log file,
33 * and attempts to execute that command. If the command executes
34 * successfully, exec_prog() returns true.
36 * If the command fails, an error message is saved to the specified log_file.
37 * If throw_error is true, this raises a PG_FATAL error and pg_upgrade
38 * terminates; otherwise it is just reported as PG_REPORT and exec_prog()
42 exec_prog(const char *log_file, const char *opt_log_file,
43 bool throw_error, const char *fmt,...)
47 #define MAXCMDLEN (2 * MAXPGPATH)
53 old_umask = umask(S_IRWXG | S_IRWXO);
55 written = strlcpy(cmd, SYSTEMQUOTE, sizeof(cmd));
57 written += vsnprintf(cmd + written, MAXCMDLEN - written, fmt, ap);
59 if (written >= MAXCMDLEN)
60 pg_log(PG_FATAL, "command too long\n");
61 written += snprintf(cmd + written, MAXCMDLEN - written,
62 " >> \"%s\" 2>&1" SYSTEMQUOTE, log_file);
63 if (written >= MAXCMDLEN)
64 pg_log(PG_FATAL, "command too long\n");
66 if ((log = fopen_priv(log_file, "a")) == NULL)
67 pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
71 pg_log(PG_VERBOSE, "%s\n", cmd);
72 fprintf(log, "command: %s\n", cmd);
75 * In Windows, we must close the log file at this point so the file is not
76 * open while the command is running, or we get a share violation.
86 report_status(PG_REPORT, "*failure*");
88 pg_log(PG_VERBOSE, "There were problems executing \"%s\"\n", cmd);
90 pg_log(throw_error ? PG_FATAL : PG_REPORT,
91 "Consult the last few lines of \"%s\" or \"%s\" for\n"
92 "the probable cause of the failure.\n",
93 log_file, opt_log_file);
95 pg_log(throw_error ? PG_FATAL : PG_REPORT,
96 "Consult the last few lines of \"%s\" for\n"
97 "the probable cause of the failure.\n",
103 * We can't do this on Windows because it will keep the "pg_ctl start"
104 * output filename open until the server stops, so we do the \n\n above
105 * on that platform. We use a unique filename for "pg_ctl start" that is
106 * never reused while the server is running, so it works fine. We could
107 * log these commands to a third file, but that just adds complexity.
109 if ((log = fopen_priv(log_file, "a")) == NULL)
110 pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
111 fprintf(log, "\n\n");
120 * is_server_running()
122 * checks whether postmaster on the given data directory is running or not.
123 * The check is performed by looking for the existence of postmaster.pid file.
126 is_server_running(const char *datadir)
128 char path[MAXPGPATH];
131 snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
133 if ((fd = open(path, O_RDONLY, 0)) < 0)
135 /* ENOTDIR means we will throw a more useful error later */
136 if (errno != ENOENT && errno != ENOTDIR)
137 pg_log(PG_FATAL, "could not open file \"%s\" for reading: %s\n",
138 path, getErrorText(errno));
149 * verify_directories()
151 * does all the hectic work of verifying directories and executables
152 * of old and new server.
154 * NOTE: May update the values of all parameters
157 verify_directories(void)
160 prep_status("Checking current, bin, and data directories");
163 if (access(".", R_OK | W_OK | X_OK) != 0)
165 if (win32_check_directory_write_permissions() != 0)
168 "You must have read and write access in the current directory.\n");
170 check_bin_dir(&old_cluster);
171 check_data_dir(old_cluster.pgdata);
172 check_bin_dir(&new_cluster);
173 check_data_dir(new_cluster.pgdata);
180 * win32_check_directory_write_permissions()
182 * access() on WIN32 can't check directory permissions, so we have to
183 * optionally create, then delete a file to check.
184 * http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx
187 win32_check_directory_write_permissions(void)
192 * We open a file we would normally create anyway. We do this even in
193 * 'check' mode, which isn't ideal, but this is the best we can do.
195 if ((fd = open(GLOBALS_DUMP_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
199 return unlink(GLOBALS_DUMP_FILE);
207 * This function validates the given cluster directory - we search for a
208 * small set of subdirectories that we expect to find in a valid $PGDATA
209 * directory. If any of the subdirectories are missing (or secured against
210 * us) we display an error message and exit()
214 check_data_dir(const char *pg_data)
216 char subDirName[MAXPGPATH];
219 /* start check with top-most directory */
220 const char *requiredSubdirs[] = {"", "base", "global", "pg_clog",
221 "pg_multixact", "pg_subtrans", "pg_tblspc", "pg_twophase",
225 subdirnum < sizeof(requiredSubdirs) / sizeof(requiredSubdirs[0]);
230 snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
231 /* Win32 can't stat() a directory with a trailing slash. */
232 *requiredSubdirs[subdirnum] ? "/" : "",
233 requiredSubdirs[subdirnum]);
235 if (stat(subDirName, &statBuf) != 0)
236 report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
237 subDirName, getErrorText(errno));
238 else if (!S_ISDIR(statBuf.st_mode))
239 report_status(PG_FATAL, "%s is not a directory\n",
248 * This function searches for the executables that we expect to find
249 * in the binaries directory. If we find that a required executable
250 * is missing (or secured against us), we display an error message and
254 check_bin_dir(ClusterInfo *cluster)
259 if (stat(cluster->bindir, &statBuf) != 0)
260 report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
261 cluster->bindir, getErrorText(errno));
262 else if (!S_ISDIR(statBuf.st_mode))
263 report_status(PG_FATAL, "%s is not a directory\n",
266 validate_exec(cluster->bindir, "postgres");
267 validate_exec(cluster->bindir, "pg_ctl");
268 validate_exec(cluster->bindir, "pg_resetxlog");
269 if (cluster == &new_cluster)
271 /* these are only needed in the new cluster */
272 validate_exec(cluster->bindir, "pg_config");
273 validate_exec(cluster->bindir, "psql");
274 validate_exec(cluster->bindir, "pg_dumpall");
282 * validate "path" as an executable file
285 validate_exec(const char *dir, const char *cmdName)
287 char path[MAXPGPATH];
290 snprintf(path, sizeof(path), "%s/%s", dir, cmdName);
293 /* Windows requires a .exe suffix for stat() */
294 if (strlen(path) <= strlen(EXE_EXT) ||
295 pg_strcasecmp(path + strlen(path) - strlen(EXE_EXT), EXE_EXT) != 0)
296 strlcat(path, EXE_EXT, sizeof(path));
300 * Ensure that the file exists and is a regular file.
302 if (stat(path, &buf) < 0)
303 pg_log(PG_FATAL, "check for \"%s\" failed: %s\n",
304 path, getErrorText(errno));
305 else if (!S_ISREG(buf.st_mode))
306 pg_log(PG_FATAL, "check for \"%s\" failed: not an executable file\n",
310 * Ensure that the file is both executable and readable (required for
314 if (access(path, R_OK) != 0)
316 if ((buf.st_mode & S_IRUSR) == 0)
318 pg_log(PG_FATAL, "check for \"%s\" failed: cannot read file (permission denied)\n",
322 if (access(path, X_OK) != 0)
324 if ((buf.st_mode & S_IXUSR) == 0)
326 pg_log(PG_FATAL, "check for \"%s\" failed: cannot execute (permission denied)\n",