From a3aa4423e70fa4cedd15eb2cd1aba525128987f4 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 14 Feb 2023 18:15:45 +0100 Subject: [PATCH 1/4] Document init function and add psInit to getrandom --- src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1cf099f..83a4172 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,6 @@ #![no_std] -/// Call this somewhere to force Rust to link this module. -/// The call doesn't need to execute, just exist. -/// -/// See +/// Reference this function somewhere (eg. ´use linker_fix_3sd::init´ in the main crate) to import all missing implementations. pub fn init() {} extern "C" { @@ -54,6 +51,7 @@ pub unsafe extern "C" fn getrandom( }; buflen = buflen.min(maxlen); + ctru_sys::psInit(); let ret = ctru_sys::PS_GenerateRandomBytes(buf, buflen); // avoid conflicting a real POSIX errno by using a value < 0 From c7753eb67ae8428f6a00661463f591b3e96ea1e9 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 14 Feb 2023 18:21:07 +0100 Subject: [PATCH 2/4] Fix lints --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 83a4172..c74dbe3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,7 @@ pub unsafe extern "C" fn getrandom( }; buflen = buflen.min(maxlen); - ctru_sys::psInit(); + let _ = ctru_sys::psInit(); let ret = ctru_sys::PS_GenerateRandomBytes(buf, buflen); // avoid conflicting a real POSIX errno by using a value < 0 From ef86bbf531e6735156da7a7b0b88e3a1074e3cd3 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Wed, 15 Feb 2023 13:57:52 +0100 Subject: [PATCH 3/4] Add error handling --- src/lib.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c74dbe3..bea1f81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,30 +51,44 @@ pub unsafe extern "C" fn getrandom( }; buflen = buflen.min(maxlen); - let _ = ctru_sys::psInit(); - let ret = ctru_sys::PS_GenerateRandomBytes(buf, buflen); - - // avoid conflicting a real POSIX errno by using a value < 0 - // should we define this in ctru-sys somewhere or something? + // Avoid conflicting a real POSIX errno by using a value < 0 + // Should we define this in ctru-sys somewhere or something? const ECTRU: libc::c_int = -1; + let ret = ctru_sys::psInit(); + + // Error handling code for psInit + if ctru_sys::R_FAILED(ret) { + // Best-effort attempt at translating return codes + *__errno() = match ctru_sys::R_SUMMARY(ret) as libc::c_uint { + // The service handle is full (would block to await availability) + ctru_sys::RS_WOULDBLOCK => libc::EAGAIN, + // The caller doesn't have the right to call the service + _ => ECTRU, + }; + return -1 + } + + let ret = ctru_sys::PS_GenerateRandomBytes(buf, buflen); + + // Error handling code for PS_GenerateRandomBytes if ctru_sys::R_SUCCEEDED(ret) { - // safe because above ensures buflen < isize::MAX + // Safe because above ensures buflen < isize::MAX buflen as libc::ssize_t } else { - // best-effort attempt at translating return codes + // Best-effort attempt at translating return codes *__errno() = match ctru_sys::R_SUMMARY(ret) as libc::c_uint { ctru_sys::RS_WOULDBLOCK => libc::EAGAIN, ctru_sys::RS_INVALIDARG | ctru_sys::RS_WRONGARG => { match ctru_sys::R_DESCRIPTION(ret) as libc::c_uint { - // most likely user error, forgot to initialize PS module + // The handle is incorrect (even though we just made it) ctru_sys::RD_INVALID_HANDLE => ECTRU, _ => libc::EINVAL, } } _ => ECTRU, }; - -1 + return -1 } } From 0b9bd5e00ffd8368e266854fca888c2eefa74b3c Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Wed, 15 Feb 2023 13:58:24 +0100 Subject: [PATCH 4/4] init function removal --- src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bea1f81..47b62d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,5 @@ #![no_std] -/// Reference this function somewhere (eg. ´use linker_fix_3sd::init´ in the main crate) to import all missing implementations. -pub fn init() {} - extern "C" { // Not provided by libc: https://github.com/rust-lang/libc/issues/1995 fn __errno() -> *mut libc::c_int;