Commit d9fd7d12 authored by Bruce Momjian's avatar Bruce Momjian

Pass shared memory id and socket descriptor number on command line for

fork/exec.
parent e8f4f2f9
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 @@ ...@@ -36,6 +36,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "storage/freespace.h" #include "storage/freespace.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "storage/proc.h" #include "storage/proc.h"
#include "tcop/tcopprot.h" #include "tcop/tcopprot.h"
#include "utils/builtins.h" #include "utils/builtins.h"
...@@ -252,7 +253,7 @@ BootstrapMain(int argc, char *argv[]) ...@@ -252,7 +253,7 @@ BootstrapMain(int argc, char *argv[])
* variable */ * 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) switch (flag)
{ {
...@@ -283,8 +284,19 @@ BootstrapMain(int argc, char *argv[]) ...@@ -283,8 +284,19 @@ BootstrapMain(int argc, char *argv[])
xlogop = atoi(optarg); xlogop = atoi(optarg);
break; break;
case 'p': case 'p':
{
/* indicates fork from postmaster */ /* 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; break;
}
case 'B': case 'B':
SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV); SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
break; break;
...@@ -292,14 +304,16 @@ BootstrapMain(int argc, char *argv[]) ...@@ -292,14 +304,16 @@ BootstrapMain(int argc, char *argv[])
usage(); usage();
break; break;
} }
} /* while */ }
if (argc - optind != 1) if (!dbname && argc - optind == 1)
{
dbname = argv[optind];
optind++;
}
if (!dbname || argc != optind)
usage(); usage();
dbname = argv[optind];
Assert(dbname);
if (IsUnderPostmaster && ExecBackend && MyProc /* ordinary backend */) if (IsUnderPostmaster && ExecBackend && MyProc /* ordinary backend */)
{ {
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 @@ ...@@ -34,13 +34,15 @@
#include "storage/ipc.h" #include "storage/ipc.h"
#include "storage/pg_shmem.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) */ typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
#define IPCProtection (0600) /* access/modify by user only */ #define IPCProtection (0600) /* access/modify by user only */
#ifdef EXEC_BACKEND
IpcMemoryKey UsedShmemSegID = 0;
#endif
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size); static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
static void IpcMemoryDetach(int status, Datum shmaddr); static void IpcMemoryDetach(int status, Datum shmaddr);
static void IpcMemoryDelete(int status, Datum shmId); static void IpcMemoryDelete(int status, Datum shmId);
...@@ -300,10 +302,14 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port) ...@@ -300,10 +302,14 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
/* Room for a header? */ /* Room for a header? */
Assert(size > MAXALIGN(sizeof(PGShmemHeader))); Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
/* Loop till we find a free IPC key */ #ifdef EXEC_BACKEND
NextShmemSegID = port * 1000; if (UsedShmemSegID != 0)
NextShmemSegID = UsedShmemSegID;
else
#endif
NextShmemSegID = port * 1000 + 1;
for (NextShmemSegID++;; NextShmemSegID++) for (;;NextShmemSegID++)
{ {
IpcMemoryId shmid; IpcMemoryId shmid;
...@@ -395,5 +401,10 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port) ...@@ -395,5 +401,10 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
hdr->totalsize = size; hdr->totalsize = size;
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader)); hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
#ifdef EXEC_BACKEND
if (!makePrivate && UsedShmemSegID == 0)
UsedShmemSegID = NextShmemSegID;
#endif
return hdr; return hdr;
} }
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * 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 * NOTES
* *
...@@ -97,6 +97,7 @@ ...@@ -97,6 +97,7 @@
#include "nodes/nodes.h" #include "nodes/nodes.h"
#include "storage/fd.h" #include "storage/fd.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h" #include "storage/pmsignal.h"
#include "storage/proc.h" #include "storage/proc.h"
#include "access/xlog.h" #include "access/xlog.h"
...@@ -2214,6 +2215,9 @@ BackendFinalize(Port *port) ...@@ -2214,6 +2215,9 @@ BackendFinalize(Port *port)
int ac; int ac;
char debugbuf[32]; char debugbuf[32];
char protobuf[32]; char protobuf[32];
#ifdef EXEC_BACKEND
char pbuf[NAMEDATALEN + 256];
#endif
int i; int i;
int status; int status;
struct timeval now; struct timeval now;
...@@ -2434,8 +2438,14 @@ BackendFinalize(Port *port) ...@@ -2434,8 +2438,14 @@ BackendFinalize(Port *port)
* database to use. -p marks the end of secure switches. * database to use. -p marks the end of secure switches.
*/ */
av[ac++] = "-p"; 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; av[ac++] = port->database_name;
#endif
/* /*
* Pass the (insecure) option switches from the connection request. * Pass the (insecure) option switches from the connection request.
* (It's OK to mangle port->cmdline_options now.) * (It's OK to mangle port->cmdline_options now.)
...@@ -2712,6 +2722,9 @@ SSDataBase(int xlop) ...@@ -2712,6 +2722,9 @@ SSDataBase(int xlop)
int ac = 0; int ac = 0;
char nbbuf[32]; char nbbuf[32];
char xlbuf[32]; char xlbuf[32];
#ifdef EXEC_BACKEND
char pbuf[NAMEDATALEN + 256];
#endif
#ifdef LINUX_PROFILE #ifdef LINUX_PROFILE
setitimer(ITIMER_PROF, &prof_itimer, NULL); setitimer(ITIMER_PROF, &prof_itimer, NULL);
...@@ -2762,7 +2775,14 @@ SSDataBase(int xlop) ...@@ -2762,7 +2775,14 @@ SSDataBase(int xlop)
av[ac++] = xlbuf; av[ac++] = xlbuf;
av[ac++] = "-p"; 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"; av[ac++] = "template1";
#endif
av[ac] = (char *) NULL; av[ac] = (char *) NULL;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -365,8 +365,7 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr)
hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL); hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL);
LWLockRelease(ShmemIndexLock); LWLockRelease(ShmemIndexLock);
elog(WARNING, "ShmemInitStruct: cannot allocate '%s'", elog(WARNING, "ShmemInitStruct: cannot allocate '%s'", name);
name);
*foundPtr = FALSE; *foundPtr = FALSE;
return NULL; return NULL;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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 * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
#include "rewrite/rewriteHandler.h" #include "rewrite/rewriteHandler.h"
#include "storage/freespace.h" #include "storage/freespace.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "storage/proc.h" #include "storage/proc.h"
#include "tcop/fastpath.h" #include "tcop/fastpath.h"
#include "tcop/pquery.h" #include "tcop/pquery.h"
...@@ -2024,7 +2025,18 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2024,7 +2025,18 @@ PostgresMain(int argc, char *argv[], const char *username)
*/ */
if (secure) 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); dbname = strdup(optarg);
#endif
secure = false; /* subsequent switches are NOT secure = false; /* subsequent switches are NOT
* secure */ * secure */
ctx = PGC_BACKEND; ctx = PGC_BACKEND;
...@@ -2381,7 +2393,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2381,7 +2393,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
puts("\nPOSTGRES backend interactive interface "); 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");
} }
/* /*
......
...@@ -17,13 +17,15 @@ ...@@ -17,13 +17,15 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 #ifndef PG_SHMEM_H
#define 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 */ typedef struct PGShmemHeader /* standard header for all Postgres shmem */
{ {
int32 magic; /* magic # to identify Postgres segments */ int32 magic; /* magic # to identify Postgres segments */
...@@ -34,6 +36,10 @@ typedef struct PGShmemHeader /* standard header for all Postgres shmem */ ...@@ -34,6 +36,10 @@ typedef struct PGShmemHeader /* standard header for all Postgres shmem */
} PGShmemHeader; } PGShmemHeader;
#ifdef EXEC_BACKEND
extern IpcMemoryKey UsedShmemSegID;
#endif
extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate, extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
int port); int port);
extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2); extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment