Skip to content

Commit efa912a

Browse files
tamirdThomasdezeeuw
authored andcommitted
Add Fuchsia support and CI
In general, feature support is identical to Linux.
1 parent 1b77162 commit efa912a

File tree

5 files changed

+68
-50
lines changed

5 files changed

+68
-50
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
runs-on: ubuntu-latest
5050
strategy:
5151
matrix:
52-
target: [x86_64-unknown-redox]
52+
target: [x86_64-unknown-redox, x86_64-fuchsia]
5353
steps:
5454
- uses: actions/checkout@master
5555
- name: Install Rust

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl TcpKeepalive {
369369
feature = "all",
370370
any(
371371
target_os = "freebsd",
372+
target_os = "fuchsia",
372373
target_os = "linux",
373374
target_os = "netbsd",
374375
target_vendor = "apple",
@@ -390,6 +391,7 @@ impl TcpKeepalive {
390391
feature = "all",
391392
any(
392393
target_os = "freebsd",
394+
target_os = "fuchsia",
393395
target_os = "linux",
394396
target_os = "netbsd",
395397
target_vendor = "apple",

src/socket.rs

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ impl Socket {
189189
target_os = "android",
190190
target_os = "dragonfly",
191191
target_os = "freebsd",
192+
target_os = "fuchsia",
192193
target_os = "illumos",
193194
target_os = "linux",
194195
target_os = "netbsd",
@@ -201,6 +202,7 @@ impl Socket {
201202
target_os = "android",
202203
target_os = "dragonfly",
203204
target_os = "freebsd",
205+
target_os = "fuchsia",
204206
target_os = "illumos",
205207
target_os = "linux",
206208
target_os = "netbsd",
@@ -580,6 +582,7 @@ fn set_common_type(ty: Type) -> Type {
580582
target_os = "android",
581583
target_os = "dragonfly",
582584
target_os = "freebsd",
585+
target_os = "fuchsia",
583586
target_os = "illumos",
584587
target_os = "linux",
585588
target_os = "netbsd",
@@ -604,6 +607,7 @@ fn set_common_flags(socket: Socket) -> io::Result<Socket> {
604607
target_os = "android",
605608
target_os = "dragonfly",
606609
target_os = "freebsd",
610+
target_os = "fuchsia",
607611
target_os = "illumos",
608612
target_os = "linux",
609613
target_os = "netbsd",
@@ -1217,6 +1221,7 @@ impl Socket {
12171221
target_os = "android",
12181222
target_os = "dragonfly",
12191223
target_os = "freebsd",
1224+
target_os = "fuchsia",
12201225
target_os = "illumos",
12211226
target_os = "linux",
12221227
target_os = "netbsd",
@@ -1241,6 +1246,7 @@ impl Socket {
12411246
target_os = "android",
12421247
target_os = "dragonfly",
12431248
target_os = "freebsd",
1249+
target_os = "fuchsia",
12441250
target_os = "illumos",
12451251
target_os = "linux",
12461252
target_os = "netbsd",

src/sys/unix.rs

+48-44
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
use std::cmp::min;
10-
#[cfg(all(feature = "all", target_os = "linux"))]
10+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
1111
use std::ffi::{CStr, CString};
1212
#[cfg(not(target_os = "redox"))]
1313
use std::io::IoSlice;
@@ -90,6 +90,7 @@ pub(crate) use libc::{
9090
feature = "all",
9191
any(
9292
target_os = "freebsd",
93+
target_os = "fuchsia",
9394
target_os = "linux",
9495
target_os = "netbsd",
9596
target_vendor = "apple",
@@ -142,6 +143,7 @@ type IovLen = usize;
142143
all(target_os = "linux", target_env = "musl"),
143144
target_os = "dragonfly",
144145
target_os = "freebsd",
146+
target_os = "fuchsia",
145147
target_os = "illumos",
146148
target_os = "netbsd",
147149
target_os = "openbsd",
@@ -159,8 +161,8 @@ impl Domain {
159161
///
160162
/// # Notes
161163
///
162-
/// This function is only available on Linux.
163-
#[cfg(all(feature = "all", target_os = "linux"))]
164+
/// This function is only available on Fuchsia and Linux.
165+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
164166
pub const PACKET: Domain = Domain(libc::AF_PACKET);
165167
}
166168

@@ -169,7 +171,7 @@ impl_debug!(
169171
libc::AF_INET,
170172
libc::AF_INET6,
171173
libc::AF_UNIX,
172-
#[cfg(target_os = "linux")]
174+
#[cfg(any(target_os = "fuchsia", target_os = "linux"))]
173175
libc::AF_PACKET,
174176
libc::AF_UNSPEC, // = 0.
175177
);
@@ -180,14 +182,15 @@ impl Type {
180182
///
181183
/// # Notes
182184
///
183-
/// This function is only available on Android, DragonFlyBSD, FreeBSD,
184-
/// Linux, NetBSD and OpenBSD.
185+
/// This function is only available on Android, DragonFlyBSD, Fuchsia,
186+
/// FreeBSD, Linux, NetBSD and OpenBSD.
185187
#[cfg(all(
186188
feature = "all",
187189
any(
188190
target_os = "android",
189191
target_os = "dragonfly",
190192
target_os = "freebsd",
193+
target_os = "fuchsia",
191194
target_os = "illumos",
192195
target_os = "linux",
193196
target_os = "netbsd",
@@ -202,14 +205,15 @@ impl Type {
202205
///
203206
/// # Notes
204207
///
205-
/// This function is only available on Android, DragonFlyBSD, FreeBSD,
206-
/// Linux, NetBSD and OpenBSD.
208+
/// This function is only available on Android, DragonFlyBSD, Fuchsia,
209+
/// FreeBSD, Linux, NetBSD and OpenBSD.
207210
#[cfg(all(
208211
feature = "all",
209212
any(
210213
target_os = "android",
211214
target_os = "dragonfly",
212215
target_os = "freebsd",
216+
target_os = "fuchsia",
213217
target_os = "illumos",
214218
target_os = "linux",
215219
target_os = "netbsd",
@@ -224,6 +228,7 @@ impl Type {
224228
target_os = "android",
225229
target_os = "dragonfly",
226230
target_os = "freebsd",
231+
target_os = "fuchsia",
227232
target_os = "illumos",
228233
target_os = "linux",
229234
target_os = "netbsd",
@@ -248,6 +253,7 @@ impl_debug!(
248253
target_os = "android",
249254
target_os = "dragonfly",
250255
target_os = "freebsd",
256+
target_os = "fuchsia",
251257
target_os = "linux",
252258
target_os = "netbsd",
253259
target_os = "openbsd"
@@ -257,6 +263,7 @@ impl_debug!(
257263
target_os = "android",
258264
target_os = "dragonfly",
259265
target_os = "freebsd",
266+
target_os = "fuchsia",
260267
target_os = "linux",
261268
target_os = "netbsd",
262269
target_os = "openbsd"
@@ -519,15 +526,12 @@ fn recvmsg(
519526
} else {
520527
size_of::<libc::sockaddr_storage>() as libc::socklen_t
521528
};
522-
let mut msg = libc::msghdr {
523-
msg_name: msg_name.cast(),
524-
msg_namelen,
525-
msg_iov: bufs.as_mut_ptr().cast(),
526-
msg_iovlen: min(bufs.len(), IovLen::MAX as usize) as IovLen,
527-
msg_control: ptr::null_mut(),
528-
msg_controllen: 0,
529-
msg_flags: 0,
530-
};
529+
// libc::msghdr contains unexported padding fields on Fuchsia.
530+
let mut msg: libc::msghdr = unsafe { mem::zeroed() };
531+
msg.msg_name = msg_name.cast();
532+
msg.msg_namelen = msg_namelen;
533+
msg.msg_iov = bufs.as_mut_ptr().cast();
534+
msg.msg_iovlen = min(bufs.len(), IovLen::MAX as usize) as IovLen;
531535
syscall!(recvmsg(fd, &mut msg, flags))
532536
.map(|n| (n as usize, msg.msg_namelen, RecvFlags(msg.msg_flags)))
533537
}
@@ -578,19 +582,16 @@ fn sendmsg(
578582
bufs: &[IoSlice<'_>],
579583
flags: c_int,
580584
) -> io::Result<usize> {
581-
let mut msg = libc::msghdr {
582-
// Safety: we're creating a `*mut` pointer from a reference, which is UB
583-
// once actually used. However the OS should not write to it in the
584-
// `sendmsg` system call.
585-
msg_name: (msg_name as *mut sockaddr_storage).cast(),
586-
msg_namelen,
587-
// Safety: Same as above about `*const` -> `*mut`.
588-
msg_iov: bufs.as_ptr() as *mut _,
589-
msg_iovlen: min(bufs.len(), IovLen::MAX as usize) as IovLen,
590-
msg_control: ptr::null_mut(),
591-
msg_controllen: 0,
592-
msg_flags: 0,
593-
};
585+
// libc::msghdr contains unexported padding fields on Fuchsia.
586+
let mut msg: libc::msghdr = unsafe { mem::zeroed() };
587+
// Safety: we're creating a `*mut` pointer from a reference, which is UB
588+
// once actually used. However the OS should not write to it in the
589+
// `sendmsg` system call.
590+
msg.msg_name = (msg_name as *mut sockaddr_storage).cast();
591+
msg.msg_namelen = msg_namelen;
592+
// Safety: Same as above about `*const` -> `*mut`.
593+
msg.msg_iov = bufs.as_ptr() as *mut _;
594+
msg.msg_iovlen = min(bufs.len(), IovLen::MAX as usize) as IovLen;
594595
syscall!(sendmsg(fd, &mut msg, flags)).map(|n| n as usize)
595596
}
596597

@@ -651,6 +652,7 @@ pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Res
651652
target_os = "android",
652653
target_os = "dragonfly",
653654
target_os = "freebsd",
655+
target_os = "fuchsia",
654656
target_os = "illumos",
655657
target_os = "linux",
656658
target_os = "netbsd",
@@ -778,6 +780,7 @@ impl crate::Socket {
778780
target_os = "android",
779781
target_os = "dragonfly",
780782
target_os = "freebsd",
783+
target_os = "fuchsia",
781784
target_os = "illumos",
782785
target_os = "linux",
783786
target_os = "netbsd",
@@ -792,6 +795,7 @@ impl crate::Socket {
792795
target_os = "android",
793796
target_os = "dragonfly",
794797
target_os = "freebsd",
798+
target_os = "fuchsia",
795799
target_os = "illumos",
796800
target_os = "linux",
797801
target_os = "netbsd",
@@ -881,9 +885,9 @@ impl crate::Socket {
881885
/// This value gets the socket mark field for each packet sent through
882886
/// this socket.
883887
///
884-
/// This function is only available on Linux and requires the
885-
/// `CAP_NET_ADMIN` capability.
886-
#[cfg(all(feature = "all", target_os = "linux"))]
888+
/// This function is only available on Fuchsia and Linux. On Linux it
889+
/// requires the `CAP_NET_ADMIN` capability.
890+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
887891
pub fn mark(&self) -> io::Result<u32> {
888892
unsafe {
889893
getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_MARK).map(|mark| mark as u32)
@@ -896,9 +900,9 @@ impl crate::Socket {
896900
/// this socket. Changing the mark can be used for mark-based routing
897901
/// without netfilter or for packet filtering.
898902
///
899-
/// This function is only available on Linux and requires the
900-
/// `CAP_NET_ADMIN` capability.
901-
#[cfg(all(feature = "all", target_os = "linux"))]
903+
/// This function is only available on Fuchsia and Linux. On Linux it
904+
/// requires the `CAP_NET_ADMIN` capability.
905+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
902906
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
903907
unsafe { setsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_MARK, mark as c_int) }
904908
}
@@ -907,8 +911,8 @@ impl crate::Socket {
907911
///
908912
/// This value gets the socket binded device's interface name.
909913
///
910-
/// This function is only available on Linux.
911-
#[cfg(all(feature = "all", target_os = "linux"))]
914+
/// This function is only available on Fuchsia and Linux.
915+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
912916
pub fn device(&self) -> io::Result<Option<CString>> {
913917
// TODO: replace with `MaybeUninit::uninit_array` once stable.
914918
let mut buf: [MaybeUninit<u8>; libc::IFNAMSIZ] =
@@ -952,8 +956,8 @@ impl crate::Socket {
952956
///
953957
/// If `interface` is `None` or an empty string it removes the binding.
954958
///
955-
/// This function is only available on Linux.
956-
#[cfg(all(feature = "all", target_os = "linux"))]
959+
/// This function is only available on Fuchsia and Linux.
960+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
957961
pub fn bind_device(&self, interface: Option<&CStr>) -> io::Result<()> {
958962
let (value, len) = if let Some(interface) = interface {
959963
(interface.as_ptr(), interface.to_bytes_with_nul().len())
@@ -1014,10 +1018,10 @@ impl crate::Socket {
10141018
///
10151019
/// For more information about this option, see [`set_freebind`].
10161020
///
1017-
/// This function is only available on Linux.
1021+
/// This function is only available on Fuchsia and Linux.
10181022
///
10191023
/// [`set_freebind`]: Socket::set_freebind
1020-
#[cfg(all(feature = "all", target_os = "linux"))]
1024+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
10211025
pub fn freebind(&self) -> io::Result<bool> {
10221026
unsafe {
10231027
getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::IP_FREEBIND)
@@ -1033,8 +1037,8 @@ impl crate::Socket {
10331037
/// dynamic IP address to be up at the time that the application is trying
10341038
/// to bind to it.
10351039
///
1036-
/// This function is only available on Linux.
1037-
#[cfg(all(feature = "all", target_os = "linux"))]
1040+
/// This function is only available on Fuchsia and Linux.
1041+
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
10381042
pub fn set_freebind(&self, reuse: bool) -> io::Result<()> {
10391043
unsafe {
10401044
setsockopt(

0 commit comments

Comments
 (0)