-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathwindows.rs
More file actions
288 lines (264 loc) · 9.6 KB
/
windows.rs
File metadata and controls
288 lines (264 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std::fs::{File, OpenOptions};
use std::io::ErrorKind;
use std::os::windows::{fs::OpenOptionsExt, io::AsRawHandle};
use std::path::PathBuf;
use crate::{log::*, ShmemConf};
use win_sys::*;
use crate::ShmemError;
#[derive(Clone, Default)]
pub struct ShmemConfExt {
allow_raw: bool,
}
impl ShmemConf {
/// If set to true, enables openning raw shared memory that is not managed by this crate
pub fn allow_raw(mut self, allow: bool) -> Self {
self.ext.allow_raw = allow;
self
}
}
pub struct MapData {
owner: bool,
/// Pointer to the first byte of our mapping
/// Keep this above `file_map` so it gets dropped first
pub view: ViewOfFile,
/// The handle to our open mapping
#[allow(dead_code)]
file_map: FileMapping,
/// This file is used for shmem persistence. When an owner wants to drop the mapping,
/// it opens the file with FILE_FLAG_DELETE_ON_CLOSE, renames the file and closes it.
/// This makes it so future calls to open the old mapping will fail (as it was renamed) and
/// deletes the renamed file once all handles have been closed.
#[allow(dead_code)]
persistent_file: Option<File>,
//Shared mapping uid
pub unique_id: String,
//Total size of the mapping
pub map_size: usize,
}
///Teardown UnmapViewOfFile and close CreateMapping handle
impl Drop for MapData {
///Takes care of properly closing the SharedMem
fn drop(&mut self) {
// Inspired by the boost implementation at
// https://github.com/boostorg/interprocess/blob/140b50efb3281fa3898f3a4cf939cfbda174718f/include/boost/interprocess/detail/win32_api.hpp
// Emulate POSIX behavior by
// 1. Opening the mmapped file with `FILE_FLAG_DELETE_ON_CLOSE`, causing it to be
// deleted when all its handles have been closed.
// 2. Renaming the mmapped file to prevent future access/opening.
// Once this has run, existing file/mapping handles remain usable but the file is
// deleted once all handles have been closed and no new handles can be opened
// because the file has been renamed. This matches the behavior of shm_unlink()
// on unix.
if self.owner {
let mut base_path = get_tmp_dir().unwrap();
// 1. Set file attributes so that it deletes itself once everyone has closed it
let file_path = base_path.join(self.unique_id.trim_start_matches('/'));
debug!("Setting mapping to delete after everyone has closed it");
match OpenOptions::new()
.access_mode(GENERIC_READ | GENERIC_WRITE | DELETE)
.share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0)
.create(false)
.attributes((FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE).0)
.open(&file_path)
{
Ok(_) => {
// 2. Rename file to prevent further use
base_path.push(&format!(
"{}_deleted",
self.unique_id.trim_start_matches('/')
));
debug!(
"Renaming {} to {}",
file_path.to_string_lossy(),
base_path.to_string_lossy()
);
if let Err(_e) = std::fs::rename(&file_path, &base_path) {
debug!(
"Failed to rename persistent_file {} : {}",
file_path.to_string_lossy(),
_e
);
}
}
Err(_e) => {
debug!(
"Failed to set DELETE_ON_CLOSE on persistent_file {} : {}",
file_path.to_string_lossy(),
_e
);
}
};
}
}
}
impl MapData {
pub fn set_owner(&mut self, is_owner: bool) -> bool {
let prev_val = self.owner;
self.owner = is_owner;
prev_val
}
pub fn as_mut_ptr(&self) -> *mut u8 {
self.view.as_mut_ptr() as _
}
}
/// Returns the path to a temporary directory in which to store files backing the shared memory. If it
/// doesn't exist, the directory is created.
fn get_tmp_dir() -> Result<PathBuf, ShmemError> {
debug!("Getting & creating shared_memory-rs temp dir");
let mut path = std::env::temp_dir();
path.push("shared_memory-rs");
if path.is_dir() {
return Ok(path);
}
match std::fs::create_dir_all(path.as_path()) {
Ok(_) => Ok(path),
Err(e) if e.kind() == ErrorKind::AlreadyExists => Ok(path),
Err(e) => Err(ShmemError::UnknownOsError(e.raw_os_error().unwrap() as _)),
}
}
fn new_map(
unique_id: &str,
mut map_size: usize,
create: bool,
allow_raw: bool,
writable: bool,
) -> Result<MapData, ShmemError> {
// Create file to back the shared memory
let mut file_path = get_tmp_dir()?;
file_path.push(unique_id.trim_start_matches('/'));
debug!(
"{} persistent_file at {}",
if create { "Creating" } else { "Openning" },
file_path.to_string_lossy()
);
let mut opt = OpenOptions::new();
opt.read(true)
.write(true)
.share_mode((FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE).0)
.attributes((FILE_ATTRIBUTE_TEMPORARY).0);
if create {
opt.create_new(true);
} else {
opt.create(false);
};
let mut persistent_file = None;
let map_h = match opt.open(&file_path) {
Ok(f) => {
//Create/open Mapping using persistent file
debug!(
"{} memory mapping",
if create { "Creating" } else { "Openning" },
);
let high_size: u32 = ((map_size as u64 & 0xFFFF_FFFF_0000_0000_u64) >> 32) as u32;
let low_size: u32 = (map_size as u64 & 0xFFFF_FFFF_u64) as u32;
trace!(
"CreateFileMapping({:?}, NULL, {:X}, {}, {}, '{}')",
HANDLE(f.as_raw_handle() as _),
PAGE_READWRITE.0,
high_size,
low_size,
unique_id,
);
match CreateFileMapping(
HANDLE(f.as_raw_handle() as _),
None,
PAGE_READWRITE,
high_size,
low_size,
unique_id,
) {
Ok(v) => {
persistent_file = Some(f);
v
}
Err(e) => {
let err_code = e.win32_error().unwrap();
return if err_code == ERROR_ALREADY_EXISTS {
Err(ShmemError::MappingIdExists)
} else {
Err(if create {
ShmemError::MapCreateFailed(err_code.0)
} else {
ShmemError::MapOpenFailed(err_code.0)
})
};
}
}
}
Err(e) if e.kind() == ErrorKind::AlreadyExists => return Err(ShmemError::MappingIdExists),
Err(e) => {
if create {
return Err(ShmemError::MapCreateFailed(e.raw_os_error().unwrap() as _));
} else if !allow_raw {
return Err(ShmemError::MapOpenFailed(ERROR_FILE_NOT_FOUND.0));
}
// This may be a mapping that isnt managed by this crate
// Try to open the mapping without any backing file
trace!(
"OpenFileMappingW({:?}, {}, '{}')",
FILE_MAP_ALL_ACCESS,
false,
unique_id,
);
match OpenFileMapping(FILE_MAP_ALL_ACCESS, false, unique_id) {
Ok(h) => h,
Err(e) => {
return Err(ShmemError::MapOpenFailed(e.win32_error().unwrap().0));
}
}
}
};
trace!("0x{:X}", map_h);
//Map mapping into address space
debug!("Loading mapping into address space");
let access = if writable {
FILE_MAP_READ | FILE_MAP_WRITE
} else {
FILE_MAP_READ
};
trace!("MapViewOfFile(0x{:X}, {:X}, 0, 0, 0)", map_h, access.0,);
let map_ptr = match MapViewOfFile(map_h.as_handle(), access, 0, 0, 0) {
Ok(v) => v,
Err(e) => {
return Err(if create {
ShmemError::MapCreateFailed(e.win32_error().unwrap().0)
} else {
ShmemError::MapOpenFailed(e.win32_error().unwrap().0)
})
}
};
trace!("\t{:p}", map_ptr);
if !create {
//Get the real size of the openned mapping
let mut info = MEMORY_BASIC_INFORMATION::default();
if let Err(e) = VirtualQuery(map_ptr.as_mut_ptr(), &mut info) {
return Err(ShmemError::UnknownOsError(e.win32_error().unwrap().0));
}
map_size = info.RegionSize;
}
Ok(MapData {
owner: create,
file_map: map_h,
persistent_file,
unique_id: unique_id.to_string(),
map_size,
view: map_ptr,
})
}
//Creates a mapping specified by the uid and size
pub fn create_mapping(
unique_id: &str,
map_size: usize,
writable: bool,
) -> Result<MapData, ShmemError> {
new_map(unique_id, map_size, true, false, writable)
}
//Opens an existing mapping specified by its uid
pub fn open_mapping(
unique_id: &str,
map_size: usize,
ext: &ShmemConfExt,
writable: bool,
) -> Result<MapData, ShmemError> {
new_map(unique_id, map_size, false, ext.allow_raw, writable)
}