From 9bcf350cc71d427bea84ffaca3d4f69015dcc0c8 Mon Sep 17 00:00:00 2001
From: "Marc G. Fournier" <scrappy@hub.org>
Date: Mon, 17 Nov 1997 03:47:31 +0000
Subject: [PATCH] Apply Bryan's IPC Patches

From: Bryan Henderson <bryanh@giraffe.netgate.net>
---
 src/backend/postmaster/postmaster.c | 46 ++++++++++++++++++++++++-----
 src/backend/utils/init/postinit.c   | 29 +++++++++++-------
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 65047f9c8ea..52bc471853f 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.61 1997/11/10 05:10:21 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.62 1997/11/17 03:47:28 scrappy Exp $
  *
  * NOTES
  *
@@ -123,6 +123,32 @@ static Dllist *PortList;
 
 static short PostPortName = -1;
 static short ActiveBackends = FALSE;
+  /* This is a boolean indicating that there is at least one backend
+     that is accessing the current shared memory and semaphores.
+     Between the time that we start up, or throw away shared memory
+     segments and start over, and the time we generate the next
+     backend (because we received a connection request), it is false.
+     Other times, it is true.  
+     */
+static short shmem_seq = 0;
+  /* This is a sequence number that indicates how many times we've had
+     to throw away the shared memory and start over because we doubted 
+     its integrity.  It starts off at zero and is incremented every
+     time we start over.  We use this to ensure that we use a new
+     IPC shared memory key for the new shared memory segment in case
+     the old segment isn't entirely gone yet.
+
+     The sequence actually cycles back to 0 after 9, so pathologically
+     there could be an IPC failure if 10 sets of backends are all stuck
+     and won't release IPC resources.
+     */
+
+static IpcMemoryKey      ipc_key;
+  /* This is the base IPC shared memory key.  Other keys are generated by
+     adding to this.
+     */
+
+
 static int	NextBackendId = MAXINT;		/* XXX why? */
 static char *progname = (char *) NULL;
 
@@ -904,11 +930,11 @@ ConnCreate(int serverFd, int *newFdP)
 static void
 reset_shared(short port)
 {
-	IPCKey		key;
-
-	key = SystemPortAddressCreateIPCKey((SystemPortAddress) port);
-	CreateSharedMemoryAndSemaphores(key);
-	ActiveBackends = FALSE;
+    ipc_key = port * 1000 + shmem_seq * 100;
+    CreateSharedMemoryAndSemaphores(ipc_key);
+    ActiveBackends = FALSE;
+    shmem_seq += 1;
+    if (shmem_seq >= 10) shmem_seq -= 10;
 }
 
 /*
@@ -1079,9 +1105,10 @@ BackendStartup(StartupInfo *packet,		/* client's startup packet */
 	Backend    *bn;				/* for backend cleanup */
 	int			pid,
 				i;
-	static char envEntry[4][2 * ARGV_SIZE];
+#define NR_ENVIRONMENT_VBL 5
+    static char envEntry[NR_ENVIRONMENT_VBL][2 * ARGV_SIZE];
 
-	for (i = 0; i < 4; ++i)
+    for (i = 0; i < NR_ENVIRONMENT_VBL; ++i) 
 	{
 		MemSet(envEntry[i], 0, 2 * ARGV_SIZE);
 	}
@@ -1101,6 +1128,9 @@ BackendStartup(StartupInfo *packet,		/* client's startup packet */
 		sprintf(envEntry[3], "PGDATA=%s", DataDir);
 		putenv(envEntry[3]);
 	}
+        sprintf(envEntry[4], "IPC_KEY=%d", ipc_key);
+        putenv(envEntry[4]);
+
 	if (DebugLvl > 2)
 	{
 		char	  **p;
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 69922388117..beec5b96fee 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.17 1997/11/10 15:15:40 thomas Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.18 1997/11/17 03:47:31 scrappy Exp $
  *
  * NOTES
  *		InitPostgres() is the function called from PostgresMain
@@ -281,8 +281,9 @@ InitUserid()
 static void
 InitCommunication()
 {
-	char	   *postid;
-	char	   *postport;
+        char *postid;       /* value of environment variable */
+        char *postport;     /* value of environment variable */
+        char *ipc_key;       /* value of environemnt variable */
 	IPCKey		key = 0;
 
 	/* ----------------
@@ -302,10 +303,15 @@ InitCommunication()
 		Assert(MyBackendTag >= 0);
 	}
 
-	/* ----------------
-	 *	try and get the ipc key from POSTPORT
-	 * ----------------
-	 */
+
+        ipc_key = getenv("IPC_KEY");
+        if (!PointerIsValid(ipc_key)) {
+            key = -1;
+        } else {
+            key = atoi(ipc_key);
+            Assert(MyBackendTag >= 0);
+        }
+     
 	postport = getenv("POSTPORT");
 
 	if (PointerIsValid(postport))
@@ -315,8 +321,6 @@ InitCommunication()
 		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.
@@ -328,8 +332,11 @@ InitCommunication()
 		 * To enable emulation, run the following shell commands (in addition
 		 * to enabling this goto)
 		 *
-		 * % setenv POSTID 1 % setenv POSTPORT 4321 % postmaster & % kill -9
-		 * %1
+		 * % setenv POSTID 1 
+                 * % setenv POSTPORT 4321 
+                 * % setenv IPC_KEY 4321000
+                 * % postmaster & 
+                 * % kill -9 %1
 		 *
 		 * Upon doing this, Postmaster will have allocated the shared memory
 		 * resources that Postgres will attach to if you enable
-- 
GitLab