Skip to content

Commit 5654936

Browse files
authored
Rollup merge of rust-lang#41353 - redox-os:master, r=alexcrichton
Improve Process::spawn with piped stdio on Redox - Adds `dup2`, and uses it for stdio piping - Removes `O_CLOEXEC` from piped stdio, as `dup` on Redox does not disable O_CLOEXEC
2 parents c398efc + 1bc9e5d commit 5654936

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/libstd/sys/redox/process.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,22 @@ impl Command {
270270
}
271271

272272
if let Some(fd) = stdio.stderr.fd() {
273-
let _ = syscall::close(2);
274-
t!(cvt(syscall::dup(fd, &[])));
275-
let _ = syscall::close(fd);
273+
t!(cvt(syscall::dup2(fd, 2, &[])));
274+
let mut flags = t!(cvt(syscall::fcntl(2, syscall::F_GETFL, 0)));
275+
flags &= ! syscall::O_CLOEXEC;
276+
t!(cvt(syscall::fcntl(2, syscall::F_SETFL, flags)));
276277
}
277278
if let Some(fd) = stdio.stdout.fd() {
278-
let _ = syscall::close(1);
279-
t!(cvt(syscall::dup(fd, &[])));
280-
let _ = syscall::close(fd);
279+
t!(cvt(syscall::dup2(fd, 1, &[])));
280+
let mut flags = t!(cvt(syscall::fcntl(1, syscall::F_GETFL, 0)));
281+
flags &= ! syscall::O_CLOEXEC;
282+
t!(cvt(syscall::fcntl(1, syscall::F_SETFL, flags)));
281283
}
282284
if let Some(fd) = stdio.stdin.fd() {
283-
let _ = syscall::close(0);
284-
t!(cvt(syscall::dup(fd, &[])));
285-
let _ = syscall::close(fd);
285+
t!(cvt(syscall::dup2(fd, 0, &[])));
286+
let mut flags = t!(cvt(syscall::fcntl(0, syscall::F_GETFL, 0)));
287+
flags &= ! syscall::O_CLOEXEC;
288+
t!(cvt(syscall::fcntl(0, syscall::F_SETFL, flags)));
286289
}
287290

288291
if let Some(g) = self.gid {

src/libstd/sys/redox/syscall/call.rs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ pub fn dup(fd: usize, buf: &[u8]) -> Result<usize> {
7171
unsafe { syscall3(SYS_DUP, fd, buf.as_ptr() as usize, buf.len()) }
7272
}
7373

74+
/// Copy and transform a file descriptor
75+
pub fn dup2(fd: usize, newfd: usize, buf: &[u8]) -> Result<usize> {
76+
unsafe { syscall4(SYS_DUP2, fd, newfd, buf.as_ptr() as usize, buf.len()) }
77+
}
78+
7479
/// Replace the current process with a new executable
7580
pub fn execve(path: &str, args: &[[usize; 2]]) -> Result<usize> {
7681
unsafe { syscall4(SYS_EXECVE, path.as_ptr() as usize, path.len(),

src/libstd/sys/redox/syscall/number.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub const SYS_UNLINK: usize = SYS_CLASS_PATH | 10;
2828

2929
pub const SYS_CLOSE: usize = SYS_CLASS_FILE | 6;
3030
pub const SYS_DUP: usize = SYS_CLASS_FILE | SYS_RET_FILE | 41;
31+
pub const SYS_DUP2: usize = SYS_CLASS_FILE | SYS_RET_FILE | 63;
3132
pub const SYS_READ: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 3;
3233
pub const SYS_WRITE: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 4;
3334
pub const SYS_LSEEK: usize = SYS_CLASS_FILE | 19;

0 commit comments

Comments
 (0)