Codebase list datefudge / debian/1.14 datefudge.c
debian/1.14

Tree @debian/1.14 (Download .tar.gz)

datefudge.c @debian/1.14raw · history · blame

/* vim:ts=4:sts=4:sw=4:et:cindent
 * datefudge.c: Twist system date back N seconds
 *
 * Copyright (C) 2001-2003, Matthias Urlichs <smurf@noris.de>
 *
 * $Id: datefudge.c 10 2008-05-10 06:19:48Z robert $
 */
#define _GNU_SOURCE

#include <sys/file.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <assert.h>
#include <features.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

int fudge = 0;

static void init_fudge (void) {
    const char *fud;

    if(fudge)return;

    fud = getenv("DATEFUDGE");
    if(fud == NULL) return;
    fudge = atoi(fud);
}

/*
 * This won't work...
 *
text_set_element (__libc_subinit, init_logger);
 */

time_t time(time_t *x) {
    static time_t (*libc_time)(time_t *) = NULL;
    time_t res;

    init_fudge();
    if(!libc_time)
        libc_time = (typeof(libc_time))dlsym (RTLD_NEXT, __func__);
    res = (*libc_time)(x);
    if(x) *x -= fudge;
    res -= fudge;
    return res;
}

int __gettimeofday(struct timeval *x, struct timezone *y) {
    static int (*libc_gettimeofday)(struct timeval *, struct timezone *) = NULL;
    int res;

    init_fudge();
    if(!libc_gettimeofday)
        libc_gettimeofday = (typeof(libc_gettimeofday))dlsym (RTLD_NEXT, __func__);
    res = (*libc_gettimeofday)(x,y);
    if(res) return res;
    x->tv_sec -= fudge;
    return 0;
}

int gettimeofday(struct timeval *x, struct timezone *y) { 
    return __gettimeofday(x,y); 
}


int clock_gettime(clockid_t x, struct timespec *y) {
    static int (*libc_clock_gettime)(clockid_t, struct timespec*);
    int res;

    init_fudge();
    if (!libc_clock_gettime)
        libc_clock_gettime =  (typeof(libc_clock_gettime))dlsym (RTLD_NEXT, __func__);
    res = (*libc_clock_gettime)(x,y);
    if (res || CLOCK_REALTIME != x) return res;
    y->tv_sec -= fudge;
    return 0;
}