From d9fd7d12f6c08f1502e8d57d222797e86198b1a6 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 6 May 2003 23:34:56 +0000
Subject: [PATCH] Pass shared memory id and socket descriptor number on command
 line for fork/exec.

---
 src/backend/bootstrap/bootstrap.c   | 28 +++++++++++++++++++++-------
 src/backend/port/sysv_shmem.c       | 23 +++++++++++++++++------
 src/backend/postmaster/postmaster.c | 24 ++++++++++++++++++++++--
 src/backend/storage/ipc/shmem.c     |  5 ++---
 src/backend/tcop/postgres.c         | 16 ++++++++++++++--
 src/include/storage/pg_shmem.h      |  8 +++++++-
 6 files changed, 83 insertions(+), 21 deletions(-)

diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 4a995889c8e..215e165cf72 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.154 2003/05/06 05:15:45 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.155 2003/05/06 23:34:55 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -36,6 +36,7 @@
 #include "miscadmin.h"
 #include "storage/freespace.h"
 #include "storage/ipc.h"
+#include "storage/pg_shmem.h"
 #include "storage/proc.h"
 #include "tcop/tcopprot.h"
 #include "utils/builtins.h"
@@ -252,7 +253,7 @@ BootstrapMain(int argc, char *argv[])
 												 * variable */
 	}
 
-	while ((flag = getopt(argc, argv, "B:d:D:Fo:px:")) != -1)
+	while ((flag = getopt(argc, argv, "B:d:D:Fo:p:x:")) != -1)
 	{
 		switch (flag)
 		{
@@ -283,8 +284,19 @@ BootstrapMain(int argc, char *argv[])
 				xlogop = atoi(optarg);
 				break;
 			case 'p':
+			{
 				/* indicates fork from postmaster */
+				char *p;
+#ifdef EXEC_BACKEND
+				sscanf(optarg, "%d,", &UsedShmemSegID);
+				p = strchr(optarg, ',');
+				if (p)
+					dbname = strdup(p+1);
+#else
+				dbname = strdup(optarg);
+#endif
 				break;
+			}
 			case 'B':
 				SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
 				break;
@@ -292,14 +304,16 @@ BootstrapMain(int argc, char *argv[])
 				usage();
 				break;
 		}
-	}							/* while */
+	}
 
-	if (argc - optind != 1)
+	if (!dbname && argc - optind == 1)
+	{
+		dbname = argv[optind];
+		optind++;
+	}
+	if (!dbname || argc != optind)
 		usage();
 
-	dbname = argv[optind];
-
-	Assert(dbname);
 
 	if (IsUnderPostmaster && ExecBackend && MyProc /* ordinary backend */)
 	{
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index dfc73bcac88..c98aff5231e 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -10,7 +10,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.7 2003/04/24 21:24:36 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.8 2003/05/06 23:34:55 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -34,13 +34,15 @@
 #include "storage/ipc.h"
 #include "storage/pg_shmem.h"
 
-
-typedef uint32 IpcMemoryKey;	/* shared memory key passed to shmget(2) */
 typedef int IpcMemoryId;		/* shared memory ID returned by shmget(2) */
 
 #define IPCProtection	(0600)	/* access/modify by user only */
 
 
+#ifdef EXEC_BACKEND
+IpcMemoryKey UsedShmemSegID = 0;
+#endif
+
 static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
 static void IpcMemoryDetach(int status, Datum shmaddr);
 static void IpcMemoryDelete(int status, Datum shmId);
@@ -300,10 +302,14 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
 	/* Room for a header? */
 	Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
 
-	/* Loop till we find a free IPC key */
-	NextShmemSegID = port * 1000;
+#ifdef EXEC_BACKEND
+	if (UsedShmemSegID != 0)
+		NextShmemSegID = UsedShmemSegID;
+	else
+#endif
+		NextShmemSegID = port * 1000 + 1;
 
-	for (NextShmemSegID++;; NextShmemSegID++)
+	for (;;NextShmemSegID++)
 	{
 		IpcMemoryId shmid;
 
@@ -395,5 +401,10 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
 	hdr->totalsize = size;
 	hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
 
+#ifdef EXEC_BACKEND
+	if (!makePrivate && UsedShmemSegID == 0)
+		UsedShmemSegID = NextShmemSegID;
+#endif
+
 	return hdr;
 }
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index ea98753bc5c..23c948ce2ba 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.321 2003/05/03 05:13:18 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.322 2003/05/06 23:34:55 momjian Exp $
  *
  * NOTES
  *
@@ -97,6 +97,7 @@
 #include "nodes/nodes.h"
 #include "storage/fd.h"
 #include "storage/ipc.h"
+#include "storage/pg_shmem.h"
 #include "storage/pmsignal.h"
 #include "storage/proc.h"
 #include "access/xlog.h"
@@ -2214,6 +2215,9 @@ BackendFinalize(Port *port)
 	int			ac;
 	char		debugbuf[32];
 	char		protobuf[32];
+#ifdef EXEC_BACKEND
+	char		pbuf[NAMEDATALEN + 256];
+#endif
 	int			i;
 	int			status;
 	struct timeval now;
@@ -2434,8 +2438,14 @@ BackendFinalize(Port *port)
 	 * database to use.  -p marks the end of secure switches.
 	 */
 	av[ac++] = "-p";
+#ifdef EXEC_BACKEND
+	Assert(UsedShmemSegID != 0);
+	/* database name at the end because it might contain commas */
+	snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%s", port->sock, UsedShmemSegID, port->database_name);
+	av[ac++] = pbuf;
+#else
 	av[ac++] = port->database_name;
-
+#endif
 	/*
 	 * Pass the (insecure) option switches from the connection request.
 	 * (It's OK to mangle port->cmdline_options now.)
@@ -2712,6 +2722,9 @@ SSDataBase(int xlop)
 		int			ac = 0;
 		char		nbbuf[32];
 		char		xlbuf[32];
+#ifdef EXEC_BACKEND
+		char		pbuf[NAMEDATALEN + 256];
+#endif
 
 #ifdef LINUX_PROFILE
 		setitimer(ITIMER_PROF, &prof_itimer, NULL);
@@ -2762,7 +2775,14 @@ SSDataBase(int xlop)
 		av[ac++] = xlbuf;
 
 		av[ac++] = "-p";
+#ifdef EXEC_BACKEND
+		Assert(UsedShmemSegID != 0);
+		/* database name at the end because it might contain commas */
+		snprintf(pbuf, NAMEDATALEN + 256, "%d,%s", UsedShmemSegID, "template1");
+		av[ac++] = pbuf;
+#else
 		av[ac++] = "template1";
+#endif
 
 		av[ac] = (char *) NULL;
 
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index ff9a83a684b..66811fab3d8 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.67 2002/09/04 20:31:25 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.68 2003/05/06 23:34:55 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -365,8 +365,7 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr)
 			hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL);
 			LWLockRelease(ShmemIndexLock);
 
-			elog(WARNING, "ShmemInitStruct: cannot allocate '%s'",
-				 name);
+			elog(WARNING, "ShmemInitStruct: cannot allocate '%s'", name);
 			*foundPtr = FALSE;
 			return NULL;
 		}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index c9ba35f7bde..78696cd0771 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.337 2003/05/06 21:51:41 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.338 2003/05/06 23:34:55 momjian Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -51,6 +51,7 @@
 #include "rewrite/rewriteHandler.h"
 #include "storage/freespace.h"
 #include "storage/ipc.h"
+#include "storage/pg_shmem.h"
 #include "storage/proc.h"
 #include "tcop/fastpath.h"
 #include "tcop/pquery.h"
@@ -2024,7 +2025,18 @@ PostgresMain(int argc, char *argv[], const char *username)
 				 */
 				if (secure)
 				{
+					char *p;
+#ifdef EXEC_BACKEND
+					sscanf(optarg, "%d,%d,", &MyProcPort->sock, &UsedShmemSegID);
+					/* Grab dbname as last param */
+					p = strchr(optarg, ',');
+					if (p)
+						p = strchr(p+1, ',');
+					if (p)					
+						dbname = strdup(p+1);
+#else
 					dbname = strdup(optarg);
+#endif
 					secure = false;		/* subsequent switches are NOT
 										 * secure */
 					ctx = PGC_BACKEND;
@@ -2381,7 +2393,7 @@ PostgresMain(int argc, char *argv[], const char *username)
 	if (!IsUnderPostmaster)
 	{
 		puts("\nPOSTGRES backend interactive interface ");
-		puts("$Revision: 1.337 $ $Date: 2003/05/06 21:51:41 $\n");
+		puts("$Revision: 1.338 $ $Date: 2003/05/06 23:34:55 $\n");
 	}
 
 	/*
diff --git a/src/include/storage/pg_shmem.h b/src/include/storage/pg_shmem.h
index c362757c0f6..5f9df7602a1 100644
--- a/src/include/storage/pg_shmem.h
+++ b/src/include/storage/pg_shmem.h
@@ -17,13 +17,15 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_shmem.h,v 1.4 2002/09/04 20:31:45 momjian Exp $
+ * $Id: pg_shmem.h,v 1.5 2003/05/06 23:34:56 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
 #ifndef PG_SHMEM_H
 #define PG_SHMEM_H
 
+typedef uint32 IpcMemoryKey;	/* shared memory key passed to shmget(2) */
+
 typedef struct PGShmemHeader	/* standard header for all Postgres shmem */
 {
 	int32		magic;			/* magic # to identify Postgres segments */
@@ -34,6 +36,10 @@ typedef struct PGShmemHeader	/* standard header for all Postgres shmem */
 } PGShmemHeader;
 
 
+#ifdef EXEC_BACKEND
+extern IpcMemoryKey UsedShmemSegID;
+#endif
+
 extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
 					 int port);
 extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
-- 
GitLab