Skip to content

Commit d9fd7d1

Browse files
committed
Pass shared memory id and socket descriptor number on command line for
fork/exec.
1 parent e8f4f2f commit d9fd7d1

File tree

6 files changed

+83
-21
lines changed

6 files changed

+83
-21
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.154 2003/05/06 05:15:45 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.155 2003/05/06 23:34:55 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -36,6 +36,7 @@
3636
#include "miscadmin.h"
3737
#include "storage/freespace.h"
3838
#include "storage/ipc.h"
39+
#include "storage/pg_shmem.h"
3940
#include "storage/proc.h"
4041
#include "tcop/tcopprot.h"
4142
#include "utils/builtins.h"
@@ -252,7 +253,7 @@ BootstrapMain(int argc, char *argv[])
252253
* variable */
253254
}
254255

255-
while ((flag = getopt(argc, argv, "B:d:D:Fo:px:")) != -1)
256+
while ((flag = getopt(argc, argv, "B:d:D:Fo:p:x:")) != -1)
256257
{
257258
switch (flag)
258259
{
@@ -283,23 +284,36 @@ BootstrapMain(int argc, char *argv[])
283284
xlogop = atoi(optarg);
284285
break;
285286
case 'p':
287+
{
286288
/* indicates fork from postmaster */
289+
char *p;
290+
#ifdef EXEC_BACKEND
291+
sscanf(optarg, "%d,", &UsedShmemSegID);
292+
p = strchr(optarg, ',');
293+
if (p)
294+
dbname = strdup(p+1);
295+
#else
296+
dbname = strdup(optarg);
297+
#endif
287298
break;
299+
}
288300
case 'B':
289301
SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
290302
break;
291303
default:
292304
usage();
293305
break;
294306
}
295-
} /* while */
307+
}
296308

297-
if (argc - optind != 1)
309+
if (!dbname && argc - optind == 1)
310+
{
311+
dbname = argv[optind];
312+
optind++;
313+
}
314+
if (!dbname || argc != optind)
298315
usage();
299316

300-
dbname = argv[optind];
301-
302-
Assert(dbname);
303317

304318
if (IsUnderPostmaster && ExecBackend && MyProc /* ordinary backend */)
305319
{

src/backend/port/sysv_shmem.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.7 2003/04/24 21:24:36 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.8 2003/05/06 23:34:55 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -34,13 +34,15 @@
3434
#include "storage/ipc.h"
3535
#include "storage/pg_shmem.h"
3636

37-
38-
typedef uint32 IpcMemoryKey; /* shared memory key passed to shmget(2) */
3937
typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
4038

4139
#define IPCProtection (0600) /* access/modify by user only */
4240

4341

42+
#ifdef EXEC_BACKEND
43+
IpcMemoryKey UsedShmemSegID = 0;
44+
#endif
45+
4446
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
4547
static void IpcMemoryDetach(int status, Datum shmaddr);
4648
static void IpcMemoryDelete(int status, Datum shmId);
@@ -300,10 +302,14 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
300302
/* Room for a header? */
301303
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
302304

303-
/* Loop till we find a free IPC key */
304-
NextShmemSegID = port * 1000;
305+
#ifdef EXEC_BACKEND
306+
if (UsedShmemSegID != 0)
307+
NextShmemSegID = UsedShmemSegID;
308+
else
309+
#endif
310+
NextShmemSegID = port * 1000 + 1;
305311

306-
for (NextShmemSegID++;; NextShmemSegID++)
312+
for (;;NextShmemSegID++)
307313
{
308314
IpcMemoryId shmid;
309315

@@ -395,5 +401,10 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
395401
hdr->totalsize = size;
396402
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
397403

404+
#ifdef EXEC_BACKEND
405+
if (!makePrivate && UsedShmemSegID == 0)
406+
UsedShmemSegID = NextShmemSegID;
407+
#endif
408+
398409
return hdr;
399410
}

src/backend/postmaster/postmaster.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.321 2003/05/03 05:13:18 momjian Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.322 2003/05/06 23:34:55 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -97,6 +97,7 @@
9797
#include "nodes/nodes.h"
9898
#include "storage/fd.h"
9999
#include "storage/ipc.h"
100+
#include "storage/pg_shmem.h"
100101
#include "storage/pmsignal.h"
101102
#include "storage/proc.h"
102103
#include "access/xlog.h"
@@ -2214,6 +2215,9 @@ BackendFinalize(Port *port)
22142215
int ac;
22152216
char debugbuf[32];
22162217
char protobuf[32];
2218+
#ifdef EXEC_BACKEND
2219+
char pbuf[NAMEDATALEN + 256];
2220+
#endif
22172221
int i;
22182222
int status;
22192223
struct timeval now;
@@ -2434,8 +2438,14 @@ BackendFinalize(Port *port)
24342438
* database to use. -p marks the end of secure switches.
24352439
*/
24362440
av[ac++] = "-p";
2441+
#ifdef EXEC_BACKEND
2442+
Assert(UsedShmemSegID != 0);
2443+
/* database name at the end because it might contain commas */
2444+
snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%s", port->sock, UsedShmemSegID, port->database_name);
2445+
av[ac++] = pbuf;
2446+
#else
24372447
av[ac++] = port->database_name;
2438-
2448+
#endif
24392449
/*
24402450
* Pass the (insecure) option switches from the connection request.
24412451
* (It's OK to mangle port->cmdline_options now.)
@@ -2712,6 +2722,9 @@ SSDataBase(int xlop)
27122722
int ac = 0;
27132723
char nbbuf[32];
27142724
char xlbuf[32];
2725+
#ifdef EXEC_BACKEND
2726+
char pbuf[NAMEDATALEN + 256];
2727+
#endif
27152728

27162729
#ifdef LINUX_PROFILE
27172730
setitimer(ITIMER_PROF, &prof_itimer, NULL);
@@ -2762,7 +2775,14 @@ SSDataBase(int xlop)
27622775
av[ac++] = xlbuf;
27632776

27642777
av[ac++] = "-p";
2778+
#ifdef EXEC_BACKEND
2779+
Assert(UsedShmemSegID != 0);
2780+
/* database name at the end because it might contain commas */
2781+
snprintf(pbuf, NAMEDATALEN + 256, "%d,%s", UsedShmemSegID, "template1");
2782+
av[ac++] = pbuf;
2783+
#else
27652784
av[ac++] = "template1";
2785+
#endif
27662786

27672787
av[ac] = (char *) NULL;
27682788

src/backend/storage/ipc/shmem.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.67 2002/09/04 20:31:25 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.68 2003/05/06 23:34:55 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -365,8 +365,7 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr)
365365
hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL);
366366
LWLockRelease(ShmemIndexLock);
367367

368-
elog(WARNING, "ShmemInitStruct: cannot allocate '%s'",
369-
name);
368+
elog(WARNING, "ShmemInitStruct: cannot allocate '%s'", name);
370369
*foundPtr = FALSE;
371370
return NULL;
372371
}

src/backend/tcop/postgres.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.337 2003/05/06 21:51:41 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.338 2003/05/06 23:34:55 momjian Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -51,6 +51,7 @@
5151
#include "rewrite/rewriteHandler.h"
5252
#include "storage/freespace.h"
5353
#include "storage/ipc.h"
54+
#include "storage/pg_shmem.h"
5455
#include "storage/proc.h"
5556
#include "tcop/fastpath.h"
5657
#include "tcop/pquery.h"
@@ -2024,7 +2025,18 @@ PostgresMain(int argc, char *argv[], const char *username)
20242025
*/
20252026
if (secure)
20262027
{
2028+
char *p;
2029+
#ifdef EXEC_BACKEND
2030+
sscanf(optarg, "%d,%d,", &MyProcPort->sock, &UsedShmemSegID);
2031+
/* Grab dbname as last param */
2032+
p = strchr(optarg, ',');
2033+
if (p)
2034+
p = strchr(p+1, ',');
2035+
if (p)
2036+
dbname = strdup(p+1);
2037+
#else
20272038
dbname = strdup(optarg);
2039+
#endif
20282040
secure = false; /* subsequent switches are NOT
20292041
* secure */
20302042
ctx = PGC_BACKEND;
@@ -2381,7 +2393,7 @@ PostgresMain(int argc, char *argv[], const char *username)
23812393
if (!IsUnderPostmaster)
23822394
{
23832395
puts("\nPOSTGRES backend interactive interface ");
2384-
puts("$Revision: 1.337 $ $Date: 2003/05/06 21:51:41 $\n");
2396+
puts("$Revision: 1.338 $ $Date: 2003/05/06 23:34:55 $\n");
23852397
}
23862398

23872399
/*

src/include/storage/pg_shmem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
20-
* $Id: pg_shmem.h,v 1.4 2002/09/04 20:31:45 momjian Exp $
20+
* $Id: pg_shmem.h,v 1.5 2003/05/06 23:34:56 momjian Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
2424
#ifndef PG_SHMEM_H
2525
#define PG_SHMEM_H
2626

27+
typedef uint32 IpcMemoryKey; /* shared memory key passed to shmget(2) */
28+
2729
typedef struct PGShmemHeader /* standard header for all Postgres shmem */
2830
{
2931
int32 magic; /* magic # to identify Postgres segments */
@@ -34,6 +36,10 @@ typedef struct PGShmemHeader /* standard header for all Postgres shmem */
3436
} PGShmemHeader;
3537

3638

39+
#ifdef EXEC_BACKEND
40+
extern IpcMemoryKey UsedShmemSegID;
41+
#endif
42+
3743
extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
3844
int port);
3945
extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);

0 commit comments

Comments
 (0)