SST  12.0.0
StructuralSimulationToolkit
shmchild.h
1 // Copyright 2009-2022 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-2022, 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 
27 namespace SST {
28 namespace Core {
29 namespace Interprocess {
30 
31 /** Class supports an IPC tunnel between two or more processes, via posix shared memory
32  * This class attaches to an existing tunnel for a child process
33  *
34  * @tparam TunnelType Tunnel definition
35  */
36 template <typename TunnelType>
37 class SHMChild
38 {
39 
40 public:
41  /** Child-side tunnel manager for an IPC tunnel
42  * Accesses an existing Tunnel using shared memory
43  *
44  * @param region_name Name of the shared-memory region to access
45  */
46  SHMChild(const std::string& region_name) : shmPtr(nullptr), 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 ) { shm_unlink(filename.c_str()); }
77  }
78 
79  /** Destructor */
80  virtual ~SHMChild()
81  {
82  delete tunnel;
83  if ( shmPtr ) {
84  munmap(shmPtr, shmSize);
85  shmPtr = nullptr;
86  shmSize = 0;
87  }
88  if ( fd >= 0 ) {
89  close(fd);
90  fd = -1;
91  }
92  }
93 
94  /** return a pointer to the tunnel */
95  TunnelType* getTunnel() { return tunnel; }
96 
97  /** return the name of the shared memory region */
98  const std::string& getRegionName(void) const { return filename; }
99 
100 private:
101  void* shmPtr;
102  int fd;
103 
104  std::string filename;
105  size_t shmSize;
106 
107  TunnelType* tunnel;
108 };
109 
110 } // namespace Interprocess
111 } // namespace Core
112 } // namespace SST
113 
114 #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:46
virtual ~SHMChild()
Destructor.
Definition: shmchild.h:80
TunnelType * getTunnel()
return a pointer to the tunnel
Definition: shmchild.h:95
Class supports an IPC tunnel between two or more processes, via posix shared memory This class attach...
Definition: shmchild.h:37
const std::string & getRegionName(void) const
return the name of the shared memory region
Definition: shmchild.h:98