Properly close FD on memmap drop

This commit is contained in:
Victor Berger 2017-02-22 11:39:13 +01:00
parent a51a780e77
commit 34accc7da3
1 changed files with 10 additions and 3 deletions

View File

@ -3,7 +3,7 @@ use std::os::unix::io::RawFd;
use std::sync::{RwLock, Once, ONCE_INIT}; use std::sync::{RwLock, Once, ONCE_INIT};
use std::ptr; use std::ptr;
use nix::{c_int, c_void, libc}; use nix::{c_int, c_void, libc, unistd};
use nix::sys::mman; use nix::sys::mman;
use nix::sys::signal::{self, SigAction, Signal, SigHandler}; use nix::sys::signal::{self, SigAction, Signal, SigHandler};
@ -14,7 +14,7 @@ static mut OLD_SIGBUS_HANDLER: *mut SigAction = 0 as *mut SigAction;
pub struct Pool { pub struct Pool {
map: RwLock<MemMap>, map: RwLock<MemMap>,
fd: i32, fd: RawFd,
log: ::slog::Logger log: ::slog::Logger
} }
@ -29,7 +29,7 @@ impl Pool {
trace!(log, "Creating new shm pool"; "fd" => fd as i32, "size" => size); trace!(log, "Creating new shm pool"; "fd" => fd as i32, "size" => size);
Ok(Pool { Ok(Pool {
map: RwLock::new(memmap), map: RwLock::new(memmap),
fd: fd as i32, fd: fd,
log: log log: log
}) })
} }
@ -84,6 +84,13 @@ impl Pool {
} }
} }
impl Drop for Pool {
fn drop(&mut self) {
trace!(self.log, "Deleting SHM pool"; "fd" => self.fd);
let _ = unsafe { unistd::close(self.fd) };
}
}
struct MemMap { struct MemMap {
ptr: *mut u8, ptr: *mut u8,
fd: RawFd, fd: RawFd,