Make the backend grok relative paths for the data directory by converting

it to an absolute path.
This commit is contained in:
Peter Eisentraut 2000-11-04 12:43:24 +00:00
parent 7bea44f449
commit abfb417574
5 changed files with 111 additions and 42 deletions

View File

@ -8,10 +8,12 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.95 2000/10/24 09:56:09 vadim Exp $ * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.96 2000/11/04 12:43:23 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h"
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <signal.h> #include <signal.h>
@ -19,7 +21,6 @@
#define BOOTSTRAP_INCLUDE /* mask out stuff in tcop/tcopprot.h */ #define BOOTSTRAP_INCLUDE /* mask out stuff in tcop/tcopprot.h */
#include "postgres.h"
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
#include <getopt.h> #include <getopt.h>
#endif #endif
@ -220,6 +221,7 @@ BootstrapMain(int argc, char *argv[])
char *dbName; char *dbName;
int flag; int flag;
bool xloginit = false; bool xloginit = false;
char *potential_DataDir = NULL;
extern int optind; extern int optind;
extern char *optarg; extern char *optarg;
@ -255,7 +257,7 @@ BootstrapMain(int argc, char *argv[])
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
ResetAllOptions(); ResetAllOptions();
DataDir = getenv("PGDATA"); /* Null if no PGDATA variable */ potential_DataDir = getenv("PGDATA"); /* Null if no PGDATA variable */
} }
while ((flag = getopt(argc, argv, "D:dCQxpB:F")) != EOF) while ((flag = getopt(argc, argv, "D:dCQxpB:F")) != EOF)
@ -263,7 +265,7 @@ BootstrapMain(int argc, char *argv[])
switch (flag) switch (flag)
{ {
case 'D': case 'D':
DataDir = optarg; potential_DataDir = optarg;
break; break;
case 'd': case 'd':
DebugMode = true; /* print out debugging info while DebugMode = true; /* print out debugging info while
@ -301,7 +303,9 @@ BootstrapMain(int argc, char *argv[])
SetProcessingMode(BootstrapProcessing); SetProcessingMode(BootstrapProcessing);
IgnoreSystemIndexes(true); IgnoreSystemIndexes(true);
if (!DataDir) if (!IsUnderPostmaster)
{
if (!potential_DataDir)
{ {
fprintf(stderr, "%s does not know where to find the database system " fprintf(stderr, "%s does not know where to find the database system "
"data. You must specify the directory that contains the " "data. You must specify the directory that contains the "
@ -310,6 +314,9 @@ BootstrapMain(int argc, char *argv[])
argv[0]); argv[0]);
proc_exit(1); proc_exit(1);
} }
SetDataDir(potential_DataDir);
}
Assert(DataDir);
if (dbName == NULL) if (dbName == NULL)
{ {

View File

@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.177 2000/11/01 21:14:02 petere Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.178 2000/11/04 12:43:23 petere Exp $
* *
* NOTES * NOTES
* *
@ -268,12 +268,12 @@ extern void GetCharSetByHost(char *, int, char *);
static void static void
checkDataDir(const char *DataDir) checkDataDir(const char *checkdir)
{ {
char path[MAXPGPATH]; char path[MAXPGPATH];
FILE *fp; FILE *fp;
if (DataDir == NULL) if (checkdir == NULL)
{ {
fprintf(stderr, "%s does not know where to find the database system " fprintf(stderr, "%s does not know where to find the database system "
"data. You must specify the directory that contains the " "data. You must specify the directory that contains the "
@ -285,10 +285,10 @@ checkDataDir(const char *DataDir)
#ifdef OLD_FILE_NAMING #ifdef OLD_FILE_NAMING
snprintf(path, sizeof(path), "%s%cbase%ctemplate1%cpg_class", snprintf(path, sizeof(path), "%s%cbase%ctemplate1%cpg_class",
DataDir, SEP_CHAR, SEP_CHAR, SEP_CHAR); checkdir, SEP_CHAR, SEP_CHAR, SEP_CHAR);
#else #else
snprintf(path, sizeof(path), "%s%cbase%c%u%c%u", snprintf(path, sizeof(path), "%s%cbase%c%u%c%u",
DataDir, SEP_CHAR, SEP_CHAR, checkdir, SEP_CHAR, SEP_CHAR,
TemplateDbOid, SEP_CHAR, RelOid_pg_class); TemplateDbOid, SEP_CHAR, RelOid_pg_class);
#endif #endif
@ -298,13 +298,13 @@ checkDataDir(const char *DataDir)
fprintf(stderr, "%s does not find the database system." fprintf(stderr, "%s does not find the database system."
"\n\tExpected to find it in the PGDATA directory \"%s\"," "\n\tExpected to find it in the PGDATA directory \"%s\","
"\n\tbut unable to open file \"%s\": %s\n\n", "\n\tbut unable to open file \"%s\": %s\n\n",
progname, DataDir, path, strerror(errno)); progname, checkdir, path, strerror(errno));
exit(2); exit(2);
} }
FreeFile(fp); FreeFile(fp);
ValidatePgVersion(DataDir); ValidatePgVersion(checkdir);
} }
@ -314,6 +314,7 @@ PostmasterMain(int argc, char *argv[])
int opt; int opt;
int status; int status;
char original_extraoptions[MAXPGPATH]; char original_extraoptions[MAXPGPATH];
char *potential_DataDir = NULL;
IsUnderPostmaster = true; /* so that backends know this */ IsUnderPostmaster = true; /* so that backends know this */
@ -353,8 +354,7 @@ PostmasterMain(int argc, char *argv[])
/* /*
* Options setup * Options setup
*/ */
if (getenv("PGDATA")) potential_DataDir = getenv("PGDATA"); /* default value */
DataDir = strdup(getenv("PGDATA")); /* default value */
ResetAllOptions(); ResetAllOptions();
@ -377,9 +377,7 @@ PostmasterMain(int argc, char *argv[])
switch(opt) switch(opt)
{ {
case 'D': case 'D':
if (DataDir) potential_DataDir = optarg;
free(DataDir);
DataDir = strdup(optarg);
break; break;
case '-': case '-':
@ -415,12 +413,14 @@ PostmasterMain(int argc, char *argv[])
} }
} }
optind = 1; /* start over */ checkDataDir(potential_DataDir); /* issues error messages */
checkDataDir(DataDir); /* issues error messages */ SetDataDir(potential_DataDir);
ProcessConfigFile(PGC_POSTMASTER); ProcessConfigFile(PGC_POSTMASTER);
IgnoreSystemIndexes(false); IgnoreSystemIndexes(false);
optind = 1; /* start over */
while ((opt = getopt(argc, argv, "A:a:B:b:D:d:Film:MN:no:p:Ss-:?")) != EOF) while ((opt = getopt(argc, argv, "A:a:B:b:D:d:Film:MN:no:p:Ss-:?")) != EOF)
{ {
switch (opt) switch (opt)

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.184 2000/10/28 18:27:56 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.185 2000/11/04 12:43:24 petere Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
@ -29,7 +29,7 @@
#include <errno.h> #include <errno.h>
#if HAVE_SYS_SELECT_H #if HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif /* aix */ #endif
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
@ -1058,6 +1058,8 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
char *remote_host; char *remote_host;
unsigned short remote_port; unsigned short remote_port;
char *potential_DataDir = NULL;
extern int optind; extern int optind;
extern char *optarg; extern char *optarg;
extern int DebugLvl; extern int DebugLvl;
@ -1082,8 +1084,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
ResetAllOptions(); ResetAllOptions();
if (getenv("PGDATA")) potential_DataDir = getenv("PGDATA");
DataDir = strdup(getenv("PGDATA"));
} }
StatFp = stderr; StatFp = stderr;
@ -1142,9 +1143,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
case 'D': /* PGDATA directory */ case 'D': /* PGDATA directory */
if (secure) if (secure)
{ {
if (DataDir) potential_DataDir = optarg;
free(DataDir);
DataDir = strdup(optarg);
} }
break; break;
@ -1429,7 +1428,9 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
Show_query_stats = false; Show_query_stats = false;
} }
if (!DataDir) if (!IsUnderPostmaster)
{
if (!potential_DataDir)
{ {
fprintf(stderr, "%s does not know where to find the database system " fprintf(stderr, "%s does not know where to find the database system "
"data. You must specify the directory that contains the " "data. You must specify the directory that contains the "
@ -1438,6 +1439,9 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
argv[0]); argv[0]);
proc_exit(1); proc_exit(1);
} }
SetDataDir(potential_DataDir);
}
Assert(DataDir);
/* /*
* 1. Set BlockSig and UnBlockSig masks. 2. Set up signal handlers. 3. * 1. Set BlockSig and UnBlockSig masks. 2. Set up signal handlers. 3.
@ -1631,7 +1635,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[], const cha
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
puts("\nPOSTGRES backend interactive interface "); puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.184 $ $Date: 2000/10/28 18:27:56 $\n"); puts("$Revision: 1.185 $ $Date: 2000/11/04 12:43:24 $\n");
} }
/* /*

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.55 2000/09/19 18:17:57 petere Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.56 2000/11/04 12:43:24 petere Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -366,6 +366,62 @@ GetUserName(Oid userid)
/*-------------------------------------------------------------------------
* Set data directory, but make sure it's an absolute path. Use this,
* never set DataDir directly.
*-------------------------------------------------------------------------
*/
void
SetDataDir(const char *dir)
{
char *new;
AssertArg(dir);
if (DataDir)
free(DataDir);
if (dir[0] != '/')
{
char *buf;
size_t buflen;
buflen = MAXPGPATH;
for (;;)
{
buf = malloc(buflen);
if (!buf)
elog(FATAL, "out of memory");
if (getcwd(buf, buflen))
break;
else if (errno == ERANGE)
{
free(buf);
buflen *= 2;
continue;
}
else
{
free(buf);
elog(FATAL, "cannot get current working directory: %m");
}
}
new = malloc(strlen(buf) + 1 + strlen(dir) + 1);
sprintf(new, "%s/%s", buf, dir);
}
else
{
new = strdup(dir);
}
if (!new)
elog(FATAL, "out of memory");
DataDir = new;
}
/*------------------------------------------------------------------------- /*-------------------------------------------------------------------------
* *
* postmaster pid file stuffs. $DATADIR/postmaster.pid is created when: * postmaster pid file stuffs. $DATADIR/postmaster.pid is created when:

View File

@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: miscadmin.h,v 1.68 2000/10/08 09:25:38 ishii Exp $ * $Id: miscadmin.h,v 1.69 2000/11/04 12:43:24 petere Exp $
* *
* NOTES * NOTES
* some of the information in this file will be moved to * some of the information in this file will be moved to
@ -137,6 +137,8 @@ extern Oid GetSessionUserId(void);
extern void SetSessionUserId(Oid userid); extern void SetSessionUserId(Oid userid);
extern void SetSessionUserIdFromUserName(const char *username); extern void SetSessionUserIdFromUserName(const char *username);
extern void SetDataDir(const char *dir);
extern int FindExec(char *full_path, const char *argv0, const char *binary_name); extern int FindExec(char *full_path, const char *argv0, const char *binary_name);
extern int CheckPathAccess(char *path, char *name, int open_mode); extern int CheckPathAccess(char *path, char *name, int open_mode);