* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.8 2003/05/06 23:34:55 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.9 2003/05/08 14:49:03 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#define IPCProtection (0600) /* access/modify by user only */
-#ifdef EXEC_BACKEND
IpcMemoryKey UsedShmemSegID = 0;
-#endif
+void *UsedShmemSegAddr = NULL;
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
static void IpcMemoryDetach(int status, Datum shmaddr);
*
* Create a shared memory segment of the given size and initialize its
* standard header. Also, register an on_shmem_exit callback to release
- * the storage.
+ * the storage. For an exec'ed backend, it just attaches.
*
* Dead Postgres segments are recycled if found, but we do not fail upon
* collision with non-Postgres shmem segments. The idea here is to detect and
/* Room for a header? */
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
-#ifdef EXEC_BACKEND
- if (UsedShmemSegID != 0)
+ if (ExecBackend && UsedShmemSegID != 0)
NextShmemSegID = UsedShmemSegID;
else
-#endif
NextShmemSegID = port * 1000 + 1;
for (;;NextShmemSegID++)
break;
}
- /* Try to create new segment */
- memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
- if (memAddress)
- break; /* successful create and attach */
+ /* If attach to fixed address, only try once */
+ if (ExecBackend && UsedShmemSegAddr != NULL && NextShmemSegID != UsedShmemSegID)
+ {
+ fprintf(stderr, "Unable to attach to memory at fixed address: shmget(key=%d, addr=%p) failed: %s\n",
+ (int) UsedShmemSegID, UsedShmemSegAddr, strerror(errno));
+ proc_exit(1);
+ }
+
+ if (!ExecBackend || UsedShmemSegAddr == NULL)
+ {
+ /* Try to create new segment */
+ memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
+ if (memAddress)
+ break; /* successful create and attach */
+ }
/* See if it looks to be leftover from a dead Postgres process */
shmid = shmget(NextShmemSegID, sizeof(PGShmemHeader), 0);
if (shmid < 0)
continue; /* failed: must be some other app's */
-#if defined(solaris) && defined(__sparc__)
/* use intimate shared memory on SPARC Solaris */
- memAddress = shmat(shmid, 0, SHM_SHARE_MMU);
+ memAddress = shmat(shmid, UsedShmemSegAddr,
+#if defined(solaris) && defined(__sparc__)
+ SHM_SHARE_MMU
#else
- memAddress = shmat(shmid, 0, 0);
+ 0
#endif
+ );
if (memAddress == (void *) -1)
continue; /* failed: must be some other app's */
+
hdr = (PGShmemHeader *) memAddress;
if (hdr->magic != PGShmemMagic)
{
continue; /* segment belongs to a non-Postgres app */
}
+ /* Successfully attached to shared memory, which is all we wanted */
+ if (ExecBackend && UsedShmemSegAddr != NULL)
+ break;
+
+ /* Check shared memory and possibly remove and recreate */
+
/*
- * If the creator PID is my own PID or does not belong to any
- * extant process, it's safe to zap it.
+ * If I am not the creator and it belongs to an extant process,
+ * continue.
*/
if (hdr->creatorPID != getpid())
{
- if (kill(hdr->creatorPID, 0) == 0 ||
- errno != ESRCH)
+ if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
{
shmdt(memAddress);
continue; /* segment belongs to a live process */
*/
}
- /*
- * OK, we created a new segment. Mark it as created by this process.
- * The order of assignments here is critical so that another Postgres
- * process can't see the header as valid but belonging to an invalid
- * PID!
- */
hdr = (PGShmemHeader *) memAddress;
- hdr->creatorPID = getpid();
- hdr->magic = PGShmemMagic;
- /*
- * Initialize space allocation status for segment.
- */
- hdr->totalsize = size;
- hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
+ if (!ExecBackend || makePrivate || UsedShmemSegAddr == NULL)
+ {
+ /*
+ * OK, we created a new segment. Mark it as created by this process.
+ * The order of assignments here is critical so that another Postgres
+ * process can't see the header as valid but belonging to an invalid
+ * PID!
+ */
+ hdr->creatorPID = getpid();
+ hdr->magic = PGShmemMagic;
+
+ /*
+ * Initialize space allocation status for segment.
+ */
+ hdr->totalsize = size;
+ hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
+ }
-#ifdef EXEC_BACKEND
- if (!makePrivate && UsedShmemSegID == 0)
+ if (ExecBackend && !makePrivate && UsedShmemSegAddr == NULL)
+ {
+ UsedShmemSegAddr = memAddress;
UsedShmemSegID = NextShmemSegID;
-#endif
+ }
return hdr;
}
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.322 2003/05/06 23:34:55 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.323 2003/05/08 14:49:03 momjian Exp $
*
* NOTES
*
*/
av[ac++] = "-p";
#ifdef EXEC_BACKEND
- Assert(UsedShmemSegID != 0);
+ Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL);
/* database name at the end because it might contain commas */
- snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%s", port->sock, UsedShmemSegID, port->database_name);
+ snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%p,%s", port->sock,
+ UsedShmemSegID, UsedShmemSegAddr, port->database_name);
av[ac++] = pbuf;
#else
av[ac++] = port->database_name;
av[ac++] = "-p";
#ifdef EXEC_BACKEND
- Assert(UsedShmemSegID != 0);
+ Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL);
/* database name at the end because it might contain commas */
- snprintf(pbuf, NAMEDATALEN + 256, "%d,%s", UsedShmemSegID, "template1");
+ snprintf(pbuf, NAMEDATALEN + 256, "%d,%p,%s", UsedShmemSegID,
+ UsedShmemSegAddr, "template1");
av[ac++] = pbuf;
#else
av[ac++] = "template1";