Skip to content

Commit 17735f3

Browse files
Update nix requirement from 0.26 to 0.30 (#4)
* Update nix requirement from 0.26 to 0.30 Updates the requirements on [nix](https://github.com/nix-rust/nix) to permit the latest version. - [Changelog](https://github.com/nix-rust/nix/blob/master/CHANGELOG.md) - [Commits](nix-rust/nix@v0.26.0...v0.30.1) --- updated-dependencies: - dependency-name: nix dependency-version: 0.30.1 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> * fix: Update unix.rs for nix 0.30 API compatibility * Convert file values to raw file descriptors --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: 0x5457 <0x5457@protonmail.com>
1 parent e4deb4a commit 17735f3

2 files changed

Lines changed: 42 additions & 35 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ rand = "0.9"
3131
log = { version = "0.4", optional = true }
3232

3333
[target.'cfg(unix)'.dependencies]
34-
nix = { version = "0.26", default-features = false, features = ["fs", "mman"] }
34+
nix = { version = "0.30", default-features = false, features = ["fs", "mman"] }
3535
libc = "0.2"
3636

3737
[target.'cfg(windows)'.dependencies]

src/unix.rs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::num::NonZeroUsize;
2+
use std::os::fd::{FromRawFd, OwnedFd};
23
use std::os::unix::fs::OpenOptionsExt;
34
use std::os::unix::io::AsRawFd;
4-
use std::os::unix::io::RawFd;
5-
use std::ptr::null_mut;
5+
use std::ptr::{null_mut, NonNull};
66

77
use crate::log::*;
88
use nix::fcntl::OFlag;
99
use nix::sys::mman::{mmap, munmap, shm_open, shm_unlink, MapFlags, ProtFlags};
1010
use nix::sys::stat::{fstat, Mode};
11-
use nix::unistd::{close, ftruncate};
11+
use nix::unistd::ftruncate;
1212

1313
use crate::ShmemError;
1414

@@ -20,7 +20,7 @@ pub struct MapData {
2020
owner: bool,
2121

2222
//File descriptor to our open mapping
23-
map_fd: RawFd,
23+
map_fd: OwnedFd,
2424

2525
//Shared mapping uid
2626
pub unique_id: String,
@@ -49,13 +49,18 @@ impl Drop for MapData {
4949
self.map_ptr,
5050
self.map_size
5151
);
52-
if let Err(_e) = unsafe { munmap(self.map_ptr as *mut _, self.map_size) } {
52+
if let Err(_e) = unsafe {
53+
munmap(
54+
NonNull::new_unchecked(self.map_ptr as *mut _),
55+
self.map_size,
56+
)
57+
} {
5358
debug!("Failed to munmap() shared memory mapping : {}", _e);
5459
};
5560
}
5661

5762
//Unlink shmem
58-
if self.map_fd != 0 {
63+
if self.map_fd.as_raw_fd() != 0 {
5964
//unlink shmem if we created it
6065
if self.owner {
6166
debug!("Deleting persistent mapping");
@@ -74,13 +79,9 @@ impl Drop for MapData {
7479
}
7580
}
7681

77-
trace!("close({})", self.map_fd);
78-
if let Err(_e) = close(self.map_fd) {
79-
debug!(
80-
"os_impl::Linux : Failed to close() shared memory file descriptor : {}",
81-
_e
82-
);
83-
};
82+
trace!("close({})", self.map_fd.as_raw_fd());
83+
// Note: OwnedFd automatically closes on drop, so we don't need to call close
84+
// The file descriptor will be closed when self.map_fd is dropped
8485
}
8586
}
8687
}
@@ -115,7 +116,7 @@ pub fn create_mapping(
115116
unique_id,
116117
OFlag::O_CREAT | OFlag::O_EXCL | OFlag::O_RDWR,
117118
mode,
118-
v
119+
v.as_raw_fd()
119120
);
120121
v
121122
}
@@ -134,8 +135,12 @@ pub fn create_mapping(
134135

135136
//Enlarge the memory descriptor file size to the requested map size
136137
debug!("Creating memory mapping");
137-
trace!("ftruncate({}, {})", new_map.map_fd, new_map.map_size);
138-
match ftruncate(new_map.map_fd, new_map.map_size as _) {
138+
trace!(
139+
"ftruncate({}, {})",
140+
new_map.map_fd.as_raw_fd(),
141+
new_map.map_size
142+
);
143+
match ftruncate(&new_map.map_fd, new_map.map_size as _) {
139144
Ok(_) => {}
140145
Err(e) => return Err(ShmemError::UnknownOsError(e as u32)),
141146
};
@@ -148,7 +153,7 @@ pub fn create_mapping(
148153
nz_map_size, //size of mapping
149154
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, //Permissions on pages
150155
MapFlags::MAP_SHARED, //What kind of mapping
151-
new_map.map_fd, //fd
156+
&new_map.map_fd, //fd
152157
0, //Offset into fd
153158
)
154159
} {
@@ -158,10 +163,10 @@ pub fn create_mapping(
158163
new_map.map_size,
159164
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
160165
MapFlags::MAP_SHARED,
161-
new_map.map_fd,
166+
new_map.map_fd.as_raw_fd(),
162167
v
163168
);
164-
v as *mut _
169+
v.as_ptr() as *mut u8
165170
}
166171
Err(e) => return Err(ShmemError::MapCreateFailed(e as u32)),
167172
};
@@ -188,7 +193,7 @@ pub fn open_mapping(
188193
unique_id,
189194
OFlag::O_RDWR,
190195
Mode::S_IRUSR,
191-
v
196+
v.as_raw_fd()
192197
);
193198
v
194199
}
@@ -205,7 +210,7 @@ pub fn open_mapping(
205210
};
206211

207212
//Get mmap size
208-
new_map.map_size = match fstat(new_map.map_fd) {
213+
new_map.map_size = match fstat(&new_map.map_fd) {
209214
Ok(v) => v.st_size as usize,
210215
Err(e) => return Err(ShmemError::MapOpenFailed(e as u32)),
211216
};
@@ -220,7 +225,7 @@ pub fn open_mapping(
220225
nz_map_size, //size of mapping
221226
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, //Permissions on pages
222227
MapFlags::MAP_SHARED, //What kind of mapping
223-
new_map.map_fd, //fd
228+
&new_map.map_fd, //fd
224229
0, //Offset into fd
225230
)
226231
} {
@@ -230,10 +235,10 @@ pub fn open_mapping(
230235
new_map.map_size,
231236
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
232237
MapFlags::MAP_SHARED,
233-
new_map.map_fd,
238+
new_map.map_fd.as_raw_fd(),
234239
v
235240
);
236-
v as *mut _
241+
v.as_ptr() as *mut u8
237242
}
238243
Err(e) => return Err(ShmemError::MapOpenFailed(e as u32)),
239244
};
@@ -274,10 +279,11 @@ pub fn create_mapping_tmpfs(
274279
})?;
275280

276281
let fd = file.as_raw_fd();
282+
let owned_fd = unsafe { OwnedFd::from_raw_fd(fd) };
277283

278284
// Set file size
279285
trace!("ftruncate({}, {})", fd, map_size);
280-
match ftruncate(fd, map_size as _) {
286+
match ftruncate(&owned_fd, map_size as _) {
281287
Ok(_) => {}
282288
Err(e) => return Err(ShmemError::UnknownOsError(e as u32)),
283289
}
@@ -290,7 +296,7 @@ pub fn create_mapping_tmpfs(
290296
nz_map_size, // Size of mapping
291297
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, // Permissions on pages
292298
MapFlags::MAP_SHARED, // What kind of mapping
293-
fd, // File descriptor
299+
&owned_fd, // File descriptor
294300
0, // Offset into fd
295301
)
296302
} {
@@ -300,10 +306,10 @@ pub fn create_mapping_tmpfs(
300306
map_size,
301307
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
302308
MapFlags::MAP_SHARED,
303-
fd,
309+
owned_fd.as_raw_fd(),
304310
v
305311
);
306-
v as *mut u8
312+
v.as_ptr() as *mut u8
307313
}
308314
Err(e) => return Err(ShmemError::MapCreateFailed(e as u32)),
309315
};
@@ -314,7 +320,7 @@ pub fn create_mapping_tmpfs(
314320
Ok(MapData {
315321
owner: true,
316322
unique_id: String::from(file_path),
317-
map_fd: fd,
323+
map_fd: owned_fd,
318324
map_size,
319325
map_ptr,
320326
is_tmpfs: true,
@@ -335,9 +341,10 @@ pub fn open_mapping_tmpfs(file_path: &str, _expected_size: usize) -> Result<MapD
335341
.map_err(|e| ShmemError::MapOpenFailed(e.raw_os_error().unwrap_or(0) as u32))?;
336342

337343
let fd = file.as_raw_fd();
344+
let owned_fd = unsafe { OwnedFd::from_raw_fd(fd) };
338345

339346
// Get file size
340-
let map_size = match fstat(fd) {
347+
let map_size = match fstat(&owned_fd) {
341348
Ok(v) => v.st_size as usize,
342349
Err(e) => return Err(ShmemError::MapOpenFailed(e as u32)),
343350
};
@@ -352,7 +359,7 @@ pub fn open_mapping_tmpfs(file_path: &str, _expected_size: usize) -> Result<MapD
352359
nz_map_size, // Size of mapping
353360
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, // Permissions on pages
354361
MapFlags::MAP_SHARED, // What kind of mapping
355-
fd, // File descriptor
362+
&owned_fd, // File descriptor
356363
0, // Offset into fd
357364
)
358365
} {
@@ -362,10 +369,10 @@ pub fn open_mapping_tmpfs(file_path: &str, _expected_size: usize) -> Result<MapD
362369
map_size,
363370
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
364371
MapFlags::MAP_SHARED,
365-
fd,
372+
owned_fd.as_raw_fd(),
366373
v
367374
);
368-
v as *mut u8
375+
v.as_ptr() as *mut u8
369376
}
370377
Err(e) => return Err(ShmemError::MapOpenFailed(e as u32)),
371378
};
@@ -376,7 +383,7 @@ pub fn open_mapping_tmpfs(file_path: &str, _expected_size: usize) -> Result<MapD
376383
Ok(MapData {
377384
owner: false,
378385
unique_id: String::from(file_path),
379-
map_fd: fd,
386+
map_fd: owned_fd,
380387
map_size,
381388
map_ptr,
382389
is_tmpfs: true,

0 commit comments

Comments
 (0)