1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2009. 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_KEY_HPP
12 #define BOOST_INTERPROCESS_XSI_KEY_HPP
13 
14 #ifndef BOOST_CONFIG_HPP
15 #  include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 #  pragma once
20 #endif
21 
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/detail/workaround.hpp>
25 
26 #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
27 #error "This header can't be used in operating systems without XSI (System V) shared memory support"
28 #endif
29 
30 #include <boost/interprocess/creation_tags.hpp>
31 #include <boost/interprocess/exceptions.hpp>
32 #include <boost/interprocess/detail/utilities.hpp>
33 #include <boost/move/utility_core.hpp>
34 #include <boost/interprocess/detail/os_file_functions.hpp>
35 #include <boost/interprocess/interprocess_fwd.hpp>
36 #include <boost/interprocess/exceptions.hpp>
37 #include <sys/types.h>
38 #include <sys/ipc.h>
39 #include <cstddef>
40 #include <boost/cstdint.hpp>
41 
42 //!\file
43 //!Describes a class representing a xsi key type.
44 
45 namespace boost {
46 namespace interprocess {
47 
48 //!A class that wraps XSI (System V) key_t type.
49 //!This type calculates key_t from path and id using ftok,
50 //!sets key to a specified value,
51 //!or sets key to IPC_PRIVATE using the default constructor.
52 class xsi_key
53 {
54    public:
55 
56    //!Default constructor.
57    //!Represents a private xsi_key.
xsi_key()58    xsi_key()
59       : m_key(IPC_PRIVATE)
60    {}
61 
62    //!Creates a new XSI key using a specified value. Constructor is explicit to avoid ambiguity with shmid.
xsi_key(key_t key)63    explicit xsi_key(key_t key)
64       : m_key(key)
65    {}
66 
67    //!Creates a new XSI  shared memory with a key obtained from a call to ftok (with path
68    //!"path" and id "id"), of size "size" and permissions "perm".
69    //!If the shared memory previously exists, throws an error.
xsi_key(const char * path,boost::uint8_t id)70    xsi_key(const char *path, boost::uint8_t id)
71    {
72       key_t key;
73       if(path){
74          key  = ::ftok(path, id);
75          if(((key_t)-1) == key){
76             error_info err = system_error_code();
77             throw interprocess_exception(err);
78          }
79       }
80       else{
81          key = IPC_PRIVATE;
82       }
83       m_key = key;
84    }
85 
86    //!Returns the internal key_t value
get_key() const87    key_t get_key() const
88    {  return m_key;  }
89 
90    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
91    private:
92    key_t m_key;
93    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
94 };
95 
96 }  //namespace interprocess {
97 }  //namespace boost {
98 
99 #include <boost/interprocess/detail/config_end.hpp>
100 
101 #endif   //BOOST_INTERPROCESS_XSI_KEY_HPP
102