Skip to content

Instantly share code, notes, and snippets.

@Hi-Angel
Last active January 4, 2019 22:54
Show Gist options
  • Select an option

  • Save Hi-Angel/6e96e3bfd4ee28c046954debfdea6c8e to your computer and use it in GitHub Desktop.

Select an option

Save Hi-Angel/6e96e3bfd4ee28c046954debfdea6c8e to your computer and use it in GitHub Desktop.

Revisions

  1. Hi-Angel revised this gist Jan 4, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions clock_gettime_override.cpp
    Original file line number Diff line number Diff line change
    @@ -10,13 +10,11 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp) {
    = (clock_gettime_ptr) dlsym(RTLD_NEXT, "clock_gettime");
    int ret = real_clock_gettime(clk_id, tp);
    if (ret != -1) {
    // printf("orig sec %li, nsec %li", tp->tv_sec, tp->tv_nsec);
    const short AMOUNT_TO_SLOW = 4;
    const time_t NSEC_FRAC = 1000000000 / AMOUNT_TO_SLOW;
    const long nsec_part = NSEC_FRAC * (tp->tv_sec % AMOUNT_TO_SLOW);
    tp->tv_nsec = tp->tv_nsec / AMOUNT_TO_SLOW + nsec_part;
    tp->tv_sec = tp->tv_sec / AMOUNT_TO_SLOW;
    // printf("\tnow sec %li, nsec %li\n", tp->tv_sec, tp->tv_nsec);
    }
    return ret;
    }
  2. Hi-Angel revised this gist Jan 4, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion clock_gettime_override.rs
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,6 @@ pub extern "C" fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
    let f = libc::dlsym(libc::RTLD_NEXT, "clock_gettime\0".as_ptr() as *const i8);
    std::mem::transmute::<*const libc::c_void, ClockGettimeType>(f)
    };

    }
    let ret = real_clock_gettime(clk_id, tp);
    if ret != -1 {
  3. Hi-Angel created this gist Jan 4, 2019.
    22 changes: 22 additions & 0 deletions clock_gettime_override.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #include <dlfcn.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>

    extern "C"
    int clock_gettime(clockid_t clk_id, struct timespec *tp) {
    using clock_gettime_ptr = int (*)(clockid_t, struct timespec*);
    const static clock_gettime_ptr real_clock_gettime
    = (clock_gettime_ptr) dlsym(RTLD_NEXT, "clock_gettime");
    int ret = real_clock_gettime(clk_id, tp);
    if (ret != -1) {
    // printf("orig sec %li, nsec %li", tp->tv_sec, tp->tv_nsec);
    const short AMOUNT_TO_SLOW = 4;
    const time_t NSEC_FRAC = 1000000000 / AMOUNT_TO_SLOW;
    const long nsec_part = NSEC_FRAC * (tp->tv_sec % AMOUNT_TO_SLOW);
    tp->tv_nsec = tp->tv_nsec / AMOUNT_TO_SLOW + nsec_part;
    tp->tv_sec = tp->tv_sec / AMOUNT_TO_SLOW;
    // printf("\tnow sec %li, nsec %li\n", tp->tv_sec, tp->tv_nsec);
    }
    return ret;
    }
    28 changes: 28 additions & 0 deletions clock_gettime_override.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #[macro_use]
    extern crate lazy_static;
    extern crate libc;

    use libc::{clockid_t, timespec, c_int};

    #[no_mangle]
    pub extern "C" fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
    type ClockGettimeType = fn(clk_id: clockid_t, tp: *mut timespec) -> c_int;
    unsafe {
    lazy_static! {
    static ref real_clock_gettime: ClockGettimeType = unsafe {
    let f = libc::dlsym(libc::RTLD_NEXT, "clock_gettime\0".as_ptr() as *const i8);
    std::mem::transmute::<*const libc::c_void, ClockGettimeType>(f)
    };

    }
    let ret = real_clock_gettime(clk_id, tp);
    if ret != -1 {
    let amount_to_slow = 4;
    let nsec_frac = 1000000000 / amount_to_slow;
    let nsec_part = nsec_frac * ((*tp).tv_sec % amount_to_slow);
    (*tp).tv_nsec = (*tp).tv_nsec / amount_to_slow + nsec_part;
    (*tp).tv_sec = (*tp).tv_sec / amount_to_slow;
    }
    ret
    }
    }