2003-11-10 23:51:16 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* initdb --- initialize a PostgreSQL installation
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* initdb creates (initializes) a PostgreSQL database cluster (site,
|
|
|
|
* instance, installation, whatever). A database cluster is a
|
2006-06-18 17:38:37 +02:00
|
|
|
* collection of PostgreSQL databases all managed by the same server.
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* To create the database cluster, we create the directory that contains
|
|
|
|
* all its data, create the files that hold the global tables, create
|
2005-06-21 06:02:34 +02:00
|
|
|
* a few other control files for it, and create three databases: the
|
|
|
|
* template databases "template0" and "template1", and a default user
|
|
|
|
* database "postgres".
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* The template databases are ordinary PostgreSQL databases. template0
|
|
|
|
* is never supposed to change after initdb, whereas template1 can be
|
|
|
|
* changed to add site-local standard data. Either one can be copied
|
|
|
|
* to produce a new database.
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2005-06-21 06:02:34 +02:00
|
|
|
* For largely-historical reasons, the template1 database is the one built
|
2005-10-15 04:49:52 +02:00
|
|
|
* by the basic bootstrap process. After it is complete, template0 and
|
2005-06-21 06:02:34 +02:00
|
|
|
* the default database, postgres, are made just by copying template1.
|
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* To create template1, we run the postgres (backend) program in bootstrap
|
|
|
|
* mode and feed it data from the postgres.bki library file. After this
|
|
|
|
* initial bootstrap phase, some additional stuff is created by normal
|
|
|
|
* SQL commands fed to a standalone backend. Some of those commands are
|
|
|
|
* just embedded into this program (yeah, it's ugly), but larger chunks
|
|
|
|
* are taken from script files.
|
|
|
|
*
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
|
|
|
* Note:
|
|
|
|
* The program has some memory leakage - it isn't worth cleaning it up.
|
2003-11-14 00:46:31 +01:00
|
|
|
*
|
|
|
|
* This is a C implementation of the previous shell script for setting up a
|
|
|
|
* PostgreSQL cluster location, and should be highly compatible with it.
|
|
|
|
* author of C translation: Andrew Dunstan mailto:andrew@dunslane.net
|
|
|
|
*
|
|
|
|
* This code is released under the terms of the PostgreSQL License.
|
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2003-11-14 00:46:31 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
2003-11-17 21:35:28 +01:00
|
|
|
* Portions taken from FreeBSD.
|
2003-11-14 00:46:31 +01:00
|
|
|
*
|
2008-12-11 08:34:09 +01:00
|
|
|
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.165 2008/12/11 07:34:07 petere Exp $
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <signal.h>
|
2005-12-09 16:51:14 +01:00
|
|
|
#include <time.h>
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
2005-08-22 18:27:36 +02:00
|
|
|
#include "getaddrinfo.h"
|
2003-11-13 02:09:24 +01:00
|
|
|
#include "getopt_long.h"
|
2005-12-09 16:51:14 +01:00
|
|
|
#include "miscadmin.h"
|
2003-11-13 02:09:24 +01:00
|
|
|
|
2004-11-27 19:51:08 +01:00
|
|
|
#ifndef HAVE_INT_OPTRESET
|
2003-11-13 02:09:24 +01:00
|
|
|
int optreset;
|
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/*
|
|
|
|
* these values are passed in by makefile defines
|
|
|
|
*/
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *share_path = NULL;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
/* values to be obtained from arguments */
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *pg_data = "";
|
|
|
|
static char *encoding = "";
|
|
|
|
static char *locale = "";
|
|
|
|
static char *lc_collate = "";
|
|
|
|
static char *lc_ctype = "";
|
|
|
|
static char *lc_monetary = "";
|
|
|
|
static char *lc_numeric = "";
|
|
|
|
static char *lc_time = "";
|
|
|
|
static char *lc_messages = "";
|
2007-08-21 03:11:32 +02:00
|
|
|
static const char *default_text_search_config = "";
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *username = "";
|
|
|
|
static bool pwprompt = false;
|
|
|
|
static char *pwfilename = NULL;
|
|
|
|
static char *authmethod = "";
|
|
|
|
static bool debug = false;
|
|
|
|
static bool noclean = false;
|
|
|
|
static bool show_setting = false;
|
2007-01-06 20:40:00 +01:00
|
|
|
static char *xlog_dir = "";
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* internal vars */
|
2005-03-11 16:36:27 +01:00
|
|
|
static const char *progname;
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *encodingid = "0";
|
|
|
|
static char *bki_file;
|
|
|
|
static char *desc_file;
|
2006-02-12 04:22:21 +01:00
|
|
|
static char *shdesc_file;
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *hba_file;
|
|
|
|
static char *ident_file;
|
|
|
|
static char *conf_file;
|
|
|
|
static char *conversion_file;
|
2007-08-21 03:11:32 +02:00
|
|
|
static char *dictionary_file;
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *info_schema_file;
|
|
|
|
static char *features_file;
|
|
|
|
static char *system_views_file;
|
|
|
|
static bool made_new_pgdata = false;
|
|
|
|
static bool found_existing_pgdata = false;
|
2007-01-06 20:40:00 +01:00
|
|
|
static bool made_new_xlogdir = false;
|
|
|
|
static bool found_existing_xlogdir = false;
|
2005-10-15 04:49:52 +02:00
|
|
|
static char infoversion[100];
|
|
|
|
static bool caught_signal = false;
|
|
|
|
static bool output_failed = false;
|
|
|
|
static int output_errno = 0;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
/* defaults */
|
2005-10-15 04:49:52 +02:00
|
|
|
static int n_connections = 10;
|
|
|
|
static int n_buffers = 50;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-08-01 07:59:13 +02:00
|
|
|
/*
|
|
|
|
* Warning messages for authentication methods
|
|
|
|
*/
|
2005-03-11 16:36:27 +01:00
|
|
|
#define AUTHTRUST_WARNING \
|
|
|
|
"# CAUTION: Configuring the system for local \"trust\" authentication allows\n" \
|
|
|
|
"# any local user to connect as any PostgreSQL user, including the database\n" \
|
|
|
|
"# superuser. If you do not trust all your local users, use another\n" \
|
|
|
|
"# authentication method.\n"
|
2005-10-15 04:49:52 +02:00
|
|
|
static char *authwarning = NULL;
|
2004-08-01 07:59:13 +02:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/*
|
|
|
|
* Centralized knowledge of switches to pass to backend
|
|
|
|
*
|
|
|
|
* Note: in the shell-script version, we also passed PGDATA as a -D switch,
|
|
|
|
* but here it is more convenient to pass it as an environment variable
|
|
|
|
* (no quoting to worry about).
|
|
|
|
*/
|
|
|
|
static const char *boot_options = "-F";
|
2006-06-18 17:38:37 +02:00
|
|
|
static const char *backend_options = "--single -F -O -c search_path=pg_catalog -c exit_on_error=true";
|
2003-11-14 00:46:31 +01:00
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-05-11 23:57:15 +02:00
|
|
|
/* path to 'initdb' binary directory */
|
2005-10-15 04:49:52 +02:00
|
|
|
static char bin_path[MAXPGPATH];
|
|
|
|
static char backend_exec[MAXPGPATH];
|
2004-05-10 22:51:58 +02:00
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
static void *pg_malloc(size_t size);
|
2004-05-10 22:51:58 +02:00
|
|
|
static char *xstrdup(const char *s);
|
2004-12-27 21:39:21 +01:00
|
|
|
static char **replace_token(char **lines,
|
2005-10-15 04:49:52 +02:00
|
|
|
const char *token, const char *replacement);
|
|
|
|
|
2004-10-06 11:13:10 +02:00
|
|
|
#ifndef HAVE_UNIX_SOCKETS
|
2004-12-27 21:39:21 +01:00
|
|
|
static char **filter_lines_with_token(char **lines, const char *token);
|
2004-10-06 11:01:18 +02:00
|
|
|
#endif
|
2004-05-10 22:51:58 +02:00
|
|
|
static char **readfile(char *path);
|
|
|
|
static void writefile(char *path, char **lines);
|
2004-11-29 04:05:03 +01:00
|
|
|
static FILE *popen_check(const char *command, const char *mode);
|
2004-08-29 07:07:03 +02:00
|
|
|
static int mkdir_p(char *path, mode_t omode);
|
2004-05-10 22:51:58 +02:00
|
|
|
static void exit_nicely(void);
|
2003-11-10 23:51:16 +01:00
|
|
|
static char *get_id(void);
|
2004-05-10 22:51:58 +02:00
|
|
|
static char *get_encoding_id(char *encoding_name);
|
2003-11-10 23:51:16 +01:00
|
|
|
static char *get_short_version(void);
|
2007-11-15 22:14:46 +01:00
|
|
|
static int check_data_dir(char *dir);
|
2004-07-19 04:47:16 +02:00
|
|
|
static bool mkdatadir(const char *subdir);
|
2004-05-10 22:51:58 +02:00
|
|
|
static void set_input(char **dest, char *filename);
|
2003-11-10 23:51:16 +01:00
|
|
|
static void check_input(char *path);
|
2004-05-10 22:51:58 +02:00
|
|
|
static void set_short_version(char *short_version, char *extrapath);
|
2003-11-10 23:51:16 +01:00
|
|
|
static void set_null_conf(void);
|
2006-01-01 00:50:59 +01:00
|
|
|
static void test_config_settings(void);
|
2003-11-10 23:51:16 +01:00
|
|
|
static void setup_config(void);
|
2004-05-10 22:51:58 +02:00
|
|
|
static void bootstrap_template1(char *short_version);
|
2005-06-28 07:09:14 +02:00
|
|
|
static void setup_auth(void);
|
2003-11-10 23:52:10 +01:00
|
|
|
static void get_set_pwd(void);
|
2003-11-10 23:51:16 +01:00
|
|
|
static void setup_depend(void);
|
|
|
|
static void setup_sysviews(void);
|
|
|
|
static void setup_description(void);
|
|
|
|
static void setup_conversion(void);
|
2007-08-21 03:11:32 +02:00
|
|
|
static void setup_dictionary(void);
|
2003-11-10 23:51:16 +01:00
|
|
|
static void setup_privileges(void);
|
|
|
|
static void set_info_version(void);
|
|
|
|
static void setup_schema(void);
|
|
|
|
static void vacuum_db(void);
|
|
|
|
static void make_template0(void);
|
2005-06-21 06:02:34 +02:00
|
|
|
static void make_postgres(void);
|
2004-05-10 22:51:58 +02:00
|
|
|
static void trapsig(int signum);
|
2003-11-10 23:51:16 +01:00
|
|
|
static void check_ok(void);
|
2004-10-07 20:57:26 +02:00
|
|
|
static char *escape_quotes(const char *src);
|
2005-12-09 16:51:14 +01:00
|
|
|
static int locale_date_order(const char *locale);
|
2008-09-23 12:58:03 +02:00
|
|
|
static bool check_locale_name(const char *locale);
|
|
|
|
static bool check_locale_encoding(const char *locale, int encoding);
|
2004-05-10 22:51:58 +02:00
|
|
|
static void setlocales(void);
|
|
|
|
static void usage(const char *progname);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-02-10 23:05:42 +01:00
|
|
|
#ifdef WIN32
|
2006-10-04 02:30:14 +02:00
|
|
|
static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo);
|
2006-02-10 23:05:42 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-05-10 22:51:58 +02:00
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* macros for running pipes to postgres
|
|
|
|
*/
|
2004-11-29 04:05:03 +01:00
|
|
|
#define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd
|
2003-11-10 23:52:10 +01:00
|
|
|
|
|
|
|
#define PG_CMD_OPEN \
|
|
|
|
do { \
|
2004-11-29 04:05:03 +01:00
|
|
|
cmdfd = popen_check(cmd, "w"); \
|
|
|
|
if (cmdfd == NULL) \
|
|
|
|
exit_nicely(); /* message already printed by popen_check */ \
|
2003-11-10 23:52:10 +01:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PG_CMD_CLOSE \
|
|
|
|
do { \
|
2004-11-29 04:05:03 +01:00
|
|
|
if (pclose_check(cmdfd)) \
|
|
|
|
exit_nicely(); /* message already printed by pclose_check */ \
|
2003-11-10 23:52:10 +01:00
|
|
|
} while (0)
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
#define PG_CMD_PUTS(line) \
|
2003-11-10 23:52:10 +01:00
|
|
|
do { \
|
2004-11-29 04:05:03 +01:00
|
|
|
if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
|
|
|
|
output_failed = true, output_errno = errno; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PG_CMD_PRINTF1(fmt, arg1) \
|
|
|
|
do { \
|
|
|
|
if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
|
|
|
|
output_failed = true, output_errno = errno; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define PG_CMD_PRINTF2(fmt, arg1, arg2) \
|
|
|
|
do { \
|
|
|
|
if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
|
|
|
|
output_failed = true, output_errno = errno; \
|
2003-11-10 23:52:10 +01:00
|
|
|
} while (0)
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-10 23:52:10 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
#define QUOTE_PATH ""
|
2004-11-29 02:14:45 +01:00
|
|
|
#define DIR_SEP "/"
|
2003-11-10 23:52:10 +01:00
|
|
|
#else
|
|
|
|
#define QUOTE_PATH "\""
|
2004-11-29 02:14:45 +01:00
|
|
|
#define DIR_SEP "\\"
|
2003-11-10 23:51:16 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2003-11-10 23:52:10 +01:00
|
|
|
* routines to check mem allocations and fail noisily.
|
2003-11-14 00:46:31 +01:00
|
|
|
*
|
2003-11-10 23:51:16 +01:00
|
|
|
* Note that we can't call exit_nicely() on a memory failure, as it calls
|
|
|
|
* rmtree() which needs memory allocation. So we just exit with a bang.
|
|
|
|
*/
|
|
|
|
static void *
|
2005-07-10 18:13:13 +02:00
|
|
|
pg_malloc(size_t size)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
result = malloc(size);
|
|
|
|
if (!result)
|
|
|
|
{
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
2003-11-10 23:51:16 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
xstrdup(const char *s)
|
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
result = strdup(s);
|
|
|
|
if (!result)
|
|
|
|
{
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("%s: out of memory\n"), progname);
|
2003-11-10 23:51:16 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make a copy of the array of lines, with token replaced by replacement
|
|
|
|
* the first time it occurs on each line.
|
2003-11-14 00:46:31 +01:00
|
|
|
*
|
2003-11-10 23:51:16 +01:00
|
|
|
* This does most of what sed was used for in the shell script, but
|
|
|
|
* doesn't need any regexp stuff.
|
|
|
|
*/
|
|
|
|
static char **
|
2004-12-27 21:39:21 +01:00
|
|
|
replace_token(char **lines, const char *token, const char *replacement)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
int numlines = 1;
|
|
|
|
int i;
|
|
|
|
char **result;
|
|
|
|
int toklen,
|
|
|
|
replen,
|
|
|
|
diff;
|
|
|
|
|
|
|
|
for (i = 0; lines[i]; i++)
|
|
|
|
numlines++;
|
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
result = (char **) pg_malloc(numlines * sizeof(char *));
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
toklen = strlen(token);
|
|
|
|
replen = strlen(replacement);
|
|
|
|
diff = replen - toklen;
|
|
|
|
|
|
|
|
for (i = 0; i < numlines; i++)
|
|
|
|
{
|
|
|
|
char *where;
|
|
|
|
char *newline;
|
|
|
|
int pre;
|
|
|
|
|
|
|
|
/* just copy pointer if NULL or no change needed */
|
|
|
|
if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
|
|
|
|
{
|
|
|
|
result[i] = lines[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we get here a change is needed - set up new line */
|
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
pre = where - lines[i];
|
|
|
|
|
|
|
|
strncpy(newline, lines[i], pre);
|
|
|
|
|
|
|
|
strcpy(newline + pre, replacement);
|
|
|
|
|
|
|
|
strcpy(newline + pre + replen, lines[i] + pre + toklen);
|
|
|
|
|
|
|
|
result[i] = newline;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-10-06 11:01:18 +02:00
|
|
|
/*
|
|
|
|
* make a copy of lines without any that contain the token
|
|
|
|
*
|
2004-12-27 21:39:21 +01:00
|
|
|
* a sort of poor man's grep -v
|
2004-10-06 11:01:18 +02:00
|
|
|
*/
|
2004-10-06 11:13:10 +02:00
|
|
|
#ifndef HAVE_UNIX_SOCKETS
|
2004-10-06 11:01:18 +02:00
|
|
|
static char **
|
2004-12-27 21:39:21 +01:00
|
|
|
filter_lines_with_token(char **lines, const char *token)
|
2004-10-06 11:01:18 +02:00
|
|
|
{
|
|
|
|
int numlines = 1;
|
2005-10-15 04:49:52 +02:00
|
|
|
int i,
|
|
|
|
src,
|
|
|
|
dst;
|
2004-10-06 11:01:18 +02:00
|
|
|
char **result;
|
|
|
|
|
|
|
|
for (i = 0; lines[i]; i++)
|
|
|
|
numlines++;
|
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
result = (char **) pg_malloc(numlines * sizeof(char *));
|
2004-10-06 11:01:18 +02:00
|
|
|
|
|
|
|
for (src = 0, dst = 0; src < numlines; src++)
|
|
|
|
{
|
|
|
|
if (lines[src] == NULL || strstr(lines[src], token) == NULL)
|
|
|
|
result[dst++] = lines[src];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* get the lines from a text file
|
|
|
|
*/
|
|
|
|
static char **
|
|
|
|
readfile(char *path)
|
|
|
|
{
|
|
|
|
FILE *infile;
|
|
|
|
int maxlength = 0,
|
|
|
|
linelen = 0;
|
|
|
|
int nlines = 0;
|
|
|
|
char **result;
|
|
|
|
char *buffer;
|
|
|
|
int c;
|
|
|
|
|
2003-11-10 23:52:10 +01:00
|
|
|
if ((infile = fopen(path, "r")) == NULL)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2003-11-10 23:51:16 +01:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass over the file twice - the first time to size the result */
|
|
|
|
|
|
|
|
while ((c = fgetc(infile)) != EOF)
|
|
|
|
{
|
|
|
|
linelen++;
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
nlines++;
|
|
|
|
if (linelen > maxlength)
|
|
|
|
maxlength = linelen;
|
|
|
|
linelen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle last line without a terminating newline (yuck) */
|
|
|
|
|
|
|
|
if (linelen)
|
|
|
|
nlines++;
|
|
|
|
if (linelen > maxlength)
|
|
|
|
maxlength = linelen;
|
|
|
|
|
|
|
|
/* set up the result and the line buffer */
|
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
result = (char **) pg_malloc((nlines + 2) * sizeof(char *));
|
|
|
|
buffer = (char *) pg_malloc(maxlength + 2);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
/* now reprocess the file and store the lines */
|
|
|
|
|
|
|
|
rewind(infile);
|
|
|
|
nlines = 0;
|
|
|
|
while (fgets(buffer, maxlength + 1, infile) != NULL)
|
|
|
|
{
|
|
|
|
result[nlines] = xstrdup(buffer);
|
|
|
|
nlines++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(infile);
|
2006-08-20 18:08:09 +02:00
|
|
|
free(buffer);
|
2003-11-10 23:51:16 +01:00
|
|
|
result[nlines] = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write an array of lines to a file
|
2004-10-24 17:55:29 +02:00
|
|
|
*
|
|
|
|
* This is only used to write text files. Use fopen "w" not PG_BINARY_W
|
|
|
|
* so that the resulting configuration files are nicely editable on Windows.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
writefile(char *path, char **lines)
|
|
|
|
{
|
|
|
|
FILE *out_file;
|
|
|
|
char **line;
|
|
|
|
|
2004-10-24 17:55:29 +02:00
|
|
|
if ((out_file = fopen(path, "w")) == NULL)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2003-11-10 23:51:16 +01:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
for (line = lines; *line != NULL; line++)
|
|
|
|
{
|
|
|
|
if (fputs(*line, out_file) < 0)
|
2004-11-29 04:05:03 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2003-11-10 23:51:16 +01:00
|
|
|
exit_nicely();
|
2004-11-29 04:05:03 +01:00
|
|
|
}
|
2003-11-10 23:51:16 +01:00
|
|
|
free(*line);
|
|
|
|
}
|
|
|
|
if (fclose(out_file))
|
2004-11-29 04:05:03 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2003-11-10 23:51:16 +01:00
|
|
|
exit_nicely();
|
2004-11-29 04:05:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a subcommand with suitable error messaging
|
|
|
|
*/
|
|
|
|
static FILE *
|
|
|
|
popen_check(const char *command, const char *mode)
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
FILE *cmdfd;
|
2004-11-29 04:05:03 +01:00
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
errno = 0;
|
|
|
|
cmdfd = popen(command, mode);
|
|
|
|
if (cmdfd == NULL)
|
|
|
|
fprintf(stderr, _("%s: could not execute command \"%s\": %s\n"),
|
|
|
|
progname, command, strerror(errno));
|
|
|
|
return cmdfd;
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* source stolen from FreeBSD /src/bin/mkdir/mkdir.c and adapted */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this tries to build all the elements of a path to a directory a la mkdir -p
|
|
|
|
* we assume the path is in canonical form, i.e. uses / as the separator
|
|
|
|
* we also assume it isn't null.
|
2005-01-28 01:34:32 +01:00
|
|
|
*
|
|
|
|
* note that on failure, the path arg has been modified to show the particular
|
|
|
|
* directory level we had problems with.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static int
|
2003-11-10 23:52:10 +01:00
|
|
|
mkdir_p(char *path, mode_t omode)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
mode_t numask,
|
|
|
|
oumask;
|
|
|
|
int first,
|
|
|
|
last,
|
|
|
|
retval;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = path;
|
|
|
|
oumask = 0;
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/* skip network and drive specifiers for win32 */
|
|
|
|
if (strlen(p) >= 2)
|
|
|
|
{
|
|
|
|
if (p[0] == '/' && p[1] == '/')
|
|
|
|
{
|
|
|
|
/* network drive */
|
|
|
|
p = strstr(p + 2, "/");
|
|
|
|
if (p == NULL)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (p[1] == ':' &&
|
|
|
|
((p[0] >= 'a' && p[0] <= 'z') ||
|
|
|
|
(p[0] >= 'A' && p[0] <= 'Z')))
|
|
|
|
{
|
|
|
|
/* local drive */
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
}
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
if (p[0] == '/') /* Skip leading '/'. */
|
|
|
|
++p;
|
|
|
|
for (first = 1, last = 0; !last; ++p)
|
|
|
|
{
|
|
|
|
if (p[0] == '\0')
|
|
|
|
last = 1;
|
|
|
|
else if (p[0] != '/')
|
|
|
|
continue;
|
|
|
|
*p = '\0';
|
2004-01-31 23:10:00 +01:00
|
|
|
if (!last && p[1] == '\0')
|
2003-11-10 23:51:16 +01:00
|
|
|
last = 1;
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* POSIX 1003.2: For each dir operand that does not name an
|
2005-10-15 04:49:52 +02:00
|
|
|
* existing directory, effects equivalent to those caused by the
|
|
|
|
* following command shall occcur:
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
|
|
|
|
* dir
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* We change the user's umask and then restore it, instead of
|
|
|
|
* doing chmod's.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
oumask = umask(0);
|
|
|
|
numask = oumask & ~(S_IWUSR | S_IXUSR);
|
|
|
|
(void) umask(numask);
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
if (last)
|
|
|
|
(void) umask(oumask);
|
2005-01-28 01:34:32 +01:00
|
|
|
|
|
|
|
/* check for pre-existing directory; ok if it's a parent */
|
|
|
|
if (stat(path, &sb) == 0)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2005-01-28 01:34:32 +01:00
|
|
|
if (!S_ISDIR(sb.st_mode))
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2005-01-28 01:34:32 +01:00
|
|
|
if (last)
|
|
|
|
errno = EEXIST;
|
|
|
|
else
|
|
|
|
errno = ENOTDIR;
|
2003-11-10 23:51:16 +01:00
|
|
|
retval = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-01-28 01:34:32 +01:00
|
|
|
else if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
|
|
|
|
{
|
|
|
|
retval = 1;
|
|
|
|
break;
|
|
|
|
}
|
2003-11-10 23:51:16 +01:00
|
|
|
if (!last)
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
if (!first && !last)
|
|
|
|
(void) umask(oumask);
|
2003-11-10 23:52:10 +01:00
|
|
|
return retval;
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clean up any files we created on failure
|
|
|
|
* if we created the data directory remove it too
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
exit_nicely(void)
|
|
|
|
{
|
|
|
|
if (!noclean)
|
|
|
|
{
|
|
|
|
if (made_new_pgdata)
|
|
|
|
{
|
2003-11-23 22:41:30 +01:00
|
|
|
fprintf(stderr, _("%s: removing data directory \"%s\"\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname, pg_data);
|
|
|
|
if (!rmtree(pg_data, true))
|
2004-11-29 04:05:03 +01:00
|
|
|
fprintf(stderr, _("%s: failed to remove data directory\n"),
|
|
|
|
progname);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
2003-11-14 19:32:34 +01:00
|
|
|
else if (found_existing_pgdata)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2003-11-23 22:41:30 +01:00
|
|
|
_("%s: removing contents of data directory \"%s\"\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname, pg_data);
|
|
|
|
if (!rmtree(pg_data, false))
|
2004-11-29 04:05:03 +01:00
|
|
|
fprintf(stderr, _("%s: failed to remove contents of data directory\n"),
|
|
|
|
progname);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
2007-01-06 20:40:00 +01:00
|
|
|
|
|
|
|
if (made_new_xlogdir)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: removing transaction log directory \"%s\"\n"),
|
|
|
|
progname, xlog_dir);
|
|
|
|
if (!rmtree(xlog_dir, true))
|
|
|
|
fprintf(stderr, _("%s: failed to remove transaction log directory\n"),
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
else if (found_existing_xlogdir)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2007-11-15 22:14:46 +01:00
|
|
|
_("%s: removing contents of transaction log directory \"%s\"\n"),
|
2007-01-06 20:40:00 +01:00
|
|
|
progname, xlog_dir);
|
|
|
|
if (!rmtree(xlog_dir, false))
|
|
|
|
fprintf(stderr, _("%s: failed to remove contents of transaction log directory\n"),
|
|
|
|
progname);
|
|
|
|
}
|
2003-11-14 19:32:34 +01:00
|
|
|
/* otherwise died during startup, do nothing! */
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
2003-11-14 00:46:31 +01:00
|
|
|
else
|
|
|
|
{
|
2003-11-14 19:32:34 +01:00
|
|
|
if (made_new_pgdata || found_existing_pgdata)
|
|
|
|
fprintf(stderr,
|
2005-10-15 04:49:52 +02:00
|
|
|
_("%s: data directory \"%s\" not removed at user's request\n"),
|
2003-11-14 19:32:34 +01:00
|
|
|
progname, pg_data);
|
2007-01-06 20:40:00 +01:00
|
|
|
|
|
|
|
if (made_new_xlogdir || found_existing_xlogdir)
|
|
|
|
fprintf(stderr,
|
2007-11-15 22:14:46 +01:00
|
|
|
_("%s: transaction log directory \"%s\" not removed at user's request\n"),
|
2007-01-06 20:40:00 +01:00
|
|
|
progname, xlog_dir);
|
2003-11-14 00:46:31 +01:00
|
|
|
}
|
2003-11-14 19:32:34 +01:00
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-17 19:44:09 +01:00
|
|
|
* find the current user
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* on unix make sure it isn't really root
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_id(void)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
|
|
|
|
struct passwd *pw;
|
|
|
|
|
2005-01-08 23:51:15 +01:00
|
|
|
pw = getpwuid(geteuid());
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2005-01-08 23:51:15 +01:00
|
|
|
if (geteuid() == 0) /* 0 is root's uid */
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2003-11-23 22:41:30 +01:00
|
|
|
_("%s: cannot be run as root\n"
|
2004-08-29 07:07:03 +02:00
|
|
|
"Please log in (using, e.g., \"su\") as the "
|
|
|
|
"(unprivileged) user that will\n"
|
|
|
|
"own the server process.\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#else /* the windows code */
|
|
|
|
|
|
|
|
struct passwd_win32
|
|
|
|
{
|
|
|
|
int pw_uid;
|
|
|
|
char pw_name[128];
|
|
|
|
} pass_win32;
|
|
|
|
struct passwd_win32 *pw = &pass_win32;
|
|
|
|
DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
|
|
|
|
|
|
|
|
pw->pw_uid = 1;
|
|
|
|
GetUserName(pw->pw_name, &pwname_size);
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
return xstrdup(pw->pw_name);
|
|
|
|
}
|
|
|
|
|
2004-07-14 19:55:10 +02:00
|
|
|
static char *
|
|
|
|
encodingid_to_string(int enc)
|
|
|
|
{
|
|
|
|
char result[20];
|
|
|
|
|
|
|
|
sprintf(result, "%d", enc);
|
|
|
|
return xstrdup(result);
|
|
|
|
}
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* get the encoding id for a given encoding name
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_encoding_id(char *encoding_name)
|
|
|
|
{
|
|
|
|
int enc;
|
|
|
|
|
|
|
|
if (encoding_name && *encoding_name)
|
|
|
|
{
|
2007-03-30 00:46:42 +02:00
|
|
|
if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
|
2004-07-14 19:55:10 +02:00
|
|
|
return encodingid_to_string(enc);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
2003-11-23 22:41:30 +01:00
|
|
|
fprintf(stderr, _("%s: \"%s\" is not a valid server encoding name\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname, encoding_name ? encoding_name : "(null)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
/*
|
|
|
|
* Support for determining the best default text search configuration.
|
2007-10-25 22:22:53 +02:00
|
|
|
* We key this off the first part of LC_CTYPE (ie, the language name).
|
2007-08-21 03:11:32 +02:00
|
|
|
*/
|
|
|
|
struct tsearch_config_match
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
const char *tsconfname;
|
|
|
|
const char *langname;
|
2007-08-21 03:11:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct tsearch_config_match tsearch_config_languages[] =
|
|
|
|
{
|
2007-10-25 22:22:53 +02:00
|
|
|
{"danish", "da"},
|
|
|
|
{"danish", "Danish"},
|
|
|
|
{"dutch", "nl"},
|
|
|
|
{"dutch", "Dutch"},
|
2007-08-21 03:11:32 +02:00
|
|
|
{"english", "C"},
|
|
|
|
{"english", "POSIX"},
|
2007-10-25 22:22:53 +02:00
|
|
|
{"english", "en"},
|
|
|
|
{"english", "English"},
|
|
|
|
{"finnish", "fi"},
|
|
|
|
{"finnish", "Finnish"},
|
|
|
|
{"french", "fr"},
|
|
|
|
{"french", "French"},
|
|
|
|
{"german", "de"},
|
|
|
|
{"german", "German"},
|
|
|
|
{"hungarian", "hu"},
|
|
|
|
{"hungarian", "Hungarian"},
|
|
|
|
{"italian", "it"},
|
|
|
|
{"italian", "Italian"},
|
|
|
|
{"norwegian", "no"},
|
|
|
|
{"norwegian", "Norwegian"},
|
|
|
|
{"portuguese", "pt"},
|
|
|
|
{"portuguese", "Portuguese"},
|
|
|
|
{"romanian", "ro"},
|
|
|
|
{"russian", "ru"},
|
|
|
|
{"russian", "Russian"},
|
|
|
|
{"spanish", "es"},
|
|
|
|
{"spanish", "Spanish"},
|
|
|
|
{"swedish", "sv"},
|
|
|
|
{"swedish", "Swedish"},
|
|
|
|
{"turkish", "tr"},
|
|
|
|
{"turkish", "Turkish"},
|
2007-08-21 03:11:32 +02:00
|
|
|
{NULL, NULL} /* end marker */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a text search configuration matching lc_ctype, and return its
|
|
|
|
* name; return NULL if no match.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
find_matching_ts_config(const char *lc_type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *langname,
|
|
|
|
*ptr;
|
|
|
|
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Convert lc_ctype to a language name by stripping everything after an
|
|
|
|
* underscore. Just for paranoia, we also stop at '.' or '@'.
|
2007-08-21 03:11:32 +02:00
|
|
|
*/
|
|
|
|
if (lc_type == NULL)
|
|
|
|
langname = xstrdup("");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = langname = xstrdup(lc_type);
|
2007-10-25 22:22:53 +02:00
|
|
|
while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@')
|
2007-08-21 03:11:32 +02:00
|
|
|
ptr++;
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; tsearch_config_languages[i].tsconfname; i++)
|
|
|
|
{
|
|
|
|
if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
|
|
|
|
{
|
|
|
|
free(langname);
|
|
|
|
return tsearch_config_languages[i].tsconfname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(langname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* get short version of VERSION
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_short_version(void)
|
|
|
|
{
|
|
|
|
bool gotdot = false;
|
|
|
|
int end;
|
|
|
|
char *vr;
|
|
|
|
|
|
|
|
vr = xstrdup(PG_VERSION);
|
|
|
|
|
|
|
|
for (end = 0; vr[end] != '\0'; end++)
|
|
|
|
{
|
|
|
|
if (vr[end] == '.')
|
|
|
|
{
|
|
|
|
if (end == 0)
|
|
|
|
return NULL;
|
|
|
|
else if (gotdot)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
gotdot = true;
|
|
|
|
}
|
|
|
|
else if (vr[end] < '0' || vr[end] > '9')
|
|
|
|
{
|
|
|
|
/* gone past digits and dots */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (end == 0 || vr[end - 1] == '.' || !gotdot)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vr[end] = '\0';
|
|
|
|
return vr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-01-06 20:40:00 +01:00
|
|
|
* make sure the directory either doesn't exist or is empty
|
2003-11-14 18:19:35 +01:00
|
|
|
*
|
|
|
|
* Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
|
|
|
|
* or -1 if trouble accessing directory
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
2003-11-14 18:19:35 +01:00
|
|
|
static int
|
2007-01-06 20:40:00 +01:00
|
|
|
check_data_dir(char *dir)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
DIR *chkdir;
|
|
|
|
struct dirent *file;
|
2003-11-14 18:19:35 +01:00
|
|
|
int result = 1;
|
|
|
|
|
|
|
|
errno = 0;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2007-01-06 20:40:00 +01:00
|
|
|
chkdir = opendir(dir);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
if (!chkdir)
|
2003-11-14 18:19:35 +01:00
|
|
|
return (errno == ENOENT) ? 0 : -1;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
while ((file = readdir(chkdir)) != NULL)
|
|
|
|
{
|
2005-08-02 17:16:27 +02:00
|
|
|
if (strcmp(".", file->d_name) == 0 ||
|
|
|
|
strcmp("..", file->d_name) == 0)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
/* skip this and parent directory */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-11-14 18:19:35 +01:00
|
|
|
result = 2; /* not empty */
|
2003-11-10 23:51:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-02 17:16:27 +02:00
|
|
|
#ifdef WIN32
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-08-02 17:16:27 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
|
|
|
|
* released version
|
2005-08-02 17:16:27 +02:00
|
|
|
*/
|
|
|
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
|
|
|
errno = 0;
|
|
|
|
#endif
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
closedir(chkdir);
|
|
|
|
|
2003-11-14 18:19:35 +01:00
|
|
|
if (errno != 0)
|
|
|
|
result = -1; /* some kind of I/O error? */
|
|
|
|
|
|
|
|
return result;
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make the data directory (or one of its subdirectories if subdir is not NULL)
|
|
|
|
*/
|
|
|
|
static bool
|
2004-07-19 04:47:16 +02:00
|
|
|
mkdatadir(const char *subdir)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
path = pg_malloc(strlen(pg_data) + 2 +
|
2005-10-15 04:49:52 +02:00
|
|
|
(subdir == NULL ? 0 : strlen(subdir)));
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
if (subdir != NULL)
|
|
|
|
sprintf(path, "%s/%s", pg_data, subdir);
|
|
|
|
else
|
|
|
|
strcpy(path, pg_data);
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
if (mkdir_p(path, 0700) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
|
|
|
|
|
|
|
return false;
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set name of given input file variable under data directory
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_input(char **dest, char *filename)
|
|
|
|
{
|
2005-07-10 18:13:13 +02:00
|
|
|
*dest = pg_malloc(strlen(share_path) + strlen(filename) + 2);
|
2004-05-17 16:35:34 +02:00
|
|
|
sprintf(*dest, "%s/%s", share_path, filename);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check that given input file exists
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
check_input(char *path)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
|
2007-01-31 19:52:49 +01:00
|
|
|
if (stat(path, &statbuf) != 0)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
2007-11-16 22:47:32 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: file \"%s\" does not exist\n"), progname, path);
|
2007-01-31 19:52:49 +01:00
|
|
|
fprintf(stderr,
|
2007-11-16 22:47:32 +01:00
|
|
|
_("This might mean you have a corrupted installation or identified\n"
|
|
|
|
"the wrong directory with the invocation option -L.\n"));
|
|
|
|
}
|
2007-01-31 19:52:49 +01:00
|
|
|
else
|
2007-11-16 22:47:32 +01:00
|
|
|
{
|
2007-01-31 19:52:49 +01:00
|
|
|
fprintf(stderr,
|
2007-11-16 22:47:32 +01:00
|
|
|
_("%s: could not access file \"%s\": %s\n"), progname, path,
|
|
|
|
strerror(errno));
|
|
|
|
fprintf(stderr,
|
|
|
|
_("This might mean you have a corrupted installation or identified\n"
|
|
|
|
"the wrong directory with the invocation option -L.\n"));
|
|
|
|
}
|
2007-01-31 19:52:49 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!S_ISREG(statbuf.st_mode))
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2007-11-16 22:47:32 +01:00
|
|
|
_("%s: file \"%s\" is not a regular file\n"), progname, path);
|
|
|
|
fprintf(stderr,
|
|
|
|
_("This might mean you have a corrupted installation or identified\n"
|
|
|
|
"the wrong directory with the invocation option -L.\n"));
|
2003-11-10 23:51:16 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write out the PG_VERSION file in the data dir, or its subdirectory
|
|
|
|
* if extrapath is not NULL
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_short_version(char *short_version, char *extrapath)
|
|
|
|
{
|
|
|
|
FILE *version_file;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
if (extrapath == NULL)
|
|
|
|
{
|
2005-07-10 18:13:13 +02:00
|
|
|
path = pg_malloc(strlen(pg_data) + 12);
|
2003-11-10 23:51:16 +01:00
|
|
|
sprintf(path, "%s/PG_VERSION", pg_data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-07-10 18:13:13 +02:00
|
|
|
path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13);
|
2003-11-10 23:51:16 +01:00
|
|
|
sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath);
|
|
|
|
}
|
|
|
|
version_file = fopen(path, PG_BINARY_W);
|
2004-01-26 23:35:32 +01:00
|
|
|
if (version_file == NULL)
|
2004-11-29 04:05:03 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2004-01-26 23:35:32 +01:00
|
|
|
exit_nicely();
|
2004-11-29 04:05:03 +01:00
|
|
|
}
|
|
|
|
if (fprintf(version_file, "%s\n", short_version) < 0 ||
|
|
|
|
fclose(version_file))
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2004-01-26 23:35:32 +01:00
|
|
|
exit_nicely();
|
2004-11-29 04:05:03 +01:00
|
|
|
}
|
2006-08-20 18:08:09 +02:00
|
|
|
free(path);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-01-01 00:50:59 +01:00
|
|
|
* set up an empty config file so we can check config settings by launching
|
|
|
|
* a test backend
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_null_conf(void)
|
|
|
|
{
|
|
|
|
FILE *conf_file;
|
|
|
|
char *path;
|
|
|
|
|
2005-07-10 18:13:13 +02:00
|
|
|
path = pg_malloc(strlen(pg_data) + 17);
|
2003-11-10 23:51:16 +01:00
|
|
|
sprintf(path, "%s/postgresql.conf", pg_data);
|
|
|
|
conf_file = fopen(path, PG_BINARY_W);
|
2004-11-29 04:05:03 +01:00
|
|
|
if (conf_file == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
if (fclose(conf_file))
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
|
|
|
|
progname, path, strerror(errno));
|
2004-01-26 23:35:32 +01:00
|
|
|
exit_nicely();
|
2004-11-29 04:05:03 +01:00
|
|
|
}
|
2006-08-20 18:08:09 +02:00
|
|
|
free(path);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
2005-12-28 00:54:01 +01:00
|
|
|
/*
|
2006-01-01 00:50:59 +01:00
|
|
|
* Determine platform-specific config settings
|
|
|
|
*
|
|
|
|
* Use reasonable values if kernel will let us, else scale back. Probe
|
|
|
|
* for max_connections first since it is subject to more constraints than
|
|
|
|
* shared_buffers.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
2006-01-01 00:50:59 +01:00
|
|
|
test_config_settings(void)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2006-01-01 00:50:59 +01:00
|
|
|
/*
|
2008-09-30 12:52:14 +02:00
|
|
|
* This macro defines the minimum shared_buffers we want for a given
|
|
|
|
* max_connections value. The arrays show the settings to try.
|
2006-01-01 00:50:59 +01:00
|
|
|
*/
|
2006-10-04 20:58:08 +02:00
|
|
|
#define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
|
2006-01-01 00:50:59 +01:00
|
|
|
|
|
|
|
static const int trial_conns[] = {
|
|
|
|
100, 50, 40, 30, 20, 10
|
|
|
|
};
|
|
|
|
static const int trial_bufs[] = {
|
2006-10-04 20:58:08 +02:00
|
|
|
4096, 3584, 3072, 2560, 2048, 1536,
|
|
|
|
1000, 900, 800, 700, 600, 500,
|
|
|
|
400, 300, 200, 100, 50
|
2006-01-01 00:50:59 +01:00
|
|
|
};
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
char cmd[MAXPGPATH];
|
2006-10-04 02:30:14 +02:00
|
|
|
const int connslen = sizeof(trial_conns) / sizeof(int);
|
|
|
|
const int bufslen = sizeof(trial_bufs) / sizeof(int);
|
2003-11-10 23:51:16 +01:00
|
|
|
int i,
|
2006-01-01 00:50:59 +01:00
|
|
|
status,
|
|
|
|
test_conns,
|
|
|
|
test_buffs,
|
2006-10-04 02:30:14 +02:00
|
|
|
ok_buffers = 0;
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("selecting default max_connections ... "));
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
|
|
|
|
2006-01-01 00:50:59 +01:00
|
|
|
for (i = 0; i < connslen; i++)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2006-01-01 00:50:59 +01:00
|
|
|
test_conns = trial_conns[i];
|
|
|
|
test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
|
2005-12-28 00:54:01 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2008-06-26 03:35:45 +02:00
|
|
|
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
|
2006-01-01 00:50:59 +01:00
|
|
|
"-c max_connections=%d "
|
2006-10-04 20:58:08 +02:00
|
|
|
"-c shared_buffers=%d "
|
2008-06-26 03:35:45 +02:00
|
|
|
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
|
|
|
|
backend_exec, boot_options,
|
2008-09-30 12:52:14 +02:00
|
|
|
test_conns, test_buffs,
|
2008-06-26 03:35:45 +02:00
|
|
|
DEVNULL, DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
status = system(cmd);
|
|
|
|
if (status == 0)
|
2006-01-02 17:45:12 +01:00
|
|
|
{
|
|
|
|
ok_buffers = test_buffs;
|
2003-11-10 23:51:16 +01:00
|
|
|
break;
|
2006-01-02 17:45:12 +01:00
|
|
|
}
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
2006-01-01 00:50:59 +01:00
|
|
|
if (i >= connslen)
|
|
|
|
i = connslen - 1;
|
|
|
|
n_connections = trial_conns[i];
|
2003-11-13 02:36:00 +01:00
|
|
|
|
|
|
|
printf("%d\n", n_connections);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2008-09-30 12:52:14 +02:00
|
|
|
printf(_("selecting default shared_buffers ... "));
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
|
|
|
|
2006-01-01 00:50:59 +01:00
|
|
|
for (i = 0; i < bufslen; i++)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2007-02-21 00:49:38 +01:00
|
|
|
/* Use same amount of memory, independent of BLCKSZ */
|
|
|
|
test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
|
2006-01-02 17:45:12 +01:00
|
|
|
if (test_buffs <= ok_buffers)
|
|
|
|
{
|
|
|
|
test_buffs = ok_buffers;
|
|
|
|
break;
|
|
|
|
}
|
2005-12-28 00:54:01 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2008-06-26 03:35:45 +02:00
|
|
|
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
|
2006-01-01 00:50:59 +01:00
|
|
|
"-c max_connections=%d "
|
2006-10-04 20:58:08 +02:00
|
|
|
"-c shared_buffers=%d "
|
2008-06-26 03:35:45 +02:00
|
|
|
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
|
|
|
|
backend_exec, boot_options,
|
2008-09-30 12:52:14 +02:00
|
|
|
n_connections, test_buffs,
|
2008-06-26 03:35:45 +02:00
|
|
|
DEVNULL, DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
status = system(cmd);
|
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
}
|
2006-01-02 17:45:12 +01:00
|
|
|
n_buffers = test_buffs;
|
2003-11-13 02:36:00 +01:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
|
2008-09-30 12:52:14 +02:00
|
|
|
printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
|
2006-10-03 23:11:55 +02:00
|
|
|
else
|
2008-09-30 12:52:14 +02:00
|
|
|
printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up all the config files
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_config(void)
|
|
|
|
{
|
|
|
|
char **conflines;
|
|
|
|
char repltok[100];
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("creating configuration files ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
/* postgresql.conf */
|
|
|
|
|
|
|
|
conflines = readfile(conf_file);
|
|
|
|
|
|
|
|
snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
|
|
|
|
conflines = replace_token(conflines, "#max_connections = 100", repltok);
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
|
2006-10-04 20:58:08 +02:00
|
|
|
snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
|
2007-11-15 22:14:46 +01:00
|
|
|
(n_buffers * (BLCKSZ / 1024)) / 1024);
|
2006-10-03 23:11:55 +02:00
|
|
|
else
|
2006-10-04 20:58:08 +02:00
|
|
|
snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
|
2007-11-15 22:14:46 +01:00
|
|
|
n_buffers * (BLCKSZ / 1024));
|
2006-10-03 23:11:55 +02:00
|
|
|
conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
|
2003-11-10 23:52:10 +01:00
|
|
|
|
2004-12-27 21:39:21 +01:00
|
|
|
#if DEF_PGPORT != 5432
|
|
|
|
snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
|
|
|
|
conflines = replace_token(conflines, "#port = 5432", repltok);
|
|
|
|
#endif
|
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
|
|
|
|
escape_quotes(lc_messages));
|
2003-11-10 23:51:16 +01:00
|
|
|
conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
|
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
|
|
|
|
escape_quotes(lc_monetary));
|
2003-11-10 23:51:16 +01:00
|
|
|
conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
|
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
|
|
|
|
escape_quotes(lc_numeric));
|
2003-11-10 23:51:16 +01:00
|
|
|
conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
|
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
|
|
|
|
escape_quotes(lc_time));
|
2003-11-10 23:51:16 +01:00
|
|
|
conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
switch (locale_date_order(lc_time))
|
|
|
|
{
|
2005-12-09 16:51:14 +01:00
|
|
|
case DATEORDER_YMD:
|
|
|
|
strcpy(repltok, "datestyle = 'iso, ymd'");
|
|
|
|
break;
|
|
|
|
case DATEORDER_DMY:
|
|
|
|
strcpy(repltok, "datestyle = 'iso, dmy'");
|
|
|
|
break;
|
|
|
|
case DATEORDER_MDY:
|
|
|
|
default:
|
|
|
|
strcpy(repltok, "datestyle = 'iso, mdy'");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
snprintf(repltok, sizeof(repltok),
|
|
|
|
"default_text_search_config = 'pg_catalog.%s'",
|
|
|
|
escape_quotes(default_text_search_config));
|
|
|
|
conflines = replace_token(conflines,
|
2007-11-15 22:14:46 +01:00
|
|
|
"#default_text_search_config = 'pg_catalog.simple'",
|
2007-08-21 03:11:32 +02:00
|
|
|
repltok);
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
writefile(path, conflines);
|
|
|
|
chmod(path, 0600);
|
|
|
|
|
|
|
|
free(conflines);
|
|
|
|
|
|
|
|
|
|
|
|
/* pg_hba.conf */
|
|
|
|
|
|
|
|
conflines = readfile(hba_file);
|
|
|
|
|
2004-10-06 11:13:10 +02:00
|
|
|
#ifndef HAVE_UNIX_SOCKETS
|
2005-10-15 04:49:52 +02:00
|
|
|
conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
|
2004-10-06 11:01:18 +02:00
|
|
|
#else
|
2005-10-15 04:49:52 +02:00
|
|
|
conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
|
2004-10-06 11:01:18 +02:00
|
|
|
#endif
|
|
|
|
|
2005-08-25 04:22:59 +02:00
|
|
|
#ifdef HAVE_IPV6
|
2005-10-15 04:49:52 +02:00
|
|
|
|
|
|
|
/*
|
2005-08-22 18:27:36 +02:00
|
|
|
* Probe to see if there is really any platform support for IPv6, and
|
|
|
|
* comment out the relevant pg_hba line if not. This avoids runtime
|
|
|
|
* warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
|
2005-10-15 04:49:52 +02:00
|
|
|
* useful on Windows, where executables built on a machine with IPv6 may
|
|
|
|
* have to run on a machine without.
|
2005-08-22 18:27:36 +02:00
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct addrinfo *gai_result;
|
|
|
|
struct addrinfo hints;
|
2005-10-15 04:49:52 +02:00
|
|
|
int err = 0;
|
|
|
|
|
2005-08-27 20:44:03 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
/* need to call WSAStartup before calling getaddrinfo */
|
2005-10-15 04:49:52 +02:00
|
|
|
WSADATA wsaData;
|
2005-08-27 20:44:03 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
err = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
2005-08-27 20:44:03 +02:00
|
|
|
#endif
|
2005-08-22 18:27:36 +02:00
|
|
|
|
|
|
|
/* for best results, this code should match parse_hba() */
|
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = 0;
|
|
|
|
hints.ai_protocol = 0;
|
|
|
|
hints.ai_addrlen = 0;
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
2005-08-27 20:44:03 +02:00
|
|
|
if (err != 0 ||
|
|
|
|
getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
|
2005-08-22 18:27:36 +02:00
|
|
|
conflines = replace_token(conflines,
|
|
|
|
"host all all ::1",
|
|
|
|
"#host all all ::1");
|
|
|
|
}
|
2005-10-15 04:49:52 +02:00
|
|
|
#else /* !HAVE_IPV6 */
|
2005-08-22 18:27:36 +02:00
|
|
|
/* If we didn't compile IPV6 support at all, always comment it out */
|
2003-11-10 23:51:16 +01:00
|
|
|
conflines = replace_token(conflines,
|
|
|
|
"host all all ::1",
|
|
|
|
"#host all all ::1");
|
2005-10-15 04:49:52 +02:00
|
|
|
#endif /* HAVE_IPV6 */
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-01 07:59:13 +02:00
|
|
|
/* Replace default authentication methods */
|
|
|
|
conflines = replace_token(conflines,
|
2004-08-29 07:07:03 +02:00
|
|
|
"@authmethod@",
|
2004-08-01 07:59:13 +02:00
|
|
|
authmethod);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-01 07:59:13 +02:00
|
|
|
conflines = replace_token(conflines,
|
|
|
|
"@authcomment@",
|
2005-10-15 04:49:52 +02:00
|
|
|
strcmp(authmethod, "trust") ? "" : AUTHTRUST_WARNING);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
writefile(path, conflines);
|
|
|
|
chmod(path, 0600);
|
|
|
|
|
|
|
|
free(conflines);
|
|
|
|
|
|
|
|
/* pg_ident.conf */
|
|
|
|
|
|
|
|
conflines = readfile(ident_file);
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
writefile(path, conflines);
|
|
|
|
chmod(path, 0600);
|
|
|
|
|
|
|
|
free(conflines);
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-11-14 00:46:31 +01:00
|
|
|
* run the BKI script in bootstrap mode to create template1
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bootstrap_template1(char *short_version)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
|
|
|
char **line;
|
2003-11-10 23:51:16 +01:00
|
|
|
char *talkargs = "";
|
|
|
|
char **bki_lines;
|
|
|
|
char headerline[MAXPGPATH];
|
2008-07-19 06:01:29 +02:00
|
|
|
char buf[64];
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("creating template1 database in %s/base/1 ... "), pg_data);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
talkargs = "-d 5";
|
|
|
|
|
|
|
|
bki_lines = readfile(bki_file);
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Check that bki file appears to be of the right version */
|
|
|
|
|
|
|
|
snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
|
|
|
|
short_version);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
if (strcmp(headerline, *bki_lines) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2005-10-15 04:49:52 +02:00
|
|
|
_("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
|
|
|
|
"Check your installation or specify the correct path "
|
|
|
|
"using the option -L.\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname, bki_file, PG_VERSION);
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
|
2008-07-19 06:01:29 +02:00
|
|
|
/* Substitute for various symbols used in the BKI file */
|
|
|
|
|
|
|
|
sprintf(buf, "%d", NAMEDATALEN);
|
|
|
|
bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
|
|
|
|
|
2008-11-14 03:09:52 +01:00
|
|
|
sprintf(buf, "%d", (int) sizeof(Pointer));
|
|
|
|
bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
|
|
|
|
|
|
|
|
bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
|
|
|
|
(sizeof(Pointer) == 4) ? "i" : "d");
|
|
|
|
|
2008-07-19 06:01:29 +02:00
|
|
|
bki_lines = replace_token(bki_lines, "FLOAT4PASSBYVAL",
|
|
|
|
FLOAT4PASSBYVAL ? "true" : "false");
|
|
|
|
|
|
|
|
bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
|
|
|
|
FLOAT8PASSBYVAL ? "true" : "false");
|
|
|
|
|
2006-02-24 01:55:49 +01:00
|
|
|
bki_lines = replace_token(bki_lines, "POSTGRES", username);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
bki_lines = replace_token(bki_lines, "ENCODING", encodingid);
|
|
|
|
|
2008-09-23 11:20:39 +02:00
|
|
|
bki_lines = replace_token(bki_lines, "LC_COLLATE", lc_collate);
|
|
|
|
|
|
|
|
bki_lines = replace_token(bki_lines, "LC_CTYPE", lc_ctype);
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
2003-11-14 00:46:31 +01:00
|
|
|
* Pass correct LC_xxx environment to bootstrap.
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* The shell script arranged to restore the LC settings afterwards, but
|
|
|
|
* there doesn't seem to be any compelling reason to do that.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate);
|
2003-11-10 23:51:16 +01:00
|
|
|
putenv(xstrdup(cmd));
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
|
2003-11-10 23:51:16 +01:00
|
|
|
putenv(xstrdup(cmd));
|
|
|
|
|
2004-05-05 23:18:29 +02:00
|
|
|
unsetenv("LC_ALL");
|
2004-05-05 18:09:31 +02:00
|
|
|
|
|
|
|
/* Also ensure backend isn't confused by this environment var: */
|
2004-05-05 23:18:29 +02:00
|
|
|
unsetenv("PGCLIENTENCODING");
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2007-02-16 03:10:07 +01:00
|
|
|
"\"%s\" --boot -x1 %s %s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, boot_options, talkargs);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
for (line = bki_lines; *line != NULL; line++)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
free(*line);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
free(bki_lines);
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up the shadow password table
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-28 07:09:14 +02:00
|
|
|
setup_auth(void)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
2007-03-18 17:50:44 +01:00
|
|
|
const char **line;
|
|
|
|
static const char *pg_authid_setup[] = {
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Create triggers to ensure manual updates to shared catalogs will be
|
|
|
|
* reflected into their "flat file" copies.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
2005-02-26 19:43:34 +01:00
|
|
|
"CREATE TRIGGER pg_sync_pg_database "
|
|
|
|
" AFTER INSERT OR UPDATE OR DELETE ON pg_database "
|
|
|
|
" FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
|
2005-06-28 07:09:14 +02:00
|
|
|
"CREATE TRIGGER pg_sync_pg_authid "
|
|
|
|
" AFTER INSERT OR UPDATE OR DELETE ON pg_authid "
|
2005-02-26 19:43:34 +01:00
|
|
|
" FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
|
2005-06-28 07:09:14 +02:00
|
|
|
"CREATE TRIGGER pg_sync_pg_auth_members "
|
|
|
|
" AFTER INSERT OR UPDATE OR DELETE ON pg_auth_members "
|
2005-02-26 19:43:34 +01:00
|
|
|
" FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The authid table shouldn't be readable except through views, to
|
|
|
|
* ensure passwords are not publicly visible.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
2005-06-28 07:09:14 +02:00
|
|
|
"REVOKE ALL on pg_authid FROM public;\n",
|
2003-11-10 23:51:16 +01:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
fputs(_("initializing pg_authid ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
for (line = pg_authid_setup; *line != NULL; line++)
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the superuser password if required, and call postgres to set it
|
|
|
|
*/
|
|
|
|
static void
|
2003-11-10 23:52:10 +01:00
|
|
|
get_set_pwd(void)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-10 23:52:10 +01:00
|
|
|
char *pwd1,
|
|
|
|
*pwd2;
|
|
|
|
char pwdpath[MAXPGPATH];
|
2003-11-10 23:51:16 +01:00
|
|
|
struct stat statbuf;
|
|
|
|
|
2004-06-24 21:26:59 +02:00
|
|
|
if (pwprompt)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2004-06-24 21:26:59 +02:00
|
|
|
/*
|
|
|
|
* Read password from terminal
|
|
|
|
*/
|
|
|
|
pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
|
|
|
|
pwd2 = simple_prompt("Enter it again: ", 100, false);
|
|
|
|
if (strcmp(pwd1, pwd2) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("Passwords didn't match.\n"));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
free(pwd2);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
2004-06-24 21:26:59 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Read password from file
|
|
|
|
*
|
|
|
|
* Ideally this should insist that the file not be world-readable.
|
2005-10-15 04:49:52 +02:00
|
|
|
* However, this option is mainly intended for use on Windows where
|
|
|
|
* file permissions may not exist at all, so we'll skip the paranoia
|
|
|
|
* for now.
|
2004-06-24 21:26:59 +02:00
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
FILE *pwf = fopen(pwfilename, "r");
|
|
|
|
char pwdbuf[MAXPGPATH];
|
|
|
|
int i;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-06-24 21:26:59 +02:00
|
|
|
if (!pwf)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
|
|
|
|
progname, pwfilename, strerror(errno));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
if (!fgets(pwdbuf, sizeof(pwdbuf), pwf))
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
|
|
|
|
progname, pwfilename, strerror(errno));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
fclose(pwf);
|
|
|
|
|
|
|
|
i = strlen(pwdbuf);
|
2004-08-29 07:07:03 +02:00
|
|
|
while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
|
2004-06-24 21:26:59 +02:00
|
|
|
pwdbuf[--i] = '\0';
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-06-24 21:26:59 +02:00
|
|
|
pwd1 = xstrdup(pwdbuf);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-06-24 21:26:59 +02:00
|
|
|
}
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("setting password ... "));
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n",
|
|
|
|
username, escape_quotes(pwd1));
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2006-08-20 18:08:09 +02:00
|
|
|
/* MM: pwd1 is no longer needed, freeing it */
|
|
|
|
free(pwd1);
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
check_ok();
|
|
|
|
|
2005-06-28 17:38:12 +02:00
|
|
|
snprintf(pwdpath, sizeof(pwdpath), "%s/global/pg_auth", pg_data);
|
2003-11-10 23:52:10 +01:00
|
|
|
if (stat(pwdpath, &statbuf) != 0 || !S_ISREG(statbuf.st_mode))
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2003-11-23 22:41:30 +01:00
|
|
|
_("%s: The password file was not generated. "
|
2004-08-29 07:07:03 +02:00
|
|
|
"Please report this problem.\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname);
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up pg_depend
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_depend(void)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
2007-11-15 22:14:46 +01:00
|
|
|
const char **line;
|
2007-03-18 17:50:44 +01:00
|
|
|
static const char *pg_depend_setup[] = {
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make PIN entries in pg_depend for all objects made so far in the
|
|
|
|
* tables that the dependency code handles. This is overkill (the
|
|
|
|
* system doesn't really depend on having every last weird datatype,
|
|
|
|
* for instance) but generating only the minimum required set of
|
|
|
|
* dependencies seems hard.
|
2003-11-14 00:46:31 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note that we deliberately do not pin the system views, which
|
2007-11-15 22:14:46 +01:00
|
|
|
* haven't been created yet. Also, no conversions, databases, or
|
|
|
|
* tablespaces are pinned.
|
2003-11-14 00:46:31 +01:00
|
|
|
*
|
|
|
|
* First delete any already-made entries; PINs override all else, and
|
|
|
|
* must be the only entries for their objects.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
"DELETE FROM pg_depend;\n",
|
2005-07-07 22:40:02 +02:00
|
|
|
"VACUUM pg_depend;\n",
|
|
|
|
"DELETE FROM pg_shdepend;\n",
|
|
|
|
"VACUUM pg_shdepend;\n",
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_class;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_proc;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_type;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_cast;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_constraint;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_attrdef;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_language;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_operator;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_opclass;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
2006-12-23 01:43:13 +01:00
|
|
|
" FROM pg_opfamily;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_amop;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_amproc;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
2003-11-10 23:51:16 +01:00
|
|
|
" FROM pg_rewrite;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_trigger;\n",
|
|
|
|
|
|
|
|
/*
|
|
|
|
* restriction here to avoid pinning the public namespace
|
|
|
|
*/
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_namespace "
|
|
|
|
" WHERE nspname LIKE 'pg%';\n",
|
2005-07-07 22:40:02 +02:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_ts_parser;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_ts_dict;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_ts_template;\n",
|
|
|
|
"INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
|
|
|
|
" FROM pg_ts_config;\n",
|
2005-07-07 22:40:02 +02:00
|
|
|
"INSERT INTO pg_shdepend SELECT 0, 0, 0, tableoid, oid, 'p' "
|
|
|
|
" FROM pg_authid;\n",
|
2003-11-10 23:51:16 +01:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2005-07-07 22:40:02 +02:00
|
|
|
fputs(_("initializing dependencies ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
for (line = pg_depend_setup; *line != NULL; line++)
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up system views
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_sysviews(void)
|
|
|
|
{
|
|
|
|
PG_CMD_DECL;
|
2004-11-29 04:05:03 +01:00
|
|
|
char **line;
|
2003-11-10 23:51:16 +01:00
|
|
|
char **sysviews_setup;
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("creating system views ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
sysviews_setup = readfile(system_views_file);
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/*
|
2006-01-05 11:07:46 +01:00
|
|
|
* We use -j here to avoid backslashing stuff in system_views.sql
|
2003-11-14 00:46:31 +01:00
|
|
|
*/
|
|
|
|
snprintf(cmd, sizeof(cmd),
|
2006-01-05 11:07:46 +01:00
|
|
|
"\"%s\" %s -j template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
for (line = sysviews_setup; *line != NULL; line++)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
free(*line);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
free(sysviews_setup);
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load description data
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_description(void)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2006-03-21 18:54:28 +01:00
|
|
|
fputs(_("loading system objects' descriptions ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_description ( "
|
|
|
|
" objoid oid, "
|
|
|
|
" classname name, "
|
|
|
|
" objsubid int4, "
|
|
|
|
" description text) WITHOUT OIDS;\n");
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
PG_CMD_PRINTF1("COPY tmp_pg_description FROM E'%s';\n",
|
|
|
|
escape_quotes(desc_file));
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS("INSERT INTO pg_description "
|
|
|
|
" SELECT t.objoid, c.oid, t.objsubid, t.description "
|
|
|
|
" FROM tmp_pg_description t, pg_class c "
|
|
|
|
" WHERE c.relname = t.classname;\n");
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2006-02-12 04:22:21 +01:00
|
|
|
PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_shdescription ( "
|
2006-03-21 18:54:28 +01:00
|
|
|
" objoid oid, "
|
|
|
|
" classname name, "
|
|
|
|
" description text) WITHOUT OIDS;\n");
|
2006-02-12 04:22:21 +01:00
|
|
|
|
2006-05-27 20:07:06 +02:00
|
|
|
PG_CMD_PRINTF1("COPY tmp_pg_shdescription FROM E'%s';\n",
|
|
|
|
escape_quotes(shdesc_file));
|
2006-02-12 04:22:21 +01:00
|
|
|
|
|
|
|
PG_CMD_PUTS("INSERT INTO pg_shdescription "
|
2006-03-21 18:54:28 +01:00
|
|
|
" SELECT t.objoid, c.oid, t.description "
|
|
|
|
" FROM tmp_pg_shdescription t, pg_class c "
|
|
|
|
" WHERE c.relname = t.classname;\n");
|
2006-02-12 04:22:21 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* load conversion functions
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_conversion(void)
|
|
|
|
{
|
|
|
|
PG_CMD_DECL;
|
2004-11-29 04:05:03 +01:00
|
|
|
char **line;
|
2003-11-10 23:51:16 +01:00
|
|
|
char **conv_lines;
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("creating conversions ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
conv_lines = readfile(conversion_file);
|
|
|
|
for (line = conv_lines; *line != NULL; line++)
|
|
|
|
{
|
|
|
|
if (strstr(*line, "DROP CONVERSION") != *line)
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
free(*line);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(conv_lines);
|
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
/*
|
|
|
|
* load extra dictionaries (Snowball stemmers)
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_dictionary(void)
|
|
|
|
{
|
|
|
|
PG_CMD_DECL;
|
|
|
|
char **line;
|
|
|
|
char **conv_lines;
|
|
|
|
|
|
|
|
fputs(_("creating dictionaries ... "), stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use -j here to avoid backslashing stuff
|
|
|
|
*/
|
|
|
|
snprintf(cmd, sizeof(cmd),
|
|
|
|
"\"%s\" %s -j template1 >%s",
|
|
|
|
backend_exec, backend_options,
|
|
|
|
DEVNULL);
|
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
conv_lines = readfile(dictionary_file);
|
|
|
|
for (line = conv_lines; *line != NULL; line++)
|
|
|
|
{
|
|
|
|
PG_CMD_PUTS(*line);
|
|
|
|
free(*line);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(conv_lines);
|
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
2003-11-14 00:46:31 +01:00
|
|
|
* Set up privileges
|
2003-11-10 23:51:16 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We mark most system catalogs as world-readable. We don't currently have
|
2005-04-12 21:29:24 +02:00
|
|
|
* to touch functions, languages, or databases, because their default
|
|
|
|
* permissions are OK.
|
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* Some objects may require different permissions by default, so we
|
|
|
|
* make sure we don't overwrite privilege sets that have already been
|
|
|
|
* set (NOT NULL).
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_privileges(void)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
|
|
|
char **line;
|
|
|
|
char **priv_lines;
|
|
|
|
static char *privileges_setup[] = {
|
2003-11-10 23:51:16 +01:00
|
|
|
"UPDATE pg_class "
|
2005-06-26 05:04:37 +02:00
|
|
|
" SET relacl = E'{\"=r/\\\\\"$POSTGRES_SUPERUSERNAME\\\\\"\"}' "
|
2003-11-10 23:51:16 +01:00
|
|
|
" WHERE relkind IN ('r', 'v', 'S') AND relacl IS NULL;\n",
|
|
|
|
"GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n",
|
|
|
|
"GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("setting privileges on built-in objects ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
priv_lines = replace_token(privileges_setup,
|
2006-02-24 01:55:49 +01:00
|
|
|
"$POSTGRES_SUPERUSERNAME", username);
|
2003-11-10 23:51:16 +01:00
|
|
|
for (line = priv_lines; *line != NULL; line++)
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-11-14 00:46:31 +01:00
|
|
|
* extract the strange version of version required for information schema
|
|
|
|
* (09.08.0007abc)
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
set_info_version(void)
|
|
|
|
{
|
|
|
|
char *letterversion;
|
|
|
|
long major = 0,
|
|
|
|
minor = 0,
|
|
|
|
micro = 0;
|
|
|
|
char *endptr;
|
|
|
|
char *vstr = xstrdup(PG_VERSION);
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = vstr + (strlen(vstr) - 1);
|
|
|
|
while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
|
|
|
|
ptr--;
|
|
|
|
letterversion = ptr + 1;
|
|
|
|
major = strtol(vstr, &endptr, 10);
|
|
|
|
if (*endptr)
|
|
|
|
minor = strtol(endptr + 1, &endptr, 10);
|
|
|
|
if (*endptr)
|
|
|
|
micro = strtol(endptr + 1, &endptr, 10);
|
|
|
|
snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
|
|
|
|
major, minor, micro, letterversion);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* load info schema and populate from features file
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_schema(void)
|
|
|
|
{
|
|
|
|
PG_CMD_DECL;
|
2004-11-29 04:05:03 +01:00
|
|
|
char **line;
|
2003-11-10 23:51:16 +01:00
|
|
|
char **lines;
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("creating information schema ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
lines = readfile(info_schema_file);
|
|
|
|
|
|
|
|
/*
|
2006-01-05 11:07:46 +01:00
|
|
|
* We use -j here to avoid backslashing stuff in information_schema.sql
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2006-01-05 11:07:46 +01:00
|
|
|
"\"%s\" %s -j template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
for (line = lines; *line != NULL; line++)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
free(*line);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(lines);
|
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "
|
2003-11-10 23:51:16 +01:00
|
|
|
" SET character_value = '%s' "
|
|
|
|
" WHERE implementation_info_name = 'DBMS VERSION';\n",
|
|
|
|
infoversion);
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PRINTF1("COPY information_schema.sql_features "
|
2003-11-14 00:46:31 +01:00
|
|
|
" (feature_id, feature_name, sub_feature_id, "
|
|
|
|
" sub_feature_name, is_supported, comments) "
|
2006-05-27 20:07:06 +02:00
|
|
|
" FROM E'%s';\n",
|
|
|
|
escape_quotes(features_file));
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clean everything up in template1
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
vacuum_db(void)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("vacuuming database template1 ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS("ANALYZE;\nVACUUM FULL;\nVACUUM FREEZE;\n");
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy template1 to template0
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
make_template0(void)
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_DECL;
|
2007-03-18 17:50:44 +01:00
|
|
|
const char **line;
|
|
|
|
static const char *template0_setup[] = {
|
2003-11-10 23:51:16 +01:00
|
|
|
"CREATE DATABASE template0;\n",
|
|
|
|
"UPDATE pg_database SET "
|
|
|
|
" datistemplate = 't', "
|
|
|
|
" datallowconn = 'f' "
|
|
|
|
" WHERE datname = 'template0';\n",
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the OID of template0 to determine lastsysoid
|
|
|
|
*/
|
|
|
|
"UPDATE pg_database SET datlastsysoid = "
|
2005-06-21 06:02:34 +02:00
|
|
|
" (SELECT oid FROM pg_database "
|
2003-11-10 23:51:16 +01:00
|
|
|
" WHERE datname = 'template0');\n",
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Explicitly revoke public create-schema and create-temp-table
|
2005-10-15 04:49:52 +02:00
|
|
|
* privileges in template1 and template0; else the latter would be on
|
|
|
|
* by default
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
"REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n",
|
|
|
|
"REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n",
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally vacuum to clean up dead rows in pg_database
|
|
|
|
*/
|
|
|
|
"VACUUM FULL pg_database;\n",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
fputs(_("copying template1 to template0 ... "), stdout);
|
2003-11-13 02:36:00 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
snprintf(cmd, sizeof(cmd),
|
2004-05-11 23:57:15 +02:00
|
|
|
"\"%s\" %s template1 >%s",
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec, backend_options,
|
2003-11-14 00:46:31 +01:00
|
|
|
DEVNULL);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
for (line = template0_setup; *line; line++)
|
2004-11-29 04:05:03 +01:00
|
|
|
PG_CMD_PUTS(*line);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
2005-06-21 06:02:34 +02:00
|
|
|
/*
|
|
|
|
* copy template1 to postgres
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
make_postgres(void)
|
|
|
|
{
|
|
|
|
PG_CMD_DECL;
|
2007-03-18 17:50:44 +01:00
|
|
|
const char **line;
|
|
|
|
static const char *postgres_setup[] = {
|
2005-06-21 06:02:34 +02:00
|
|
|
"CREATE DATABASE postgres;\n",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
fputs(_("copying template1 to postgres ... "), stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
snprintf(cmd, sizeof(cmd),
|
|
|
|
"\"%s\" %s template1 >%s",
|
|
|
|
backend_exec, backend_options,
|
|
|
|
DEVNULL);
|
|
|
|
|
|
|
|
PG_CMD_OPEN;
|
|
|
|
|
|
|
|
for (line = postgres_setup; *line; line++)
|
|
|
|
PG_CMD_PUTS(*line);
|
|
|
|
|
|
|
|
PG_CMD_CLOSE;
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* signal handler in case we are interrupted.
|
|
|
|
*
|
|
|
|
* The Windows runtime docs at
|
|
|
|
* http://msdn.microsoft.com/library/en-us/vclib/html/_crt_signal.asp
|
|
|
|
* specifically forbid a number of things being done from a signal handler,
|
|
|
|
* including IO, memory allocation and system calls, and only allow jmpbuf
|
|
|
|
* if you are handling SIGFPE.
|
|
|
|
*
|
|
|
|
* I avoided doing the forbidden things by setting a flag instead of calling
|
|
|
|
* exit_nicely() directly.
|
|
|
|
*
|
|
|
|
* Also note the behaviour of Windows with SIGINT, which says this:
|
|
|
|
* Note SIGINT is not supported for any Win32 application, including
|
|
|
|
* Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs,
|
|
|
|
* Win32 operating systems generate a new thread to specifically handle
|
|
|
|
* that interrupt. This can cause a single-thread application such as UNIX,
|
|
|
|
* to become multithreaded, resulting in unexpected behavior.
|
|
|
|
*
|
|
|
|
* I have no idea how to handle this. (Strange they call UNIX an application!)
|
|
|
|
* So this will need some testing on Windows.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
trapsig(int signum)
|
|
|
|
{
|
|
|
|
/* handle systems that reset the handler, like Windows (grr) */
|
|
|
|
pqsignal(signum, trapsig);
|
2003-11-14 19:32:34 +01:00
|
|
|
caught_signal = true;
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call exit_nicely() if we got a signal, or else output "ok".
|
|
|
|
*/
|
|
|
|
static void
|
2004-10-11 01:37:45 +02:00
|
|
|
check_ok(void)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2003-11-14 19:32:34 +01:00
|
|
|
if (caught_signal)
|
|
|
|
{
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("caught signal\n"));
|
2004-11-29 04:05:03 +01:00
|
|
|
fflush(stdout);
|
2003-11-14 19:32:34 +01:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
else if (output_failed)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
printf(_("could not write to child process: %s\n"),
|
|
|
|
strerror(output_errno));
|
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-11-14 19:32:34 +01:00
|
|
|
/* all seems well */
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("ok\n"));
|
2004-11-29 04:05:03 +01:00
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-07 18:53:25 +02:00
|
|
|
/*
|
2006-05-27 20:07:06 +02:00
|
|
|
* Escape (by doubling) any single quotes or backslashes in given string
|
|
|
|
*
|
|
|
|
* Note: this is used to process both postgresql.conf entries and SQL
|
|
|
|
* string literals. Since postgresql.conf strings are defined to treat
|
2006-10-04 02:30:14 +02:00
|
|
|
* backslashes as escapes, we have to double backslashes here. Hence,
|
2006-05-27 20:07:06 +02:00
|
|
|
* when using this for a SQL string literal, use E'' syntax.
|
|
|
|
*
|
|
|
|
* We do not need to worry about encoding considerations because all
|
|
|
|
* valid backend encodings are ASCII-safe.
|
2004-10-07 18:53:25 +02:00
|
|
|
*/
|
2004-10-07 20:57:26 +02:00
|
|
|
static char *
|
|
|
|
escape_quotes(const char *src)
|
2004-10-07 18:53:25 +02:00
|
|
|
{
|
2004-10-07 20:57:26 +02:00
|
|
|
int len = strlen(src),
|
2005-10-15 04:49:52 +02:00
|
|
|
i,
|
|
|
|
j;
|
|
|
|
char *result = pg_malloc(len * 2 + 1);
|
|
|
|
|
2004-10-07 18:53:25 +02:00
|
|
|
for (i = 0, j = 0; i < len; i++)
|
|
|
|
{
|
2006-05-27 01:48:54 +02:00
|
|
|
if (SQL_STR_DOUBLE(src[i], true))
|
2005-07-02 19:01:59 +02:00
|
|
|
result[j++] = src[i];
|
2004-10-07 20:57:26 +02:00
|
|
|
result[j++] = src[i];
|
2004-10-07 18:53:25 +02:00
|
|
|
}
|
2004-10-07 20:57:26 +02:00
|
|
|
result[j] = '\0';
|
|
|
|
return result;
|
2004-10-07 18:53:25 +02:00
|
|
|
}
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2007-07-12 01:15:38 +02:00
|
|
|
/* Hack to suppress a warning about %x from some versions of gcc */
|
|
|
|
static inline size_t
|
2007-11-15 22:14:46 +01:00
|
|
|
my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)
|
2007-07-12 01:15:38 +02:00
|
|
|
{
|
|
|
|
return strftime(s, max, fmt, tm);
|
|
|
|
}
|
|
|
|
|
2005-12-09 16:51:14 +01:00
|
|
|
/*
|
|
|
|
* Determine likely date order from locale
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
locale_date_order(const char *locale)
|
|
|
|
{
|
|
|
|
struct tm testtime;
|
|
|
|
char buf[128];
|
|
|
|
char *posD;
|
|
|
|
char *posM;
|
|
|
|
char *posY;
|
|
|
|
char *save;
|
|
|
|
size_t res;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = DATEORDER_MDY; /* default */
|
|
|
|
|
|
|
|
save = setlocale(LC_TIME, NULL);
|
|
|
|
if (!save)
|
|
|
|
return result;
|
|
|
|
save = xstrdup(save);
|
|
|
|
|
|
|
|
setlocale(LC_TIME, locale);
|
|
|
|
|
|
|
|
memset(&testtime, 0, sizeof(testtime));
|
|
|
|
testtime.tm_mday = 22;
|
|
|
|
testtime.tm_mon = 10; /* November, should come out as "11" */
|
|
|
|
testtime.tm_year = 133; /* 2033 */
|
|
|
|
|
2007-07-12 01:15:38 +02:00
|
|
|
res = my_strftime(buf, sizeof(buf), "%x", &testtime);
|
2005-12-09 16:51:14 +01:00
|
|
|
|
|
|
|
setlocale(LC_TIME, save);
|
|
|
|
free(save);
|
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
posM = strstr(buf, "11");
|
|
|
|
posD = strstr(buf, "22");
|
|
|
|
posY = strstr(buf, "33");
|
|
|
|
|
|
|
|
if (!posM || !posD || !posY)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (posY < posM && posM < posD)
|
|
|
|
result = DATEORDER_YMD;
|
|
|
|
else if (posD < posM)
|
|
|
|
result = DATEORDER_DMY;
|
|
|
|
else
|
|
|
|
result = DATEORDER_MDY;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
2003-11-14 00:46:31 +01:00
|
|
|
* check if given string is a valid locale specifier
|
2008-09-23 11:20:39 +02:00
|
|
|
*
|
|
|
|
* this should match the backend check_locale() function
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static bool
|
2008-09-23 12:58:03 +02:00
|
|
|
check_locale_name(const char *locale)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
int category = LC_CTYPE;
|
|
|
|
char *save;
|
|
|
|
|
|
|
|
save = setlocale(category, NULL);
|
|
|
|
if (!save)
|
|
|
|
return false; /* should not happen; */
|
|
|
|
|
|
|
|
save = xstrdup(save);
|
|
|
|
|
|
|
|
ret = (setlocale(category, locale) != NULL);
|
|
|
|
|
|
|
|
setlocale(category, save);
|
|
|
|
free(save);
|
|
|
|
|
|
|
|
/* should we exit here? */
|
|
|
|
if (!ret)
|
2003-11-23 22:41:30 +01:00
|
|
|
fprintf(stderr, _("%s: invalid locale name \"%s\"\n"), progname, locale);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-23 12:58:03 +02:00
|
|
|
/*
|
|
|
|
* check if the chosen encoding matches the encoding required by the locale
|
|
|
|
*
|
|
|
|
* this should match the similar check in the backend createdb() function
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
check_locale_encoding(const char *locale, int user_enc)
|
|
|
|
{
|
|
|
|
int locale_enc;
|
|
|
|
|
|
|
|
locale_enc = pg_get_encoding_from_locale(locale);
|
|
|
|
|
|
|
|
/* We allow selection of SQL_ASCII --- see notes in createdb() */
|
|
|
|
if (!(locale_enc == user_enc ||
|
|
|
|
locale_enc == PG_SQL_ASCII ||
|
|
|
|
user_enc == PG_SQL_ASCII
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On win32, if the encoding chosen is UTF8, all locales are OK
|
|
|
|
* (assuming the actual locale name passed the checks above). This is
|
|
|
|
* because UTF8 is a pseudo-codepage, that we convert to UTF16 before
|
|
|
|
* doing any operations on, and UTF16 supports all locales.
|
|
|
|
*/
|
|
|
|
|| user_enc == PG_UTF8
|
|
|
|
#endif
|
|
|
|
))
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: encoding mismatch\n"), progname);
|
|
|
|
fprintf(stderr,
|
|
|
|
_("The encoding you selected (%s) and the encoding that the\n"
|
|
|
|
"selected locale uses (%s) do not match. This would lead to\n"
|
|
|
|
"misbehavior in various character string processing functions.\n"
|
|
|
|
"Rerun %s and either do not specify an encoding explicitly,\n"
|
|
|
|
"or choose a matching combination.\n"),
|
|
|
|
pg_encoding_to_char(user_enc),
|
|
|
|
pg_encoding_to_char(locale_enc),
|
|
|
|
progname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* set up the locale variables
|
|
|
|
*
|
2003-11-14 00:46:31 +01:00
|
|
|
* assumes we have called setlocale(LC_ALL,"")
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setlocales(void)
|
|
|
|
{
|
|
|
|
/* set empty lc_* values to locale config if set */
|
|
|
|
|
|
|
|
if (strlen(locale) > 0)
|
|
|
|
{
|
|
|
|
if (strlen(lc_ctype) == 0)
|
|
|
|
lc_ctype = locale;
|
|
|
|
if (strlen(lc_collate) == 0)
|
|
|
|
lc_collate = locale;
|
|
|
|
if (strlen(lc_numeric) == 0)
|
|
|
|
lc_numeric = locale;
|
|
|
|
if (strlen(lc_time) == 0)
|
|
|
|
lc_time = locale;
|
|
|
|
if (strlen(lc_monetary) == 0)
|
|
|
|
lc_monetary = locale;
|
|
|
|
if (strlen(lc_messages) == 0)
|
|
|
|
lc_messages = locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* override absent/invalid config settings from initdb's locale settings
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
|
2008-09-23 12:58:03 +02:00
|
|
|
if (strlen(lc_ctype) == 0 || !check_locale_name(lc_ctype))
|
2003-11-10 23:51:16 +01:00
|
|
|
lc_ctype = xstrdup(setlocale(LC_CTYPE, NULL));
|
2008-09-23 12:58:03 +02:00
|
|
|
if (strlen(lc_collate) == 0 || !check_locale_name(lc_collate))
|
2003-11-10 23:51:16 +01:00
|
|
|
lc_collate = xstrdup(setlocale(LC_COLLATE, NULL));
|
2008-09-23 12:58:03 +02:00
|
|
|
if (strlen(lc_numeric) == 0 || !check_locale_name(lc_numeric))
|
2003-11-10 23:51:16 +01:00
|
|
|
lc_numeric = xstrdup(setlocale(LC_NUMERIC, NULL));
|
2008-09-23 12:58:03 +02:00
|
|
|
if (strlen(lc_time) == 0 || !check_locale_name(lc_time))
|
2003-11-10 23:51:16 +01:00
|
|
|
lc_time = xstrdup(setlocale(LC_TIME, NULL));
|
2008-09-23 12:58:03 +02:00
|
|
|
if (strlen(lc_monetary) == 0 || !check_locale_name(lc_monetary))
|
2003-11-10 23:51:16 +01:00
|
|
|
lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL));
|
2008-09-23 12:58:03 +02:00
|
|
|
if (strlen(lc_messages) == 0 || !check_locale_name(lc_messages))
|
2004-10-23 00:30:57 +02:00
|
|
|
#if defined(LC_MESSAGES) && !defined(WIN32)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
/* when available get the current locale setting */
|
|
|
|
lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
/* when not available, get the CTYPE setting */
|
|
|
|
lc_messages = xstrdup(setlocale(LC_CTYPE, NULL));
|
|
|
|
}
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-02-10 23:05:42 +01:00
|
|
|
#ifdef WIN32
|
2006-10-04 02:30:14 +02:00
|
|
|
typedef BOOL(WINAPI * __CreateRestrictedToken) (HANDLE, DWORD, DWORD, PSID_AND_ATTRIBUTES, DWORD, PLUID_AND_ATTRIBUTES, DWORD, PSID_AND_ATTRIBUTES, PHANDLE);
|
|
|
|
|
|
|
|
#define DISABLE_MAX_PRIVILEGE 0x1
|
2006-02-10 23:05:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a restricted token and execute the specified process with it.
|
|
|
|
*
|
|
|
|
* Returns 0 on failure, non-zero on success, same as CreateProcess().
|
|
|
|
*
|
|
|
|
* On NT4, or any other system not containing the required functions, will
|
|
|
|
* NOT execute anything.
|
|
|
|
*/
|
2006-10-04 02:30:14 +02:00
|
|
|
static int
|
|
|
|
CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo)
|
2006-02-10 23:05:42 +01:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
BOOL b;
|
|
|
|
STARTUPINFO si;
|
|
|
|
HANDLE origToken;
|
|
|
|
HANDLE restrictedToken;
|
|
|
|
SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
|
|
|
|
SID_AND_ATTRIBUTES dropSids[2];
|
|
|
|
__CreateRestrictedToken _CreateRestrictedToken = NULL;
|
|
|
|
HANDLE Advapi32Handle;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
|
|
|
|
Advapi32Handle = LoadLibrary("ADVAPI32.DLL");
|
|
|
|
if (Advapi32Handle != NULL)
|
|
|
|
{
|
|
|
|
_CreateRestrictedToken = (__CreateRestrictedToken) GetProcAddress(Advapi32Handle, "CreateRestrictedToken");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_CreateRestrictedToken == NULL)
|
|
|
|
{
|
2007-05-31 17:13:06 +02:00
|
|
|
fprintf(stderr, "WARNING: cannot create restricted tokens on this platform\n");
|
2006-10-04 02:30:14 +02:00
|
|
|
if (Advapi32Handle != NULL)
|
|
|
|
FreeLibrary(Advapi32Handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the current token to use as a base for the restricted one */
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to open process token: %lu\n", GetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate list of SIDs to remove */
|
|
|
|
ZeroMemory(&dropSids, sizeof(dropSids));
|
|
|
|
if (!AllocateAndInitializeSid(&NtAuthority, 2,
|
|
|
|
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
|
|
|
|
0, &dropSids[0].Sid) ||
|
|
|
|
!AllocateAndInitializeSid(&NtAuthority, 2,
|
|
|
|
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
|
|
|
|
0, &dropSids[1].Sid))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to allocate SIDs: %lu\n", GetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = _CreateRestrictedToken(origToken,
|
|
|
|
DISABLE_MAX_PRIVILEGE,
|
|
|
|
sizeof(dropSids) / sizeof(dropSids[0]),
|
|
|
|
dropSids,
|
|
|
|
0, NULL,
|
|
|
|
0, NULL,
|
|
|
|
&restrictedToken);
|
|
|
|
|
|
|
|
FreeSid(dropSids[1].Sid);
|
|
|
|
FreeSid(dropSids[0].Sid);
|
|
|
|
CloseHandle(origToken);
|
|
|
|
FreeLibrary(Advapi32Handle);
|
|
|
|
|
|
|
|
if (!b)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create restricted token: %lu\n", GetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-29 16:31:33 +01:00
|
|
|
if (!CreateProcessAsUser(restrictedToken,
|
|
|
|
NULL,
|
|
|
|
cmd,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
CREATE_SUSPENDED,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
processInfo))
|
|
|
|
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CreateProcessAsUser failed: %lu\n", GetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-01 00:31:20 +01:00
|
|
|
#ifndef __CYGWIN__
|
2008-02-29 16:31:33 +01:00
|
|
|
AddUserToDacl(processInfo->hProcess);
|
2008-03-01 00:31:20 +01:00
|
|
|
#endif
|
2008-02-29 16:31:33 +01:00
|
|
|
|
|
|
|
return ResumeThread(processInfo->hThread);
|
2006-02-10 23:05:42 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
|
|
|
* print help text
|
|
|
|
*/
|
|
|
|
static void
|
2003-11-23 22:41:30 +01:00
|
|
|
usage(const char *progname)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
|
|
|
|
printf(_("Usage:\n"));
|
|
|
|
printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
|
|
|
|
printf(_("\nOptions:\n"));
|
|
|
|
printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
|
|
|
|
printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
|
2008-09-23 11:20:39 +02:00
|
|
|
printf(_(" --locale=LOCALE set default locale for new databases\n"));
|
2003-11-23 23:17:59 +01:00
|
|
|
printf(_(" --lc-collate, --lc-ctype, --lc-messages=LOCALE\n"
|
|
|
|
" --lc-monetary, --lc-numeric, --lc-time=LOCALE\n"
|
2008-09-23 11:20:39 +02:00
|
|
|
" set default locale in the respective\n"
|
|
|
|
" category for new databases (default\n"
|
|
|
|
" taken from environment)\n"));
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_(" --no-locale equivalent to --locale=C\n"));
|
2007-09-25 18:29:34 +02:00
|
|
|
printf(_(" -T, --text-search-config=CFG\n"
|
2007-11-15 22:14:46 +01:00
|
|
|
" default text search configuration\n"));
|
2007-01-06 20:40:00 +01:00
|
|
|
printf(_(" -X, --xlogdir=XLOGDIR location for the transaction log directory\n"));
|
2004-10-12 23:54:45 +02:00
|
|
|
printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_(" -U, --username=NAME database superuser name\n"));
|
|
|
|
printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
|
2004-10-12 23:54:45 +02:00
|
|
|
printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_(" -?, --help show this help, then exit\n"));
|
|
|
|
printf(_(" -V, --version output version information, then exit\n"));
|
2003-11-23 23:17:59 +01:00
|
|
|
printf(_("\nLess commonly used options:\n"));
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_(" -d, --debug generate lots of debugging output\n"));
|
|
|
|
printf(_(" -s, --show show internal settings\n"));
|
|
|
|
printf(_(" -L DIRECTORY where to find the input files\n"));
|
|
|
|
printf(_(" -n, --noclean do not clean up after errors\n"));
|
2003-11-23 23:17:59 +01:00
|
|
|
printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
|
|
|
|
"is used.\n"));
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
|
|
|
|
}
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* options with no short version return a low integer, the rest return
|
|
|
|
* their short version value
|
|
|
|
*/
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"pgdata", required_argument, NULL, 'D'},
|
|
|
|
{"encoding", required_argument, NULL, 'E'},
|
|
|
|
{"locale", required_argument, NULL, 1},
|
|
|
|
{"lc-collate", required_argument, NULL, 2},
|
|
|
|
{"lc-ctype", required_argument, NULL, 3},
|
|
|
|
{"lc-monetary", required_argument, NULL, 4},
|
|
|
|
{"lc-numeric", required_argument, NULL, 5},
|
|
|
|
{"lc-time", required_argument, NULL, 6},
|
|
|
|
{"lc-messages", required_argument, NULL, 7},
|
|
|
|
{"no-locale", no_argument, NULL, 8},
|
2007-08-21 03:11:32 +02:00
|
|
|
{"text-search-config", required_argument, NULL, 'T'},
|
2004-08-29 07:07:03 +02:00
|
|
|
{"auth", required_argument, NULL, 'A'},
|
2006-10-04 02:30:14 +02:00
|
|
|
{"pwprompt", no_argument, NULL, 'W'},
|
2004-06-24 21:26:59 +02:00
|
|
|
{"pwfile", required_argument, NULL, 9},
|
2003-11-10 23:51:16 +01:00
|
|
|
{"username", required_argument, NULL, 'U'},
|
|
|
|
{"help", no_argument, NULL, '?'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"debug", no_argument, NULL, 'd'},
|
|
|
|
{"show", no_argument, NULL, 's'},
|
|
|
|
{"noclean", no_argument, NULL, 'n'},
|
2007-01-06 20:40:00 +01:00
|
|
|
{"xlogdir", required_argument, NULL, 'X'},
|
2004-10-11 01:37:45 +02:00
|
|
|
{NULL, 0, NULL, 0}
|
2003-11-10 23:51:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int c,
|
2004-05-11 23:57:15 +02:00
|
|
|
i,
|
|
|
|
ret;
|
2003-11-10 23:51:16 +01:00
|
|
|
int option_index;
|
|
|
|
char *short_version;
|
2006-10-04 02:30:14 +02:00
|
|
|
char *effective_user;
|
2004-08-25 22:07:57 +02:00
|
|
|
char *pgdenv; /* PGDATA value gotten from and sent to
|
2003-11-10 23:51:16 +01:00
|
|
|
* environment */
|
2004-11-29 02:14:45 +01:00
|
|
|
char bin_dir[MAXPGPATH];
|
2005-10-15 04:49:52 +02:00
|
|
|
char *pg_data_native;
|
2008-09-23 12:58:03 +02:00
|
|
|
int user_enc;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-02-24 03:02:41 +01:00
|
|
|
#ifdef WIN32
|
2006-10-04 02:30:14 +02:00
|
|
|
char *restrict_env;
|
2006-02-24 03:02:41 +01:00
|
|
|
#endif
|
2004-07-19 04:47:16 +02:00
|
|
|
static const char *subdirs[] = {
|
|
|
|
"global",
|
|
|
|
"pg_xlog",
|
|
|
|
"pg_xlog/archive_status",
|
|
|
|
"pg_clog",
|
|
|
|
"pg_subtrans",
|
2005-06-18 00:32:51 +02:00
|
|
|
"pg_twophase",
|
2005-04-28 23:47:18 +02:00
|
|
|
"pg_multixact/members",
|
|
|
|
"pg_multixact/offsets",
|
2004-07-19 04:47:16 +02:00
|
|
|
"base",
|
|
|
|
"base/1",
|
2008-08-05 14:09:30 +02:00
|
|
|
"pg_tblspc",
|
|
|
|
"pg_stat_tmp"
|
2004-07-19 04:47:16 +02:00
|
|
|
};
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-05-11 23:57:15 +02:00
|
|
|
progname = get_progname(argv[0]);
|
2008-12-11 08:34:09 +01:00
|
|
|
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
|
|
|
usage(progname);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
|
|
|
puts("initdb (PostgreSQL) " PG_VERSION);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2003-11-23 23:17:59 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* process command-line options */
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2004-08-01 07:59:13 +02:00
|
|
|
case 'A':
|
|
|
|
authmethod = xstrdup(optarg);
|
|
|
|
break;
|
2003-11-10 23:51:16 +01:00
|
|
|
case 'D':
|
|
|
|
pg_data = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
encoding = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
pwprompt = true;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
username = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debug = true;
|
2004-08-29 07:07:03 +02:00
|
|
|
printf(_("Running in debug mode.\n"));
|
2003-11-10 23:51:16 +01:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
noclean = true;
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("Running in noclean mode. Mistakes will not be cleaned up.\n"));
|
2003-11-10 23:51:16 +01:00
|
|
|
break;
|
|
|
|
case 'L':
|
2004-05-17 16:35:34 +02:00
|
|
|
share_path = xstrdup(optarg);
|
2003-11-10 23:51:16 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
locale = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
lc_collate = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
lc_ctype = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
lc_monetary = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
lc_numeric = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
lc_time = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
lc_messages = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
locale = "C";
|
|
|
|
break;
|
2004-06-24 21:26:59 +02:00
|
|
|
case 9:
|
|
|
|
pwfilename = xstrdup(optarg);
|
|
|
|
break;
|
2003-11-10 23:51:16 +01:00
|
|
|
case 's':
|
|
|
|
show_setting = true;
|
|
|
|
break;
|
2007-08-21 03:11:32 +02:00
|
|
|
case 'T':
|
|
|
|
default_text_search_config = xstrdup(optarg);
|
|
|
|
break;
|
2007-01-06 20:40:00 +01:00
|
|
|
case 'X':
|
|
|
|
xlog_dir = xstrdup(optarg);
|
|
|
|
break;
|
2003-11-10 23:51:16 +01:00
|
|
|
default:
|
2004-11-29 04:05:03 +01:00
|
|
|
/* getopt_long already emitted a complaint */
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
2004-08-29 07:07:03 +02:00
|
|
|
progname);
|
2003-11-23 23:17:59 +01:00
|
|
|
exit(1);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-24 03:02:41 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Non-option argument specifies data directory */
|
2003-11-10 23:51:16 +01:00
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
pg_data = xstrdup(argv[optind]);
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
if (optind < argc)
|
|
|
|
{
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
|
2004-08-29 07:07:03 +02:00
|
|
|
progname, argv[optind + 1]);
|
2003-11-23 23:17:59 +01:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
2007-08-04 23:01:09 +02:00
|
|
|
exit(1);
|
2003-11-14 00:46:31 +01:00
|
|
|
}
|
|
|
|
|
2004-06-24 21:26:59 +02:00
|
|
|
if (pwprompt && pwfilename)
|
|
|
|
{
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
fprintf(stderr, _("%s: password prompt and password file cannot be specified together\n"), progname);
|
2004-06-24 21:26:59 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-01 07:59:13 +02:00
|
|
|
if (authmethod == NULL || !strlen(authmethod))
|
|
|
|
{
|
2004-10-12 23:54:45 +02:00
|
|
|
authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n"
|
2004-10-17 23:04:41 +02:00
|
|
|
"You can change this by editing pg_hba.conf or using the -A option the\n"
|
2004-08-01 07:59:13 +02:00
|
|
|
"next time you run initdb.\n");
|
2004-08-29 07:07:03 +02:00
|
|
|
authmethod = "trust";
|
2004-08-01 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (strcmp(authmethod, "md5") &&
|
|
|
|
strcmp(authmethod, "ident") &&
|
2005-10-15 04:49:52 +02:00
|
|
|
strncmp(authmethod, "ident ", 6) && /* ident with space = param */
|
2004-08-29 07:07:03 +02:00
|
|
|
strcmp(authmethod, "trust") &&
|
2004-08-01 07:59:13 +02:00
|
|
|
#ifdef USE_PAM
|
2004-08-29 07:07:03 +02:00
|
|
|
strcmp(authmethod, "pam") &&
|
|
|
|
strncmp(authmethod, "pam ", 4) && /* pam with space = param */
|
2004-08-01 07:59:13 +02:00
|
|
|
#endif
|
2004-08-29 07:07:03 +02:00
|
|
|
strcmp(authmethod, "crypt") &&
|
|
|
|
strcmp(authmethod, "password")
|
2004-08-01 07:59:13 +02:00
|
|
|
)
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-01 07:59:13 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Kerberos methods not listed because they are not supported over
|
|
|
|
* local connections and are rejected in hba.c
|
2004-08-01 07:59:13 +02:00
|
|
|
*/
|
|
|
|
{
|
2004-11-29 04:05:03 +01:00
|
|
|
fprintf(stderr, _("%s: unrecognized authentication method \"%s\"\n"),
|
|
|
|
progname, authmethod);
|
2004-08-01 07:59:13 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if ((!strcmp(authmethod, "md5") ||
|
|
|
|
!strcmp(authmethod, "crypt") ||
|
|
|
|
!strcmp(authmethod, "password")) &&
|
|
|
|
!(pwprompt || pwfilename))
|
2004-08-01 07:59:13 +02:00
|
|
|
{
|
2004-10-12 23:54:45 +02:00
|
|
|
fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname, authmethod);
|
2004-08-01 07:59:13 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-06-24 21:26:59 +02:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
if (strlen(pg_data) == 0)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
|
|
|
pgdenv = getenv("PGDATA");
|
|
|
|
if (pgdenv && strlen(pgdenv))
|
|
|
|
{
|
|
|
|
/* PGDATA found */
|
|
|
|
pg_data = xstrdup(pgdenv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2003-11-23 22:41:30 +01:00
|
|
|
_("%s: no data directory specified\n"
|
2003-11-23 23:17:59 +01:00
|
|
|
"You must identify the directory where the data for this database system\n"
|
|
|
|
"will reside. Do this with either the invocation option -D or the\n"
|
|
|
|
"environment variable PGDATA.\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
progname);
|
2003-11-14 00:46:31 +01:00
|
|
|
exit(1);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-29 02:14:45 +01:00
|
|
|
pg_data_native = pg_data;
|
2003-11-10 23:52:10 +01:00
|
|
|
canonicalize_path(pg_data);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2006-02-10 23:05:42 +01:00
|
|
|
#ifdef WIN32
|
2006-02-24 03:02:41 +01:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
/*
|
|
|
|
* Before we execute another program, make sure that we are running with a
|
|
|
|
* restricted token. If not, re-execute ourselves with one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((restrict_env = getenv("PG_RESTRICT_EXEC")) == NULL
|
|
|
|
|| strcmp(restrict_env, "1") != 0)
|
|
|
|
{
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
char *cmdline;
|
2006-02-10 23:05:42 +01:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
|
|
|
cmdline = xstrdup(GetCommandLine());
|
2006-02-24 03:02:41 +01:00
|
|
|
|
|
|
|
putenv("PG_RESTRICT_EXEC=1");
|
2006-10-04 02:30:14 +02:00
|
|
|
|
|
|
|
if (!CreateRestrictedProcess(cmdline, &pi))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to re-exec with restricted token: %lu.\n", GetLastError());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Successfully re-execed. Now wait for child process to capture
|
|
|
|
* exitcode.
|
|
|
|
*/
|
|
|
|
DWORD x;
|
|
|
|
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
|
|
|
|
if (!GetExitCodeProcess(pi.hProcess, &x))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to get exit code from subprocess: %lu\n", GetLastError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(x);
|
|
|
|
}
|
|
|
|
}
|
2006-02-10 23:05:42 +01:00
|
|
|
#endif
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* we have to set PGDATA for postgres rather than pass it on the command
|
|
|
|
* line to avoid dumb quoting problems on Windows, and we would especially
|
|
|
|
* need quotes otherwise on Windows because paths there are most likely to
|
|
|
|
* have embedded spaces.
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
2005-07-10 18:13:13 +02:00
|
|
|
pgdenv = pg_malloc(8 + strlen(pg_data));
|
2003-11-10 23:51:16 +01:00
|
|
|
sprintf(pgdenv, "PGDATA=%s", pg_data);
|
|
|
|
putenv(pgdenv);
|
|
|
|
|
2008-02-20 23:46:24 +01:00
|
|
|
if ((ret = find_other_exec(argv[0], "postgres", PG_BACKEND_VERSIONSTR,
|
2004-05-17 15:17:29 +02:00
|
|
|
backend_exec)) < 0)
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
char full_path[MAXPGPATH];
|
2004-10-15 06:32:28 +02:00
|
|
|
|
|
|
|
if (find_my_exec(argv[0], full_path) < 0)
|
2007-02-10 15:58:55 +01:00
|
|
|
strlcpy(full_path, progname, sizeof(full_path));
|
2004-10-15 06:32:28 +02:00
|
|
|
|
2004-05-11 23:57:15 +02:00
|
|
|
if (ret == -1)
|
|
|
|
fprintf(stderr,
|
2004-08-29 07:07:03 +02:00
|
|
|
_("The program \"postgres\" is needed by %s "
|
2004-10-15 06:32:28 +02:00
|
|
|
"but was not found in the\n"
|
|
|
|
"same directory as \"%s\".\n"
|
2004-08-29 07:07:03 +02:00
|
|
|
"Check your installation.\n"),
|
2004-10-15 06:32:28 +02:00
|
|
|
progname, full_path);
|
2004-05-11 23:57:15 +02:00
|
|
|
else
|
|
|
|
fprintf(stderr,
|
2004-10-15 06:32:28 +02:00
|
|
|
_("The program \"postgres\" was found by \"%s\"\n"
|
|
|
|
"but was not the same version as %s.\n"
|
2004-08-29 07:07:03 +02:00
|
|
|
"Check your installation.\n"),
|
2004-10-15 06:32:28 +02:00
|
|
|
full_path, progname);
|
2003-11-10 23:51:16 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-05-11 23:57:15 +02:00
|
|
|
/* store binary directory */
|
2004-05-17 16:35:34 +02:00
|
|
|
strcpy(bin_path, backend_exec);
|
2004-06-11 00:26:24 +02:00
|
|
|
*last_dir_separator(bin_path) = '\0';
|
2004-08-09 22:20:47 +02:00
|
|
|
canonicalize_path(bin_path);
|
2004-05-17 16:35:34 +02:00
|
|
|
|
|
|
|
if (!share_path)
|
|
|
|
{
|
2005-07-10 18:13:13 +02:00
|
|
|
share_path = pg_malloc(MAXPGPATH);
|
2004-05-17 16:35:34 +02:00
|
|
|
get_share_path(backend_exec, share_path);
|
|
|
|
}
|
2004-08-16 17:44:03 +02:00
|
|
|
else if (!is_absolute_path(share_path))
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: input file location must be an absolute path\n"), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-05-18 05:36:45 +02:00
|
|
|
canonicalize_path(share_path);
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
if ((short_version = get_short_version()) == NULL)
|
|
|
|
{
|
2003-11-25 20:18:26 +01:00
|
|
|
fprintf(stderr, _("%s: could not determine valid short version string\n"), progname);
|
2003-11-10 23:51:16 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2006-02-24 01:55:49 +01:00
|
|
|
effective_user = get_id();
|
|
|
|
if (strlen(username) == 0)
|
|
|
|
username = effective_user;
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
set_input(&bki_file, "postgres.bki");
|
|
|
|
set_input(&desc_file, "postgres.description");
|
2006-02-12 04:22:21 +01:00
|
|
|
set_input(&shdesc_file, "postgres.shdescription");
|
2003-11-10 23:51:16 +01:00
|
|
|
set_input(&hba_file, "pg_hba.conf.sample");
|
|
|
|
set_input(&ident_file, "pg_ident.conf.sample");
|
|
|
|
set_input(&conf_file, "postgresql.conf.sample");
|
|
|
|
set_input(&conversion_file, "conversion_create.sql");
|
2007-08-21 03:11:32 +02:00
|
|
|
set_input(&dictionary_file, "snowball_create.sql");
|
2003-11-10 23:51:16 +01:00
|
|
|
set_input(&info_schema_file, "information_schema.sql");
|
|
|
|
set_input(&features_file, "sql_features.txt");
|
|
|
|
set_input(&system_views_file, "system_views.sql");
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
set_info_version();
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
if (show_setting || debug)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"VERSION=%s\n"
|
2004-05-17 16:35:34 +02:00
|
|
|
"PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
|
2003-11-10 23:51:16 +01:00
|
|
|
"POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
|
2006-02-12 04:22:21 +01:00
|
|
|
"POSTGRES_DESCR=%s\nPOSTGRES_SHDESCR=%s\n"
|
|
|
|
"POSTGRESQL_CONF_SAMPLE=%s\n"
|
2003-11-10 23:51:16 +01:00
|
|
|
"PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
|
|
|
|
PG_VERSION,
|
2004-05-17 16:35:34 +02:00
|
|
|
pg_data, share_path, bin_path,
|
2006-02-24 01:55:49 +01:00
|
|
|
username, bki_file,
|
2006-02-12 04:22:21 +01:00
|
|
|
desc_file, shdesc_file,
|
|
|
|
conf_file,
|
2003-11-10 23:51:16 +01:00
|
|
|
hba_file, ident_file);
|
2003-11-14 00:46:31 +01:00
|
|
|
if (show_setting)
|
|
|
|
exit(0);
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
check_input(bki_file);
|
|
|
|
check_input(desc_file);
|
2006-02-12 04:22:21 +01:00
|
|
|
check_input(shdesc_file);
|
2003-11-10 23:51:16 +01:00
|
|
|
check_input(hba_file);
|
|
|
|
check_input(ident_file);
|
|
|
|
check_input(conf_file);
|
|
|
|
check_input(conversion_file);
|
2007-08-21 03:11:32 +02:00
|
|
|
check_input(dictionary_file);
|
2003-11-10 23:51:16 +01:00
|
|
|
check_input(info_schema_file);
|
|
|
|
check_input(features_file);
|
|
|
|
check_input(system_views_file);
|
|
|
|
|
2004-07-14 19:55:10 +02:00
|
|
|
setlocales();
|
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("The files belonging to this database system will be owned "
|
2004-08-29 07:07:03 +02:00
|
|
|
"by user \"%s\".\n"
|
|
|
|
"This user must also own the server process.\n\n"),
|
2003-11-13 21:12:47 +01:00
|
|
|
effective_user);
|
2003-11-13 16:01:40 +01:00
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
if (strcmp(lc_ctype, lc_collate) == 0 &&
|
|
|
|
strcmp(lc_ctype, lc_time) == 0 &&
|
|
|
|
strcmp(lc_ctype, lc_numeric) == 0 &&
|
|
|
|
strcmp(lc_ctype, lc_monetary) == 0 &&
|
|
|
|
strcmp(lc_ctype, lc_messages) == 0)
|
2004-07-14 19:55:10 +02:00
|
|
|
printf(_("The database cluster will be initialized with locale %s.\n"), lc_ctype);
|
2003-11-10 23:51:16 +01:00
|
|
|
else
|
|
|
|
{
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("The database cluster will be initialized with locales\n"
|
2004-08-29 07:07:03 +02:00
|
|
|
" COLLATE: %s\n"
|
|
|
|
" CTYPE: %s\n"
|
|
|
|
" MESSAGES: %s\n"
|
|
|
|
" MONETARY: %s\n"
|
|
|
|
" NUMERIC: %s\n"
|
|
|
|
" TIME: %s\n"),
|
2003-11-10 23:51:16 +01:00
|
|
|
lc_collate,
|
|
|
|
lc_ctype,
|
|
|
|
lc_messages,
|
|
|
|
lc_monetary,
|
|
|
|
lc_numeric,
|
|
|
|
lc_time);
|
|
|
|
}
|
|
|
|
|
2007-09-29 00:25:49 +02:00
|
|
|
if (strlen(encoding) == 0)
|
2004-07-14 19:55:10 +02:00
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
int ctype_enc;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2007-09-29 00:25:49 +02:00
|
|
|
ctype_enc = pg_get_encoding_from_locale(lc_ctype);
|
|
|
|
|
|
|
|
if (ctype_enc == PG_SQL_ASCII &&
|
|
|
|
!(pg_strcasecmp(lc_ctype, "C") == 0 ||
|
|
|
|
pg_strcasecmp(lc_ctype, "POSIX") == 0))
|
|
|
|
{
|
2007-09-29 02:14:40 +02:00
|
|
|
/* Hmm, couldn't recognize the locale's codeset */
|
2007-10-16 11:09:11 +02:00
|
|
|
fprintf(stderr, _("%s: could not find suitable encoding for locale %s\n"),
|
2007-09-29 00:25:49 +02:00
|
|
|
progname, lc_ctype);
|
|
|
|
fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
|
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
2004-07-14 19:55:10 +02:00
|
|
|
}
|
2007-10-13 22:18:42 +02:00
|
|
|
else if (!pg_valid_server_encoding_id(ctype_enc))
|
2007-09-29 02:14:40 +02:00
|
|
|
{
|
|
|
|
/* We recognized it, but it's not a legal server encoding */
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: locale %s requires unsupported encoding %s\n"),
|
|
|
|
progname, lc_ctype, pg_encoding_to_char(ctype_enc));
|
|
|
|
fprintf(stderr,
|
2007-11-15 22:14:46 +01:00
|
|
|
_("Encoding %s is not allowed as a server-side encoding.\n"
|
|
|
|
"Rerun %s with a different locale selection.\n"),
|
2007-09-29 02:14:40 +02:00
|
|
|
pg_encoding_to_char(ctype_enc), progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-07-14 19:55:10 +02:00
|
|
|
else
|
2007-09-29 00:25:49 +02:00
|
|
|
{
|
|
|
|
encodingid = encodingid_to_string(ctype_enc);
|
|
|
|
printf(_("The default database encoding has accordingly been set to %s.\n"),
|
|
|
|
pg_encoding_to_char(ctype_enc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
encodingid = get_encoding_id(encoding);
|
|
|
|
|
2008-09-23 12:58:03 +02:00
|
|
|
user_enc = atoi(encodingid);
|
|
|
|
if (!check_locale_encoding(lc_ctype, user_enc) ||
|
|
|
|
!check_locale_encoding(lc_collate, user_enc))
|
|
|
|
exit(1); /* check_locale_encoding printed the error */
|
2004-07-14 19:55:10 +02:00
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
if (strlen(default_text_search_config) == 0)
|
|
|
|
{
|
|
|
|
default_text_search_config = find_matching_ts_config(lc_ctype);
|
|
|
|
if (default_text_search_config == NULL)
|
|
|
|
{
|
2007-10-16 11:09:11 +02:00
|
|
|
printf(_("%s: could not find suitable text search configuration for locale %s\n"),
|
2007-08-21 03:11:32 +02:00
|
|
|
progname, lc_ctype);
|
|
|
|
default_text_search_config = "simple";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *checkmatch = find_matching_ts_config(lc_ctype);
|
|
|
|
|
|
|
|
if (checkmatch == NULL)
|
|
|
|
{
|
2007-10-16 11:09:11 +02:00
|
|
|
printf(_("%s: warning: suitable text search configuration for locale %s is unknown\n"),
|
2007-08-21 03:11:32 +02:00
|
|
|
progname, lc_ctype);
|
|
|
|
}
|
|
|
|
else if (strcmp(checkmatch, default_text_search_config) != 0)
|
|
|
|
{
|
2007-10-16 11:09:11 +02:00
|
|
|
printf(_("%s: warning: specified text search configuration \"%s\" might not match locale %s\n"),
|
2007-08-21 03:11:32 +02:00
|
|
|
progname, default_text_search_config, lc_ctype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(_("The default text search configuration will be set to \"%s\".\n"),
|
|
|
|
default_text_search_config);
|
|
|
|
|
2004-07-14 19:55:10 +02:00
|
|
|
printf("\n");
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
umask(077);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* now we are starting to do real work, trap signals so we can clean up
|
2003-11-10 23:51:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* some of these are not valid on Windows */
|
|
|
|
#ifdef SIGHUP
|
|
|
|
pqsignal(SIGHUP, trapsig);
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
#ifdef SIGINT
|
|
|
|
pqsignal(SIGINT, trapsig);
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
#ifdef SIGQUIT
|
|
|
|
pqsignal(SIGQUIT, trapsig);
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
#ifdef SIGTERM
|
|
|
|
pqsignal(SIGTERM, trapsig);
|
2003-11-10 23:52:10 +01:00
|
|
|
#endif
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 19:32:34 +01:00
|
|
|
/* Ignore SIGPIPE when writing to backend, so we can clean up */
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
2007-01-06 20:40:00 +01:00
|
|
|
switch (check_data_dir(pg_data))
|
2003-11-10 23:51:16 +01:00
|
|
|
{
|
2003-11-14 18:19:35 +01:00
|
|
|
case 0:
|
|
|
|
/* PGDATA not there, must create it */
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("creating directory %s ... "),
|
2003-11-14 18:19:35 +01:00
|
|
|
pg_data);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if (!mkdatadir(NULL))
|
|
|
|
exit_nicely();
|
|
|
|
else
|
|
|
|
check_ok();
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 18:19:35 +01:00
|
|
|
made_new_pgdata = true;
|
|
|
|
break;
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 18:19:35 +01:00
|
|
|
case 1:
|
|
|
|
/* Present but empty, fix permissions and use it */
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("fixing permissions on existing directory %s ... "),
|
2003-11-14 18:19:35 +01:00
|
|
|
pg_data);
|
|
|
|
fflush(stdout);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 18:19:35 +01:00
|
|
|
if (chmod(pg_data, 0700) != 0)
|
|
|
|
{
|
2004-11-15 00:36:53 +01:00
|
|
|
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
|
2004-11-09 16:57:57 +01:00
|
|
|
progname, pg_data, strerror(errno));
|
2003-11-14 19:32:34 +01:00
|
|
|
exit_nicely();
|
2003-11-14 18:19:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
check_ok();
|
2003-11-14 19:32:34 +01:00
|
|
|
|
|
|
|
found_existing_pgdata = true;
|
2003-11-14 18:19:35 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Present and not empty */
|
|
|
|
fprintf(stderr,
|
2007-11-16 22:47:32 +01:00
|
|
|
_("%s: directory \"%s\" exists but is not empty\n"),
|
|
|
|
progname, pg_data);
|
|
|
|
fprintf(stderr,
|
|
|
|
_("If you want to create a new database system, either remove or empty\n"
|
2004-08-29 07:07:03 +02:00
|
|
|
"the directory \"%s\" or run %s\n"
|
|
|
|
"with an argument other than \"%s\".\n"),
|
2007-11-16 22:47:32 +01:00
|
|
|
pg_data, progname, pg_data);
|
2003-11-14 19:32:34 +01:00
|
|
|
exit(1); /* no further message needed */
|
2003-11-14 18:19:35 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Trouble accessing directory */
|
2004-11-15 00:36:53 +01:00
|
|
|
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
|
2004-11-09 16:57:57 +01:00
|
|
|
progname, pg_data, strerror(errno));
|
2003-11-14 19:32:34 +01:00
|
|
|
exit_nicely();
|
2003-11-10 23:51:16 +01:00
|
|
|
}
|
|
|
|
|
2007-01-06 20:40:00 +01:00
|
|
|
/* Create transaction log symlink, if required */
|
|
|
|
if (strcmp(xlog_dir, "") != 0)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
char *linkloc;
|
2007-01-06 20:40:00 +01:00
|
|
|
|
2008-06-02 05:48:00 +02:00
|
|
|
/* clean up xlog directory name, check it's absolute */
|
|
|
|
canonicalize_path(xlog_dir);
|
|
|
|
if (!is_absolute_path(xlog_dir))
|
|
|
|
{
|
2008-10-31 08:15:11 +01:00
|
|
|
fprintf(stderr, _("%s: transaction log directory location must be an absolute path\n"), progname);
|
2008-06-02 05:48:00 +02:00
|
|
|
exit_nicely();
|
|
|
|
}
|
2007-01-06 20:40:00 +01:00
|
|
|
|
|
|
|
/* check if the specified xlog directory is empty */
|
|
|
|
switch (check_data_dir(xlog_dir))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
/* xlog directory not there, must create it */
|
|
|
|
printf(_("creating directory %s ... "),
|
|
|
|
xlog_dir);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if (mkdir_p(xlog_dir, 0700) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
|
|
|
|
progname, xlog_dir, strerror(errno));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
check_ok();
|
|
|
|
|
|
|
|
made_new_xlogdir = true;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* Present but empty, fix permissions and use it */
|
|
|
|
printf(_("fixing permissions on existing directory %s ... "),
|
|
|
|
xlog_dir);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if (chmod(xlog_dir, 0700) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
|
|
|
|
progname, xlog_dir, strerror(errno));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
check_ok();
|
|
|
|
|
|
|
|
found_existing_xlogdir = true;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* Present and not empty */
|
|
|
|
fprintf(stderr,
|
2007-11-16 22:47:32 +01:00
|
|
|
_("%s: directory \"%s\" exists but is not empty\n"),
|
|
|
|
progname, xlog_dir);
|
|
|
|
fprintf(stderr,
|
|
|
|
_("If you want to store the transaction log there, either\n"
|
2007-01-06 20:40:00 +01:00
|
|
|
"remove or empty the directory \"%s\".\n"),
|
2007-11-16 22:47:32 +01:00
|
|
|
xlog_dir);
|
2008-06-02 05:48:00 +02:00
|
|
|
exit_nicely();
|
2007-01-06 20:40:00 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Trouble accessing directory */
|
|
|
|
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
|
|
|
|
progname, xlog_dir, strerror(errno));
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
|
2008-06-02 05:48:00 +02:00
|
|
|
/* form name of the place where the symlink must go */
|
|
|
|
linkloc = (char *) pg_malloc(strlen(pg_data) + 8 + 1);
|
|
|
|
sprintf(linkloc, "%s/pg_xlog", pg_data);
|
|
|
|
|
2007-01-06 20:40:00 +01:00
|
|
|
#ifdef HAVE_SYMLINK
|
|
|
|
if (symlink(xlog_dir, linkloc) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
|
2007-11-15 22:14:46 +01:00
|
|
|
progname, linkloc, strerror(errno));
|
2007-01-06 20:40:00 +01:00
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
#else
|
2007-05-31 17:13:06 +02:00
|
|
|
fprintf(stderr, _("%s: symlinks are not supported on this platform"));
|
2007-01-06 20:40:00 +01:00
|
|
|
exit_nicely();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Create required subdirectories */
|
2006-01-27 20:01:15 +01:00
|
|
|
printf(_("creating subdirectories ... "));
|
|
|
|
fflush(stdout);
|
2003-11-14 00:46:31 +01:00
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
|
|
|
|
{
|
|
|
|
if (!mkdatadir(subdirs[i]))
|
|
|
|
exit_nicely();
|
|
|
|
}
|
|
|
|
|
2006-01-27 20:01:15 +01:00
|
|
|
check_ok();
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Top level PG_VERSION is checked by bootstrapper, so make it first */
|
2003-11-10 23:51:16 +01:00
|
|
|
set_short_version(short_version, NULL);
|
|
|
|
|
2006-01-01 00:50:59 +01:00
|
|
|
/* Select suitable configuration settings */
|
2003-11-10 23:51:16 +01:00
|
|
|
set_null_conf();
|
2006-01-01 00:50:59 +01:00
|
|
|
test_config_settings();
|
2003-11-10 23:51:16 +01:00
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Now create all the text config files */
|
2003-11-10 23:51:16 +01:00
|
|
|
setup_config();
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Bootstrap template1 */
|
2003-11-10 23:51:16 +01:00
|
|
|
bootstrap_template1(short_version);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make the per-database PG_VERSION for template1 only after init'ing it
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2003-11-10 23:51:16 +01:00
|
|
|
set_short_version(short_version, "base/1");
|
|
|
|
|
2003-11-14 00:46:31 +01:00
|
|
|
/* Create the stuff we don't need to use bootstrap mode for */
|
|
|
|
|
2005-06-28 07:09:14 +02:00
|
|
|
setup_auth();
|
2004-06-24 21:26:59 +02:00
|
|
|
if (pwprompt || pwfilename)
|
2003-11-10 23:52:10 +01:00
|
|
|
get_set_pwd();
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
setup_depend();
|
|
|
|
|
|
|
|
setup_sysviews();
|
|
|
|
|
|
|
|
setup_description();
|
|
|
|
|
|
|
|
setup_conversion();
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
setup_dictionary();
|
|
|
|
|
2003-11-10 23:51:16 +01:00
|
|
|
setup_privileges();
|
|
|
|
|
|
|
|
setup_schema();
|
|
|
|
|
|
|
|
vacuum_db();
|
|
|
|
|
|
|
|
make_template0();
|
|
|
|
|
2005-06-21 06:02:34 +02:00
|
|
|
make_postgres();
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2004-08-01 07:59:13 +02:00
|
|
|
if (authwarning != NULL)
|
2005-04-30 10:08:51 +02:00
|
|
|
fprintf(stderr, "%s", authwarning);
|
2004-08-01 07:59:13 +02:00
|
|
|
|
2004-11-29 02:14:45 +01:00
|
|
|
/* Get directory specification used to start this executable */
|
|
|
|
strcpy(bin_dir, argv[0]);
|
|
|
|
get_parent_directory(bin_dir);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2003-11-23 22:41:30 +01:00
|
|
|
printf(_("\nSuccess. You can now start the database server using:\n\n"
|
2006-06-18 17:38:37 +02:00
|
|
|
" %s%s%spostgres%s -D %s%s%s\n"
|
2004-08-29 07:07:03 +02:00
|
|
|
"or\n"
|
2005-08-29 00:21:46 +02:00
|
|
|
" %s%s%spg_ctl%s -D %s%s%s -l logfile start\n\n"),
|
2005-10-15 04:49:52 +02:00
|
|
|
QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
|
|
|
|
QUOTE_PATH, pg_data_native, QUOTE_PATH,
|
|
|
|
QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
|
|
|
|
QUOTE_PATH, pg_data_native, QUOTE_PATH);
|
2003-11-10 23:51:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|