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
|
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <cstring>
#include <iostream>
#include <utility>
#include <functional>
#include <unordered_set>
static std::unordered_set<std::string> tracked_files;
template <class Result, typename... Arguments>
std::function<Result(Arguments...)> get_real_function(
const std::string& symbol_name) {
Result (*real_function)(Arguments...) = nullptr;
const void* symbol_address = dlsym(RTLD_NEXT, symbol_name.c_str());
std::memcpy(&real_function, &symbol_address, sizeof(symbol_address));
return std::function<Result(Arguments...)>(real_function);
}
void exit(int status) {
get_real_function<void, int>("exit")(status);
}
std::string get_file_name(int fd) {
char proc_link[20];
char file_name[256];
snprintf(proc_link, sizeof(proc_link), "/proc/self/fd/%d", fd);
const ssize_t name_size = readlink(proc_link, file_name, sizeof(file_name));
if ( name_size > 0 ) {
file_name[name_size] = '\0';
return std::string(file_name);
} else {
return std::string();
}
}
bool is_regular_file(int fd) {
struct stat fd_stat;
fstat(fd, &fd_stat);
return S_ISREG(fd_stat.st_mode);
}
bool is_tracked_file(const std::string& file_name) {
return tracked_files.find(file_name) != tracked_files.end();
}
bool track_file(const std::string& file_name) {
return tracked_files.emplace(file_name).second;
}
ssize_t write(int fd, const void* buffer, size_t count) {
static auto real_write = get_real_function<ssize_t, int, const void*, size_t>("write");
if ( is_regular_file(fd) ) {
const std::string file_name{ get_file_name(fd) };
if ( !is_tracked_file(file_name) ) {
track_file(file_name);
std::cerr << "wrote to '" << file_name << "'" << std::endl;
}
}
return real_write(fd, buffer, count);
}
int rename(const char* old_path, const char* new_path) {
static auto real_rename = get_real_function<int, const char*, const char*>("rename");
std::cerr << "renamed '" << old_path << "' to '" << new_path << "'" << std::endl;
return real_rename(old_path, new_path);
}
int rmdir(const char* path) {
static auto real_rmdir = get_real_function<int, const char*>("rmdir");
std::cerr << "removed directory '" << path << "'" << std::endl;
return real_rmdir(path);
}
int unlink(const char* path) {
static auto real_unlink = get_real_function<int, const char*>("unlink");
std::cerr << "removed '" << path << "'" << std::endl;
return real_unlink(path);
}
int unlinkat(int dirfd, const char* path, int flags) {
static auto real_unlinkat = get_real_function<int, int, const char*, int>("unlinkat");
if ( dirfd == AT_FDCWD ) {
std::cerr << "removed '" << path << "'" << std::endl;
} else {
std::cerr << "removed '" << get_file_name(dirfd) << path << "'" << std::endl;
}
return real_unlinkat(dirfd, path, flags);
}
|