]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/detail/shared_dir_helpers.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / detail / shared_dir_helpers.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-2014. 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_DETAIL_SHARED_DIR_HELPERS_HPP
12 #define BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_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/interprocess/detail/os_file_functions.hpp>
25 #include <boost/interprocess/errors.hpp>
26 #include <boost/interprocess/exceptions.hpp>
27 #include <string>
28
29 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME) && defined(BOOST_INTERPROCESS_WINDOWS)
30 #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
31 #endif
32
33 namespace boost {
34 namespace interprocess {
35 namespace ipcdetail {
36
37 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
38 #if defined(BOOST_INTERPROCESS_WINDOWS)
39 //This type will initialize the stamp
40 struct windows_bootstamp
41 {
42 windows_bootstamp()
43 {
44 //Throw if bootstamp not available
45 if(!winapi::get_last_bootup_time(stamp)){
46 error_info err = system_error_code();
47 throw interprocess_exception(err);
48 }
49 }
50 //Use std::string. Even if this will be constructed in shared memory, all
51 //modules/dlls are from this process so internal raw pointers to heap are always valid
52 std::string stamp;
53 };
54
55 inline void get_bootstamp(std::string &s, bool add = false)
56 {
57 const windows_bootstamp &bootstamp = windows_intermodule_singleton<windows_bootstamp>::get();
58 if(add){
59 s += bootstamp.stamp;
60 }
61 else{
62 s = bootstamp.stamp;
63 }
64 }
65 #elif defined(BOOST_INTERPROCESS_HAS_BSD_KERNEL_BOOTTIME)
66 inline void get_bootstamp(std::string &s, bool add = false)
67 {
68 // FreeBSD specific: sysctl "kern.boottime"
69 int request[2] = { CTL_KERN, KERN_BOOTTIME };
70 struct ::timeval result;
71 std::size_t result_len = sizeof result;
72
73 if (::sysctl (request, 2, &result, &result_len, 0, 0) < 0)
74 return;
75
76 char bootstamp_str[256];
77
78 const char Characters [] =
79 { '0', '1', '2', '3', '4', '5', '6', '7'
80 , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
81
82 std::size_t char_counter = 0;
83 //32 bit values to allow 32 and 64 bit process IPC
84 boost::uint32_t fields[2] = { boost::uint32_t(result.tv_sec), boost::uint32_t(result.tv_usec) };
85 for(std::size_t field = 0; field != 2; ++field){
86 for(std::size_t i = 0; i != sizeof(fields[0]); ++i){
87 const char *ptr = (const char *)&fields[field];
88 bootstamp_str[char_counter++] = Characters[(ptr[i]&0xF0)>>4];
89 bootstamp_str[char_counter++] = Characters[(ptr[i]&0x0F)];
90 }
91 }
92 bootstamp_str[char_counter] = 0;
93 if(add){
94 s += bootstamp_str;
95 }
96 else{
97 s = bootstamp_str;
98 }
99 }
100 #else
101 #error "BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME defined with no known implementation"
102 #endif
103 #endif //#if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
104
105 inline void get_shared_dir_root(std::string &dir_path)
106 {
107 #if defined (BOOST_INTERPROCESS_WINDOWS)
108 winapi::get_shared_documents_folder(dir_path);
109 #else
110 dir_path = "/tmp";
111 #endif
112 //We always need this path, so throw on error
113 if(dir_path.empty()){
114 error_info err = system_error_code();
115 throw interprocess_exception(err);
116 }
117 //Remove final null.
118 dir_path += "/boost_interprocess";
119 }
120
121 inline void get_shared_dir(std::string &shared_dir)
122 {
123 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
124 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
125 #else
126 get_shared_dir_root(shared_dir);
127 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
128 shared_dir += "/";
129 get_bootstamp(shared_dir, true);
130 #endif
131 #endif
132 }
133
134 inline void shared_filepath(const char *filename, std::string &filepath)
135 {
136 get_shared_dir(filepath);
137 filepath += "/";
138 filepath += filename;
139 }
140
141 inline void create_shared_dir_and_clean_old(std::string &shared_dir)
142 {
143 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
144 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
145 #else
146 //First get the temp directory
147 std::string root_shared_dir;
148 get_shared_dir_root(root_shared_dir);
149
150 //If fails, check that it's because already exists
151 if(!create_directory(root_shared_dir.c_str())){
152 error_info info(system_error_code());
153 if(info.get_error_code() != already_exists_error){
154 throw interprocess_exception(info);
155 }
156 }
157
158 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
159 get_shared_dir(shared_dir);
160
161 //If fails, check that it's because already exists
162 if(!create_directory(shared_dir.c_str())){
163 error_info info(system_error_code());
164 if(info.get_error_code() != already_exists_error){
165 throw interprocess_exception(info);
166 }
167 }
168 //Now erase all old directories created in the previous boot sessions
169 std::string subdir = shared_dir;
170 subdir.erase(0, root_shared_dir.size()+1);
171 delete_subdirectories(root_shared_dir, subdir.c_str());
172 #else
173 shared_dir = root_shared_dir;
174 #endif
175 #endif
176 }
177
178 inline void create_shared_dir_cleaning_old_and_get_filepath(const char *filename, std::string &shared_dir)
179 {
180 create_shared_dir_and_clean_old(shared_dir);
181 shared_dir += "/";
182 shared_dir += filename;
183 }
184
185 inline void add_leading_slash(const char *name, std::string &new_name)
186 {
187 if(name[0] != '/'){
188 new_name = '/';
189 }
190 new_name += name;
191 }
192
193 } //namespace boost{
194 } //namespace interprocess {
195 } //namespace ipcdetail {
196
197 #include <boost/interprocess/detail/config_end.hpp>
198
199 #endif //ifndef BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP