2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* exec.c
|
|
|
|
*
|
|
|
|
* execution functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2013-01-02 06:15:01 +08:00
|
|
|
* Copyright (c) 2010-2013, 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
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 09:16:24 +08:00
|
|
|
|
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()
|
2012-08-28 02:21:09 +08:00
|
|
|
* Execute an external program with stdout/stderr redirected, and report
|
|
|
|
* errors
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
2012-08-28 02:21:09 +08:00
|
|
|
* Formats a command from the given argument list, logs it to the log file,
|
|
|
|
* and attempts to execute that command. If the command executes
|
|
|
|
* successfully, exec_prog() returns true.
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
2012-08-28 02:21:09 +08:00
|
|
|
* If the command fails, an error message is saved to the specified log_file.
|
|
|
|
* If throw_error is true, this raises a PG_FATAL error and pg_upgrade
|
|
|
|
* terminates; otherwise it is just reported as PG_REPORT and exec_prog()
|
|
|
|
* returns false.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
2012-08-28 02:21:09 +08:00
|
|
|
bool
|
|
|
|
exec_prog(const char *log_file, const char *opt_log_file,
|
|
|
|
bool throw_error, const char *fmt,...)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int result;
|
2012-08-28 02:21:09 +08:00
|
|
|
int written;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-08-28 02:21:09 +08:00
|
|
|
#define MAXCMDLEN (2 * MAXPGPATH)
|
|
|
|
char cmd[MAXCMDLEN];
|
2012-03-28 02:47:23 +08:00
|
|
|
mode_t old_umask = 0;
|
2012-08-08 01:10:44 +08:00
|
|
|
FILE *log;
|
2012-08-28 02:21:09 +08:00
|
|
|
va_list ap;
|
2012-03-13 07:47:54 +08:00
|
|
|
|
2012-08-28 02:21:09 +08:00
|
|
|
old_umask = umask(S_IRWXG | S_IRWXO);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-09-04 03:31:26 +08:00
|
|
|
written = strlcpy(cmd, SYSTEMQUOTE, sizeof(cmd));
|
2012-08-28 02:21:09 +08:00
|
|
|
va_start(ap, fmt);
|
|
|
|
written += vsnprintf(cmd + written, MAXCMDLEN - written, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (written >= MAXCMDLEN)
|
|
|
|
pg_log(PG_FATAL, "command too long\n");
|
|
|
|
written += snprintf(cmd + written, MAXCMDLEN - written,
|
|
|
|
" >> \"%s\" 2>&1" SYSTEMQUOTE, log_file);
|
|
|
|
if (written >= MAXCMDLEN)
|
|
|
|
pg_log(PG_FATAL, "command too long\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-09-06 11:14:49 +08:00
|
|
|
log = fopen_priv(log_file, "a");
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
{
|
2013-05-30 04:58:43 +08:00
|
|
|
/*
|
|
|
|
* "pg_ctl -w stop" might have reported that the server has stopped
|
|
|
|
* because the postmaster.pid file has been removed, but "pg_ctl -w
|
|
|
|
* start" might still be in the process of closing and might still be
|
|
|
|
* holding its stdout and -l log file descriptors open. Therefore,
|
|
|
|
* try to open the log file a few more times.
|
2012-09-06 11:14:49 +08:00
|
|
|
*/
|
2013-05-30 04:58:43 +08:00
|
|
|
int iter;
|
|
|
|
|
2012-09-06 11:14:49 +08:00
|
|
|
for (iter = 0; iter < 4 && log == NULL; iter++)
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
log = fopen_priv(log_file, "a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (log == NULL)
|
2012-08-08 01:10:44 +08:00
|
|
|
pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
|
2012-09-05 12:01:13 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
fprintf(log, "\n\n");
|
|
|
|
#endif
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "%s\n", cmd);
|
2012-06-29 11:27:00 +08:00
|
|
|
fprintf(log, "command: %s\n", cmd);
|
2012-08-28 02:21:09 +08:00
|
|
|
|
2012-08-08 01:10:44 +08:00
|
|
|
/*
|
2012-08-28 02:21:09 +08:00
|
|
|
* In Windows, we must close the log file at this point so the file is not
|
|
|
|
* open while the command is running, or we get a share violation.
|
2012-08-08 01:10:44 +08:00
|
|
|
*/
|
|
|
|
fclose(log);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
result = system(cmd);
|
|
|
|
|
2012-08-28 02:21:09 +08:00
|
|
|
umask(old_umask);
|
2012-03-13 07:47:54 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2012-12-01 05:30:13 +08:00
|
|
|
/* we might be in on a progress status line, so go to the next line */
|
|
|
|
report_status(PG_REPORT, "\n*failure*");
|
2012-03-13 07:47:54 +08:00
|
|
|
fflush(stdout);
|
2012-12-01 05:30:13 +08:00
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
pg_log(PG_VERBOSE, "There were problems executing \"%s\"\n", cmd);
|
2012-08-28 02:21:09 +08:00
|
|
|
if (opt_log_file)
|
|
|
|
pg_log(throw_error ? PG_FATAL : PG_REPORT,
|
|
|
|
"Consult the last few lines of \"%s\" or \"%s\" for\n"
|
|
|
|
"the probable cause of the failure.\n",
|
|
|
|
log_file, opt_log_file);
|
|
|
|
else
|
|
|
|
pg_log(throw_error ? PG_FATAL : PG_REPORT,
|
|
|
|
"Consult the last few lines of \"%s\" for\n"
|
|
|
|
"the probable cause of the failure.\n",
|
|
|
|
log_file);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
2012-09-04 03:31:26 +08:00
|
|
|
#ifndef WIN32
|
2013-05-30 04:58:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't do this on Windows because it will keep the "pg_ctl start"
|
|
|
|
* output filename open until the server stops, so we do the \n\n above on
|
|
|
|
* that platform. We use a unique filename for "pg_ctl start" that is
|
|
|
|
* never reused while the server is running, so it works fine. We could
|
|
|
|
* log these commands to a third file, but that just adds complexity.
|
2012-09-04 03:31:26 +08:00
|
|
|
*/
|
2012-09-05 12:01:13 +08:00
|
|
|
if ((log = fopen_priv(log_file, "a")) == NULL)
|
2012-08-08 01:10:44 +08:00
|
|
|
pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
|
2012-06-29 11:27:00 +08:00
|
|
|
fprintf(log, "\n\n");
|
|
|
|
fclose(log);
|
2012-09-04 03:31:26 +08:00
|
|
|
#endif
|
2012-06-29 11:27:00 +08:00
|
|
|
|
2012-08-28 02:21:09 +08:00
|
|
|
return result == 0;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-14 02:09:55 +08:00
|
|
|
/*
|
2013-01-25 04:20:11 +08:00
|
|
|
* pid_lock_file_exists()
|
2010-07-14 02:09:55 +08:00
|
|
|
*
|
2013-01-25 04:20:11 +08:00
|
|
|
* Checks whether the postmaster.pid file exists.
|
2010-07-14 02:09:55 +08:00
|
|
|
*/
|
|
|
|
bool
|
2013-01-25 04:20:11 +08:00
|
|
|
pid_lock_file_exists(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-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-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, "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
|
|
|
}
|