-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_hooks.c
167 lines (133 loc) · 4.02 KB
/
time_hooks.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "common.h"
#include "config.h"
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/time.h>
#include "hooks.h"
time_t (*enhancer_real_time)(time_t *RetVal)=NULL;
int (*enhancer_real_gettimeofday)(struct timeval *restrict tv, void *restrict tz)=NULL;
int (*enhancer_real_settimeofday)(const struct timeval *tv, const struct timezone *tz)=NULL;
int (*enhancer_real_setitimer)(__itimer_which_t Type, const struct itimerval *new, struct itimerval *curr)=NULL;
time_t ChangeTime(time_t tval, const char *TimeMod)
{
time_t mod=0;
const char *ptr;
if (TimeMod)
{
ptr=TimeMod;
while (strvalid(ptr))
{
mod=strtol(ptr, (char **) &ptr, 10);
if (mod==0) break; //something went wrong
switch (*ptr)
{
case 'm':
mod *= 60;
break;
case 'h':
mod *= 3600;
break;
case 'd':
mod *= 3600 * 24;
break;
case 'w':
mod *= 3600 * 24 * 7;
break;
case 'y':
mod *= 3600 * 24 * 365;
break;
case 'Y':
mod *= 3600 * 24 * 365;
break;
}
tval+=mod;
ptr++;
}
}
return(tval);
}
time_t time(time_t *RetVal)
{
int Flags;
char *TimeMod=NULL;
time_t tval;
if (! enhancer_real_time) enhancer_get_real_functions();
Flags=enhancer_checkconfig_with_redirect(FUNC_TIME, "time", "", "", 0, 0, &TimeMod);
if (Flags & FLAG_DENY) return(-1);
tval=enhancer_real_time(NULL);
tval=ChangeTime(tval, TimeMod);
if (RetVal) *RetVal=tval;
return(tval);
}
time_t enhancer_gettime()
{
struct timeval tv;
enhancer_real_gettimeofday(&tv, NULL);
return(tv.tv_sec);
}
#ifndef GETTIMEOFDAY_NONE
#ifdef GETTIMEOFDAY_RTZRVOID
int gettimeofday(struct timeval *restrict tv, void *restrict tz)
#elif defined GETTIMEOFDAY_TZVOID
int gettimeofday(struct timeval *tv, void *tz)
#elif defined GETTIMEOFDAY_VOIDVOID
int gettimeofday(void *tv, void *tz)
#else
int gettimeofday(struct timeval *restrict tv, struct timezone *restrict tz)
#endif
{
int Flags;
char *TimeMod=NULL;
int result;
if (! enhancer_real_gettimeofday) enhancer_get_real_functions();
Flags=enhancer_checkconfig_with_redirect(FUNC_TIME, "time", "", "", 0, 0, &TimeMod);
if (Flags & FLAG_DENY)
{
destroy(TimeMod);
return(-1);
}
result=enhancer_real_gettimeofday(tv, tz);
tv->tv_sec=ChangeTime(tv->tv_sec, TimeMod);
destroy(TimeMod);
return(result);
}
#endif
int settimeofday(const struct timeval *itv, const struct timezone *tz)
{
int Flags;
char *TimeMod=NULL;
int result;
struct timeval tv;
tv.tv_sec=itv->tv_sec;
tv.tv_usec=itv->tv_usec;
if (! enhancer_real_settimeofday) enhancer_get_real_functions();
Flags=enhancer_checkconfig_with_redirect(FUNC_TIME, "time", "", "", 0, 0, &TimeMod);
if (Flags & FLAG_DENY)
{
destroy(TimeMod);
return(-1);
}
tv.tv_sec=ChangeTime(tv.tv_sec, TimeMod);
result=enhancer_real_settimeofday(&tv, tz);
destroy(TimeMod);
return(result);
}
int setitimer(__itimer_which_t Type, const struct itimerval * new, struct itimerval * curr)
{
struct itimerval tval;
tval.it_interval.tv_sec=0;
tval.it_interval.tv_usec=40000;
tval.it_value.tv_sec=0;
tval.it_value.tv_usec=40000;
if (! enhancer_real_setitimer) enhancer_get_real_functions();
return(enhancer_real_setitimer(Type, &tval, curr));
}
void enhancer_time_hooks()
{
if (! enhancer_real_time) enhancer_real_time = dlsym(RTLD_NEXT, "time");
if (! enhancer_real_setitimer) enhancer_real_setitimer = dlsym(RTLD_NEXT, "setitimer");
if (! enhancer_real_settimeofday) enhancer_real_settimeofday = dlsym(RTLD_NEXT, "settimeofday");
#ifndef GETTIMEOFDAY_NONE
if (! enhancer_real_gettimeofday) enhancer_real_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
#endif
}