Skip to content

Commit ccabda3

Browse files
committed
Fix posix behavior for windows & raw mappings
1 parent ba71d28 commit ccabda3

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub struct ShmemConf {
5252
overwrite_flink: bool,
5353
flink_path: Option<PathBuf>,
5454
size: usize,
55+
ext: os_impl::ShmemConfExt,
5556
}
5657
impl Drop for ShmemConf {
5758
fn drop(&mut self) {
@@ -184,6 +185,7 @@ impl ShmemConf {
184185
let mut retry = 0;
185186
loop {
186187
let unique_id = if let Some(ref unique_id) = self.os_id {
188+
retry = 5;
187189
unique_id.as_str()
188190
} else {
189191
let flink_path = self.flink_path.as_ref().unwrap();
@@ -202,7 +204,7 @@ impl ShmemConf {
202204
flink_uid.as_str()
203205
};
204206

205-
match os_impl::open_mapping(unique_id, self.size) {
207+
match os_impl::open_mapping(unique_id, self.size, &self.ext) {
206208
Ok(m) => {
207209
self.size = m.map_size;
208210
self.owner = false;

src/unix.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use nix::unistd::{close, ftruncate};
99

1010
use crate::ShmemError;
1111

12+
#[derive(Clone, Default)]
13+
pub struct ShmemConfExt;
14+
1215
pub struct MapData {
1316
//On linux, you must shm_unlink() the object created for the mapping. It wont disappear automatically.
1417
owner: bool,
@@ -145,7 +148,7 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result<MapData, Shmem
145148
}
146149

147150
/// Opens an existing mapping specified by its uid
148-
pub fn open_mapping(unique_id: &str, _map_size: usize) -> Result<MapData, ShmemError> {
151+
pub fn open_mapping(unique_id: &str, _map_size: usize, _ext: &ShmemConfExt) -> Result<MapData, ShmemError> {
149152
//Open shared memory
150153
debug!("Openning persistent mapping at {}", unique_id);
151154
let shmem_fd = match shm_open(

src/windows.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@ use std::io::ErrorKind;
33
use std::os::windows::{fs::OpenOptionsExt, io::AsRawHandle};
44
use std::path::PathBuf;
55

6-
use crate::log::*;
6+
use crate::{log::*, ShmemConf};
77
use win_sys::*;
88

99
use crate::ShmemError;
1010

11+
#[derive(Clone, Default)]
12+
pub struct ShmemConfExt {
13+
allow_raw: bool,
14+
}
15+
16+
impl ShmemConf {
17+
/// If set to true, enables openning raw shared memory that is not managed by this crate
18+
pub fn allow_raw(mut self, allow: bool) -> Self {
19+
self.ext.allow_raw = allow;
20+
self
21+
}
22+
}
23+
1124
pub struct MapData {
1225
owner: bool,
1326

@@ -118,7 +131,12 @@ fn get_tmp_dir() -> Result<PathBuf, ShmemError> {
118131
}
119132
}
120133

121-
fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, ShmemError> {
134+
fn new_map(
135+
unique_id: &str,
136+
mut map_size: usize,
137+
create: bool,
138+
allow_raw: bool,
139+
) -> Result<MapData, ShmemError> {
122140
// Create file to back the shared memory
123141
let mut file_path = get_tmp_dir()?;
124142
file_path.push(unique_id.trim_start_matches('/'));
@@ -188,6 +206,8 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
188206
Err(e) => {
189207
if create {
190208
return Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _));
209+
} else if !allow_raw {
210+
return Err(ShmemError::MapOpenFailed(ERROR_FILE_NOT_FOUND.0));
191211
}
192212

193213
// This may be a mapping that isnt managed by this crate
@@ -248,10 +268,14 @@ fn new_map(unique_id: &str, map_size: usize, create: bool) -> Result<MapData, Sh
248268

249269
//Creates a mapping specified by the uid and size
250270
pub fn create_mapping(unique_id: &str, map_size: usize) -> Result<MapData, ShmemError> {
251-
new_map(unique_id, map_size, true)
271+
new_map(unique_id, map_size, true, false)
252272
}
253273

254274
//Opens an existing mapping specified by its uid
255-
pub fn open_mapping(unique_id: &str, map_size: usize) -> Result<MapData, ShmemError> {
256-
new_map(unique_id, map_size, false)
275+
pub fn open_mapping(
276+
unique_id: &str,
277+
map_size: usize,
278+
ext: &ShmemConfExt,
279+
) -> Result<MapData, ShmemError> {
280+
new_map(unique_id, map_size, false, ext.allow_raw)
257281
}

0 commit comments

Comments
 (0)