2017-05-03 17:45:10 +00:00
|
|
|
// Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
//! Runs hardware devices in child processes.
|
|
|
|
|
2017-05-31 16:47:23 +00:00
|
|
|
use std::io::{Error, ErrorKind, Result};
|
2017-05-03 17:45:10 +00:00
|
|
|
use std::os::unix::net::UnixDatagram;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use byteorder::{NativeEndian, ByteOrder};
|
|
|
|
|
|
|
|
use hw::BusDevice;
|
2017-05-31 16:47:23 +00:00
|
|
|
use sys_util::{clone_process, CloneNamespace};
|
2017-05-03 17:45:10 +00:00
|
|
|
|
|
|
|
const SOCKET_TIMEOUT_MS: u64 = 2000;
|
|
|
|
const MSG_SIZE: usize = 24;
|
|
|
|
const CHILD_SIGNATURE: [u8; MSG_SIZE] = [0x7f; MSG_SIZE];
|
|
|
|
|
|
|
|
enum Command {
|
|
|
|
Read = 0,
|
|
|
|
Write = 1,
|
|
|
|
Shutdown = 2,
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:47:23 +00:00
|
|
|
fn child_proc(sock: UnixDatagram, device: &mut BusDevice) {
|
2017-05-03 17:45:10 +00:00
|
|
|
let mut running = true;
|
|
|
|
|
2017-06-29 18:35:17 +00:00
|
|
|
let res = handle_eintr!(sock.send(&CHILD_SIGNATURE));
|
2017-05-03 17:45:10 +00:00
|
|
|
if let Err(e) = res {
|
|
|
|
println!("error: failed to send child started signal: {}", e);
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while running {
|
|
|
|
let mut buf = [0; MSG_SIZE];
|
2017-06-29 18:35:17 +00:00
|
|
|
match handle_eintr!(sock.recv(&mut buf)) {
|
2017-05-03 17:45:10 +00:00
|
|
|
Ok(c) if c != buf.len() => {
|
|
|
|
println!("error: child device process incorrect recv size: got {}, expected {}",
|
|
|
|
c,
|
|
|
|
buf.len());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("error: child device process failed recv: {}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let cmd = NativeEndian::read_u32(&buf[0..]);
|
|
|
|
let len = NativeEndian::read_u32(&buf[4..]) as usize;
|
|
|
|
let offset = NativeEndian::read_u64(&buf[8..]);
|
|
|
|
|
|
|
|
let res = if cmd == Command::Read as u32 {
|
|
|
|
device.read(offset, &mut buf[16..16 + len]);
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(sock.send(&buf))
|
2017-05-03 17:45:10 +00:00
|
|
|
} else if cmd == Command::Write as u32 {
|
|
|
|
device.write(offset, &buf[16..16 + len]);
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(sock.send(&buf))
|
2017-05-03 17:45:10 +00:00
|
|
|
} else if cmd == Command::Shutdown as u32 {
|
|
|
|
running = false;
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(sock.send(&buf))
|
2017-05-03 17:45:10 +00:00
|
|
|
} else {
|
|
|
|
println!("child device process unknown command: {}", cmd);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
println!("error: child device process failed send: {}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:45:10 +00:00
|
|
|
/// Wraps an inner `hw::BusDevice` that is run inside a child process via fork.
|
|
|
|
///
|
|
|
|
/// Because forks are very unfriendly to destructors and all memory mappings and file descriptors
|
|
|
|
/// are inherited, this should be used as early as possible in the main process.
|
|
|
|
pub struct ProxyDevice {
|
|
|
|
sock: UnixDatagram,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProxyDevice {
|
|
|
|
/// Takes the given device and isolates it into another process via fork before returning.
|
|
|
|
///
|
|
|
|
/// The forked process will automatically be terminated when this is dropped, so be sure to keep
|
|
|
|
/// a reference.
|
2017-05-31 16:47:23 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
/// * `device` - The device to isolate to another process.
|
|
|
|
/// * `post_clone_cb` - Called after forking the child process, passed the child end of the pipe
|
|
|
|
/// that must be kept open.
|
2017-05-12 23:15:53 +00:00
|
|
|
pub fn new<D: BusDevice, F>(mut device: D, post_clone_cb: F) -> Result<ProxyDevice>
|
2017-05-31 16:47:23 +00:00
|
|
|
where F: FnOnce(&UnixDatagram)
|
|
|
|
{
|
2017-05-03 17:45:10 +00:00
|
|
|
let (child_sock, parent_sock) = UnixDatagram::pair()?;
|
|
|
|
|
2017-05-31 16:47:23 +00:00
|
|
|
clone_process(CloneNamespace::NewUserPid, move || {
|
2017-05-12 23:15:53 +00:00
|
|
|
post_clone_cb(&child_sock);
|
2017-05-03 17:45:10 +00:00
|
|
|
child_proc(child_sock, &mut device);
|
2017-05-31 16:47:23 +00:00
|
|
|
})
|
|
|
|
.map_err(|e| Error::new(ErrorKind::Other, format!("{:?}", e)))?;
|
2017-05-03 17:45:10 +00:00
|
|
|
|
|
|
|
let mut buf = [0; MSG_SIZE];
|
|
|
|
parent_sock
|
|
|
|
.set_write_timeout(Some(Duration::from_millis(SOCKET_TIMEOUT_MS)))?;
|
|
|
|
parent_sock
|
|
|
|
.set_read_timeout(Some(Duration::from_millis(SOCKET_TIMEOUT_MS)))?;
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(parent_sock.recv(&mut buf))?;
|
2017-05-03 17:45:10 +00:00
|
|
|
assert_eq!(buf, CHILD_SIGNATURE);
|
|
|
|
Ok(ProxyDevice { sock: parent_sock })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_cmd(&self, cmd: Command, offset: u64, len: u32, data: &[u8]) -> Result<()> {
|
|
|
|
let mut buf = [0; MSG_SIZE];
|
|
|
|
NativeEndian::write_u32(&mut buf[0..], cmd as u32);
|
|
|
|
NativeEndian::write_u32(&mut buf[4..], len);
|
|
|
|
NativeEndian::write_u64(&mut buf[8..], offset);
|
|
|
|
buf[16..16 + data.len()].clone_from_slice(data);
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(self.sock.send(&buf)).map(|_| ())
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recv_resp(&self, data: &mut [u8]) -> Result<()> {
|
|
|
|
let mut buf = [0; MSG_SIZE];
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(self.sock.recv(&mut buf))?;
|
2017-05-03 17:45:10 +00:00
|
|
|
let len = data.len();
|
|
|
|
data.clone_from_slice(&buf[16..16 + len]);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait(&self) -> Result<()> {
|
|
|
|
let mut buf = [0; MSG_SIZE];
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(self.sock.recv(&mut buf)).map(|_| ())
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BusDevice for ProxyDevice {
|
|
|
|
fn read(&mut self, offset: u64, data: &mut [u8]) {
|
|
|
|
let res = self.send_cmd(Command::Read, offset, data.len() as u32, &[])
|
|
|
|
.and_then(|_| self.recv_resp(data));
|
|
|
|
if let Err(e) = res {
|
|
|
|
println!("error: failed read from child device process: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, offset: u64, data: &[u8]) {
|
|
|
|
let res = self.send_cmd(Command::Write, offset, data.len() as u32, data)
|
|
|
|
.and_then(|_| self.wait());
|
|
|
|
if let Err(e) = res {
|
|
|
|
println!("error: failed write to child device process: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ProxyDevice {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let res = self.send_cmd(Command::Shutdown, 0, 0, &[]);
|
|
|
|
if let Err(e) = res {
|
|
|
|
println!("error: failed to shutdown child device process: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|