From 6716cdd58ab1e189388b15f0a38bb2f5f440d5f5 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 30 Jan 2022 19:24:57 -0500 Subject: [PATCH 1/6] Add getrandom call on horizon OS (cherry picked from commit ab957c0cbe1e08519df47180dba3f38a5681a79d) --- src/unix/newlib/horizon/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index a154d72707b9c..210f6a6562eae 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -190,5 +190,7 @@ extern "C" { value: *mut ::c_void, ) -> ::c_int; + pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t; + pub fn gethostid() -> ::c_long; } From e99e36e8d177d47af57f10ccdbeacd99aa0ffd16 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Mon, 31 Jan 2022 23:04:28 -0500 Subject: [PATCH 2/6] Add constants for getrandom flags (cherry picked from commit 4c03853dab634dccef9e5dd8b8f2177ee8c09195) --- src/unix/newlib/horizon/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 210f6a6562eae..1d8ae53cdfc3c 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -147,6 +147,10 @@ pub const FIONBIO: ::c_ulong = 1; pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void; +// For getrandom() +pub const GRND_NONBLOCK: ::c_uint = 0x1; +pub const GRND_RANDOM: ::c_uint = 0x2; + // Horizon OS works doesn't or can't hold any of this information safe_f! { pub {const} fn WIFSTOPPED(_status: ::c_int) -> bool { From 4646be7a6bdc39fbf404ad3f1eb290f41237f60a Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sat, 12 Feb 2022 20:43:17 -0500 Subject: [PATCH 3/6] Fix uid_t and gid_t sizes on horizon (cherry picked from commit e84dbb7cb3a015c9f33db9b29019975c669113de) --- src/unix/mod.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 5ff2294e797c3..52508345c55ba 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -23,13 +23,21 @@ pub type uintptr_t = usize; pub type ssize_t = isize; pub type pid_t = i32; -pub type uid_t = u32; -pub type gid_t = u32; pub type in_addr_t = u32; pub type in_port_t = u16; pub type sighandler_t = ::size_t; pub type cc_t = ::c_uchar; +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub type uid_t = ::c_ushort; + pub type gid_t = ::c_ushort; + } else { + pub type uid_t = u32; + pub type gid_t = u32; + } +} + #[cfg_attr(feature = "extra_traits", derive(Debug))] pub enum DIR {} impl ::Copy for DIR {} From c8208666bfe219bf28d4e900054c04a3697a4f3b Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sun, 13 Feb 2022 14:40:05 -0500 Subject: [PATCH 4/6] Add fixes from libc-test results (cherry picked from commit 494fc865b0544851a9b18964abf2646628be3006) --- src/unix/newlib/horizon/mod.rs | 4 +- src/unix/newlib/mod.rs | 83 ++++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 1d8ae53cdfc3c..511866095519d 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -6,7 +6,6 @@ pub type c_ulong = u32; pub type wchar_t = ::c_uint; -pub type in_port_t = ::c_ushort; pub type u_register_t = ::c_uint; pub type u_char = ::c_uchar; pub type u_short = ::c_ushort; @@ -34,8 +33,9 @@ s! { pub struct sockaddr_in { pub sin_family: ::sa_family_t, - pub sin_port: in_port_t, + pub sin_port: ::in_port_t, pub sin_addr: ::in_addr, + pub sin_zero: [::c_uchar; 8], } pub struct sockaddr_in6 { diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index f1d738cb05b8e..1f3e82b800d5c 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -129,25 +129,6 @@ s! { pub tm_isdst: ::c_int, } - pub struct stat { - pub st_dev: ::dev_t, - pub st_ino: ::ino_t, - pub st_mode: ::mode_t, - pub st_nlink: ::nlink_t, - pub st_uid: ::uid_t, - pub st_gid: ::gid_t, - pub st_rdev: dev_t, - pub st_size: off_t, - pub st_atime: time_t, - pub st_spare1: ::c_long, - pub st_mtime: time_t, - pub st_spare2: ::c_long, - pub st_ctime: time_t, - pub st_spare3: ::c_long, - pub st_blksize: blksize_t, - pub st_blocks: blkcnt_t, - pub st_spare4: [::c_long; 2usize], - } pub struct statvfs { pub f_bsize: ::c_ulong, @@ -163,10 +144,6 @@ s! { pub f_namemax: ::c_ulong, } - pub struct sigset_t { - __val: [::c_ulong; 16], - } - pub struct sigaction { pub sa_handler: extern fn(arg1: ::c_int), pub sa_mask: sigset_t, @@ -242,6 +219,57 @@ s! { } } +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub type sigset_t = ::c_ulong; + + s! { + pub struct stat { + pub st_dev: ::dev_t, + pub st_ino: ::ino_t, + pub st_mode: ::mode_t, + pub st_nlink: ::nlink_t, + pub st_uid: ::uid_t, + pub st_gid: ::gid_t, + pub st_rdev: dev_t, + pub st_size: off_t, + pub st_atim: ::timespec, + pub st_mtim: ::timespec, + pub st_ctim: ::timespec, + pub st_blksize: blksize_t, + pub st_blocks: blkcnt_t, + pub st_spare4: [::c_long; 2usize], + } + } + } else { + s! { + pub struct sigset_t { + __val: [::c_ulong; 16], + } + + pub struct stat { + pub st_dev: ::dev_t, + pub st_ino: ::ino_t, + pub st_mode: ::mode_t, + pub st_nlink: ::nlink_t, + pub st_uid: ::uid_t, + pub st_gid: ::gid_t, + pub st_rdev: dev_t, + pub st_size: off_t, + pub st_atime: time_t, + pub st_spare1: ::c_long, + pub st_mtime: time_t, + pub st_spare2: ::c_long, + pub st_ctime: time_t, + pub st_spare3: ::c_long, + pub st_blksize: blksize_t, + pub st_blocks: blkcnt_t, + pub st_spare4: [::c_long; 2usize], + } + } + } +} + // unverified constants align_const! { pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t { @@ -283,7 +311,14 @@ pub const __PTHREAD_RWLOCK_INT_FLAGS_SHARED: usize = 1; pub const PTHREAD_MUTEX_NORMAL: ::c_int = 0; pub const PTHREAD_MUTEX_RECURSIVE: ::c_int = 1; pub const PTHREAD_MUTEX_ERRORCHECK: ::c_int = 2; -pub const FD_SETSIZE: usize = 1024; + +cfg_if! { + if #[cfg(target_os = "horizon")] { + pub const FD_SETSIZE: usize = 64; + } else { + pub const FD_SETSIZE: usize = 1024; + } +} // intentionally not public, only used for fd_set const ULONG_SIZE: usize = 32; From 0c92066e212219feef8472a8c66621e09eabd209 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Sun, 13 Mar 2022 17:39:24 -0700 Subject: [PATCH 5/6] Reorganize some newlib definitions into a "generic" module --- src/unix/newlib/aarch64/mod.rs | 2 ++ src/unix/newlib/arm/mod.rs | 2 ++ src/unix/newlib/espidf/mod.rs | 2 ++ src/unix/newlib/generic.rs | 27 +++++++++++++++++ src/unix/newlib/horizon/mod.rs | 19 +++++++++++- src/unix/newlib/mod.rs | 53 ++-------------------------------- src/unix/newlib/powerpc/mod.rs | 2 ++ 7 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 src/unix/newlib/generic.rs diff --git a/src/unix/newlib/aarch64/mod.rs b/src/unix/newlib/aarch64/mod.rs index 71aa894922bbe..6454756bf1c16 100644 --- a/src/unix/newlib/aarch64/mod.rs +++ b/src/unix/newlib/aarch64/mod.rs @@ -1,3 +1,5 @@ +pub use crate::unix::newlib::generic::{sigset_t, stat}; + pub type clock_t = ::c_long; pub type c_char = u8; pub type wchar_t = u32; diff --git a/src/unix/newlib/arm/mod.rs b/src/unix/newlib/arm/mod.rs index 78bea276533d5..835455357eb7d 100644 --- a/src/unix/newlib/arm/mod.rs +++ b/src/unix/newlib/arm/mod.rs @@ -1,3 +1,5 @@ +pub use crate::unix::newlib::generic::{sigset_t, stat}; + pub type clock_t = ::c_long; pub type c_char = u8; pub type wchar_t = u32; diff --git a/src/unix/newlib/espidf/mod.rs b/src/unix/newlib/espidf/mod.rs index d3e1059465f7f..7188571a27c76 100644 --- a/src/unix/newlib/espidf/mod.rs +++ b/src/unix/newlib/espidf/mod.rs @@ -1,3 +1,5 @@ +pub use crate::unix::newlib::generic::{sigset_t, stat}; + pub type clock_t = ::c_ulong; pub type c_char = i8; pub type wchar_t = u32; diff --git a/src/unix/newlib/generic.rs b/src/unix/newlib/generic.rs new file mode 100644 index 0000000000000..db7797f17c297 --- /dev/null +++ b/src/unix/newlib/generic.rs @@ -0,0 +1,27 @@ +//! Common types used by most newlib platforms + +s! { + pub struct sigset_t { + __val: [::c_ulong; 16], + } + + pub struct stat { + pub st_dev: ::dev_t, + pub st_ino: ::ino_t, + pub st_mode: ::mode_t, + pub st_nlink: ::nlink_t, + pub st_uid: ::uid_t, + pub st_gid: ::gid_t, + pub st_rdev: ::dev_t, + pub st_size: ::off_t, + pub st_atime: ::time_t, + pub st_spare1: ::c_long, + pub st_mtime: ::time_t, + pub st_spare2: ::c_long, + pub st_ctime: ::time_t, + pub st_spare3: ::c_long, + pub st_blksize: ::blksize_t, + pub st_blocks: ::blkcnt_t, + pub st_spare4: [::c_long; 2usize], + } +} diff --git a/src/unix/newlib/horizon/mod.rs b/src/unix/newlib/horizon/mod.rs index 84dd9c43edb40..7d3e6d02db8ef 100644 --- a/src/unix/newlib/horizon/mod.rs +++ b/src/unix/newlib/horizon/mod.rs @@ -18,8 +18,8 @@ pub type clock_t = c_ulong; pub type daddr_t = c_long; pub type caddr_t = *mut c_char; pub type sbintime_t = ::c_longlong; +pub type sigset_t = ::c_ulong; -// External implementations are needed to use networking and threading. s! { pub struct sockaddr { pub sa_family: ::sa_family_t, @@ -55,6 +55,23 @@ s! { pub struct sched_param { pub sched_priority: ::c_int, } + + pub struct stat { + pub st_dev: ::dev_t, + pub st_ino: ::ino_t, + pub st_mode: ::mode_t, + pub st_nlink: ::nlink_t, + pub st_uid: ::uid_t, + pub st_gid: ::gid_t, + pub st_rdev: ::dev_t, + pub st_size: ::off_t, + pub st_atim: ::timespec, + pub st_mtim: ::timespec, + pub st_ctim: ::timespec, + pub st_blksize: ::blksize_t, + pub st_blocks: ::blkcnt_t, + pub st_spare4: [::c_long; 2usize], + } } pub const SIGEV_NONE: ::c_int = 1; diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index f4473c543c335..149ade445180a 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -229,57 +229,6 @@ s! { } } -cfg_if! { - if #[cfg(target_os = "horizon")] { - pub type sigset_t = ::c_ulong; - - s! { - pub struct stat { - pub st_dev: ::dev_t, - pub st_ino: ::ino_t, - pub st_mode: ::mode_t, - pub st_nlink: ::nlink_t, - pub st_uid: ::uid_t, - pub st_gid: ::gid_t, - pub st_rdev: dev_t, - pub st_size: off_t, - pub st_atim: ::timespec, - pub st_mtim: ::timespec, - pub st_ctim: ::timespec, - pub st_blksize: blksize_t, - pub st_blocks: blkcnt_t, - pub st_spare4: [::c_long; 2usize], - } - } - } else { - s! { - pub struct sigset_t { - __val: [::c_ulong; 16], - } - - pub struct stat { - pub st_dev: ::dev_t, - pub st_ino: ::ino_t, - pub st_mode: ::mode_t, - pub st_nlink: ::nlink_t, - pub st_uid: ::uid_t, - pub st_gid: ::gid_t, - pub st_rdev: dev_t, - pub st_size: off_t, - pub st_atime: time_t, - pub st_spare1: ::c_long, - pub st_mtime: time_t, - pub st_spare2: ::c_long, - pub st_ctime: time_t, - pub st_spare3: ::c_long, - pub st_blksize: blksize_t, - pub st_blocks: blkcnt_t, - pub st_spare4: [::c_long; 2usize], - } - } - } -} - // unverified constants align_const! { pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t { @@ -774,6 +723,8 @@ extern "C" { pub fn uname(buf: *mut ::utsname) -> ::c_int; } +mod generic; + cfg_if! { if #[cfg(target_os = "espidf")] { mod espidf; diff --git a/src/unix/newlib/powerpc/mod.rs b/src/unix/newlib/powerpc/mod.rs index 4289658cd6623..d046d202bd020 100644 --- a/src/unix/newlib/powerpc/mod.rs +++ b/src/unix/newlib/powerpc/mod.rs @@ -1,3 +1,5 @@ +pub use crate::unix::newlib::generic::{sigset_t, stat}; + pub type clock_t = ::c_ulong; pub type c_char = u8; pub type wchar_t = ::c_int; From f25ae982ecf014c85c87c078d4baa904a9e71106 Mon Sep 17 00:00:00 2001 From: Mark Drobnak Date: Sun, 13 Mar 2022 17:57:12 -0700 Subject: [PATCH 6/6] Fix style issues --- src/unix/newlib/aarch64/mod.rs | 4 ++-- src/unix/newlib/arm/mod.rs | 4 ++-- src/unix/newlib/espidf/mod.rs | 4 ++-- src/unix/newlib/mod.rs | 1 - src/unix/newlib/powerpc/mod.rs | 4 ++-- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/unix/newlib/aarch64/mod.rs b/src/unix/newlib/aarch64/mod.rs index 6454756bf1c16..d686b3692d86d 100644 --- a/src/unix/newlib/aarch64/mod.rs +++ b/src/unix/newlib/aarch64/mod.rs @@ -1,5 +1,3 @@ -pub use crate::unix::newlib::generic::{sigset_t, stat}; - pub type clock_t = ::c_long; pub type c_char = u8; pub type wchar_t = u32; @@ -52,3 +50,5 @@ pub const MSG_DONTROUTE: ::c_int = 0; pub const MSG_WAITALL: ::c_int = 0; pub const MSG_MORE: ::c_int = 0; pub const MSG_NOSIGNAL: ::c_int = 0; + +pub use crate::unix::newlib::generic::{sigset_t, stat}; diff --git a/src/unix/newlib/arm/mod.rs b/src/unix/newlib/arm/mod.rs index 835455357eb7d..f644349cb997f 100644 --- a/src/unix/newlib/arm/mod.rs +++ b/src/unix/newlib/arm/mod.rs @@ -1,5 +1,3 @@ -pub use crate::unix::newlib::generic::{sigset_t, stat}; - pub type clock_t = ::c_long; pub type c_char = u8; pub type wchar_t = u32; @@ -54,3 +52,5 @@ pub const MSG_DONTROUTE: ::c_int = 0; pub const MSG_WAITALL: ::c_int = 0; pub const MSG_MORE: ::c_int = 0; pub const MSG_NOSIGNAL: ::c_int = 0; + +pub use crate::unix::newlib::generic::{sigset_t, stat}; diff --git a/src/unix/newlib/espidf/mod.rs b/src/unix/newlib/espidf/mod.rs index 7188571a27c76..38c99c6b35373 100644 --- a/src/unix/newlib/espidf/mod.rs +++ b/src/unix/newlib/espidf/mod.rs @@ -1,5 +1,3 @@ -pub use crate::unix::newlib::generic::{sigset_t, stat}; - pub type clock_t = ::c_ulong; pub type c_char = i8; pub type wchar_t = u32; @@ -103,3 +101,5 @@ extern "C" { #[link_name = "lwip_recvmsg"] pub fn recvmsg(s: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t; } + +pub use crate::unix::newlib::generic::{sigset_t, stat}; diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs index 149ade445180a..34a63a81a03f7 100644 --- a/src/unix/newlib/mod.rs +++ b/src/unix/newlib/mod.rs @@ -139,7 +139,6 @@ s! { pub tm_isdst: ::c_int, } - pub struct statvfs { pub f_bsize: ::c_ulong, pub f_frsize: ::c_ulong, diff --git a/src/unix/newlib/powerpc/mod.rs b/src/unix/newlib/powerpc/mod.rs index d046d202bd020..6bed1ce27acbf 100644 --- a/src/unix/newlib/powerpc/mod.rs +++ b/src/unix/newlib/powerpc/mod.rs @@ -1,5 +1,3 @@ -pub use crate::unix::newlib::generic::{sigset_t, stat}; - pub type clock_t = ::c_ulong; pub type c_char = u8; pub type wchar_t = ::c_int; @@ -7,6 +5,8 @@ pub type wchar_t = ::c_int; pub type c_long = i32; pub type c_ulong = u32; +pub use crate::unix::newlib::generic::{sigset_t, stat}; + // the newlib shipped with devkitPPC does not support the following components: // - sockaddr // - AF_INET6