PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_shmem.h File Reference
#include "storage/dsm_impl.h"
Include dependency graph for pg_shmem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PGShmemHeader
 

Macros

#define PGShmemMagic   679834894
 

Typedefs

typedef struct PGShmemHeader PGShmemHeader
 

Enumerations

enum  HugePagesType { HUGE_PAGES_OFF, HUGE_PAGES_ON, HUGE_PAGES_TRY }
 

Functions

PGShmemHeaderPGSharedMemoryCreate (Size size, bool makePrivate, int port, PGShmemHeader **shim)
 
bool PGSharedMemoryIsInUse (unsigned long id1, unsigned long id2)
 
void PGSharedMemoryDetach (void)
 

Variables

int huge_pages
 
unsigned long UsedShmemSegID
 
void * UsedShmemSegAddr
 

Macro Definition Documentation

#define PGShmemMagic   679834894

Typedef Documentation

Enumeration Type Documentation

Enumerator
HUGE_PAGES_OFF 
HUGE_PAGES_ON 
HUGE_PAGES_TRY 

Definition at line 48 of file pg_shmem.h.

Function Documentation

PGShmemHeader* PGSharedMemoryCreate ( Size  size,
bool  makePrivate,
int  port,
PGShmemHeader **  shim 
)

Definition at line 536 of file sysv_shmem.c.

References AnonymousShmem, AnonymousShmemDetach(), AnonymousShmemSize, Assert, CreateAnonymousSegment(), PGShmemHeader::creatorPID, DataDir, PGShmemHeader::device, dsm_cleanup_using_control_segment(), PGShmemHeader::dsm_control, elog, ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg(), ERROR, FATAL, free, PGShmemHeader::freeoffset, GetSharedMemName(), huge_pages, HUGE_PAGES_ON, i, PGShmemHeader::inode, InternalIpcMemoryCreate(), IPC_RMID, LOG, PGShmemHeader::magic, MAXALIGN, NULL, on_shmem_exit(), PGSharedMemoryAttach(), PGShmemMagic, pgwin32_SharedMemoryDelete(), PointerGetDatum, PGShmemHeader::totalsize, TRUE, UsedShmemSegAddr, UsedShmemSegID, and UsedShmemSegSize.

Referenced by CreateSharedMemoryAndSemaphores().

538 {
539  IpcMemoryKey NextShmemSegID;
540  void *memAddress;
541  PGShmemHeader *hdr;
542  IpcMemoryId shmid;
543  struct stat statbuf;
544  Size sysvsize;
545 
546  /* Complain if hugepages demanded but we can't possibly support them */
547 #if !defined(USE_ANONYMOUS_SHMEM) || !defined(MAP_HUGETLB)
548  if (huge_pages == HUGE_PAGES_ON)
549  ereport(ERROR,
550  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
551  errmsg("huge pages not supported on this platform")));
552 #endif
553 
554  /* Room for a header? */
555  Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
556 
557 #ifdef USE_ANONYMOUS_SHMEM
559  AnonymousShmemSize = size;
560 
561  /* Register on-exit routine to unmap the anonymous segment */
563 
564  /* Now we need only allocate a minimal-sized SysV shmem block. */
565  sysvsize = sizeof(PGShmemHeader);
566 #else
567  sysvsize = size;
568 #endif
569 
570  /* Make sure PGSharedMemoryAttach doesn't fail without need */
572 
573  /* Loop till we find a free IPC key */
574  NextShmemSegID = port * 1000;
575 
576  for (NextShmemSegID++;; NextShmemSegID++)
577  {
578  /* Try to create new segment */
579  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
580  if (memAddress)
581  break; /* successful create and attach */
582 
583  /* Check shared memory and possibly remove and recreate */
584 
585  if (makePrivate) /* a standalone backend shouldn't do this */
586  continue;
587 
588  if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL)
589  continue; /* can't attach, not one of mine */
590 
591  /*
592  * If I am not the creator and it belongs to an extant process,
593  * continue.
594  */
595  hdr = (PGShmemHeader *) memAddress;
596  if (hdr->creatorPID != getpid())
597  {
598  if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
599  {
600  shmdt(memAddress);
601  continue; /* segment belongs to a live process */
602  }
603  }
604 
605  /*
606  * The segment appears to be from a dead Postgres process, or from a
607  * previous cycle of life in this same process. Zap it, if possible,
608  * and any associated dynamic shared memory segments, as well. This
609  * probably shouldn't fail, but if it does, assume the segment belongs
610  * to someone else after all, and continue quietly.
611  */
612  if (hdr->dsm_control != 0)
614  shmdt(memAddress);
615  if (shmctl(shmid, IPC_RMID, NULL) < 0)
616  continue;
617 
618  /*
619  * Now try again to create the segment.
620  */
621  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
622  if (memAddress)
623  break; /* successful create and attach */
624 
625  /*
626  * Can only get here if some other process managed to create the same
627  * shmem key before we did. Let him have that one, loop around to try
628  * next key.
629  */
630  }
631 
632  /*
633  * OK, we created a new segment. Mark it as created by this process. The
634  * order of assignments here is critical so that another Postgres process
635  * can't see the header as valid but belonging to an invalid PID!
636  */
637  hdr = (PGShmemHeader *) memAddress;
638  hdr->creatorPID = getpid();
639  hdr->magic = PGShmemMagic;
640  hdr->dsm_control = 0;
641 
642  /* Fill in the data directory ID info, too */
643  if (stat(DataDir, &statbuf) < 0)
644  ereport(FATAL,
646  errmsg("could not stat data directory \"%s\": %m",
647  DataDir)));
648  hdr->device = statbuf.st_dev;
649  hdr->inode = statbuf.st_ino;
650 
651  /*
652  * Initialize space allocation status for segment.
653  */
654  hdr->totalsize = size;
655  hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
656  *shim = hdr;
657 
658  /* Save info for possible future use */
659  UsedShmemSegAddr = memAddress;
660  UsedShmemSegID = (unsigned long) NextShmemSegID;
661 
662  /*
663  * If AnonymousShmem is NULL here, then we're not using anonymous shared
664  * memory, and should return a pointer to the System V shared memory
665  * block. Otherwise, the System V shared memory block is only a shim, and
666  * we must return a pointer to the real block.
667  */
668 #ifdef USE_ANONYMOUS_SHMEM
669  if (AnonymousShmem == NULL)
670  return hdr;
671  memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
672  return (PGShmemHeader *) AnonymousShmem;
673 #else
674  return hdr;
675 #endif
676 }
pid_t creatorPID
Definition: pg_shmem.h:33
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
Definition: dsm.c:211
static PGShmemHeader * PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid)
Definition: sysv_shmem.c:800
dsm_handle dsm_control
Definition: pg_shmem.h:36
struct PGShmemHeader PGShmemHeader
static Size AnonymousShmemSize
Definition: sysv_shmem.c:78
int errcode(int sqlerrcode)
Definition: elog.c:575
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
Definition: sysv_shmem.c:102
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:348
key_t IpcMemoryKey
Definition: sysv_shmem.c:70
int errcode_for_file_access(void)
Definition: elog.c:598
void * UsedShmemSegAddr
Definition: sysv_shmem.c:75
static void AnonymousShmemDetach(int status, Datum arg)
Definition: sysv_shmem.c:503
#define ereport(elevel, rest)
Definition: elog.h:122
static int port
Definition: pg_regress.c:87
int32 magic
Definition: pg_shmem.h:31
static void * CreateAnonymousSegment(Size *size)
Definition: sysv_shmem.c:435
Size totalsize
Definition: pg_shmem.h:34
uintptr_t Datum
Definition: postgres.h:374
unsigned long UsedShmemSegID
Definition: sysv_shmem.c:74
#define IPC_RMID
Definition: win32.h:114
static void * AnonymousShmem
Definition: sysv_shmem.c:79
ino_t inode
Definition: pg_shmem.h:40
dev_t device
Definition: pg_shmem.h:39
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:667
#define PGShmemMagic
Definition: pg_shmem.h:32
Size freeoffset
Definition: pg_shmem.h:35
size_t Size
Definition: c.h:352
#define MAXALIGN(LEN)
Definition: c.h:580
int IpcMemoryId
Definition: sysv_shmem.c:71
int huge_pages
Definition: guc.c:482
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * DataDir
Definition: globals.c:59
void PGSharedMemoryDetach ( void  )

Definition at line 768 of file sysv_shmem.c.

References AnonymousShmem, AnonymousShmemSize, elog, LOG, NULL, UsedShmemSegAddr, and UsedShmemSegID.

Referenced by pgarch_start(), PGSharedMemoryNoReAttach(), pgstat_start(), pgwin32_SharedMemoryDelete(), StartBackgroundWorker(), and SysLogger_Start().

769 {
770  if (UsedShmemSegAddr != NULL)
771  {
772  if ((shmdt(UsedShmemSegAddr) < 0)
773 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
774  /* Work-around for cygipc exec bug */
775  && shmdt(NULL) < 0
776 #endif
777  )
778  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
780  }
781 
782 #ifdef USE_ANONYMOUS_SHMEM
783  if (AnonymousShmem != NULL)
784  {
785  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
786  elog(LOG, "munmap(%p, %zu) failed: %m",
789  }
790 #endif
791 }
static Size AnonymousShmemSize
Definition: sysv_shmem.c:78
#define LOG
Definition: elog.h:26
void * UsedShmemSegAddr
Definition: sysv_shmem.c:75
static void * AnonymousShmem
Definition: sysv_shmem.c:79
#define NULL
Definition: c.h:226
#define elog
Definition: elog.h:219
bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 267 of file sysv_shmem.c.

References DataDir, EIDRM, FALSE, free, GetSharedMemName(), IPC_STAT, NULL, PG_SHMAT_FLAGS, and PGShmemMagic.

Referenced by CreateLockFile().

268 {
269  IpcMemoryId shmId = (IpcMemoryId) id2;
270  struct shmid_ds shmStat;
271  struct stat statbuf;
272  PGShmemHeader *hdr;
273 
274  /*
275  * We detect whether a shared memory segment is in use by seeing whether
276  * it (a) exists and (b) has any processes attached to it.
277  */
278  if (shmctl(shmId, IPC_STAT, &shmStat) < 0)
279  {
280  /*
281  * EINVAL actually has multiple possible causes documented in the
282  * shmctl man page, but we assume it must mean the segment no longer
283  * exists.
284  */
285  if (errno == EINVAL)
286  return false;
287 
288  /*
289  * EACCES implies that the segment belongs to some other userid, which
290  * means it is not a Postgres shmem segment (or at least, not one that
291  * is relevant to our data directory).
292  */
293  if (errno == EACCES)
294  return false;
295 
296  /*
297  * Some Linux kernel versions (in fact, all of them as of July 2007)
298  * sometimes return EIDRM when EINVAL is correct. The Linux kernel
299  * actually does not have any internal state that would justify
300  * returning EIDRM, so we can get away with assuming that EIDRM is
301  * equivalent to EINVAL on that platform.
302  */
303 #ifdef HAVE_LINUX_EIDRM_BUG
304  if (errno == EIDRM)
305  return false;
306 #endif
307 
308  /*
309  * Otherwise, we had better assume that the segment is in use. The
310  * only likely case is EIDRM, which implies that the segment has been
311  * IPC_RMID'd but there are still processes attached to it.
312  */
313  return true;
314  }
315 
316  /* If it has no attached processes, it's not in use */
317  if (shmStat.shm_nattch == 0)
318  return false;
319 
320  /*
321  * Try to attach to the segment and see if it matches our data directory.
322  * This avoids shmid-conflict problems on machines that are running
323  * several postmasters under the same userid.
324  */
325  if (stat(DataDir, &statbuf) < 0)
326  return true; /* if can't stat, be conservative */
327 
328  hdr = (PGShmemHeader *) shmat(shmId, NULL, PG_SHMAT_FLAGS);
329 
330  if (hdr == (PGShmemHeader *) -1)
331  return true; /* if can't attach, be conservative */
332 
333  if (hdr->magic != PGShmemMagic ||
334  hdr->device != statbuf.st_dev ||
335  hdr->inode != statbuf.st_ino)
336  {
337  /*
338  * It's either not a Postgres segment, or not one for my data
339  * directory. In either case it poses no threat.
340  */
341  shmdt((void *) hdr);
342  return false;
343  }
344 
345  /* Trouble --- looks a lot like there's still live backends */
346  shmdt((void *) hdr);
347 
348  return true;
349 }
#define PG_SHMAT_FLAGS
Definition: mem.h:20
#define IPC_STAT
Definition: win32.h:119
#define NULL
Definition: c.h:226
#define PGShmemMagic
Definition: pg_shmem.h:32
int IpcMemoryId
Definition: sysv_shmem.c:71
char * DataDir
Definition: globals.c:59
#define EIDRM
Definition: win32.h:123

Variable Documentation

int huge_pages

Definition at line 482 of file guc.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

unsigned long UsedShmemSegID