SST  10.1.0
StructuralSimulationToolkit
shmchild.h
1 // Copyright 2009-2020 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-2020, 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 1
14 
15 
16 #include <fcntl.h>
17 #include <cstdio>
18 #include <string>
19 #include <errno.h>
20 #include <cstring>
21 #include <cstdlib>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 
26 #include "sst/core/interprocess/tunneldef.h"
27 
28 namespace SST {
29 namespace Core {
30 namespace 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 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",
54  filename.c_str(), strerror(errno));
55  exit(1);
56  }
57 
58  shmPtr = mmap(nullptr, sizeof(InternalSharedData), PROT_READ, MAP_SHARED, fd, 0);
59  if ( shmPtr == MAP_FAILED ) {
60  // Not using Output because IPC means Output might not be available
61  fprintf(stderr, "mmap 0 failed: %s\n", strerror(errno));
62  exit(1);
63  }
64 
65  tunnel = new TunnelType(shmPtr);
66  shmSize = tunnel->getTunnelSize();
67 
68  munmap(shmPtr, sizeof(InternalSharedData));
69 
70  shmPtr = mmap(nullptr, shmSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
71  if ( shmPtr == MAP_FAILED ) {
72  // Not using Output because IPC means Output might not be available
73  fprintf(stderr, "mmap 1 failed: %s\n", strerror(errno));
74  exit(1);
75  }
76  uint32_t childnum = tunnel->initialize(shmPtr);
77  if ( childnum == 0) {
78  shm_unlink(filename.c_str());
79  }
80  }
81 
82 
83  /** Destructor */
84  virtual ~SHMChild()
85  {
86  delete tunnel;
87  if ( shmPtr ) {
88  munmap(shmPtr, shmSize);
89  shmPtr = nullptr;
90  shmSize = 0;
91  }
92  if ( fd >= 0 ) {
93  close(fd);
94  fd = -1;
95  }
96  }
97 
98  /** return a pointer to the tunnel */
99  TunnelType* getTunnel() { return tunnel; }
100 
101  /** return the name of the shared memory region */
102  const std::string& getRegionName(void) const { return filename; }
103 
104 private:
105  void *shmPtr;
106  int fd;
107 
108  std::string filename;
109  size_t shmSize;
110 
111  TunnelType* tunnel;
112 };
113 
114 }
115 }
116 }
117 
118 
119 
120 #endif
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:84
TunnelType * getTunnel()
return a pointer to the tunnel
Definition: shmchild.h:99
Class supports an IPC tunnel between two or more processes, via posix shared memory This class attach...
Definition: shmchild.h:38
const std::string & getRegionName(void) const
return the name of the shared memory region
Definition: shmchild.h:102