变速齿轮的实现
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>
#include <sys/time.h>

typedef int (*GETTIMEOFDAY)(struct timeval *tv, struct timezone *tz);
typedef int (*CLOCK_GETTIME)(clockid_t clk_id, struct timespec *tp);

GETTIMEOFDAY real_gettimeofday;
CLOCK_GETTIME real_clock_gettime;

float speedmultiplier;

int new_clock_gettime(clockid_t clk_id, struct timespec *tp)
{
    int r = -1;
    struct timespec currenttp;

    r = real_clock_gettime(clk_id, &currenttp);

    if (clk_id <= 9) {
        // Your hook code here
        // Modify the currenttp according to the speed multiplier

        if (tp) {
            *tp = currenttp;
        }
    } else {
        if (tp) {
            *tp = currenttp;
        }
    }

    return r;
}

int new_gettimeofday(struct timeval *tv, struct timezone *tz)
{
    int r;
    struct timeval currenttv;

    r = real_gettimeofday(&currenttv, tz);

    // Your hook code here
    // Modify the currenttv according to the speed multiplier

    if (tv) {
        *tv = currenttv;
    }

    return r;
}

int speedhack_initializeSpeed(float speed)
{
    // Load the real functions
    real_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
    real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");

    // Your initialization code here
    // Store the initial time and offset

    speedmultiplier = speed;

    return 1;
}

TAG:none