-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpmwatch.cpp
280 lines (228 loc) · 7.47 KB
/
hpmwatch.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (C) 2014 Philippe Daouadi <p.daouadi@free.fr>
* Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <p@demauro.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: installwatch.c,v 0.6.3.2 2001/12/22 23:01:20 izto Exp $
*
* april-15-2001 - Modifications by Felipe Eduardo Sanchez Diaz Duran
* <izto@asic-linux.com.mx>
* Added backup() and make_path() functions.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <dlfcn.h>
#include <mutex>
#define BUFSIZE 4096
#define error(X) (X < 0 ? strerror(errno) : "success")
std::once_flag initialized;
static int(*true_creat)(const char *, mode_t);
static FILE *(*true_fopen)(const char *,const char*);
static int(*true_link)(const char *, const char *);
static int(*true_open)(const char *, int, ...);
static int(*true_rename)(const char *, const char *);
static int(*true_symlink)(const char *, const char *);
static int(*true_creat64)(const char *, mode_t);
static FILE *(*true_fopen64)(const char *,const char *);
static int(*true_open64)(const char *, int, ...);
static void addfile(const char *filename);
static int log_fd = -1;
int initialize_watch(void) {
void *libc_handle;
libc_handle = RTLD_NEXT;
true_creat = (int(*)(const char*, mode_t))dlsym(libc_handle, "creat");
true_fopen = (FILE*(*)(const char*, const char*))dlsym(libc_handle, "fopen");
true_link = (int(*)(const char*, const char*))dlsym(libc_handle, "link");
true_open = (int(*)(const char*, int, ...))dlsym(libc_handle, "open");
true_rename = (int(*)(const char*, const char*))dlsym(libc_handle, "rename");
true_symlink = (int(*)(const char*, const char*))dlsym(libc_handle, "symlink");
true_creat64 = (int(*)(const char*, __mode_t))dlsym(libc_handle, "creat64");
true_fopen64 = (FILE*(*)(const char*, const char*))dlsym(libc_handle, "fopen64");
true_open64 = (int(*)(const char*, int, ...))dlsym(libc_handle, "open64");
if (!true_creat ||
!true_fopen ||
!true_link ||
!true_open ||
!true_rename ||
!true_symlink ||
!true_creat64 ||
!true_fopen64 ||
!true_open64)
{
puts("Error: cannot load one of the symbols");
exit(-1);
}
char* logname = getenv("INSTALLWATCHFILE");
if (!logname)
{
puts("Error: INSTALLWATCHFILE not defined");
exit(-1);
}
log_fd = true_open(logname, O_WRONLY | O_CREAT | O_APPEND, 0644);
if (log_fd == -1)
{
perror("cannot open logfile");
exit(-1);
}
return 0;
}
static int forceinit = initialize_watch();
static void addfile(const char *filename) {
char buffer[BUFSIZE];
int count;
count = snprintf(buffer, BUFSIZE, "%s\n", filename);
if(count == -1) {
/* The buffer was not big enough */
strcpy(&(buffer[BUFSIZE - 5]), "...\n");
count = BUFSIZE - 1;
}
if(write(log_fd, buffer, count) != count)
perror("could not write in logs");
}
static void canonicalize(const char *path, char *resolved_path) {
if(!realpath(path, resolved_path) && (path[0] != '/')) {
/* The path could not be canonicalized, append it
* to the current working directory if it was not
* an absolute path */
getcwd(resolved_path, MAXPATHLEN - 2);
strcat(resolved_path, "/");
strncat(resolved_path, path, MAXPATHLEN - 1);
}
}
int creat(const char *pathname, mode_t mode) {
call_once(initialized, initialize_watch);
/* Is it a system call? */
int result;
char canonic[MAXPATHLEN];
canonicalize(pathname, canonic);
result = true_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
if (result != -1)
addfile(canonic);
return result;
}
FILE *fopen(const char *pathname, const char *mode) {
call_once(initialized, initialize_watch);
FILE *result;
char canonic[MAXPATHLEN];
canonicalize(pathname, canonic);
result = true_fopen(pathname,mode);
if ((mode[0] == 'w' ||
mode[0] == 'a' ||
(mode[0] != '\0' && mode[1]=='+')) &&
result)
addfile(canonic);
return result;
}
int link(const char *oldpath, const char *newpath) {
call_once(initialized, initialize_watch);
int result;
char old_canonic[MAXPATHLEN], new_canonic[MAXPATHLEN];
canonicalize(oldpath, old_canonic);
canonicalize(newpath, new_canonic);
result = true_link(oldpath, newpath);
if (result != -1)
addfile(new_canonic);
return result;
}
int open(const char *pathname, int flags, ...) {
call_once(initialized, initialize_watch);
/* Eventually, there is a third parameter: it's mode_t mode */
va_list ap;
mode_t mode;
int result;
char canonic[MAXPATHLEN];
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
canonicalize(pathname, canonic);
result = true_open(pathname, flags, mode);
if ((flags & (O_WRONLY | O_RDWR | O_CREAT | O_TRUNC)) && result != -1)
addfile(canonic);
return result;
}
int rename(const char *oldpath, const char *newpath) {
call_once(initialized, initialize_watch);
int result;
char old_canonic[MAXPATHLEN], new_canonic[MAXPATHLEN];
canonicalize(oldpath, old_canonic);
canonicalize(newpath, new_canonic);
result = true_rename(oldpath, newpath);
if (result != -1)
addfile(new_canonic);
return result;
}
int symlink(const char *oldpath, const char *newpath) {
call_once(initialized, initialize_watch);
int result;
char old_canonic[MAXPATHLEN], new_canonic[MAXPATHLEN];
canonicalize(oldpath, old_canonic);
canonicalize(newpath, new_canonic);
result = true_symlink(oldpath, newpath);
if (result != -1)
addfile(new_canonic);
return result;
}
int creat64(const char *pathname, mode_t mode) {
call_once(initialized, initialize_watch);
/* Is it a system call? */
int result;
char canonic[MAXPATHLEN];
canonicalize(pathname, canonic);
result = true_open64(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
if (result != -1)
addfile(canonic);
return result;
}
FILE *fopen64(const char *pathname, const char *mode) {
call_once(initialized, initialize_watch);
FILE *result;
char canonic[MAXPATHLEN];
canonicalize(pathname, canonic);
result = true_fopen(pathname,mode);
if ((mode[0] == 'w' ||
mode[0] == 'a' ||
(mode[0] != '\0' && mode[1]=='+')) &&
result)
addfile(canonic);
return result;
}
int open64(const char *pathname, int flags, ...) {
call_once(initialized, initialize_watch);
/* Eventually, there is a third parameter: it's mode_t mode */
va_list ap;
mode_t mode;
int result;
char canonic[MAXPATHLEN];
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
canonicalize(pathname, canonic);
result = true_open64(pathname, flags, mode);
if ((flags & (O_WRONLY | O_RDWR | O_CREAT | O_TRUNC)) && result != -1)
addfile(canonic);
return result;
}