PostgreSQL Source Code  git master
sysv_shmem.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "portability/mem.h"
#include "storage/dsm.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "utils/guc.h"
#include "utils/pidfile.h"
Include dependency graph for sysv_shmem.c:

Go to the source code of this file.

Typedefs

typedef key_t IpcMemoryKey
 
typedef int IpcMemoryId
 

Enumerations

enum  IpcMemoryState {
  SHMSTATE_ANALYSIS_FAILURE , SHMSTATE_ATTACHED , SHMSTATE_ENOENT , SHMSTATE_FOREIGN ,
  SHMSTATE_UNATTACHED
}
 

Functions

static void * InternalIpcMemoryCreate (IpcMemoryKey memKey, Size size)
 
static void IpcMemoryDetach (int status, Datum shmaddr)
 
static void IpcMemoryDelete (int status, Datum shmId)
 
static IpcMemoryState PGSharedMemoryAttach (IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
 
bool PGSharedMemoryIsInUse (unsigned long id1, unsigned long id2)
 
void GetHugePageSize (Size *hugepagesize, int *mmap_flags)
 
static void * CreateAnonymousSegment (Size *size)
 
static void AnonymousShmemDetach (int status, Datum arg)
 
PGShmemHeaderPGSharedMemoryCreate (Size size, PGShmemHeader **shim)
 
void PGSharedMemoryDetach (void)
 

Variables

unsigned long UsedShmemSegID = 0
 
void * UsedShmemSegAddr = NULL
 
static Size AnonymousShmemSize
 
static void * AnonymousShmem = NULL
 

Typedef Documentation

◆ IpcMemoryId

typedef int IpcMemoryId

Definition at line 74 of file sysv_shmem.c.

◆ IpcMemoryKey

Definition at line 73 of file sysv_shmem.c.

Enumeration Type Documentation

◆ IpcMemoryState

Enumerator
SHMSTATE_ANALYSIS_FAILURE 
SHMSTATE_ATTACHED 
SHMSTATE_ENOENT 
SHMSTATE_FOREIGN 
SHMSTATE_UNATTACHED 

Definition at line 87 of file sysv_shmem.c.

88 {
89  SHMSTATE_ANALYSIS_FAILURE, /* unexpected failure to analyze the ID */
90  SHMSTATE_ATTACHED, /* pertinent to DataDir, has attached PIDs */
91  SHMSTATE_ENOENT, /* no segment of that ID */
92  SHMSTATE_FOREIGN, /* exists, but not pertinent to DataDir */
93  SHMSTATE_UNATTACHED /* pertinent to DataDir, no attached PIDs */
IpcMemoryState
Definition: sysv_shmem.c:88
@ SHMSTATE_ATTACHED
Definition: sysv_shmem.c:90
@ SHMSTATE_UNATTACHED
Definition: sysv_shmem.c:93
@ SHMSTATE_FOREIGN
Definition: sysv_shmem.c:92
@ SHMSTATE_ENOENT
Definition: sysv_shmem.c:91
@ SHMSTATE_ANALYSIS_FAILURE
Definition: sysv_shmem.c:89

Function Documentation

◆ AnonymousShmemDetach()

static void AnonymousShmemDetach ( int  status,
Datum  arg 
)
static

Definition at line 653 of file sysv_shmem.c.

654 {
655  /* Release anonymous shared memory block, if any. */
656  if (AnonymousShmem != NULL)
657  {
658  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
659  elog(LOG, "munmap(%p, %zu) failed: %m",
661  AnonymousShmem = NULL;
662  }
663 }
#define LOG
Definition: elog.h:25
#define elog(elevel,...)
Definition: elog.h:218
static Size AnonymousShmemSize
Definition: sysv_shmem.c:100
static void * AnonymousShmem
Definition: sysv_shmem.c:101

References AnonymousShmem, AnonymousShmemSize, elog, and LOG.

Referenced by PGSharedMemoryCreate().

◆ CreateAnonymousSegment()

static void* CreateAnonymousSegment ( Size size)
static

Definition at line 585 of file sysv_shmem.c.

586 {
587  Size allocsize = *size;
588  void *ptr = MAP_FAILED;
589  int mmap_errno = 0;
590 
591 #ifndef MAP_HUGETLB
592  /* PGSharedMemoryCreate should have dealt with this case */
594 #else
596  {
597  /*
598  * Round up the request size to a suitable large value.
599  */
600  Size hugepagesize;
601  int mmap_flags;
602 
603  GetHugePageSize(&hugepagesize, &mmap_flags);
604 
605  if (allocsize % hugepagesize != 0)
606  allocsize += hugepagesize - (allocsize % hugepagesize);
607 
608  ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
609  PG_MMAP_FLAGS | mmap_flags, -1, 0);
610  mmap_errno = errno;
611  if (huge_pages == HUGE_PAGES_TRY && ptr == MAP_FAILED)
612  elog(DEBUG1, "mmap(%zu) with MAP_HUGETLB failed, huge pages disabled: %m",
613  allocsize);
614  }
615 #endif
616 
617  if (ptr == MAP_FAILED && huge_pages != HUGE_PAGES_ON)
618  {
619  /*
620  * Use the original size, not the rounded-up value, when falling back
621  * to non-huge pages.
622  */
623  allocsize = *size;
624  ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
625  PG_MMAP_FLAGS, -1, 0);
626  mmap_errno = errno;
627  }
628 
629  if (ptr == MAP_FAILED)
630  {
631  errno = mmap_errno;
632  ereport(FATAL,
633  (errmsg("could not map anonymous shared memory: %m"),
634  (mmap_errno == ENOMEM) ?
635  errhint("This error usually means that PostgreSQL's request "
636  "for a shared memory segment exceeded available memory, "
637  "swap space, or huge pages. To reduce the request size "
638  "(currently %zu bytes), reduce PostgreSQL's shared "
639  "memory usage, perhaps by reducing shared_buffers or "
640  "max_connections.",
641  allocsize) : 0));
642  }
643 
644  *size = allocsize;
645  return ptr;
646 }
size_t Size
Definition: c.h:540
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define FATAL
Definition: elog.h:35
#define DEBUG1
Definition: elog.h:24
#define ereport(elevel,...)
Definition: elog.h:143
int huge_pages
Definition: guc.c:680
Assert(fmt[strlen(fmt) - 1] !='\n')
#define PG_MMAP_FLAGS
Definition: mem.h:41
#define MAP_FAILED
Definition: mem.h:45
@ HUGE_PAGES_ON
Definition: pg_shmem.h:53
@ HUGE_PAGES_TRY
Definition: pg_shmem.h:54
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:482

References Assert(), DEBUG1, elog, ereport, errhint(), errmsg(), FATAL, GetHugePageSize(), huge_pages, HUGE_PAGES_ON, HUGE_PAGES_TRY, MAP_FAILED, and PG_MMAP_FLAGS.

Referenced by PGSharedMemoryCreate().

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int *  mmap_flags 
)

Definition at line 482 of file sysv_shmem.c.

483 {
484 #ifdef MAP_HUGETLB
485 
486  Size default_hugepagesize = 0;
487  Size hugepagesize_local = 0;
488  int mmap_flags_local = 0;
489 
490  /*
491  * System-dependent code to find out the default huge page size.
492  *
493  * On Linux, read /proc/meminfo looking for a line like "Hugepagesize:
494  * nnnn kB". Ignore any failures, falling back to the preset default.
495  */
496 #ifdef __linux__
497 
498  {
499  FILE *fp = AllocateFile("/proc/meminfo", "r");
500  char buf[128];
501  unsigned int sz;
502  char ch;
503 
504  if (fp)
505  {
506  while (fgets(buf, sizeof(buf), fp))
507  {
508  if (sscanf(buf, "Hugepagesize: %u %c", &sz, &ch) == 2)
509  {
510  if (ch == 'k')
511  {
512  default_hugepagesize = sz * (Size) 1024;
513  break;
514  }
515  /* We could accept other units besides kB, if needed */
516  }
517  }
518  FreeFile(fp);
519  }
520  }
521 #endif /* __linux__ */
522 
523  if (huge_page_size != 0)
524  {
525  /* If huge page size is requested explicitly, use that. */
526  hugepagesize_local = (Size) huge_page_size * 1024;
527  }
528  else if (default_hugepagesize != 0)
529  {
530  /* Otherwise use the system default, if we have it. */
531  hugepagesize_local = default_hugepagesize;
532  }
533  else
534  {
535  /*
536  * If we fail to find out the system's default huge page size, or no
537  * huge page size is requested explicitly, assume it is 2MB. This will
538  * work fine when the actual size is less. If it's more, we might get
539  * mmap() or munmap() failures due to unaligned requests; but at this
540  * writing, there are no reports of any non-Linux systems being picky
541  * about that.
542  */
543  hugepagesize_local = 2 * 1024 * 1024;
544  }
545 
546  mmap_flags_local = MAP_HUGETLB;
547 
548  /*
549  * On recent enough Linux, also include the explicit page size, if
550  * necessary.
551  */
552 #if defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT)
553  if (hugepagesize_local != default_hugepagesize)
554  {
555  int shift = pg_ceil_log2_64(hugepagesize_local);
556 
557  mmap_flags_local |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
558  }
559 #endif
560 
561  /* assign the results found */
562  if (mmap_flags)
563  *mmap_flags = mmap_flags_local;
564  if (hugepagesize)
565  *hugepagesize = hugepagesize_local;
566 
567 #else
568 
569  if (hugepagesize)
570  *hugepagesize = 0;
571  if (mmap_flags)
572  *mmap_flags = 0;
573 
574 #endif /* MAP_HUGETLB */
575 }
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2461
int FreeFile(FILE *file)
Definition: fd.c:2660
int huge_page_size
Definition: guc.c:681
static uint64 pg_ceil_log2_64(uint64 num)
Definition: pg_bitutils.h:242
static char * buf
Definition: pg_test_fsync.c:67

References AllocateFile(), buf, FreeFile(), huge_page_size, and pg_ceil_log2_64().

Referenced by CreateAnonymousSegment(), and InitializeShmemGUCs().

◆ InternalIpcMemoryCreate()

static void * InternalIpcMemoryCreate ( IpcMemoryKey  memKey,
Size  size 
)
static

Definition at line 124 of file sysv_shmem.c.

125 {
126  IpcMemoryId shmid;
127  void *requestedAddress = NULL;
128  void *memAddress;
129 
130  /*
131  * Normally we just pass requestedAddress = NULL to shmat(), allowing the
132  * system to choose where the segment gets mapped. But in an EXEC_BACKEND
133  * build, it's possible for whatever is chosen in the postmaster to not
134  * work for backends, due to variations in address space layout. As a
135  * rather klugy workaround, allow the user to specify the address to use
136  * via setting the environment variable PG_SHMEM_ADDR. (If this were of
137  * interest for anything except debugging, we'd probably create a cleaner
138  * and better-documented way to set it, such as a GUC.)
139  */
140 #ifdef EXEC_BACKEND
141  {
142  char *pg_shmem_addr = getenv("PG_SHMEM_ADDR");
143 
144  if (pg_shmem_addr)
145  requestedAddress = (void *) strtoul(pg_shmem_addr, NULL, 0);
146  else
147  {
148 #if defined(__darwin__) && SIZEOF_VOID_P == 8
149  /*
150  * Provide a default value that is believed to avoid problems with
151  * ASLR on the current macOS release.
152  */
153  requestedAddress = (void *) 0x80000000000;
154 #endif
155  }
156  }
157 #endif
158 
159  shmid = shmget(memKey, size, IPC_CREAT | IPC_EXCL | IPCProtection);
160 
161  if (shmid < 0)
162  {
163  int shmget_errno = errno;
164 
165  /*
166  * Fail quietly if error indicates a collision with existing segment.
167  * One would expect EEXIST, given that we said IPC_EXCL, but perhaps
168  * we could get a permission violation instead? Also, EIDRM might
169  * occur if an old seg is slated for destruction but not gone yet.
170  */
171  if (shmget_errno == EEXIST || shmget_errno == EACCES
172 #ifdef EIDRM
173  || shmget_errno == EIDRM
174 #endif
175  )
176  return NULL;
177 
178  /*
179  * Some BSD-derived kernels are known to return EINVAL, not EEXIST, if
180  * there is an existing segment but it's smaller than "size" (this is
181  * a result of poorly-thought-out ordering of error tests). To
182  * distinguish between collision and invalid size in such cases, we
183  * make a second try with size = 0. These kernels do not test size
184  * against SHMMIN in the preexisting-segment case, so we will not get
185  * EINVAL a second time if there is such a segment.
186  */
187  if (shmget_errno == EINVAL)
188  {
189  shmid = shmget(memKey, 0, IPC_CREAT | IPC_EXCL | IPCProtection);
190 
191  if (shmid < 0)
192  {
193  /* As above, fail quietly if we verify a collision */
194  if (errno == EEXIST || errno == EACCES
195 #ifdef EIDRM
196  || errno == EIDRM
197 #endif
198  )
199  return NULL;
200  /* Otherwise, fall through to report the original error */
201  }
202  else
203  {
204  /*
205  * On most platforms we cannot get here because SHMMIN is
206  * greater than zero. However, if we do succeed in creating a
207  * zero-size segment, free it and then fall through to report
208  * the original error.
209  */
210  if (shmctl(shmid, IPC_RMID, NULL) < 0)
211  elog(LOG, "shmctl(%d, %d, 0) failed: %m",
212  (int) shmid, IPC_RMID);
213  }
214  }
215 
216  /*
217  * Else complain and abort.
218  *
219  * Note: at this point EINVAL should mean that either SHMMIN or SHMMAX
220  * is violated. SHMALL violation might be reported as either ENOMEM
221  * (BSDen) or ENOSPC (Linux); the Single Unix Spec fails to say which
222  * it should be. SHMMNI violation is ENOSPC, per spec. Just plain
223  * not-enough-RAM is ENOMEM.
224  */
225  errno = shmget_errno;
226  ereport(FATAL,
227  (errmsg("could not create shared memory segment: %m"),
228  errdetail("Failed system call was shmget(key=%lu, size=%zu, 0%o).",
229  (unsigned long) memKey, size,
231  (shmget_errno == EINVAL) ?
232  errhint("This error usually means that PostgreSQL's request for a shared memory "
233  "segment exceeded your kernel's SHMMAX parameter, or possibly that "
234  "it is less than "
235  "your kernel's SHMMIN parameter.\n"
236  "The PostgreSQL documentation contains more information about shared "
237  "memory configuration.") : 0,
238  (shmget_errno == ENOMEM) ?
239  errhint("This error usually means that PostgreSQL's request for a shared "
240  "memory segment exceeded your kernel's SHMALL parameter. You might need "
241  "to reconfigure the kernel with larger SHMALL.\n"
242  "The PostgreSQL documentation contains more information about shared "
243  "memory configuration.") : 0,
244  (shmget_errno == ENOSPC) ?
245  errhint("This error does *not* mean that you have run out of disk space. "
246  "It occurs either if all available shared memory IDs have been taken, "
247  "in which case you need to raise the SHMMNI parameter in your kernel, "
248  "or because the system's overall limit for shared memory has been "
249  "reached.\n"
250  "The PostgreSQL documentation contains more information about shared "
251  "memory configuration.") : 0));
252  }
253 
254  /* Register on-exit routine to delete the new segment */
256 
257  /* OK, should be able to attach to the segment */
258  memAddress = shmat(shmid, requestedAddress, PG_SHMAT_FLAGS);
259 
260  if (memAddress == (void *) -1)
261  elog(FATAL, "shmat(id=%d, addr=%p, flags=0x%x) failed: %m",
262  shmid, requestedAddress, PG_SHMAT_FLAGS);
263 
264  /* Register on-exit routine to detach new segment before deleting */
266 
267  /*
268  * Store shmem key and ID in data directory lockfile. Format to try to
269  * keep it the same length always (trailing junk in the lockfile won't
270  * hurt, but might confuse humans).
271  */
272  {
273  char line[64];
274 
275  sprintf(line, "%9lu %9lu",
276  (unsigned long) memKey, (unsigned long) shmid);
278  }
279 
280  return memAddress;
281 }
int errdetail(const char *fmt,...)
Definition: elog.c:1037
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define PG_SHMAT_FLAGS
Definition: mem.h:20
void AddToDataDirLockFile(int target_line, const char *str)
Definition: miscinit.c:1351
#define LOCK_FILE_LINE_SHMEM_KEY
Definition: pidfile.h:43
#define sprintf
Definition: port.h:227
#define IPCProtection
Definition: posix_sema.c:59
#define Int32GetDatum(X)
Definition: postgres.h:523
#define PointerGetDatum(X)
Definition: postgres.h:600
int IpcMemoryId
Definition: sysv_shmem.c:74
static void IpcMemoryDetach(int status, Datum shmaddr)
Definition: sysv_shmem.c:289
static void IpcMemoryDelete(int status, Datum shmId)
Definition: sysv_shmem.c:301
#define IPC_RMID
Definition: win32_port.h:94
#define IPC_EXCL
Definition: win32_port.h:96
#define IPC_CREAT
Definition: win32_port.h:95
#define EIDRM
Definition: win32_port.h:103

References AddToDataDirLockFile(), EIDRM, elog, ereport, errdetail(), errhint(), errmsg(), FATAL, Int32GetDatum, IPC_CREAT, IPC_EXCL, IPC_RMID, IpcMemoryDelete(), IpcMemoryDetach(), IPCProtection, LOCK_FILE_LINE_SHMEM_KEY, LOG, on_shmem_exit(), PG_SHMAT_FLAGS, PointerGetDatum, and sprintf.

Referenced by PGSharedMemoryCreate().

◆ IpcMemoryDelete()

static void IpcMemoryDelete ( int  status,
Datum  shmId 
)
static

Definition at line 301 of file sysv_shmem.c.

302 {
303  if (shmctl(DatumGetInt32(shmId), IPC_RMID, NULL) < 0)
304  elog(LOG, "shmctl(%d, %d, 0) failed: %m",
305  DatumGetInt32(shmId), IPC_RMID);
306 }
#define DatumGetInt32(X)
Definition: postgres.h:516

References DatumGetInt32, elog, IPC_RMID, and LOG.

Referenced by InternalIpcMemoryCreate().

◆ IpcMemoryDetach()

static void IpcMemoryDetach ( int  status,
Datum  shmaddr 
)
static

Definition at line 289 of file sysv_shmem.c.

290 {
291  /* Detach System V shared memory block. */
292  if (shmdt((void *) DatumGetPointer(shmaddr)) < 0)
293  elog(LOG, "shmdt(%p) failed: %m", DatumGetPointer(shmaddr));
294 }
#define DatumGetPointer(X)
Definition: postgres.h:593

References DatumGetPointer, elog, and LOG.

Referenced by InternalIpcMemoryCreate().

◆ PGSharedMemoryAttach()

static IpcMemoryState PGSharedMemoryAttach ( IpcMemoryId  shmId,
void *  attachAt,
PGShmemHeader **  addr 
)
static

Definition at line 350 of file sysv_shmem.c.

353 {
354  struct shmid_ds shmStat;
355  struct stat statbuf;
356  PGShmemHeader *hdr;
357 
358  *addr = NULL;
359 
360  /*
361  * First, try to stat the shm segment ID, to see if it exists at all.
362  */
363  if (shmctl(shmId, IPC_STAT, &shmStat) < 0)
364  {
365  /*
366  * EINVAL actually has multiple possible causes documented in the
367  * shmctl man page, but we assume it must mean the segment no longer
368  * exists.
369  */
370  if (errno == EINVAL)
371  return SHMSTATE_ENOENT;
372 
373  /*
374  * EACCES implies we have no read permission, which means it is not a
375  * Postgres shmem segment (or at least, not one that is relevant to
376  * our data directory).
377  */
378  if (errno == EACCES)
379  return SHMSTATE_FOREIGN;
380 
381  /*
382  * Some Linux kernel versions (in fact, all of them as of July 2007)
383  * sometimes return EIDRM when EINVAL is correct. The Linux kernel
384  * actually does not have any internal state that would justify
385  * returning EIDRM, so we can get away with assuming that EIDRM is
386  * equivalent to EINVAL on that platform.
387  */
388 #ifdef HAVE_LINUX_EIDRM_BUG
389  if (errno == EIDRM)
390  return SHMSTATE_ENOENT;
391 #endif
392 
393  /*
394  * Otherwise, we had better assume that the segment is in use. The
395  * only likely case is (non-Linux, assumed spec-compliant) EIDRM,
396  * which implies that the segment has been IPC_RMID'd but there are
397  * still processes attached to it.
398  */
400  }
401 
402  /*
403  * Try to attach to the segment and see if it matches our data directory.
404  * This avoids any risk of duplicate-shmem-key conflicts on machines that
405  * are running several postmasters under the same userid.
406  *
407  * (When we're called from PGSharedMemoryCreate, this stat call is
408  * duplicative; but since this isn't a high-traffic case it's not worth
409  * trying to optimize.)
410  */
411  if (stat(DataDir, &statbuf) < 0)
412  return SHMSTATE_ANALYSIS_FAILURE; /* can't stat; be conservative */
413 
414  hdr = (PGShmemHeader *) shmat(shmId, attachAt, PG_SHMAT_FLAGS);
415  if (hdr == (PGShmemHeader *) -1)
416  {
417  /*
418  * Attachment failed. The cases we're interested in are the same as
419  * for the shmctl() call above. In particular, note that the owning
420  * postmaster could have terminated and removed the segment between
421  * shmctl() and shmat().
422  *
423  * If attachAt isn't NULL, it's possible that EINVAL reflects a
424  * problem with that address not a vanished segment, so it's best to
425  * pass NULL when probing for conflicting segments.
426  */
427  if (errno == EINVAL)
428  return SHMSTATE_ENOENT; /* segment disappeared */
429  if (errno == EACCES)
430  return SHMSTATE_FOREIGN; /* must be non-Postgres */
431 #ifdef HAVE_LINUX_EIDRM_BUG
432  if (errno == EIDRM)
433  return SHMSTATE_ENOENT; /* segment disappeared */
434 #endif
435  /* Otherwise, be conservative. */
437  }
438  *addr = hdr;
439 
440  if (hdr->magic != PGShmemMagic ||
441  hdr->device != statbuf.st_dev ||
442  hdr->inode != statbuf.st_ino)
443  {
444  /*
445  * It's either not a Postgres segment, or not one for my data
446  * directory.
447  */
448  return SHMSTATE_FOREIGN;
449  }
450 
451  /*
452  * It does match our data directory, so now test whether any processes are
453  * still attached to it. (We are, now, but the shm_nattch result is from
454  * before we attached to it.)
455  */
456  return shmStat.shm_nattch == 0 ? SHMSTATE_UNATTACHED : SHMSTATE_ATTACHED;
457 }
char * DataDir
Definition: globals.c:66
#define PGShmemMagic
Definition: pg_shmem.h:32
#define stat
Definition: win32_port.h:283
#define IPC_STAT
Definition: win32_port.h:99

References DataDir, EIDRM, IPC_STAT, PG_SHMAT_FLAGS, PGShmemMagic, SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, SHMSTATE_UNATTACHED, stat::st_dev, stat::st_ino, and stat.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryIsInUse().

◆ PGSharedMemoryCreate()

PGShmemHeader* PGSharedMemoryCreate ( Size  size,
PGShmemHeader **  shim 
)

Definition at line 678 of file sysv_shmem.c.

680 {
681  IpcMemoryKey NextShmemSegID;
682  void *memAddress;
683  PGShmemHeader *hdr;
684  struct stat statbuf;
685  Size sysvsize;
686 
687  /*
688  * We use the data directory's ID info (inode and device numbers) to
689  * positively identify shmem segments associated with this data dir, and
690  * also as seeds for searching for a free shmem key.
691  */
692  if (stat(DataDir, &statbuf) < 0)
693  ereport(FATAL,
695  errmsg("could not stat data directory \"%s\": %m",
696  DataDir)));
697 
698  /* Complain if hugepages demanded but we can't possibly support them */
699 #if !defined(MAP_HUGETLB)
700  if (huge_pages == HUGE_PAGES_ON)
701  ereport(ERROR,
702  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
703  errmsg("huge pages not supported on this platform")));
704 #endif
705 
706  /* For now, we don't support huge pages in SysV memory */
708  ereport(ERROR,
709  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
710  errmsg("huge pages not supported with the current shared_memory_type setting")));
711 
712  /* Room for a header? */
713  Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
714 
716  {
718  AnonymousShmemSize = size;
719 
720  /* Register on-exit routine to unmap the anonymous segment */
722 
723  /* Now we need only allocate a minimal-sized SysV shmem block. */
724  sysvsize = sizeof(PGShmemHeader);
725  }
726  else
727  sysvsize = size;
728 
729  /*
730  * Loop till we find a free IPC key. Trust CreateDataDirLockFile() to
731  * ensure no more than one postmaster per data directory can enter this
732  * loop simultaneously. (CreateDataDirLockFile() does not entirely ensure
733  * that, but prefer fixing it over coping here.)
734  */
735  NextShmemSegID = statbuf.st_ino;
736 
737  for (;;)
738  {
739  IpcMemoryId shmid;
740  PGShmemHeader *oldhdr;
742 
743  /* Try to create new segment */
744  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
745  if (memAddress)
746  break; /* successful create and attach */
747 
748  /* Check shared memory and possibly remove and recreate */
749 
750  /*
751  * shmget() failure is typically EACCES, hence SHMSTATE_FOREIGN.
752  * ENOENT, a narrow possibility, implies SHMSTATE_ENOENT, but one can
753  * safely treat SHMSTATE_ENOENT like SHMSTATE_FOREIGN.
754  */
755  shmid = shmget(NextShmemSegID, sizeof(PGShmemHeader), 0);
756  if (shmid < 0)
757  {
758  oldhdr = NULL;
760  }
761  else
762  state = PGSharedMemoryAttach(shmid, NULL, &oldhdr);
763 
764  switch (state)
765  {
767  case SHMSTATE_ATTACHED:
768  ereport(FATAL,
769  (errcode(ERRCODE_LOCK_FILE_EXISTS),
770  errmsg("pre-existing shared memory block (key %lu, ID %lu) is still in use",
771  (unsigned long) NextShmemSegID,
772  (unsigned long) shmid),
773  errhint("Terminate any old server processes associated with data directory \"%s\".",
774  DataDir)));
775  break;
776  case SHMSTATE_ENOENT:
777 
778  /*
779  * To our surprise, some other process deleted since our last
780  * InternalIpcMemoryCreate(). Moments earlier, we would have
781  * seen SHMSTATE_FOREIGN. Try that same ID again.
782  */
783  elog(LOG,
784  "shared memory block (key %lu, ID %lu) deleted during startup",
785  (unsigned long) NextShmemSegID,
786  (unsigned long) shmid);
787  break;
788  case SHMSTATE_FOREIGN:
789  NextShmemSegID++;
790  break;
791  case SHMSTATE_UNATTACHED:
792 
793  /*
794  * The segment pertains to DataDir, and every process that had
795  * used it has died or detached. Zap it, if possible, and any
796  * associated dynamic shared memory segments, as well. This
797  * shouldn't fail, but if it does, assume the segment belongs
798  * to someone else after all, and try the next candidate.
799  * Otherwise, try again to create the segment. That may fail
800  * if some other process creates the same shmem key before we
801  * do, in which case we'll try the next key.
802  */
803  if (oldhdr->dsm_control != 0)
805  if (shmctl(shmid, IPC_RMID, NULL) < 0)
806  NextShmemSegID++;
807  break;
808  }
809 
810  if (oldhdr && shmdt((void *) oldhdr) < 0)
811  elog(LOG, "shmdt(%p) failed: %m", oldhdr);
812  }
813 
814  /* Initialize new segment. */
815  hdr = (PGShmemHeader *) memAddress;
816  hdr->creatorPID = getpid();
817  hdr->magic = PGShmemMagic;
818  hdr->dsm_control = 0;
819 
820  /* Fill in the data directory ID info, too */
821  hdr->device = statbuf.st_dev;
822  hdr->inode = statbuf.st_ino;
823 
824  /*
825  * Initialize space allocation status for segment.
826  */
827  hdr->totalsize = size;
828  hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
829  *shim = hdr;
830 
831  /* Save info for possible future use */
832  UsedShmemSegAddr = memAddress;
833  UsedShmemSegID = (unsigned long) NextShmemSegID;
834 
835  /*
836  * If AnonymousShmem is NULL here, then we're not using anonymous shared
837  * memory, and should return a pointer to the System V shared memory
838  * block. Otherwise, the System V shared memory block is only a shim, and
839  * we must return a pointer to the real block.
840  */
841  if (AnonymousShmem == NULL)
842  return hdr;
843  memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
844  return (PGShmemHeader *) AnonymousShmem;
845 }
#define MAXALIGN(LEN)
Definition: c.h:757
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
Definition: dsm.c:212
int errcode_for_file_access(void)
Definition: elog.c:716
int errcode(int sqlerrcode)
Definition: elog.c:693
#define ERROR
Definition: elog.h:33
int shared_memory_type
Definition: ipci.c:53
@ SHMEM_TYPE_MMAP
Definition: pg_shmem.h:62
struct PGShmemHeader PGShmemHeader
uintptr_t Datum
Definition: postgres.h:411
dsm_handle dsm_control
Definition: pg_shmem.h:36
ino_t inode
Definition: pg_shmem.h:40
Size freeoffset
Definition: pg_shmem.h:35
pid_t creatorPID
Definition: pg_shmem.h:33
dev_t device
Definition: pg_shmem.h:39
int32 magic
Definition: pg_shmem.h:31
Size totalsize
Definition: pg_shmem.h:34
Definition: regguts.h:318
static void AnonymousShmemDetach(int status, Datum arg)
Definition: sysv_shmem.c:653
static void * CreateAnonymousSegment(Size *size)
Definition: sysv_shmem.c:585
key_t IpcMemoryKey
Definition: sysv_shmem.c:73
unsigned long UsedShmemSegID
Definition: sysv_shmem.c:97
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
Definition: sysv_shmem.c:124
void * UsedShmemSegAddr
Definition: sysv_shmem.c:98
static IpcMemoryState PGSharedMemoryAttach(IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
Definition: sysv_shmem.c:350

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(), errhint(), errmsg(), ERROR, FATAL, PGShmemHeader::freeoffset, huge_pages, HUGE_PAGES_ON, PGShmemHeader::inode, InternalIpcMemoryCreate(), IPC_RMID, LOG, PGShmemHeader::magic, MAXALIGN, on_shmem_exit(), PGSharedMemoryAttach(), PGShmemMagic, shared_memory_type, SHMEM_TYPE_MMAP, SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, SHMSTATE_UNATTACHED, stat::st_dev, stat::st_ino, stat, PGShmemHeader::totalsize, UsedShmemSegAddr, and UsedShmemSegID.

Referenced by CreateSharedMemoryAndSemaphores().

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 942 of file sysv_shmem.c.

943 {
944  if (UsedShmemSegAddr != NULL)
945  {
946  if ((shmdt(UsedShmemSegAddr) < 0)
947 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
948  /* Work-around for cygipc exec bug */
949  && shmdt(NULL) < 0
950 #endif
951  )
952  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
953  UsedShmemSegAddr = NULL;
954  }
955 
956  if (AnonymousShmem != NULL)
957  {
958  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
959  elog(LOG, "munmap(%p, %zu) failed: %m",
961  AnonymousShmem = NULL;
962  }
963 }

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

Referenced by SysLogger_Start().

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 320 of file sysv_shmem.c.

321 {
322  PGShmemHeader *memAddress;
324 
325  state = PGSharedMemoryAttach((IpcMemoryId) id2, NULL, &memAddress);
326  if (memAddress && shmdt((void *) memAddress) < 0)
327  elog(LOG, "shmdt(%p) failed: %m", memAddress);
328  switch (state)
329  {
330  case SHMSTATE_ENOENT:
331  case SHMSTATE_FOREIGN:
332  case SHMSTATE_UNATTACHED:
333  return false;
335  case SHMSTATE_ATTACHED:
336  return true;
337  }
338  return true;
339 }

References elog, LOG, PGSharedMemoryAttach(), SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, and SHMSTATE_UNATTACHED.

Referenced by CreateLockFile().

Variable Documentation

◆ AnonymousShmem

void* AnonymousShmem = NULL
static

Definition at line 101 of file sysv_shmem.c.

Referenced by AnonymousShmemDetach(), PGSharedMemoryCreate(), and PGSharedMemoryDetach().

◆ AnonymousShmemSize

Size AnonymousShmemSize
static

Definition at line 100 of file sysv_shmem.c.

Referenced by AnonymousShmemDetach(), PGSharedMemoryCreate(), and PGSharedMemoryDetach().

◆ UsedShmemSegAddr

void* UsedShmemSegAddr = NULL

Definition at line 98 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryDetach().

◆ UsedShmemSegID

unsigned long UsedShmemSegID = 0

Definition at line 97 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate().