From 4253e25b7f31d207c69b9997f403b6e4e560e258 Mon Sep 17 00:00:00 2001
From: Tamir Duberstein <tamird@gmail.com>
Date: Thu, 20 Mar 2025 11:45:08 -0400
Subject: [PATCH] musl: enable `getrandom` on all musl platforms

The existing bindings were added in #1399 and limited to targets where
rustc used musl version >= 1.1.20 which was not all musl targets at that
time. Since https://github.com/rust-lang/rust/pull/107129 all musl
targets use musl 1.2.3. Hence, move the binding to the module root so it
is available for all musl targets.

Upstream-Status: Backport [https://github.com/rust-lang/libc/pull/4346]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 src/unix/linux_like/linux/musl/b32/arm/mod.rs | 4 ----
 src/unix/linux_like/linux/musl/b32/powerpc.rs | 4 ----
 src/unix/linux_like/linux/musl/b32/x86/mod.rs | 4 ----
 src/unix/linux_like/linux/musl/b64/mod.rs     | 4 ----
 src/unix/linux_like/linux/musl/mod.rs         | 2 ++
 5 files changed, 2 insertions(+), 16 deletions(-)

--- a/src/unix/linux_like/linux/musl/b32/arm/mod.rs
+++ b/src/unix/linux_like/linux/musl/b32/arm/mod.rs
@@ -841,10 +841,6 @@ pub const SYS_process_mrelease: ::c_long
 pub const SYS_futex_waitv: ::c_long = 449;
 pub const SYS_set_mempolicy_home_node: ::c_long = 450;
 
-extern "C" {
-    pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
-}
-
 cfg_if! {
     if #[cfg(libc_align)] {
         mod align;
--- a/src/unix/linux_like/linux/musl/b32/powerpc.rs
+++ b/src/unix/linux_like/linux/musl/b32/powerpc.rs
@@ -796,7 +796,3 @@ pub const SYS_memfd_secret: ::c_long = 4
 pub const SYS_process_mrelease: ::c_long = 448;
 pub const SYS_futex_waitv: ::c_long = 449;
 pub const SYS_set_mempolicy_home_node: ::c_long = 450;
-
-extern "C" {
-    pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
-}
--- a/src/unix/linux_like/linux/musl/b32/x86/mod.rs
+++ b/src/unix/linux_like/linux/musl/b32/x86/mod.rs
@@ -956,10 +956,6 @@ pub const EFL: ::c_int = 14;
 pub const UESP: ::c_int = 15;
 pub const SS: ::c_int = 16;
 
-extern "C" {
-    pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
-}
-
 cfg_if! {
     if #[cfg(libc_align)] {
         mod align;
--- a/src/unix/linux_like/linux/musl/b64/mod.rs
+++ b/src/unix/linux_like/linux/musl/b64/mod.rs
@@ -134,10 +134,6 @@ pub const __SIZEOF_PTHREAD_RWLOCK_T: usi
 pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 40;
 pub const __SIZEOF_PTHREAD_BARRIER_T: usize = 32;
 
-extern "C" {
-    pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
-}
-
 cfg_if! {
     if #[cfg(target_arch = "aarch64")] {
         mod aarch64;
--- a/src/unix/linux_like/linux/musl/mod.rs
+++ b/src/unix/linux_like/linux/musl/mod.rs
@@ -885,6 +885,8 @@ extern "C" {
 
     pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
     pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
+    // Addded in `musl` 1.1.20
+    pub fn getrandom(buf: *mut ::c_void, buflen: ::size_t, flags: ::c_uint) -> ::ssize_t;
 }
 
 // Alias <foo> to <foo>64 to mimic glibc's LFS64 support
