~ubuntu-branches/ubuntu/saucy/datefudge/saucy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* 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>
 * Copyright (C) 2008-2011, Robert Luberda <robert@debian.org>
 *
 */
#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>

static int fudge = 0;
static int dostatic = 0;

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

    if(fudge)return;

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

static void set_fudge(time_t *seconds)
{
    if (!seconds)
        return;

    init_fudge();

    if (dostatic)
        *seconds = fudge;
    else
        *seconds -= fudge;
}

/*
 * 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;

    if(!libc_time)
        libc_time = (typeof(libc_time))dlsym (RTLD_NEXT, __func__);
    res = (*libc_time)(x);
    set_fudge(x);
    set_fudge(&res);
    return res;
}

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

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

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

#ifndef __GNU__

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

    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;
    set_fudge(&y->tv_sec);
    return 0;
}

#endif