2002-05-05 00:03:29 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* sysv_shmem.c
|
|
|
|
* Implement shared memory using SysV facilities
|
|
|
|
*
|
|
|
|
* These routines represent a fairly thin layer on top of SysV shared
|
|
|
|
* memory functionality.
|
|
|
|
*
|
2004-08-29 04:13:13 +00:00
|
|
|
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
|
2002-05-05 00:03:29 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-08-29 05:07:03 +00:00
|
|
|
* $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.36 2004/08/29 05:06:44 momjian Exp $
|
2002-05-05 00:03:29 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#ifdef HAVE_SYS_IPC_H
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SHM_H
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_KERNEL_OS_H
|
|
|
|
#include <kernel/OS.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "storage/ipc.h"
|
|
|
|
#include "storage/pg_shmem.h"
|
|
|
|
|
2003-12-01 22:15:38 +00:00
|
|
|
|
|
|
|
typedef key_t IpcMemoryKey; /* shared memory key passed to shmget(2) */
|
2002-05-05 00:03:29 +00:00
|
|
|
typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
|
|
|
|
|
|
|
|
#define IPCProtection (0600) /* access/modify by user only */
|
|
|
|
|
|
|
|
|
2003-12-01 22:15:38 +00:00
|
|
|
unsigned long UsedShmemSegID = 0;
|
2003-08-04 00:43:34 +00:00
|
|
|
void *UsedShmemSegAddr = NULL;
|
2003-05-06 23:34:56 +00:00
|
|
|
|
2002-05-05 00:03:29 +00:00
|
|
|
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
|
|
|
|
static void IpcMemoryDetach(int status, Datum shmaddr);
|
|
|
|
static void IpcMemoryDelete(int status, Datum shmId);
|
2003-05-08 19:17:07 +00:00
|
|
|
static PGShmemHeader *PGSharedMemoryAttach(IpcMemoryKey key,
|
2003-08-04 00:43:34 +00:00
|
|
|
IpcMemoryId *shmid);
|
2002-05-05 00:03:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* InternalIpcMemoryCreate(memKey, size)
|
|
|
|
*
|
|
|
|
* Attempt to create a new shared memory segment with the specified key.
|
|
|
|
* Will fail (return NULL) if such a segment already exists. If successful,
|
|
|
|
* attach the segment to the current process and return its attached address.
|
|
|
|
* On success, callbacks are registered with on_shmem_exit to detach and
|
|
|
|
* delete the segment when on_shmem_exit is called.
|
|
|
|
*
|
|
|
|
* If we fail with a failure code other than collision-with-existing-segment,
|
|
|
|
* print out an error and abort. Other types of errors are not recoverable.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
|
|
|
|
{
|
|
|
|
IpcMemoryId shmid;
|
|
|
|
void *memAddress;
|
|
|
|
|
|
|
|
shmid = shmget(memKey, size, IPC_CREAT | IPC_EXCL | IPCProtection);
|
|
|
|
|
|
|
|
if (shmid < 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Fail quietly if error indicates a collision with existing
|
|
|
|
* segment. One would expect EEXIST, given that we said IPC_EXCL,
|
|
|
|
* but perhaps we could get a permission violation instead? Also,
|
|
|
|
* EIDRM might occur if an old seg is slated for destruction but
|
|
|
|
* not gone yet.
|
|
|
|
*/
|
|
|
|
if (errno == EEXIST || errno == EACCES
|
|
|
|
#ifdef EIDRM
|
|
|
|
|| errno == EIDRM
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Else complain and abort
|
|
|
|
*/
|
2003-07-27 21:49:55 +00:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("could not create shared memory segment: %m"),
|
2004-08-29 05:07:03 +00:00
|
|
|
errdetail("Failed system call was shmget(key=%lu, size=%u, 0%o).",
|
|
|
|
(unsigned long) memKey, size,
|
|
|
|
IPC_CREAT | IPC_EXCL | IPCProtection),
|
2003-07-27 21:49:55 +00:00
|
|
|
(errno == EINVAL) ?
|
|
|
|
errhint("This error usually means that PostgreSQL's request for a shared memory "
|
|
|
|
"segment exceeded your kernel's SHMMAX parameter. You can either "
|
2003-09-29 00:05:25 +00:00
|
|
|
"reduce the request size or reconfigure the kernel with larger SHMMAX. "
|
2004-08-29 05:07:03 +00:00
|
|
|
"To reduce the request size (currently %u bytes), reduce "
|
|
|
|
"PostgreSQL's shared_buffers parameter (currently %d) and/or "
|
2003-07-27 21:49:55 +00:00
|
|
|
"its max_connections parameter (currently %d).\n"
|
|
|
|
"If the request size is already small, it's possible that it is less than "
|
|
|
|
"your kernel's SHMMIN parameter, in which case raising the request size or "
|
|
|
|
"reconfiguring SHMMIN is called for.\n"
|
|
|
|
"The PostgreSQL documentation contains more information about shared "
|
|
|
|
"memory configuration.",
|
|
|
|
size, NBuffers, MaxBackends) : 0,
|
|
|
|
(errno == ENOMEM) ?
|
|
|
|
errhint("This error usually means that PostgreSQL's request for a shared "
|
2003-08-04 00:43:34 +00:00
|
|
|
"memory segment exceeded available memory or swap space. "
|
|
|
|
"To reduce the request size (currently %u bytes), reduce "
|
|
|
|
"PostgreSQL's shared_buffers parameter (currently %d) and/or "
|
2003-07-27 21:49:55 +00:00
|
|
|
"its max_connections parameter (currently %d).\n"
|
|
|
|
"The PostgreSQL documentation contains more information about shared "
|
|
|
|
"memory configuration.",
|
|
|
|
size, NBuffers, MaxBackends) : 0,
|
|
|
|
(errno == ENOSPC) ?
|
|
|
|
errhint("This error does *not* mean that you have run out of disk space. "
|
|
|
|
"It occurs either if all available shared memory IDs have been taken, "
|
|
|
|
"in which case you need to raise the SHMMNI parameter in your kernel, "
|
|
|
|
"or because the system's overall limit for shared memory has been "
|
2003-08-04 00:43:34 +00:00
|
|
|
"reached. If you cannot increase the shared memory limit, "
|
|
|
|
"reduce PostgreSQL's shared memory request (currently %u bytes), "
|
|
|
|
"by reducing its shared_buffers parameter (currently %d) and/or "
|
2003-07-27 21:49:55 +00:00
|
|
|
"its max_connections parameter (currently %d).\n"
|
|
|
|
"The PostgreSQL documentation contains more information about shared "
|
|
|
|
"memory configuration.",
|
|
|
|
size, NBuffers, MaxBackends) : 0));
|
2002-05-05 00:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Register on-exit routine to delete the new segment */
|
|
|
|
on_shmem_exit(IpcMemoryDelete, Int32GetDatum(shmid));
|
|
|
|
|
|
|
|
/* OK, should be able to attach to the segment */
|
2003-10-27 18:30:07 +00:00
|
|
|
#ifdef SHM_SHARE_MMU
|
2003-11-07 21:55:50 +00:00
|
|
|
/* use intimate shared memory on Solaris */
|
2002-05-05 00:03:29 +00:00
|
|
|
memAddress = shmat(shmid, 0, SHM_SHARE_MMU);
|
|
|
|
#else
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
|
|
|
|
#ifdef EXEC_BACKEND
|
|
|
|
memAddress = shmat(shmid, UsedShmemSegAddr, 0);
|
|
|
|
#else
|
2002-09-04 20:31:48 +00:00
|
|
|
memAddress = shmat(shmid, 0, 0);
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
#endif
|
2002-05-05 00:03:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (memAddress == (void *) -1)
|
2003-07-27 21:49:55 +00:00
|
|
|
elog(FATAL, "shmat(id=%d) failed: %m", shmid);
|
2002-05-05 00:03:29 +00:00
|
|
|
|
|
|
|
/* Register on-exit routine to detach new segment before deleting */
|
|
|
|
on_shmem_exit(IpcMemoryDetach, PointerGetDatum(memAddress));
|
|
|
|
|
|
|
|
/* Record key and ID in lockfile for data directory. */
|
|
|
|
RecordSharedMemoryInLockFile((unsigned long) memKey,
|
|
|
|
(unsigned long) shmid);
|
|
|
|
|
|
|
|
return memAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
/* IpcMemoryDetach(status, shmaddr) removes a shared memory segment */
|
|
|
|
/* from process' address spaceq */
|
|
|
|
/* (called as an on_shmem_exit callback, hence funny argument list) */
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
|
|
IpcMemoryDetach(int status, Datum shmaddr)
|
|
|
|
{
|
|
|
|
if (shmdt(DatumGetPointer(shmaddr)) < 0)
|
2003-07-27 21:49:55 +00:00
|
|
|
elog(LOG, "shmdt(%p) failed: %m", DatumGetPointer(shmaddr));
|
2002-05-05 00:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
/* IpcMemoryDelete(status, shmId) deletes a shared memory segment */
|
|
|
|
/* (called as an on_shmem_exit callback, hence funny argument list) */
|
|
|
|
/****************************************************************************/
|
|
|
|
static void
|
|
|
|
IpcMemoryDelete(int status, Datum shmId)
|
|
|
|
{
|
2004-01-07 18:56:30 +00:00
|
|
|
if (shmctl(DatumGetInt32(shmId), IPC_RMID, NULL) < 0)
|
2003-07-27 21:49:55 +00:00
|
|
|
elog(LOG, "shmctl(%d, %d, 0) failed: %m",
|
|
|
|
DatumGetInt32(shmId), IPC_RMID);
|
2002-05-05 00:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PGSharedMemoryIsInUse
|
|
|
|
*
|
|
|
|
* Is a previously-existing shmem segment still existing and in use?
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
|
|
|
|
{
|
|
|
|
IpcMemoryId shmId = (IpcMemoryId) id2;
|
|
|
|
struct shmid_ds shmStat;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We detect whether a shared memory segment is in use by seeing
|
|
|
|
* whether it (a) exists and (b) has any processes are attached to it.
|
|
|
|
*
|
|
|
|
* If we are unable to perform the stat operation for a reason other than
|
|
|
|
* nonexistence of the segment (most likely, because it doesn't belong
|
|
|
|
* to our userid), assume it is in use.
|
|
|
|
*/
|
|
|
|
if (shmctl(shmId, IPC_STAT, &shmStat) < 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* EINVAL actually has multiple possible causes documented in the
|
|
|
|
* shmctl man page, but we assume it must mean the segment no
|
|
|
|
* longer exists.
|
|
|
|
*/
|
|
|
|
if (errno == EINVAL)
|
|
|
|
return false;
|
|
|
|
/* Else assume segment is in use */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/* If it has attached processes, it's in use */
|
|
|
|
if (shmStat.shm_nattch != 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PGSharedMemoryCreate
|
|
|
|
*
|
|
|
|
* Create a shared memory segment of the given size and initialize its
|
|
|
|
* standard header. Also, register an on_shmem_exit callback to release
|
2003-05-08 14:49:04 +00:00
|
|
|
* the storage. For an exec'ed backend, it just attaches.
|
2002-05-05 00:03:29 +00:00
|
|
|
*
|
|
|
|
* Dead Postgres segments are recycled if found, but we do not fail upon
|
2002-09-04 20:31:48 +00:00
|
|
|
* collision with non-Postgres shmem segments. The idea here is to detect and
|
2002-05-05 00:03:29 +00:00
|
|
|
* re-use keys that may have been assigned by a crashed postmaster or backend.
|
|
|
|
*
|
2003-07-14 20:00:23 +00:00
|
|
|
* makePrivate means to always create a new segment, rather than attach to
|
|
|
|
* or recycle any existing segment.
|
|
|
|
*
|
2002-05-05 00:03:29 +00:00
|
|
|
* The port number is passed for possible use as a key (for SysV, we use
|
2002-09-04 20:31:48 +00:00
|
|
|
* it to generate the starting shmem key). In a standalone backend,
|
2002-05-05 00:03:29 +00:00
|
|
|
* zero will be passed.
|
|
|
|
*/
|
|
|
|
PGShmemHeader *
|
|
|
|
PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
|
|
|
|
{
|
|
|
|
IpcMemoryKey NextShmemSegID;
|
|
|
|
void *memAddress;
|
|
|
|
PGShmemHeader *hdr;
|
2003-05-08 19:17:07 +00:00
|
|
|
IpcMemoryId shmid;
|
2002-05-05 00:03:29 +00:00
|
|
|
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
#ifdef EXEC_BACKEND
|
2003-11-07 21:55:50 +00:00
|
|
|
/* If Exec case, just attach and return the pointer */
|
2004-05-06 19:23:25 +00:00
|
|
|
if (UsedShmemSegAddr != NULL && !makePrivate && IsUnderPostmaster)
|
2002-05-05 00:03:29 +00:00
|
|
|
{
|
2004-08-29 05:07:03 +00:00
|
|
|
void *origUsedShmemSegAddr = UsedShmemSegAddr;
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
|
2004-02-25 19:41:23 +00:00
|
|
|
#ifdef __CYGWIN__
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
/* cygipc (currently) appears to not detach on exec. */
|
|
|
|
PGSharedMemoryDetach();
|
|
|
|
UsedShmemSegAddr = origUsedShmemSegAddr;
|
|
|
|
#endif
|
2004-08-29 05:07:03 +00:00
|
|
|
elog(DEBUG3, "Attaching to %p", UsedShmemSegAddr);
|
2003-12-01 22:15:38 +00:00
|
|
|
hdr = PGSharedMemoryAttach((IpcMemoryKey) UsedShmemSegID, &shmid);
|
|
|
|
if (hdr == NULL)
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
elog(FATAL, "could not attach to proper memory at fixed address: shmget(key=%d, addr=%p) failed: %m",
|
|
|
|
(int) UsedShmemSegID, UsedShmemSegAddr);
|
|
|
|
if (hdr != origUsedShmemSegAddr)
|
2004-08-29 05:07:03 +00:00
|
|
|
elog(FATAL, "attaching to shared mem returned unexpected address (got %p, expected %p)",
|
|
|
|
hdr, UsedShmemSegAddr);
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
UsedShmemSegAddr = hdr;
|
2003-05-08 19:17:07 +00:00
|
|
|
return hdr;
|
|
|
|
}
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Room for a header? */
|
|
|
|
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
|
2003-05-08 14:49:04 +00:00
|
|
|
|
2003-11-07 21:55:50 +00:00
|
|
|
/* Make sure PGSharedMemoryAttach doesn't fail without need */
|
|
|
|
UsedShmemSegAddr = NULL;
|
|
|
|
|
2003-07-14 20:00:23 +00:00
|
|
|
/* Loop till we find a free IPC key */
|
|
|
|
NextShmemSegID = port * 1000;
|
2003-05-08 14:49:04 +00:00
|
|
|
|
2003-07-14 20:00:23 +00:00
|
|
|
for (NextShmemSegID++;; NextShmemSegID++)
|
2003-05-08 19:17:07 +00:00
|
|
|
{
|
|
|
|
/* Try to create new segment */
|
|
|
|
memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
|
|
|
|
if (memAddress)
|
|
|
|
break; /* successful create and attach */
|
2003-05-08 14:49:04 +00:00
|
|
|
|
|
|
|
/* Check shared memory and possibly remove and recreate */
|
2003-07-14 20:00:23 +00:00
|
|
|
|
|
|
|
if (makePrivate) /* a standalone backend shouldn't do this */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL)
|
2003-05-08 19:17:07 +00:00
|
|
|
continue; /* can't attach, not one of mine */
|
|
|
|
|
2002-05-05 00:03:29 +00:00
|
|
|
/*
|
2003-05-08 14:49:04 +00:00
|
|
|
* If I am not the creator and it belongs to an extant process,
|
|
|
|
* continue.
|
2002-05-05 00:03:29 +00:00
|
|
|
*/
|
2003-07-14 20:00:23 +00:00
|
|
|
hdr = (PGShmemHeader *) memAddress;
|
2002-05-05 00:03:29 +00:00
|
|
|
if (hdr->creatorPID != getpid())
|
|
|
|
{
|
2004-02-08 22:28:57 +00:00
|
|
|
if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
|
2002-05-05 00:03:29 +00:00
|
|
|
{
|
|
|
|
shmdt(memAddress);
|
|
|
|
continue; /* segment belongs to a live process */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The segment appears to be from a dead Postgres process, or from
|
|
|
|
* a previous cycle of life in this same process. Zap it, if
|
|
|
|
* possible. This probably shouldn't fail, but if it does, assume
|
|
|
|
* the segment belongs to someone else after all, and continue
|
|
|
|
* quietly.
|
|
|
|
*/
|
|
|
|
shmdt(memAddress);
|
2004-01-07 18:56:30 +00:00
|
|
|
if (shmctl(shmid, IPC_RMID, NULL) < 0)
|
2002-05-05 00:03:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now try again to create the segment.
|
|
|
|
*/
|
|
|
|
memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
|
|
|
|
if (memAddress)
|
|
|
|
break; /* successful create and attach */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can only get here if some other process managed to create the
|
|
|
|
* same shmem key before we did. Let him have that one, loop
|
|
|
|
* around to try next key.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2003-05-08 19:17:07 +00:00
|
|
|
/*
|
|
|
|
* 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!
|
|
|
|
*/
|
2002-05-05 00:03:29 +00:00
|
|
|
hdr = (PGShmemHeader *) memAddress;
|
2003-05-08 19:17:07 +00:00
|
|
|
hdr->creatorPID = getpid();
|
|
|
|
hdr->magic = PGShmemMagic;
|
2002-05-05 00:03:29 +00:00
|
|
|
|
2003-05-08 19:17:07 +00:00
|
|
|
/*
|
|
|
|
* Initialize space allocation status for segment.
|
|
|
|
*/
|
|
|
|
hdr->totalsize = size;
|
|
|
|
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
|
2002-05-05 00:03:29 +00:00
|
|
|
|
2003-11-07 21:55:50 +00:00
|
|
|
/* Save info for possible future use */
|
|
|
|
UsedShmemSegAddr = memAddress;
|
2003-12-01 22:15:38 +00:00
|
|
|
UsedShmemSegID = (unsigned long) NextShmemSegID;
|
2003-08-04 00:43:34 +00:00
|
|
|
|
2003-05-08 19:17:07 +00:00
|
|
|
return hdr;
|
|
|
|
}
|
|
|
|
|
2003-11-07 21:55:50 +00:00
|
|
|
/*
|
|
|
|
* PGSharedMemoryDetach
|
|
|
|
*
|
|
|
|
* Detach from the shared memory segment, if still attached. This is not
|
|
|
|
* intended for use by the process that originally created the segment
|
2004-08-29 05:07:03 +00:00
|
|
|
* (it will have an on_shmem_exit callback registered to do that). Rather,
|
2003-11-07 21:55:50 +00:00
|
|
|
* this is for subprocesses that have inherited an attachment and want to
|
|
|
|
* get rid of it.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
PGSharedMemoryDetach(void)
|
|
|
|
{
|
|
|
|
if (UsedShmemSegAddr != NULL)
|
|
|
|
{
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
if ((shmdt(UsedShmemSegAddr) < 0)
|
2004-02-25 19:41:23 +00:00
|
|
|
#if (defined(EXEC_BACKEND) && defined(__CYGWIN__))
|
2004-08-29 05:07:03 +00:00
|
|
|
/* Work-around for cygipc exec bug */
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
&& shmdt(NULL) < 0
|
|
|
|
#endif
|
|
|
|
)
|
2003-11-07 21:55:50 +00:00
|
|
|
elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
|
|
|
|
UsedShmemSegAddr = NULL;
|
|
|
|
}
|
|
|
|
}
|
2003-05-08 19:17:07 +00:00
|
|
|
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 00:11:31 +00:00
|
|
|
|
2003-05-08 19:17:07 +00:00
|
|
|
/*
|
2003-07-14 20:00:23 +00:00
|
|
|
* Attach to shared memory and make sure it has a Postgres header
|
|
|
|
*
|
|
|
|
* Returns attach address if OK, else NULL
|
2003-05-08 19:17:07 +00:00
|
|
|
*/
|
|
|
|
static PGShmemHeader *
|
2003-07-14 20:00:23 +00:00
|
|
|
PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid)
|
2003-05-08 19:17:07 +00:00
|
|
|
{
|
|
|
|
PGShmemHeader *hdr;
|
|
|
|
|
|
|
|
if ((*shmid = shmget(key, sizeof(PGShmemHeader), 0)) < 0)
|
|
|
|
return NULL;
|
2003-05-06 23:34:56 +00:00
|
|
|
|
2003-07-14 20:00:23 +00:00
|
|
|
hdr = (PGShmemHeader *) shmat(*shmid,
|
|
|
|
UsedShmemSegAddr,
|
2003-10-27 18:30:07 +00:00
|
|
|
#ifdef SHM_SHARE_MMU
|
2003-08-04 00:43:34 +00:00
|
|
|
/* use intimate shared memory on Solaris */
|
2003-07-14 20:00:23 +00:00
|
|
|
SHM_SHARE_MMU
|
2003-05-08 19:17:07 +00:00
|
|
|
#else
|
2003-07-14 20:00:23 +00:00
|
|
|
0
|
2003-05-08 19:17:07 +00:00
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hdr == (PGShmemHeader *) -1)
|
|
|
|
return NULL; /* failed: must be some other app's */
|
|
|
|
|
|
|
|
if (hdr->magic != PGShmemMagic)
|
|
|
|
{
|
2003-10-13 22:47:15 +00:00
|
|
|
shmdt(hdr);
|
2003-05-08 19:17:07 +00:00
|
|
|
return NULL; /* segment belongs to a non-Postgres app */
|
|
|
|
}
|
2003-07-14 20:00:23 +00:00
|
|
|
|
2002-05-05 00:03:29 +00:00
|
|
|
return hdr;
|
|
|
|
}
|