-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrapper.c
178 lines (154 loc) · 4.58 KB
/
wrapper.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
#define _GNU_SOURCE
#include <dlfcn.h>
#include "wrapper.h"
static struct flexfile *files[65536]; // int fd -> struct flexfile *, mapping of open files per process
static FILE *tmpfiles[65536]; // int fd -> FILE *, to avoid memory leaks
#define O_FLEXFILE 0xffffffff
#define FIXED_PATH "/tmp/flexfile"
static int (*real_open)(const char *pathname, int flags, ...) = NULL;
int open(const char *pathname, int flags, ...)
{
if ((flags & O_FLEXFILE) != O_FLEXFILE &&
strncmp(pathname, FIXED_PATH, strlen(pathname)) != 0) {
va_list args;
va_start(args, flags);
mode_t mode = va_arg(args, mode_t);
va_end(args);
if (!real_open) {
real_open = (typeof(real_open))dlsym(RTLD_NEXT, "open");
}
return real_open(pathname, flags, mode);
}
printf("open %s\n", pathname);
fflush(stdout);
struct flexfile *const ff = flexfile_open(pathname);
if (ff) {
FILE *const fp = tmpfile();
const int fd = fileno(fp);
printf("open succeed, fake fd %d\n", fd);
fflush(stdout);
files[fd] = ff;
tmpfiles[fd] = fp;
return fd;
}
return 0;
(void)flags;
}
static int (*real_close)(int fd) = NULL;
int close(int fd)
{
if (!files[fd]) {
if (!real_close) {
real_close = (typeof(real_close))dlsym(RTLD_NEXT, "close");
}
return real_close(fd);
}
printf("close %d\n", fd);
fflush(stdout);
struct flexfile *const ff = files[fd];
FILE *const fp = tmpfiles[fd];
fclose(fp);
flexfile_close(ff);
files[fd] = NULL;
tmpfiles[fd] = NULL;
return 0;
}
static ssize_t (*real_read)(int fd, void *buf, size_t count) = NULL;
ssize_t read(int fd, void *buf, size_t count)
{
if (!files[fd]) {
if (!real_read) {
real_read= (typeof(real_read))dlsym(RTLD_NEXT, "read");
}
return real_read(fd, buf, count);
}
const off_t off = lseek(fd, 0, SEEK_CUR);
return pread(fd, buf, count, off);
}
static ssize_t (*real_pread)(int fd, void *buf, size_t count, off_t offset) = NULL;
ssize_t pread(int fd, void *buf, size_t count, off_t offset)
{
if (!files[fd]) {
if (!real_pread) {
real_pread= (typeof(real_pread))dlsym(RTLD_NEXT, "pread");
}
return real_pread(fd, buf, count, offset);
}
struct flexfile *const ff = files[fd];
return flexfile_read(ff, buf, offset, count);
}
static ssize_t (*real_write)(int fd, const void *buf, size_t count) = NULL;
ssize_t write(int fd, const void *buf, size_t count)
{
if (!files[fd]) {
if (!real_write) {
real_write = (typeof(real_write))dlsym(RTLD_NEXT, "write");
}
return real_write(fd, buf, count);
}
const off_t off = lseek(fd, 0, SEEK_CUR);
return pwrite(fd, buf, count, off);
}
static ssize_t (*real_pwrite)(int fd, const void *buf, size_t count, off_t offset) = NULL;
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
{
if (!files[fd]) {
if (!real_pwrite) {
real_pwrite = (typeof(real_pwrite))dlsym(RTLD_NEXT, "pwrite");
}
return real_pwrite(fd, buf, count, offset);
}
struct flexfile *const ff = files[fd];
return flexfile_write(ff, buf, offset, count);
}
static int (*real_fsync)(int fd) = NULL;
int fsync(int fd)
{
if (!files[fd]) {
if (!real_fsync) {
real_fsync = (typeof(real_fsync))dlsym(RTLD_NEXT, "fsync");
}
return real_fsync(fd);
}
struct flexfile *const ff = files[fd];
flexfile_sync(ff);
return 0;
}
static int (*real_ftruncate)(int fd, off_t length) = NULL;
int ftruncate(int fd, off_t length)
{
if (!files[fd]) {
if (!real_ftruncate) {
real_ftruncate = (typeof(real_ftruncate))dlsym(RTLD_NEXT, "ftruncate");
}
return real_ftruncate(fd, length);
}
struct flexfile *const ff = files[fd];
return flexfile_ftruncate(ff, length);
}
static int (*real_dup)(int oldfd) = NULL;
int dup(int oldfd)
{
if (!files[oldfd]) {
if (!real_dup) {
real_dup = (typeof(real_dup))dlsym(RTLD_NEXT, "dup");
}
return real_dup(oldfd);
}
printf("dup is not implemented for flexfile\n");
fflush(stdout);
exit(1);
}
static int (*real_dup2)(int oldfd, int newfd) = NULL;
int dup2(int oldfd, int newfd)
{
if (!files[oldfd]) {
if (!real_dup2) {
real_dup2 = (typeof(real_dup2))dlsym(RTLD_NEXT, "dup2");
}
return real_dup2(oldfd, newfd);
}
printf("dup2 is not implemented for flexfile\n");
fflush(stdout);
exit(1);
}