summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-03-14 10:08:37 +0000
committerbors <bors@rust-lang.org>2022-03-14 10:08:37 +0000
commit1a311288a8f7523bfe5f88f57c6e96724eb16842 (patch)
tree2a9bf44b52e17793599acf0ffbd8711c69ef3bf1
parent55cc85ff7f61e4f11b260ecb68b366818dc4bbe0 (diff)
parentf25ae982ecf014c85c87c078d4baa904a9e71106 (diff)
downloadrust-libc-1a311288a8f7523bfe5f88f57c6e96724eb16842.tar.gz
Auto merge of #2714 - AzureMarker:horizon-getrandom-and-fixes, r=Amanieu
Horizon (Nintendo 3DS) getrandom function and fixes This PR adds `getrandom`, conforming to the [Linux spec](https://man7.org/linux/man-pages/man2/getrandom.2.html), to the `horizon` OS (Nintendo 3DS). The 3DS doesn't have a full libc implementation, and its randomness API is pretty complicated: https://github.com/rust-random/getrandom/pull/248. For this reason (see the linked PR for more details), the randomness implementation is abstracted by using the Linux `getrandom` interface. This PR also fixes some types on the horizon platform. See the commits and diff. cc: `@ian-h-chamberlain` `@Meziu`
-rw-r--r--src/unix/mod.rs11
-rw-r--r--src/unix/newlib/aarch64/mod.rs2
-rw-r--r--src/unix/newlib/arm/mod.rs2
-rw-r--r--src/unix/newlib/espidf/mod.rs2
-rw-r--r--src/unix/newlib/generic.rs27
-rw-r--r--src/unix/newlib/horizon/mod.rs29
-rw-r--r--src/unix/newlib/mod.rs35
-rw-r--r--src/unix/newlib/powerpc/mod.rs2
8 files changed, 77 insertions, 33 deletions
diff --git a/src/unix/mod.rs b/src/unix/mod.rs
index b6d08f7dc4..3664607253 100644
--- a/src/unix/mod.rs
+++ b/src/unix/mod.rs
@@ -23,8 +23,13 @@ pub type uintptr_t = usize;
pub type ssize_t = isize;
pub type pid_t = i32;
+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 = "espidf")] {
+ if #[cfg(any(target_os = "espidf", target_os = "horizon"))] {
pub type uid_t = ::c_ushort;
pub type gid_t = ::c_ushort;
} else {
@@ -32,10 +37,6 @@ cfg_if! {
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_attr(feature = "extra_traits", derive(Debug))]
pub enum DIR {}
diff --git a/src/unix/newlib/aarch64/mod.rs b/src/unix/newlib/aarch64/mod.rs
index 71aa894922..d686b3692d 100644
--- a/src/unix/newlib/aarch64/mod.rs
+++ b/src/unix/newlib/aarch64/mod.rs
@@ -50,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 78bea27653..f644349cb9 100644
--- a/src/unix/newlib/arm/mod.rs
+++ b/src/unix/newlib/arm/mod.rs
@@ -52,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 d3e1059465..38c99c6b35 100644
--- a/src/unix/newlib/espidf/mod.rs
+++ b/src/unix/newlib/espidf/mod.rs
@@ -101,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/generic.rs b/src/unix/newlib/generic.rs
new file mode 100644
index 0000000000..db7797f17c
--- /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 5884501daa..7d3e6d02db 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;
@@ -19,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,
@@ -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 {
@@ -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;
@@ -155,6 +172,10 @@ pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void;
pub const SCHED_FIFO: ::c_int = 1;
pub const SCHED_RR: ::c_int = 2;
+// 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 {
@@ -232,5 +253,7 @@ extern "C" {
pub fn pthread_getprocessorid_np() -> ::c_int;
+ pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
+
pub fn gethostid() -> ::c_long;
}
diff --git a/src/unix/newlib/mod.rs b/src/unix/newlib/mod.rs
index 8f85afb41c..34a63a81a0 100644
--- a/src/unix/newlib/mod.rs
+++ b/src/unix/newlib/mod.rs
@@ -139,26 +139,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,
pub f_frsize: ::c_ulong,
@@ -173,10 +153,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,
@@ -293,7 +269,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;
@@ -739,6 +722,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 4289658cd6..6bed1ce27a 100644
--- a/src/unix/newlib/powerpc/mod.rs
+++ b/src/unix/newlib/powerpc/mod.rs
@@ -5,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