From 34accc7da3f9f4cf689563c73a9bffd9ee148fe1 Mon Sep 17 00:00:00 2001 From: Victor Berger Date: Wed, 22 Feb 2017 11:39:13 +0100 Subject: [PATCH] Properly close FD on memmap drop --- src/shm/pool.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/shm/pool.rs b/src/shm/pool.rs index c2fae05..886007c 100644 --- a/src/shm/pool.rs +++ b/src/shm/pool.rs @@ -3,7 +3,7 @@ use std::os::unix::io::RawFd; use std::sync::{RwLock, Once, ONCE_INIT}; 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::signal::{self, SigAction, Signal, SigHandler}; @@ -14,7 +14,7 @@ static mut OLD_SIGBUS_HANDLER: *mut SigAction = 0 as *mut SigAction; pub struct Pool { map: RwLock, - fd: i32, + fd: RawFd, log: ::slog::Logger } @@ -29,7 +29,7 @@ impl Pool { trace!(log, "Creating new shm pool"; "fd" => fd as i32, "size" => size); Ok(Pool { map: RwLock::new(memmap), - fd: fd as i32, + fd: fd, 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 { ptr: *mut u8, fd: RawFd,