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.24 2003/10/27 18:30:07 momjian Exp $
13+ * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.25 2003/11/07 21:55:49 tgl Exp $
1414 *
1515 *-------------------------------------------------------------------------
1616 */
@@ -134,7 +134,7 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
134134
135135/* OK, should be able to attach to the segment */
136136#ifdef SHM_SHARE_MMU
137- /* use intimate shared memory onSPARC Solaris */
137+ /* use intimate shared memory on Solaris */
138138memAddress = shmat (shmid ,0 ,SHM_SHARE_MMU );
139139#else
140140memAddress = shmat (shmid ,0 ,0 );
@@ -244,7 +244,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
244244/* Room for a header? */
245245Assert (size > MAXALIGN (sizeof (PGShmemHeader )));
246246
247- /*Just attach and return the pointer */
247+ /*If Exec case, just attach and return the pointer */
248248if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate )
249249{
250250if ((hdr = PGSharedMemoryAttach (UsedShmemSegID ,& shmid ))== NULL )
@@ -253,6 +253,9 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
253253return hdr ;
254254}
255255
256+ /* Make sure PGSharedMemoryAttach doesn't fail without need */
257+ UsedShmemSegAddr = NULL ;
258+
256259/* Loop till we find a free IPC key */
257260NextShmemSegID = port * 1000 ;
258261
@@ -326,16 +329,32 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
326329hdr -> totalsize = size ;
327330hdr -> freeoffset = MAXALIGN (sizeof (PGShmemHeader ));
328331
329-
330- if (ExecBackend && UsedShmemSegAddr == NULL && !makePrivate )
331- {
332- UsedShmemSegAddr = memAddress ;
333- UsedShmemSegID = NextShmemSegID ;
334- }
332+ /* Save info for possible future use */
333+ UsedShmemSegAddr = memAddress ;
334+ UsedShmemSegID = NextShmemSegID ;
335335
336336return hdr ;
337337}
338338
339+ /*
340+ * PGSharedMemoryDetach
341+ *
342+ * Detach from the shared memory segment, if still attached. This is not
343+ * intended for use by the process that originally created the segment
344+ * (it will have an on_shmem_exit callback registered to do that). Rather,
345+ * this is for subprocesses that have inherited an attachment and want to
346+ * get rid of it.
347+ */
348+ void
349+ PGSharedMemoryDetach (void )
350+ {
351+ if (UsedShmemSegAddr != NULL )
352+ {
353+ if (shmdt (UsedShmemSegAddr )< 0 )
354+ elog (LOG ,"shmdt(%p) failed: %m" ,UsedShmemSegAddr );
355+ UsedShmemSegAddr = NULL ;
356+ }
357+ }
339358
340359/*
341360 * Attach to shared memory and make sure it has a Postgres header