1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP
12 #define BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
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>
29 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME) && defined(BOOST_INTERPROCESS_WINDOWS)
30 #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
34 namespace interprocess {
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
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);
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
55 inline void get_bootstamp(std::string &s, bool add = false)
57 const windows_bootstamp &bootstamp = windows_intermodule_singleton<windows_bootstamp>::get();
65 #elif defined(BOOST_INTERPROCESS_HAS_BSD_KERNEL_BOOTTIME)
66 inline void get_bootstamp(std::string &s, bool add = false)
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;
73 if (::sysctl (request, 2, &result, &result_len, 0, 0) < 0)
76 char bootstamp_str[256];
78 const char Characters [] =
79 { '0', '1', '2', '3', '4', '5', '6', '7'
80 , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
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)];
92 bootstamp_str[char_counter] = 0;
101 #error "BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME defined with no known implementation"
103 #endif //#if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
105 inline void get_shared_dir_root(std::string &dir_path)
107 #if defined (BOOST_INTERPROCESS_WINDOWS)
108 winapi::get_shared_documents_folder(dir_path);
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);
118 dir_path += "/boost_interprocess";
121 inline void get_shared_dir(std::string &shared_dir)
123 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
124 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
126 get_shared_dir_root(shared_dir);
127 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
129 get_bootstamp(shared_dir, true);
134 inline void shared_filepath(const char *filename, std::string &filepath)
136 get_shared_dir(filepath);
138 filepath += filename;
141 inline void create_shared_dir_and_clean_old(std::string &shared_dir)
143 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
144 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
146 //First get the temp directory
147 std::string root_shared_dir;
148 get_shared_dir_root(root_shared_dir);
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);
158 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
159 get_shared_dir(shared_dir);
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);
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());
173 shared_dir = root_shared_dir;
178 inline void create_shared_dir_cleaning_old_and_get_filepath(const char *filename, std::string &shared_dir)
180 create_shared_dir_and_clean_old(shared_dir);
182 shared_dir += filename;
185 inline void add_leading_slash(const char *name, std::string &new_name)
194 } //namespace interprocess {
195 } //namespace ipcdetail {
197 #include <boost/interprocess/detail/config_end.hpp>
199 #endif //ifndef BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP