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/ipc.h>
#include <sys/mman.h>
#include <sys/shm.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/guc_hooks.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)
 
bool check_huge_page_size (int *newval, void **extra, GucSource source)
 
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 71 of file sysv_shmem.c.

◆ IpcMemoryKey

Definition at line 70 of file sysv_shmem.c.

Enumeration Type Documentation

◆ IpcMemoryState

Enumerator
SHMSTATE_ANALYSIS_FAILURE 
SHMSTATE_ATTACHED 
SHMSTATE_ENOENT 
SHMSTATE_FOREIGN 
SHMSTATE_UNATTACHED 

Definition at line 84 of file sysv_shmem.c.

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

Function Documentation

◆ AnonymousShmemDetach()

static void AnonymousShmemDetach ( int  status,
Datum  arg 
)
static

Definition at line 675 of file sysv_shmem.c.

676 {
677  /* Release anonymous shared memory block, if any. */
678  if (AnonymousShmem != NULL)
679  {
680  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
681  elog(LOG, "munmap(%p, %zu) failed: %m",
683  AnonymousShmem = NULL;
684  }
685 }
#define LOG
Definition: elog.h:31
#define elog(elevel,...)
Definition: elog.h:224
static Size AnonymousShmemSize
Definition: sysv_shmem.c:97
static void * AnonymousShmem
Definition: sysv_shmem.c:98

References AnonymousShmem, AnonymousShmemSize, elog, and LOG.

Referenced by PGSharedMemoryCreate().

◆ check_huge_page_size()

bool check_huge_page_size ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 578 of file sysv_shmem.c.

579 {
580 #if !(defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT))
581  /* Recent enough Linux only, for now. See GetHugePageSize(). */
582  if (*newval != 0)
583  {
584  GUC_check_errdetail("huge_page_size must be 0 on this platform.");
585  return false;
586  }
587 #endif
588  return true;
589 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:448

References GUC_check_errdetail, and newval.

◆ CreateAnonymousSegment()

static void* CreateAnonymousSegment ( Size size)
static

Definition at line 599 of file sysv_shmem.c.

600 {
601  Size allocsize = *size;
602  void *ptr = MAP_FAILED;
603  int mmap_errno = 0;
604 
605 #ifndef MAP_HUGETLB
606  /* PGSharedMemoryCreate should have dealt with this case */
608 #else
610  {
611  /*
612  * Round up the request size to a suitable large value.
613  */
614  Size hugepagesize;
615  int mmap_flags;
616 
617  GetHugePageSize(&hugepagesize, &mmap_flags);
618 
619  if (allocsize % hugepagesize != 0)
620  allocsize += hugepagesize - (allocsize % hugepagesize);
621 
622  ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
623  PG_MMAP_FLAGS | mmap_flags, -1, 0);
624  mmap_errno = errno;
625  if (huge_pages == HUGE_PAGES_TRY && ptr == MAP_FAILED)
626  elog(DEBUG1, "mmap(%zu) with MAP_HUGETLB failed, huge pages disabled: %m",
627  allocsize);
628  }
629 #endif
630 
631  /*
632  * Report whether huge pages are in use. This needs to be tracked before
633  * the second mmap() call if attempting to use huge pages failed
634  * previously.
635  */
636  SetConfigOption("huge_pages_status", (ptr == MAP_FAILED) ? "off" : "on",
638 
639  if (ptr == MAP_FAILED && huge_pages != HUGE_PAGES_ON)
640  {
641  /*
642  * Use the original size, not the rounded-up value, when falling back
643  * to non-huge pages.
644  */
645  allocsize = *size;
646  ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
647  PG_MMAP_FLAGS, -1, 0);
648  mmap_errno = errno;
649  }
650 
651  if (ptr == MAP_FAILED)
652  {
653  errno = mmap_errno;
654  ereport(FATAL,
655  (errmsg("could not map anonymous shared memory: %m"),
656  (mmap_errno == ENOMEM) ?
657  errhint("This error usually means that PostgreSQL's request "
658  "for a shared memory segment exceeded available memory, "
659  "swap space, or huge pages. To reduce the request size "
660  "(currently %zu bytes), reduce PostgreSQL's shared "
661  "memory usage, perhaps by reducing shared_buffers or "
662  "max_connections.",
663  allocsize) : 0));
664  }
665 
666  *size = allocsize;
667  return ptr;
668 }
#define Assert(condition)
Definition: c.h:858
size_t Size
Definition: c.h:605
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define FATAL
Definition: elog.h:41
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4275
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:110
@ PGC_INTERNAL
Definition: guc.h:69
int huge_pages
Definition: guc_tables.c:565
#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
static pg_noinline void Size size
Definition: slab.c:607
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:479

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

Referenced by PGSharedMemoryCreate().

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int *  mmap_flags 
)

Definition at line 479 of file sysv_shmem.c.

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

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

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

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(), size, and sprintf.

Referenced by PGSharedMemoryCreate().

◆ IpcMemoryDelete()

static void IpcMemoryDelete ( int  status,
Datum  shmId 
)
static

Definition at line 298 of file sysv_shmem.c.

299 {
300  if (shmctl(DatumGetInt32(shmId), IPC_RMID, NULL) < 0)
301  elog(LOG, "shmctl(%d, %d, 0) failed: %m",
302  DatumGetInt32(shmId), IPC_RMID);
303 }
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202

References DatumGetInt32(), elog, IPC_RMID, and LOG.

Referenced by InternalIpcMemoryCreate().

◆ IpcMemoryDetach()

static void IpcMemoryDetach ( int  status,
Datum  shmaddr 
)
static

Definition at line 286 of file sysv_shmem.c.

287 {
288  /* Detach System V shared memory block. */
289  if (shmdt((void *) DatumGetPointer(shmaddr)) < 0)
290  elog(LOG, "shmdt(%p) failed: %m", DatumGetPointer(shmaddr));
291 }
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312

References DatumGetPointer(), elog, and LOG.

Referenced by InternalIpcMemoryCreate().

◆ PGSharedMemoryAttach()

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

Definition at line 347 of file sysv_shmem.c.

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

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

702 {
703  IpcMemoryKey NextShmemSegID;
704  void *memAddress;
705  PGShmemHeader *hdr;
706  struct stat statbuf;
707  Size sysvsize;
708 
709  /*
710  * We use the data directory's ID info (inode and device numbers) to
711  * positively identify shmem segments associated with this data dir, and
712  * also as seeds for searching for a free shmem key.
713  */
714  if (stat(DataDir, &statbuf) < 0)
715  ereport(FATAL,
717  errmsg("could not stat data directory \"%s\": %m",
718  DataDir)));
719 
720  /* Complain if hugepages demanded but we can't possibly support them */
721 #if !defined(MAP_HUGETLB)
722  if (huge_pages == HUGE_PAGES_ON)
723  ereport(ERROR,
724  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
725  errmsg("huge pages not supported on this platform")));
726 #endif
727 
728  /* For now, we don't support huge pages in SysV memory */
730  ereport(ERROR,
731  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
732  errmsg("huge pages not supported with the current shared_memory_type setting")));
733 
734  /* Room for a header? */
735  Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
736 
738  {
741 
742  /* Register on-exit routine to unmap the anonymous segment */
744 
745  /* Now we need only allocate a minimal-sized SysV shmem block. */
746  sysvsize = sizeof(PGShmemHeader);
747  }
748  else
749  {
750  sysvsize = size;
751 
752  /* huge pages are only available with mmap */
753  SetConfigOption("huge_pages_status", "off",
755  }
756 
757  /*
758  * Loop till we find a free IPC key. Trust CreateDataDirLockFile() to
759  * ensure no more than one postmaster per data directory can enter this
760  * loop simultaneously. (CreateDataDirLockFile() does not entirely ensure
761  * that, but prefer fixing it over coping here.)
762  */
763  NextShmemSegID = statbuf.st_ino;
764 
765  for (;;)
766  {
767  IpcMemoryId shmid;
768  PGShmemHeader *oldhdr;
770 
771  /* Try to create new segment */
772  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
773  if (memAddress)
774  break; /* successful create and attach */
775 
776  /* Check shared memory and possibly remove and recreate */
777 
778  /*
779  * shmget() failure is typically EACCES, hence SHMSTATE_FOREIGN.
780  * ENOENT, a narrow possibility, implies SHMSTATE_ENOENT, but one can
781  * safely treat SHMSTATE_ENOENT like SHMSTATE_FOREIGN.
782  */
783  shmid = shmget(NextShmemSegID, sizeof(PGShmemHeader), 0);
784  if (shmid < 0)
785  {
786  oldhdr = NULL;
788  }
789  else
790  state = PGSharedMemoryAttach(shmid, NULL, &oldhdr);
791 
792  switch (state)
793  {
795  case SHMSTATE_ATTACHED:
796  ereport(FATAL,
797  (errcode(ERRCODE_LOCK_FILE_EXISTS),
798  errmsg("pre-existing shared memory block (key %lu, ID %lu) is still in use",
799  (unsigned long) NextShmemSegID,
800  (unsigned long) shmid),
801  errhint("Terminate any old server processes associated with data directory \"%s\".",
802  DataDir)));
803  break;
804  case SHMSTATE_ENOENT:
805 
806  /*
807  * To our surprise, some other process deleted since our last
808  * InternalIpcMemoryCreate(). Moments earlier, we would have
809  * seen SHMSTATE_FOREIGN. Try that same ID again.
810  */
811  elog(LOG,
812  "shared memory block (key %lu, ID %lu) deleted during startup",
813  (unsigned long) NextShmemSegID,
814  (unsigned long) shmid);
815  break;
816  case SHMSTATE_FOREIGN:
817  NextShmemSegID++;
818  break;
819  case SHMSTATE_UNATTACHED:
820 
821  /*
822  * The segment pertains to DataDir, and every process that had
823  * used it has died or detached. Zap it, if possible, and any
824  * associated dynamic shared memory segments, as well. This
825  * shouldn't fail, but if it does, assume the segment belongs
826  * to someone else after all, and try the next candidate.
827  * Otherwise, try again to create the segment. That may fail
828  * if some other process creates the same shmem key before we
829  * do, in which case we'll try the next key.
830  */
831  if (oldhdr->dsm_control != 0)
833  if (shmctl(shmid, IPC_RMID, NULL) < 0)
834  NextShmemSegID++;
835  break;
836  }
837 
838  if (oldhdr && shmdt((void *) oldhdr) < 0)
839  elog(LOG, "shmdt(%p) failed: %m", oldhdr);
840  }
841 
842  /* Initialize new segment. */
843  hdr = (PGShmemHeader *) memAddress;
844  hdr->creatorPID = getpid();
845  hdr->magic = PGShmemMagic;
846  hdr->dsm_control = 0;
847 
848  /* Fill in the data directory ID info, too */
849  hdr->device = statbuf.st_dev;
850  hdr->inode = statbuf.st_ino;
851 
852  /*
853  * Initialize space allocation status for segment.
854  */
855  hdr->totalsize = size;
856  hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
857  *shim = hdr;
858 
859  /* Save info for possible future use */
860  UsedShmemSegAddr = memAddress;
861  UsedShmemSegID = (unsigned long) NextShmemSegID;
862 
863  /*
864  * If AnonymousShmem is NULL here, then we're not using anonymous shared
865  * memory, and should return a pointer to the System V shared memory
866  * block. Otherwise, the System V shared memory block is only a shim, and
867  * we must return a pointer to the real block.
868  */
869  if (AnonymousShmem == NULL)
870  return hdr;
871  memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
872  return (PGShmemHeader *) AnonymousShmem;
873 }
#define MAXALIGN(LEN)
Definition: c.h:811
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
Definition: dsm.c:238
int errcode_for_file_access(void)
Definition: elog.c:882
int errcode(int sqlerrcode)
Definition: elog.c:859
#define ERROR
Definition: elog.h:39
int shared_memory_type
Definition: ipci.c:57
@ SHMEM_TYPE_MMAP
Definition: pg_shmem.h:63
struct PGShmemHeader PGShmemHeader
uintptr_t Datum
Definition: postgres.h:64
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:323
static void AnonymousShmemDetach(int status, Datum arg)
Definition: sysv_shmem.c:675
static void * CreateAnonymousSegment(Size *size)
Definition: sysv_shmem.c:599
key_t IpcMemoryKey
Definition: sysv_shmem.c:70
unsigned long UsedShmemSegID
Definition: sysv_shmem.c:94
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
Definition: sysv_shmem.c:121
void * UsedShmemSegAddr
Definition: sysv_shmem.c:95
static IpcMemoryState PGSharedMemoryAttach(IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
Definition: sysv_shmem.c:347

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(), PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, PGSharedMemoryAttach(), PGShmemMagic, SetConfigOption(), shared_memory_type, SHMEM_TYPE_MMAP, SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, SHMSTATE_UNATTACHED, size, stat::st_dev, stat::st_ino, stat, PGShmemHeader::totalsize, UsedShmemSegAddr, and UsedShmemSegID.

Referenced by CreateSharedMemoryAndSemaphores().

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 970 of file sysv_shmem.c.

971 {
972  if (UsedShmemSegAddr != NULL)
973  {
974  if ((shmdt(UsedShmemSegAddr) < 0)
975 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
976  /* Work-around for cygipc exec bug */
977  && shmdt(NULL) < 0
978 #endif
979  )
980  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
981  UsedShmemSegAddr = NULL;
982  }
983 
984  if (AnonymousShmem != NULL)
985  {
986  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
987  elog(LOG, "munmap(%p, %zu) failed: %m",
989  AnonymousShmem = NULL;
990  }
991 }

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

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 317 of file sysv_shmem.c.

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

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

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

◆ AnonymousShmemSize

Size AnonymousShmemSize
static

Definition at line 97 of file sysv_shmem.c.

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

◆ UsedShmemSegAddr

void* UsedShmemSegAddr = NULL

Definition at line 95 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryDetach().

◆ UsedShmemSegID

unsigned long UsedShmemSegID = 0

Definition at line 94 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate().