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 {
windows_bootstampboost::interprocess::ipcdetail::windows_bootstamp42 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
get_bootstamp(std::string & s,bool add=false)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
get_shared_dir_root(std::string & dir_path)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
113 //We always need this path, so throw on error
114 if(dir_path.empty()){
115 error_info err = system_error_code();
116 throw interprocess_exception(err);
117 }
118 //Remove final null.
119 dir_path += "/boost_interprocess";
120 }
121
122 #if defined(BOOST_INTERPROCESS_SHARED_DIR_FUNC) && defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
123 #error "Error: Both BOOST_INTERPROCESS_SHARED_DIR_FUNC and BOOST_INTERPROCESS_SHARED_DIR_PATH defined!"
124 #endif
125
126 #ifdef BOOST_INTERPROCESS_SHARED_DIR_FUNC
127
128 // When BOOST_INTERPROCESS_SHARED_DIR_FUNC is defined, users have to implement
129 // get_shared_dir
130 void get_shared_dir(std::string &shared_dir);
131
132 #else
get_shared_dir(std::string & shared_dir)133 inline void get_shared_dir(std::string &shared_dir)
134 {
135 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
136 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
137 #else
138 get_shared_dir_root(shared_dir);
139 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
140 shared_dir += "/";
141 get_bootstamp(shared_dir, true);
142 #endif
143 #endif
144 }
145 #endif
146
shared_filepath(const char * filename,std::string & filepath)147 inline void shared_filepath(const char *filename, std::string &filepath)
148 {
149 get_shared_dir(filepath);
150 filepath += "/";
151 filepath += filename;
152 }
153
create_shared_dir_and_clean_old(std::string & shared_dir)154 inline void create_shared_dir_and_clean_old(std::string &shared_dir)
155 {
156 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH) || defined(BOOST_INTERPROCESS_SHARED_DIR_FUNC)
157 get_shared_dir(shared_dir);
158 #else
159 //First get the temp directory
160 std::string root_shared_dir;
161 get_shared_dir_root(root_shared_dir);
162
163 //If fails, check that it's because already exists
164 if(!create_directory(root_shared_dir.c_str())){
165 error_info info(system_error_code());
166 if(info.get_error_code() != already_exists_error){
167 throw interprocess_exception(info);
168 }
169 }
170
171 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
172 get_shared_dir(shared_dir);
173
174 //If fails, check that it's because already exists
175 if(!create_directory(shared_dir.c_str())){
176 error_info info(system_error_code());
177 if(info.get_error_code() != already_exists_error){
178 throw interprocess_exception(info);
179 }
180 }
181 //Now erase all old directories created in the previous boot sessions
182 std::string subdir = shared_dir;
183 subdir.erase(0, root_shared_dir.size()+1);
184 delete_subdirectories(root_shared_dir, subdir.c_str());
185 #else
186 shared_dir = root_shared_dir;
187 #endif
188 #endif
189 }
190
create_shared_dir_cleaning_old_and_get_filepath(const char * filename,std::string & shared_dir)191 inline void create_shared_dir_cleaning_old_and_get_filepath(const char *filename, std::string &shared_dir)
192 {
193 create_shared_dir_and_clean_old(shared_dir);
194 shared_dir += "/";
195 shared_dir += filename;
196 }
197
add_leading_slash(const char * name,std::string & new_name)198 inline void add_leading_slash(const char *name, std::string &new_name)
199 {
200 if(name[0] != '/'){
201 new_name = '/';
202 }
203 new_name += name;
204 }
205
206 } //namespace boost{
207 } //namespace interprocess {
208 } //namespace ipcdetail {
209
210 #include <boost/interprocess/detail/config_end.hpp>
211
212 #endif //ifndef BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP
213