Skip to content

Commit

Permalink
std: Clean up process spawn impl on unix
Browse files Browse the repository at this point in the history
* De-indent quite a bit by removing usage of FnOnce closures
* Clearly separate code for the parent/child after the fork
* Use `fs2::{File, OpenOptions}` instead of calling `open` manually
* Use RAII to close I/O objects wherever possible
* Remove loop for closing all file descriptors, all our own ones are now
  `CLOEXEC` by default so they cannot be inherited
  • Loading branch information
alexcrichton committed Apr 10, 2015
1 parent d6c7230 commit 33a2191
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 250 deletions.
2 changes: 1 addition & 1 deletion src/libstd/process.rs
Expand Up @@ -340,7 +340,7 @@ fn setup_io(io: &StdioImp, fd: libc::c_int, readable: bool)
(Some(AnonPipe::from_fd(fd)), None)
}
Piped => {
let (reader, writer) = try!(unsafe { pipe2::anon_pipe() });
let (reader, writer) = try!(pipe2::anon_pipe());
if readable {
(Some(reader), Some(writer))
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/unix/c.rs
Expand Up @@ -159,6 +159,8 @@ extern {
pub fn utimes(filename: *const libc::c_char,
times: *const libc::timeval) -> libc::c_int;
pub fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char;
pub fn setgroups(ngroups: libc::c_int,
ptr: *const libc::c_void) -> libc::c_int;
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand Down
8 changes: 7 additions & 1 deletion src/libstd/sys/unix/fs2.rs
Expand Up @@ -205,13 +205,17 @@ impl OpenOptions {

impl File {
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
let path = try!(cstr(path));
File::open_c(&path, opts)
}

pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
let flags = opts.flags | match (opts.read, opts.write) {
(true, true) => libc::O_RDWR,
(false, true) => libc::O_WRONLY,
(true, false) |
(false, false) => libc::O_RDONLY,
};
let path = try!(cstr(path));
let fd = try!(cvt_r(|| unsafe {
libc::open(path.as_ptr(), flags, opts.mode)
}));
Expand All @@ -220,6 +224,8 @@ impl File {
Ok(File(fd))
}

pub fn into_fd(self) -> FileDesc { self.0 }

pub fn file_attr(&self) -> io::Result<FileAttr> {
let mut stat: libc::stat = unsafe { mem::zeroed() };
try!(cvt(unsafe { libc::fstat(self.0.raw(), &mut stat) }));
Expand Down
11 changes: 5 additions & 6 deletions src/libstd/sys/unix/pipe2.rs
Expand Up @@ -20,11 +20,10 @@ use libc;

pub struct AnonPipe(FileDesc);

pub unsafe fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
let mut fds = [0; 2];
if libc::pipe(fds.as_mut_ptr()) == 0 {
Ok((AnonPipe::from_fd(fds[0]),
AnonPipe::from_fd(fds[1])))
if unsafe { libc::pipe(fds.as_mut_ptr()) == 0 } {
Ok((AnonPipe::from_fd(fds[0]), AnonPipe::from_fd(fds[1])))
} else {
Err(io::Error::last_os_error())
}
Expand All @@ -45,7 +44,7 @@ impl AnonPipe {
self.0.write(buf)
}

pub fn raw(&self) -> libc::c_int {
self.0.raw()
pub fn into_fd(self) -> FileDesc {
self.0
}
}

0 comments on commit 33a2191

Please sign in to comment.