Skip to content

Commit 33a2191

Browse files
committed
std: Clean up process spawn impl on unix
* 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
1 parent d6c7230 commit 33a2191

File tree

6 files changed

+214
-250
lines changed

6 files changed

+214
-250
lines changed

src/libstd/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ fn setup_io(io: &StdioImp, fd: libc::c_int, readable: bool)
340340
(Some(AnonPipe::from_fd(fd)), None)
341341
}
342342
Piped => {
343-
let (reader, writer) = try!(unsafe { pipe2::anon_pipe() });
343+
let (reader, writer) = try!(pipe2::anon_pipe());
344344
if readable {
345345
(Some(reader), Some(writer))
346346
} else {

src/libstd/sys/unix/c.rs

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ extern {
159159
pub fn utimes(filename: *const libc::c_char,
160160
times: *const libc::timeval) -> libc::c_int;
161161
pub fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char;
162+
pub fn setgroups(ngroups: libc::c_int,
163+
ptr: *const libc::c_void) -> libc::c_int;
162164
}
163165

164166
#[cfg(any(target_os = "macos", target_os = "ios"))]

src/libstd/sys/unix/fs2.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,17 @@ impl OpenOptions {
205205

206206
impl File {
207207
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
208+
let path = try!(cstr(path));
209+
File::open_c(&path, opts)
210+
}
211+
212+
pub fn open_c(path: &CStr, opts: &OpenOptions) -> io::Result<File> {
208213
let flags = opts.flags | match (opts.read, opts.write) {
209214
(true, true) => libc::O_RDWR,
210215
(false, true) => libc::O_WRONLY,
211216
(true, false) |
212217
(false, false) => libc::O_RDONLY,
213218
};
214-
let path = try!(cstr(path));
215219
let fd = try!(cvt_r(|| unsafe {
216220
libc::open(path.as_ptr(), flags, opts.mode)
217221
}));
@@ -220,6 +224,8 @@ impl File {
220224
Ok(File(fd))
221225
}
222226

227+
pub fn into_fd(self) -> FileDesc { self.0 }
228+
223229
pub fn file_attr(&self) -> io::Result<FileAttr> {
224230
let mut stat: libc::stat = unsafe { mem::zeroed() };
225231
try!(cvt(unsafe { libc::fstat(self.0.raw(), &mut stat) }));

src/libstd/sys/unix/pipe2.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ use libc;
2020

2121
pub struct AnonPipe(FileDesc);
2222

23-
pub unsafe fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
23+
pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
2424
let mut fds = [0; 2];
25-
if libc::pipe(fds.as_mut_ptr()) == 0 {
26-
Ok((AnonPipe::from_fd(fds[0]),
27-
AnonPipe::from_fd(fds[1])))
25+
if unsafe { libc::pipe(fds.as_mut_ptr()) == 0 } {
26+
Ok((AnonPipe::from_fd(fds[0]), AnonPipe::from_fd(fds[1])))
2827
} else {
2928
Err(io::Error::last_os_error())
3029
}
@@ -45,7 +44,7 @@ impl AnonPipe {
4544
self.0.write(buf)
4645
}
4746

48-
pub fn raw(&self) -> libc::c_int {
49-
self.0.raw()
47+
pub fn into_fd(self) -> FileDesc {
48+
self.0
5049
}
5150
}

0 commit comments

Comments
 (0)