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/enbl.c21
-rw-r--r--src/backend/utils/init/findbe.c377
-rw-r--r--src/backend/utils/init/globals.c127
-rw-r--r--src/backend/utils/init/miscinit.c268
-rw-r--r--src/backend/utils/init/postinit.c1064
5 files changed, 955 insertions, 902 deletions
diff --git a/src/backend/utils/init/enbl.c b/src/backend/utils/init/enbl.c
index 995ab9d9567..dfcf295e34c 100644
--- a/src/backend/utils/init/enbl.c
+++ b/src/backend/utils/init/enbl.c
@@ -1,13 +1,13 @@
/*-------------------------------------------------------------------------
*
* enbl.c--
- * POSTGRES module enable and disable support code.
+ * POSTGRES module enable and disable support code.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/enbl.c,v 1.1.1.1 1996/07/09 06:22:08 scrappy Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/enbl.c,v 1.2 1997/09/07 04:53:42 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,15 +16,15 @@
/*
* BypassEnable --
- * False iff enable/disable processing is required given on and "*countP."
+ * False iff enable/disable processing is required given on and "*countP."
*
* Note:
- * As a side-effect, *countP is modified. It should be 0 initially.
+ * As a side-effect, *countP is modified. It should be 0 initially.
*
* Exceptions:
- * BadState if called with pointer to value 0 and false.
- * BadArg if "countP" is invalid pointer.
- * BadArg if on is invalid.
+ * BadState if called with pointer to value 0 and false.
+ * BadArg if "countP" is invalid pointer.
+ * BadArg if on is invalid.
*/
bool
BypassEnable(int *enableCountInOutP, bool on)
@@ -32,14 +32,15 @@ BypassEnable(int *enableCountInOutP, bool on)
AssertArg(PointerIsValid(enableCountInOutP));
AssertArg(BoolIsValid(on));
- if (on) {
+ if (on)
+ {
*enableCountInOutP += 1;
- return ((bool)(*enableCountInOutP >= 2));
+ return ((bool) (*enableCountInOutP >= 2));
}
AssertState(*enableCountInOutP >= 1);
*enableCountInOutP -= 1;
- return ((bool)(*enableCountInOutP >= 1));
+ return ((bool) (*enableCountInOutP >= 1));
}
diff --git a/src/backend/utils/init/findbe.c b/src/backend/utils/init/findbe.c
index 8718e3f91d6..5e6aa7412a6 100644
--- a/src/backend/utils/init/findbe.c
+++ b/src/backend/utils/init/findbe.c
@@ -6,7 +6,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/findbe.c,v 1.5 1997/08/27 03:48:38 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/findbe.c,v 1.6 1997/09/07 04:53:45 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -19,213 +19,230 @@
#include <unistd.h>
#include "postgres.h"
-#include "miscadmin.h" /* for DebugLvl */
+#include "miscadmin.h" /* for DebugLvl */
-#ifndef S_IRUSR /* XXX [TRH] should be in a header */
-# define S_IRUSR S_IREAD
-# define S_IWUSR S_IWRITE
-# define S_IXUSR S_IEXEC
-# define S_IRGRP ((S_IRUSR)>>3)
-# define S_IWGRP ((S_IWUSR)>>3)
-# define S_IXGRP ((S_IXUSR)>>3)
-# define S_IROTH ((S_IRUSR)>>6)
-# define S_IWOTH ((S_IWUSR)>>6)
-# define S_IXOTH ((S_IXUSR)>>6)
+#ifndef S_IRUSR /* XXX [TRH] should be in a header */
+#define S_IRUSR S_IREAD
+#define S_IWUSR S_IWRITE
+#define S_IXUSR S_IEXEC
+#define S_IRGRP ((S_IRUSR)>>3)
+#define S_IWGRP ((S_IWUSR)>>3)
+#define S_IXGRP ((S_IXUSR)>>3)
+#define S_IROTH ((S_IRUSR)>>6)
+#define S_IWOTH ((S_IWUSR)>>6)
+#define S_IXOTH ((S_IXUSR)>>6)
#endif
/*
* ValidateBackend -- validate "path" as a POSTGRES executable file
*
* returns 0 if the file is found and no error is encountered.
- * -1 if the regular file "path" does not exist or cannot be executed.
- * -2 if the file is otherwise valid but cannot be read.
+ * -1 if the regular file "path" does not exist or cannot be executed.
+ * -2 if the file is otherwise valid but cannot be read.
*/
int
ValidateBackend(char *path)
{
- struct stat buf;
- uid_t euid;
- struct group *gp;
- struct passwd *pwp;
- int i;
- int is_r = 0;
- int is_x = 0;
- int in_grp = 0;
-
- /*
- * Ensure that the file exists and is a regular file.
- *
- * XXX if you have a broken system where stat() looks at the symlink
- * instead of the underlying file, you lose.
- */
- if (strlen(path) >= MAXPGPATH) {
- if (DebugLvl > 1)
- fprintf(stderr, "ValidateBackend: pathname \"%s\" is too long\n",
- path);
- return(-1);
- }
+ struct stat buf;
+ uid_t euid;
+ struct group *gp;
+ struct passwd *pwp;
+ int i;
+ int is_r = 0;
+ int is_x = 0;
+ int in_grp = 0;
- if (stat(path, &buf) < 0) {
- if (DebugLvl > 1)
- fprintf(stderr, "ValidateBackend: can't stat \"%s\"\n",
- path);
- return(-1);
- }
- if (!(buf.st_mode & S_IFREG)) {
- if (DebugLvl > 1)
- fprintf(stderr, "ValidateBackend: \"%s\" is not a regular file\n",
- path);
- return(-1);
- }
-
- /*
- * Ensure that we are using an authorized backend.
- *
- * XXX I'm open to suggestions here. I would like to enforce ownership
- * of backends by user "postgres" but people seem to like to run
- * as users other than "postgres"...
- */
-
- /*
- * Ensure that the file is both executable and readable (required for
- * dynamic loading).
- *
- * We use the effective uid here because the backend will not have
- * executed setuid() by the time it calls this routine.
- */
- euid = geteuid();
- if (euid == buf.st_uid) {
- is_r = buf.st_mode & S_IRUSR;
- is_x = buf.st_mode & S_IXUSR;
- if (DebugLvl > 1 && !(is_r && is_x))
- fprintf(stderr, "ValidateBackend: \"%s\" is not user read/execute\n",
- path);
- return(is_x ? (is_r ? 0 : -2) : -1);
- }
- pwp = getpwuid(euid);
- if (pwp) {
- if (pwp->pw_gid == buf.st_gid) {
- ++in_grp;
- } else if (pwp->pw_name &&
- (gp = getgrgid(buf.st_gid))) {
- for (i = 0; gp->gr_mem[i]; ++i) {
- if (!strcmp(gp->gr_mem[i], pwp->pw_name)) {
- ++in_grp;
- break;
- }
- }
+ /*
+ * Ensure that the file exists and is a regular file.
+ *
+ * XXX if you have a broken system where stat() looks at the symlink
+ * instead of the underlying file, you lose.
+ */
+ if (strlen(path) >= MAXPGPATH)
+ {
+ if (DebugLvl > 1)
+ fprintf(stderr, "ValidateBackend: pathname \"%s\" is too long\n",
+ path);
+ return (-1);
}
- if (in_grp) {
- is_r = buf.st_mode & S_IRGRP;
- is_x = buf.st_mode & S_IXGRP;
- if (DebugLvl > 1 && !(is_r && is_x))
- fprintf(stderr, "ValidateBackend: \"%s\" is not group read/execute\n",
- path);
- return(is_x ? (is_r ? 0 : -2) : -1);
+
+ if (stat(path, &buf) < 0)
+ {
+ if (DebugLvl > 1)
+ fprintf(stderr, "ValidateBackend: can't stat \"%s\"\n",
+ path);
+ return (-1);
}
- }
- is_r = buf.st_mode & S_IROTH;
- is_x = buf.st_mode & S_IXOTH;
- if (DebugLvl > 1 && !(is_r && is_x))
- fprintf(stderr, "ValidateBackend: \"%s\" is not other read/execute\n",
- path);
- return(is_x ? (is_r ? 0 : -2) : -1);
+ if (!(buf.st_mode & S_IFREG))
+ {
+ if (DebugLvl > 1)
+ fprintf(stderr, "ValidateBackend: \"%s\" is not a regular file\n",
+ path);
+ return (-1);
+ }
+
+ /*
+ * Ensure that we are using an authorized backend.
+ *
+ * XXX I'm open to suggestions here. I would like to enforce ownership
+ * of backends by user "postgres" but people seem to like to run as
+ * users other than "postgres"...
+ */
+
+ /*
+ * Ensure that the file is both executable and readable (required for
+ * dynamic loading).
+ *
+ * We use the effective uid here because the backend will not have
+ * executed setuid() by the time it calls this routine.
+ */
+ euid = geteuid();
+ if (euid == buf.st_uid)
+ {
+ is_r = buf.st_mode & S_IRUSR;
+ is_x = buf.st_mode & S_IXUSR;
+ if (DebugLvl > 1 && !(is_r && is_x))
+ fprintf(stderr, "ValidateBackend: \"%s\" is not user read/execute\n",
+ path);
+ return (is_x ? (is_r ? 0 : -2) : -1);
+ }
+ pwp = getpwuid(euid);
+ if (pwp)
+ {
+ if (pwp->pw_gid == buf.st_gid)
+ {
+ ++in_grp;
+ }
+ else if (pwp->pw_name &&
+ (gp = getgrgid(buf.st_gid)))
+ {
+ for (i = 0; gp->gr_mem[i]; ++i)
+ {
+ if (!strcmp(gp->gr_mem[i], pwp->pw_name))
+ {
+ ++in_grp;
+ break;
+ }
+ }
+ }
+ if (in_grp)
+ {
+ is_r = buf.st_mode & S_IRGRP;
+ is_x = buf.st_mode & S_IXGRP;
+ if (DebugLvl > 1 && !(is_r && is_x))
+ fprintf(stderr, "ValidateBackend: \"%s\" is not group read/execute\n",
+ path);
+ return (is_x ? (is_r ? 0 : -2) : -1);
+ }
+ }
+ is_r = buf.st_mode & S_IROTH;
+ is_x = buf.st_mode & S_IXOTH;
+ if (DebugLvl > 1 && !(is_r && is_x))
+ fprintf(stderr, "ValidateBackend: \"%s\" is not other read/execute\n",
+ path);
+ return (is_x ? (is_r ? 0 : -2) : -1);
}
/*
* FindBackend -- find an absolute path to a valid backend executable
*
* The reason we have to work so hard to find an absolute path is that
- * we need to feed the backend server the location of its actual
+ * we need to feed the backend server the location of its actual
* executable file -- otherwise, we can't do dynamic loading.
*/
int
FindBackend(char *backend, char *argv0)
{
- char buf[MAXPGPATH + 2];
- char *p;
- char *path, *startp, *endp;
- int pathlen;
-
- /*
- * for the postmaster:
- * First try: use the backend that's located in the same directory
- * as the postmaster, if it was invoked with an explicit path.
- * Presumably the user used an explicit path because it wasn't in
- * PATH, and we don't want to use incompatible executables.
- *
- * This has the neat property that it works for installed binaries,
- * old source trees (obj/support/post{master,gres}) and new marc
- * source trees (obj/post{master,gres}) because they all put the
- * two binaries in the same place.
- *
- * for the backend server:
- * First try: if we're given some kind of path, use it (making sure
- * that a relative path is made absolute before returning it).
- */
- if (argv0 && (p = strrchr(argv0, '/')) && *++p) {
- if (*argv0 == '/' || !getcwd(buf, MAXPGPATH))
- buf[0] = '\0';
- else
- strcat(buf, "/");
- strcat(buf, argv0);
- p = strrchr(buf, '/');
- strcpy(++p, "postgres");
- if (!ValidateBackend(buf)) {
- strncpy(backend, buf, MAXPGPATH);
- if (DebugLvl)
- fprintf(stderr, "FindBackend: found \"%s\" using argv[0]\n",
- backend);
- return(0);
+ char buf[MAXPGPATH + 2];
+ char *p;
+ char *path,
+ *startp,
+ *endp;
+ int pathlen;
+
+ /*
+ * for the postmaster: First try: use the backend that's located in
+ * the same directory as the postmaster, if it was invoked with an
+ * explicit path. Presumably the user used an explicit path because it
+ * wasn't in PATH, and we don't want to use incompatible executables.
+ *
+ * This has the neat property that it works for installed binaries, old
+ * source trees (obj/support/post{master,gres}) and new marc source
+ * trees (obj/post{master,gres}) because they all put the two binaries
+ * in the same place.
+ *
+ * for the backend server: First try: if we're given some kind of path,
+ * use it (making sure that a relative path is made absolute before
+ * returning it).
+ */
+ if (argv0 && (p = strrchr(argv0, '/')) && *++p)
+ {
+ if (*argv0 == '/' || !getcwd(buf, MAXPGPATH))
+ buf[0] = '\0';
+ else
+ strcat(buf, "/");
+ strcat(buf, argv0);
+ p = strrchr(buf, '/');
+ strcpy(++p, "postgres");
+ if (!ValidateBackend(buf))
+ {
+ strncpy(backend, buf, MAXPGPATH);
+ if (DebugLvl)
+ fprintf(stderr, "FindBackend: found \"%s\" using argv[0]\n",
+ backend);
+ return (0);
+ }
+ fprintf(stderr, "FindBackend: invalid backend \"%s\"\n",
+ buf);
+ return (-1);
}
- fprintf(stderr, "FindBackend: invalid backend \"%s\"\n",
- buf);
- return(-1);
- }
-
- /*
- * Second try: since no explicit path was supplied, the user must
- * have been relying on PATH. We'll use the same PATH.
- */
- if ((p = getenv("PATH")) && *p) {
- if (DebugLvl)
- fprintf(stderr, "FindBackend: searching PATH ...\n");
- pathlen = strlen(p);
- path = malloc(pathlen + 1);
- strcpy(path, p);
- for (startp = path, endp = strchr(path, ':');
- startp && *startp;
- startp = endp + 1, endp = strchr(startp, ':')) {
- if (startp == endp) /* it's a "::" */
- continue;
- if (endp)
- *endp = '\0';
- if (*startp == '/' || !getcwd(buf, MAXPGPATH))
- buf[0] = '\0';
- strcat(buf, startp);
- strcat(buf, "/postgres");
- switch (ValidateBackend(buf)) {
- case 0: /* found ok */
- strncpy(backend, buf, MAXPGPATH);
+
+ /*
+ * Second try: since no explicit path was supplied, the user must have
+ * been relying on PATH. We'll use the same PATH.
+ */
+ if ((p = getenv("PATH")) && *p)
+ {
if (DebugLvl)
- fprintf(stderr, "FindBackend: found \"%s\" using PATH\n",
- backend);
- free(path);
- return(0);
- case -1: /* wasn't even a candidate, keep looking */
- break;
- case -2: /* found but disqualified */
- fprintf(stderr, "FindBackend: could not read backend \"%s\"\n",
- buf);
+ fprintf(stderr, "FindBackend: searching PATH ...\n");
+ pathlen = strlen(p);
+ path = malloc(pathlen + 1);
+ strcpy(path, p);
+ for (startp = path, endp = strchr(path, ':');
+ startp && *startp;
+ startp = endp + 1, endp = strchr(startp, ':'))
+ {
+ if (startp == endp) /* it's a "::" */
+ continue;
+ if (endp)
+ *endp = '\0';
+ if (*startp == '/' || !getcwd(buf, MAXPGPATH))
+ buf[0] = '\0';
+ strcat(buf, startp);
+ strcat(buf, "/postgres");
+ switch (ValidateBackend(buf))
+ {
+ case 0: /* found ok */
+ strncpy(backend, buf, MAXPGPATH);
+ if (DebugLvl)
+ fprintf(stderr, "FindBackend: found \"%s\" using PATH\n",
+ backend);
+ free(path);
+ return (0);
+ case -1: /* wasn't even a candidate, keep looking */
+ break;
+ case -2: /* found but disqualified */
+ fprintf(stderr, "FindBackend: could not read backend \"%s\"\n",
+ buf);
+ free(path);
+ return (-1);
+ }
+ if (!endp) /* last one */
+ break;
+ }
free(path);
- return(-1);
- }
- if (!endp) /* last one */
- break;
}
- free(path);
- }
- fprintf(stderr, "FindBackend: could not find a backend to execute...\n");
- return(-1);
+ fprintf(stderr, "FindBackend: could not find a backend to execute...\n");
+ return (-1);
}
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index 053bcbb8775..2ed0c83e0b2 100644
--- a/src/backend/utils/init/globals.c
+++ b/src/backend/utils/init/globals.c
@@ -1,17 +1,17 @@
/*-------------------------------------------------------------------------
*
* globals.c--
- * global variable declarations
+ * global variable declarations
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.10 1997/08/14 16:11:21 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.11 1997/09/07 04:53:48 momjian Exp $
*
* NOTES
- * Globals used all over the place should be declared here and not
- * in other modules.
+ * Globals used all over the place should be declared here and not
+ * in other modules.
*
*-------------------------------------------------------------------------
*/
@@ -24,7 +24,7 @@
#include <unistd.h>
#include "postgres.h"
-#include "miscadmin.h" /* where the declarations go */
+#include "miscadmin.h" /* where the declarations go */
#include <storage/backendid.h>
#include "access/heapam.h"
@@ -36,83 +36,86 @@
#include "catalog/catname.h"
-int Portfd = -1;
-int Noversion = 0;
-int Quiet = 1;
+int Portfd = -1;
+int Noversion = 0;
+int Quiet = 1;
-int MasterPid;
-char *DataDir;
- /* The PGDATA directory user says to use, or defaults to via environment
- variable. NULL if no option given and no environment variable set
+int MasterPid;
+char *DataDir;
+
+ /*
+ * The PGDATA directory user says to use, or defaults to via environment
+ * variable. NULL if no option given and no environment variable set
*/
-Relation reldesc; /* current relation descriptor */
-
-char OutputFileName[MAXPGPATH] = "";
+Relation reldesc; /* current relation descriptor */
+
+char OutputFileName[MAXPGPATH] = "";
-BackendId MyBackendId;
-BackendTag MyBackendTag;
+BackendId MyBackendId;
+BackendTag MyBackendTag;
-char *UserName = NULL;
-char *DatabaseName = NULL;
-char *DatabasePath = NULL;
+char *UserName = NULL;
+char *DatabaseName = NULL;
+char *DatabasePath = NULL;
-bool MyDatabaseIdIsInitialized = false;
-Oid MyDatabaseId = InvalidOid;
-bool TransactionInitWasProcessed = false;
+bool MyDatabaseIdIsInitialized = false;
+Oid MyDatabaseId = InvalidOid;
+bool TransactionInitWasProcessed = false;
-bool IsUnderPostmaster = false;
-bool IsPostmaster = false;
+bool IsUnderPostmaster = false;
+bool IsPostmaster = false;
-short DebugLvl = 0;
+short DebugLvl = 0;
-int DateStyle = USE_POSTGRES_DATES;
-bool EuroDates = false;
-bool HasCTZSet = false;
-bool CDayLight = false;
-int CTimeZone = 0;
-char CTZName[MAXTZLEN+1] = "";
+int DateStyle = USE_POSTGRES_DATES;
+bool EuroDates = false;
+bool HasCTZSet = false;
+bool CDayLight = false;
+int CTimeZone = 0;
+char CTZName[MAXTZLEN + 1] = "";
-char DateFormat[20] = "%d-%m-%Y"; /* mjl: sizes! or better malloc? XXX */
-char FloatFormat[20] = "%f";
+char DateFormat[20] = "%d-%m-%Y"; /* mjl: sizes! or better
+ * malloc? XXX */
+char FloatFormat[20] = "%f";
-int fsyncOff = 0;
-int SortMem = 512;
+int fsyncOff = 0;
+int SortMem = 512;
-char *IndexedCatalogNames[] = {
- AttributeRelationName,
- ProcedureRelationName,
- TypeRelationName,
- RelationRelationName,
- 0
+char *IndexedCatalogNames[] = {
+ AttributeRelationName,
+ ProcedureRelationName,
+ TypeRelationName,
+ RelationRelationName,
+ 0
};
/* ----------------
* we just do a linear search now so there's no requirement that the list
- * be ordered. The list is so small it shouldn't make much difference.
+ * be ordered. The list is so small it shouldn't make much difference.
* make sure the list is null-terminated
- * - jolly 8/19/95
- *
+ * - jolly 8/19/95
+ *
* OLD COMMENT
- * WARNING WARNING WARNING WARNING WARNING WARNING
+ * WARNING WARNING WARNING WARNING WARNING WARNING
*
- * keep SharedSystemRelationNames[] in SORTED order! A binary search
- * is done on it in catalog.c!
+ * keep SharedSystemRelationNames[] in SORTED order! A binary search
+ * is done on it in catalog.c!
*
- * XXX this is a serious hack which should be fixed -cim 1/26/90
+ * XXX this is a serious hack which should be fixed -cim 1/26/90
* ----------------
*/
-char *SharedSystemRelationNames[] = {
- DatabaseRelationName,
- DefaultsRelationName,
- DemonRelationName,
- GroupRelationName,
- HostsRelationName,
- LogRelationName,
- MagicRelationName,
- ServerRelationName,
- TimeRelationName,
- UserRelationName,
- VariableRelationName,
- 0
+char *SharedSystemRelationNames[] = {
+ DatabaseRelationName,
+ DefaultsRelationName,
+ DemonRelationName,
+ GroupRelationName,
+ HostsRelationName,
+ LogRelationName,
+ MagicRelationName,
+ ServerRelationName,
+ TimeRelationName,
+ UserRelationName,
+ VariableRelationName,
+ 0
};
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 2fe002bd914..71efb80d120 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -1,55 +1,55 @@
/*-------------------------------------------------------------------------
*
* miscinit.c--
- * miscellanious initialization support stuff
+ * miscellanious initialization support stuff
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.6 1997/08/19 21:35:44 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.7 1997/09/07 04:53:49 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <string.h>
-#include <sys/param.h> /* for MAXPATHLEN */
+#include <sys/param.h> /* for MAXPATHLEN */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <stdio.h>
#include <unistd.h>
-#include <grp.h> /* for getgrgid */
-#include <pwd.h> /* for getpwuid */
+#include <grp.h> /* for getgrgid */
+#include <pwd.h> /* for getpwuid */
#include "postgres.h"
-#include "utils/portal.h" /* for EnablePortalManager, etc. */
-#include "utils/exc.h" /* for EnableExceptionHandling, etc. */
-#include "utils/mcxt.h" /* for EnableMemoryContext, etc. */
+#include "utils/portal.h" /* for EnablePortalManager, etc. */
+#include "utils/exc.h" /* for EnableExceptionHandling, etc. */
+#include "utils/mcxt.h" /* for EnableMemoryContext, etc. */
#include "utils/elog.h"
#include "utils/builtins.h"
-#include "miscadmin.h" /* where the declarations go */
+#include "miscadmin.h" /* where the declarations go */
#include "catalog/catname.h"
#include "catalog/pg_user.h"
#include "catalog/pg_proc.h"
#include "utils/syscache.h"
-#include "storage/fd.h" /* for O_ */
+#include "storage/fd.h" /* for O_ */
/*
* EnableAbortEnvVarName --
- * Enables system abort iff set to a non-empty string in environment.
+ * Enables system abort iff set to a non-empty string in environment.
*/
#define EnableAbortEnvVarName "POSTGRESABORT"
-extern char *getenv(const char *name); /* XXX STDLIB */
+extern char *getenv(const char *name); /* XXX STDLIB */
-/* from globals.c */
-extern char *DatabaseName;
-extern char *UserName;
-extern char *DatabasePath;
+/* from globals.c */
+extern char *DatabaseName;
+extern char *UserName;
+extern char *DatabasePath;
/*
@@ -59,282 +59,286 @@ extern char *DatabasePath;
#define USE_ENVIRONMENT
/* ----------------------------------------------------------------
- * some of the 19 ways to leave postgres
+ * some of the 19 ways to leave postgres
* ----------------------------------------------------------------
*/
/*
* ExitPostgres --
- * Exit POSTGRES with a status code.
+ * Exit POSTGRES with a status code.
*
* Note:
- * This function never returns.
- * ...
+ * This function never returns.
+ * ...
*
* Side effects:
- * ...
+ * ...
*
* Exceptions:
- * none
+ * none
*/
void
ExitPostgres(ExitStatus status)
{
#ifdef __SABER__
- saber_stop();
+ saber_stop();
#endif
- exitpg(status);
+ exitpg(status);
}
/*
* AbortPostgres --
- * Abort POSTGRES dumping core.
+ * Abort POSTGRES dumping core.
*
* Note:
- * This function never returns.
- * ...
+ * This function never returns.
+ * ...
*
* Side effects:
- * Core is dumped iff EnableAbortEnvVarName is set to a non-empty string.
- * ...
+ * Core is dumped iff EnableAbortEnvVarName is set to a non-empty string.
+ * ...
*
* Exceptions:
- * none
+ * none
*/
#ifdef NOT_USED
void
AbortPostgres()
{
- char *abortValue = getenv(EnableAbortEnvVarName);
-
+ char *abortValue = getenv(EnableAbortEnvVarName);
+
#ifdef __SABER__
- saber_stop();
+ saber_stop();
#endif
-
- if (PointerIsValid(abortValue) && abortValue[0] != '\0')
- abort();
- else
- exitpg(FatalExitStatus);
+
+ if (PointerIsValid(abortValue) && abortValue[0] != '\0')
+ abort();
+ else
+ exitpg(FatalExitStatus);
}
+
#endif
/* ----------------
- * StatusBackendExit
+ * StatusBackendExit
* ----------------
*/
void
StatusBackendExit(int status)
{
- /* someday, do some real cleanup and then call the LISP exit */
- /* someday, call StatusPostmasterExit if running without postmaster */
- exitpg(status);
+ /* someday, do some real cleanup and then call the LISP exit */
+ /* someday, call StatusPostmasterExit if running without postmaster */
+ exitpg(status);
}
/* ----------------
- * StatusPostmasterExit
+ * StatusPostmasterExit
* ----------------
*/
void
StatusPostmasterExit(int status)
{
- /* someday, do some real cleanup and then call the LISP exit */
- exitpg(status);
+ /* someday, do some real cleanup and then call the LISP exit */
+ exitpg(status);
}
/* ----------------------------------------------------------------
- * processing mode support stuff (used to be in pmod.c)
+ * processing mode support stuff (used to be in pmod.c)
* ----------------------------------------------------------------
*/
-static ProcessingMode Mode = NoProcessing;
+static ProcessingMode Mode = NoProcessing;
/*
* IsNoProcessingMode --
- * True iff processing mode is NoProcessing.
+ * True iff processing mode is NoProcessing.
*/
bool
IsNoProcessingMode()
{
- return ((bool)(Mode == NoProcessing));
+ return ((bool) (Mode == NoProcessing));
}
/*
* IsBootstrapProcessingMode --
- * True iff processing mode is BootstrapProcessing.
+ * True iff processing mode is BootstrapProcessing.
*/
bool
IsBootstrapProcessingMode()
{
- return ((bool)(Mode == BootstrapProcessing));
+ return ((bool) (Mode == BootstrapProcessing));
}
/*
* IsInitProcessingMode --
- * True iff processing mode is InitProcessing.
+ * True iff processing mode is InitProcessing.
*/
bool
IsInitProcessingMode()
{
- return ((bool)(Mode == InitProcessing));
+ return ((bool) (Mode == InitProcessing));
}
/*
* IsNormalProcessingMode --
- * True iff processing mode is NormalProcessing.
+ * True iff processing mode is NormalProcessing.
*/
bool
IsNormalProcessingMode()
{
- return ((bool)(Mode == NormalProcessing));
+ return ((bool) (Mode == NormalProcessing));
}
/*
* SetProcessingMode --
- * Sets mode of processing as specified.
+ * Sets mode of processing as specified.
*
* Exceptions:
- * BadArg if called with invalid mode.
+ * BadArg if called with invalid mode.
*
* Note:
- * Mode is NoProcessing before the first time this is called.
+ * Mode is NoProcessing before the first time this is called.
*/
void
SetProcessingMode(ProcessingMode mode)
{
- AssertArg(mode == NoProcessing || mode == BootstrapProcessing ||
- mode == InitProcessing || mode == NormalProcessing);
-
- Mode = mode;
+ AssertArg(mode == NoProcessing || mode == BootstrapProcessing ||
+ mode == InitProcessing || mode == NormalProcessing);
+
+ Mode = mode;
}
ProcessingMode
GetProcessingMode()
{
- return (Mode);
+ return (Mode);
}
/* ----------------------------------------------------------------
- * database path / name support stuff
+ * database path / name support stuff
* ----------------------------------------------------------------
*/
/*
* GetDatabasePath --
- * Returns path to database.
+ * Returns path to database.
*
*/
-char*
+char *
GetDatabasePath()
{
- return DatabasePath;
+ return DatabasePath;
}
/*
* GetDatabaseName --
- * Returns name of database.
+ * Returns name of database.
*/
-char*
+char *
GetDatabaseName()
{
- return DatabaseName;
+ return DatabaseName;
}
void
SetDatabasePath(char *path)
{
- /* use malloc since this is done before memory contexts are set up */
- if (DatabasePath)
- free(DatabasePath);
- DatabasePath = malloc(strlen(path)+1);
- strcpy(DatabasePath, path);
+ /* use malloc since this is done before memory contexts are set up */
+ if (DatabasePath)
+ free(DatabasePath);
+ DatabasePath = malloc(strlen(path) + 1);
+ strcpy(DatabasePath, path);
}
void
SetDatabaseName(char *name)
{
- if (DatabaseName)
- free (DatabaseName);
- DatabaseName = malloc(strlen(name)+1);
- strcpy(DatabaseName, name);
+ if (DatabaseName)
+ free(DatabaseName);
+ DatabaseName = malloc(strlen(name) + 1);
+ strcpy(DatabaseName, name);
}
/* ----------------
- * GetPgUserName and SetPgUserName
+ * GetPgUserName and SetPgUserName
*
- * SetPgUserName must be called before InitPostgres, since the setuid()
- * is done there.
+ * SetPgUserName must be called before InitPostgres, since the setuid()
+ * is done there.
*
- * Replace GetPgUserName() with a lower-case version
- * to allow use in new case-insensitive SQL (referenced
- * in pg_proc.h). Define GetPgUserName() as a macro - tgl 97/04/26
+ * Replace GetPgUserName() with a lower-case version
+ * to allow use in new case-insensitive SQL (referenced
+ * in pg_proc.h). Define GetPgUserName() as a macro - tgl 97/04/26
* ----------------
*/
-char*
+char *
getpgusername()
{
- return UserName;
+ return UserName;
}
void
SetPgUserName()
{
#ifndef NO_SECURITY
- char *p;
- struct passwd *pw;
-
- if (IsUnderPostmaster) {
- /* use the (possibly) authenticated name that's provided */
- if (!(p = getenv("PG_USER")))
- elog(FATAL, "SetPgUserName: PG_USER environment variable unset");
- } else {
- /* setuid() has not yet been done, see above comment */
- if (!(pw = getpwuid(geteuid())))
- elog(FATAL, "SetPgUserName: no entry in passwd file");
- p = pw->pw_name;
- }
- if (UserName)
- free(UserName);
- UserName = malloc(strlen(p)+1);
- strcpy(UserName, p);
-#endif /* NO_SECURITY */
+ char *p;
+ struct passwd *pw;
+
+ if (IsUnderPostmaster)
+ {
+ /* use the (possibly) authenticated name that's provided */
+ if (!(p = getenv("PG_USER")))
+ elog(FATAL, "SetPgUserName: PG_USER environment variable unset");
+ }
+ else
+ {
+ /* setuid() has not yet been done, see above comment */
+ if (!(pw = getpwuid(geteuid())))
+ elog(FATAL, "SetPgUserName: no entry in passwd file");
+ p = pw->pw_name;
+ }
+ if (UserName)
+ free(UserName);
+ UserName = malloc(strlen(p) + 1);
+ strcpy(UserName, p);
+#endif /* NO_SECURITY */
}
/* ----------------------------------------------------------------
- * GetUserId and SetUserId
+ * GetUserId and SetUserId
* ----------------------------------------------------------------
*/
-static Oid UserId = InvalidOid;
+static Oid UserId = InvalidOid;
Oid
GetUserId()
{
- Assert(OidIsValid(UserId));
- return(UserId);
+ Assert(OidIsValid(UserId));
+ return (UserId);
}
void
SetUserId()
{
- HeapTuple userTup;
- char *userName;
-
- Assert(!OidIsValid(UserId)); /* only once */
-
- /*
- * Don't do scans if we're bootstrapping, none of the system
- * catalogs exist yet, and they should be owned by postgres
- * anyway.
- */
- if (IsBootstrapProcessingMode()) {
- UserId = geteuid();
- return;
- }
-
- userName = GetPgUserName();
- userTup = SearchSysCacheTuple(USENAME, PointerGetDatum(userName),
- 0,0,0);
- if (!HeapTupleIsValid(userTup))
- elog(FATAL, "SetUserId: user \"%s\" is not in \"%s\"",
- userName,
- UserRelationName);
- UserId = (Oid) ((Form_pg_user) GETSTRUCT(userTup))->usesysid;
+ HeapTuple userTup;
+ char *userName;
+
+ Assert(!OidIsValid(UserId));/* only once */
+
+ /*
+ * Don't do scans if we're bootstrapping, none of the system catalogs
+ * exist yet, and they should be owned by postgres anyway.
+ */
+ if (IsBootstrapProcessingMode())
+ {
+ UserId = geteuid();
+ return;
+ }
+
+ userName = GetPgUserName();
+ userTup = SearchSysCacheTuple(USENAME, PointerGetDatum(userName),
+ 0, 0, 0);
+ if (!HeapTupleIsValid(userTup))
+ elog(FATAL, "SetUserId: user \"%s\" is not in \"%s\"",
+ userName,
+ UserRelationName);
+ UserId = (Oid) ((Form_pg_user) GETSTRUCT(userTup))->usesysid;
}
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index fb21e51a492..5a8a1a83d4d 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -1,28 +1,28 @@
/*-------------------------------------------------------------------------
*
* postinit.c--
- * postgres initialization utilities
+ * postgres initialization utilities
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.12 1997/08/27 03:48:39 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.13 1997/09/07 04:53:50 momjian Exp $
*
* NOTES
- * InitPostgres() is the function called from PostgresMain
- * which does all non-trival initialization, mainly by calling
- * all the other initialization functions. InitPostgres()
- * is only used within the "postgres" backend and so that routine
- * is in tcop/postgres.c InitPostgres() is needed in cinterface.a
- * because things like the bootstrap backend program need it. Hence
- * you find that in this file...
+ * InitPostgres() is the function called from PostgresMain
+ * which does all non-trival initialization, mainly by calling
+ * all the other initialization functions. InitPostgres()
+ * is only used within the "postgres" backend and so that routine
+ * is in tcop/postgres.c InitPostgres() is needed in cinterface.a
+ * because things like the bootstrap backend program need it. Hence
+ * you find that in this file...
*
- * If you feel the need to add more initialization code, it should be
- * done in InitPostgres() or someplace lower. Do not start
- * putting stuff in PostgresMain - if you do then someone
- * will have to clean it up later, and it's not going to be me!
- * -cim 10/3/90
+ * If you feel the need to add more initialization code, it should be
+ * done in InitPostgres() or someplace lower. Do not start
+ * putting stuff in PostgresMain - if you do then someone
+ * will have to clean it up later, and it's not going to be me!
+ * -cim 10/3/90
*
*-------------------------------------------------------------------------
*/
@@ -47,22 +47,22 @@
#include "access/heapam.h"
#include "access/xact.h"
#include "storage/bufmgr.h"
-#include "access/transam.h" /* XXX dependency problem */
+#include "access/transam.h" /* XXX dependency problem */
#include "utils/tqual.h"
#include "utils/syscache.h"
-#include "storage/bufpage.h" /* for page layout, for InitMyDatabaseId() */
+#include "storage/bufpage.h" /* for page layout, for InitMyDatabaseId() */
#include "storage/sinval.h"
#include "storage/sinvaladt.h"
#include "storage/lmgr.h"
-#include "miscadmin.h" /* for global decls */
-#include "utils/portal.h" /* for EnablePortalManager, etc. */
+#include "miscadmin.h" /* for global decls */
+#include "utils/portal.h" /* for EnablePortalManager, etc. */
-#include "utils/exc.h" /* for EnableExceptionHandling, etc. */
-#include "fmgr.h" /* for EnableDynamicFunctionManager, etc. */
+#include "utils/exc.h" /* for EnableExceptionHandling, etc. */
+#include "fmgr.h" /* for EnableDynamicFunctionManager, etc. */
#include "utils/elog.h"
#include "utils/palloc.h"
-#include "utils/mcxt.h" /* for EnableMemoryContext, etc. */
+#include "utils/mcxt.h" /* for EnableMemoryContext, etc. */
#include "catalog/catname.h"
#include "catalog/pg_database.h"
@@ -70,584 +70,612 @@
#include "port-protos.h"
#include "libpq/libpq-be.h"
-static void InitCommunication(void);
-static void InitMyDatabaseId(void);
-static void InitStdio(void);
-static void InitUserid(void);
+static void InitCommunication(void);
+static void InitMyDatabaseId(void);
+static void InitStdio(void);
+static void InitUserid(void);
-static IPCKey PostgresIpcKey;
+static IPCKey PostgresIpcKey;
/* ----------------------------------------------------------------
- * InitPostgres support
+ * InitPostgres support
* ----------------------------------------------------------------
*/
/* --------------------------------
- * InitMyDatabaseId() -- Find and record the OID of the database we are
- * to open.
+ * InitMyDatabaseId() -- Find and record the OID of the database we are
+ * to open.
*
- * The database's oid forms half of the unique key for the system
- * caches and lock tables. We therefore want it initialized before
- * we open any relations, since opening relations puts things in the
- * cache. To get around this problem, this code opens and scans the
- * pg_database relation by hand.
+ * The database's oid forms half of the unique key for the system
+ * caches and lock tables. We therefore want it initialized before
+ * we open any relations, since opening relations puts things in the
+ * cache. To get around this problem, this code opens and scans the
+ * pg_database relation by hand.
*
- * This algorithm relies on the fact that first attribute in the
- * pg_database relation schema is the database name. It also knows
- * about the internal format of tuples on disk and the length of
- * the datname attribute. It knows the location of the pg_database
- * file.
+ * This algorithm relies on the fact that first attribute in the
+ * pg_database relation schema is the database name. It also knows
+ * about the internal format of tuples on disk and the length of
+ * the datname attribute. It knows the location of the pg_database
+ * file.
*
- * This code is called from InitDatabase(), after we chdir() to the
- * database directory but before we open any relations.
+ * This code is called from InitDatabase(), after we chdir() to the
+ * database directory but before we open any relations.
* --------------------------------
*/
static void
InitMyDatabaseId()
{
- int dbfd;
- int fileflags;
- int nbytes;
- int max, i;
- HeapTuple tup;
- Page pg;
- PageHeader ph;
- char *dbfname;
- Form_pg_database tup_db;
-
- /*
- * At bootstrap time, we don't need to check the oid of the database
- * in use, since we're not using shared memory. This is lucky, since
- * the database may not be in the tables yet.
- */
-
- if (IsBootstrapProcessingMode()) {
- LockDisable(true);
- return;
- }
-
- dbfname = (char *) palloc(strlen(DataDir) + strlen("pg_database") + 2);
- sprintf(dbfname, "%s%cpg_database", DataDir, SEP_CHAR);
- fileflags = O_RDONLY;
-
- if ((dbfd = open(dbfname, O_RDONLY, 0)) < 0)
- elog(FATAL, "Cannot open %s", dbfname);
-
- pfree(dbfname);
-
- /* ----------------
- * read and examine every page in pg_database
- *
- * Raw I/O! Read those tuples the hard way! Yow!
- *
- * Why don't we use the access methods or move this code
- * someplace else? This is really pg_database schema dependent
- * code. Perhaps it should go in lib/catalog/pg_database?
- * -cim 10/3/90
- *
- * mao replies 4 apr 91: yeah, maybe this should be moved to
- * lib/catalog. however, we CANNOT use the access methods since
- * those use the buffer cache, which uses the relation cache, which
- * requires that the dbid be set, which is what we're trying to do
- * here.
- * ----------------
- */
- pg = (Page) palloc(BLCKSZ);
- ph = (PageHeader) pg;
-
- while ((nbytes = read(dbfd, pg, BLCKSZ)) == BLCKSZ) {
- max = PageGetMaxOffsetNumber(pg);
-
- /* look at each tuple on the page */
- for (i = 0; i <= max; i++) {
- int offset;
-
- /* if it's a freed tuple, ignore it */
- if (!(ph->pd_linp[i].lp_flags & LP_USED))
- continue;
-
- /* get a pointer to the tuple itself */
- offset = (int) ph->pd_linp[i].lp_off;
- tup = (HeapTuple) (((char *) pg) + offset);
-
- /*
- * if the tuple has been deleted (the database was destroyed),
- * skip this tuple. XXX warning, will robinson: violation of
- * transaction semantics happens right here. we should check
- * to be sure that the xact that deleted this tuple actually
- * committed. only way to do this at init time is to paw over
- * the log relation by hand, too. let's be optimistic.
- *
- * XXX This is an evil type cast. tup->t_xmax is char[5] while
- * TransactionId is struct * { char data[5] }. It works but
- * if data is ever moved and no longer the first field this
- * will be broken!! -mer 11 Nov 1991.
- */
- if (TransactionIdIsValid((TransactionId)tup->t_xmax))
- continue;
-
- /*
- * Okay, see if this is the one we want.
- * XXX 1 july 91: mao and mer discover that tuples now squash
- * t_bits. Why is this?
- *
- * 24 july 92: mer realizes that the t_bits field is only
- * used in the event of null values. If no
- * fields are null we reduce the header size
- * by doing the squash. t_hoff tells you exactly
- * how big the header actually is. use the PC
- * means of getting at sys cat attrs.
- */
- tup_db = (Form_pg_database)GETSTRUCT(tup);
-
- if (strncmp(GetDatabaseName(),
- &(tup_db->datname.data[0]),
- 16) == 0)
- {
- MyDatabaseId = tup->t_oid;
- goto done;
- }
- }
- }
-
- done:
- close(dbfd);
- pfree(pg);
-
- if (!OidIsValid(MyDatabaseId))
- elog(FATAL,
- "Database %s does not exist in %s",
- GetDatabaseName(),
- DatabaseRelationName);
+ int dbfd;
+ int fileflags;
+ int nbytes;
+ int max,
+ i;
+ HeapTuple tup;
+ Page pg;
+ PageHeader ph;
+ char *dbfname;
+ Form_pg_database tup_db;
+
+ /*
+ * At bootstrap time, we don't need to check the oid of the database
+ * in use, since we're not using shared memory. This is lucky, since
+ * the database may not be in the tables yet.
+ */
+
+ if (IsBootstrapProcessingMode())
+ {
+ LockDisable(true);
+ return;
+ }
+
+ dbfname = (char *) palloc(strlen(DataDir) + strlen("pg_database") + 2);
+ sprintf(dbfname, "%s%cpg_database", DataDir, SEP_CHAR);
+ fileflags = O_RDONLY;
+
+ if ((dbfd = open(dbfname, O_RDONLY, 0)) < 0)
+ elog(FATAL, "Cannot open %s", dbfname);
+
+ pfree(dbfname);
+
+ /* ----------------
+ * read and examine every page in pg_database
+ *
+ * Raw I/O! Read those tuples the hard way! Yow!
+ *
+ * Why don't we use the access methods or move this code
+ * someplace else? This is really pg_database schema dependent
+ * code. Perhaps it should go in lib/catalog/pg_database?
+ * -cim 10/3/90
+ *
+ * mao replies 4 apr 91: yeah, maybe this should be moved to
+ * lib/catalog. however, we CANNOT use the access methods since
+ * those use the buffer cache, which uses the relation cache, which
+ * requires that the dbid be set, which is what we're trying to do
+ * here.
+ * ----------------
+ */
+ pg = (Page) palloc(BLCKSZ);
+ ph = (PageHeader) pg;
+
+ while ((nbytes = read(dbfd, pg, BLCKSZ)) == BLCKSZ)
+ {
+ max = PageGetMaxOffsetNumber(pg);
+
+ /* look at each tuple on the page */
+ for (i = 0; i <= max; i++)
+ {
+ int offset;
+
+ /* if it's a freed tuple, ignore it */
+ if (!(ph->pd_linp[i].lp_flags & LP_USED))
+ continue;
+
+ /* get a pointer to the tuple itself */
+ offset = (int) ph->pd_linp[i].lp_off;
+ tup = (HeapTuple) (((char *) pg) + offset);
+
+ /*
+ * if the tuple has been deleted (the database was destroyed),
+ * skip this tuple. XXX warning, will robinson: violation of
+ * transaction semantics happens right here. we should check
+ * to be sure that the xact that deleted this tuple actually
+ * committed. only way to do this at init time is to paw over
+ * the log relation by hand, too. let's be optimistic.
+ *
+ * XXX This is an evil type cast. tup->t_xmax is char[5] while
+ * TransactionId is struct * { char data[5] }. It works but
+ * if data is ever moved and no longer the first field this
+ * will be broken!! -mer 11 Nov 1991.
+ */
+ if (TransactionIdIsValid((TransactionId) tup->t_xmax))
+ continue;
+
+ /*
+ * Okay, see if this is the one we want. XXX 1 july 91: mao
+ * and mer discover that tuples now squash t_bits. Why is
+ * this?
+ *
+ * 24 july 92: mer realizes that the t_bits field is only used
+ * in the event of null values. If no fields are null we
+ * reduce the header size by doing the squash. t_hoff tells
+ * you exactly how big the header actually is. use the PC
+ * means of getting at sys cat attrs.
+ */
+ tup_db = (Form_pg_database) GETSTRUCT(tup);
+
+ if (strncmp(GetDatabaseName(),
+ &(tup_db->datname.data[0]),
+ 16) == 0)
+ {
+ MyDatabaseId = tup->t_oid;
+ goto done;
+ }
+ }
+ }
+
+done:
+ close(dbfd);
+ pfree(pg);
+
+ if (!OidIsValid(MyDatabaseId))
+ elog(FATAL,
+ "Database %s does not exist in %s",
+ GetDatabaseName(),
+ DatabaseRelationName);
}
/*
* DoChdirAndInitDatabaseNameAndPath --
- * Set current directory to the database directory for the database
- * named <name>.
- * Also set global variables DatabasePath and DatabaseName to those
- * values. Also check for proper version of database system and
- * database. Exit program via elog() if anything doesn't check out.
+ * Set current directory to the database directory for the database
+ * named <name>.
+ * Also set global variables DatabasePath and DatabaseName to those
+ * values. Also check for proper version of database system and
+ * database. Exit program via elog() if anything doesn't check out.
*
* Arguments:
- * Path and name are invalid if it invalid as a string.
- * Path is "badly formated" if it is not a string containing a path
- * to a writable directory.
- * Name is "badly formated" if it contains more than 16 characters or if
- * it is a bad file name (e.g., it contains a '/' or an 8-bit character).
+ * Path and name are invalid if it invalid as a string.
+ * Path is "badly formated" if it is not a string containing a path
+ * to a writable directory.
+ * Name is "badly formated" if it contains more than 16 characters or if
+ * it is a bad file name (e.g., it contains a '/' or an 8-bit character).
*
* Exceptions:
- * BadState if called more than once.
- * BadArg if both path and name are "badly formated" or invalid.
- * BadArg if path and name are both "inconsistent" and valid.
+ * BadState if called more than once.
+ * BadArg if both path and name are "badly formated" or invalid.
+ * BadArg if path and name are both "inconsistent" and valid.
*
- * This routine is inappropriate in bootstrap mode, since the directories
- * and version files need not exist yet if we're in bootstrap mode.
+ * This routine is inappropriate in bootstrap mode, since the directories
+ * and version files need not exist yet if we're in bootstrap mode.
*/
static void
-DoChdirAndInitDatabaseNameAndPath(char *name) {
- char *reason;
- /* Failure reason returned by some function. NULL if no failure */
- int fd;
- char errormsg[1000];
-
- if ((fd = open(DataDir, O_RDONLY,0)) == -1)
- sprintf(errormsg, "Database system does not exist. "
- "PGDATA directory '%s' not found. Normally, you "
- "create a database system by running initdb.",
- DataDir);
- else {
- char myPath[MAXPGPATH]; /* DatabasePath points here! */
-
- close(fd);
- if (strlen(DataDir) + strlen(name) + 10 > sizeof(myPath))
- sprintf(errormsg, "Internal error in postinit.c: database "
- "pathname exceeds maximum allowable length.");
- else {
- sprintf(myPath, "%s/base/%s", DataDir, name);
-
- if ((fd = open(myPath, O_RDONLY,0)) == -1)
- sprintf(errormsg,
- "Database '%s' does not exist. "
- "(We know this because the directory '%s' "
- "does not exist). You can create a database "
- "with the SQL command CREATE DATABASE. To see "
- "what databases exist, look at the subdirectories "
- "of '%s/base/'.",
- name, myPath, DataDir);
- else {
- close(fd);
- ValidatePgVersion(DataDir, &reason);
- if (reason != NULL)
- sprintf(errormsg,
- "InitPostgres could not validate that the database "
- "system version is compatible with this level of "
- "Postgres. You may need to run initdb to create "
- "a new database system. %s",
- reason);
- else {
- ValidatePgVersion(myPath, &reason);
- if (reason != NULL)
- sprintf(errormsg,
- "InitPostgres could not validate that the "
- "database version is compatible with this level "
- "of Postgres, even though the database system "
- "as a whole appears to be at a compatible level. "
- "You may need to recreate the database with SQL "
- "commands DROP DATABASE and CREATE DATABASE. "
- "%s",
- reason);
- else {
- /* The directories and PG_VERSION files are in order.*/
- int rc; /* return code from some function we call */
-
- SetDatabasePath(myPath);
- SetDatabaseName(name);
- rc = chdir(myPath);
- if (rc < 0)
- sprintf(errormsg,
- "InitPostgres unable to change "
- "current directory to '%s', errno = %s (%d).",
- myPath, strerror(errno), errno);
- else errormsg[0] = '\0';
- }
- }
- }
- }
- }
- if (errormsg[0] != '\0')
- elog(FATAL, errormsg);
- /* Above does not return */
+DoChdirAndInitDatabaseNameAndPath(char *name)
+{
+ char *reason;
+
+ /* Failure reason returned by some function. NULL if no failure */
+ int fd;
+ char errormsg[1000];
+
+ if ((fd = open(DataDir, O_RDONLY, 0)) == -1)
+ sprintf(errormsg, "Database system does not exist. "
+ "PGDATA directory '%s' not found. Normally, you "
+ "create a database system by running initdb.",
+ DataDir);
+ else
+ {
+ char myPath[MAXPGPATH]; /* DatabasePath points
+ * here! */
+
+ close(fd);
+ if (strlen(DataDir) + strlen(name) + 10 > sizeof(myPath))
+ sprintf(errormsg, "Internal error in postinit.c: database "
+ "pathname exceeds maximum allowable length.");
+ else
+ {
+ sprintf(myPath, "%s/base/%s", DataDir, name);
+
+ if ((fd = open(myPath, O_RDONLY, 0)) == -1)
+ sprintf(errormsg,
+ "Database '%s' does not exist. "
+ "(We know this because the directory '%s' "
+ "does not exist). You can create a database "
+ "with the SQL command CREATE DATABASE. To see "
+ "what databases exist, look at the subdirectories "
+ "of '%s/base/'.",
+ name, myPath, DataDir);
+ else
+ {
+ close(fd);
+ ValidatePgVersion(DataDir, &reason);
+ if (reason != NULL)
+ sprintf(errormsg,
+ "InitPostgres could not validate that the database "
+ "system version is compatible with this level of "
+ "Postgres. You may need to run initdb to create "
+ "a new database system. %s",
+ reason);
+ else
+ {
+ ValidatePgVersion(myPath, &reason);
+ if (reason != NULL)
+ sprintf(errormsg,
+ "InitPostgres could not validate that the "
+ "database version is compatible with this level "
+ "of Postgres, even though the database system "
+ "as a whole appears to be at a compatible level. "
+ "You may need to recreate the database with SQL "
+ "commands DROP DATABASE and CREATE DATABASE. "
+ "%s",
+ reason);
+ else
+ {
+
+ /*
+ * The directories and PG_VERSION files are in
+ * order.
+ */
+ int rc; /* return code from some
+ * function we call */
+
+ SetDatabasePath(myPath);
+ SetDatabaseName(name);
+ rc = chdir(myPath);
+ if (rc < 0)
+ sprintf(errormsg,
+ "InitPostgres unable to change "
+ "current directory to '%s', errno = %s (%d).",
+ myPath, strerror(errno), errno);
+ else
+ errormsg[0] = '\0';
+ }
+ }
+ }
+ }
+ }
+ if (errormsg[0] != '\0')
+ elog(FATAL, errormsg);
+ /* Above does not return */
}
/* --------------------------------
- * InitUserid
+ * InitUserid
*
- * initializes crap associated with the user id.
+ * initializes crap associated with the user id.
* --------------------------------
*/
static void
InitUserid()
{
- setuid(geteuid());
- SetUserId();
+ setuid(geteuid());
+ SetUserId();
}
/* --------------------------------
- * InitCommunication
+ * InitCommunication
*
- * This routine initializes stuff needed for ipc, locking, etc.
- * it should be called something more informative.
+ * This routine initializes stuff needed for ipc, locking, etc.
+ * it should be called something more informative.
*
* Note:
- * This does not set MyBackendId. MyBackendTag is set, however.
+ * This does not set MyBackendId. MyBackendTag is set, however.
* --------------------------------
*/
static void
InitCommunication()
{
- char *postid;
- char *postport;
- IPCKey key = 0;
-
- /* ----------------
- * try and get the backend tag from POSTID
- * ----------------
- */
- MyBackendId = -1;
-
- postid = getenv("POSTID");
- if (!PointerIsValid(postid)) {
- MyBackendTag = -1;
- } else {
- MyBackendTag = atoi(postid);
- Assert(MyBackendTag >= 0);
- }
-
- /* ----------------
- * try and get the ipc key from POSTPORT
- * ----------------
- */
- postport = getenv("POSTPORT");
-
- if (PointerIsValid(postport)) {
- SystemPortAddress address = atoi(postport);
-
- if (address == 0)
- elog(FATAL, "InitCommunication: invalid POSTPORT");
-
- if (MyBackendTag == -1)
- elog(FATAL, "InitCommunication: missing POSTID");
-
- key = SystemPortAddressCreateIPCKey(address);
-
- /*
- * Enable this if you are trying to force the backend to run as if it
- * is running under the postmaster.
- *
- * This goto forces Postgres to attach to shared memory instead of
- * using malloc'ed memory (which is the normal behavior if run
- * directly).
- *
- * To enable emulation, run the following shell commands (in addition
- * to enabling this goto)
- *
- * % setenv POSTID 1
- * % setenv POSTPORT 4321
- * % postmaster &
- * % kill -9 %1
- *
- * Upon doing this, Postmaster will have allocated the shared memory
- * resources that Postgres will attach to if you enable
- * EMULATE_UNDER_POSTMASTER.
- *
- * This comment may well age with time - it is current as of
- * 8 January 1990
- *
- * Greg
- */
-
+ char *postid;
+ char *postport;
+ IPCKey key = 0;
+
+ /* ----------------
+ * try and get the backend tag from POSTID
+ * ----------------
+ */
+ MyBackendId = -1;
+
+ postid = getenv("POSTID");
+ if (!PointerIsValid(postid))
+ {
+ MyBackendTag = -1;
+ }
+ else
+ {
+ MyBackendTag = atoi(postid);
+ Assert(MyBackendTag >= 0);
+ }
+
+ /* ----------------
+ * try and get the ipc key from POSTPORT
+ * ----------------
+ */
+ postport = getenv("POSTPORT");
+
+ if (PointerIsValid(postport))
+ {
+ SystemPortAddress address = atoi(postport);
+
+ if (address == 0)
+ elog(FATAL, "InitCommunication: invalid POSTPORT");
+
+ if (MyBackendTag == -1)
+ elog(FATAL, "InitCommunication: missing POSTID");
+
+ key = SystemPortAddressCreateIPCKey(address);
+
+ /*
+ * Enable this if you are trying to force the backend to run as if
+ * it is running under the postmaster.
+ *
+ * This goto forces Postgres to attach to shared memory instead of
+ * using malloc'ed memory (which is the normal behavior if run
+ * directly).
+ *
+ * To enable emulation, run the following shell commands (in addition
+ * to enabling this goto)
+ *
+ * % setenv POSTID 1 % setenv POSTPORT 4321 % postmaster & % kill -9
+ * %1
+ *
+ * Upon doing this, Postmaster will have allocated the shared memory
+ * resources that Postgres will attach to if you enable
+ * EMULATE_UNDER_POSTMASTER.
+ *
+ * This comment may well age with time - it is current as of 8
+ * January 1990
+ *
+ * Greg
+ */
+
#ifdef EMULATE_UNDER_POSTMASTER
-
- goto forcesharedmemory;
-
+
+ goto forcesharedmemory;
+
#endif
-
- } else if (IsUnderPostmaster) {
- elog(FATAL,
- "InitCommunication: under postmaster and POSTPORT not set");
- } else {
- /* ----------------
- * assume we're running a postgres backend by itself with
- * no front end or postmaster.
- * ----------------
- */
- if (MyBackendTag == -1) {
- MyBackendTag = 1;
- }
-
- key = PrivateIPCKey;
- }
-
- /* ----------------
- * initialize shared memory and semaphores appropriately.
- * ----------------
- */
+
+ }
+ else if (IsUnderPostmaster)
+ {
+ elog(FATAL,
+ "InitCommunication: under postmaster and POSTPORT not set");
+ }
+ else
+ {
+ /* ----------------
+ * assume we're running a postgres backend by itself with
+ * no front end or postmaster.
+ * ----------------
+ */
+ if (MyBackendTag == -1)
+ {
+ MyBackendTag = 1;
+ }
+
+ key = PrivateIPCKey;
+ }
+
+ /* ----------------
+ * initialize shared memory and semaphores appropriately.
+ * ----------------
+ */
#ifdef EMULATE_UNDER_POSTMASTER
-
- forcesharedmemory:
-
+
+forcesharedmemory:
+
#endif
-
- PostgresIpcKey = key;
- AttachSharedMemoryAndSemaphores(key);
+
+ PostgresIpcKey = key;
+ AttachSharedMemoryAndSemaphores(key);
}
/* --------------------------------
- * InitStdio
+ * InitStdio
*
- * this routine consists of a bunch of code fragments
- * that used to be randomly scattered through cinit().
- * they all seem to do stuff associated with io.
+ * this routine consists of a bunch of code fragments
+ * that used to be randomly scattered through cinit().
+ * they all seem to do stuff associated with io.
* --------------------------------
*/
static void
InitStdio()
{
- DebugFileOpen();
+ DebugFileOpen();
}
/* --------------------------------
* InitPostgres --
- * Initialize POSTGRES.
+ * Initialize POSTGRES.
*
* Note:
- * Be very careful with the order of calls in the InitPostgres function.
+ * Be very careful with the order of calls in the InitPostgres function.
* --------------------------------
*/
-bool PostgresIsInitialized = false;
-extern int NBuffers;
+bool PostgresIsInitialized = false;
+extern int NBuffers;
/*
- * this global is used by wei for testing his code, but must be declared
- * here rather than in postgres.c so that it's defined for cinterface.a
- * applications.
+ * this global is used by wei for testing his code, but must be declared
+ * here rather than in postgres.c so that it's defined for cinterface.a
+ * applications.
*/
-/*int testFlag = 0;*/
-int lockingOff = 0;
+/*int testFlag = 0;*/
+int lockingOff = 0;
/*
*/
void
-InitPostgres(char *name) /* database name */
+InitPostgres(char *name) /* database name */
{
- bool bootstrap; /* true if BootstrapProcessing */
-
- /* ----------------
- * see if we're running in BootstrapProcessing mode
- * ----------------
- */
- bootstrap = IsBootstrapProcessingMode();
-
- /* ----------------
- * turn on the exception handler. Note: we cannot use elog, Assert,
- * AssertState, etc. until after exception handling is on.
- * ----------------
- */
- EnableExceptionHandling(true);
-
- /* ----------------
- * A stupid check to make sure we don't call this more than once.
- * But things like ReinitPostgres() get around this by just diddling
- * the PostgresIsInitialized flag.
- * ----------------
- */
- AssertState(!PostgresIsInitialized);
-
- /* ----------------
- * Memory system initialization.
- * (we may call palloc after EnableMemoryContext())
- *
- * Note EnableMemoryContext() must happen before EnablePortalManager().
- * ----------------
- */
- EnableMemoryContext(true); /* initializes the "top context" */
- EnablePortalManager(true); /* memory for portal/transaction stuff */
-
- /* ----------------
- * initialize the backend local portal stack used by
- * internal PQ function calls. see src/lib/libpq/be-dumpdata.c
- * This is different from the "portal manager" so this goes here.
- * -cim 2/12/91
- * ----------------
- */
- be_portalinit();
-
- /* ----------------
- * attach to shared memory and semaphores, and initialize our
- * input/output/debugging file descriptors.
- * ----------------
- */
- InitCommunication();
- InitStdio();
-
- /*
- * initialize the local buffer manager
- */
- InitLocalBuffer();
-
- if (!TransactionFlushEnabled())
- on_exitpg(FlushBufferPool, (caddr_t) NULL);
-
- if (bootstrap) {
- SetDatabasePath(".");
- SetDatabaseName(name);
- } else {
- DoChdirAndInitDatabaseNameAndPath(name);
- }
-
- /* ********************************
- * code after this point assumes we are in the proper directory!
- * ********************************
- */
-
- /* ----------------
- * initialize the database id used for system caches and lock tables
- * ----------------
- */
- InitMyDatabaseId();
-
- smgrinit();
-
- /* ----------------
- * initialize the transaction system and the relation descriptor
- * cache. Note we have to make certain the lock manager is off while
- * we do this.
- * ----------------
- */
- AmiTransactionOverride(IsBootstrapProcessingMode());
- LockDisable(true);
-
- /*
- * Part of the initialization processing done here sets a read
- * lock on pg_log. Since locking is disabled the set doesn't have
- * intended effect of locking out writers, but this is ok, since
- * we only lock it to examine AMI transaction status, and this is
- * never written after initdb is done. -mer 15 June 1992
- */
- RelationInitialize(); /* pre-allocated reldescs created here */
- InitializeTransactionSystem(); /* pg_log,etc init/crash recovery here */
-
- LockDisable(false);
-
- /* ----------------
- * anyone knows what this does? something having to do with
- * system catalog cache invalidation in the case of multiple
- * backends, I think -cim 10/3/90
- * Sets up MyBackendId a unique backend identifier.
- * ----------------
- */
- InitSharedInvalidationState();
-
- /* ----------------
- * Set up a per backend process in shared memory. Must be done after
- * InitSharedInvalidationState() as it relies on MyBackendId being
- * initialized already. XXX -mer 11 Aug 1991
- * ----------------
- */
- InitProcess(PostgresIpcKey);
-
- if (MyBackendId > MaxBackendId || MyBackendId <= 0) {
- elog(FATAL, "cinit2: bad backend id %d (%d)",
- MyBackendTag,
- MyBackendId);
- }
-
- /* ----------------
- * initialize the access methods.
- * ----------------
- */
- initam();
-
- /* ----------------
- * initialize all the system catalog caches.
- * ----------------
- */
- zerocaches();
- InitCatalogCache();
-
- /* ----------------
- * set ourselves to the proper user id and figure out our postgres
- * user id. If we ever add security so that we check for valid
- * postgres users, we might do it here.
- * ----------------
- */
- InitUserid();
-
- /* ----------------
- * ok, all done, now let's make sure we don't do it again.
- * ----------------
- */
- PostgresIsInitialized = true;
-/* on_exitpg(DestroyLocalRelList, (caddr_t) NULL); */
-
- /* ----------------
- * Done with "InitPostgres", now change to NormalProcessing unless
- * we're in BootstrapProcessing mode.
- * ----------------
- */
- if (!bootstrap)
- SetProcessingMode(NormalProcessing);
-/* if (testFlag || lockingOff) */
- if (lockingOff)
- LockDisable(true);
-}
+ bool bootstrap; /* true if BootstrapProcessing */
+
+ /* ----------------
+ * see if we're running in BootstrapProcessing mode
+ * ----------------
+ */
+ bootstrap = IsBootstrapProcessingMode();
+
+ /* ----------------
+ * turn on the exception handler. Note: we cannot use elog, Assert,
+ * AssertState, etc. until after exception handling is on.
+ * ----------------
+ */
+ EnableExceptionHandling(true);
+
+ /* ----------------
+ * A stupid check to make sure we don't call this more than once.
+ * But things like ReinitPostgres() get around this by just diddling
+ * the PostgresIsInitialized flag.
+ * ----------------
+ */
+ AssertState(!PostgresIsInitialized);
+
+ /* ----------------
+ * Memory system initialization.
+ * (we may call palloc after EnableMemoryContext())
+ *
+ * Note EnableMemoryContext() must happen before EnablePortalManager().
+ * ----------------
+ */
+ EnableMemoryContext(true); /* initializes the "top context" */
+ EnablePortalManager(true); /* memory for portal/transaction stuff */
+
+ /* ----------------
+ * initialize the backend local portal stack used by
+ * internal PQ function calls. see src/lib/libpq/be-dumpdata.c
+ * This is different from the "portal manager" so this goes here.
+ * -cim 2/12/91
+ * ----------------
+ */
+ be_portalinit();
+
+ /* ----------------
+ * attach to shared memory and semaphores, and initialize our
+ * input/output/debugging file descriptors.
+ * ----------------
+ */
+ InitCommunication();
+ InitStdio();
+
+ /*
+ * initialize the local buffer manager
+ */
+ InitLocalBuffer();
+ if (!TransactionFlushEnabled())
+ on_exitpg(FlushBufferPool, (caddr_t) NULL);
+ if (bootstrap)
+ {
+ SetDatabasePath(".");
+ SetDatabaseName(name);
+ }
+ else
+ {
+ DoChdirAndInitDatabaseNameAndPath(name);
+ }
+
+ /*
+ * ******************************** code after this point assumes we
+ * are in the proper directory! ********************************
+ *
+ */
+
+ /* ----------------
+ * initialize the database id used for system caches and lock tables
+ * ----------------
+ */
+ InitMyDatabaseId();
+
+ smgrinit();
+
+ /* ----------------
+ * initialize the transaction system and the relation descriptor
+ * cache. Note we have to make certain the lock manager is off while
+ * we do this.
+ * ----------------
+ */
+ AmiTransactionOverride(IsBootstrapProcessingMode());
+ LockDisable(true);
+
+ /*
+ * Part of the initialization processing done here sets a read lock on
+ * pg_log. Since locking is disabled the set doesn't have intended
+ * effect of locking out writers, but this is ok, since we only lock
+ * it to examine AMI transaction status, and this is never written
+ * after initdb is done. -mer 15 June 1992
+ */
+ RelationInitialize(); /* pre-allocated reldescs created here */
+ InitializeTransactionSystem(); /* pg_log,etc init/crash recovery
+ * here */
+
+ LockDisable(false);
+
+ /* ----------------
+ * anyone knows what this does? something having to do with
+ * system catalog cache invalidation in the case of multiple
+ * backends, I think -cim 10/3/90
+ * Sets up MyBackendId a unique backend identifier.
+ * ----------------
+ */
+ InitSharedInvalidationState();
+
+ /* ----------------
+ * Set up a per backend process in shared memory. Must be done after
+ * InitSharedInvalidationState() as it relies on MyBackendId being
+ * initialized already. XXX -mer 11 Aug 1991
+ * ----------------
+ */
+ InitProcess(PostgresIpcKey);
+
+ if (MyBackendId > MaxBackendId || MyBackendId <= 0)
+ {
+ elog(FATAL, "cinit2: bad backend id %d (%d)",
+ MyBackendTag,
+ MyBackendId);
+ }
+
+ /* ----------------
+ * initialize the access methods.
+ * ----------------
+ */
+ initam();
+
+ /* ----------------
+ * initialize all the system catalog caches.
+ * ----------------
+ */
+ zerocaches();
+ InitCatalogCache();
+
+ /* ----------------
+ * set ourselves to the proper user id and figure out our postgres
+ * user id. If we ever add security so that we check for valid
+ * postgres users, we might do it here.
+ * ----------------
+ */
+ InitUserid();
+
+ /* ----------------
+ * ok, all done, now let's make sure we don't do it again.
+ * ----------------
+ */
+ PostgresIsInitialized = true;
+/* on_exitpg(DestroyLocalRelList, (caddr_t) NULL); */
+
+ /* ----------------
+ * Done with "InitPostgres", now change to NormalProcessing unless
+ * we're in BootstrapProcessing mode.
+ * ----------------
+ */
+ if (!bootstrap)
+ SetProcessingMode(NormalProcessing);
+/* if (testFlag || lockingOff) */
+ if (lockingOff)
+ LockDisable(true);
+}