SST  15.1.0
StructuralSimulationToolkit
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 <cerrno>
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 
30 namespace SST::Core::Interprocess {
31 
32 /** Class supports an IPC tunnel between two or more processes, via posix shared memory
33  * This class attaches to an existing tunnel for a child process
34  *
35  * @tparam TunnelType Tunnel definition
36  */
37 template <typename TunnelType>
38 class SHMChild
39 {
40 
41 public:
42  /** Child-side tunnel manager for an IPC tunnel
43  * Accesses an existing Tunnel using shared memory
44  *
45  * @param region_name Name of the shared-memory region to access
46  */
47  explicit SHMChild(const std::string& region_name) :
48  shmPtr(nullptr),
49  fd(-1)
50  {
51  fd = shm_open(region_name.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
52  filename = region_name;
53 
54  if ( fd < 0 ) {
55  // Not using Output because IPC means Output might not be available
56  fprintf(stderr, "Failed to open IPC region '%s': %s\n", filename.c_str(), strerror(errno));
57  exit(1);
58  }
59 
60  shmPtr = mmap(nullptr, sizeof(InternalSharedData), PROT_READ, MAP_SHARED, fd, 0);
61  if ( shmPtr == MAP_FAILED ) {
62  // Not using Output because IPC means Output might not be available
63  fprintf(stderr, "mmap 0 failed: %s\n", strerror(errno));
64  exit(1);
65  }
66 
67  tunnel = new TunnelType(shmPtr);
68  shmSize = tunnel->getTunnelSize();
69 
70  munmap(shmPtr, sizeof(InternalSharedData));
71 
72  shmPtr = mmap(nullptr, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
73  if ( shmPtr == MAP_FAILED ) {
74  // Not using Output because IPC means Output might not be available
75  fprintf(stderr, "mmap 1 failed: %s\n", strerror(errno));
76  exit(1);
77  }
78  uint32_t childnum = tunnel->initialize(shmPtr);
79  if ( childnum == 0 ) {
80  shm_unlink(filename.c_str());
81  }
82  }
83 
84  /** Destructor */
85  virtual ~SHMChild()
86  {
87  delete tunnel;
88  if ( shmPtr ) {
89  munmap(shmPtr, shmSize);
90  shmPtr = nullptr;
91  shmSize = 0;
92  }
93  if ( fd >= 0 ) {
94  close(fd);
95  fd = -1;
96  }
97  }
98 
99  /** return a pointer to the tunnel */
100  TunnelType* getTunnel() { return tunnel; }
101 
102  /** return the name of the shared memory region */
103  const std::string& getRegionName() const { return filename; }
104 
105 private:
106  void* shmPtr;
107  int fd;
108 
109  std::string filename;
110  size_t shmSize;
111 
112  TunnelType* tunnel;
113 };
114 
115 } // namespace SST::Core::Interprocess
116 
117 #endif // SST_CORE_INTERPROCESS_TUNNEL_SHM_CHILD_H
SHMChild(const std::string &region_name)
Child-side tunnel manager for an IPC tunnel Accesses an existing Tunnel using shared memory...
Definition: shmchild.h:47
virtual ~SHMChild()
Destructor.
Definition: shmchild.h:85
TunnelType * getTunnel()
return a pointer to the tunnel
Definition: shmchild.h:100
Class supports an IPC tunnel between two or more processes, via posix shared memory This class attach...
Definition: shmchild.h:38
Definition: circularBuffer.h:20
const std::string & getRegionName() const
return the name of the shared memory region
Definition: shmchild.h:103