Newer
Older
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
/**
* All intercepted functions are defined here
*/
#include <preload/preload.hpp>
#include <preload/rpc/ld_rpc_metadentry.hpp>
#include <preload/passthrough.hpp>
#include <preload/adafs_functions.hpp>
using namespace std;
OpenFileMap file_map{};
int open(const char* path, int flags, ...) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
mode_t mode = 0;
if (flags & O_CREAT) {
va_list vl;
va_start(vl, flags);
mode = static_cast<mode_t>(va_arg(vl, int));
va_end(vl);
}
if (ld_is_env_initialized() && is_fs_path(path)) {
return adafs_open(path, mode, flags);
}
return (reinterpret_cast<decltype(&open)>(libc_open))(path, flags, mode);
}
int open64(__const char* path, int flags, ...) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
mode_t mode = 0;
if (flags & O_CREAT) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
return open(path, flags | O_LARGEFILE, mode);
}
//// TODO This function somehow always blocks forever if one puts anything between the paththru...
//FILE* fopen(const char* path, const char* mode) {
//// init_passthrough_if_needed();
//// DAEMON_DEBUG(debug_fd, "fopen called with path %s\n", path);
// return (reinterpret_cast<decltype(&fopen)>(libc_fopen))(path, mode);
//}
//// TODO This function somehow always blocks forever if one puts anything between the paththru...
//FILE* fopen64(const char* path, const char* mode) {
//// init_passthrough_if_needed();
//// DAEMON_DEBUG(debug_fd, "fopen64 called with path %s\n", path);
// return (reinterpret_cast<decltype(&fopen)>(libc_fopen))(path, mode);
//}
#undef creat
int creat(const char* path, mode_t mode) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {} with mode {}", __func__, path, mode);
return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
}
#undef creat64
int creat64(const char* path, mode_t mode) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {} with mode {}", __func__, path, mode);
return open(path, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, mode);
}
int mkdir(const char* path, mode_t mode) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {} with mode {}", __func__, path, mode);
if (ld_is_env_initialized() && is_fs_path(path)) {
// XXX Possible don't use open here but a separate call to handle RPCs directly (unsure about O_DIRECTORY flag)
return open(path, O_CREAT | O_DIRECTORY | O_WRONLY, mode);
}
return (reinterpret_cast<decltype(&mkdir)>(libc_mkdir))(path, mode);
}
int mkdirat(int dirfd, const char* path, mode_t mode) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {} with mode {} with dirfd {}", __func__, path, mode, dirfd);
if (ld_is_env_initialized() && is_fs_path(path)) {
// not implemented
ld_logger->trace("{}() not implemented.", __func__);
return -1;
}
return (reinterpret_cast<decltype(&mkdirat)>(libc_mkdirat))(dirfd, path, mode);
}
int unlink(const char* path) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
if (ld_is_env_initialized() && is_fs_path(path)) {
return rpc_send_unlink(path);
}
return (reinterpret_cast<decltype(&unlink)>(libc_unlink))(path);
}
int rmdir(const char* path) {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
if (ld_is_env_initialized() && is_fs_path(path)) {
// XXX Possible need another call to specifically handle remove dirs. For now handle them the same as files
return rpc_send_unlink(path);
}
return (reinterpret_cast<decltype(&rmdir)>(libc_rmdir))(path);
}
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
int close(int fd) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
// Currently no call to the daemon is required
file_map.remove(fd);
return 0;
}
return (reinterpret_cast<decltype(&close)>(libc_close))(fd);
}
int __close(int fd) {
return close(fd);
}
int stat(const char* path, struct stat* buf) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
if (ld_is_env_initialized() && is_fs_path(path)) {
return adafs_stat(path, buf);
}
return (reinterpret_cast<decltype(&stat)>(libc_stat))(path, buf);
}
int fstat(int fd, struct stat* buf) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with fd {}", __func__, fd);
if (ld_is_env_initialized() && file_map.exist(fd)) {
auto path = file_map.get(fd)->path();
return adafs_stat(path, buf);
}
return (reinterpret_cast<decltype(&fstat)>(libc_fstat))(fd, buf);
}
int __xstat(int ver, const char* path, struct stat* buf) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
if (ld_is_env_initialized() && is_fs_path(path)) {
return adafs_stat(path, buf);
}
return (reinterpret_cast<decltype(&__xstat)>(libc___xstat))(ver, path, buf);
}
int __xstat64(int ver, const char* path, struct stat64* buf) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with path {}", __func__, path);
if (ld_is_env_initialized() && is_fs_path(path)) {
return adafs_stat64(path, buf);
// // Not implemented
// return -1;
}
return (reinterpret_cast<decltype(&__xstat64)>(libc___xstat64))(ver, path, buf);
}
int __fxstat(int ver, int fd, struct stat* buf) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with fd {}", __func__, fd);
if (ld_is_env_initialized() && file_map.exist(fd)) {
auto path = file_map.get(fd)->path();
return adafs_stat(path, buf);
}
return (reinterpret_cast<decltype(&__fxstat)>(libc___fxstat))(ver, fd, buf);
}
int __fxstat64(int ver, int fd, struct stat64* buf) __THROW {
init_passthrough_if_needed();
ld_logger->trace("{}() called with fd {}", __func__, fd);
if (ld_is_env_initialized() && file_map.exist(fd)) {
auto path = file_map.get(fd)->path();
return adafs_stat64(path, buf);
}
return (reinterpret_cast<decltype(&__fxstat64)>(libc___fxstat64))(ver, fd, buf);
}
extern int __lxstat(int ver, const char* path, struct stat* buf) __THROW {
init_passthrough_if_needed();
if (ld_is_env_initialized() && is_fs_path(path)) {
// Not implemented
return -1;
}
return (reinterpret_cast<decltype(&__lxstat)>(libc___lxstat))(ver, path, buf);
}
extern int __lxstat64(int ver, const char* path, struct stat64* buf) __THROW {
init_passthrough_if_needed();
if (ld_is_env_initialized() && is_fs_path(path)) {
// Not implemented
return -1;
}
return (reinterpret_cast<decltype(&__lxstat64)>(libc___lxstat64))(ver, path, buf);
}
int access(const char* path, int mode) __THROW {
init_passthrough_if_needed();
if (ld_is_env_initialized() && is_fs_path(path)) {
// TODO
}
return (reinterpret_cast<decltype(&access)>(libc_access))(path, mode);
}
int puts(const char* str) {
return (reinterpret_cast<decltype(&puts)>(libc_puts))(str);
}
ssize_t write(int fd, const void* buf, size_t count) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
ld_logger->trace("{}() called with fd {}", __func__, fd);
// TODO if append flag has been given, set offset accordingly.
// XXX handle lseek too
return pwrite(fd, buf, count, 0);
}
return (reinterpret_cast<decltype(&write)>(libc_write))(fd, buf, count);
}
ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
ld_logger->trace("{}() called with fd {}", __func__, fd);
return adafs_pwrite_ws(fd, buf, count, offset);
}
return (reinterpret_cast<decltype(&pwrite)>(libc_pwrite))(fd, buf, count, offset);
}
ssize_t pwrite64(int fd, const void* buf, size_t count, __off64_t offset) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
ld_logger->trace("{}() called with fd {}", __func__, fd);
return adafs_pwrite_ws(fd, buf, count, offset);
}
return (reinterpret_cast<decltype(&pwrite64)>(libc_pwrite64))(fd, buf, count, offset);
}
ssize_t read(int fd, void* buf, size_t count) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
ld_logger->trace("{}() called with fd {}", __func__, fd);
return pread(fd, buf, count, 0);
}
return (reinterpret_cast<decltype(&read)>(libc_read))(fd, buf, count);
}
ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
ld_logger->trace("{}() called with fd {}", __func__, fd);
return adafs_pread_ws(fd, buf, count, offset);
}
return (reinterpret_cast<decltype(&pread)>(libc_pread))(fd, buf, count, offset);
}
ssize_t pread64(int fd, void* buf, size_t count, __off64_t offset) {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(fd)) {
ld_logger->trace("{}() called with fd {}", __func__, fd);
return adafs_pread_ws(fd, buf, count, offset);
}
return (reinterpret_cast<decltype(&pread64)>(libc_pread64))(fd, buf, count, offset);
}
off_t lseek(int fd, off_t offset, int whence) __THROW {
init_passthrough_if_needed();
return (reinterpret_cast<decltype(&lseek)>(libc_lseek))(fd, offset, whence);
}
off_t lseek64(int fd, off_t offset, int whence) __THROW {
init_passthrough_if_needed();
return lseek(fd, offset, whence);
}
int truncate(const char* path, off_t length) __THROW {
init_passthrough_if_needed();
return (reinterpret_cast<decltype(&truncate)>(libc_truncate))(path, length);
}
int ftruncate(int fd, off_t length) __THROW {
init_passthrough_if_needed();
return (reinterpret_cast<decltype(&ftruncate)>(libc_ftruncate))(fd, length);
}
int dup(int oldfd) __THROW {
init_passthrough_if_needed();
if (ld_is_env_initialized() && file_map.exist(oldfd)) {
// Not implemented
return -1;
}
return (reinterpret_cast<decltype(&dup)>(libc_dup))(oldfd);
}
int dup2(int oldfd, int newfd) __THROW {
init_passthrough_if_needed();
if (ld_is_env_initialized() && (file_map.exist(oldfd) || file_map.exist(newfd))) {
// Not implemented
return -1;
}
return (reinterpret_cast<decltype(&dup2)>(libc_dup2))(oldfd, newfd);
}