Skip to content

Commit 1a31128

Browse files
committed
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: rust-random/getrandom#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`
2 parents 55cc85f + f25ae98 commit 1a31128

File tree

8 files changed

+77
-33
lines changed

8 files changed

+77
-33
lines changed

src/unix/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@ pub type uintptr_t = usize;
2323
pub type ssize_t = isize;
2424

2525
pub type pid_t = i32;
26+
pub type in_addr_t = u32;
27+
pub type in_port_t = u16;
28+
pub type sighandler_t = ::size_t;
29+
pub type cc_t = ::c_uchar;
30+
2631
cfg_if! {
27-
if #[cfg(target_os = "espidf")] {
32+
if #[cfg(any(target_os = "espidf", target_os = "horizon"))] {
2833
pub type uid_t = ::c_ushort;
2934
pub type gid_t = ::c_ushort;
3035
} else {
3136
pub type uid_t = u32;
3237
pub type gid_t = u32;
3338
}
3439
}
35-
pub type in_addr_t = u32;
36-
pub type in_port_t = u16;
37-
pub type sighandler_t = ::size_t;
38-
pub type cc_t = ::c_uchar;
3940

4041
#[cfg_attr(feature = "extra_traits", derive(Debug))]
4142
pub enum DIR {}

src/unix/newlib/aarch64/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ pub const MSG_DONTROUTE: ::c_int = 0;
5050
pub const MSG_WAITALL: ::c_int = 0;
5151
pub const MSG_MORE: ::c_int = 0;
5252
pub const MSG_NOSIGNAL: ::c_int = 0;
53+
54+
pub use crate::unix::newlib::generic::{sigset_t, stat};

src/unix/newlib/arm/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ pub const MSG_DONTROUTE: ::c_int = 0;
5252
pub const MSG_WAITALL: ::c_int = 0;
5353
pub const MSG_MORE: ::c_int = 0;
5454
pub const MSG_NOSIGNAL: ::c_int = 0;
55+
56+
pub use crate::unix::newlib::generic::{sigset_t, stat};

src/unix/newlib/espidf/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ extern "C" {
101101
#[link_name = "lwip_recvmsg"]
102102
pub fn recvmsg(s: ::c_int, msg: *mut ::msghdr, flags: ::c_int) -> ::ssize_t;
103103
}
104+
105+
pub use crate::unix::newlib::generic::{sigset_t, stat};

src/unix/newlib/generic.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Common types used by most newlib platforms
2+
3+
s! {
4+
pub struct sigset_t {
5+
__val: [::c_ulong; 16],
6+
}
7+
8+
pub struct stat {
9+
pub st_dev: ::dev_t,
10+
pub st_ino: ::ino_t,
11+
pub st_mode: ::mode_t,
12+
pub st_nlink: ::nlink_t,
13+
pub st_uid: ::uid_t,
14+
pub st_gid: ::gid_t,
15+
pub st_rdev: ::dev_t,
16+
pub st_size: ::off_t,
17+
pub st_atime: ::time_t,
18+
pub st_spare1: ::c_long,
19+
pub st_mtime: ::time_t,
20+
pub st_spare2: ::c_long,
21+
pub st_ctime: ::time_t,
22+
pub st_spare3: ::c_long,
23+
pub st_blksize: ::blksize_t,
24+
pub st_blocks: ::blkcnt_t,
25+
pub st_spare4: [::c_long; 2usize],
26+
}
27+
}

src/unix/newlib/horizon/mod.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub type c_ulong = u32;
66

77
pub type wchar_t = ::c_uint;
88

9-
pub type in_port_t = ::c_ushort;
109
pub type u_register_t = ::c_uint;
1110
pub type u_char = ::c_uchar;
1211
pub type u_short = ::c_ushort;
@@ -19,8 +18,8 @@ pub type clock_t = c_ulong;
1918
pub type daddr_t = c_long;
2019
pub type caddr_t = *mut c_char;
2120
pub type sbintime_t = ::c_longlong;
21+
pub type sigset_t = ::c_ulong;
2222

23-
// External implementations are needed to use networking and threading.
2423
s! {
2524
pub struct sockaddr {
2625
pub sa_family: ::sa_family_t,
@@ -34,8 +33,9 @@ s! {
3433

3534
pub struct sockaddr_in {
3635
pub sin_family: ::sa_family_t,
37-
pub sin_port: in_port_t,
36+
pub sin_port: ::in_port_t,
3837
pub sin_addr: ::in_addr,
38+
pub sin_zero: [::c_uchar; 8],
3939
}
4040

4141
pub struct sockaddr_in6 {
@@ -55,6 +55,23 @@ s! {
5555
pub struct sched_param {
5656
pub sched_priority: ::c_int,
5757
}
58+
59+
pub struct stat {
60+
pub st_dev: ::dev_t,
61+
pub st_ino: ::ino_t,
62+
pub st_mode: ::mode_t,
63+
pub st_nlink: ::nlink_t,
64+
pub st_uid: ::uid_t,
65+
pub st_gid: ::gid_t,
66+
pub st_rdev: ::dev_t,
67+
pub st_size: ::off_t,
68+
pub st_atim: ::timespec,
69+
pub st_mtim: ::timespec,
70+
pub st_ctim: ::timespec,
71+
pub st_blksize: ::blksize_t,
72+
pub st_blocks: ::blkcnt_t,
73+
pub st_spare4: [::c_long; 2usize],
74+
}
5875
}
5976

6077
pub const SIGEV_NONE: ::c_int = 1;
@@ -155,6 +172,10 @@ pub const RTLD_DEFAULT: *mut ::c_void = 0 as *mut ::c_void;
155172
pub const SCHED_FIFO: ::c_int = 1;
156173
pub const SCHED_RR: ::c_int = 2;
157174

175+
// For getrandom()
176+
pub const GRND_NONBLOCK: ::c_uint = 0x1;
177+
pub const GRND_RANDOM: ::c_uint = 0x2;
178+
158179
// Horizon OS works doesn't or can't hold any of this information
159180
safe_f! {
160181
pub {const} fn WIFSTOPPED(_status: ::c_int) -> bool {
@@ -232,5 +253,7 @@ extern "C" {
232253

233254
pub fn pthread_getprocessorid_np() -> ::c_int;
234255

256+
pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
257+
235258
pub fn gethostid() -> ::c_long;
236259
}

src/unix/newlib/mod.rs

+10-25
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,6 @@ s! {
139139
pub tm_isdst: ::c_int,
140140
}
141141

142-
pub struct stat {
143-
pub st_dev: ::dev_t,
144-
pub st_ino: ::ino_t,
145-
pub st_mode: ::mode_t,
146-
pub st_nlink: ::nlink_t,
147-
pub st_uid: ::uid_t,
148-
pub st_gid: ::gid_t,
149-
pub st_rdev: dev_t,
150-
pub st_size: off_t,
151-
pub st_atime: time_t,
152-
pub st_spare1: ::c_long,
153-
pub st_mtime: time_t,
154-
pub st_spare2: ::c_long,
155-
pub st_ctime: time_t,
156-
pub st_spare3: ::c_long,
157-
pub st_blksize: blksize_t,
158-
pub st_blocks: blkcnt_t,
159-
pub st_spare4: [::c_long; 2usize],
160-
}
161-
162142
pub struct statvfs {
163143
pub f_bsize: ::c_ulong,
164144
pub f_frsize: ::c_ulong,
@@ -173,10 +153,6 @@ s! {
173153
pub f_namemax: ::c_ulong,
174154
}
175155

176-
pub struct sigset_t {
177-
__val: [::c_ulong; 16],
178-
}
179-
180156
pub struct sigaction {
181157
pub sa_handler: extern fn(arg1: ::c_int),
182158
pub sa_mask: sigset_t,
@@ -293,7 +269,14 @@ pub const __PTHREAD_RWLOCK_INT_FLAGS_SHARED: usize = 1;
293269
pub const PTHREAD_MUTEX_NORMAL: ::c_int = 0;
294270
pub const PTHREAD_MUTEX_RECURSIVE: ::c_int = 1;
295271
pub const PTHREAD_MUTEX_ERRORCHECK: ::c_int = 2;
296-
pub const FD_SETSIZE: usize = 1024;
272+
273+
cfg_if! {
274+
if #[cfg(target_os = "horizon")] {
275+
pub const FD_SETSIZE: usize = 64;
276+
} else {
277+
pub const FD_SETSIZE: usize = 1024;
278+
}
279+
}
297280
// intentionally not public, only used for fd_set
298281
const ULONG_SIZE: usize = 32;
299282

@@ -739,6 +722,8 @@ extern "C" {
739722
pub fn uname(buf: *mut ::utsname) -> ::c_int;
740723
}
741724

725+
mod generic;
726+
742727
cfg_if! {
743728
if #[cfg(target_os = "espidf")] {
744729
mod espidf;

src/unix/newlib/powerpc/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub type wchar_t = ::c_int;
55
pub type c_long = i32;
66
pub type c_ulong = u32;
77

8+
pub use crate::unix::newlib::generic::{sigset_t, stat};
9+
810
// the newlib shipped with devkitPPC does not support the following components:
911
// - sockaddr
1012
// - AF_INET6

0 commit comments

Comments
 (0)