PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "storage/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 voidInternalIpcMemoryCreate (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 voidCreateAnonymousSegment (Size *size)
 
static void AnonymousShmemDetach (int status, Datum arg)
 
PGShmemHeaderPGSharedMemoryCreate (Size size, PGShmemHeader **shim)
 
void PGSharedMemoryDetach (void)
 

Variables

unsigned long UsedShmemSegID = 0
 
voidUsedShmemSegAddr = NULL
 
static Size AnonymousShmemSize
 
static voidAnonymousShmem = NULL
 

Typedef Documentation

◆ IpcMemoryId

Definition at line 72 of file sysv_shmem.c.

◆ IpcMemoryKey

Definition at line 71 of file sysv_shmem.c.

Enumeration Type Documentation

◆ IpcMemoryState

Enumerator
SHMSTATE_ANALYSIS_FAILURE 
SHMSTATE_ATTACHED 
SHMSTATE_ENOENT 
SHMSTATE_FOREIGN 
SHMSTATE_UNATTACHED 

Definition at line 85 of file sysv_shmem.c.

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

Function Documentation

◆ AnonymousShmemDetach()

static void AnonymousShmemDetach ( int  status,
Datum  arg 
)
static

Definition at line 677 of file sysv_shmem.c.

678{
679 /* Release anonymous shared memory block, if any. */
680 if (AnonymousShmem != NULL)
681 {
683 elog(LOG, "munmap(%p, %zu) failed: %m",
686 }
687}
#define LOG
Definition elog.h:31
#define elog(elevel,...)
Definition elog.h:226
static int fb(int x)
static Size AnonymousShmemSize
Definition sysv_shmem.c:98
static void * AnonymousShmem
Definition sysv_shmem.c:99

References AnonymousShmem, AnonymousShmemSize, elog, fb(), and LOG.

Referenced by PGSharedMemoryCreate().

◆ check_huge_page_size()

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

Definition at line 579 of file sysv_shmem.c.

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

References GUC_check_errdetail, and newval.

◆ CreateAnonymousSegment()

static void * CreateAnonymousSegment ( Size size)
static

Definition at line 600 of file sysv_shmem.c.

601{
602 Size allocsize = *size;
603 void *ptr = MAP_FAILED;
604 int mmap_errno = 0;
606
607#ifndef MAP_HUGETLB
608 /* PGSharedMemoryCreate should have dealt with this case */
610#else
612 {
613 /*
614 * Round up the request size to a suitable large value.
615 */
617 int huge_mmap_flags;
618
620
621 if (allocsize % hugepagesize != 0)
622 allocsize = add_size(allocsize, hugepagesize - (allocsize % hugepagesize));
623
624 ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
625 mmap_flags | huge_mmap_flags, -1, 0);
627 if (huge_pages == HUGE_PAGES_TRY && ptr == MAP_FAILED)
628 elog(DEBUG1, "mmap(%zu) with MAP_HUGETLB failed, huge pages disabled: %m",
629 allocsize);
630 }
631#endif
632
633 /*
634 * Report whether huge pages are in use. This needs to be tracked before
635 * the second mmap() call if attempting to use huge pages failed
636 * previously.
637 */
638 SetConfigOption("huge_pages_status", (ptr == MAP_FAILED) ? "off" : "on",
640
641 if (ptr == MAP_FAILED && huge_pages != HUGE_PAGES_ON)
642 {
643 /*
644 * Use the original size, not the rounded-up value, when falling back
645 * to non-huge pages.
646 */
647 allocsize = *size;
648 ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
649 mmap_flags, -1, 0);
651 }
652
653 if (ptr == MAP_FAILED)
654 {
657 (errmsg("could not map anonymous shared memory: %m"),
658 (mmap_errno == ENOMEM) ?
659 errhint("This error usually means that PostgreSQL's request "
660 "for a shared memory segment exceeded available memory, "
661 "swap space, or huge pages. To reduce the request size "
662 "(currently %zu bytes), reduce PostgreSQL's shared "
663 "memory usage, perhaps by reducing \"shared_buffers\" or "
664 "\"max_connections\".",
665 allocsize) : 0));
666 }
667
668 *size = allocsize;
669 return ptr;
670}
#define Assert(condition)
Definition c.h:873
size_t Size
Definition c.h:619
int errhint(const char *fmt,...)
Definition elog.c:1330
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define FATAL
Definition elog.h:41
#define DEBUG1
Definition elog.h:30
#define ereport(elevel,...)
Definition elog.h:150
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4196
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_INTERNAL
Definition guc.h:73
int huge_pages
Definition guc_tables.c:580
#define MAP_FAILED
Definition mem.h:43
#define MAP_HASSEMAPHORE
Definition mem.h:30
#define MAP_ANONYMOUS
Definition mem.h:25
@ HUGE_PAGES_ON
Definition pg_shmem.h:54
@ HUGE_PAGES_TRY
Definition pg_shmem.h:55
Size add_size(Size s1, Size s2)
Definition shmem.c:482
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition sysv_shmem.c:480

References add_size(), Assert, DEBUG1, elog, ereport, errhint(), errmsg(), FATAL, fb(), GetHugePageSize(), huge_pages, HUGE_PAGES_ON, HUGE_PAGES_TRY, MAP_ANONYMOUS, MAP_FAILED, MAP_HASSEMAPHORE, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, and SetConfigOption().

Referenced by PGSharedMemoryCreate().

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int mmap_flags 
)

Definition at line 480 of file sysv_shmem.c.

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

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

Referenced by CreateAnonymousSegment(), InitializeShmemGUCs(), and pg_get_shmem_pagesize().

◆ InternalIpcMemoryCreate()

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

Definition at line 122 of file sysv_shmem.c.

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

References AddToDataDirLockFile(), EIDRM, elog, ereport, errdetail(), errhint(), errmsg(), FATAL, fb(), 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 299 of file sysv_shmem.c.

300{
302 elog(LOG, "shmctl(%d, %d, 0) failed: %m",
304}
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212

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

Referenced by InternalIpcMemoryCreate().

◆ IpcMemoryDetach()

static void IpcMemoryDetach ( int  status,
Datum  shmaddr 
)
static

Definition at line 287 of file sysv_shmem.c.

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

References DatumGetPointer(), elog, fb(), and LOG.

Referenced by InternalIpcMemoryCreate().

◆ PGSharedMemoryAttach()

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

Definition at line 348 of file sysv_shmem.c.

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

References DataDir, EIDRM, fb(), IPC_STAT, PG_SHMAT_FLAGS, PGShmemMagic, SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, SHMSTATE_UNATTACHED, and stat.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryIsInUse().

◆ PGSharedMemoryCreate()

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

Definition at line 702 of file sysv_shmem.c.

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

References AnonymousShmem, AnonymousShmemDetach(), AnonymousShmemSize, Assert, PGShmemHeader::content_offset, 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, fb(), 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, stat, PGShmemHeader::totalsize, UsedShmemSegAddr, and UsedShmemSegID.

Referenced by CreateSharedMemoryAndSemaphores().

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 972 of file sysv_shmem.c.

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

References AnonymousShmem, AnonymousShmemSize, elog, fb(), LOG, and UsedShmemSegAddr.

Referenced by postmaster_child_launch().

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 318 of file sysv_shmem.c.

319{
322
324 if (memAddress && shmdt(memAddress) < 0)
325 elog(LOG, "shmdt(%p) failed: %m", memAddress);
326 switch (state)
327 {
328 case SHMSTATE_ENOENT:
329 case SHMSTATE_FOREIGN:
331 return false;
334 return true;
335 }
336 return true;
337}

References elog, fb(), 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 99 of file sysv_shmem.c.

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

◆ AnonymousShmemSize

Size AnonymousShmemSize
static

Definition at line 98 of file sysv_shmem.c.

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

◆ UsedShmemSegAddr

void* UsedShmemSegAddr = NULL

Definition at line 96 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate(), and PGSharedMemoryDetach().

◆ UsedShmemSegID

unsigned long UsedShmemSegID = 0

Definition at line 95 of file sysv_shmem.c.

Referenced by PGSharedMemoryCreate().