2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* util.c
|
|
|
|
*
|
|
|
|
* utility functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2015-01-07 00:43:47 +08:00
|
|
|
* Copyright (c) 2010-2015, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/util.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
|
|
|
|
2014-01-11 09:56:47 +08:00
|
|
|
#include "common/username.h"
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
2011-04-10 23:42:00 +08:00
|
|
|
LogOpts log_opts;
|
2010-10-20 05:38:16 +08:00
|
|
|
|
2015-03-27 02:03:19 +08:00
|
|
|
static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2, 0);
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* report_status()
|
|
|
|
*
|
|
|
|
* Displays the result of an operation (ok, failed, error message,...)
|
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
report_status(eLogType type, const char *fmt,...)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char message[MAX_STRING];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(message, sizeof(message), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(type, "%s\n", message);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-01 05:30:13 +08:00
|
|
|
/* force blank output for progress display */
|
|
|
|
void
|
|
|
|
end_progress_output(void)
|
|
|
|
{
|
|
|
|
/*
|
2013-05-30 04:58:43 +08:00
|
|
|
* In case nothing printed; pass a space so gcc doesn't complain about
|
|
|
|
* empty format string.
|
2012-12-01 05:30:13 +08:00
|
|
|
*/
|
|
|
|
prep_status(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
2010-10-20 05:38:16 +08:00
|
|
|
* prep_status
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
|
|
|
* Displays a message that describes an operation we are about to begin.
|
|
|
|
* We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
|
|
|
|
* "failed" indicators line up nicely.
|
|
|
|
*
|
|
|
|
* A typical sequence would look like this:
|
2010-10-20 05:38:16 +08:00
|
|
|
* prep_status("about to flarb the next %d files", fileCount );
|
2010-05-12 10:19:11 +08:00
|
|
|
*
|
|
|
|
* if(( message = flarbFiles(fileCount)) == NULL)
|
2010-10-20 05:38:16 +08:00
|
|
|
* report_status(PG_REPORT, "ok" );
|
2010-05-12 10:19:11 +08:00
|
|
|
* else
|
2011-05-07 09:47:12 +08:00
|
|
|
* pg_log(PG_FATAL, "failed - %s\n", message );
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status(const char *fmt,...)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char message[MAX_STRING];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(message, sizeof(message), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "%s", message);
|
2010-05-12 10:19:11 +08:00
|
|
|
else
|
2012-12-08 01:26:13 +08:00
|
|
|
/* trim strings that don't end in a newline */
|
|
|
|
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-27 02:03:19 +08:00
|
|
|
static void
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_log_v(eLogType type, const char *fmt, va_list ap)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2014-08-21 01:03:58 +08:00
|
|
|
char message[QUERY_ALLOC];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-10-02 09:24:56 +08:00
|
|
|
vsnprintf(message, sizeof(message), fmt, ap);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-08 01:26:13 +08:00
|
|
|
/* PG_VERBOSE and PG_STATUS are only output in verbose mode */
|
2012-08-11 05:14:48 +08:00
|
|
|
/* fopen() on log_opts.internal might have failed, so check it */
|
2012-12-08 01:26:13 +08:00
|
|
|
if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
|
|
|
|
log_opts.internal != NULL)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-12-08 01:26:13 +08:00
|
|
|
if (type == PG_STATUS)
|
|
|
|
/* status messages need two leading spaces and a newline */
|
|
|
|
fprintf(log_opts.internal, " %s\n", message);
|
|
|
|
else
|
|
|
|
fprintf(log_opts.internal, "%s", message);
|
2012-03-13 07:47:54 +08:00
|
|
|
fflush(log_opts.internal);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2012-03-13 07:47:54 +08:00
|
|
|
case PG_VERBOSE:
|
2010-10-20 10:31:17 +08:00
|
|
|
if (log_opts.verbose)
|
2010-05-12 10:19:11 +08:00
|
|
|
printf("%s", _(message));
|
|
|
|
break;
|
|
|
|
|
2012-12-08 01:26:13 +08:00
|
|
|
case PG_STATUS:
|
|
|
|
/* for output to a display, do leading truncation and append \r */
|
|
|
|
if (isatty(fileno(stdout)))
|
|
|
|
/* -2 because we use a 2-space indent */
|
2013-05-30 04:58:43 +08:00
|
|
|
printf(" %s%-*.*s\r",
|
|
|
|
/* prefix with "..." if we do leading truncation */
|
|
|
|
strlen(message) <= MESSAGE_WIDTH - 2 ? "" : "...",
|
|
|
|
MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
|
|
|
|
/* optional leading truncation */
|
|
|
|
strlen(message) <= MESSAGE_WIDTH - 2 ? message :
|
|
|
|
message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
|
2012-12-08 01:26:13 +08:00
|
|
|
else
|
|
|
|
printf(" %s\n", _(message));
|
|
|
|
break;
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
case PG_REPORT:
|
|
|
|
case PG_WARNING:
|
|
|
|
printf("%s", _(message));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PG_FATAL:
|
2011-05-07 09:47:12 +08:00
|
|
|
printf("\n%s", _(message));
|
2014-01-08 20:01:16 +08:00
|
|
|
printf("Failure, exiting\n");
|
|
|
|
exit(1);
|
2010-05-12 10:19:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-02 09:24:56 +08:00
|
|
|
void
|
|
|
|
pg_log(eLogType type, const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
pg_log_v(type, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pg_fatal(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
pg_log_v(PG_FATAL, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
printf("Failure, exiting\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
/* all seems well */
|
2010-10-20 05:38:16 +08:00
|
|
|
report_status(PG_REPORT, "ok");
|
2010-05-12 10:19:11 +08:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* quote_identifier()
|
|
|
|
* Properly double-quote a SQL identifier.
|
|
|
|
*
|
|
|
|
* The result should be pg_free'd, but most callers don't bother because
|
|
|
|
* memory leakage is not a big deal in this program.
|
|
|
|
*/
|
|
|
|
char *
|
2010-10-20 05:38:16 +08:00
|
|
|
quote_identifier(const char *s)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
char *result = pg_malloc(strlen(s) * 2 + 3);
|
2010-05-12 10:19:11 +08:00
|
|
|
char *r = result;
|
|
|
|
|
|
|
|
*r++ = '"';
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
if (*s == '"')
|
|
|
|
*r++ = *s;
|
|
|
|
*r++ = *s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*r++ = '"';
|
|
|
|
*r++ = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_user_info()
|
|
|
|
*/
|
|
|
|
int
|
2013-12-19 02:31:35 +08:00
|
|
|
get_user_info(char **user_name_p)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-07-07 03:19:02 +08:00
|
|
|
int user_id;
|
2013-12-19 02:31:35 +08:00
|
|
|
const char *user_name;
|
2013-12-19 01:16:16 +08:00
|
|
|
char *errstr;
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
user_id = geteuid();
|
2013-12-19 01:16:16 +08:00
|
|
|
#else
|
2010-05-12 10:19:11 +08:00
|
|
|
user_id = 1;
|
|
|
|
#endif
|
|
|
|
|
2013-12-19 02:31:35 +08:00
|
|
|
user_name = get_user_name(&errstr);
|
|
|
|
if (!user_name)
|
2013-12-19 01:16:16 +08:00
|
|
|
pg_fatal("%s\n", errstr);
|
|
|
|
|
|
|
|
/* make a copy */
|
2013-12-19 02:31:35 +08:00
|
|
|
*user_name_p = pg_strdup(user_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
return user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getErrorText()
|
|
|
|
*
|
|
|
|
* Returns the text of the error message for the given error number
|
|
|
|
*
|
|
|
|
* This feature is factored into a separate function because it is
|
|
|
|
* system-dependent.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
getErrorText(int errNum)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
_dosmaperr(GetLastError());
|
|
|
|
#endif
|
2011-07-21 04:37:17 +08:00
|
|
|
return pg_strdup(strerror(errNum));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2010-09-29 05:41:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* str2uint()
|
|
|
|
*
|
|
|
|
* convert string to oid
|
|
|
|
*/
|
|
|
|
unsigned int
|
|
|
|
str2uint(const char *str)
|
|
|
|
{
|
2010-09-29 10:40:25 +08:00
|
|
|
return strtoul(str, NULL, 10);
|
2010-09-29 05:41:03 +08:00
|
|
|
}
|
2011-05-16 22:46:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_putenv()
|
|
|
|
*
|
|
|
|
* This is like putenv(), but takes two arguments.
|
|
|
|
* It also does unsetenv() if val is NULL.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pg_putenv(const char *var, const char *val)
|
|
|
|
{
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
2013-10-13 12:09:18 +08:00
|
|
|
char *envstr;
|
2011-05-16 22:46:52 +08:00
|
|
|
|
2013-10-23 07:40:26 +08:00
|
|
|
envstr = psprintf("%s=%s", var, val);
|
2011-05-16 22:46:52 +08:00
|
|
|
putenv(envstr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not free envstr because it becomes part of the environment on
|
2014-05-07 00:12:18 +08:00
|
|
|
* some operating systems. See port/unsetenv.c::unsetenv.
|
2011-05-16 22:46:52 +08:00
|
|
|
*/
|
|
|
|
#else
|
|
|
|
SetEnvironmentVariableA(var, val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
unsetenv(var);
|
|
|
|
#else
|
|
|
|
SetEnvironmentVariableA(var, "");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|