forked from xzfc/cached-nix-shell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trace-nix.c
318 lines (270 loc) · 7.57 KB
/
trace-nix.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
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
309
310
311
312
313
314
315
316
317
318
#define _GNU_SOURCE
#include "blake3.h"
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static FILE *log_f = NULL;
static const char *pwd = NULL;
#define FATAL() \
do { \
fprintf(stderr, "nix-trace.c:%d: %s: %s\n", \
__LINE__, __func__, strerror(errno)); \
exit(2); \
} while(0)
#define LEN 16
// Locks
#ifdef __APPLE__
#include <dispatch/dispatch.h>
static dispatch_semaphore_t print_mutex;
static dispatch_semaphore_t buf_mutex;
#define INIT_MUTEX(MUTEX) MUTEX = dispatch_semaphore_create(1)
#define LOCK(MUTEX) dispatch_semaphore_wait(MUTEX, DISPATCH_TIME_FOREVER)
#define UNLOCK(MUTEX) dispatch_semaphore_signal(MUTEX)
#else
#include <pthread.h>
static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER;
#define INIT_MUTEX(MUTEX)
#define LOCK(MUTEX) pthread_mutex_lock(&MUTEX)
#define UNLOCK(MUTEX) pthread_mutex_unlock(&MUTEX)
#endif
// Predeclarations
static void convert_digest(char [static LEN*2+1], const uint8_t [static LEN]);
static int enable(const char *);
static void hash_dir(char [static LEN*2+1], DIR *);
static void hash_file(char [static LEN*2+1], int);
static void print_log(char, const char *, const char *);
static void print_stat(int result, const char *path, struct stat *sb);
static int strcmp_qsort(const void *, const void *);
////////////////////////////////////////////////////////////////////////////////
static void __attribute__((constructor)) init() {
// Remove ourselves from LD_PRELOAD and DYLD_INSERT_LIBRARIES.
// We do not want to log child processes.
// TODO: use `ld.so --preload` instead
unsetenv("LD_PRELOAD");
unsetenv("DYLD_INSERT_LIBRARIES");
const char *fname = getenv("TRACE_NIX");
if (fname != NULL) {
log_f = fopen(fname, "w");
if (log_f == NULL) {
fprintf(stderr, "trace-nix: can't open file %s: %s\n", fname,
strerror(errno));
errno = 0;
}
#ifdef __APPLE__
pwd = getcwd(NULL, 0);
#else
pwd = get_current_dir_name();
#endif
if (pwd == NULL)
FATAL();
}
unsetenv("TRACE_NIX");
INIT_MUTEX(print_mutex);
INIT_MUTEX(buf_mutex);
}
#ifdef __APPLE__
#define WRAPPER(RET, FUN, ARGS) \
static RET _cns_wrapper_##FUN ARGS; \
__attribute__((used)) static void *_cns_interpose_##FUN[2] \
__attribute__((section("__DATA,__interpose"))) = { &_cns_wrapper_##FUN, &FUN }; \
static RET _cns_wrapper_##FUN ARGS
#define REAL(FUN) FUN
#else
#define WRAPPER(RET, FUN, ARGS) \
static RET (*_cns_real_##FUN)ARGS = NULL; \
RET FUN ARGS
#define REAL(FUN) \
(_cns_real_##FUN == NULL ? (_cns_real_##FUN = dlsym(RTLD_NEXT, #FUN)) : _cns_real_##FUN)
#endif
WRAPPER(int, lstat, (const char *path, struct stat *sb)) {
int result = REAL(lstat)(path, sb);
print_stat(result, path, sb);
return result;
}
#ifdef __linux__
WRAPPER(int, __lxstat, (int ver, const char *path, struct stat *sb)) {
int result = REAL(__lxstat)(ver, path, sb);
print_stat(result, path, sb);
return result;
}
#endif
WRAPPER(int, open, (const char *path, int flags, ...)) {
va_list args;
va_start(args, flags);
int mode = va_arg(args, int);
va_end(args);
int fd = REAL(open)(path, flags, mode);
if (flags == (O_RDONLY|O_CLOEXEC) && enable(path)) {
if (fd == -1) {
print_log('f', path, "-");
} else {
char digest[LEN*2+1];
hash_file(digest, fd);
print_log('f', path, digest);
}
}
return fd;
}
WRAPPER(DIR *, opendir, (const char *path)) {
DIR *dirp = REAL(opendir)(path);
if (enable(path)) {
if (dirp == NULL) {
print_log('d', path, "-");
} else {
char digest[LEN*2+1];
hash_dir(digest, dirp);
print_log('d', path, digest);
}
}
return dirp;
}
////////////////////////////////////////////////////////////////////////////////
static int enable(const char *path) {
if (log_f == NULL || (*path != '/' && strcmp(path, "shell.nix")))
return 0;
static const char *ignored_paths[] = {
"/etc/ssl/certs/ca-certificates.crt",
"/nix/var/nix/daemon-socket/socket",
"/nix",
"/nix/store",
NULL,
};
static const char *ignored_prefices[] = {
"/nix/store/", // assuming store paths are immutable
"/nix/var/nix/temproots/",
"/proc/",
NULL,
};
for (const char **p = ignored_paths; *p; p++)
if (!strcmp(path, *p))
return 0;
for (const char **p = ignored_prefices; *p; p++)
if (!memcmp(path, *p, strlen(*p)))
return 0;
return 1;
}
static void print_stat(int result, const char *path, struct stat *sb) {
static char *buf = NULL;
static off_t buf_len = 0;
if (enable(path)) {
if (result != 0) {
print_log('s', path, "-");
} else if (S_ISLNK(sb->st_mode)) {
LOCK(buf_mutex);
if (buf_len < sb->st_size + 2) {
buf_len = sb->st_size + 2;
buf = realloc(buf, buf_len);
if (buf == NULL)
FATAL();
}
ssize_t link_len = readlink(path, buf+1, sb->st_size);
if (link_len < 0 || link_len != sb->st_size)
FATAL();
buf[0] = 'l';
buf[sb->st_size+1] = 0;
print_log('s', path, buf);
UNLOCK(buf_mutex);
} else if (S_ISDIR(sb->st_mode)) {
print_log('s', path, "d");
} else {
print_log('s', path, "+");
}
}
}
static void print_log(char op, const char *path, const char *result) {
LOCK(print_mutex);
fprintf(
log_f,
"%c" "%s%s" "%s%c" "%s%c",
op,
path[0] == '/' ? "" : pwd, path[0] == '/' ? "" : "/",
path, (char)0,
result, (char)0
);
fflush(log_f);
UNLOCK(print_mutex);
}
static void hash_file(char digest_s[static LEN*2+1], int fd) {
struct stat stat_;
int rc = fstat(fd, &stat_);
if (rc != 0)
FATAL();
char *mmaped = NULL;
if (stat_.st_size != 0) {
mmaped = mmap(NULL, stat_.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mmaped == MAP_FAILED) {
strcpy(digest_s, "e");
return;
}
}
blake3_hasher hasher;
blake3_hasher_init(&hasher);
blake3_hasher_update(&hasher, mmaped, stat_.st_size);
uint8_t digest_b[LEN];
blake3_hasher_finalize(&hasher, digest_b, LEN);
convert_digest(digest_s, digest_b);
if (stat_.st_size != 0) {
rc = munmap(mmaped, stat_.st_size);
if (rc != 0)
FATAL();
}
}
static int strcmp_qsort(const void *a, const void *b) {
return strcmp(*(const char* const*)a, *(const char * const*)b);
}
static void hash_dir(char digest_s[static LEN*2+1], DIR *dirp) {
// A dynamically growing array of strings
size_t entries_total = 32, n = 0;
char **entries = calloc(entries_total, sizeof(char*));
if (entries == NULL)
FATAL();
struct dirent *ent;
while ((ent = readdir(dirp))) {
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
continue;
if (n+1 >= entries_total) {
entries_total *= 2;
entries = realloc(entries, entries_total * sizeof(char*));
if (entries == NULL)
FATAL();
}
char ent_type =
ent->d_type == DT_DIR ? 'd' :
ent->d_type == DT_LNK ? 'l' :
ent->d_type == DT_REG ? 'f' :
'u';
int l = asprintf(&entries[n++], "%s=%c", ent->d_name, ent_type);
if (l == -1)
FATAL();
}
qsort(entries, n, sizeof(char*), strcmp_qsort);
// Calculate hash
uint8_t digest_b[LEN];
blake3_hasher hasher;
blake3_hasher_init(&hasher);
for (size_t i = 0; i < n; i++)
blake3_hasher_update(&hasher, entries[i], strlen(entries[i])+1);
blake3_hasher_finalize(&hasher, digest_b, LEN);
convert_digest(digest_s, digest_b);
// Memory cleanup
for (size_t i = 0; i < n; i++)
free(entries[i]);
free(entries);
// Revert dirp into initial state
rewinddir(dirp);
}
static void convert_digest(char digest_s[static LEN*2+1], const uint8_t digest_b[static LEN]) {
for (int i = 0; i < LEN; i++)
sprintf(digest_s + i*2, "%02x", (unsigned)digest_b[i]);
}