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-08-24 21:05:48 +00:00
|
|
|
use libc::pid_t;
|
|
|
|
|
2017-10-13 20:18:00 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2017-05-03 17:45:10 +00:00
|
|
|
use std::os::unix::net::UnixDatagram;
|
2017-10-13 20:18:00 +00:00
|
|
|
use std::process;
|
2017-05-03 17:45:10 +00:00
|
|
|
use std::time::Duration;
|
2018-10-03 17:22:32 +00:00
|
|
|
use std::{self, fmt, io};
|
2017-05-03 17:45:10 +00:00
|
|
|
|
2018-10-03 17:22:32 +00:00
|
|
|
use byteorder::{ByteOrder, LittleEndian, NativeEndian};
|
2017-05-03 17:45:10 +00:00
|
|
|
|
2017-10-13 20:18:00 +00:00
|
|
|
use io_jail::{self, Minijail};
|
2018-10-03 17:22:32 +00:00
|
|
|
use BusDevice;
|
2017-10-13 20:18:00 +00:00
|
|
|
|
|
|
|
/// Errors for proxy devices.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
ForkingJail(io_jail::Error),
|
|
|
|
Io(io::Error),
|
|
|
|
}
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
&Error::ForkingJail(_) => write!(f, "Failed to fork jail process"),
|
|
|
|
&Error::Io(ref e) => write!(f, "IO error configuring proxy device {}.", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 17:45:10 +00:00
|
|
|
|
|
|
|
const SOCKET_TIMEOUT_MS: u64 = 2000;
|
|
|
|
const MSG_SIZE: usize = 24;
|
|
|
|
|
|
|
|
enum Command {
|
|
|
|
Read = 0,
|
|
|
|
Write = 1,
|
2018-05-24 00:57:05 +00:00
|
|
|
ReadConfig = 2,
|
|
|
|
WriteConfig = 3,
|
|
|
|
Shutdown = 4,
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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() => {
|
2018-10-03 17:22:32 +00:00
|
|
|
error!(
|
|
|
|
"child device process incorrect recv size: got {}, expected {}",
|
|
|
|
c,
|
|
|
|
buf.len()
|
|
|
|
);
|
2017-05-03 17:45:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2017-09-29 18:17:53 +00:00
|
|
|
error!("child device process failed recv: {}", e);
|
2017-05-03 17:45:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let cmd = NativeEndian::read_u32(&buf[0..]);
|
|
|
|
|
|
|
|
let res = if cmd == Command::Read as u32 {
|
2018-05-24 00:57:05 +00:00
|
|
|
let len = NativeEndian::read_u32(&buf[4..]) as usize;
|
|
|
|
let offset = NativeEndian::read_u64(&buf[8..]);
|
2017-05-03 17:45:10 +00:00
|
|
|
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 {
|
2018-05-24 00:57:05 +00:00
|
|
|
let len = NativeEndian::read_u32(&buf[4..]) as usize;
|
|
|
|
let offset = NativeEndian::read_u64(&buf[8..]);
|
2017-05-03 17:45:10 +00:00
|
|
|
device.write(offset, &buf[16..16 + len]);
|
2017-06-29 18:35:17 +00:00
|
|
|
handle_eintr!(sock.send(&buf))
|
2018-05-24 00:57:05 +00:00
|
|
|
} else if cmd == Command::ReadConfig as u32 {
|
|
|
|
let reg_idx = NativeEndian::read_u32(&buf[4..]) as usize;
|
|
|
|
let val = device.config_register_read(reg_idx);
|
|
|
|
buf[16] = val as u8;
|
|
|
|
buf[17] = (val >> 8) as u8;
|
|
|
|
buf[18] = (val >> 16) as u8;
|
|
|
|
buf[19] = (val >> 24) as u8;
|
|
|
|
handle_eintr!(sock.send(&buf))
|
|
|
|
} else if cmd == Command::WriteConfig as u32 {
|
|
|
|
let reg_idx = NativeEndian::read_u32(&buf[4..]) as usize;
|
|
|
|
let offset = u64::from(NativeEndian::read_u32(&buf[8..]));
|
|
|
|
let len = u64::from(NativeEndian::read_u32(&buf[16..]));
|
|
|
|
device.config_register_write(reg_idx, offset, &buf[20..(20 + len as usize)]);
|
|
|
|
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 {
|
2017-09-29 18:17:53 +00:00
|
|
|
error!("child device process unknown command: {}", cmd);
|
2017-05-03 17:45:10 +00:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
2017-09-29 18:17:53 +00:00
|
|
|
error!("error: child device process failed send: {}", e);
|
2017-05-03 17:45:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:26:46 +00:00
|
|
|
/// Wraps an inner `BusDevice` that is run inside a child process via fork.
|
2017-05-03 17:45:10 +00:00
|
|
|
///
|
|
|
|
/// 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,
|
2017-08-24 21:05:48 +00:00
|
|
|
pid: pid_t,
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-10-13 20:18:00 +00:00
|
|
|
/// * `keep_fds` - File descriptors that will be kept open in the child
|
2018-10-03 17:22:32 +00:00
|
|
|
pub fn new<D: BusDevice>(
|
|
|
|
mut device: D,
|
|
|
|
jail: &Minijail,
|
|
|
|
mut keep_fds: Vec<RawFd>,
|
|
|
|
) -> Result<ProxyDevice> {
|
2017-10-13 20:18:00 +00:00
|
|
|
let (child_sock, parent_sock) = UnixDatagram::pair().map_err(Error::Io)?;
|
|
|
|
|
|
|
|
keep_fds.push(child_sock.as_raw_fd());
|
|
|
|
// Forking here is safe as long as the program is still single threaded.
|
|
|
|
let pid = unsafe {
|
|
|
|
match jail.fork(Some(&keep_fds)).map_err(Error::ForkingJail)? {
|
|
|
|
0 => {
|
|
|
|
child_proc(child_sock, &mut device);
|
|
|
|
// ! Never returns
|
|
|
|
process::exit(0);
|
2018-10-03 17:22:32 +00:00
|
|
|
}
|
2017-10-13 20:18:00 +00:00
|
|
|
p => p,
|
|
|
|
}
|
|
|
|
};
|
2017-05-03 17:45:10 +00:00
|
|
|
|
|
|
|
parent_sock
|
2017-10-13 20:18:00 +00:00
|
|
|
.set_write_timeout(Some(Duration::from_millis(SOCKET_TIMEOUT_MS)))
|
|
|
|
.map_err(Error::Io)?;
|
2017-05-03 17:45:10 +00:00
|
|
|
parent_sock
|
2017-10-13 20:18:00 +00:00
|
|
|
.set_read_timeout(Some(Duration::from_millis(SOCKET_TIMEOUT_MS)))
|
|
|
|
.map_err(Error::Io)?;
|
2017-09-29 18:17:53 +00:00
|
|
|
Ok(ProxyDevice {
|
2018-10-03 17:22:32 +00:00
|
|
|
sock: parent_sock,
|
|
|
|
pid,
|
|
|
|
})
|
2017-08-24 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pid(&self) -> pid_t {
|
|
|
|
self.pid
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-10-03 17:22:32 +00:00
|
|
|
handle_eintr!(self.sock.send(&buf))
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::Io)
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 17:22:32 +00:00
|
|
|
fn send_config_cmd(&self, cmd: Command, reg_idx: u32, offset: u64, data: &[u8]) -> Result<()> {
|
2018-05-24 00:57:05 +00:00
|
|
|
let mut buf = [0; MSG_SIZE];
|
|
|
|
NativeEndian::write_u32(&mut buf[0..], cmd as u32);
|
|
|
|
NativeEndian::write_u32(&mut buf[4..], reg_idx);
|
|
|
|
NativeEndian::write_u64(&mut buf[8..], offset);
|
|
|
|
NativeEndian::write_u32(&mut buf[16..], data.len() as u32);
|
|
|
|
buf[20..20 + data.len()].clone_from_slice(data);
|
|
|
|
handle_eintr!(self.sock.send(&buf))
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::Io)
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:45:10 +00:00
|
|
|
fn recv_resp(&self, data: &mut [u8]) -> Result<()> {
|
|
|
|
let mut buf = [0; MSG_SIZE];
|
2017-10-13 20:18:00 +00:00
|
|
|
handle_eintr!(self.sock.recv(&mut buf)).map_err(Error::Io)?;
|
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];
|
2018-10-03 17:22:32 +00:00
|
|
|
handle_eintr!(self.sock.recv(&mut buf))
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(Error::Io)
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
2018-05-24 00:57:05 +00:00
|
|
|
|
|
|
|
pub fn config_register_write(&mut self, reg_idx: usize, offset: u64, data: &[u8]) {
|
|
|
|
let res = self
|
|
|
|
.send_config_cmd(Command::WriteConfig, reg_idx as u32, offset, data)
|
|
|
|
.and_then(|_| self.wait());
|
|
|
|
if let Err(e) = res {
|
|
|
|
error!("failed write to child device process: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn config_register_read(&self, reg_idx: usize) -> u32 {
|
|
|
|
let mut data = [0u8; 4];
|
|
|
|
let res = self
|
|
|
|
.send_config_cmd(Command::ReadConfig, reg_idx as u32, 0, &[])
|
|
|
|
.and_then(|_| self.recv_resp(&mut data));
|
|
|
|
if let Err(e) = res {
|
|
|
|
error!("failed write to child device process: {}", e);
|
|
|
|
}
|
|
|
|
LittleEndian::read_u32(&data)
|
|
|
|
}
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BusDevice for ProxyDevice {
|
|
|
|
fn read(&mut self, offset: u64, data: &mut [u8]) {
|
2018-10-03 17:22:32 +00:00
|
|
|
let res = self
|
|
|
|
.send_cmd(Command::Read, offset, data.len() as u32, &[])
|
2017-05-03 17:45:10 +00:00
|
|
|
.and_then(|_| self.recv_resp(data));
|
|
|
|
if let Err(e) = res {
|
2017-09-29 18:17:53 +00:00
|
|
|
error!("failed read from child device process: {}", e);
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, offset: u64, data: &[u8]) {
|
2018-10-03 17:22:32 +00:00
|
|
|
let res = self
|
|
|
|
.send_cmd(Command::Write, offset, data.len() as u32, data)
|
2017-05-03 17:45:10 +00:00
|
|
|
.and_then(|_| self.wait());
|
|
|
|
if let Err(e) = res {
|
2017-09-29 18:17:53 +00:00
|
|
|
error!("failed write to child device process: {}", e);
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ProxyDevice {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let res = self.send_cmd(Command::Shutdown, 0, 0, &[]);
|
|
|
|
if let Err(e) = res {
|
2017-09-29 18:17:53 +00:00
|
|
|
error!("failed to shutdown child device process: {}", e);
|
2017-05-03 17:45:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|