Skip to content

Commit a206097

Browse files
authored
Rollup merge of rust-lang#137494 - nabijaczleweli:dup, r=Mark-Simulacrum
libstd: init(): dup() subsequent /dev/nulls instead of opening them again This will be faster, and also it deduplicates the code so win/win The dup() is actually infallible here. But whatever. Before: ``` poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 1 ([{fd=2, revents=POLLNVAL}]) openat(AT_FDCWD, "/dev/null", O_RDWR) = 2 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f5749313050}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 2 ([{fd=0, revents=POLLNVAL}, {fd=2, revents=POLLNVAL}]) openat(AT_FDCWD, "/dev/null", O_RDWR) = 0 openat(AT_FDCWD, "/dev/null", O_RDWR) = 2 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7efe12006050}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 3 ([{fd=0, revents=POLLNVAL}, {fd=1, revents=POLLNVAL}, {fd=2, revents=POLLNVAL}]) openat(AT_FDCWD, "/dev/null", O_RDWR) = 0 openat(AT_FDCWD, "/dev/null", O_RDWR) = 1 openat(AT_FDCWD, "/dev/null", O_RDWR) = 2 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fc2dc7ca050}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 ``` After: ``` poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 1 ([{fd=1, revents=POLLNVAL}]) openat(AT_FDCWD, "/dev/null", O_RDWR) = 1 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f488a3fb050}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 2 ([{fd=1, revents=POLLNVAL}, {fd=2, revents=POLLNVAL}]) openat(AT_FDCWD, "/dev/null", O_RDWR) = 1 dup(1) = 2 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f1a8943c050}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 poll([{fd=0, events=0}, {fd=1, events=0}, {fd=2, events=0}], 3, 0) = 3 ([{fd=0, revents=POLLNVAL}, {fd=1, revents=POLLNVAL}, {fd=2, revents=POLLNVAL}]) openat(AT_FDCWD, "/dev/null", O_RDWR) = 0 dup(0) = 1 dup(0) = 2 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[PIPE], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f4e3a4c7050}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0 ```
2 parents 7cd6e2f + c73cfb8 commit a206097

File tree

1 file changed

+26
-24
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+26
-24
lines changed

library/std/src/sys/pal/unix/mod.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
5959
}
6060

6161
unsafe fn sanitize_standard_fds() {
62+
#[allow(dead_code)]
63+
let mut opened_devnull = -1;
64+
#[allow(dead_code)]
65+
let mut open_devnull = || {
66+
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
67+
use libc::open;
68+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
69+
use libc::open64 as open;
70+
71+
if opened_devnull != -1 {
72+
if libc::dup(opened_devnull) != -1 {
73+
return;
74+
}
75+
}
76+
opened_devnull = open(c"/dev/null".as_ptr(), libc::O_RDWR, 0);
77+
if opened_devnull == -1 {
78+
// If the stream is closed but we failed to reopen it, abort the
79+
// process. Otherwise we wouldn't preserve the safety of
80+
// operations on the corresponding Rust object Stdin, Stdout, or
81+
// Stderr.
82+
libc::abort();
83+
}
84+
};
85+
6286
// fast path with a single syscall for systems with poll()
6387
#[cfg(not(any(
6488
miri,
@@ -74,11 +98,6 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
7498
target_vendor = "apple",
7599
)))]
76100
'poll: {
77-
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
78-
use libc::open as open64;
79-
#[cfg(all(target_os = "linux", target_env = "gnu"))]
80-
use libc::open64;
81-
82101
use crate::sys::os::errno;
83102
let pfds: &mut [_] = &mut [
84103
libc::pollfd { fd: 0, events: 0, revents: 0 },
@@ -106,13 +125,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
106125
if pfd.revents & libc::POLLNVAL == 0 {
107126
continue;
108127
}
109-
if open64(c"/dev/null".as_ptr(), libc::O_RDWR, 0) == -1 {
110-
// If the stream is closed but we failed to reopen it, abort the
111-
// process. Otherwise we wouldn't preserve the safety of
112-
// operations on the corresponding Rust object Stdin, Stdout, or
113-
// Stderr.
114-
libc::abort();
115-
}
128+
open_devnull();
116129
}
117130
return;
118131
}
@@ -129,21 +142,10 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
129142
target_os = "vita",
130143
)))]
131144
{
132-
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
133-
use libc::open as open64;
134-
#[cfg(all(target_os = "linux", target_env = "gnu"))]
135-
use libc::open64;
136-
137145
use crate::sys::os::errno;
138146
for fd in 0..3 {
139147
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
140-
if open64(c"/dev/null".as_ptr(), libc::O_RDWR, 0) == -1 {
141-
// If the stream is closed but we failed to reopen it, abort the
142-
// process. Otherwise we wouldn't preserve the safety of
143-
// operations on the corresponding Rust object Stdin, Stdout, or
144-
// Stderr.
145-
libc::abort();
146-
}
148+
open_devnull();
147149
}
148150
}
149151
}

0 commit comments

Comments
 (0)