Add function to make mmap shmem available for child processes (#2390)

This commit is contained in:
Valentin Huber 2024-07-13 13:22:32 +02:00 committed by GitHub
parent 79cbc16800
commit 4f7444395a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -651,11 +651,11 @@ pub mod unix_shmem {
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
ptr, slice, ptr, slice,
}; };
use std::process; use std::{io, process};
use libc::{ use libc::{
c_int, c_uchar, close, ftruncate, mmap, munmap, shm_open, shm_unlink, shmat, shmctl, c_int, c_uchar, close, fcntl, ftruncate, mmap, munmap, shm_open, shm_unlink, shmat,
shmdt, shmget, shmctl, shmdt, shmget,
}; };
use crate::{ use crate::{
@ -825,6 +825,35 @@ pub mod unix_shmem {
pub fn filename_path(&self) -> &Option<[u8; MAX_MMAP_FILENAME_LEN]> { pub fn filename_path(&self) -> &Option<[u8; MAX_MMAP_FILENAME_LEN]> {
&self.filename_path &self.filename_path
} }
/// If called, the shared memory will also be available in subprocesses.
///
/// You likely want to pass the [`crate::shmem::ShMemDescription`] and reopen the shared memory in the child process using [`crate::shmem::ShMemProvider::shmem_from_description`].
///
/// # Errors
///
/// This function will return an error if the appropriate flags could not be extracted or set.
pub fn persist_for_child_processes(&self) -> Result<&Self, Error> {
unsafe {
let flags = fcntl(self.shm_fd, libc::F_GETFD);
if flags == -1 {
return Err(Error::os_error(
io::Error::last_os_error(),
"Failed to retrieve FD flags",
));
}
if fcntl(self.shm_fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC) == -1 {
return Err(Error::os_error(
io::Error::last_os_error(),
"Failed to set FD flags",
));
}
}
Ok(self)
}
} }
/// A [`ShMemProvider`] which uses [`shm_open`] and [`mmap`] to provide shared memory mappings. /// A [`ShMemProvider`] which uses [`shm_open`] and [`mmap`] to provide shared memory mappings.