Skip to content

Commit 1bfbb03

Browse files
Merge #2012
2012: Enable socket and select on redox r=asomers a=coolreader18 Co-authored-by: Noa <[email protected]>
2 parents 7b0cd34 + fff2ca4 commit 1bfbb03

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3030
`sys::kevent::Kqueue::kevent`, and `sys::event::kqueue` is deprecated in
3131
favor of `sys::kevent::Kqueue::new`.
3232
([#1943](https://github.com/nix-rust/nix/pull/1943))
33+
- `nix::socket` and `nix::select` are now available on Redox.
34+
([#2012](https://github.com/nix-rust/nix/pull/2012))
3335

3436
### Fixed
3537
- Fix `SockaddrIn6` bug that was swapping flowinfo and scope_id byte ordering.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ targets = [
2727
]
2828

2929
[dependencies]
30-
libc = { git = "https://github.com/rust-lang/libc", rev = "44cc30c6b68427d3628926868758d35fe561bbe6", features = [ "extra_traits" ] }
30+
libc = { git = "https://github.com/rust-lang/libc", rev = "60bf6d7fa9d561820ea562751ee455ccf67d3015", features = [ "extra_traits" ] }
3131
bitflags = "1.1"
3232
cfg-if = "1.0"
3333
pin-utils = { version = "0.1.0", optional = true }
3434
static_assertions = "1"
35-
36-
[target.'cfg(not(target_os = "redox"))'.dependencies]
3735
memoffset = { version = "0.8", optional = true }
3836

3937
[features]

src/sys/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ feature! {
110110
pub mod resource;
111111
}
112112

113-
#[cfg(not(target_os = "redox"))]
114113
feature! {
115114
#![feature = "poll"]
116115
pub mod select;
@@ -138,7 +137,6 @@ feature! {
138137
pub mod signalfd;
139138
}
140139

141-
#[cfg(not(target_os = "redox"))]
142140
feature! {
143141
#![feature = "socket"]
144142
#[allow(missing_docs)]

src/sys/socket/addr.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ pub enum AddressFamily {
100100
#[cfg_attr(docsrs, doc(cfg(all())))]
101101
Ax25 = libc::AF_AX25,
102102
/// IPX - Novell protocols
103+
#[cfg(not(target_os = "redox"))]
103104
Ipx = libc::AF_IPX,
104105
/// AppleTalk
106+
#[cfg(not(target_os = "redox"))]
105107
AppleTalk = libc::AF_APPLETALK,
106108
/// AX.25 packet layer protocol.
107109
/// (see [netrom(4)](https://www.unix.com/man-page/linux/4/netrom/))
@@ -130,7 +132,7 @@ pub enum AddressFamily {
130132
#[cfg_attr(docsrs, doc(cfg(all())))]
131133
Rose = libc::AF_ROSE,
132134
/// DECet protocol sockets.
133-
#[cfg(not(target_os = "haiku"))]
135+
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
134136
Decnet = libc::AF_DECnet,
135137
/// Reserved for "802.2LLC project"; never used.
136138
#[cfg(any(target_os = "android", target_os = "linux"))]
@@ -162,7 +164,7 @@ pub enum AddressFamily {
162164
#[cfg_attr(docsrs, doc(cfg(all())))]
163165
Rds = libc::AF_RDS,
164166
/// IBM SNA
165-
#[cfg(not(target_os = "haiku"))]
167+
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
166168
Sna = libc::AF_SNA,
167169
/// Socket interface over IrDA
168170
#[cfg(any(target_os = "android", target_os = "linux"))]
@@ -202,7 +204,8 @@ pub enum AddressFamily {
202204
target_os = "illumos",
203205
target_os = "ios",
204206
target_os = "macos",
205-
target_os = "solaris"
207+
target_os = "solaris",
208+
target_os = "redox",
206209
)))]
207210
#[cfg_attr(docsrs, doc(cfg(all())))]
208211
Bluetooth = libc::AF_BLUETOOTH,
@@ -219,7 +222,8 @@ pub enum AddressFamily {
219222
#[cfg(not(any(
220223
target_os = "illumos",
221224
target_os = "solaris",
222-
target_os = "haiku"
225+
target_os = "haiku",
226+
target_os = "redox",
223227
)))]
224228
#[cfg_attr(docsrs, doc(cfg(all())))]
225229
Isdn = libc::AF_ISDN,
@@ -460,7 +464,8 @@ pub struct UnixAddr {
460464
target_os = "android",
461465
target_os = "fuchsia",
462466
target_os = "illumos",
463-
target_os = "linux"
467+
target_os = "linux",
468+
target_os = "redox",
464469
))]
465470
sun_len: u8,
466471
}
@@ -624,7 +629,8 @@ impl UnixAddr {
624629
if #[cfg(any(target_os = "android",
625630
target_os = "fuchsia",
626631
target_os = "illumos",
627-
target_os = "linux"
632+
target_os = "linux",
633+
target_os = "redox",
628634
))]
629635
{
630636
UnixAddr { sun, sun_len }
@@ -690,7 +696,8 @@ impl UnixAddr {
690696
if #[cfg(any(target_os = "android",
691697
target_os = "fuchsia",
692698
target_os = "illumos",
693-
target_os = "linux"
699+
target_os = "linux",
700+
target_os = "redox",
694701
))]
695702
{
696703
self.sun_len
@@ -736,7 +743,8 @@ impl SockaddrLike for UnixAddr {
736743
if #[cfg(any(target_os = "android",
737744
target_os = "fuchsia",
738745
target_os = "illumos",
739-
target_os = "linux"
746+
target_os = "linux",
747+
target_os = "redox",
740748
))] {
741749
let su_len = len.unwrap_or(
742750
mem::size_of::<libc::sockaddr_un>() as libc::socklen_t
@@ -1221,7 +1229,7 @@ pub union SockaddrStorage {
12211229
#[cfg(any(target_os = "android", target_os = "linux"))]
12221230
#[cfg_attr(docsrs, doc(cfg(all())))]
12231231
alg: AlgAddr,
1224-
#[cfg(feature = "net")]
1232+
#[cfg(all(feature = "net", not(target_os = "redox")))]
12251233
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
12261234
dl: LinkAddr,
12271235
#[cfg(any(target_os = "android", target_os = "linux"))]
@@ -2338,6 +2346,7 @@ mod tests {
23382346
}
23392347
}
23402348

2349+
#[cfg(not(target_os = "redox"))]
23412350
mod link {
23422351
#![allow(clippy::cast_ptr_alignment)]
23432352

@@ -2534,7 +2543,7 @@ mod tests {
25342543
nix_sin6.0.sin6_flowinfo = 0x12345678;
25352544
nix_sin6.0.sin6_scope_id = 0x9abcdef0;
25362545

2537-
let std_sin6 : std::net::SocketAddrV6 = nix_sin6.into();
2546+
let std_sin6: std::net::SocketAddrV6 = nix_sin6.into();
25382547
assert_eq!(nix_sin6, std_sin6.into());
25392548
}
25402549
}

src/sys/socket/mod.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
#[cfg(target_os = "linux")]
55
#[cfg(feature = "uio")]
66
use crate::sys::time::TimeSpec;
7+
#[cfg(not(target_os = "redox"))]
78
#[cfg(feature = "uio")]
89
use crate::sys::time::TimeVal;
910
use crate::{errno::Errno, Result};
1011
use cfg_if::cfg_if;
12+
use libc::{self, c_int, c_void, size_t, socklen_t};
13+
#[cfg(all(feature = "uio", not(target_os = "redox")))]
1114
use libc::{
12-
self, c_int, c_void, iovec, size_t, socklen_t, CMSG_DATA, CMSG_FIRSTHDR,
13-
CMSG_LEN, CMSG_NXTHDR,
15+
iovec, CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE,
1416
};
17+
#[cfg(not(target_os = "redox"))]
1518
use std::io::{IoSlice, IoSliceMut};
1619
#[cfg(feature = "net")]
1720
use std::net;
1821
use std::os::unix::io::RawFd;
19-
use std::{mem, ptr, slice};
22+
use std::{mem, ptr};
2023

2124
#[deny(missing_docs)]
2225
mod addr;
@@ -38,14 +41,16 @@ pub use self::addr::{AddressFamily, UnixAddr};
3841
#[cfg(not(any(
3942
target_os = "illumos",
4043
target_os = "solaris",
41-
target_os = "haiku"
44+
target_os = "haiku",
45+
target_os = "redox",
4246
)))]
4347
#[cfg(feature = "net")]
4448
pub use self::addr::{LinkAddr, SockaddrIn, SockaddrIn6};
4549
#[cfg(any(
4650
target_os = "illumos",
4751
target_os = "solaris",
48-
target_os = "haiku"
52+
target_os = "haiku",
53+
target_os = "redox",
4954
))]
5055
#[cfg(feature = "net")]
5156
pub use self::addr::{SockaddrIn, SockaddrIn6};
@@ -60,16 +65,12 @@ pub use crate::sys::socket::addr::sys_control::SysControlAddr;
6065
#[cfg(any(target_os = "android", target_os = "linux"))]
6166
pub use crate::sys::socket::addr::vsock::VsockAddr;
6267

63-
#[cfg(feature = "uio")]
68+
#[cfg(all(feature = "uio", not(target_os = "redox")))]
6469
pub use libc::{cmsghdr, msghdr};
6570
pub use libc::{sa_family_t, sockaddr, sockaddr_storage, sockaddr_un};
6671
#[cfg(feature = "net")]
6772
pub use libc::{sockaddr_in, sockaddr_in6};
6873

69-
// Needed by the cmsg_space macro
70-
#[doc(hidden)]
71-
pub use libc::{c_uint, CMSG_SPACE};
72-
7374
#[cfg(feature = "net")]
7475
use crate::sys::socket::addr::{ipv4addr_to_libc, ipv6addr_to_libc};
7576

@@ -92,10 +93,11 @@ pub enum SockType {
9293
/// entire packet with each input system call.
9394
SeqPacket = libc::SOCK_SEQPACKET,
9495
/// Provides raw network protocol access.
96+
#[cfg(not(target_os = "redox"))]
9597
Raw = libc::SOCK_RAW,
9698
/// Provides a reliable datagram layer that does not
9799
/// guarantee ordering.
98-
#[cfg(not(any(target_os = "haiku")))]
100+
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
99101
Rdm = libc::SOCK_RDM,
100102
}
101103
// The TryFrom impl could've been derived using libc_enum!. But for
@@ -109,8 +111,9 @@ impl TryFrom<i32> for SockType {
109111
libc::SOCK_STREAM => Ok(Self::Stream),
110112
libc::SOCK_DGRAM => Ok(Self::Datagram),
111113
libc::SOCK_SEQPACKET => Ok(Self::SeqPacket),
114+
#[cfg(not(target_os = "redox"))]
112115
libc::SOCK_RAW => Ok(Self::Raw),
113-
#[cfg(not(any(target_os = "haiku")))]
116+
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
114117
libc::SOCK_RDM => Ok(Self::Rdm),
115118
_ => Err(Errno::EINVAL),
116119
}
@@ -239,7 +242,7 @@ libc_bitflags! {
239242
///
240243
/// For use with [`Timestamping`][sockopt::Timestamping].
241244
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
242-
pub struct TimestampingFlag: c_uint {
245+
pub struct TimestampingFlag: libc::c_uint {
243246
/// Report any software timestamps when available.
244247
SOF_TIMESTAMPING_SOFTWARE;
245248
/// Report hardware timestamps as generated by SOF_TIMESTAMPING_TX_HARDWARE when available.
@@ -456,7 +459,7 @@ cfg_if! {
456459
/// Returns a list group identifiers (the first one being the effective GID)
457460
pub fn groups(&self) -> &[libc::gid_t] {
458461
unsafe {
459-
slice::from_raw_parts(
462+
std::slice::from_raw_parts(
460463
self.0.cmcred_groups.as_ptr() as *const libc::gid_t,
461464
self.0.cmcred_ngroups as _
462465
)
@@ -549,6 +552,7 @@ impl Ipv6MembershipRequest {
549552
}
550553
}
551554

555+
#[cfg(not(target_os = "redox"))]
552556
feature! {
553557
#![feature = "uio"]
554558

@@ -578,18 +582,19 @@ feature! {
578582
macro_rules! cmsg_space {
579583
( $( $x:ty ),* ) => {
580584
{
581-
let mut space = 0;
582-
$(
583-
// CMSG_SPACE is always safe
584-
space += unsafe {
585-
$crate::sys::socket::CMSG_SPACE(::std::mem::size_of::<$x>() as $crate::sys::socket::c_uint)
586-
} as usize;
587-
)*
585+
let space = 0 $(+ $crate::sys::socket::cmsg_space::<$x>())*;
588586
Vec::<u8>::with_capacity(space)
589587
}
590588
}
591589
}
592590

591+
#[inline]
592+
#[doc(hidden)]
593+
pub fn cmsg_space<T>() -> usize {
594+
// SAFETY: CMSG_SPACE is always safe
595+
unsafe { libc::CMSG_SPACE(mem::size_of::<T>() as libc::c_uint) as usize }
596+
}
597+
593598
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
594599
/// Contains outcome of sending or receiving a message
595600
///
@@ -984,7 +989,7 @@ impl ControlMessageOwned {
984989
ControlMessageOwned::Ipv6OrigDstAddr(dl)
985990
},
986991
(_, _) => {
987-
let sl = slice::from_raw_parts(p, len);
992+
let sl = std::slice::from_raw_parts(p, len);
988993
let ucmsg = UnknownCmsg(*header, Vec::<u8>::from(sl));
989994
ControlMessageOwned::Unknown(ucmsg)
990995
}
@@ -2392,6 +2397,7 @@ pub fn shutdown(df: RawFd, how: Shutdown) -> Result<()> {
23922397

23932398
#[cfg(test)]
23942399
mod tests {
2400+
#[cfg(not(target_os = "redox"))]
23952401
#[test]
23962402
fn can_use_cmsg_space() {
23972403
let _ = cmsg_space!(u8);

src/sys/socket/sockopt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,13 @@ cfg_if! {
544544
sockopt_impl!(
545545
/// The maximum segment size for outgoing TCP packets.
546546
TcpMaxSeg, Both, libc::IPPROTO_TCP, libc::TCP_MAXSEG, u32);
547-
} else {
547+
} else if #[cfg(not(target_os = "redox"))] {
548548
sockopt_impl!(
549549
/// The maximum segment size for outgoing TCP packets.
550550
TcpMaxSeg, GetOnly, libc::IPPROTO_TCP, libc::TCP_MAXSEG, u32);
551551
}
552552
}
553-
#[cfg(not(any(target_os = "openbsd", target_os = "haiku")))]
553+
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "redox")))]
554554
#[cfg(feature = "net")]
555555
sockopt_impl!(
556556
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
@@ -572,7 +572,7 @@ sockopt_impl!(
572572
libc::TCP_REPAIR,
573573
u32
574574
);
575-
#[cfg(not(any(target_os = "openbsd", target_os = "haiku")))]
575+
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "redox")))]
576576
#[cfg(feature = "net")]
577577
sockopt_impl!(
578578
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
@@ -693,7 +693,7 @@ sockopt_impl!(
693693
libc::SO_TIMESTAMPING,
694694
super::TimestampingFlag
695695
);
696-
#[cfg(not(target_os = "haiku"))]
696+
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
697697
sockopt_impl!(
698698
/// Enable or disable the receiving of the `SO_TIMESTAMP` control message.
699699
ReceiveTimestamp,

0 commit comments

Comments
 (0)