Skip to content

Commit 34e6673

Browse files
committed
Auto merge of #107405 - hermitcore:bsd, r=bjorn3
add support of RustyHermit's BSD socket layer RustyHermit is a tier 3 platform and publishes a new kernel interface. The new version supports a common BSD socket layer. By supporting this interface, the implementation of `std` can be harmonized to other operating systems. In `sys_common/mod.rs` we remove only a special case for RustyHermit. All changes are done in the RustyHermit specific directories. To realize this socket layer, the handling of file descriptors is also harmonized to other operating systems.
2 parents 31448ba + af8ee64 commit 34e6673

File tree

16 files changed

+594
-478
lines changed

16 files changed

+594
-478
lines changed

Cargo.lock

+10-2
Original file line numberDiff line numberDiff line change
@@ -1927,8 +1927,16 @@ version = "0.2.6"
19271927
source = "registry+https://github.com/rust-lang/crates.io-index"
19281928
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
19291929
dependencies = [
1930-
"compiler_builtins",
19311930
"libc",
1931+
]
1932+
1933+
[[package]]
1934+
name = "hermit-abi"
1935+
version = "0.3.0"
1936+
source = "registry+https://github.com/rust-lang/crates.io-index"
1937+
checksum = "856b5cb0902c2b6d65d5fd97dfa30f9b70c7538e770b98eab5ed52d8db923e01"
1938+
dependencies = [
1939+
"compiler_builtins",
19321940
"rustc-std-workspace-alloc",
19331941
"rustc-std-workspace-core",
19341942
]
@@ -5294,7 +5302,7 @@ dependencies = [
52945302
"dlmalloc",
52955303
"fortanix-sgx-abi",
52965304
"hashbrown 0.12.3",
5297-
"hermit-abi 0.2.6",
5305+
"hermit-abi 0.3.0",
52985306
"libc",
52995307
"miniz_oxide",
53005308
"object 0.29.0",

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }
4343
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'] }
4444

4545
[target.'cfg(target_os = "hermit")'.dependencies]
46-
hermit-abi = { version = "0.2.6", features = ['rustc-dep-of-std'] }
46+
hermit-abi = { version = "0.3.0", features = ['rustc-dep-of-std'] }
4747

4848
[target.wasm32-wasi.dependencies]
4949
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }

library/std/src/os/fd/owned.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::fs;
99
use crate::io;
1010
use crate::marker::PhantomData;
1111
use crate::mem::forget;
12-
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx")))]
12+
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit")))]
1313
use crate::sys::cvt;
1414
use crate::sys_common::{AsInner, FromInner, IntoInner};
1515

@@ -89,7 +89,7 @@ impl OwnedFd {
8989
impl BorrowedFd<'_> {
9090
/// Creates a new `OwnedFd` instance that shares the same underlying file
9191
/// description as the existing `BorrowedFd` instance.
92-
#[cfg(not(target_arch = "wasm32"))]
92+
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit")))]
9393
#[stable(feature = "io_safety", since = "1.63.0")]
9494
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
9595
// We want to atomically duplicate this file descriptor and set the
@@ -112,7 +112,7 @@ impl BorrowedFd<'_> {
112112

113113
/// Creates a new `OwnedFd` instance that shares the same underlying file
114114
/// description as the existing `BorrowedFd` instance.
115-
#[cfg(target_arch = "wasm32")]
115+
#[cfg(any(target_arch = "wasm32", target_os = "hermit"))]
116116
#[stable(feature = "io_safety", since = "1.63.0")]
117117
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
118118
Err(crate::io::const_io_error!(
@@ -174,7 +174,10 @@ impl Drop for OwnedFd {
174174
// the file descriptor was closed or not, and if we retried (for
175175
// something like EINTR), we might close another valid file descriptor
176176
// opened after we closed ours.
177+
#[cfg(not(target_os = "hermit"))]
177178
let _ = libc::close(self.fd);
179+
#[cfg(target_os = "hermit")]
180+
let _ = hermit_abi::close(self.fd);
178181
}
179182
}
180183
}

library/std/src/os/fd/raw.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use crate::fs;
66
use crate::io;
7+
#[cfg(target_os = "hermit")]
8+
use crate::os::hermit::io::OwnedFd;
9+
#[cfg(not(target_os = "hermit"))]
710
use crate::os::raw;
811
#[cfg(all(doc, not(target_arch = "wasm32")))]
912
use crate::os::unix::io::AsFd;
@@ -12,11 +15,18 @@ use crate::os::unix::io::OwnedFd;
1215
#[cfg(target_os = "wasi")]
1316
use crate::os::wasi::io::OwnedFd;
1417
use crate::sys_common::{AsInner, IntoInner};
18+
#[cfg(target_os = "hermit")]
19+
use hermit_abi as libc;
1520

1621
/// Raw file descriptors.
1722
#[rustc_allowed_through_unstable_modules]
1823
#[stable(feature = "rust1", since = "1.0.0")]
24+
#[cfg(not(target_os = "hermit"))]
1925
pub type RawFd = raw::c_int;
26+
#[rustc_allowed_through_unstable_modules]
27+
#[stable(feature = "rust1", since = "1.0.0")]
28+
#[cfg(target_os = "hermit")]
29+
pub type RawFd = i32;
2030

2131
/// A trait to extract the raw file descriptor from an underlying object.
2232
///

library/std/src/os/hermit/io/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![stable(feature = "os_fd", since = "1.66.0")]
2+
3+
mod net;
4+
#[path = "../../fd/owned.rs"]
5+
mod owned;
6+
#[path = "../../fd/raw.rs"]
7+
mod raw;
8+
9+
// Export the types and traits for the public API.
10+
#[stable(feature = "os_fd", since = "1.66.0")]
11+
pub use owned::*;
12+
#[stable(feature = "os_fd", since = "1.66.0")]
13+
pub use raw::*;

library/std/src/os/hermit/io/net.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use crate::os::hermit::io::OwnedFd;
2+
use crate::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
3+
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
4+
use crate::{net, sys};
5+
6+
macro_rules! impl_as_raw_fd {
7+
($($t:ident)*) => {$(
8+
#[stable(feature = "rust1", since = "1.0.0")]
9+
impl AsRawFd for net::$t {
10+
#[inline]
11+
fn as_raw_fd(&self) -> RawFd {
12+
self.as_inner().socket().as_raw_fd()
13+
}
14+
}
15+
)*};
16+
}
17+
impl_as_raw_fd! { TcpStream TcpListener UdpSocket }
18+
19+
macro_rules! impl_from_raw_fd {
20+
($($t:ident)*) => {$(
21+
#[stable(feature = "from_raw_os", since = "1.1.0")]
22+
impl FromRawFd for net::$t {
23+
#[inline]
24+
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
25+
unsafe {
26+
let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
27+
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
28+
}
29+
}
30+
}
31+
)*};
32+
}
33+
impl_from_raw_fd! { TcpStream TcpListener UdpSocket }
34+
35+
macro_rules! impl_into_raw_fd {
36+
($($t:ident)*) => {$(
37+
#[stable(feature = "into_raw_os", since = "1.4.0")]
38+
impl IntoRawFd for net::$t {
39+
#[inline]
40+
fn into_raw_fd(self) -> RawFd {
41+
self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
42+
}
43+
}
44+
)*};
45+
}
46+
impl_into_raw_fd! { TcpStream TcpListener UdpSocket }

library/std/src/os/hermit/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#![stable(feature = "rust1", since = "1.0.0")]
22

3+
#[allow(unused_extern_crates)]
4+
#[stable(feature = "rust1", since = "1.0.0")]
5+
pub extern crate hermit_abi as abi;
6+
37
pub mod ffi;
8+
pub mod io;
49

510
/// A prelude for conveniently writing platform-specific code.
611
///

library/std/src/os/mod.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ pub mod windows {}
6060
all(target_vendor = "fortanix", target_env = "sgx")
6161
)
6262
)))]
63-
#[cfg(target_os = "hermit")]
64-
#[path = "hermit/mod.rs"]
65-
pub mod unix;
66-
#[cfg(not(all(
67-
doc,
68-
any(
69-
all(target_arch = "wasm32", not(target_os = "wasi")),
70-
all(target_vendor = "fortanix", target_env = "sgx")
71-
)
72-
)))]
7363
#[cfg(all(not(target_os = "hermit"), any(unix, doc)))]
7464
pub mod unix;
7565

@@ -123,6 +113,8 @@ pub mod freebsd;
123113
pub mod fuchsia;
124114
#[cfg(target_os = "haiku")]
125115
pub mod haiku;
116+
#[cfg(target_os = "hermit")]
117+
pub mod hermit;
126118
#[cfg(target_os = "horizon")]
127119
pub mod horizon;
128120
#[cfg(target_os = "illumos")]

library/std/src/sys/hermit/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ffi::{c_char, CStr, OsString};
22
use crate::fmt;
3-
use crate::os::unix::ffi::OsStringExt;
3+
use crate::os::hermit::ffi::OsStringExt;
44
use crate::ptr;
55
use crate::sync::atomic::{
66
AtomicIsize, AtomicPtr,

library/std/src/sys/hermit/fd.rs

+45-32
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
#![unstable(reason = "not public", issue = "none", feature = "fd")]
22

33
use crate::io::{self, Read};
4-
use crate::mem;
4+
use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd};
55
use crate::sys::cvt;
66
use crate::sys::hermit::abi;
77
use crate::sys::unsupported;
8-
use crate::sys_common::AsInner;
8+
use crate::sys_common::{AsInner, FromInner, IntoInner};
9+
10+
use crate::os::hermit::io::*;
911

1012
#[derive(Debug)]
1113
pub struct FileDesc {
12-
fd: i32,
14+
fd: OwnedFd,
1315
}
1416

1517
impl FileDesc {
16-
pub fn new(fd: i32) -> FileDesc {
17-
FileDesc { fd }
18-
}
19-
20-
pub fn raw(&self) -> i32 {
21-
self.fd
22-
}
23-
24-
/// Extracts the actual file descriptor without closing it.
25-
pub fn into_raw(self) -> i32 {
26-
let fd = self.fd;
27-
mem::forget(self);
28-
fd
29-
}
30-
3118
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
32-
let result = unsafe { abi::read(self.fd, buf.as_mut_ptr(), buf.len()) };
33-
cvt(result as i32)
19+
let result = cvt(unsafe { abi::read(self.fd.as_raw_fd(), buf.as_mut_ptr(), buf.len()) })?;
20+
Ok(result as usize)
3421
}
3522

3623
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
@@ -39,8 +26,8 @@ impl FileDesc {
3926
}
4027

4128
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
42-
let result = unsafe { abi::write(self.fd, buf.as_ptr(), buf.len()) };
43-
cvt(result as i32)
29+
let result = cvt(unsafe { abi::write(self.fd.as_raw_fd(), buf.as_ptr(), buf.len()) })?;
30+
Ok(result as usize)
4431
}
4532

4633
pub fn duplicate(&self) -> io::Result<FileDesc> {
@@ -69,19 +56,45 @@ impl<'a> Read for &'a FileDesc {
6956
}
7057
}
7158

72-
impl AsInner<i32> for FileDesc {
73-
fn as_inner(&self) -> &i32 {
59+
impl IntoInner<OwnedFd> for FileDesc {
60+
fn into_inner(self) -> OwnedFd {
61+
self.fd
62+
}
63+
}
64+
65+
impl FromInner<OwnedFd> for FileDesc {
66+
fn from_inner(owned_fd: OwnedFd) -> Self {
67+
Self { fd: owned_fd }
68+
}
69+
}
70+
71+
impl FromRawFd for FileDesc {
72+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
73+
Self { fd: FromRawFd::from_raw_fd(raw_fd) }
74+
}
75+
}
76+
77+
impl AsInner<OwnedFd> for FileDesc {
78+
fn as_inner(&self) -> &OwnedFd {
7479
&self.fd
7580
}
7681
}
7782

78-
impl Drop for FileDesc {
79-
fn drop(&mut self) {
80-
// Note that errors are ignored when closing a file descriptor. The
81-
// reason for this is that if an error occurs we don't actually know if
82-
// the file descriptor was closed or not, and if we retried (for
83-
// something like EINTR), we might close another valid file descriptor
84-
// (opened after we closed ours.
85-
let _ = unsafe { abi::close(self.fd) };
83+
impl AsFd for FileDesc {
84+
fn as_fd(&self) -> BorrowedFd<'_> {
85+
self.fd.as_fd()
86+
}
87+
}
88+
89+
impl AsRawFd for FileDesc {
90+
#[inline]
91+
fn as_raw_fd(&self) -> RawFd {
92+
self.fd.as_raw_fd()
93+
}
94+
}
95+
96+
impl IntoRawFd for FileDesc {
97+
fn into_raw_fd(self) -> RawFd {
98+
self.fd.into_raw_fd()
8699
}
87100
}

0 commit comments

Comments
 (0)