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>
|
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);
|
2011-07-24 13:42:45 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
static int win32_check_directory_write_permissions(void);
|
|
|
|
#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
|
2010-10-20 05:38:16 +08:00
|
|
|
exec_prog(bool throw_error, const char *fmt,...)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int result;
|
|
|
|
char cmd[MAXPGPATH];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(cmd, MAXPGPATH, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_INFO, "%s\n", cmd);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
result = system(cmd);
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(throw_error ? PG_FATAL : PG_INFO,
|
2011-07-12 12:13:51 +08:00
|
|
|
"There were problems executing \"%s\"\n", cmd);
|
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)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
/* Win32 can't stat() a directory with a trailing slash. */
|
|
|
|
*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
|
|
|
}
|