-
Notifications
You must be signed in to change notification settings - Fork 3
/
envtrace-helper.c
217 lines (183 loc) · 5.76 KB
/
envtrace-helper.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <inttypes.h>
#include <unistd.h>
#include <dlfcn.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <string.h>
extern char * program_invocation_name;
static FILE * logfile = 0;
static const char * logfile_name = 0;
static char * (*real_getenv) ( const char *name ) = 0;
static int (*real_setenv) ( const char *name,const char *value,int overwrite ) = 0;
static int (*real_unsetenv) ( const char *name ) = 0;
static pid_t (*real_fork) ( ) = 0;
static int (*real_clone) ( int (*fn)(void *),void *child_stack,int flags,void *arg,... ) = 0;
char * getenv( const char *name )
{
if(!real_getenv) {
real_getenv = dlsym(RTLD_NEXT,"getenv");
if(!real_getenv) {
fprintf(stderr,"envtrace-helper: couldn't find original getenv()\n");
exit(1);
}
}
if(!logfile_name) {
logfile_name = real_getenv("ENVTRACE_LOGFILE");
if(!logfile_name) {
fprintf(stderr,"envtrace-helper: ENVTRACE_LOGFILE is not set.\n");
exit(1);
}
}
if(!logfile) {
logfile = fopen(logfile_name,"a");
if(!logfile) {
fprintf(stderr,"envtrace-helper: couldn't open %s for logging: %s\n",(char *)logfile,strerror(errno));
exit(1);
}
}
char *result = real_getenv(name);
pid_t pid = getpid();
pid_t ppid = getppid();
struct timeval timestamp;
gettimeofday(×tamp,NULL);
if(result) {
fprintf(logfile,"%ld: %ld %ld %s %s HIT %s\n",(long)timestamp.tv_sec,(long)ppid,(long)pid,program_invocation_name,name,result);
} else {
fprintf(logfile,"%ld: %ld %ld %s %s MISS\n",(long)timestamp.tv_sec,(long)ppid,(long)pid,program_invocation_name,name);
}
fflush(logfile);
return result;
}
int setenv(const char *name, const char *value, int overwrite)
{
if(!real_setenv) {
real_setenv = dlsym(RTLD_NEXT,"setenv");
if(!real_setenv) {
fprintf(stderr,"envtrace-helper: couldn't find original setenv()\n");
exit(1);
}
}
if(!logfile_name) {
logfile_name = getenv("ENVTRACE_LOGFILE");
if(!logfile_name) {
fprintf(stderr,"envtrace-helper: ENVTRACE_LOGFILE is not set.\n");
exit(1);
}
}
if(!logfile) {
logfile = fopen(logfile_name,"a");
if(!logfile) {
fprintf(stderr,"envtrace-helper: couldn't open %s for logging: %s\n",(char *)logfile,strerror(errno));
exit(1);
}
}
int result = real_setenv(name,value,overwrite);
pid_t pid = getpid();
pid_t ppid = getppid();
struct timeval timestamp;
gettimeofday(×tamp,NULL);
fprintf(logfile,"%ld: SETENV %ld %ld %s %s %s %d\n",(long)timestamp.tv_sec,(long)ppid,(long)pid,program_invocation_name,name,value,result);
fflush(logfile);
return result;
}
int unsetenv(const char *name)
{
if(!real_unsetenv) {
real_unsetenv = dlsym(RTLD_NEXT,"unsetenv");
if(!real_unsetenv) {
fprintf(stderr,"envtrace-helper: couldn't find original unsetenv()\n");
exit(1);
}
}
if(!logfile_name) {
logfile_name = getenv("ENVTRACE_LOGFILE");
if(!logfile_name) {
fprintf(stderr,"envtrace-helper: ENVTRACE_LOGFILE is not set.\n");
exit(1);
}
}
if(!logfile) {
logfile = fopen(logfile_name,"a");
if(!logfile) {
fprintf(stderr,"envtrace-helper: couldn't open %s for logging: %s\n",(char *)logfile,strerror(errno));
exit(1);
}
}
int result = real_unsetenv(name);
pid_t pid = getpid();
pid_t ppid = getppid();
struct timeval timestamp;
gettimeofday(×tamp,NULL);
fprintf(logfile,"%ld: UNSET %ld %ld %s %s %d\n",(long)timestamp.tv_sec,(long)ppid,(long)pid,program_invocation_name,name,result);
fflush(logfile);
return result;
}
pid_t fork()
{
if(!real_fork) {
real_fork = dlsym(RTLD_NEXT,"fork");
if(!real_fork) {
fprintf(stderr,"envtrace-helper: couldn't find original fork()\n");
exit(1);
}
}
if(!logfile_name) {
logfile_name = getenv("ENVTRACE_LOGFILE");
if(!logfile_name) {
fprintf(stderr,"envtrace-helper: ENVTRACE_LOGFILE is not set.\n");
exit(1);
}
}
if(!logfile) {
logfile = fopen(logfile_name,"a");
if(!logfile) {
fprintf(stderr,"envtrace-helper: couldn't open %s for logging: %s\n",(char *)logfile,strerror(errno));
exit(1);
}
}
pid_t result = real_fork();
pid_t pid = getpid();
pid_t ppid = getppid();
struct timeval timestamp;
gettimeofday(×tamp,NULL);
fprintf(logfile,"%ld: FORK %ld %ld %s %"PRId64"\n",(long)timestamp.tv_sec,(long)ppid,(long)pid,program_invocation_name,(int64_t) result);
fflush(logfile);
return result;
}
int clone( int (*fn)(void *),void *child_stack,int flags,void *arg,.../* pid_t *ptid, void *newtls, pid_t *ctid */ )
{
if(!real_clone) {
real_clone = dlsym(RTLD_NEXT,"clone");
if(!real_clone) {
fprintf(stderr,"envtrace-helper: couldn't find original clone()\n");
exit(1);
}
}
if(!logfile_name) {
logfile_name = getenv("ENVTRACE_LOGFILE");
if(!logfile_name) {
fprintf(stderr,"envtrace-helper: ENVTRACE_LOGFILE is not set.\n");
exit(1);
}
}
if(!logfile) {
logfile = fopen(logfile_name,"a");
if(!logfile) {
fprintf(stderr,"envtrace-helper: couldn't open %s for logging: %s\n",(char *)logfile,strerror(errno));
exit(1);
}
}
int result = real_clone(fn,child_stack,flags,arg);
pid_t pid = getpid();
pid_t ppid = getppid();
struct timeval timestamp;
gettimeofday(×tamp,NULL);
fprintf(logfile,"%ld: CLONE %ld %ld %s %d\n",(long)timestamp.tv_sec,(long)ppid,(long)pid,program_invocation_name,result);
fflush(logfile);
return result;
}