Skip to content

Commit

Permalink
[eclipse-iceoryx#458] Handle shm state file correctly when underlying…
Browse files Browse the repository at this point in the history
… shared memory is gone
  • Loading branch information
elfenpiff committed Jan 13, 2025
1 parent 3aec042 commit 78537df
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions iceoryx2-pal/posix/src/windows/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::posix::{constants::*, settings::*, to_dir_search_string, types::*, Er
use crate::win32call;

use iceoryx2_pal_configuration::PATH_SEPARATOR;
use windows_sys::Win32::Foundation::ERROR_FILE_EXISTS;
use windows_sys::Win32::Storage::FileSystem::{
FindClose, FindFirstFileA, FindNextFileA, FILE_ATTRIBUTE_DIRECTORY, WIN32_FIND_DATAA,
};
Expand Down Expand Up @@ -170,8 +171,7 @@ pub unsafe fn shm_open(name: *const c_char, oflag: int, mode: mode_t) -> int {
}

let last_mapping_error;
(shm_handle, last_mapping_error) =
win32call! {OpenFileMappingA(FILE_MAP_ALL_ACCESS, false as i32, name as *const u8)};
(shm_handle, last_mapping_error) = win32call! {OpenFileMappingA(FILE_MAP_ALL_ACCESS, false as i32, name as *const u8), ignore ERROR_FILE_NOT_FOUND};

if shm_handle == 0 {
Errno::set(Errno::ENOENT);
Expand Down Expand Up @@ -224,18 +224,42 @@ unsafe fn shm_file_path(name: *const c_char, suffix: &[u8]) -> [u8; MAX_PATH_LEN
state_file_path
}

unsafe fn does_file_mapping_exist(name: *const c_char) -> bool {
let name = remove_leading_path_separator(name.cast());
let (shm_handle, _) = win32call! {OpenFileMappingA(FILE_MAP_ALL_ACCESS, false as i32, name as *const u8), ignore ERROR_FILE_NOT_FOUND};
if shm_handle == 0 {
return false;
}

win32call! {CloseHandle(shm_handle)};
true
}

unsafe fn create_state_handle(name: *const c_char) -> HANDLE {
let name = remove_leading_path_separator(name);

let (handle, _) = win32call! {CreateFileA(
shm_file_path(name, SHM_STATE_SUFFIX).as_ptr(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
core::ptr::null::<SECURITY_ATTRIBUTES>(),
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
0,
)};
let create_file = || {
win32call! {CreateFileA(
shm_file_path(name, SHM_STATE_SUFFIX).as_ptr(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
core::ptr::null::<SECURITY_ATTRIBUTES>(),
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
0,
), ignore ERROR_FILE_EXISTS}
};

let (mut handle, error_code) = create_file();

if error_code == ERROR_FILE_EXISTS {
if !does_file_mapping_exist(name) {
remove_state_handle(name);
}

(handle, _) = create_file();
}

handle
}

Expand All @@ -254,6 +278,19 @@ unsafe fn open_state_handle(name: *const c_char) -> HANDLE {
handle
}

unsafe fn remove_state_handle(name: *const c_char) -> int {
let name = remove_leading_path_separator(name);

let (has_deleted_file, _) = win32call! { DeleteFileA(shm_file_path(name, SHM_STATE_SUFFIX).as_ptr()),
ignore ERROR_FILE_NOT_FOUND, ERROR_ACCESS_DENIED};
if has_deleted_file == FALSE {
// TODO: [#9]
Errno::set(Errno::ENOENT);
return -1;
}
0
}

pub(crate) unsafe fn shm_set_size(fd_handle: HANDLE, shm_size: u64) {
if fd_handle == INVALID_HANDLE_VALUE {
return;
Expand Down Expand Up @@ -299,16 +336,7 @@ pub(crate) unsafe fn shm_get_size(fd_handle: HANDLE) -> u64 {
}

pub unsafe fn shm_unlink(name: *const c_char) -> int {
let name = remove_leading_path_separator(name);

let (has_deleted_file, _) = win32call! { DeleteFileA(shm_file_path(name, SHM_STATE_SUFFIX).as_ptr()),
ignore ERROR_FILE_NOT_FOUND, ERROR_ACCESS_DENIED};
if has_deleted_file == FALSE {
// TODO: [#9]
Errno::set(Errno::ENOENT);
return -1;
}
0
remove_state_handle(name)
}

pub unsafe fn mmap(
Expand Down

0 comments on commit 78537df

Please sign in to comment.