Skip to content

Commit 68dbdfb

Browse files
committed
Simplify Command::spawn (no semantic change)
This minimizes the size of an unsafe block, and allows outdenting some complex code.
1 parent 2917eda commit 68dbdfb

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

library/std/src/sys/unix/process/process_unix.rs

+27-33
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,35 @@ impl Command {
5151
// a lock any more because the parent won't do anything and the child is
5252
// in its own process. Thus the parent drops the lock guard while the child
5353
// forgets it to avoid unlocking it on a new thread, which would be invalid.
54-
let (env_lock, result) = unsafe { (sys::os::env_read_lock(), cvt(libc::fork())?) };
55-
56-
let pid = unsafe {
57-
match result {
58-
0 => {
59-
mem::forget(env_lock);
60-
drop(input);
61-
let Err(err) = self.do_exec(theirs, envp.as_ref());
62-
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
63-
let errno = errno.to_be_bytes();
64-
let bytes = [
65-
errno[0],
66-
errno[1],
67-
errno[2],
68-
errno[3],
69-
CLOEXEC_MSG_FOOTER[0],
70-
CLOEXEC_MSG_FOOTER[1],
71-
CLOEXEC_MSG_FOOTER[2],
72-
CLOEXEC_MSG_FOOTER[3],
73-
];
74-
// pipe I/O up to PIPE_BUF bytes should be atomic, and then
75-
// we want to be sure we *don't* run at_exit destructors as
76-
// we're being torn down regardless
77-
rtassert!(output.write(&bytes).is_ok());
78-
libc::_exit(1)
79-
}
80-
n => {
81-
drop(env_lock);
82-
n
83-
}
84-
}
85-
};
54+
let (env_lock, pid) = unsafe { (sys::os::env_read_lock(), cvt(libc::fork())?) };
8655

87-
let mut p = Process { pid, status: None };
56+
if pid == 0 {
57+
mem::forget(env_lock);
58+
drop(input);
59+
let Err(err) = unsafe { self.do_exec(theirs, envp.as_ref()) };
60+
let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
61+
let errno = errno.to_be_bytes();
62+
let bytes = [
63+
errno[0],
64+
errno[1],
65+
errno[2],
66+
errno[3],
67+
CLOEXEC_MSG_FOOTER[0],
68+
CLOEXEC_MSG_FOOTER[1],
69+
CLOEXEC_MSG_FOOTER[2],
70+
CLOEXEC_MSG_FOOTER[3],
71+
];
72+
// pipe I/O up to PIPE_BUF bytes should be atomic, and then
73+
// we want to be sure we *don't* run at_exit destructors as
74+
// we're being torn down regardless
75+
rtassert!(output.write(&bytes).is_ok());
76+
unsafe { libc::_exit(1) }
77+
}
78+
79+
drop(env_lock);
8880
drop(output);
81+
82+
let mut p = Process { pid, status: None };
8983
let mut bytes = [0; 8];
9084

9185
// loop to handle EINTR

0 commit comments

Comments
 (0)