2016-11-23 13:58:13 -08:00
|
|
|
use io;
|
2017-10-04 15:29:01 -07:00
|
|
|
use libc::{self, size_t};
|
2016-11-23 13:58:13 -08:00
|
|
|
use mem;
|
|
|
|
|
use ptr;
|
|
|
|
|
|
2017-09-15 12:38:08 -07:00
|
|
|
use sys::process::zircon::{Handle, zx_handle_t};
|
2016-11-23 13:58:13 -08:00
|
|
|
use sys::process::process_common::*;
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Command
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
|
pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
|
|
|
|
|
-> io::Result<(Process, StdioPipes)> {
|
2017-12-17 15:21:47 +00:00
|
|
|
let envp = self.capture_env();
|
|
|
|
|
|
2016-11-23 13:58:13 -08:00
|
|
|
if self.saw_nul() {
|
|
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidInput,
|
|
|
|
|
"nul byte found in provided data"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
|
|
|
|
|
|
2017-12-17 15:21:47 +00:00
|
|
|
let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
|
2016-11-23 13:58:13 -08:00
|
|
|
|
2017-02-27 20:26:55 -08:00
|
|
|
Ok((Process { handle: Handle::new(process_handle) }, ours))
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn exec(&mut self, default: Stdio) -> io::Error {
|
|
|
|
|
if self.saw_nul() {
|
|
|
|
|
return io::Error::new(io::ErrorKind::InvalidInput,
|
|
|
|
|
"nul byte found in provided data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match self.setup_io(default, true) {
|
|
|
|
|
Ok((_, _)) => {
|
|
|
|
|
// FIXME: This is tough because we don't support the exec syscalls
|
|
|
|
|
unimplemented!();
|
|
|
|
|
},
|
|
|
|
|
Err(e) => e,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-17 15:21:47 +00:00
|
|
|
unsafe fn do_exec(&mut self, stdio: ChildPipes, maybe_envp: Option<&CStringArray>)
|
2017-09-15 12:38:08 -07:00
|
|
|
-> io::Result<zx_handle_t> {
|
|
|
|
|
use sys::process::zircon::*;
|
2016-11-23 13:58:13 -08:00
|
|
|
|
2017-12-17 15:21:47 +00:00
|
|
|
let envp = match maybe_envp {
|
|
|
|
|
Some(envp) => envp.as_ptr(),
|
2016-11-23 13:58:13 -08:00
|
|
|
None => ptr::null(),
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-01 12:58:30 -07:00
|
|
|
let transfer_or_clone = |opt_fd, target_fd| if let Some(local_fd) = opt_fd {
|
|
|
|
|
fdio_spawn_action_t {
|
|
|
|
|
action: FDIO_SPAWN_ACTION_TRANSFER_FD,
|
|
|
|
|
local_fd,
|
|
|
|
|
target_fd,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fdio_spawn_action_t {
|
|
|
|
|
action: FDIO_SPAWN_ACTION_CLONE_FD,
|
|
|
|
|
local_fd: target_fd,
|
|
|
|
|
target_fd,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-11-23 13:58:13 -08:00
|
|
|
|
|
|
|
|
// Clone stdin, stdout, and stderr
|
2018-06-01 12:58:30 -07:00
|
|
|
let action1 = transfer_or_clone(stdio.stdin.fd(), 0);
|
|
|
|
|
let action2 = transfer_or_clone(stdio.stdout.fd(), 1);
|
|
|
|
|
let action3 = transfer_or_clone(stdio.stderr.fd(), 2);
|
|
|
|
|
let actions = [action1, action2, action3];
|
2016-11-23 13:58:13 -08:00
|
|
|
|
2018-06-01 12:58:30 -07:00
|
|
|
// We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
|
|
|
|
|
// always consumes transferred file descriptors.
|
2016-11-23 13:58:13 -08:00
|
|
|
mem::forget(stdio);
|
|
|
|
|
|
|
|
|
|
for callback in self.get_closures().iter_mut() {
|
|
|
|
|
callback()?;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 12:38:08 -07:00
|
|
|
let mut process_handle: zx_handle_t = 0;
|
2018-06-01 12:58:30 -07:00
|
|
|
zx_cvt(fdio_spawn_etc(
|
|
|
|
|
0,
|
|
|
|
|
FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE,
|
|
|
|
|
self.get_argv()[0], self.get_argv().as_ptr(), envp, 3, actions.as_ptr(),
|
|
|
|
|
&mut process_handle,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
))?;
|
2017-02-27 20:26:55 -08:00
|
|
|
// FIXME: See if we want to do something with that err_msg
|
|
|
|
|
|
|
|
|
|
Ok(process_handle)
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Processes
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
pub struct Process {
|
2016-11-30 14:20:44 -08:00
|
|
|
handle: Handle,
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Process {
|
|
|
|
|
pub fn id(&self) -> u32 {
|
2016-11-30 14:20:44 -08:00
|
|
|
self.handle.raw() as u32
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn kill(&mut self) -> io::Result<()> {
|
2017-09-15 12:38:08 -07:00
|
|
|
use sys::process::zircon::*;
|
2016-11-23 13:58:13 -08:00
|
|
|
|
2017-09-15 12:38:08 -07:00
|
|
|
unsafe { zx_cvt(zx_task_kill(self.handle.raw()))?; }
|
2016-11-30 14:20:44 -08:00
|
|
|
|
|
|
|
|
Ok(())
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn wait(&mut self) -> io::Result<ExitStatus> {
|
|
|
|
|
use default::Default;
|
2017-09-15 12:38:08 -07:00
|
|
|
use sys::process::zircon::*;
|
2016-11-23 13:58:13 -08:00
|
|
|
|
2017-09-15 12:38:08 -07:00
|
|
|
let mut proc_info: zx_info_process_t = Default::default();
|
2017-10-04 15:29:01 -07:00
|
|
|
let mut actual: size_t = 0;
|
|
|
|
|
let mut avail: size_t = 0;
|
2016-11-23 13:58:13 -08:00
|
|
|
|
|
|
|
|
unsafe {
|
2017-09-15 14:11:04 -07:00
|
|
|
zx_cvt(zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
|
|
|
|
|
ZX_TIME_INFINITE, ptr::null_mut()))?;
|
|
|
|
|
zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
|
2016-11-23 13:58:13 -08:00
|
|
|
&mut proc_info as *mut _ as *mut libc::c_void,
|
2017-09-15 12:38:08 -07:00
|
|
|
mem::size_of::<zx_info_process_t>(), &mut actual,
|
2016-11-23 13:58:13 -08:00
|
|
|
&mut avail))?;
|
|
|
|
|
}
|
|
|
|
|
if actual != 1 {
|
2016-11-30 14:20:44 -08:00
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidData,
|
|
|
|
|
"Failed to get exit status of process"));
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|
|
|
|
|
Ok(ExitStatus::new(proc_info.rec.return_code))
|
|
|
|
|
}
|
2017-01-24 13:01:47 -08:00
|
|
|
|
2017-02-03 17:39:41 -05:00
|
|
|
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
|
2017-01-24 13:01:47 -08:00
|
|
|
use default::Default;
|
2017-09-15 12:38:08 -07:00
|
|
|
use sys::process::zircon::*;
|
2017-01-24 13:01:47 -08:00
|
|
|
|
2017-09-15 12:38:08 -07:00
|
|
|
let mut proc_info: zx_info_process_t = Default::default();
|
2017-10-04 15:29:01 -07:00
|
|
|
let mut actual: size_t = 0;
|
|
|
|
|
let mut avail: size_t = 0;
|
2017-01-24 13:01:47 -08:00
|
|
|
|
|
|
|
|
unsafe {
|
2017-09-15 14:11:04 -07:00
|
|
|
let status = zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
|
2017-01-24 13:01:47 -08:00
|
|
|
0, ptr::null_mut());
|
|
|
|
|
match status {
|
|
|
|
|
0 => { }, // Success
|
|
|
|
|
x if x == ERR_TIMED_OUT => {
|
2017-02-03 17:39:41 -05:00
|
|
|
return Ok(None);
|
2017-01-24 13:01:47 -08:00
|
|
|
},
|
|
|
|
|
_ => { panic!("Failed to wait on process handle: {}", status); },
|
|
|
|
|
}
|
2017-09-15 14:11:04 -07:00
|
|
|
zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
|
2017-01-24 13:01:47 -08:00
|
|
|
&mut proc_info as *mut _ as *mut libc::c_void,
|
2017-09-15 12:38:08 -07:00
|
|
|
mem::size_of::<zx_info_process_t>(), &mut actual,
|
2017-01-24 13:01:47 -08:00
|
|
|
&mut avail))?;
|
|
|
|
|
}
|
|
|
|
|
if actual != 1 {
|
|
|
|
|
return Err(io::Error::new(io::ErrorKind::InvalidData,
|
|
|
|
|
"Failed to get exit status of process"));
|
|
|
|
|
}
|
2017-02-03 17:39:41 -05:00
|
|
|
Ok(Some(ExitStatus::new(proc_info.rec.return_code)))
|
2017-01-24 13:01:47 -08:00
|
|
|
}
|
2016-11-23 13:58:13 -08:00
|
|
|
}
|