2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* exec.c
|
|
|
|
*
|
|
|
|
* execution functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2012-01-02 07:01:58 +08:00
|
|
|
* Copyright (c) 2010-2012, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/exec.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2010-12-12 03:17:46 +08:00
|
|
|
#include <unistd.h>
|
2012-03-13 07:47:54 +08:00
|
|
|
#include <sys/types.h>
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 06:37:04 +08:00
|
|
|
static void check_data_dir(const char *pg_data);
|
2011-01-02 01:06:36 +08:00
|
|
|
static void check_bin_dir(ClusterInfo *cluster);
|
2011-04-10 23:42:00 +08:00
|
|
|
static void validate_exec(const char *dir, const char *cmdName);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-07-24 13:42:45 +08:00
|
|
|
#ifdef WIN32
|
2012-06-11 03:20:04 +08:00
|
|
|
static int win32_check_directory_write_permissions(void);
|
2011-07-24 13:42:45 +08:00
|
|
|
#endif
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exec_prog()
|
|
|
|
*
|
|
|
|
* Formats a command from the given argument list and executes that
|
|
|
|
* command. If the command executes, exec_prog() returns 1 otherwise
|
|
|
|
* exec_prog() logs an error message and returns 0.
|
|
|
|
*
|
|
|
|
* If throw_error is TRUE, this function will throw a PG_FATAL error
|
|
|
|
* instead of returning should an error occur.
|
|
|
|
*/
|
|
|
|
int
|
2012-03-13 07:47:54 +08:00
|
|
|
exec_prog(bool throw_error, bool is_priv,
|
|
|
|
const char *log_file, const char *fmt,...)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int result;
|
|
|
|
char cmd[MAXPGPATH];
|
2012-03-28 02:47:23 +08:00
|
|
|
mode_t old_umask = 0;
|
2012-03-13 07:47:54 +08:00
|
|
|
|
|
|
|
if (is_priv)
|
|
|
|
old_umask = umask(S_IRWXG | S_IRWXO);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(cmd, MAXPGPATH, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "%s\n", cmd);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
result = system(cmd);
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
if (is_priv)
|
|
|
|
umask(old_umask);
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2012-03-13 07:47:54 +08:00
|
|
|
report_status(PG_REPORT, "*failure*");
|
|
|
|
fflush(stdout);
|
|
|
|
pg_log(PG_VERBOSE, "There were problems executing \"%s\"\n", cmd);
|
|
|
|
pg_log(throw_error ? PG_FATAL : PG_REPORT,
|
|
|
|
"Consult the last few lines of \"%s\" for\n"
|
|
|
|
"the probable cause of the failure.\n",
|
2012-06-11 03:20:04 +08:00
|
|
|
log_file);
|
2010-05-12 10:19:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-14 02:09:55 +08:00
|
|
|
/*
|
|
|
|
* is_server_running()
|
|
|
|
*
|
|
|
|
* checks whether postmaster on the given data directory is running or not.
|
|
|
|
* The check is performed by looking for the existence of postmaster.pid file.
|
|
|
|
*/
|
|
|
|
bool
|
2010-10-20 05:38:16 +08:00
|
|
|
is_server_running(const char *datadir)
|
2010-07-14 02:09:55 +08:00
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/postmaster.pid", datadir);
|
|
|
|
|
|
|
|
if ((fd = open(path, O_RDONLY, 0)) < 0)
|
|
|
|
{
|
2011-05-19 10:22:40 +08:00
|
|
|
/* ENOTDIR means we will throw a more useful error later */
|
|
|
|
if (errno != ENOENT && errno != ENOTDIR)
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "could not open file \"%s\" for reading: %s\n",
|
|
|
|
path, getErrorText(errno));
|
2010-07-14 02:09:55 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* verify_directories()
|
|
|
|
*
|
|
|
|
* does all the hectic work of verifying directories and executables
|
|
|
|
* of old and new server.
|
|
|
|
*
|
|
|
|
* NOTE: May update the values of all parameters
|
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
verify_directories(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-05-16 23:01:29 +08:00
|
|
|
|
2011-05-19 06:36:52 +08:00
|
|
|
prep_status("Checking current, bin, and data directories");
|
|
|
|
|
2011-05-19 00:13:37 +08:00
|
|
|
#ifndef WIN32
|
2011-07-24 13:42:45 +08:00
|
|
|
if (access(".", R_OK | W_OK | X_OK) != 0)
|
|
|
|
#else
|
|
|
|
if (win32_check_directory_write_permissions() != 0)
|
2011-05-19 00:13:37 +08:00
|
|
|
#endif
|
2011-05-16 23:01:29 +08:00
|
|
|
pg_log(PG_FATAL,
|
2011-06-10 02:32:50 +08:00
|
|
|
"You must have read and write access in the current directory.\n");
|
2011-05-16 23:01:29 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
check_bin_dir(&old_cluster);
|
2011-05-19 06:36:52 +08:00
|
|
|
check_data_dir(old_cluster.pgdata);
|
2011-01-02 01:06:36 +08:00
|
|
|
check_bin_dir(&new_cluster);
|
2011-05-19 06:36:52 +08:00
|
|
|
check_data_dir(new_cluster.pgdata);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-24 13:42:45 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
/*
|
|
|
|
* win32_check_directory_write_permissions()
|
|
|
|
*
|
|
|
|
* access() on WIN32 can't check directory permissions, so we have to
|
|
|
|
* optionally create, then delete a file to check.
|
|
|
|
* http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
win32_check_directory_write_permissions(void)
|
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
int fd;
|
2011-07-24 13:42:45 +08:00
|
|
|
|
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* We open a file we would normally create anyway. We do this even in
|
|
|
|
* 'check' mode, which isn't ideal, but this is the best we can do.
|
|
|
|
*/
|
2011-07-24 13:42:45 +08:00
|
|
|
if ((fd = open(GLOBALS_DUMP_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
|
|
|
|
return -1;
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return unlink(GLOBALS_DUMP_FILE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
2010-07-14 02:09:55 +08:00
|
|
|
* check_data_dir()
|
|
|
|
*
|
|
|
|
* This function validates the given cluster directory - we search for a
|
|
|
|
* small set of subdirectories that we expect to find in a valid $PGDATA
|
|
|
|
* directory. If any of the subdirectories are missing (or secured against
|
|
|
|
* us) we display an error message and exit()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
check_data_dir(const char *pg_data)
|
2010-07-14 02:09:55 +08:00
|
|
|
{
|
|
|
|
char subDirName[MAXPGPATH];
|
|
|
|
int subdirnum;
|
2011-06-10 02:32:50 +08:00
|
|
|
|
2011-05-19 06:36:52 +08:00
|
|
|
/* start check with top-most directory */
|
|
|
|
const char *requiredSubdirs[] = {"", "base", "global", "pg_clog",
|
2010-07-14 02:09:55 +08:00
|
|
|
"pg_multixact", "pg_subtrans", "pg_tblspc", "pg_twophase",
|
2011-06-10 02:32:50 +08:00
|
|
|
"pg_xlog"};
|
2010-07-14 02:09:55 +08:00
|
|
|
|
|
|
|
for (subdirnum = 0;
|
|
|
|
subdirnum < sizeof(requiredSubdirs) / sizeof(requiredSubdirs[0]);
|
|
|
|
++subdirnum)
|
|
|
|
{
|
|
|
|
struct stat statBuf;
|
2011-06-10 02:32:50 +08:00
|
|
|
|
2011-07-30 13:50:18 +08:00
|
|
|
snprintf(subDirName, sizeof(subDirName), "%s%s%s", pg_data,
|
2012-06-11 03:20:04 +08:00
|
|
|
/* Win32 can't stat() a directory with a trailing slash. */
|
2011-07-30 13:50:18 +08:00
|
|
|
*requiredSubdirs[subdirnum] ? "/" : "",
|
2010-07-14 02:09:55 +08:00
|
|
|
requiredSubdirs[subdirnum]);
|
|
|
|
|
|
|
|
if (stat(subDirName, &statBuf) != 0)
|
2011-07-12 12:13:51 +08:00
|
|
|
report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
subDirName, getErrorText(errno));
|
2010-07-14 02:09:55 +08:00
|
|
|
else if (!S_ISDIR(statBuf.st_mode))
|
2011-05-07 09:47:12 +08:00
|
|
|
report_status(PG_FATAL, "%s is not a directory\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
subDirName);
|
2010-07-14 02:09:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_bin_dir()
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
|
|
|
* This function searches for the executables that we expect to find
|
|
|
|
* in the binaries directory. If we find that a required executable
|
|
|
|
* is missing (or secured against us), we display an error message and
|
|
|
|
* exit().
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-02 01:06:36 +08:00
|
|
|
check_bin_dir(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-05-19 06:36:52 +08:00
|
|
|
struct stat statBuf;
|
|
|
|
|
|
|
|
/* check bindir */
|
|
|
|
if (stat(cluster->bindir, &statBuf) != 0)
|
2011-07-12 12:13:51 +08:00
|
|
|
report_status(PG_FATAL, "check for \"%s\" failed: %s\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
cluster->bindir, getErrorText(errno));
|
|
|
|
else if (!S_ISDIR(statBuf.st_mode))
|
2011-06-10 02:32:50 +08:00
|
|
|
report_status(PG_FATAL, "%s is not a directory\n",
|
|
|
|
cluster->bindir);
|
2011-05-19 06:36:52 +08:00
|
|
|
|
2011-02-03 04:40:20 +08:00
|
|
|
validate_exec(cluster->bindir, "postgres");
|
|
|
|
validate_exec(cluster->bindir, "pg_ctl");
|
|
|
|
validate_exec(cluster->bindir, "pg_resetxlog");
|
2011-01-02 01:06:36 +08:00
|
|
|
if (cluster == &new_cluster)
|
2010-12-30 02:43:53 +08:00
|
|
|
{
|
|
|
|
/* these are only needed in the new cluster */
|
2011-02-03 04:40:20 +08:00
|
|
|
validate_exec(cluster->bindir, "pg_config");
|
|
|
|
validate_exec(cluster->bindir, "psql");
|
|
|
|
validate_exec(cluster->bindir, "pg_dumpall");
|
2010-12-30 02:43:53 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* validate_exec()
|
|
|
|
*
|
|
|
|
* validate "path" as an executable file
|
|
|
|
*/
|
2011-02-03 04:40:20 +08:00
|
|
|
static void
|
|
|
|
validate_exec(const char *dir, const char *cmdName)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-02-03 04:40:20 +08:00
|
|
|
char path[MAXPGPATH];
|
2010-05-12 10:19:11 +08:00
|
|
|
struct stat buf;
|
|
|
|
|
2011-02-03 04:40:20 +08:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", dir, cmdName);
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#ifdef WIN32
|
2011-02-03 09:26:43 +08:00
|
|
|
/* Windows requires a .exe suffix for stat() */
|
|
|
|
if (strlen(path) <= strlen(EXE_EXT) ||
|
2010-05-12 10:19:11 +08:00
|
|
|
pg_strcasecmp(path + strlen(path) - strlen(EXE_EXT), EXE_EXT) != 0)
|
2011-02-03 09:26:43 +08:00
|
|
|
strlcat(path, EXE_EXT, sizeof(path));
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the file exists and is a regular file.
|
|
|
|
*/
|
|
|
|
if (stat(path, &buf) < 0)
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "check for \"%s\" failed: %s\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
path, getErrorText(errno));
|
|
|
|
else if (!S_ISREG(buf.st_mode))
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "check for \"%s\" failed: not an executable file\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
path);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the file is both executable and readable (required for
|
|
|
|
* dynamic loading).
|
|
|
|
*/
|
|
|
|
#ifndef WIN32
|
2010-12-12 03:17:46 +08:00
|
|
|
if (access(path, R_OK) != 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
#else
|
|
|
|
if ((buf.st_mode & S_IRUSR) == 0)
|
2011-02-03 04:40:20 +08:00
|
|
|
#endif
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "check for \"%s\" failed: cannot read file (permission denied)\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
path);
|
2011-02-03 04:40:20 +08:00
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
if (access(path, X_OK) != 0)
|
|
|
|
#else
|
2010-05-12 10:19:11 +08:00
|
|
|
if ((buf.st_mode & S_IXUSR) == 0)
|
|
|
|
#endif
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "check for \"%s\" failed: cannot execute (permission denied)\n",
|
2011-05-19 06:36:52 +08:00
|
|
|
path);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|