1
Fork 0

mv std libs to library/

This commit is contained in:
mark 2020-06-11 21:31:49 -05:00
parent 9be8ffcb02
commit 2c31b45ae8
875 changed files with 1255 additions and 1223 deletions

View file

@ -0,0 +1,13 @@
pub use self::process_common::{Command, ExitCode, Stdio, StdioPipes};
pub use self::process_inner::{ExitStatus, Process};
pub use crate::ffi::OsString as EnvKey;
mod process_common;
#[cfg(not(target_os = "fuchsia"))]
#[path = "process_unix.rs"]
mod process_inner;
#[cfg(target_os = "fuchsia")]
#[path = "process_fuchsia.rs"]
mod process_inner;
#[cfg(target_os = "fuchsia")]
mod zircon;

View file

@ -0,0 +1,469 @@
use crate::os::unix::prelude::*;
use crate::collections::BTreeMap;
use crate::ffi::{CStr, CString, OsStr, OsString};
use crate::fmt;
use crate::io;
use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::File;
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::CommandEnv;
#[cfg(not(target_os = "fuchsia"))]
use crate::sys::fs::OpenOptions;
use libc::{c_char, c_int, gid_t, uid_t, EXIT_FAILURE, EXIT_SUCCESS};
cfg_if::cfg_if! {
if #[cfg(target_os = "fuchsia")] {
// fuchsia doesn't have /dev/null
} else if #[cfg(target_os = "redox")] {
const DEV_NULL: &str = "null:\0";
} else {
const DEV_NULL: &str = "/dev/null\0";
}
}
// Android with api less than 21 define sig* functions inline, so it is not
// available for dynamic link. Implementing sigemptyset and sigaddset allow us
// to support older Android version (independent of libc version).
// The following implementations are based on https://git.io/vSkNf
cfg_if::cfg_if! {
if #[cfg(target_os = "android")] {
pub unsafe fn sigemptyset(set: *mut libc::sigset_t) -> libc::c_int {
set.write_bytes(0u8, 1);
return 0;
}
#[allow(dead_code)]
pub unsafe fn sigaddset(set: *mut libc::sigset_t, signum: libc::c_int) -> libc::c_int {
use crate::{slice, mem};
let raw = slice::from_raw_parts_mut(set as *mut u8, mem::size_of::<libc::sigset_t>());
let bit = (signum - 1) as usize;
raw[bit / 8] |= 1 << (bit % 8);
return 0;
}
} else {
pub use libc::{sigemptyset, sigaddset};
}
}
////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////
pub struct Command {
// Currently we try hard to ensure that the call to `.exec()` doesn't
// actually allocate any memory. While many platforms try to ensure that
// memory allocation works after a fork in a multithreaded process, it's
// been observed to be buggy and somewhat unreliable, so we do our best to
// just not do it at all!
//
// Along those lines, the `argv` and `envp` raw pointers here are exactly
// what's gonna get passed to `execvp`. The `argv` array starts with the
// `program` and ends with a NULL, and the `envp` pointer, if present, is
// also null-terminated.
//
// Right now we don't support removing arguments, so there's no much fancy
// support there, but we support adding and removing environment variables,
// so a side table is used to track where in the `envp` array each key is
// located. Whenever we add a key we update it in place if it's already
// present, and whenever we remove a key we update the locations of all
// other keys.
program: CString,
args: Vec<CString>,
argv: Argv,
env: CommandEnv,
cwd: Option<CString>,
uid: Option<uid_t>,
gid: Option<gid_t>,
saw_nul: bool,
closures: Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>>,
stdin: Option<Stdio>,
stdout: Option<Stdio>,
stderr: Option<Stdio>,
}
// Create a new type for argv, so that we can make it `Send` and `Sync`
struct Argv(Vec<*const c_char>);
// It is safe to make `Argv` `Send` and `Sync`, because it contains
// pointers to memory owned by `Command.args`
unsafe impl Send for Argv {}
unsafe impl Sync for Argv {}
// passed back to std::process with the pipes connected to the child, if any
// were requested
pub struct StdioPipes {
pub stdin: Option<AnonPipe>,
pub stdout: Option<AnonPipe>,
pub stderr: Option<AnonPipe>,
}
// passed to do_exec() with configuration of what the child stdio should look
// like
pub struct ChildPipes {
pub stdin: ChildStdio,
pub stdout: ChildStdio,
pub stderr: ChildStdio,
}
pub enum ChildStdio {
Inherit,
Explicit(c_int),
Owned(FileDesc),
// On Fuchsia, null stdio is the default, so we simply don't specify
// any actions at the time of spawning.
#[cfg(target_os = "fuchsia")]
Null,
}
pub enum Stdio {
Inherit,
Null,
MakePipe,
Fd(FileDesc),
}
impl Command {
pub fn new(program: &OsStr) -> Command {
let mut saw_nul = false;
let program = os2c(program, &mut saw_nul);
Command {
argv: Argv(vec![program.as_ptr(), ptr::null()]),
args: vec![program.clone()],
program,
env: Default::default(),
cwd: None,
uid: None,
gid: None,
saw_nul,
closures: Vec::new(),
stdin: None,
stdout: None,
stderr: None,
}
}
pub fn set_arg_0(&mut self, arg: &OsStr) {
// Set a new arg0
let arg = os2c(arg, &mut self.saw_nul);
debug_assert!(self.argv.0.len() > 1);
self.argv.0[0] = arg.as_ptr();
self.args[0] = arg;
}
pub fn arg(&mut self, arg: &OsStr) {
// Overwrite the trailing NULL pointer in `argv` and then add a new null
// pointer.
let arg = os2c(arg, &mut self.saw_nul);
self.argv.0[self.args.len()] = arg.as_ptr();
self.argv.0.push(ptr::null());
// Also make sure we keep track of the owned value to schedule a
// destructor for this memory.
self.args.push(arg);
}
pub fn cwd(&mut self, dir: &OsStr) {
self.cwd = Some(os2c(dir, &mut self.saw_nul));
}
pub fn uid(&mut self, id: uid_t) {
self.uid = Some(id);
}
pub fn gid(&mut self, id: gid_t) {
self.gid = Some(id);
}
pub fn saw_nul(&self) -> bool {
self.saw_nul
}
pub fn get_argv(&self) -> &Vec<*const c_char> {
&self.argv.0
}
pub fn get_program(&self) -> &CStr {
&*self.program
}
#[allow(dead_code)]
pub fn get_cwd(&self) -> &Option<CString> {
&self.cwd
}
#[allow(dead_code)]
pub fn get_uid(&self) -> Option<uid_t> {
self.uid
}
#[allow(dead_code)]
pub fn get_gid(&self) -> Option<gid_t> {
self.gid
}
pub fn get_closures(&mut self) -> &mut Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>> {
&mut self.closures
}
pub unsafe fn pre_exec(&mut self, f: Box<dyn FnMut() -> io::Result<()> + Send + Sync>) {
self.closures.push(f);
}
pub fn stdin(&mut self, stdin: Stdio) {
self.stdin = Some(stdin);
}
pub fn stdout(&mut self, stdout: Stdio) {
self.stdout = Some(stdout);
}
pub fn stderr(&mut self, stderr: Stdio) {
self.stderr = Some(stderr);
}
pub fn env_mut(&mut self) -> &mut CommandEnv {
&mut self.env
}
pub fn capture_env(&mut self) -> Option<CStringArray> {
let maybe_env = self.env.capture_if_changed();
maybe_env.map(|env| construct_envp(env, &mut self.saw_nul))
}
#[allow(dead_code)]
pub fn env_saw_path(&self) -> bool {
self.env.have_changed_path()
}
pub fn setup_io(
&self,
default: Stdio,
needs_stdin: bool,
) -> io::Result<(StdioPipes, ChildPipes)> {
let null = Stdio::Null;
let default_stdin = if needs_stdin { &default } else { &null };
let stdin = self.stdin.as_ref().unwrap_or(default_stdin);
let stdout = self.stdout.as_ref().unwrap_or(&default);
let stderr = self.stderr.as_ref().unwrap_or(&default);
let (their_stdin, our_stdin) = stdin.to_child_stdio(true)?;
let (their_stdout, our_stdout) = stdout.to_child_stdio(false)?;
let (their_stderr, our_stderr) = stderr.to_child_stdio(false)?;
let ours = StdioPipes { stdin: our_stdin, stdout: our_stdout, stderr: our_stderr };
let theirs = ChildPipes { stdin: their_stdin, stdout: their_stdout, stderr: their_stderr };
Ok((ours, theirs))
}
}
fn os2c(s: &OsStr, saw_nul: &mut bool) -> CString {
CString::new(s.as_bytes()).unwrap_or_else(|_e| {
*saw_nul = true;
CString::new("<string-with-nul>").unwrap()
})
}
// Helper type to manage ownership of the strings within a C-style array.
pub struct CStringArray {
items: Vec<CString>,
ptrs: Vec<*const c_char>,
}
impl CStringArray {
pub fn with_capacity(capacity: usize) -> Self {
let mut result = CStringArray {
items: Vec::with_capacity(capacity),
ptrs: Vec::with_capacity(capacity + 1),
};
result.ptrs.push(ptr::null());
result
}
pub fn push(&mut self, item: CString) {
let l = self.ptrs.len();
self.ptrs[l - 1] = item.as_ptr();
self.ptrs.push(ptr::null());
self.items.push(item);
}
pub fn as_ptr(&self) -> *const *const c_char {
self.ptrs.as_ptr()
}
}
fn construct_envp(env: BTreeMap<OsString, OsString>, saw_nul: &mut bool) -> CStringArray {
let mut result = CStringArray::with_capacity(env.len());
for (mut k, v) in env {
// Reserve additional space for '=' and null terminator
k.reserve_exact(v.len() + 2);
k.push("=");
k.push(&v);
// Add the new entry into the array
if let Ok(item) = CString::new(k.into_vec()) {
result.push(item);
} else {
*saw_nul = true;
}
}
result
}
impl Stdio {
pub fn to_child_stdio(&self, readable: bool) -> io::Result<(ChildStdio, Option<AnonPipe>)> {
match *self {
Stdio::Inherit => Ok((ChildStdio::Inherit, None)),
// Make sure that the source descriptors are not an stdio
// descriptor, otherwise the order which we set the child's
// descriptors may blow away a descriptor which we are hoping to
// save. For example, suppose we want the child's stderr to be the
// parent's stdout, and the child's stdout to be the parent's
// stderr. No matter which we dup first, the second will get
// overwritten prematurely.
Stdio::Fd(ref fd) => {
if fd.raw() >= 0 && fd.raw() <= libc::STDERR_FILENO {
Ok((ChildStdio::Owned(fd.duplicate()?), None))
} else {
Ok((ChildStdio::Explicit(fd.raw()), None))
}
}
Stdio::MakePipe => {
let (reader, writer) = pipe::anon_pipe()?;
let (ours, theirs) = if readable { (writer, reader) } else { (reader, writer) };
Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours)))
}
#[cfg(not(target_os = "fuchsia"))]
Stdio::Null => {
let mut opts = OpenOptions::new();
opts.read(readable);
opts.write(!readable);
let path = unsafe { CStr::from_ptr(DEV_NULL.as_ptr() as *const _) };
let fd = File::open_c(&path, &opts)?;
Ok((ChildStdio::Owned(fd.into_fd()), None))
}
#[cfg(target_os = "fuchsia")]
Stdio::Null => Ok((ChildStdio::Null, None)),
}
}
}
impl From<AnonPipe> for Stdio {
fn from(pipe: AnonPipe) -> Stdio {
Stdio::Fd(pipe.into_fd())
}
}
impl From<File> for Stdio {
fn from(file: File) -> Stdio {
Stdio::Fd(file.into_fd())
}
}
impl ChildStdio {
pub fn fd(&self) -> Option<c_int> {
match *self {
ChildStdio::Inherit => None,
ChildStdio::Explicit(fd) => Some(fd),
ChildStdio::Owned(ref fd) => Some(fd.raw()),
#[cfg(target_os = "fuchsia")]
ChildStdio::Null => None,
}
}
}
impl fmt::Debug for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.program != self.args[0] {
write!(f, "[{:?}] ", self.program)?;
}
write!(f, "{:?}", self.args[0])?;
for arg in &self.args[1..] {
write!(f, " {:?}", arg)?;
}
Ok(())
}
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitCode(u8);
impl ExitCode {
pub const SUCCESS: ExitCode = ExitCode(EXIT_SUCCESS as _);
pub const FAILURE: ExitCode = ExitCode(EXIT_FAILURE as _);
#[inline]
pub fn as_i32(&self) -> i32 {
self.0 as i32
}
}
#[cfg(all(test, not(target_os = "emscripten")))]
mod tests {
use super::*;
use crate::ffi::OsStr;
use crate::mem;
use crate::ptr;
use crate::sys::cvt;
macro_rules! t {
($e:expr) => {
match $e {
Ok(t) => t,
Err(e) => panic!("received error for `{}`: {}", stringify!($e), e),
}
};
}
// See #14232 for more information, but it appears that signal delivery to a
// newly spawned process may just be raced in the macOS, so to prevent this
// test from being flaky we ignore it on macOS.
#[test]
#[cfg_attr(target_os = "macos", ignore)]
// When run under our current QEMU emulation test suite this test fails,
// although the reason isn't very clear as to why. For now this test is
// ignored there.
#[cfg_attr(target_arch = "arm", ignore)]
#[cfg_attr(target_arch = "aarch64", ignore)]
#[cfg_attr(target_arch = "riscv64", ignore)]
fn test_process_mask() {
unsafe {
// Test to make sure that a signal mask does not get inherited.
let mut cmd = Command::new(OsStr::new("cat"));
let mut set = mem::MaybeUninit::<libc::sigset_t>::uninit();
let mut old_set = mem::MaybeUninit::<libc::sigset_t>::uninit();
t!(cvt(sigemptyset(set.as_mut_ptr())));
t!(cvt(sigaddset(set.as_mut_ptr(), libc::SIGINT)));
t!(cvt(libc::pthread_sigmask(libc::SIG_SETMASK, set.as_ptr(), old_set.as_mut_ptr())));
cmd.stdin(Stdio::MakePipe);
cmd.stdout(Stdio::MakePipe);
let (mut cat, mut pipes) = t!(cmd.spawn(Stdio::Null, true));
let stdin_write = pipes.stdin.take().unwrap();
let stdout_read = pipes.stdout.take().unwrap();
t!(cvt(libc::pthread_sigmask(libc::SIG_SETMASK, old_set.as_ptr(), ptr::null_mut())));
t!(cvt(libc::kill(cat.id() as libc::pid_t, libc::SIGINT)));
// We need to wait until SIGINT is definitely delivered. The
// easiest way is to write something to cat, and try to read it
// back: if SIGINT is unmasked, it'll get delivered when cat is
// next scheduled.
let _ = stdin_write.write(b"Hello");
drop(stdin_write);
// Either EOF or failure (EPIPE) is okay.
let mut buf = [0; 5];
if let Ok(ret) = stdout_read.read(&mut buf) {
assert_eq!(ret, 0);
}
t!(cat.wait());
}
}
}

View file

@ -0,0 +1,260 @@
use crate::convert::TryInto;
use crate::fmt;
use crate::io;
use crate::mem;
use crate::ptr;
use crate::sys::process::process_common::*;
use crate::sys::process::zircon::{zx_handle_t, Handle};
use libc::{c_int, size_t};
////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////
impl Command {
pub fn spawn(
&mut self,
default: Stdio,
needs_stdin: bool,
) -> io::Result<(Process, StdioPipes)> {
let envp = self.capture_env();
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)?;
let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
Ok((Process { handle: Handle::new(process_handle) }, ours))
}
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,
}
}
unsafe fn do_exec(
&mut self,
stdio: ChildPipes,
maybe_envp: Option<&CStringArray>,
) -> io::Result<zx_handle_t> {
use crate::sys::process::zircon::*;
let envp = match maybe_envp {
// None means to clone the current environment, which is done in the
// flags below.
None => ptr::null(),
Some(envp) => envp.as_ptr(),
};
let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
if let Some(local_fd) = local_io.fd() {
Ok(fdio_spawn_action_t {
action: FDIO_SPAWN_ACTION_TRANSFER_FD,
local_fd,
target_fd,
..Default::default()
})
} else {
if let ChildStdio::Null = local_io {
// acts as no-op
return Ok(Default::default());
}
let mut handle = ZX_HANDLE_INVALID;
let status = fdio_fd_clone(target_fd, &mut handle);
if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
// This descriptor is closed; skip it rather than generating an
// error.
return Ok(Default::default());
}
zx_cvt(status)?;
let mut cloned_fd = 0;
zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
Ok(fdio_spawn_action_t {
action: FDIO_SPAWN_ACTION_TRANSFER_FD,
local_fd: cloned_fd as i32,
target_fd,
..Default::default()
})
}
};
// Clone stdin, stdout, and stderr
let action1 = make_action(&stdio.stdin, 0)?;
let action2 = make_action(&stdio.stdout, 1)?;
let action3 = make_action(&stdio.stderr, 2)?;
let actions = [action1, action2, action3];
// We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
// always consumes transferred file descriptors.
mem::forget(stdio);
for callback in self.get_closures().iter_mut() {
callback()?;
}
let mut process_handle: zx_handle_t = 0;
zx_cvt(fdio_spawn_etc(
ZX_HANDLE_INVALID,
FDIO_SPAWN_CLONE_JOB
| FDIO_SPAWN_CLONE_LDSVC
| FDIO_SPAWN_CLONE_NAMESPACE
| FDIO_SPAWN_CLONE_ENVIRON, // this is ignored when envp is non-null
self.get_program().as_ptr(),
self.get_argv().as_ptr(),
envp,
actions.len() as size_t,
actions.as_ptr(),
&mut process_handle,
ptr::null_mut(),
))?;
// FIXME: See if we want to do something with that err_msg
Ok(process_handle)
}
}
////////////////////////////////////////////////////////////////////////////////
// Processes
////////////////////////////////////////////////////////////////////////////////
pub struct Process {
handle: Handle,
}
impl Process {
pub fn id(&self) -> u32 {
self.handle.raw() as u32
}
pub fn kill(&mut self) -> io::Result<()> {
use crate::sys::process::zircon::*;
unsafe {
zx_cvt(zx_task_kill(self.handle.raw()))?;
}
Ok(())
}
pub fn wait(&mut self) -> io::Result<ExitStatus> {
use crate::default::Default;
use crate::sys::process::zircon::*;
let mut proc_info: zx_info_process_t = Default::default();
let mut actual: size_t = 0;
let mut avail: size_t = 0;
unsafe {
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,
&mut proc_info as *mut _ as *mut libc::c_void,
mem::size_of::<zx_info_process_t>(),
&mut actual,
&mut avail,
))?;
}
if actual != 1 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Failed to get exit status of process",
));
}
Ok(ExitStatus(proc_info.return_code))
}
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
use crate::default::Default;
use crate::sys::process::zircon::*;
let mut proc_info: zx_info_process_t = Default::default();
let mut actual: size_t = 0;
let mut avail: size_t = 0;
unsafe {
let status =
zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED, 0, ptr::null_mut());
match status {
0 => {} // Success
x if x == ERR_TIMED_OUT => {
return Ok(None);
}
_ => {
panic!("Failed to wait on process handle: {}", status);
}
}
zx_cvt(zx_object_get_info(
self.handle.raw(),
ZX_INFO_PROCESS,
&mut proc_info as *mut _ as *mut libc::c_void,
mem::size_of::<zx_info_process_t>(),
&mut actual,
&mut avail,
))?;
}
if actual != 1 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Failed to get exit status of process",
));
}
Ok(Some(ExitStatus(proc_info.return_code)))
}
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitStatus(i64);
impl ExitStatus {
pub fn success(&self) -> bool {
self.code() == Some(0)
}
pub fn code(&self) -> Option<i32> {
// FIXME: support extracting return code as an i64
self.0.try_into().ok()
}
pub fn signal(&self) -> Option<i32> {
None
}
}
/// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
impl From<c_int> for ExitStatus {
fn from(a: c_int) -> ExitStatus {
ExitStatus(a as i64)
}
}
impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "exit code: {}", self.0)
}
}

View file

@ -0,0 +1,494 @@
use crate::convert::TryInto;
use crate::fmt;
use crate::io::{self, Error, ErrorKind};
use crate::ptr;
use crate::sys;
use crate::sys::cvt;
use crate::sys::process::process_common::*;
use libc::{c_int, gid_t, pid_t, uid_t};
////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////
impl Command {
pub fn spawn(
&mut self,
default: Stdio,
needs_stdin: bool,
) -> io::Result<(Process, StdioPipes)> {
const CLOEXEC_MSG_FOOTER: [u8; 4] = *b"NOEX";
let envp = self.capture_env();
if self.saw_nul() {
return Err(io::Error::new(ErrorKind::InvalidInput, "nul byte found in provided data"));
}
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
if let Some(ret) = self.posix_spawn(&theirs, envp.as_ref())? {
return Ok((ret, ours));
}
let (input, output) = sys::pipe::anon_pipe()?;
// Whatever happens after the fork is almost for sure going to touch or
// look at the environment in one way or another (PATH in `execvp` or
// accessing the `environ` pointer ourselves). Make sure no other thread
// is accessing the environment when we do the fork itself.
//
// Note that as soon as we're done with the fork there's no need to hold
// a lock any more because the parent won't do anything and the child is
// in its own process.
let result = unsafe {
let _env_lock = sys::os::env_lock();
cvt(libc::fork())?
};
let pid = unsafe {
match result {
0 => {
drop(input);
let Err(err) = self.do_exec(theirs, envp.as_ref());
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
let errno = errno.to_be_bytes();
let bytes = [
errno[0],
errno[1],
errno[2],
errno[3],
CLOEXEC_MSG_FOOTER[0],
CLOEXEC_MSG_FOOTER[1],
CLOEXEC_MSG_FOOTER[2],
CLOEXEC_MSG_FOOTER[3],
];
// pipe I/O up to PIPE_BUF bytes should be atomic, and then
// we want to be sure we *don't* run at_exit destructors as
// we're being torn down regardless
assert!(output.write(&bytes).is_ok());
libc::_exit(1)
}
n => n,
}
};
let mut p = Process { pid, status: None };
drop(output);
let mut bytes = [0; 8];
// loop to handle EINTR
loop {
match input.read(&mut bytes) {
Ok(0) => return Ok((p, ours)),
Ok(8) => {
let (errno, footer) = bytes.split_at(4);
assert_eq!(
CLOEXEC_MSG_FOOTER, footer,
"Validation on the CLOEXEC pipe failed: {:?}",
bytes
);
let errno = i32::from_be_bytes(errno.try_into().unwrap());
assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
return Err(Error::from_raw_os_error(errno));
}
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
Err(e) => {
assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
panic!("the CLOEXEC pipe failed: {:?}", e)
}
Ok(..) => {
// pipe I/O up to PIPE_BUF bytes should be atomic
assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
panic!("short read on the CLOEXEC pipe")
}
}
}
}
pub fn exec(&mut self, default: Stdio) -> io::Error {
let envp = self.capture_env();
if self.saw_nul() {
return io::Error::new(ErrorKind::InvalidInput, "nul byte found in provided data");
}
match self.setup_io(default, true) {
Ok((_, theirs)) => {
unsafe {
// Similar to when forking, we want to ensure that access to
// the environment is synchronized, so make sure to grab the
// environment lock before we try to exec.
let _lock = sys::os::env_lock();
let Err(e) = self.do_exec(theirs, envp.as_ref());
e
}
}
Err(e) => e,
}
}
// And at this point we've reached a special time in the life of the
// child. The child must now be considered hamstrung and unable to
// do anything other than syscalls really. Consider the following
// scenario:
//
// 1. Thread A of process 1 grabs the malloc() mutex
// 2. Thread B of process 1 forks(), creating thread C
// 3. Thread C of process 2 then attempts to malloc()
// 4. The memory of process 2 is the same as the memory of
// process 1, so the mutex is locked.
//
// This situation looks a lot like deadlock, right? It turns out
// that this is what pthread_atfork() takes care of, which is
// presumably implemented across platforms. The first thing that
// threads to *before* forking is to do things like grab the malloc
// mutex, and then after the fork they unlock it.
//
// Despite this information, libnative's spawn has been witnessed to
// deadlock on both macOS and FreeBSD. I'm not entirely sure why, but
// all collected backtraces point at malloc/free traffic in the
// child spawned process.
//
// For this reason, the block of code below should contain 0
// invocations of either malloc of free (or their related friends).
//
// As an example of not having malloc/free traffic, we don't close
// this file descriptor by dropping the FileDesc (which contains an
// allocation). Instead we just close it manually. This will never
// have the drop glue anyway because this code never returns (the
// child will either exec() or invoke libc::exit)
unsafe fn do_exec(
&mut self,
stdio: ChildPipes,
maybe_envp: Option<&CStringArray>,
) -> Result<!, io::Error> {
use crate::sys::{self, cvt_r};
if let Some(fd) = stdio.stdin.fd() {
cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
}
if let Some(fd) = stdio.stdout.fd() {
cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO))?;
}
if let Some(fd) = stdio.stderr.fd() {
cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
}
#[cfg(not(target_os = "l4re"))]
{
if let Some(u) = self.get_gid() {
cvt(libc::setgid(u as gid_t))?;
}
if let Some(u) = self.get_uid() {
// When dropping privileges from root, the `setgroups` call
// will remove any extraneous groups. If we don't call this,
// then even though our uid has dropped, we may still have
// groups that enable us to do super-user things. This will
// fail if we aren't root, so don't bother checking the
// return value, this is just done as an optimistic
// privilege dropping function.
//FIXME: Redox kernel does not support setgroups yet
#[cfg(not(target_os = "redox"))]
let _ = libc::setgroups(0, ptr::null());
cvt(libc::setuid(u as uid_t))?;
}
}
if let Some(ref cwd) = *self.get_cwd() {
cvt(libc::chdir(cwd.as_ptr()))?;
}
// emscripten has no signal support.
#[cfg(not(target_os = "emscripten"))]
{
use crate::mem::MaybeUninit;
// Reset signal handling so the child process starts in a
// standardized state. libstd ignores SIGPIPE, and signal-handling
// libraries often set a mask. Child processes inherit ignored
// signals and the signal mask from their parent, but most
// UNIX programs do not reset these things on their own, so we
// need to clean things up now to avoid confusing the program
// we're about to run.
let mut set = MaybeUninit::<libc::sigset_t>::uninit();
cvt(sigemptyset(set.as_mut_ptr()))?;
cvt(libc::pthread_sigmask(libc::SIG_SETMASK, set.as_ptr(), ptr::null_mut()))?;
let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
if ret == libc::SIG_ERR {
return Err(io::Error::last_os_error());
}
}
for callback in self.get_closures().iter_mut() {
callback()?;
}
// Although we're performing an exec here we may also return with an
// error from this function (without actually exec'ing) in which case we
// want to be sure to restore the global environment back to what it
// once was, ensuring that our temporary override, when free'd, doesn't
// corrupt our process's environment.
let mut _reset = None;
if let Some(envp) = maybe_envp {
struct Reset(*const *const libc::c_char);
impl Drop for Reset {
fn drop(&mut self) {
unsafe {
*sys::os::environ() = self.0;
}
}
}
_reset = Some(Reset(*sys::os::environ()));
*sys::os::environ() = envp.as_ptr();
}
libc::execvp(self.get_program().as_ptr(), self.get_argv().as_ptr());
Err(io::Error::last_os_error())
}
#[cfg(not(any(
target_os = "macos",
target_os = "freebsd",
all(target_os = "linux", target_env = "gnu")
)))]
fn posix_spawn(
&mut self,
_: &ChildPipes,
_: Option<&CStringArray>,
) -> io::Result<Option<Process>> {
Ok(None)
}
// Only support platforms for which posix_spawn() can return ENOENT
// directly.
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
all(target_os = "linux", target_env = "gnu")
))]
fn posix_spawn(
&mut self,
stdio: &ChildPipes,
envp: Option<&CStringArray>,
) -> io::Result<Option<Process>> {
use crate::mem::MaybeUninit;
use crate::sys;
if self.get_gid().is_some()
|| self.get_uid().is_some()
|| self.env_saw_path()
|| !self.get_closures().is_empty()
{
return Ok(None);
}
// Only glibc 2.24+ posix_spawn() supports returning ENOENT directly.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
{
if let Some(version) = sys::os::glibc_version() {
if version < (2, 24) {
return Ok(None);
}
} else {
return Ok(None);
}
}
// Solaris and glibc 2.29+ can set a new working directory, and maybe
// others will gain this non-POSIX function too. We'll check for this
// weak symbol as soon as it's needed, so we can return early otherwise
// to do a manual chdir before exec.
weak! {
fn posix_spawn_file_actions_addchdir_np(
*mut libc::posix_spawn_file_actions_t,
*const libc::c_char
) -> libc::c_int
}
let addchdir = match self.get_cwd() {
Some(cwd) => match posix_spawn_file_actions_addchdir_np.get() {
Some(f) => Some((f, cwd)),
None => return Ok(None),
},
None => None,
};
let mut p = Process { pid: 0, status: None };
struct PosixSpawnFileActions(MaybeUninit<libc::posix_spawn_file_actions_t>);
impl Drop for PosixSpawnFileActions {
fn drop(&mut self) {
unsafe {
libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr());
}
}
}
struct PosixSpawnattr(MaybeUninit<libc::posix_spawnattr_t>);
impl Drop for PosixSpawnattr {
fn drop(&mut self) {
unsafe {
libc::posix_spawnattr_destroy(self.0.as_mut_ptr());
}
}
}
unsafe {
let mut file_actions = PosixSpawnFileActions(MaybeUninit::uninit());
let mut attrs = PosixSpawnattr(MaybeUninit::uninit());
libc::posix_spawnattr_init(attrs.0.as_mut_ptr());
libc::posix_spawn_file_actions_init(file_actions.0.as_mut_ptr());
if let Some(fd) = stdio.stdin.fd() {
cvt(libc::posix_spawn_file_actions_adddup2(
file_actions.0.as_mut_ptr(),
fd,
libc::STDIN_FILENO,
))?;
}
if let Some(fd) = stdio.stdout.fd() {
cvt(libc::posix_spawn_file_actions_adddup2(
file_actions.0.as_mut_ptr(),
fd,
libc::STDOUT_FILENO,
))?;
}
if let Some(fd) = stdio.stderr.fd() {
cvt(libc::posix_spawn_file_actions_adddup2(
file_actions.0.as_mut_ptr(),
fd,
libc::STDERR_FILENO,
))?;
}
if let Some((f, cwd)) = addchdir {
cvt(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
}
let mut set = MaybeUninit::<libc::sigset_t>::uninit();
cvt(sigemptyset(set.as_mut_ptr()))?;
cvt(libc::posix_spawnattr_setsigmask(attrs.0.as_mut_ptr(), set.as_ptr()))?;
cvt(sigaddset(set.as_mut_ptr(), libc::SIGPIPE))?;
cvt(libc::posix_spawnattr_setsigdefault(attrs.0.as_mut_ptr(), set.as_ptr()))?;
let flags = libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK;
cvt(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
// Make sure we synchronize access to the global `environ` resource
let _env_lock = sys::os::env_lock();
let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
let ret = libc::posix_spawnp(
&mut p.pid,
self.get_program().as_ptr(),
file_actions.0.as_ptr(),
attrs.0.as_ptr(),
self.get_argv().as_ptr() as *const _,
envp as *const _,
);
if ret == 0 { Ok(Some(p)) } else { Err(io::Error::from_raw_os_error(ret)) }
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Processes
////////////////////////////////////////////////////////////////////////////////
/// The unique ID of the process (this should never be negative).
pub struct Process {
pid: pid_t,
status: Option<ExitStatus>,
}
impl Process {
pub fn id(&self) -> u32 {
self.pid as u32
}
pub fn kill(&mut self) -> io::Result<()> {
// If we've already waited on this process then the pid can be recycled
// and used for another process, and we probably shouldn't be killing
// random processes, so just return an error.
if self.status.is_some() {
Err(Error::new(
ErrorKind::InvalidInput,
"invalid argument: can't kill an exited process",
))
} else {
cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
}
}
pub fn wait(&mut self) -> io::Result<ExitStatus> {
use crate::sys::cvt_r;
if let Some(status) = self.status {
return Ok(status);
}
let mut status = 0 as c_int;
cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
self.status = Some(ExitStatus::new(status));
Ok(ExitStatus::new(status))
}
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
if let Some(status) = self.status {
return Ok(Some(status));
}
let mut status = 0 as c_int;
let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
if pid == 0 {
Ok(None)
} else {
self.status = Some(ExitStatus::new(status));
Ok(Some(ExitStatus::new(status)))
}
}
}
/// Unix exit statuses
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct ExitStatus(c_int);
impl ExitStatus {
pub fn new(status: c_int) -> ExitStatus {
ExitStatus(status)
}
fn exited(&self) -> bool {
unsafe { libc::WIFEXITED(self.0) }
}
pub fn success(&self) -> bool {
self.code() == Some(0)
}
pub fn code(&self) -> Option<i32> {
if self.exited() { Some(unsafe { libc::WEXITSTATUS(self.0) }) } else { None }
}
pub fn signal(&self) -> Option<i32> {
if !self.exited() { Some(unsafe { libc::WTERMSIG(self.0) }) } else { None }
}
}
/// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
impl From<c_int> for ExitStatus {
fn from(a: c_int) -> ExitStatus {
ExitStatus(a)
}
}
impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.code() {
write!(f, "exit code: {}", code)
} else {
let signal = self.signal().unwrap();
write!(f, "signal: {}", signal)
}
}
}

View file

@ -0,0 +1,307 @@
#![allow(non_camel_case_types, unused)]
use crate::convert::TryInto;
use crate::i64;
use crate::io;
use crate::mem::MaybeUninit;
use crate::os::raw::c_char;
use libc::{c_int, c_void, size_t};
pub type zx_handle_t = u32;
pub type zx_vaddr_t = usize;
pub type zx_rights_t = u32;
pub type zx_status_t = i32;
pub const ZX_HANDLE_INVALID: zx_handle_t = 0;
pub type zx_time_t = i64;
pub const ZX_TIME_INFINITE: zx_time_t = i64::MAX;
pub type zx_signals_t = u32;
pub const ZX_OBJECT_SIGNAL_3: zx_signals_t = 1 << 3;
pub const ZX_TASK_TERMINATED: zx_signals_t = ZX_OBJECT_SIGNAL_3;
pub const ZX_RIGHT_SAME_RIGHTS: zx_rights_t = 1 << 31;
pub type zx_object_info_topic_t = u32;
pub const ZX_INFO_PROCESS: zx_object_info_topic_t = 3;
pub fn zx_cvt<T>(t: T) -> io::Result<T>
where
T: TryInto<zx_status_t> + Copy,
{
if let Ok(status) = TryInto::try_into(t) {
if status < 0 { Err(io::Error::from_raw_os_error(status)) } else { Ok(t) }
} else {
Err(io::Error::last_os_error())
}
}
// Safe wrapper around zx_handle_t
pub struct Handle {
raw: zx_handle_t,
}
impl Handle {
pub fn new(raw: zx_handle_t) -> Handle {
Handle { raw }
}
pub fn raw(&self) -> zx_handle_t {
self.raw
}
}
impl Drop for Handle {
fn drop(&mut self) {
unsafe {
zx_cvt(zx_handle_close(self.raw)).expect("Failed to close zx_handle_t");
}
}
}
// Returned for topic ZX_INFO_PROCESS
#[derive(Default)]
#[repr(C)]
pub struct zx_info_process_t {
pub return_code: i64,
pub started: bool,
pub exited: bool,
pub debugger_attached: bool,
}
extern "C" {
pub fn zx_job_default() -> zx_handle_t;
pub fn zx_task_kill(handle: zx_handle_t) -> zx_status_t;
pub fn zx_handle_close(handle: zx_handle_t) -> zx_status_t;
pub fn zx_handle_duplicate(
handle: zx_handle_t,
rights: zx_rights_t,
out: *const zx_handle_t,
) -> zx_handle_t;
pub fn zx_object_wait_one(
handle: zx_handle_t,
signals: zx_signals_t,
timeout: zx_time_t,
pending: *mut zx_signals_t,
) -> zx_status_t;
pub fn zx_object_get_info(
handle: zx_handle_t,
topic: u32,
buffer: *mut c_void,
buffer_size: size_t,
actual_size: *mut size_t,
avail: *mut size_t,
) -> zx_status_t;
}
#[derive(Default)]
#[repr(C)]
pub struct fdio_spawn_action_t {
pub action: u32,
pub reserved0: u32,
pub local_fd: i32,
pub target_fd: i32,
pub reserved1: u64,
}
extern "C" {
pub fn fdio_spawn_etc(
job: zx_handle_t,
flags: u32,
path: *const c_char,
argv: *const *const c_char,
envp: *const *const c_char,
action_count: size_t,
actions: *const fdio_spawn_action_t,
process: *mut zx_handle_t,
err_msg: *mut c_char,
) -> zx_status_t;
pub fn fdio_fd_clone(fd: c_int, out_handle: *mut zx_handle_t) -> zx_status_t;
pub fn fdio_fd_create(handle: zx_handle_t, fd: *mut c_int) -> zx_status_t;
}
// fdio_spawn_etc flags
pub const FDIO_SPAWN_CLONE_JOB: u32 = 0x0001;
pub const FDIO_SPAWN_CLONE_LDSVC: u32 = 0x0002;
pub const FDIO_SPAWN_CLONE_NAMESPACE: u32 = 0x0004;
pub const FDIO_SPAWN_CLONE_STDIO: u32 = 0x0008;
pub const FDIO_SPAWN_CLONE_ENVIRON: u32 = 0x0010;
pub const FDIO_SPAWN_CLONE_ALL: u32 = 0xFFFF;
// fdio_spawn_etc actions
pub const FDIO_SPAWN_ACTION_CLONE_FD: u32 = 0x0001;
pub const FDIO_SPAWN_ACTION_TRANSFER_FD: u32 = 0x0002;
// Errors
#[allow(unused)]
pub const ERR_INTERNAL: zx_status_t = -1;
// ERR_NOT_SUPPORTED: The operation is not implemented, supported,
// or enabled.
#[allow(unused)]
pub const ERR_NOT_SUPPORTED: zx_status_t = -2;
// ERR_NO_RESOURCES: The system was not able to allocate some resource
// needed for the operation.
#[allow(unused)]
pub const ERR_NO_RESOURCES: zx_status_t = -3;
// ERR_NO_MEMORY: The system was not able to allocate memory needed
// for the operation.
#[allow(unused)]
pub const ERR_NO_MEMORY: zx_status_t = -4;
// ERR_CALL_FAILED: The second phase of zx_channel_call(; did not complete
// successfully.
#[allow(unused)]
pub const ERR_CALL_FAILED: zx_status_t = -5;
// ERR_INTERRUPTED_RETRY: The system call was interrupted, but should be
// retried. This should not be seen outside of the VDSO.
#[allow(unused)]
pub const ERR_INTERRUPTED_RETRY: zx_status_t = -6;
// ======= Parameter errors =======
// ERR_INVALID_ARGS: an argument is invalid, ex. null pointer
#[allow(unused)]
pub const ERR_INVALID_ARGS: zx_status_t = -10;
// ERR_BAD_HANDLE: A specified handle value does not refer to a handle.
#[allow(unused)]
pub const ERR_BAD_HANDLE: zx_status_t = -11;
// ERR_WRONG_TYPE: The subject of the operation is the wrong type to
// perform the operation.
// Example: Attempting a message_read on a thread handle.
#[allow(unused)]
pub const ERR_WRONG_TYPE: zx_status_t = -12;
// ERR_BAD_SYSCALL: The specified syscall number is invalid.
#[allow(unused)]
pub const ERR_BAD_SYSCALL: zx_status_t = -13;
// ERR_OUT_OF_RANGE: An argument is outside the valid range for this
// operation.
#[allow(unused)]
pub const ERR_OUT_OF_RANGE: zx_status_t = -14;
// ERR_BUFFER_TOO_SMALL: A caller provided buffer is too small for
// this operation.
#[allow(unused)]
pub const ERR_BUFFER_TOO_SMALL: zx_status_t = -15;
// ======= Precondition or state errors =======
// ERR_BAD_STATE: operation failed because the current state of the
// object does not allow it, or a precondition of the operation is
// not satisfied
#[allow(unused)]
pub const ERR_BAD_STATE: zx_status_t = -20;
// ERR_TIMED_OUT: The time limit for the operation elapsed before
// the operation completed.
#[allow(unused)]
pub const ERR_TIMED_OUT: zx_status_t = -21;
// ERR_SHOULD_WAIT: The operation cannot be performed currently but
// potentially could succeed if the caller waits for a prerequisite
// to be satisfied, for example waiting for a handle to be readable
// or writable.
// Example: Attempting to read from a message pipe that has no
// messages waiting but has an open remote will return ERR_SHOULD_WAIT.
// Attempting to read from a message pipe that has no messages waiting
// and has a closed remote end will return ERR_REMOTE_CLOSED.
#[allow(unused)]
pub const ERR_SHOULD_WAIT: zx_status_t = -22;
// ERR_CANCELED: The in-progress operation (e.g., a wait) has been
// // canceled.
#[allow(unused)]
pub const ERR_CANCELED: zx_status_t = -23;
// ERR_PEER_CLOSED: The operation failed because the remote end
// of the subject of the operation was closed.
#[allow(unused)]
pub const ERR_PEER_CLOSED: zx_status_t = -24;
// ERR_NOT_FOUND: The requested entity is not found.
#[allow(unused)]
pub const ERR_NOT_FOUND: zx_status_t = -25;
// ERR_ALREADY_EXISTS: An object with the specified identifier
// already exists.
// Example: Attempting to create a file when a file already exists
// with that name.
#[allow(unused)]
pub const ERR_ALREADY_EXISTS: zx_status_t = -26;
// ERR_ALREADY_BOUND: The operation failed because the named entity
// is already owned or controlled by another entity. The operation
// could succeed later if the current owner releases the entity.
#[allow(unused)]
pub const ERR_ALREADY_BOUND: zx_status_t = -27;
// ERR_UNAVAILABLE: The subject of the operation is currently unable
// to perform the operation.
// Note: This is used when there's no direct way for the caller to
// observe when the subject will be able to perform the operation
// and should thus retry.
#[allow(unused)]
pub const ERR_UNAVAILABLE: zx_status_t = -28;
// ======= Permission check errors =======
// ERR_ACCESS_DENIED: The caller did not have permission to perform
// the specified operation.
#[allow(unused)]
pub const ERR_ACCESS_DENIED: zx_status_t = -30;
// ======= Input-output errors =======
// ERR_IO: Otherwise unspecified error occurred during I/O.
#[allow(unused)]
pub const ERR_IO: zx_status_t = -40;
// ERR_REFUSED: The entity the I/O operation is being performed on
// rejected the operation.
// Example: an I2C device NAK'ing a transaction or a disk controller
// rejecting an invalid command.
#[allow(unused)]
pub const ERR_IO_REFUSED: zx_status_t = -41;
// ERR_IO_DATA_INTEGRITY: The data in the operation failed an integrity
// check and is possibly corrupted.
// Example: CRC or Parity error.
#[allow(unused)]
pub const ERR_IO_DATA_INTEGRITY: zx_status_t = -42;
// ERR_IO_DATA_LOSS: The data in the operation is currently unavailable
// and may be permanently lost.
// Example: A disk block is irrecoverably damaged.
#[allow(unused)]
pub const ERR_IO_DATA_LOSS: zx_status_t = -43;
// Filesystem specific errors
#[allow(unused)]
pub const ERR_BAD_PATH: zx_status_t = -50;
#[allow(unused)]
pub const ERR_NOT_DIR: zx_status_t = -51;
#[allow(unused)]
pub const ERR_NOT_FILE: zx_status_t = -52;
// ERR_FILE_BIG: A file exceeds a filesystem-specific size limit.
#[allow(unused)]
pub const ERR_FILE_BIG: zx_status_t = -53;
// ERR_NO_SPACE: Filesystem or device space is exhausted.
#[allow(unused)]
pub const ERR_NO_SPACE: zx_status_t = -54;