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 425 of file sysv_shmem.c.

References AnonymousShmem, 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().

427 {
428  IpcMemoryKey NextShmemSegID;
429  void *memAddress;
430  PGShmemHeader *hdr;
431  IpcMemoryId shmid;
432  struct stat statbuf;
433  Size sysvsize;
434 
435 #if defined(EXEC_BACKEND) || !defined(MAP_HUGETLB)
436  if (huge_pages == HUGE_PAGES_ON)
437  ereport(ERROR,
438  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
439  errmsg("huge pages not supported on this platform")));
440 #endif
441 
442  /* Room for a header? */
443  Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
444 
445  /*
446  * As of PostgreSQL 9.3, we normally allocate only a very small amount of
447  * System V shared memory, and only for the purposes of providing an
448  * interlock to protect the data directory. The real shared memory block
449  * is allocated using mmap(). This works around the problem that many
450  * systems have very low limits on the amount of System V shared memory
451  * that can be allocated. Even a limit of a few megabytes will be enough
452  * to run many copies of PostgreSQL without needing to adjust system
453  * settings.
454  *
455  * We assume that no one will attempt to run PostgreSQL 9.3 or later on
456  * systems that are ancient enough that anonymous shared memory is not
457  * supported, such as pre-2.4 versions of Linux. If that turns out to be
458  * false, we might need to add a run-time test here and do this only if
459  * the running kernel supports it.
460  *
461  * However, we disable this logic in the EXEC_BACKEND case, and fall back
462  * to the old method of allocating the entire segment using System V
463  * shared memory, because there's no way to attach an mmap'd segment to a
464  * process after exec(). Since EXEC_BACKEND is intended only for
465  * developer use, this shouldn't be a big problem.
466  */
467 #ifndef EXEC_BACKEND
469  AnonymousShmemSize = size;
470 
471  /* Now we need only allocate a minimal-sized SysV shmem block. */
472  sysvsize = sizeof(PGShmemHeader);
473 #else
474  sysvsize = size;
475 #endif
476 
477  /* Make sure PGSharedMemoryAttach doesn't fail without need */
479 
480  /* Loop till we find a free IPC key */
481  NextShmemSegID = port * 1000;
482 
483  for (NextShmemSegID++;; NextShmemSegID++)
484  {
485  /* Try to create new segment */
486  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
487  if (memAddress)
488  break; /* successful create and attach */
489 
490  /* Check shared memory and possibly remove and recreate */
491 
492  if (makePrivate) /* a standalone backend shouldn't do this */
493  continue;
494 
495  if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL)
496  continue; /* can't attach, not one of mine */
497 
498  /*
499  * If I am not the creator and it belongs to an extant process,
500  * continue.
501  */
502  hdr = (PGShmemHeader *) memAddress;
503  if (hdr->creatorPID != getpid())
504  {
505  if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
506  {
507  shmdt(memAddress);
508  continue; /* segment belongs to a live process */
509  }
510  }
511 
512  /*
513  * The segment appears to be from a dead Postgres process, or from a
514  * previous cycle of life in this same process. Zap it, if possible,
515  * and any associated dynamic shared memory segments, as well. This
516  * probably shouldn't fail, but if it does, assume the segment belongs
517  * to someone else after all, and continue quietly.
518  */
519  if (hdr->dsm_control != 0)
521  shmdt(memAddress);
522  if (shmctl(shmid, IPC_RMID, NULL) < 0)
523  continue;
524 
525  /*
526  * Now try again to create the segment.
527  */
528  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
529  if (memAddress)
530  break; /* successful create and attach */
531 
532  /*
533  * Can only get here if some other process managed to create the same
534  * shmem key before we did. Let him have that one, loop around to try
535  * next key.
536  */
537  }
538 
539  /*
540  * OK, we created a new segment. Mark it as created by this process. The
541  * order of assignments here is critical so that another Postgres process
542  * can't see the header as valid but belonging to an invalid PID!
543  */
544  hdr = (PGShmemHeader *) memAddress;
545  hdr->creatorPID = getpid();
546  hdr->magic = PGShmemMagic;
547  hdr->dsm_control = 0;
548 
549  /* Fill in the data directory ID info, too */
550  if (stat(DataDir, &statbuf) < 0)
551  ereport(FATAL,
553  errmsg("could not stat data directory \"%s\": %m",
554  DataDir)));
555  hdr->device = statbuf.st_dev;
556  hdr->inode = statbuf.st_ino;
557 
558  /*
559  * Initialize space allocation status for segment.
560  */
561  hdr->totalsize = size;
562  hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
563  *shim = hdr;
564 
565  /* Save info for possible future use */
566  UsedShmemSegAddr = memAddress;
567  UsedShmemSegID = (unsigned long) NextShmemSegID;
568 
569  /*
570  * If AnonymousShmem is NULL here, then we're not using anonymous shared
571  * memory, and should return a pointer to the System V shared memory
572  * block. Otherwise, the System V shared memory block is only a shim, and
573  * we must return a pointer to the real block.
574  */
575  if (AnonymousShmem == NULL)
576  return hdr;
577  memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
578  return (PGShmemHeader *) AnonymousShmem;
579 }
pid_t creatorPID
Definition: pg_shmem.h:33
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
Definition: dsm.c:209
static PGShmemHeader * PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid)
Definition: sysv_shmem.c:661
dsm_handle dsm_control
Definition: pg_shmem.h:36
struct PGShmemHeader PGShmemHeader
static Size AnonymousShmemSize
Definition: sysv_shmem.c:45
int errcode(int sqlerrcode)
Definition: elog.c:569
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
Definition: sysv_shmem.c:68
#define ERROR
Definition: elog.h:41
#define FATAL
Definition: elog.h:50
key_t IpcMemoryKey
Definition: sysv_shmem.c:39
int errcode_for_file_access(void)
Definition: elog.c:592
void * UsedShmemSegAddr
Definition: sysv_shmem.c:44
#define ereport(elevel, rest)
Definition: elog.h:132
static int port
Definition: pg_regress.c:87
int32 magic
Definition: pg_shmem.h:31
static void * CreateAnonymousSegment(Size *size)
Definition: sysv_shmem.c:330
Size totalsize
Definition: pg_shmem.h:34
unsigned long UsedShmemSegID
Definition: sysv_shmem.c:43
#define IPC_RMID
Definition: win32.h:100
static void * AnonymousShmem
Definition: sysv_shmem.c:46
ino_t inode
Definition: pg_shmem.h:40
dev_t device
Definition: pg_shmem.h:39
#define NULL
Definition: c.h:202
#define Assert(condition)
Definition: c.h:648
#define PGShmemMagic
Definition: pg_shmem.h:32
Size freeoffset
Definition: pg_shmem.h:35
size_t Size
Definition: c.h:333
#define MAXALIGN(LEN)
Definition: c.h:561
int IpcMemoryId
Definition: sysv_shmem.c:40
int huge_pages
Definition: guc.c:461
int errmsg(const char *fmt,...)
Definition: elog.c:791
char * DataDir
Definition: globals.c:58
void PGSharedMemoryDetach ( void  )

Definition at line 634 of file sysv_shmem.c.

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

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

635 {
636  if (UsedShmemSegAddr != NULL)
637  {
638  if ((shmdt(UsedShmemSegAddr) < 0)
639 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
640  /* Work-around for cygipc exec bug */
641  && shmdt(NULL) < 0
642 #endif
643  )
644  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
646  }
647 
648  /* Release anonymous shared memory block, if any. */
649  if (AnonymousShmem != NULL
650  && munmap(AnonymousShmem, AnonymousShmemSize) < 0)
651  elog(LOG, "munmap(%p) failed: %m", AnonymousShmem);
652 }
static Size AnonymousShmemSize
Definition: sysv_shmem.c:45
#define LOG
Definition: elog.h:26
void * UsedShmemSegAddr
Definition: sysv_shmem.c:44
static void * AnonymousShmem
Definition: sysv_shmem.c:46
#define NULL
Definition: c.h:202
#define elog
Definition: elog.h:228
bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 237 of file sysv_shmem.c.

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

Referenced by CreateLockFile().

238 {
239  IpcMemoryId shmId = (IpcMemoryId) id2;
240  struct shmid_ds shmStat;
241  struct stat statbuf;
242  PGShmemHeader *hdr;
243 
244  /*
245  * We detect whether a shared memory segment is in use by seeing whether
246  * it (a) exists and (b) has any processes attached to it.
247  */
248  if (shmctl(shmId, IPC_STAT, &shmStat) < 0)
249  {
250  /*
251  * EINVAL actually has multiple possible causes documented in the
252  * shmctl man page, but we assume it must mean the segment no longer
253  * exists.
254  */
255  if (errno == EINVAL)
256  return false;
257 
258  /*
259  * EACCES implies that the segment belongs to some other userid, which
260  * means it is not a Postgres shmem segment (or at least, not one that
261  * is relevant to our data directory).
262  */
263  if (errno == EACCES)
264  return false;
265 
266  /*
267  * Some Linux kernel versions (in fact, all of them as of July 2007)
268  * sometimes return EIDRM when EINVAL is correct. The Linux kernel
269  * actually does not have any internal state that would justify
270  * returning EIDRM, so we can get away with assuming that EIDRM is
271  * equivalent to EINVAL on that platform.
272  */
273 #ifdef HAVE_LINUX_EIDRM_BUG
274  if (errno == EIDRM)
275  return false;
276 #endif
277 
278  /*
279  * Otherwise, we had better assume that the segment is in use. The
280  * only likely case is EIDRM, which implies that the segment has been
281  * IPC_RMID'd but there are still processes attached to it.
282  */
283  return true;
284  }
285 
286  /* If it has no attached processes, it's not in use */
287  if (shmStat.shm_nattch == 0)
288  return false;
289 
290  /*
291  * Try to attach to the segment and see if it matches our data directory.
292  * This avoids shmid-conflict problems on machines that are running
293  * several postmasters under the same userid.
294  */
295  if (stat(DataDir, &statbuf) < 0)
296  return true; /* if can't stat, be conservative */
297 
298  hdr = (PGShmemHeader *) shmat(shmId, NULL, PG_SHMAT_FLAGS);
299 
300  if (hdr == (PGShmemHeader *) -1)
301  return true; /* if can't attach, be conservative */
302 
303  if (hdr->magic != PGShmemMagic ||
304  hdr->device != statbuf.st_dev ||
305  hdr->inode != statbuf.st_ino)
306  {
307  /*
308  * It's either not a Postgres segment, or not one for my data
309  * directory. In either case it poses no threat.
310  */
311  shmdt((void *) hdr);
312  return false;
313  }
314 
315  /* Trouble --- looks a lot like there's still live backends */
316  shmdt((void *) hdr);
317 
318  return true;
319 }
#define PG_SHMAT_FLAGS
Definition: mem.h:20
#define IPC_STAT
Definition: win32.h:105
#define NULL
Definition: c.h:202
#define PGShmemMagic
Definition: pg_shmem.h:32
int IpcMemoryId
Definition: sysv_shmem.c:40
char * DataDir
Definition: globals.c:58
#define EIDRM
Definition: win32.h:109

Variable Documentation

int huge_pages

Definition at line 461 of file guc.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

unsigned long UsedShmemSegID

Definition at line 43 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryReAttach().