-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathposixshmem.rs
More file actions
52 lines (47 loc) · 1.7 KB
/
posixshmem.rs
File metadata and controls
52 lines (47 loc) · 1.7 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
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
pub(crate) use _posixshmem::module_def;
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
#[pymodule]
mod _posixshmem {
use alloc::ffi::CString;
use crate::{
common::os::errno_io_error,
vm::{
FromArgs, PyResult, VirtualMachine, builtins::PyUtf8StrRef, convert::IntoPyException,
},
};
#[derive(FromArgs)]
struct ShmOpenArgs {
#[pyarg(any)]
name: PyUtf8StrRef,
#[pyarg(any)]
flags: libc::c_int,
#[pyarg(any, default = 0o600)]
mode: libc::mode_t,
}
#[pyfunction]
fn shm_open(args: ShmOpenArgs, vm: &VirtualMachine) -> PyResult<libc::c_int> {
let name = CString::new(args.name.as_str()).map_err(|e| e.into_pyexception(vm))?;
let mode: libc::c_uint = args.mode as _;
#[cfg(target_os = "freebsd")]
let mode = mode.try_into().unwrap();
// SAFETY: `name` is a NUL-terminated string and `shm_open` does not write through it.
let fd = unsafe { libc::shm_open(name.as_ptr(), args.flags, mode) };
if fd == -1 {
Err(errno_io_error().into_pyexception(vm))
} else {
Ok(fd)
}
}
#[pyfunction]
fn shm_unlink(name: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult<()> {
let name = CString::new(name.as_str()).map_err(|e| e.into_pyexception(vm))?;
// SAFETY: `name` is a valid NUL-terminated string and `shm_unlink` only reads it.
let ret = unsafe { libc::shm_unlink(name.as_ptr()) };
if ret == -1 {
Err(errno_io_error().into_pyexception(vm))
} else {
Ok(())
}
}
}