-
Notifications
You must be signed in to change notification settings - Fork 0
/
statfs-faker.c
238 lines (191 loc) · 4.02 KB
/
statfs-faker.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#define _LARGEFILE64_SOURCE
#ifdef MAIN
#include <stdio.h>
#endif
#include <sys/vfs.h>
#include <linux/magic.h>
#include <stdlib.h>
#include <errno.h>
#include <link.h>
#include <string.h>
#include <pthread.h>
#ifdef __mips__
#define OFFSET(map, x) ((x) + ((map)->l_addr))
#else
#define OFFSET(map, x) (x)
#endif
int (*statfs_orig)(
const char *path,
struct statfs *buf
);
int (*fstatfs_orig)(
int fd,
struct statfs *buf
);
int (*statfs64_orig)(
const char *path,
struct statfs64 *buf
);
int (*fstatfs64_orig)(
int fd,
struct statfs64 *buf
);
static __fsword_t fake_f_type = -1;
static pthread_once_t statfs_init_once = PTHREAD_ONCE_INIT;
static void statfs_orig_init(void)
{
struct link_map *map = _r_debug.r_map;
if (!map) {
// FIXME
return;
}
for (; map; map = map->l_next) {
if (!strstr(map->l_name, "/libc.so")) {
continue;
}
ElfW(Dyn) *dyn;
ElfW(Sym) *symtab = NULL;
char const *strtab = NULL;
unsigned int symnum = 0;
for (dyn = map->l_ld; dyn->d_tag != DT_NULL; ++dyn) {
if (dyn->d_tag == DT_SYMTAB) {
symtab = (ElfW(Sym) *)OFFSET(map, dyn->d_un.d_ptr);
}
else if (dyn->d_tag == DT_STRTAB) {
strtab = (char const *)OFFSET(map, dyn->d_un.d_ptr);
}
else if (dyn->d_tag == DT_HASH) {
symnum = ((unsigned int *)OFFSET(map, dyn->d_un.d_ptr))[1];
}
}
if (!symtab || !strtab || !symnum) {
// FIXME
return;
}
for (; symnum; symtab++, symnum--) {
char const *name = strtab + symtab->st_name;
void *value = (void *)symtab->st_value + map->l_addr;
if (!strcmp(name, "statfs")) {
statfs_orig = value;
#ifdef MAIN
printf("statfs() found: %p\n", value);
#endif
}
else if (!strcmp(name, "fstatfs")) {
fstatfs_orig = value;
#ifdef MAIN
printf("fstatfs() found: %p\n", value);
#endif
}
else if (!strcmp(name, "statfs64")) {
statfs64_orig = value;
#ifdef MAIN
printf("statfs64() found: %p\n", value);
#endif
}
else if (!strcmp(name, "fstatfs64")) {
fstatfs64_orig = value;
#ifdef MAIN
printf("fstatfs64() found: %p\n", value);
#endif
}
}
}
char *fake_f_type_str = getenv("STATFS_FAKER_F_TYPE");
if (fake_f_type_str) {
long fake_f_type_rc;
char *endptr;
fake_f_type_rc = strtol(fake_f_type_str, &endptr, 0);
if (*endptr == '\0') {
fake_f_type = (__fsword_t)fake_f_type_rc;
}
}
}
static void statfs_faker(struct statfs *buf)
{
if (fake_f_type >= 0) {
buf->f_type = fake_f_type;
}
}
int statfs(const char *path, struct statfs *buf)
{
int rc;
(void)pthread_once(&statfs_init_once, statfs_orig_init);
if (!statfs_orig) {
errno = ENOSYS;
return -1;
}
rc = statfs_orig(path, buf);
if (rc != 0) {
return rc;
}
statfs_faker(buf);
return rc;
}
int fstatfs(int fd, struct statfs *buf)
{
int rc;
(void)pthread_once(&statfs_init_once, statfs_orig_init);
if (!fstatfs_orig) {
errno = ENOSYS;
return -1;
}
rc = fstatfs_orig(fd, buf);
if (rc != 0) {
return rc;
}
statfs_faker(buf);
return rc;
}
static void statfs64_faker(struct statfs64 *buf)
{
if (fake_f_type) {
buf->f_type = fake_f_type;
}
}
int statfs64(const char *path, struct statfs64 *buf)
{
int rc;
(void)pthread_once(&statfs_init_once, statfs_orig_init);
if (!statfs64_orig) {
errno = ENOSYS;
return -1;
}
rc = statfs64_orig(path, buf);
if (rc != 0) {
return rc;
}
statfs64_faker(buf);
return rc;
}
int fstatfs64(int fd, struct statfs64 *buf)
{
int rc;
(void)pthread_once(&statfs_init_once, statfs_orig_init);
if (!fstatfs64_orig) {
errno = ENOSYS;
return -1;
}
rc = fstatfs64_orig(fd, buf);
if (rc != 0) {
return rc;
}
statfs64_faker(buf);
return rc;
}
#ifdef MAIN
int main(int argc, const char **argv)
{
int rc;
const char *path;
struct statfs64 buf;
path = argc >= 2 ? argv[1] : "/";
rc = statfs(path, &buf);
if (rc != 0) {
return 1;
}
printf("path: %s\n", path);
printf("f_type: 0x%lX\n", (long)buf.f_type);
return rc;
}
#endif