SST 15.0
Structural Simulation Toolkit
shmchild.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_INTERPROCESS_TUNNEL_SHM_CHILD_H
13#define SST_CORE_INTERPROCESS_TUNNEL_SHM_CHILD_H
14
15#include "sst/core/interprocess/tunneldef.h"
16
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <errno.h>
21#include <fcntl.h>
22#include <string>
23#include <sys/mman.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
27namespace SST::Core::Interprocess {
28
29/** Class supports an IPC tunnel between two or more processes, via posix shared memory
30 * This class attaches to an existing tunnel for a child process
31 *
32 * @tparam TunnelType Tunnel definition
33 */
34template <typename TunnelType>
36{
37
38public:
39 /** Child-side tunnel manager for an IPC tunnel
40 * Accesses an existing Tunnel using shared memory
41 *
42 * @param region_name Name of the shared-memory region to access
43 */
44 explicit SHMChild(const std::string& region_name) :
45 shmPtr(nullptr),
46 fd(-1)
47 {
48 fd = shm_open(region_name.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
49 filename = region_name;
50
51 if ( fd < 0 ) {
52 // Not using Output because IPC means Output might not be available
53 fprintf(stderr, "Failed to open IPC region '%s': %s\n", filename.c_str(), strerror(errno));
54 exit(1);
55 }
56
57 shmPtr = mmap(nullptr, sizeof(InternalSharedData), PROT_READ, MAP_SHARED, fd, 0);
58 if ( shmPtr == MAP_FAILED ) {
59 // Not using Output because IPC means Output might not be available
60 fprintf(stderr, "mmap 0 failed: %s\n", strerror(errno));
61 exit(1);
62 }
63
64 tunnel = new TunnelType(shmPtr);
65 shmSize = tunnel->getTunnelSize();
66
67 munmap(shmPtr, sizeof(InternalSharedData));
68
69 shmPtr = mmap(nullptr, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
70 if ( shmPtr == MAP_FAILED ) {
71 // Not using Output because IPC means Output might not be available
72 fprintf(stderr, "mmap 1 failed: %s\n", strerror(errno));
73 exit(1);
74 }
75 uint32_t childnum = tunnel->initialize(shmPtr);
76 if ( childnum == 0 ) {
77 shm_unlink(filename.c_str());
78 }
79 }
80
81 /** Destructor */
82 virtual ~SHMChild()
83 {
84 delete tunnel;
85 if ( shmPtr ) {
86 munmap(shmPtr, shmSize);
87 shmPtr = nullptr;
88 shmSize = 0;
89 }
90 if ( fd >= 0 ) {
91 close(fd);
92 fd = -1;
93 }
94 }
95
96 /** return a pointer to the tunnel */
97 TunnelType* getTunnel() { return tunnel; }
98
99 /** return the name of the shared memory region */
100 const std::string& getRegionName() const { return filename; }
101
102private:
103 void* shmPtr;
104 int fd;
105
106 std::string filename;
107 size_t shmSize;
108
109 TunnelType* tunnel;
110};
111
112} // namespace SST::Core::Interprocess
113
114#endif // SST_CORE_INTERPROCESS_TUNNEL_SHM_CHILD_H
const std::string & getRegionName() const
return the name of the shared memory region
Definition shmchild.h:100
TunnelType * getTunnel()
return a pointer to the tunnel
Definition shmchild.h:97
SHMChild(const std::string &region_name)
Child-side tunnel manager for an IPC tunnel Accesses an existing Tunnel using shared memory.
Definition shmchild.h:44
virtual ~SHMChild()
Destructor.
Definition shmchild.h:82