summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2017-08-29 16:33:28 -0700
committerchrome-bot <chrome-bot@chromium.org>2017-08-30 13:04:16 -0700
commitee73bf34a841fd7720e1af0c7a1a3346fc6c5768 (patch)
tree06981dadf2cfeae965faeb4c87808681cf164387 /src/main.rs
parent56077cf8635b6e4928130843553e08be1d139eee (diff)
downloadcrosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.tar
crosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.tar.gz
crosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.tar.bz2
crosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.tar.lz
crosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.tar.xz
crosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.tar.zst
crosvm-ee73bf34a841fd7720e1af0c7a1a3346fc6c5768.zip
crosvm: unlink control sockets bound to files
This is a regression from the control socket refactoring. The removal of
the control socket receiver class, whose drop impl handled the removal
of the socket's files, meant that no code took care of cleaning them up.

BUG=None
TEST=Run with `-s./` and after a clean exit, make sure there is no
     *.sock files in the current directory.

Change-Id: I0064900f4eec6d054d174d59a4aefdf36ab4d3b3
Reviewed-on: https://chromium-review.googlesource.com/642510
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Jason Clinton <jclinton@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 63819a1..d480aff 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -25,8 +25,7 @@ pub mod device_manager;
 
 use std::ffi::{CString, CStr};
 use std::fmt;
-use std::fs::File;
-use std::fs::OpenOptions;
+use std::fs::{File, OpenOptions, remove_file};
 use std::io::{stdin, stdout};
 use std::net;
 use std::os::unix::net::UnixDatagram;
@@ -136,6 +135,24 @@ impl fmt::Display for Error {
 
 type Result<T> = std::result::Result<T, Error>;
 
+struct UnlinkUnixDatagram(UnixDatagram);
+impl AsRef<UnixDatagram> for UnlinkUnixDatagram {
+    fn as_ref(&self) -> &UnixDatagram{
+        &self.0
+    }
+}
+impl Drop for UnlinkUnixDatagram {
+    fn drop(&mut self) {
+        if let Ok(addr) = self.0.local_addr() {
+            if let Some(path) = addr.as_pathname() {
+                if let Err(e) = remove_file(path) {
+                    warn!("failed to remove control socket file: {:?}", e);
+                }
+            }
+        }
+    }
+}
+
 struct DiskOption<'a> {
     path: &'a str,
     writable: bool,
@@ -228,7 +245,7 @@ fn run_config(cfg: Config) -> Result<()> {
                 UnixDatagram::bind(path)
             }
             .map_err(|e| Error::Socket(e))?;
-        control_sockets.push(control_socket);
+        control_sockets.push(UnlinkUnixDatagram(control_socket));
     }
 
     let mem_size = cfg.memory.unwrap_or(256) << 20;
@@ -331,7 +348,7 @@ fn run_kvm(requests: Vec<VmRequest>,
            vcpu_count: u32,
            guest_mem: GuestMemory,
            mmio_bus: &hw::Bus,
-           control_sockets: Vec<UnixDatagram>,
+           control_sockets: Vec<UnlinkUnixDatagram>,
            warn_unknown_ports: bool)
            -> Result<()> {
     let kvm = Kvm::new().map_err(Error::Kvm)?;
@@ -521,7 +538,7 @@ fn run_kvm(requests: Vec<VmRequest>,
 }
 
 fn run_control(mut vm: Vm,
-               control_sockets: Vec<UnixDatagram>,
+               control_sockets: Vec<UnlinkUnixDatagram>,
                mut next_dev_pfn: u64,
                stdio_serial: Arc<Mutex<hw::Serial>>,
                exit_evt: EventFd,
@@ -547,7 +564,7 @@ fn run_control(mut vm: Vm,
     pollables.push((STDIN, &stdin_lock as &Pollable));
     pollables.push((CHILD_SIGNAL, &sigchld_fd as &Pollable));
     for (i, socket) in control_sockets.iter().enumerate() {
-        pollables.push((VM_BASE + i as u32, socket as &Pollable));
+        pollables.push((VM_BASE + i as u32, socket.as_ref() as &Pollable));
     }
 
     let mut poller = Poller::new(pollables.len());
@@ -596,12 +613,12 @@ fn run_control(mut vm: Vm,
                 }
                 t if t >= VM_BASE && t < VM_BASE + (control_sockets.len() as u32) => {
                     let socket = &control_sockets[(t - VM_BASE) as usize];
-                    match VmRequest::recv(&mut scm, socket) {
+                    match VmRequest::recv(&mut scm, socket.as_ref()) {
                         Ok(request) => {
                             let mut running = true;
                             let response =
                                 request.execute(&mut vm, &mut next_dev_pfn, &mut running);
-                            if let Err(e) = response.send(&mut scm, socket) {
+                            if let Err(e) = response.send(&mut scm, socket.as_ref()) {
                                 println!("failed to send VmResponse: {:?}", e);
                             }
                             if !running {