Don't try to use /dev/null on Fuchsia

This commit is contained in:
Tyler Mandry
2019-08-30 11:51:04 -07:00
parent 5da1123c5e
commit 403701f976
3 changed files with 36 additions and 9 deletions

View File

@@ -1,19 +1,27 @@
use crate::os::unix::prelude::*; use crate::os::unix::prelude::*;
use crate::ffi::{OsString, OsStr, CString, CStr}; use crate::ffi::{OsString, OsStr, CString};
use crate::fmt; use crate::fmt;
use crate::io; use crate::io;
use crate::ptr; use crate::ptr;
use crate::sys::fd::FileDesc; use crate::sys::fd::FileDesc;
use crate::sys::fs::{File, OpenOptions}; use crate::sys::fs::File;
use crate::sys::pipe::{self, AnonPipe}; use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::{CommandEnv, DefaultEnvKey}; use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
use crate::collections::BTreeMap; use crate::collections::BTreeMap;
#[cfg(not(target_os = "fuchsia"))]
use {
crate::ffi::CStr,
crate::sys::fs::OpenOptions,
};
use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE}; use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "redox")] { if #[cfg(target_os = "fuchsia")] {
// fuchsia doesn't have /dev/null
} else if #[cfg(target_os = "redox")] {
const DEV_NULL: &'static str = "null:\0"; const DEV_NULL: &'static str = "null:\0";
} else { } else {
const DEV_NULL: &'static str = "/dev/null\0"; const DEV_NULL: &'static str = "/dev/null\0";
@@ -83,6 +91,11 @@ pub enum ChildStdio {
Inherit, Inherit,
Explicit(c_int), Explicit(c_int),
Owned(FileDesc), 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 { pub enum Stdio {
@@ -301,6 +314,7 @@ impl Stdio {
Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours))) Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours)))
} }
#[cfg(not(target_os = "fuchsia"))]
Stdio::Null => { Stdio::Null => {
let mut opts = OpenOptions::new(); let mut opts = OpenOptions::new();
opts.read(readable); opts.read(readable);
@@ -311,6 +325,11 @@ impl Stdio {
let fd = File::open_c(&path, &opts)?; let fd = File::open_c(&path, &opts)?;
Ok((ChildStdio::Owned(fd.into_fd()), None)) Ok((ChildStdio::Owned(fd.into_fd()), None))
} }
#[cfg(target_os = "fuchsia")]
Stdio::Null => {
Ok((ChildStdio::Null, None))
}
} }
} }
} }
@@ -333,6 +352,9 @@ impl ChildStdio {
ChildStdio::Inherit => None, ChildStdio::Inherit => None,
ChildStdio::Explicit(fd) => Some(fd), ChildStdio::Explicit(fd) => Some(fd),
ChildStdio::Owned(ref fd) => Some(fd.raw()), ChildStdio::Owned(ref fd) => Some(fd.raw()),
#[cfg(target_os = "fuchsia")]
ChildStdio::Null => None,
} }
} }
} }

View File

@@ -52,7 +52,7 @@ impl Command {
None => ptr::null(), None => ptr::null(),
}; };
let transfer_or_clone = |opt_fd, target_fd| if let Some(local_fd) = opt_fd { let make_action = |local_io: &ChildStdio, target_fd| if let Some(local_fd) = local_io.fd() {
fdio_spawn_action_t { fdio_spawn_action_t {
action: FDIO_SPAWN_ACTION_TRANSFER_FD, action: FDIO_SPAWN_ACTION_TRANSFER_FD,
local_fd, local_fd,
@@ -60,6 +60,10 @@ impl Command {
..Default::default() ..Default::default()
} }
} else { } else {
if let ChildStdio::Null = local_io {
// acts as no-op
return Default::default();
}
fdio_spawn_action_t { fdio_spawn_action_t {
action: FDIO_SPAWN_ACTION_CLONE_FD, action: FDIO_SPAWN_ACTION_CLONE_FD,
local_fd: target_fd, local_fd: target_fd,
@@ -69,9 +73,9 @@ impl Command {
}; };
// Clone stdin, stdout, and stderr // Clone stdin, stdout, and stderr
let action1 = transfer_or_clone(stdio.stdin.fd(), 0); let action1 = make_action(&stdio.stdin, 0);
let action2 = transfer_or_clone(stdio.stdout.fd(), 1); let action2 = make_action(&stdio.stdout, 1);
let action3 = transfer_or_clone(stdio.stderr.fd(), 2); let action3 = make_action(&stdio.stderr, 2);
let actions = [action1, action2, action3]; let actions = [action1, action2, action3];
// We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
@@ -86,7 +90,8 @@ impl Command {
zx_cvt(fdio_spawn_etc( zx_cvt(fdio_spawn_etc(
0, 0,
FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE, 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(), self.get_argv()[0], self.get_argv().as_ptr(), envp,
actions.len() as size_t, actions.as_ptr(),
&mut process_handle, &mut process_handle,
ptr::null_mut(), ptr::null_mut(),
))?; ))?;

View File

@@ -120,7 +120,7 @@ pub struct fdio_spawn_action_t {
extern { extern {
pub fn fdio_spawn_etc(job: zx_handle_t, flags: u32, path: *const c_char, 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, argv: *const *const c_char, envp: *const *const c_char,
action_count: u64, actions: *const fdio_spawn_action_t, action_count: size_t, actions: *const fdio_spawn_action_t,
process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t; process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t;
} }