irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

sharedmemory.cpp
Go to the documentation of this file.
1 /* For copyright information please refer to files in the COPYRIGHT directory
2  */
3 
4 #include <fcntl.h>
5 #include "sharedmemory.hpp"
6 #include "rodsConnect.h"
8 
9 namespace bi = boost::interprocess;
10 
11 static std::map<std::string,std::unique_ptr<bi::shared_memory_object>> shm_obj;
12 static std::map<std::string,std::unique_ptr<bi::mapped_region>> mapped;
13 
14 unsigned char *prepareServerSharedMemory( const std::string& _key ) {
15  std::string shared_memory_name;
16  irods::error ret = getSharedMemoryName( _key, shared_memory_name );
17  if ( !ret.ok() ) {
18  rodsLog( LOG_ERROR, "prepareServerSharedMemory: failed to get shared memory name" );
19  return NULL;
20  }
21 
22  try {
23  bi::shared_memory_object* tmp_s = new bi::shared_memory_object(
24  bi::open_or_create,
25  shared_memory_name.c_str(),
26  bi::read_write,
27  0600 );
28  shm_obj[_key] = std::unique_ptr<bi::shared_memory_object>(tmp_s);
29 
30  bi::offset_t size;
31  if ( tmp_s->get_size( size ) && size == 0 ) {
32  tmp_s->truncate( SHMMAX );
33  }
34 
35  bi::mapped_region* tmp_m = new bi::mapped_region( *tmp_s, bi::read_write );
36  unsigned char *shmBuf = ( unsigned char * ) tmp_m->get_address();
37 
38  std::memset( shmBuf, 0, tmp_m->get_size() );
39  strncpy( (char*)shmBuf, "UNINITIALIZED", 15 );
40 
41  mapped[_key] = std::unique_ptr<bi::mapped_region>(tmp_m);
42 
43 
44  return shmBuf;
45  }
46  catch ( const bi::interprocess_exception &e ) {
47  rodsLog( LOG_ERROR, "prepareServerSharedMemory: failed to prepare shared memory. Exception caught [%s]", e.what() );
48  return NULL;
49  }
50 }
51 
52 void detachSharedMemory( const std::string& _key ) {
53  //delete mapped;
54  //delete shm_obj;
55 }
56 
57 int removeSharedMemory( const std::string& _key ) {
58  std::string shared_memory_name;
59  irods::error ret = getSharedMemoryName( _key, shared_memory_name );
60  if ( !ret.ok() ) {
61  rodsLog( LOG_ERROR, "removeSharedMemory: failed to get shared memory name" );
62  return RE_SHM_UNLINK_ERROR;
63  }
64 
65  if ( !bi::shared_memory_object::remove( shared_memory_name.c_str() ) ) {
66  rodsLog( LOG_ERROR, "removeSharedMemory: failed to remove shared memory" );
67  return RE_SHM_UNLINK_ERROR;
68  }
69  return 0;
70 }
71 
72 unsigned char *prepareNonServerSharedMemory( const std::string& _key ) {
73  std::string shared_memory_name;
74  irods::error ret = getSharedMemoryName( _key, shared_memory_name );
75  if ( !ret.ok() ) {
76  rodsLog( LOG_ERROR, "prepareNonServerSharedMemory: failed to get shared memory name [%s]", shared_memory_name.c_str() );
77  return NULL;
78  }
79 
80  try {
81  bi::shared_memory_object* tmp_s = new bi::shared_memory_object(
82  bi::open_only,
83  shared_memory_name.c_str(),
84  bi::read_only );
85  shm_obj[_key] = std::unique_ptr<bi::shared_memory_object>(tmp_s);
86  bi::mapped_region* tmp_m = new bi::mapped_region( *tmp_s, bi::read_only );
87  unsigned char *buf = ( unsigned char * ) tmp_m->get_address();
88  mapped[_key] = std::unique_ptr<bi::mapped_region>(tmp_m);
89  return buf;
90  }
91  catch ( const bi::interprocess_exception &e ) {
92  rodsLog( LOG_ERROR, "prepareNonServerSharedMemory: failed to get shared memory object [%s]. Exception caught [%s]", shared_memory_name.c_str(), e.what() );
93  return NULL;
94  }
95 }
96 
97 irods::error getSharedMemoryName( const std::string& _key, std::string &shared_memory_name ) {
98  try {
99  const auto& shared_memory_name_salt = irods::get_server_property<const std::string>(irods::CFG_RE_CACHE_SALT_KW);
100  shared_memory_name = "irods_re_cache_shared_memory_" + _key + "_" + shared_memory_name_salt;
101  } catch ( const irods::exception& e ) {
102  rodsLog( LOG_ERROR, "getSharedMemoryName: failed to retrieve re cache salt from server_properties\n%s", e.what() );
103  return irods::error(e);
104  }
105  return SUCCESS();
106 }
rodsLog
void rodsLog(int level, const char *formatStr,...)
Definition: rodsLog.cpp:86
NULL
#define NULL
Definition: rodsDef.h:70
irods_server_properties.hpp
SHMMAX
#define SHMMAX
Definition: sharedmemory.hpp:14
RE_SHM_UNLINK_ERROR
@ RE_SHM_UNLINK_ERROR
Definition: rodsErrorTable.h:715
detachSharedMemory
void detachSharedMemory(const std::string &_key)
Definition: sharedmemory.cpp:52
LOG_ERROR
#define LOG_ERROR
Definition: rodsLog.h:43
rodsConnect.h
SUCCESS
#define SUCCESS()
Definition: irods_error.hpp:121
prepareNonServerSharedMemory
unsigned char * prepareNonServerSharedMemory(const std::string &_key)
Definition: sharedmemory.cpp:72
removeSharedMemory
int removeSharedMemory(const std::string &_key)
Definition: sharedmemory.cpp:57
mapped
static std::map< std::string, std::unique_ptr< bi::mapped_region > > mapped
Definition: sharedmemory.cpp:12
sharedmemory.hpp
getSharedMemoryName
irods::error getSharedMemoryName(const std::string &_key, std::string &shared_memory_name)
Definition: sharedmemory.cpp:97
terminate_irods_processes.e
e
Definition: terminate_irods_processes.py:19
irods::error
Definition: irods_error.hpp:23
prepareServerSharedMemory
unsigned char * prepareServerSharedMemory(const std::string &_key)
Definition: sharedmemory.cpp:14
irods::experimental::filesystem::client::remove
auto remove(rcComm_t &_comm, const path &_p, remove_options _opts=remove_options::none) -> bool
Definition: filesystem.cpp:699
error
int error
Definition: filesystem.cpp:101
irods::exception
Definition: irods_exception.hpp:15
irods::CFG_RE_CACHE_SALT_KW
const std::string CFG_RE_CACHE_SALT_KW("reCacheSalt")
size
long long size
Definition: filesystem.cpp:102
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
buf
static char buf[64+50+1]
Definition: rsAuthRequest.cpp:21
shm_obj
static std::map< std::string, std::unique_ptr< bi::shared_memory_object > > shm_obj
Definition: sharedmemory.cpp:11