summaryrefslogtreecommitdiff
path: root/src/backend/utils/init
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/init')
-rw-r--r--src/backend/utils/init/flatfiles.c36
-rw-r--r--src/backend/utils/init/globals.c20
-rw-r--r--src/backend/utils/init/miscinit.c72
-rw-r--r--src/backend/utils/init/postinit.c8
4 files changed, 70 insertions, 66 deletions
diff --git a/src/backend/utils/init/flatfiles.c b/src/backend/utils/init/flatfiles.c
index 2343c01b54..e4b7154b93 100644
--- a/src/backend/utils/init/flatfiles.c
+++ b/src/backend/utils/init/flatfiles.c
@@ -23,7 +23,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/backend/utils/init/flatfiles.c,v 1.11 2005/06/29 20:34:15 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/init/flatfiles.c,v 1.12 2005/07/04 04:51:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -50,9 +50,9 @@
#include "utils/syscache.h"
-/* Actual names of the flat files (within $PGDATA/global/) */
-#define DATABASE_FLAT_FILE "pg_database"
-#define AUTH_FLAT_FILE "pg_auth"
+/* Actual names of the flat files (within $PGDATA) */
+#define DATABASE_FLAT_FILE "global/pg_database"
+#define AUTH_FLAT_FILE "global/pg_auth"
/* Info bits in a flatfiles 2PC record */
#define FF_BIT_DATABASE 1
@@ -98,41 +98,29 @@ auth_file_update_needed(void)
/*
- * database_getflatfilename --- get full pathname of database file
+ * database_getflatfilename --- get pathname of database file
*
* Note that result string is palloc'd, and should be freed by the caller.
+ * (This convention is not really needed anymore, since the relative path
+ * is fixed.)
*/
char *
database_getflatfilename(void)
{
- int bufsize;
- char *pfnam;
-
- bufsize = strlen(DataDir) + strlen("/global/") +
- strlen(DATABASE_FLAT_FILE) + 1;
- pfnam = (char *) palloc(bufsize);
- snprintf(pfnam, bufsize, "%s/global/%s", DataDir, DATABASE_FLAT_FILE);
-
- return pfnam;
+ return pstrdup(DATABASE_FLAT_FILE);
}
/*
- * auth_getflatfilename --- get full pathname of auth file
+ * auth_getflatfilename --- get pathname of auth file
*
* Note that result string is palloc'd, and should be freed by the caller.
+ * (This convention is not really needed anymore, since the relative path
+ * is fixed.)
*/
char *
auth_getflatfilename(void)
{
- int bufsize;
- char *pfnam;
-
- bufsize = strlen(DataDir) + strlen("/global/") +
- strlen(AUTH_FLAT_FILE) + 1;
- pfnam = (char *) palloc(bufsize);
- snprintf(pfnam, bufsize, "%s/global/%s", DataDir, AUTH_FLAT_FILE);
-
- return pfnam;
+ return pstrdup(AUTH_FLAT_FILE);
}
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index 1c9725d629..a4fb354a4c 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/init/globals.c,v 1.95 2004/12/31 22:01:40 pgsql Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/init/globals.c,v 1.96 2005/07/04 04:51:50 tgl Exp $
*
* NOTES
* Globals used all over the place should be declared here and not
@@ -36,13 +36,14 @@ int MyProcPid;
struct Port *MyProcPort;
long MyCancelKey;
+/*
+ * DataDir is the absolute path to the top level of the PGDATA directory tree.
+ * Except during early startup, this is also the server's working directory;
+ * most code therefore can simply use relative paths and not reference DataDir
+ * explicitly.
+ */
char *DataDir = NULL;
- /*
- * The PGDATA directory user says to use, or defaults to via environment
- * variable. NULL if no option given and no environment variable set
- */
-
char OutputFileName[MAXPGPATH]; /* debugging output file */
char my_exec_path[MAXPGPATH]; /* full path to my executable */
@@ -56,11 +57,16 @@ char postgres_exec_path[MAXPGPATH]; /* full path to backend */
BackendId MyBackendId = InvalidBackendId;
-char *DatabasePath = NULL;
Oid MyDatabaseId = InvalidOid;
Oid MyDatabaseTableSpace = InvalidOid;
+/*
+ * DatabasePath is the path (relative to DataDir) of my database's
+ * primary directory, ie, its directory in the default tablespace.
+ */
+char *DatabasePath = NULL;
+
pid_t PostmasterPid = 0;
/*
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index be1f6e7049..ffe7db5f7e 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/init/miscinit.c,v 1.144 2005/06/28 22:16:45 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/init/miscinit.c,v 1.145 2005/07/04 04:51:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -41,10 +41,11 @@
#include "utils/syscache.h"
+#define DIRECTORY_LOCK_FILE "postmaster.pid"
+
ProcessingMode Mode = InitProcessing;
-/* Note: we rely on these to initialize as zeroes */
-static char directoryLockFile[MAXPGPATH];
+/* Note: we rely on this to initialize as zeroes */
static char socketLockFile[MAXPGPATH];
@@ -178,15 +179,32 @@ SetDataDir(const char *dir)
}
/*
+ * Change working directory to DataDir. Most of the postmaster and backend
+ * code assumes that we are in DataDir so it can use relative paths to access
+ * stuff in and under the data directory. For convenience during path
+ * setup, however, we don't force the chdir to occur during SetDataDir.
+ */
+void
+ChangeToDataDir(void)
+{
+ AssertState(DataDir);
+
+ if (chdir(DataDir) < 0)
+ ereport(FATAL,
+ (errcode_for_file_access(),
+ errmsg("could not change directory to \"%s\": %m",
+ DataDir)));
+}
+
+/*
* If the given pathname isn't already absolute, make it so, interpreting
* it relative to the current working directory.
*
* Also canonicalizes the path. The result is always a malloc'd copy.
*
- * Note: it is probably unwise to use this in running backends, since they
- * have chdir'd to a database-specific subdirectory; the results would not be
- * consistent across backends. Currently this is used only during postmaster
- * or standalone-backend startup.
+ * Note: interpretation of relative-path arguments during postmaster startup
+ * should happen before doing ChangeToDataDir(), else the user will probably
+ * not like the results.
*/
char *
make_absolute_path(const char *path)
@@ -713,17 +731,22 @@ CreateLockFile(const char *filename, bool amPostmaster,
on_proc_exit(UnlinkLockFile, PointerGetDatum(strdup(filename)));
}
+/*
+ * Create the data directory lockfile.
+ *
+ * When this is called, we must have already switched the working
+ * directory to DataDir, so we can just use a relative path. This
+ * helps ensure that we are locking the directory we should be.
+ */
void
-CreateDataDirLockFile(const char *datadir, bool amPostmaster)
+CreateDataDirLockFile(bool amPostmaster)
{
- char lockfile[MAXPGPATH];
-
- snprintf(lockfile, sizeof(lockfile), "%s/postmaster.pid", datadir);
- CreateLockFile(lockfile, amPostmaster, true, datadir);
- /* Save name of lockfile for RecordSharedMemoryInLockFile */
- strcpy(directoryLockFile, lockfile);
+ CreateLockFile(DIRECTORY_LOCK_FILE, amPostmaster, true, DataDir);
}
+/*
+ * Create a lockfile for the specified Unix socket file.
+ */
void
CreateSocketLockFile(const char *socketfile, bool amPostmaster)
{
@@ -777,7 +800,7 @@ TouchSocketLockFile(void)
/*
* Append information about a shared memory segment to the data directory
- * lock file (if we have created one).
+ * lock file.
*
* This may be called multiple times in the life of a postmaster, if we
* delete and recreate shmem due to backend crash. Therefore, be prepared
@@ -793,20 +816,13 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
char *ptr;
char buffer[BLCKSZ];
- /*
- * Do nothing if we did not create a lockfile (probably because we are
- * running standalone).
- */
- if (directoryLockFile[0] == '\0')
- return;
-
- fd = open(directoryLockFile, O_RDWR | PG_BINARY, 0);
+ fd = open(DIRECTORY_LOCK_FILE, O_RDWR | PG_BINARY, 0);
if (fd < 0)
{
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not open file \"%s\": %m",
- directoryLockFile)));
+ DIRECTORY_LOCK_FILE)));
return;
}
len = read(fd, buffer, sizeof(buffer) - 100);
@@ -815,7 +831,7 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not read from file \"%s\": %m",
- directoryLockFile)));
+ DIRECTORY_LOCK_FILE)));
close(fd);
return;
}
@@ -828,7 +844,7 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
if (ptr == NULL ||
(ptr = strchr(ptr + 1, '\n')) == NULL)
{
- elog(LOG, "bogus data in \"%s\"", directoryLockFile);
+ elog(LOG, "bogus data in \"%s\"", DIRECTORY_LOCK_FILE);
close(fd);
return;
}
@@ -855,7 +871,7 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m",
- directoryLockFile)));
+ DIRECTORY_LOCK_FILE)));
close(fd);
return;
}
@@ -864,7 +880,7 @@ RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
ereport(LOG,
(errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m",
- directoryLockFile)));
+ DIRECTORY_LOCK_FILE)));
}
}
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 21b0650e82..18e60cab0f 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.151 2005/06/28 19:51:23 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.152 2005/07/04 04:51:50 tgl Exp $
*
*
*-------------------------------------------------------------------------
@@ -338,12 +338,6 @@ InitPostgres(const char *dbname, const char *username)
ValidatePgVersion(fullpath);
- if (chdir(fullpath) == -1)
- ereport(FATAL,
- (errcode_for_file_access(),
- errmsg("could not change directory to \"%s\": %m",
- fullpath)));
-
SetDatabasePath(fullpath);
}