-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendix.c
255 lines (198 loc) · 6.18 KB
/
vendix.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h> // opendir
#include <dlfcn.h> // dlopen
#include <fcntl.h> // open, openat
#include <fts.h> // fts_open, fts_open_b
#include <ftw.h> // ftw, nftw
#include <sys/stat.h> // stat, lstat, fstatat
#define NIX_HOME "/nix"
// TODO: Call getenv("DYLD_ROOT_PATH") only once
static char *expand_store(const char *source) {
char *target = NULL;
if (strncmp(NIX_HOME, source, strlen(NIX_HOME)) == 0)
asprintf(&target, "%s%s", getenv("DYLD_ROOT_PATH"), source);
return target;
}
static void freep(void *p) { free(*(void **)p); }
#define raii __attribute__((cleanup(freep)))
#define CONCAT_HELPER(x, y) x##y
#define CONCAT(x, y) CONCAT_HELPER(x, y)
#define OR(x, y) x ? x : y
#define EXPAND_STORE_TO(source, target) \
raii char *target = expand_store(source); \
source = OR(target, source)
#define EXPAND_STORE(source) \
EXPAND_STORE_TO(source, CONCAT(store_target, __COUNTER__))
int access_wrapper(const char *path, int mode) {
EXPAND_STORE(path);
return access(path, mode);
}
int chdir_wrapper(const char *path) {
EXPAND_STORE(path);
return chdir(path);
}
void *dlopen_wrapper(const char *path, int mode) {
EXPAND_STORE(path);
return dlopen(path, mode);
}
int execl_wrapper(const char *path, const char *arg0, ...) {
va_list args;
va_start(args, arg0);
raii const char **argv = malloc(sizeof(char *));
size_t i = 0;
argv[i] = arg0;
while (argv[i] != NULL) {
argv = realloc(argv, (++i + 1) * sizeof(char *));
argv[i] = va_arg(args, const char *);
}
va_end(args);
EXPAND_STORE(path);
return execvp(path, argv);
}
int execle_wrapper(const char *path, const char *arg0, ...) {
return -1; // TODO
}
int execlp_wrapper(const char *path, const char *arg0, ...) {
return -1; // TODO
}
int execv_wrapper(const char *path, char *const argv[]) {
EXPAND_STORE(path);
return execv(path, argv);
}
int execve_wrapper(const char *path, char *const argv[], char *const envp[]) {
EXPAND_STORE(path);
return execve(path, argv, envp);
}
int execvp_wrapper(const char *path, char *const argv[]) {
EXPAND_STORE(path);
return execvp(path, argv);
}
int execvP_wrapper(const char *path, const char *searched_path,
char *const argv[]) {
EXPAND_STORE(path);
return execvP(path, searched_path, argv);
}
FILE *fopen_wrapper(const char *path, const char *mode) {
EXPAND_STORE(path);
return fopen(path, mode);
}
FILE *freopen_wrapper(const char *path, const char *mode, FILE *stream) {
EXPAND_STORE(path);
return freopen(path, mode, stream);
}
int fstatat_wrapper(int fd, const char *path, struct stat *buf, int flag) {
EXPAND_STORE(path);
return fstatat(fd, path, buf, flag);
}
void **copy_array(const void *const *array) {
void *const *ptr = (void *const)array;
size_t len = 0;
while (ptr[len++] != NULL)
;
void **array_copy = malloc(len * sizeof(void *));
memcpy(array_copy, array, len * sizeof(void *));
return array_copy;
}
FTS *fts_open_wrapper(char *const *path_argv, int options,
int (*compar)(const FTSENT **, const FTSENT **)) {
raii char **path_argv_copy =
(char **)copy_array((const void *const *)path_argv);
EXPAND_STORE(path_argv_copy[0]);
return fts_open(path_argv_copy, options, compar);
}
FTS *fts_open_b_wrapper(char *const *path_argv, int options,
int (^compar)(const FTSENT **, const FTSENT **)) {
raii char **path_argv_copy =
(char **)copy_array((const void *const *)path_argv);
EXPAND_STORE(path_argv_copy[0]);
return fts_open_b(path_argv, options, compar);
}
int ftw_wrapper(const char *path,
int (*fn)(const char *, const struct stat *ptr, int flag),
int depth) {
EXPAND_STORE(path);
return ftw(path, fn, depth);
}
int lstat_wrapper(const char *path, struct stat *buf) {
EXPAND_STORE(path);
return lstat(path, buf);
}
int nftw_wrapper(const char *path,
int (*fn)(const char *, const struct stat *ptr, int flag,
struct FTW *),
int depth, int flags) {
EXPAND_STORE(path);
return nftw(path, fn, depth, flags);
}
int open_wrapper(const char *path, int oflag, ...) {
va_list args;
va_start(args, oflag);
int mode = 0;
if (oflag & O_CREAT)
mode = va_arg(args, int);
va_end(args);
EXPAND_STORE(path);
return open(path, oflag, mode);
}
int openat_wrapper(int fd, const char *path, int oflag, ...) {
va_list args;
va_start(args, oflag);
int mode = 0;
if (oflag & O_CREAT)
mode = va_arg(args, int);
va_end(args);
EXPAND_STORE(path);
return openat(fd, path, oflag, mode);
}
DIR *opendir_wrapper(const char *path) {
EXPAND_STORE(path);
return opendir(path);
}
long pathconf_wrapper(const char *path, int name) {
EXPAND_STORE(path);
return pathconf(path, name);
}
char *realpath_wrapper(const char *path, char *resolved_path) {
EXPAND_STORE(path);
return realpath(path, resolved_path);
}
int stat_wrapper(const char *path, struct stat *buf) {
EXPAND_STORE(path);
return stat(path, buf);
}
// https://opensource.apple.com/source/dyld/dyld-210.2.3/include/mach-o/dyld-interposing.h
#define DYLD_INTERPOSE(_replacement, _replacee) \
__attribute__((used)) static struct { \
const void *replacement; \
const void *replacee; \
} _interpose_##_replacee __attribute__((section("__DATA,__interpose"))) = { \
(const void *)(unsigned long)&_replacement, \
(const void *)(unsigned long)&_replacee};
#define WRAP(f) DYLD_INTERPOSE(f##_wrapper, f)
WRAP(access);
WRAP(chdir);
WRAP(dlopen);
WRAP(execl);
WRAP(execv);
WRAP(execve);
WRAP(execvp);
WRAP(execvP);
WRAP(fopen);
WRAP(freopen);
WRAP(fstatat);
WRAP(fts_open);
WRAP(fts_open_b);
WRAP(ftw);
WRAP(lstat);
WRAP(nftw);
WRAP(open);
WRAP(openat);
WRAP(opendir);
WRAP(pathconf);
WRAP(realpath);
WRAP(stat);