1
Fork 0

Auto merge of #83170 - joshtriplett:spawn-cleanup, r=kennytm

Simplify Command::spawn (no semantic change)

This minimizes the size of an unsafe block, and allows outdenting some
complex code.
This commit is contained in:
bors 2021-03-30 14:26:01 +00:00
commit 7b6fc5a3dd

View file

@ -51,41 +51,35 @@ impl Command {
// a lock any more because the parent won't do anything and the child is // a lock any more because the parent won't do anything and the child is
// in its own process. Thus the parent drops the lock guard while the child // in its own process. Thus the parent drops the lock guard while the child
// forgets it to avoid unlocking it on a new thread, which would be invalid. // forgets it to avoid unlocking it on a new thread, which would be invalid.
let (env_lock, result) = unsafe { (sys::os::env_read_lock(), cvt(libc::fork())?) }; let (env_lock, pid) = unsafe { (sys::os::env_read_lock(), cvt(libc::fork())?) };
let pid = unsafe { if pid == 0 {
match result { mem::forget(env_lock);
0 => { drop(input);
mem::forget(env_lock); let Err(err) = unsafe { self.do_exec(theirs, envp.as_ref()) };
drop(input); let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
let Err(err) = self.do_exec(theirs, envp.as_ref()); let errno = errno.to_be_bytes();
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32; let bytes = [
let errno = errno.to_be_bytes(); errno[0],
let bytes = [ errno[1],
errno[0], errno[2],
errno[1], errno[3],
errno[2], CLOEXEC_MSG_FOOTER[0],
errno[3], CLOEXEC_MSG_FOOTER[1],
CLOEXEC_MSG_FOOTER[0], CLOEXEC_MSG_FOOTER[2],
CLOEXEC_MSG_FOOTER[1], CLOEXEC_MSG_FOOTER[3],
CLOEXEC_MSG_FOOTER[2], ];
CLOEXEC_MSG_FOOTER[3], // pipe I/O up to PIPE_BUF bytes should be atomic, and then
]; // we want to be sure we *don't* run at_exit destructors as
// pipe I/O up to PIPE_BUF bytes should be atomic, and then // we're being torn down regardless
// we want to be sure we *don't* run at_exit destructors as rtassert!(output.write(&bytes).is_ok());
// we're being torn down regardless unsafe { libc::_exit(1) }
rtassert!(output.write(&bytes).is_ok()); }
libc::_exit(1)
} drop(env_lock);
n => { drop(output);
drop(env_lock);
n
}
}
};
let mut p = Process { pid, status: None }; let mut p = Process { pid, status: None };
drop(output);
let mut bytes = [0; 8]; let mut bytes = [0; 8];
// loop to handle EINTR // loop to handle EINTR