Skip to content

Commit 403701f

Browse files
committed
Don't try to use /dev/null on Fuchsia
1 parent 5da1123 commit 403701f

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/libstd/sys/unix/process/process_common.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
use crate::os::unix::prelude::*;
22

3-
use crate::ffi::{OsString, OsStr, CString, CStr};
3+
use crate::ffi::{OsString, OsStr, CString};
44
use crate::fmt;
55
use crate::io;
66
use crate::ptr;
77
use crate::sys::fd::FileDesc;
8-
use crate::sys::fs::{File, OpenOptions};
8+
use crate::sys::fs::File;
99
use crate::sys::pipe::{self, AnonPipe};
1010
use crate::sys_common::process::{CommandEnv, DefaultEnvKey};
1111
use crate::collections::BTreeMap;
1212

13+
#[cfg(not(target_os = "fuchsia"))]
14+
use {
15+
crate::ffi::CStr,
16+
crate::sys::fs::OpenOptions,
17+
};
18+
1319
use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};
1420

1521
cfg_if::cfg_if! {
16-
if #[cfg(target_os = "redox")] {
22+
if #[cfg(target_os = "fuchsia")] {
23+
// fuchsia doesn't have /dev/null
24+
} else if #[cfg(target_os = "redox")] {
1725
const DEV_NULL: &'static str = "null:\0";
1826
} else {
1927
const DEV_NULL: &'static str = "/dev/null\0";
@@ -83,6 +91,11 @@ pub enum ChildStdio {
8391
Inherit,
8492
Explicit(c_int),
8593
Owned(FileDesc),
94+
95+
// On Fuchsia, null stdio is the default, so we simply don't specify
96+
// any actions at the time of spawning.
97+
#[cfg(target_os = "fuchsia")]
98+
Null,
8699
}
87100

88101
pub enum Stdio {
@@ -301,6 +314,7 @@ impl Stdio {
301314
Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours)))
302315
}
303316

317+
#[cfg(not(target_os = "fuchsia"))]
304318
Stdio::Null => {
305319
let mut opts = OpenOptions::new();
306320
opts.read(readable);
@@ -311,6 +325,11 @@ impl Stdio {
311325
let fd = File::open_c(&path, &opts)?;
312326
Ok((ChildStdio::Owned(fd.into_fd()), None))
313327
}
328+
329+
#[cfg(target_os = "fuchsia")]
330+
Stdio::Null => {
331+
Ok((ChildStdio::Null, None))
332+
}
314333
}
315334
}
316335
}
@@ -333,6 +352,9 @@ impl ChildStdio {
333352
ChildStdio::Inherit => None,
334353
ChildStdio::Explicit(fd) => Some(fd),
335354
ChildStdio::Owned(ref fd) => Some(fd.raw()),
355+
356+
#[cfg(target_os = "fuchsia")]
357+
ChildStdio::Null => None,
336358
}
337359
}
338360
}

src/libstd/sys/unix/process/process_fuchsia.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ impl Command {
5252
None => ptr::null(),
5353
};
5454

55-
let transfer_or_clone = |opt_fd, target_fd| if let Some(local_fd) = opt_fd {
55+
let make_action = |local_io: &ChildStdio, target_fd| if let Some(local_fd) = local_io.fd() {
5656
fdio_spawn_action_t {
5757
action: FDIO_SPAWN_ACTION_TRANSFER_FD,
5858
local_fd,
5959
target_fd,
6060
..Default::default()
6161
}
6262
} else {
63+
if let ChildStdio::Null = local_io {
64+
// acts as no-op
65+
return Default::default();
66+
}
6367
fdio_spawn_action_t {
6468
action: FDIO_SPAWN_ACTION_CLONE_FD,
6569
local_fd: target_fd,
@@ -69,9 +73,9 @@ impl Command {
6973
};
7074

7175
// Clone stdin, stdout, and stderr
72-
let action1 = transfer_or_clone(stdio.stdin.fd(), 0);
73-
let action2 = transfer_or_clone(stdio.stdout.fd(), 1);
74-
let action3 = transfer_or_clone(stdio.stderr.fd(), 2);
76+
let action1 = make_action(&stdio.stdin, 0);
77+
let action2 = make_action(&stdio.stdout, 1);
78+
let action3 = make_action(&stdio.stderr, 2);
7579
let actions = [action1, action2, action3];
7680

7781
// We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
@@ -86,7 +90,8 @@ impl Command {
8690
zx_cvt(fdio_spawn_etc(
8791
0,
8892
FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE,
89-
self.get_argv()[0], self.get_argv().as_ptr(), envp, 3, actions.as_ptr(),
93+
self.get_argv()[0], self.get_argv().as_ptr(), envp,
94+
actions.len() as size_t, actions.as_ptr(),
9095
&mut process_handle,
9196
ptr::null_mut(),
9297
))?;

src/libstd/sys/unix/process/zircon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub struct fdio_spawn_action_t {
120120
extern {
121121
pub fn fdio_spawn_etc(job: zx_handle_t, flags: u32, path: *const c_char,
122122
argv: *const *const c_char, envp: *const *const c_char,
123-
action_count: u64, actions: *const fdio_spawn_action_t,
123+
action_count: size_t, actions: *const fdio_spawn_action_t,
124124
process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t;
125125
}
126126

0 commit comments

Comments
 (0)