inline bool semaphore_open (sem_t *&handle, create_enum_t type, const char *origname, unsigned int count = 0, const permissions &perm = permissions()) { std::string name; #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES add_leading_slash(origname, name); #else create_shared_dir_cleaning_old_and_get_filepath(origname, name); #endif //Create new mapping int oflag = 0; switch(type){ case DoOpen: { //No addition handle = ::sem_open(name.c_str(), oflag); } break; case DoOpenOrCreate: case DoCreate: { while(1){ oflag = (O_CREAT | O_EXCL); handle = ::sem_open(name.c_str(), oflag, perm.get_permissions(), count); if(handle != BOOST_INTERPROCESS_POSIX_SEM_FAILED){ //We can't change semaphore permissions! //::fchmod(handle, perm.get_permissions()); break; } else if(errno == EEXIST && type == DoOpenOrCreate){ oflag = 0; if( (handle = ::sem_open(name.c_str(), oflag)) != BOOST_INTERPROCESS_POSIX_SEM_FAILED || (errno != ENOENT) ){ break; } } else{ break; } } } break; default: { error_info err(other_error); throw interprocess_exception(err); } } //Check for error if(handle == BOOST_INTERPROCESS_POSIX_SEM_FAILED){ throw interprocess_exception(error_info(errno)); } return true; }
inline void windows_named_sync::open_or_create ( create_enum_t creation_type , const char *name , const permissions &perm , windows_named_sync_interface &sync_interface) { std::string aux_str(name); m_file_hnd = winapi::invalid_handle_value; //Use a file to emulate POSIX lifetime semantics. After this logic //we'll obtain the ID of the native handle to open in aux_str { create_shared_dir_cleaning_old_and_get_filepath(name, aux_str); //Create a file with required permissions. m_file_hnd = winapi::create_file ( aux_str.c_str() , winapi::generic_read | winapi::generic_write , creation_type == DoOpen ? winapi::open_existing : (creation_type == DoCreate ? winapi::create_new : winapi::open_always) , 0 , (winapi::interprocess_security_attributes*)perm.get_permissions()); //Obtain OS error in case something has failed error_info err; bool success = false; if(m_file_hnd != winapi::invalid_handle_value){ //Now lock the file const std::size_t buflen = sync_interface.get_data_size(); typedef __int64 unique_id_type; const std::size_t sizeof_file_info = sizeof(unique_id_type) + buflen; winapi::interprocess_overlapped overlapped; if(winapi::lock_file_ex (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){ __int64 filesize = 0; //Obtain the unique id to open the native semaphore. //If file size was created if(winapi::get_file_size(m_file_hnd, filesize)){ unsigned long written_or_read = 0; unique_id_type unique_id_val; if(static_cast<std::size_t>(filesize) != sizeof_file_info){ winapi::set_end_of_file(m_file_hnd); winapi::query_performance_counter(&unique_id_val); const void *buf = sync_interface.buffer_with_init_data_to_file(); //Write unique ID in file. This ID will be used to calculate the semaphore name if(winapi::write_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) && written_or_read == sizeof(unique_id_val) && winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0) && written_or_read == buflen ){ success = true; } winapi::get_file_size(m_file_hnd, filesize); BOOST_ASSERT(std::size_t(filesize) == sizeof_file_info); } else{ void *buf = sync_interface.buffer_to_store_init_data_from_file(); if(winapi::read_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) && written_or_read == sizeof(unique_id_val) && winapi::read_file(m_file_hnd, buf, buflen, &written_or_read, 0) && written_or_read == buflen ){ success = true; } } if(success){ //Now create a global semaphore name based on the unique id char unique_id_name[sizeof(unique_id_val)*2+1]; std::size_t name_suffix_length = sizeof(unique_id_name); bytes_to_str(&unique_id_val, sizeof(unique_id_val), &unique_id_name[0], name_suffix_length); success = sync_interface.open(creation_type, unique_id_name); } } //Obtain OS error in case something has failed err = system_error_code(); //If this fails we have no possible rollback so don't check the return if(!winapi::unlock_file_ex(m_file_hnd, 0, sizeof_file_info, 0, &overlapped)){ err = system_error_code(); } } else{ //Obtain OS error in case something has failed err = system_error_code(); } } else{ err = system_error_code(); } if(!success){ if(m_file_hnd != winapi::invalid_handle_value){ winapi::close_handle(m_file_hnd); m_file_hnd = winapi::invalid_handle_value; } //Throw as something went wrong throw interprocess_exception(err); } } }