shared_memory_pos.cpp
Go to the documentation of this file.
1 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 
22 #include "../../include/ecl/ipc/shared_memory_pos.hpp"
23 
24 #ifdef ECL_HAS_POSIX_SHARED_MEMORY
25 
26 #include <string>
27 #include <sys/mman.h> /* For shm_open() */
28 #include <fcntl.h> /* For O_* constants */
29 #include <errno.h>
31 #include <iostream>
32 /*****************************************************************************
33 ** Namespaces
34 *****************************************************************************/
35 
36 namespace ecl {
37 namespace ipc {
38 
39 void SharedMemoryBase::unlink() {
40  shm_unlink(name.c_str());
41 }
42 
43 int SharedMemoryBase::open() {
44  /*********************
45  * Open Flags
46  *********************/
47  /*
48  * O_CREAT : it will try and create some memory or just open if it already exists
49  * if the shared memory object already exists.
50  * O_EXCL : when used with O_CREAT fails to open if the message queue already exists.
51  * : if neither, then it will only open an existing memory.
52  * O_WRONLY : write only.
53  * O_RDONLY : read only.
54  * O_RDWR : read-write.
55  * O_NONBLOCK : normally it will block if sending to a full queue. Disable this.
56  */
57  static const int open_flags = O_RDWR;
58  static const int create_flags = O_CREAT|O_RDWR|O_EXCL;
59  /*********************
60  * Permissions
61  *********************/
62  /*
63  * Permissions. Must be specified when O_CRELOC is used, otherwise it is ignored. It
64  * is combined with the process umask (permissions & ~umask).
65  * S_IRWXU [00700] - read, write & exec by user
66  * S_IRUSR [00400] - read by the user who owns it
67  * S_IWUSR [00200] - write by user
68  * S_IXUSR [00100] - exec by user
69  * S_IRWXG [00070] - read, write & exec by group
70  * S_IRGRP [00040] - read by group
71  * S_IWGRP [00020] - write by group
72  * S_IXGRP [00010] - exe by group
73  * S_IRWXG [00007] - read, write & exec by other
74  * S_IROTH [00004] - read by other
75  * S_IWOTH [00002] - write by other
76  * S_IXOTH [00001] - exe by other
77  */
78  static const int permissions = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
79  /*************************************************************************
80  ** Name
81  *************************************************************************/
82  /*
83  * The name is like a filename. To be compatible on all systems, always
84  * begin with a leading '/' and include no other '/'s.
85  * In debug mode, might be worth parsing the name here.
86  */
87  /*************************************************************************
88  ** Open
89  *************************************************************************/
90  int shm_descriptor = shm_open(name.c_str(),create_flags,permissions);
91  if ( ( shm_descriptor == -1 ) && ( errno == EEXIST ) ) {
92  // Maybe its already open, try opening it normally
93  shm_descriptor = shm_open(name.c_str(),open_flags,permissions);
94  } else {
95  shared_memory_manager = true;
96  }
97  if ( shm_descriptor == -1) {
98  }
99  return shm_descriptor;
100 }
101 
102 
103 /*****************************************************************************
104 ** Exception Handlers
105 *****************************************************************************/
106 
107 #ifndef ECL_DISABLE_EXCEPTIONS
108 
109 ecl::StandardException openSharedSectionException(const char* loc) {
110  int error_result = errno;
111  switch (error_result) {
112  case ( EACCES ) : {
113  throw StandardException(LOC,PermissionsError,"Opening shared memory failed - permission denied.");
114  break;
115  }
116  // case ( EEXIST ) : { // If using O_EXCL, need this, otherwise no.
117  // // pathname exists, assume its a fifo and continue
118  // break;
119  // }
120  case ( EMFILE ) : case ( ENFILE ) : {
121  throw StandardException(LOC,OutOfResourcesError,"Opening shared memory failed - too many file connections already open.");
122  break;
123  }
124  case ( ENOENT ) : case ( ENAMETOOLONG ) : case ( EINVAL ) : {
125  throw StandardException(LOC,InvalidArgError,"Opening shared memory failed - pathname problem.");
126  break;
127  }
128  case ( ENOSYS ) : {
129  throw StandardException(LOC,NotSupportedError,"Opening shared memory failed - kernel system functions are not available (remake the kernel).");
130  break;
131  }
132  default :
133  {
134  std::ostringstream ostream;
135  ostream << "Posix error " << error_result << ": " << strerror(error_result) << ".";
136  return StandardException(loc, UnknownError, ostream.str());
137  }
138  }
139 }
140 
141 ecl::StandardException memoryMapException(const char* loc) {
142  int error_result = errno;
143  switch (error_result) {
144  case ( EACCES ) : {
145  return StandardException(LOC,PermissionsError,"Shared mapping failed - permission problems (see man mmap).");
146  }
147  case ( EAGAIN ) : {
148  return StandardException(LOC,MemoryError,"Shared mapping failed - file locked or too much memory has been locked.");
149  }
150  case ( EBADF ) : {
151  return StandardException(LOC,InvalidArgError,"Shared mapping failed - not a valid file descriptor (see man mmap).");
152  }
153  case ( EINVAL ) : {
154  return StandardException(LOC,InvalidArgError,"Shared mapping failed - start, length or offset were invalid or MAP_PRIVLOCE and MAP_SHARED were either both present or both obso (see man mmap).");
155  }
156  case ( ENFILE ) : {
157  return StandardException(LOC,OutOfResourcesError,"Shared mapping failed - system limit on the total number of open files has been reached (see man mmap).");
158  }
159  case ( ENODEV ) : {
160  return StandardException(LOC,NotSupportedError,"Shared mapping failed - underlying filesystem of the specified file doesn't support memory mapping (see man mmap).");
161  }
162  case ( ENOMEM ) : {
163  return StandardException(LOC,MemoryError,"Shared mapping failed - no mem available, or max mappings exceeded (see man mmap).");
164  }
165  case ( EPERM ) : {
166  return StandardException(LOC,PermissionsError,"Shared mapping failed - EPERM (see man mmap).");
167  }
168 // case ( ETXBSY ) : {
169 // return StandardException(LOC,"Shared mapping failed.","ETXBSY (see man mmap).");
170 // break;
171 // }
172  default :
173  {
174  std::ostringstream ostream;
175  ostream << "Posix error " << error_result << ": " << strerror(error_result) << ".";
176  return StandardException(loc, UnknownError, ostream.str());
177  }
178  }
179 }
180 #endif /* ECL_DISABLE_EXCEPTIONS */
181 
182 } // namespace ipc
183 } // namespace ecl
184 
185 #endif /* ECL_HAS_POSIX_SHARED_MEMORY */
Embedded control libraries.
PermissionsError
InvalidArgError
NotSupportedError
UnknownError
OutOfResourcesError
MemoryError


ecl_ipc
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:17