2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* exec.c
|
|
|
|
*
|
|
|
|
* execution functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, 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
|
|
|
*/
|
|
|
|
|
|
|
|
#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-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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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,
|
2010-05-12 10:19:11 +08:00
|
|
|
"\nThere were problems executing %s\n", cmd);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL, "\ncould not open file \"%s\" for reading\n",
|
2010-07-14 02:09:55 +08:00
|
|
|
path);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Checking old data directory (%s)", old_cluster.pgdata);
|
|
|
|
check_data_dir(old_cluster.pgdata);
|
|
|
|
check_ok();
|
2010-07-14 02:09:55 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Checking old bin directory (%s)", old_cluster.bindir);
|
2011-01-02 01:06:36 +08:00
|
|
|
check_bin_dir(&old_cluster);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Checking new data directory (%s)", new_cluster.pgdata);
|
|
|
|
check_data_dir(new_cluster.pgdata);
|
|
|
|
check_ok();
|
2010-07-14 02:09:55 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Checking new bin directory (%s)", new_cluster.bindir);
|
2011-01-02 01:06:36 +08:00
|
|
|
check_bin_dir(&new_cluster);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
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;
|
|
|
|
const char *requiredSubdirs[] = {"base", "global", "pg_clog",
|
|
|
|
"pg_multixact", "pg_subtrans", "pg_tblspc", "pg_twophase",
|
2010-10-20 06:37:04 +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;
|
|
|
|
|
|
|
|
snprintf(subDirName, sizeof(subDirName), "%s/%s", pg_data,
|
|
|
|
requiredSubdirs[subdirnum]);
|
|
|
|
|
|
|
|
if (stat(subDirName, &statBuf) != 0)
|
2010-10-20 05:38:16 +08:00
|
|
|
report_status(PG_FATAL, "check for %s failed: %s",
|
2010-07-14 02:09:55 +08:00
|
|
|
requiredSubdirs[subdirnum], getErrorText(errno));
|
|
|
|
else if (!S_ISDIR(statBuf.st_mode))
|
2010-10-20 06:37:04 +08:00
|
|
|
report_status(PG_FATAL, "%s is not a directory",
|
|
|
|
requiredSubdirs[subdirnum]);
|
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-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-02-03 04:40:20 +08:00
|
|
|
pg_log(PG_FATAL, "check for %s failed - %s\n",
|
|
|
|
cmdName, getErrorText(errno));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-12-12 03:17:46 +08:00
|
|
|
if (!S_ISREG(buf.st_mode))
|
2011-02-03 04:40:20 +08:00
|
|
|
pg_log(PG_FATAL, "check for %s failed - not an executable file\n",
|
|
|
|
cmdName);
|
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
|
|
|
|
pg_log(PG_FATAL, "check for %s failed - cannot read file (permission denied)\n",
|
|
|
|
cmdName);
|
|
|
|
|
|
|
|
#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-02-03 04:40:20 +08:00
|
|
|
pg_log(PG_FATAL, "check for %s failed - cannot execute (permission denied)\n",
|
|
|
|
cmdName);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|