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:87

Function Documentation

◆ AnonymousShmemDetach()

static void AnonymousShmemDetach ( int  status,
Datum  arg 
)
static

Definition at line 653 of file sysv_shmem.c.

References AnonymousShmem, AnonymousShmemSize, elog, and LOG.

Referenced by PGSharedMemoryCreate().

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 }
static Size AnonymousShmemSize
Definition: sysv_shmem.c:100
#define LOG
Definition: elog.h:26
static void * AnonymousShmem
Definition: sysv_shmem.c:101
#define elog(elevel,...)
Definition: elog.h:232

◆ CreateAnonymousSegment()

static void* CreateAnonymousSegment ( Size size)
static

Definition at line 585 of file sysv_shmem.c.

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().

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 }
#define MAP_FAILED
Definition: mem.h:45
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define FATAL
Definition: elog.h:49
#define PG_MMAP_FLAGS
Definition: mem.h:41
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
int huge_pages
Definition: guc.c:640
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:482
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int *  mmap_flags 
)

Definition at line 482 of file sysv_shmem.c.

Referenced by CreateAnonymousSegment(), and InitializeShmemGUCs().

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 }
static uint64 pg_ceil_log2_64(uint64 num)
Definition: pg_bitutils.h:248
int huge_page_size
Definition: guc.c:641
static char * buf
Definition: pg_test_fsync.c:68
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2459
size_t Size
Definition: c.h:540
int FreeFile(FILE *file)
Definition: fd.c:2658

◆ InternalIpcMemoryCreate()

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

Definition at line 124 of file sysv_shmem.c.

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().

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 errhint(const char *fmt,...)
Definition: elog.c:1156
static void IpcMemoryDetach(int status, Datum shmaddr)
Definition: sysv_shmem.c:289
#define IPC_CREAT
Definition: win32_port.h:95
void AddToDataDirLockFile(int target_line, const char *str)
Definition: miscinit.c:1348
#define PointerGetDatum(X)
Definition: postgres.h:600
#define IPCProtection
Definition: posix_sema.c:59
#define LOCK_FILE_LINE_SHMEM_KEY
Definition: pidfile.h:43
#define PG_SHMAT_FLAGS
Definition: mem.h:20
#define LOG
Definition: elog.h:26
#define sprintf
Definition: port.h:219
#define FATAL
Definition: elog.h:49
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define EIDRM
Definition: win32_port.h:103
int errdetail(const char *fmt,...)
Definition: elog.c:1042
static void IpcMemoryDelete(int status, Datum shmId)
Definition: sysv_shmem.c:301
#define IPC_RMID
Definition: win32_port.h:94
#define ereport(elevel,...)
Definition: elog.h:157
int IpcMemoryId
Definition: sysv_shmem.c:74
#define IPC_EXCL
Definition: win32_port.h:96
#define Int32GetDatum(X)
Definition: postgres.h:523
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ IpcMemoryDelete()

static void IpcMemoryDelete ( int  status,
Datum  shmId 
)
static

Definition at line 301 of file sysv_shmem.c.

References DatumGetInt32, elog, IPC_RMID, and LOG.

Referenced by InternalIpcMemoryCreate().

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
#define LOG
Definition: elog.h:26
#define IPC_RMID
Definition: win32_port.h:94
#define elog(elevel,...)
Definition: elog.h:232

◆ IpcMemoryDetach()

static void IpcMemoryDetach ( int  status,
Datum  shmaddr 
)
static

Definition at line 289 of file sysv_shmem.c.

References DatumGetPointer, elog, and LOG.

Referenced by InternalIpcMemoryCreate().

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

◆ PGSharedMemoryAttach()

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

Definition at line 350 of file sysv_shmem.c.

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().

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 }
#define PG_SHMAT_FLAGS
Definition: mem.h:20
#define EIDRM
Definition: win32_port.h:103
#define PGShmemMagic
Definition: pg_shmem.h:32
char * DataDir
Definition: globals.c:65
#define IPC_STAT
Definition: win32_port.h:99
#define stat
Definition: win32_port.h:283

◆ PGSharedMemoryCreate()

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

Definition at line 678 of file sysv_shmem.c.

Referenced by CreateSharedMemoryAndSemaphores().

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  /* Room for a header? */
707  Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
708 
710  {
712  AnonymousShmemSize = size;
713 
714  /* Register on-exit routine to unmap the anonymous segment */
716 
717  /* Now we need only allocate a minimal-sized SysV shmem block. */
718  sysvsize = sizeof(PGShmemHeader);
719  }
720  else
721  sysvsize = size;
722 
723  /*
724  * Loop till we find a free IPC key. Trust CreateDataDirLockFile() to
725  * ensure no more than one postmaster per data directory can enter this
726  * loop simultaneously. (CreateDataDirLockFile() does not entirely ensure
727  * that, but prefer fixing it over coping here.)
728  */
729  NextShmemSegID = statbuf.st_ino;
730 
731  for (;;)
732  {
733  IpcMemoryId shmid;
734  PGShmemHeader *oldhdr;
736 
737  /* Try to create new segment */
738  memAddress = InternalIpcMemoryCreate(NextShmemSegID, sysvsize);
739  if (memAddress)
740  break; /* successful create and attach */
741 
742  /* Check shared memory and possibly remove and recreate */
743 
744  /*
745  * shmget() failure is typically EACCES, hence SHMSTATE_FOREIGN.
746  * ENOENT, a narrow possibility, implies SHMSTATE_ENOENT, but one can
747  * safely treat SHMSTATE_ENOENT like SHMSTATE_FOREIGN.
748  */
749  shmid = shmget(NextShmemSegID, sizeof(PGShmemHeader), 0);
750  if (shmid < 0)
751  {
752  oldhdr = NULL;
753  state = SHMSTATE_FOREIGN;
754  }
755  else
756  state = PGSharedMemoryAttach(shmid, NULL, &oldhdr);
757 
758  switch (state)
759  {
761  case SHMSTATE_ATTACHED:
762  ereport(FATAL,
763  (errcode(ERRCODE_LOCK_FILE_EXISTS),
764  errmsg("pre-existing shared memory block (key %lu, ID %lu) is still in use",
765  (unsigned long) NextShmemSegID,
766  (unsigned long) shmid),
767  errhint("Terminate any old server processes associated with data directory \"%s\".",
768  DataDir)));
769  break;
770  case SHMSTATE_ENOENT:
771 
772  /*
773  * To our surprise, some other process deleted since our last
774  * InternalIpcMemoryCreate(). Moments earlier, we would have
775  * seen SHMSTATE_FOREIGN. Try that same ID again.
776  */
777  elog(LOG,
778  "shared memory block (key %lu, ID %lu) deleted during startup",
779  (unsigned long) NextShmemSegID,
780  (unsigned long) shmid);
781  break;
782  case SHMSTATE_FOREIGN:
783  NextShmemSegID++;
784  break;
785  case SHMSTATE_UNATTACHED:
786 
787  /*
788  * The segment pertains to DataDir, and every process that had
789  * used it has died or detached. Zap it, if possible, and any
790  * associated dynamic shared memory segments, as well. This
791  * shouldn't fail, but if it does, assume the segment belongs
792  * to someone else after all, and try the next candidate.
793  * Otherwise, try again to create the segment. That may fail
794  * if some other process creates the same shmem key before we
795  * do, in which case we'll try the next key.
796  */
797  if (oldhdr->dsm_control != 0)
799  if (shmctl(shmid, IPC_RMID, NULL) < 0)
800  NextShmemSegID++;
801  break;
802  }
803 
804  if (oldhdr && shmdt(oldhdr) < 0)
805  elog(LOG, "shmdt(%p) failed: %m", oldhdr);
806  }
807 
808  /* Initialize new segment. */
809  hdr = (PGShmemHeader *) memAddress;
810  hdr->creatorPID = getpid();
811  hdr->magic = PGShmemMagic;
812  hdr->dsm_control = 0;
813 
814  /* Fill in the data directory ID info, too */
815  hdr->device = statbuf.st_dev;
816  hdr->inode = statbuf.st_ino;
817 
818  /*
819  * Initialize space allocation status for segment.
820  */
821  hdr->totalsize = size;
822  hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
823  *shim = hdr;
824 
825  /* Save info for possible future use */
826  UsedShmemSegAddr = memAddress;
827  UsedShmemSegID = (unsigned long) NextShmemSegID;
828 
829  /*
830  * If AnonymousShmem is NULL here, then we're not using anonymous shared
831  * memory, and should return a pointer to the System V shared memory
832  * block. Otherwise, the System V shared memory block is only a shim, and
833  * we must return a pointer to the real block.
834  */
835  if (AnonymousShmem == NULL)
836  return hdr;
837  memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
838  return (PGShmemHeader *) AnonymousShmem;
839 }
IpcMemoryState
Definition: sysv_shmem.c:87
pid_t creatorPID
Definition: pg_shmem.h:33
int errhint(const char *fmt,...)
Definition: elog.c:1156
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
Definition: dsm.c:210
dsm_handle dsm_control
Definition: pg_shmem.h:36
struct PGShmemHeader PGShmemHeader
int shared_memory_type
Definition: ipci.c:51
static Size AnonymousShmemSize
Definition: sysv_shmem.c:100
int errcode(int sqlerrcode)
Definition: elog.c:698
#define LOG
Definition: elog.h:26
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
Definition: sysv_shmem.c:124
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
key_t IpcMemoryKey
Definition: sysv_shmem.c:73
static IpcMemoryState PGSharedMemoryAttach(IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
Definition: sysv_shmem.c:350
int errcode_for_file_access(void)
Definition: elog.c:721
void * UsedShmemSegAddr
Definition: sysv_shmem.c:98
static void AnonymousShmemDetach(int status, Datum arg)
Definition: sysv_shmem.c:653
int32 magic
Definition: pg_shmem.h:31
static void * CreateAnonymousSegment(Size *size)
Definition: sysv_shmem.c:585
Size totalsize
Definition: pg_shmem.h:34
uintptr_t Datum
Definition: postgres.h:411
unsigned long UsedShmemSegID
Definition: sysv_shmem.c:97
#define IPC_RMID
Definition: win32_port.h:94
static void * AnonymousShmem
Definition: sysv_shmem.c:101
ino_t inode
Definition: pg_shmem.h:40
#define ereport(elevel,...)
Definition: elog.h:157
dev_t device
Definition: pg_shmem.h:39
#define Assert(condition)
Definition: c.h:804
Definition: regguts.h:317
#define PGShmemMagic
Definition: pg_shmem.h:32
Size freeoffset
Definition: pg_shmem.h:35
size_t Size
Definition: c.h:540
int IpcMemoryId
Definition: sysv_shmem.c:74
#define MAXALIGN(LEN)
Definition: c.h:757
int huge_pages
Definition: guc.c:640
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
char * DataDir
Definition: globals.c:65
#define stat
Definition: win32_port.h:283

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 936 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate(), pgstat_start(), and SysLogger_Start().

937 {
938  if (UsedShmemSegAddr != NULL)
939  {
940  if ((shmdt(UsedShmemSegAddr) < 0)
941 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
942  /* Work-around for cygipc exec bug */
943  && shmdt(NULL) < 0
944 #endif
945  )
946  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
947  UsedShmemSegAddr = NULL;
948  }
949 
950  if (AnonymousShmem != NULL)
951  {
952  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
953  elog(LOG, "munmap(%p, %zu) failed: %m",
955  AnonymousShmem = NULL;
956  }
957 }
static Size AnonymousShmemSize
Definition: sysv_shmem.c:100
#define LOG
Definition: elog.h:26
void * UsedShmemSegAddr
Definition: sysv_shmem.c:98
static void * AnonymousShmem
Definition: sysv_shmem.c:101
#define elog(elevel,...)
Definition: elog.h:232

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 320 of file sysv_shmem.c.

Referenced by CreateLockFile().

321 {
322  PGShmemHeader *memAddress;
324 
325  state = PGSharedMemoryAttach((IpcMemoryId) id2, NULL, &memAddress);
326  if (memAddress && shmdt(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 }
IpcMemoryState
Definition: sysv_shmem.c:87
#define LOG
Definition: elog.h:26
static IpcMemoryState PGSharedMemoryAttach(IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
Definition: sysv_shmem.c:350
Definition: regguts.h:317
int IpcMemoryId
Definition: sysv_shmem.c:74
#define elog(elevel,...)
Definition: elog.h:232

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

◆ UsedShmemSegID

unsigned long UsedShmemSegID = 0

Definition at line 97 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate(), and PostmasterMarkPIDForWorkerNotify().