Pass shared memory id and socket descriptor number on command line for
authorBruce Momjian <bruce@momjian.us>
Tue, 6 May 2003 23:34:56 +0000 (23:34 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 6 May 2003 23:34:56 +0000 (23:34 +0000)
fork/exec.

src/backend/bootstrap/bootstrap.c
src/backend/port/sysv_shmem.c
src/backend/postmaster/postmaster.c
src/backend/storage/ipc/shmem.c
src/backend/tcop/postgres.c
src/include/storage/pg_shmem.h

index 4a995889c8eec5abc56c1c97b5b3ecd2ed97ef88..215e165cf72becdf72c2b352329436279e9ef07a 100644 (file)
@@ -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 */)
    {
index dfc73bcac88435ef9e3caeaa06ac95987d75c8a8..c98aff5231eb26e0a0531fe644593a367f2951b4 100644 (file)
@@ -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 $
  *
  *-------------------------------------------------------------------------
  */
 #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;
 }
index ea98753bc5cec06631a2ae6ca48901095ae2c3e6..23c948ce2ba237feed6d67c571216cc35cee284f 100644 (file)
@@ -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;
 
index ff9a83a684be542e6c9433137caa488f6e74a21e..66811fab3d897d5355c1785dddf821d9750ca130 100644 (file)
@@ -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;
        }
index c9ba35f7bde273cd7c8e499a11edde2aa246950e..78696cd0771b82d87903dae270dad5e3cb846049 100644 (file)
@@ -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");
    }
 
    /*
index c362757c0f65483bb349e3f333fbfee1752a5177..5f9df7602a1082bcf8e7c3fef0c06f1deabd4fbe 100644 (file)
  * 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);