如何在不使用不安全的情况下读取自定义文件描述符?
How to read from custom file descriptors without using unsafe?
从this answer,我了解到可以使用unsafe
读取文件描述符:
use std::{
fs::File,
io::{self, Read},
os::unix::io::FromRawFd,
};
fn main() -> io::Result<()> {
let mut f = unsafe { File::from_raw_fd(3) };
let mut input = String::new();
f.read_to_string(&mut input)?;
println!("I read: {}", input);
Ok(())
}
$ cat /tmp/output
Hello, world!
$ target/debug/example 3< /tmp/output
I read: Hello, world!
如何在不使用 unsafe
的情况下获得相同的结果?
我目前正在创建这样的文件描述符 (zsh
shell):
function test_fd {
if ! read -r line <&; then
line="[Read on fd failed]"
fi
echo $line
# Remove the handler and close the fd
zle -F
exec {1}<&-
}
exec {FD}< <(/path/to/my/app)
zle -F $FD test_fd
我想用 read
或更好的东西替换 test_fd
,如果它可以 read & close
提供的文件描述符,这样我就可以用这样的东西结尾:
function test_fd {
/something/in/rust "$@"
}
exec {FD}< <(/path/to/my/app)
zle -F $FD test_fd
你不能这样做。您唯一的途径是使用 unsafe
.
如the documentation for FromRawFd
所述:
This function is also unsafe as the primitives currently returned have the contract that they are the sole owner of the file descriptor they are wrapping. Usage of this function could accidentally allow violating this contract which can cause memory unsafety in code that relies on it being true.
您可能能够使用fcntl
function to test if a given descriptor is valid, but I don't know the details about how these work in the presence of threading — it's possible that one thread checks for validity of the file descriptor and it's valid, another thread closes it, then the first attempts to use it. This is a straight-forward Time-of-check to time-of-use issue。
另请参阅:
- How to check if a given file descriptor stored in a variable is still valid?
从this answer,我了解到可以使用unsafe
读取文件描述符:
use std::{
fs::File,
io::{self, Read},
os::unix::io::FromRawFd,
};
fn main() -> io::Result<()> {
let mut f = unsafe { File::from_raw_fd(3) };
let mut input = String::new();
f.read_to_string(&mut input)?;
println!("I read: {}", input);
Ok(())
}
$ cat /tmp/output
Hello, world!
$ target/debug/example 3< /tmp/output
I read: Hello, world!
如何在不使用 unsafe
的情况下获得相同的结果?
我目前正在创建这样的文件描述符 (zsh
shell):
function test_fd {
if ! read -r line <&; then
line="[Read on fd failed]"
fi
echo $line
# Remove the handler and close the fd
zle -F
exec {1}<&-
}
exec {FD}< <(/path/to/my/app)
zle -F $FD test_fd
我想用 read
或更好的东西替换 test_fd
,如果它可以 read & close
提供的文件描述符,这样我就可以用这样的东西结尾:
function test_fd {
/something/in/rust "$@"
}
exec {FD}< <(/path/to/my/app)
zle -F $FD test_fd
你不能这样做。您唯一的途径是使用 unsafe
.
如the documentation for FromRawFd
所述:
This function is also unsafe as the primitives currently returned have the contract that they are the sole owner of the file descriptor they are wrapping. Usage of this function could accidentally allow violating this contract which can cause memory unsafety in code that relies on it being true.
您可能能够使用fcntl
function to test if a given descriptor is valid, but I don't know the details about how these work in the presence of threading — it's possible that one thread checks for validity of the file descriptor and it's valid, another thread closes it, then the first attempts to use it. This is a straight-forward Time-of-check to time-of-use issue。
另请参阅:
- How to check if a given file descriptor stored in a variable is still valid?