From a8b7c6804a49c1d311d7634c5737ec36b8e23595 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 26 Jan 2022 22:45:53 -0500 Subject: [PATCH 1/2] Add clock_gettime implementation --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 3385b95..ccd64e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,8 @@ #![no_std] +use core::mem::MaybeUninit; +use core::ptr; + extern crate libc; /// Call this somewhere to force Rust to link this module. @@ -8,6 +11,11 @@ extern crate libc; /// See https://github.com/rust-lang/rust/issues/47384 pub fn init() {} +extern "C" { + // Not provided by libc: https://github.com/rust-lang/libc/issues/1995 + fn __errno() -> *mut libc::c_int; +} + #[no_mangle] extern "C" fn posix_memalign( memptr: *mut *mut libc::c_void, @@ -36,3 +44,26 @@ unsafe extern "C" fn realpath( resolved_path } + +#[no_mangle] +unsafe extern "C" fn clock_gettime( + clock_id: libc::clockid_t, + tp: *mut libc::timespec, +) -> libc::c_int { + let mut retval = -1; + match clock_id { + libc::CLOCK_REALTIME => { + let mut tv = MaybeUninit::uninit(); + + retval = libc::gettimeofday(tv.as_mut_ptr(), ptr::null_mut()); + if retval == 0 { + let tv = tv.assume_init(); + (*tp).tv_nsec = (tv.tv_usec * 1000).into(); + (*tp).tv_sec = tv.tv_sec; + } + } + _ => *__errno() = libc::EINVAL, + } + + retval +} From f9db3a9101ff9fcc680e66e20ea29496832bcdbd Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Wed, 26 Jan 2022 23:24:08 -0500 Subject: [PATCH 2/2] Update src/lib.rs Co-authored-by: Mark Drobnak --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ccd64e8..7783976 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,7 @@ unsafe extern "C" fn clock_gettime( retval = libc::gettimeofday(tv.as_mut_ptr(), ptr::null_mut()); if retval == 0 { let tv = tv.assume_init(); - (*tp).tv_nsec = (tv.tv_usec * 1000).into(); + (*tp).tv_nsec = tv.tv_usec * 1000; (*tp).tv_sec = tv.tv_sec; } }