Imported Upstream version 1.49.0
[platform/upstream/boost.git] / boost / interprocess / detail / xsi_shared_memory_device.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2009-2011. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_XSI_SHARED_MEMORY_DEVICE_HPP
12 #define BOOST_INTERPROCESS_XSI_SHARED_MEMORY_DEVICE_HPP
13
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
16 #include <boost/detail/workaround.hpp>
17
18 #if defined(BOOST_INTERPROCESS_WINDOWS)
19 #error "This header can't be used in Windows operating systems"
20 #endif
21
22 #include <boost/interprocess/creation_tags.hpp>
23 #include <boost/interprocess/exceptions.hpp>
24 #include <boost/interprocess/detail/utilities.hpp>
25 #include <boost/interprocess/detail/os_file_functions.hpp>
26 #include <boost/interprocess/detail/tmp_dir_helpers.hpp>
27 #include <boost/interprocess/interprocess_fwd.hpp>
28 #include <boost/interprocess/exceptions.hpp>
29
30 #include <boost/interprocess/xsi_shared_memory.hpp>
31 #include <boost/interprocess/sync/xsi/xsi_named_mutex.hpp>
32 #include <boost/interprocess/mapped_region.hpp>
33 #include <boost/interprocess/sync/scoped_lock.hpp>
34 #include <cstddef>
35 #include <boost/cstdint.hpp>
36 #include <string>
37 #include <cstring>
38
39 //!\file
40 //!Describes a class representing a native xsi shared memory.
41
42 namespace boost {
43 namespace interprocess {
44
45 class xsi_shared_memory_device
46 {
47    /// @cond
48    BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_shared_memory_file_wrapper)
49    /// @endcond 
50
51    public:
52
53    xsi_shared_memory_device();
54
55    xsi_shared_memory_device(create_only_t, const char *name, mode_t mode, std::size_t size)
56    {  this->priv_open_or_create_name_only(ipcdetail::DoCreate, name, mode, size);  }
57
58    xsi_shared_memory_device(open_or_create_t, const char *name, mode_t mode, std::size_t size)
59    {  this->priv_open_or_create_name_only(ipcdetail::DoOpenOrCreate, name, mode, size);  }
60
61    xsi_shared_memory_device(open_only_t, const char *name, mode_t mode)
62    {  this->priv_open_or_create_name_only(ipcdetail::DoOpen, name, mode, 0);  }
63
64    xsi_shared_memory_device(create_only_t, const char *filepath, boost::uint8_t id, mode_t mode, std::size_t size)
65    {  this->priv_open_or_create_name_id(ipcdetail::DoCreate, name, id, mode, size);  }
66
67    xsi_shared_memory_device(open_or_create_t, const char *filepath, boost::uint8_t id, mode_t mode, std::size_t size)
68    {  this->priv_open_or_create_name_id(ipcdetail::DoOpenOrCreate, id, name, mode, size);  }
69
70    xsi_shared_memory_device(open_only_t, const char *filepath, boost::uint8_t id, mode_t mode)
71    {  this->priv_open_or_create_name_id(ipcdetail::DoOpen, name, id, mode, 0);  }
72
73    xsi_shared_memory_device(BOOST_RV_REF(xsi_shared_memory_device) moved)
74    {  this->swap(moved);   }
75
76    xsi_shared_memory_device &operator=(BOOST_RV_REF(xsi_shared_memory_device) moved)
77    {  
78       xsi_shared_memory_device tmp(boost::move(moved));
79       this->swap(tmp);
80       return *this;  
81    }
82
83    //!Swaps two xsi_shared_memory_device. Does not throw
84    void swap(xsi_shared_memory_device &other);
85
86    //!Destroys *this. The shared memory won't be destroyed, just
87    //!this connection to it. Use remove() to destroy the shared memory.
88    ~xsi_shared_memory_device();
89
90    //!Returns the name of the
91    //!shared memory.
92    const char *get_name() const;
93
94    //!Returns the shared memory ID that
95    //!identifies the shared memory
96    int get_shmid() const;
97
98    //!Returns access
99    //!permissions
100    mode_t get_mode() const;
101
102    //!Returns the mapping handle.
103    //!Never throws
104    mapping_handle_t get_mapping_handle() const;
105
106    //!Erases a XSI shared memory object identified by shmname
107    //!from the system.
108    //!Returns false on error. Never throws
109    static bool remove(const char *shmname);
110
111    //!Erases the XSI shared memory object identified by shmid
112    //!from the system.
113    //!Returns false on error. Never throws
114    static bool remove(int shmid);
115
116    /// @cond
117    private:
118    template<int Dummy>
119    struct info_constants_t
120    {
121       static const std::size_t MaxName = 32;
122       static const std::size_t FirstID = 2;
123       static const std::size_t LastID  = 256;
124       static const std::size_t NumID   = LastID - FirstID;
125    };
126
127    struct info_t
128    {
129       struct names_t
130       {
131          char buf[info_constants_t<0>::MaxName];
132       } names[info_constants_t<0>::NumID];
133    };
134
135    static void priv_obtain_index(mapped_region &m, xsi_named_mutex &m, std::string &path);
136    static bool priv_remove_dead_memory(info_t *info, const char *path);
137
138    bool priv_open_or_create_name_only( ipcdetail::create_enum_t type
139                            , const char *shmname
140                            , mode_t mode
141                            , std::size_t size);
142    bool priv_open_or_create_name_id( ipcdetail::create_enum_t type
143                            , const char *shmname
144                            , boost::uint8_t id
145                            , mode_t mode
146                            , std::size_t size);
147    xsi_shared_memory m_shm;
148    mode_t            m_mode;
149    std::string       m_name;
150    /// @endcond
151 };
152
153 template<int Dummy>
154 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::MaxName;
155
156 template<int Dummy>
157 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::FirstID;
158
159 template<int Dummy>
160 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::LastID;
161
162 template<int Dummy>
163 const std::size_t xsi_shared_memory_device::info_constants_t<Dummy>::NumID;
164
165 /// @cond
166
167 inline xsi_shared_memory_device::xsi_shared_memory_device()
168    : m_shm(), m_mode(invalid_mode), m_name()
169 {}
170
171 inline xsi_shared_memory_device::~xsi_shared_memory_device() 
172 {}
173
174 inline const char *xsi_shared_memory_device::get_name() const
175 {  return m_name.c_str(); }
176
177 inline void xsi_shared_memory_device::swap(xsi_shared_memory_device &other)
178 {
179    m_shm.swap(other.m_shm);
180    std::swap(m_mode,  other.m_mode);
181    m_name.swap(other.m_name);   
182 }
183
184 inline mapping_handle_t xsi_shared_memory_device::get_mapping_handle() const
185 {  return m_shm.get_mapping_handle();   }
186
187 inline mode_t xsi_shared_memory_device::get_mode() const
188 {  return m_mode; }
189
190 inline int xsi_shared_memory::get_shmid() const
191 {  return m_shm.get_shmid(); }
192
193 inline void xsi_shared_memory_device::priv_obtain_index
194    (mapped_region &reg, xsi_named_mutex &mut, std::string &path)
195 {
196    const char *const filename = "xsi_shm_emulation_file";
197    permissions p;
198    p.set_unrestricted();
199    std::string xsi_shm_emulation_file_path;
200    ipcdetail::create_tmp_and_clean_old_and_get_filename(filename, xsi_shm_emulation_file_path);
201    ipcdetail::create_or_open_file(xsi_shm_emulation_file_path.c_str(), read_write, p);
202    const std::size_t MemSize = sizeof(info_t);
203
204    xsi_shared_memory index_shm(open_or_create, xsi_shm_emulation_file_path.c_str(), 1, MemSize, 0666);
205    mapped_region r(index_shm, read_write, 0, MemSize, 0);
206    xsi_named_mutex m(open_or_create, xsi_shm_emulation_file_path.c_str(), 2, 0666);
207    reg = boost::move(r);
208    mut = boost::move(m);
209    path.swap(xsi_shm_emulation_file_path);
210 }
211
212 inline bool xsi_shared_memory_device::priv_remove_dead_memory
213    (xsi_shared_memory_device::info_t *info, const char *path)
214 {
215    bool removed = false;
216    for(std::size_t i = 0; i != info_constants_t<0>::NumID; ++i){
217       if(info->names[i].buf[0]){
218          try{
219             xsi_shared_memory temp( open_only, path, i+info_constants_t<0>::FirstID, 0600);
220          }
221          catch(interprocess_exception &e){
222                if(e.get_error_code() == not_found_error){
223                   std::memset(info->names[i].buf, 0, info_constants_t<0>::MaxName);
224                   removed = true;
225                }
226          }
227       }
228    }
229    return removed;
230 }
231
232 inline bool xsi_shared_memory_device::priv_open_or_create_name_id
233    (ipcdetail::create_enum_t type, const char *filepath, mode_t mode, std::size_t size)
234 {
235    //Set accesses
236    if (mode != read_write && mode != read_only){
237       error_info err = other_error;
238       throw interprocess_exception(err);
239    }
240
241    int perm = (mode == read_only) ? (0444) : (0666);
242
243    if(type == ipcdetail::DoOpen){
244       if(!found){
245          error_info err = not_found_error;
246          throw interprocess_exception(err);
247       }
248       xsi_shared_memory temp(open_only, filepath, id, perm);
249       m_shm = boost::move(temp);
250    }
251    else if(type == ipcdetail::DoCreate){
252       //Try to reuse slot
253       xsi_shared_memory temp(create_only, filepath, id, size, perm);
254       std::strcpy(info->names[target_entry].buf, shmname);
255       m_shm = boost::move(temp);
256    }
257    else{ // if(type == ipcdetail::DoOpenOrCreate){
258       xsi_shared_memory temp(open_or_create, filepath, id, size, perm);
259       m_shm = boost::move(temp);
260    }
261
262    m_mode = mode;
263    m_name.clear();
264    return true;
265 }
266
267 inline bool xsi_shared_memory_device::priv_open_or_create_name_only
268    (ipcdetail::create_enum_t type, const char *shmname, mode_t mode, std::size_t size)
269 {
270    //Set accesses
271    if (mode != read_write && mode != read_only){
272       error_info err = other_error;
273       throw interprocess_exception(err);
274    }
275
276    if (std::strlen(shmname) >= (info_constants_t<0>::MaxName)){
277       error_info err = other_error;
278       throw interprocess_exception(err);
279    }
280
281    {
282       //Obtain index and index lock
283       mapped_region region;
284       xsi_named_mutex mut;
285       std::string xsi_shm_emulation_file_path;
286       priv_obtain_index(region, mut, xsi_shm_emulation_file_path);
287       info_t *info = static_cast<info_t *>(region.get_address());
288       scoped_lock<xsi_named_mutex> lock(mut);
289
290       //Find the correct entry or the first empty index
291       bool found = false;
292       int target_entry = -1;
293       int tries = 2;
294       while(tries--){
295          for(std::size_t i = 0; i != info_constants_t<0>::NumID; ++i){
296             if(target_entry < 0 && !info->names[i].buf[0]){
297                target_entry = static_cast<int>(i);
298             }
299             else if(0 == std::strcmp(info->names[i].buf, shmname)){
300                found = true;
301                target_entry = static_cast<int>(i);
302                break;
303             }
304          }
305          if(target_entry < 0){
306             if(!tries || !priv_remove_dead_memory(info, xsi_shm_emulation_file_path.c_str())){
307                error_info err = out_of_resource_error;
308                throw interprocess_exception(err);
309             }
310          }
311       }
312       //Now handle the result
313       int perm = (mode == read_only) ? (0444) : (0666);
314       if(type == ipcdetail::DoOpen){
315          if(!found){
316             error_info err = not_found_error;
317             throw interprocess_exception(err);
318          }
319          xsi_shared_memory temp( open_only, xsi_shm_emulation_file_path.c_str()
320                                , target_entry+info_constants_t<0>::FirstID, perm);
321          m_shm = boost::move(temp);
322       }
323       else{
324
325          if(type == ipcdetail::DoCreate){
326             //Try to reuse slot
327             xsi_shared_memory temp( create_only, xsi_shm_emulation_file_path.c_str()
328                                   , target_entry+info_constants_t<0>::FirstID, size, perm);
329             std::strcpy(info->names[target_entry].buf, shmname);
330             m_shm = boost::move(temp);
331          }
332          else{ // if(type == ipcdetail::DoOpenOrCreate){
333             xsi_shared_memory temp( open_or_create, xsi_shm_emulation_file_path.c_str()
334                                   , target_entry+info_constants_t<0>::FirstID, size, perm);
335             if(!found){
336                std::memset(info->names[target_entry].buf, 0, info_constants_t<0>::MaxName);
337                std::strcpy(info->names[target_entry].buf, shmname);
338             }
339             m_shm = boost::move(temp);
340          }
341       }
342    }
343
344    m_mode = mode;
345    m_name = shmname;
346    return true;
347 }
348
349 inline bool xsi_shared_memory_device::remove(const char *shmname)
350 {
351    try{
352       //Obtain index and index lockss
353       mapped_region region;
354       xsi_named_mutex mut;
355       std::string xsi_shm_emulation_file_path;
356       priv_obtain_index(region, mut, xsi_shm_emulation_file_path);
357       scoped_lock<xsi_named_mutex> lock(mut);
358       info_t *info = static_cast<info_t *>(region.get_address());
359
360       //Now check and remove
361       bool removed = false;
362
363       for(std::size_t i = 0; i != info_constants_t<0>::NumID; ++i){
364          if(0 == std::strcmp(info->names[i].buf, name)){
365             xsi_shared_memory temp( open_only, xsi_shm_emulation_file_path.c_str()
366                                   , i+info_constants_t<0>::FirstID);
367             if(!xsi_shared_memory::remove(temp.get_shmid()) && (system_error_code() != invalid_argument)){
368                return false;
369             }
370             std::memset(info->names[i].buf, 0, info_constants_t<0>::MaxName);
371             removed = true;
372             break;
373          }
374       }
375       return removed;
376    }
377    catch(...){
378       return false;
379    }
380 }
381
382 inline bool xsi_shared_memory_device::remove(int shmid)
383 {  return xsi_shared_memory::remove(shmid);  }
384
385 ///@endcond
386
387 }  //namespace interprocess {
388 }  //namespace boost {
389
390 #include <boost/interprocess/detail/config_end.hpp>
391
392 #endif   //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_DEVICE_HPP