-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintromake.cxx
773 lines (579 loc) · 16.3 KB
/
intromake.cxx
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/**
* A passthrough FUSE filesystem that traces `open` and `create` calls
* adapted from the libfuse example fusexmp_fh.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/xattr.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
extern "C"{
#include <ulockmgr.h>
}
#include "sysutil.hxx"
#include "ProcessTree.hxx"
#include "ProcIORecorder.hxx"
class IntrofsState
{
public:
IntrofsState(pid_t _tool_pid, const char *logfilename)
: tool_pid(_tool_pid) {
logfp = fopen(logfilename, "w");
}
~IntrofsState() {
if (this->logfp != NULL)
fclose(logfp);
}
const pid_t tool_pid;
FILE *logfp;
ProcIORecorder pio;
ProcessTree pstree;
};
static int log_pid() noexcept
{
struct fuse_context *context = fuse_get_context();
IntrofsState* state = static_cast<IntrofsState *>(context->private_data);
FILE *logfp = state->logfp;
if (logfp == NULL)
return 0;
const pid_t pid = context->pid;
const pid_t ppid = state->pstree.ppidof(pid);
return fprintf(logfp, "%d %d ", pid, ppid);
}
static int log_printf(const char *format, ...) noexcept
{
struct fuse_context *context = fuse_get_context();
IntrofsState* state = static_cast<IntrofsState *>(context->private_data);
FILE *logfp = state->logfp;
if (logfp == NULL)
return 0;
va_list args;
va_start(args, format);
int n = vfprintf(logfp, format, args);
va_end(args);
return n;
}
//
// FUSE delegate definitions
//
static void *introfs_init(struct fuse_conn_info *conn) noexcept
{
IntrofsState *pstate = static_cast<IntrofsState *>(fuse_get_context()->private_data);
sigqueue(pstate->tool_pid, SIGUSR2, sigval());
return pstate;
}
static void introfs_destroy(void *pdata) noexcept
{
try {
IntrofsState *pstate = static_cast<IntrofsState *>(pdata);
//
// Dump out file-ids
//
const FilenameInternTable &ftable = pstate->pio.get_filename_table();
typedef FilenameInternTable::id_type id_type;
for( id_type id = 0; id < ftable.size(); ++id )
log_printf("%d %s\n", id, ftable.name(id).c_str());
//
// Dump out dependency information
//
ProcIORecorder::dependency_map_type depmap;
pstate->pio.populate_dependency_map(depmap);
id_type last_id = -1;
for(auto const &d : depmap) {
if (d.first != last_id)
log_printf("\n%d:", d.first);
log_printf(" %d", d.second);
last_id = d.first;
}
}
catch(...) {}
}
static int introfs_getattr(const char *path, struct stat *stbuf)
{
int res;
res = lstat(path, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int introfs_fgetattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
int res;
(void) path;
res = fstat(fi->fh, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int introfs_access(const char *path, int mask)
{
int res;
res = access(path, mask);
if (res == -1)
return -errno;
return 0;
}
static int introfs_readlink(const char *path, char *buf, size_t size)
{
int res;
res = readlink(path, buf, size - 1);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
static int introfs_opendir(const char *path, struct fuse_file_info *fi)
{
DIR *dp = opendir(path);
if (dp == NULL)
return -errno;
fi->fh = (unsigned long) dp;
return 0;
}
static int introfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
DIR *dp = (DIR *) fi->fh;
struct dirent *de;
(void) path;
seekdir(dp, offset);
while ((de = readdir(dp)) != NULL) {
struct stat st = {};
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, telldir(dp)))
break;
}
return 0;
}
static int introfs_releasedir(const char *path, struct fuse_file_info *fi)
{
DIR *dp = (DIR *) fi->fh;
(void) path;
closedir(dp);
return 0;
}
static int introfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
if (S_ISFIFO(mode))
res = mkfifo(path, mode);
else
res = mknod(path, mode, rdev);
if (res == -1)
return -errno;
return 0;
}
static int introfs_mkdir(const char *path, mode_t mode)
{
int res;
res = mkdir(path, mode);
if (res == -1)
return -errno;
return 0;
}
static int introfs_unlink(const char *path)
{
int res;
res = unlink(path);
if (res == -1)
return -errno;
return 0;
}
static int introfs_rmdir(const char *path)
{
int res;
res = rmdir(path);
if (res == -1)
return -errno;
return 0;
}
static int introfs_symlink(const char *from, const char *to)
{
int res;
res = symlink(from, to);
if (res == -1)
return -errno;
return 0;
}
static int introfs_rename(const char *from, const char *to)
{
int res;
res = rename(from, to);
if (res == -1)
return -errno;
return 0;
}
static int introfs_link(const char *from, const char *to)
{
int res;
res = link(from, to);
if (res == -1)
return -errno;
return 0;
}
static int introfs_chmod(const char *path, mode_t mode)
{
int res;
res = chmod(path, mode);
if (res == -1)
return -errno;
return 0;
}
static int introfs_chown(const char *path, uid_t uid, gid_t gid)
{
int res;
res = lchown(path, uid, gid);
if (res == -1)
return -errno;
return 0;
}
static int introfs_truncate(const char *path, off_t size)
{
int res;
res = truncate(path, size);
if (res == -1)
return -errno;
return 0;
}
static int introfs_ftruncate(const char *path, off_t size,
struct fuse_file_info *fi)
{
int res;
(void) path;
res = ftruncate(fi->fh, size);
if (res == -1)
return -errno;
return 0;
}
static int introfs_utimens(const char *path, const struct timespec ts[2])
{
int res;
struct timeval tv[2];
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = ts[0].tv_nsec / 1000;
tv[1].tv_sec = ts[1].tv_sec;
tv[1].tv_usec = ts[1].tv_nsec / 1000;
res = utimes(path, tv);
if (res == -1)
return -errno;
return 0;
}
static int introfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
int fd;
fd = open(path, fi->flags, mode);
if (fd == -1)
return -errno;
try {
auto *context = fuse_get_context();
auto *pstate = static_cast<IntrofsState *>(context->private_data);
pstate->pio.record_process_output(context->pid, path);
} catch(...) { return -1; }
fi->fh = fd;
return 0;
}
static int introfs_open(const char *path, struct fuse_file_info *fi) noexcept
{
int fd = open(path, fi->flags);
if (fd == -1)
return -errno;
try {
auto *context = fuse_get_context();
auto *pstate = static_cast<IntrofsState *>(context->private_data);
if ( fi->flags & (O_WRONLY | O_RDWR | O_CREAT | O_TRUNC) ) {
pstate->pio.record_process_output(context->pid, path);
}
else {
pstate->pio.record_process_input(context->pid, path);
}
} catch(...) { return -1; }
fi->fh = fd;
return 0;
}
static int introfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int res;
(void) path;
res = pread(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int introfs_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int res;
(void) path;
res = pwrite(fi->fh, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int introfs_statfs(const char *path, struct statvfs *stbuf)
{
int res;
res = statvfs(path, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int introfs_flush(const char *path, struct fuse_file_info *fi)
{
int res;
(void) path;
/* This is called from every close on an open file, so call the
close on the underlying filesystem. But since flush may be
called multiple times for an open file, this must not really
close the file. This is important if used on a network
filesystem like NFS which flush the data/metadata on close() */
res = close(dup(fi->fh));
if (res == -1)
return -errno;
return 0;
}
static int introfs_release(const char *path, struct fuse_file_info *fi)
{
(void) path;
close(fi->fh);
return 0;
}
static int introfs_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
int res;
(void) path;
(void) isdatasync;
res = fsync(fi->fh);
if (res == -1)
return -errno;
return 0;
}
static int introfs_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags)
{
int res = lsetxattr(path, name, value, size, flags);
if (res == -1)
return -errno;
return 0;
}
static int introfs_getxattr(const char *path, const char *name, char *value,
size_t size)
{
int res = lgetxattr(path, name, value, size);
if (res == -1)
return -errno;
return res;
}
static int introfs_listxattr(const char *path, char *list, size_t size)
{
int res = llistxattr(path, list, size);
if (res == -1)
return -errno;
return res;
}
static int introfs_removexattr(const char *path, const char *name)
{
int res = lremovexattr(path, name);
if (res == -1)
return -errno;
return 0;
}
static int introfs_lock(const char *path, struct fuse_file_info *fi, int cmd,
struct flock *lock)
{
(void) path;
return ulockmgr_op(fi->fh, cmd, lock, &fi->lock_owner,
sizeof(fi->lock_owner));
}
static struct _introfs_operations: public fuse_operations
{
_introfs_operations()
{
this->init = introfs_init;
this->destroy = introfs_destroy;
this->getattr = introfs_getattr;
this->fgetattr = introfs_fgetattr;
this->access = introfs_access;
this->readlink = introfs_readlink;
this->opendir = introfs_opendir;
this->readdir = introfs_readdir;
this->releasedir = introfs_releasedir;
this->mknod = introfs_mknod;
this->mkdir = introfs_mkdir;
this->symlink = introfs_symlink;
this->unlink = introfs_unlink;
this->rmdir = introfs_rmdir;
this->rename = introfs_rename;
this->link = introfs_link;
this->chmod = introfs_chmod;
this->chown = introfs_chown;
this->truncate = introfs_truncate;
this->ftruncate = introfs_ftruncate;
this->utimens = introfs_utimens;
this->create = introfs_create;
this->open = introfs_open;
this->read = introfs_read;
this->write = introfs_write;
this->statfs = introfs_statfs;
this->flush = introfs_flush;
this->release = introfs_release;
this->fsync = introfs_fsync;
this->setxattr = introfs_setxattr;
this->getxattr = introfs_getxattr;
this->listxattr = introfs_listxattr;
this->removexattr= introfs_removexattr;
this->lock = introfs_lock;
this->flag_nullpath_ok = 0;
}
} introfs_oper;
/*
* `intromake` works by using FUSE (filesystem in user space) to setup a new
* filesystem that mirrors the filesystem rooted under the / directory.
* The new filesystem serves content from the original filesystem while
* keeping track of file opens and creates.
*
* When `intromake` is invoked inside a directory `dir`, it first sets up the
* mirroring-introspecting filesystem and then proceeds to spawn a `make`
* inside the mirrored copy of `dir`. This allows intromake to monitor the
* file operations performed by `make` and its subprocesses.
*
* In particular, `intromake` introspects file creates and accesses. During a
* build, if a file `E` was created by opening and processing files
* `F_1`, `F_2`, ..., `F_n`, then it is reasonable to assume that `E` depends
* on `F_{1 to N}`. Thus, the introspection information stored by `intromake`
* enables automatic dependency extraction.
*
* `intromake` then waits for the spawned `make` to complete and then sets up
* a lazy unmount of the mirroring filesystem with an invocation of
* `fusermount -uz`
*
* Implementation detail:
* ----------------------
*
* The sequence of filesystem setup, make invocation and filesystem unmount
* could possibly have been written as a shell script. However, we need
* some fine grained synchronization that is best achieved in code with some
* system calls. This synchronization is described next.
*
* After `make` is spawned, it needs to pause till the FUSE filesystem is
* setup by the parent `intromake` process. Otherwise it will begin building
* inside a non-existent directory tree. In code, this is achieved by making
* the spawned `make` process pause util a signal is sent. The parent
* `intromake` process sends this signal on filesystem init.
*/
/*
* intromake configuration. Might load this from a config file in the future.
*/
static struct Configuration {
char const *mount_point;
char const *log_filename;
char const *tool_name;
Configuration()
: mount_point("/tmp/__introfs__"),
log_filename("/tmp/__introfs__.log"),
tool_name("make")
{}
std::string get_mirrored_path(const std::string &path) const {
return std::string(mount_point) + "/" + path;
}
} config;
void *fuse_ops_thread_func(void *pstate)
{
static char *args[] = {
const_cast<char *>( "introfs" ),
const_cast<char *>( config.mount_point ),
const_cast<char *>( "-f" ),
};
fuse_main(array_size(args), args, &introfs_oper, pstate);
return NULL;
}
void intromake_main(const pid_t &child_pid)
{
ensure_mount_point("/tmp/__introfs__");
IntrofsState state(child_pid, config.log_filename);
//
// spawn a thread to handle FUSE events
//
pthread_t fuse_ops_thread;
int err = pthread_create(&fuse_ops_thread, NULL, fuse_ops_thread_func, &state);
if ( err != 0 )
throw SystemException("pthread_create failed", err);
//
// Meanwhile wait for the spawned `make` process to complete
//
while (true) {
int wstatus;
int cid = waitpid(child_pid, &wstatus, WNOHANG | WUNTRACED);
if (cid == 0)
/* The waitpid call would have hanged. The child process
is possibly not executing. try again */
continue;
if (cid == -1)
continue;
if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus))
/* The child actually terminated */
break;
}
//
// Lazy unmount the introfs filesystem
//
system("fusermount -uz /tmp/__introfs__");
}
/*
* No-op signal handler
*/
void on_user_signal1(int) { return; }
void spawned_make_main(int argc0, char* argv0[])
{
//
// Prepare to be woken up by the `intromake` parent process
// and then pause for a signal
//
signal(SIGUSR2, on_user_signal1);
pause();
//
// Change directory to the mirrored copy.
// chroot to the mount point.
//
const auto &curdir = get_current_dir();
change_dir( config.get_mirrored_path(curdir) );
chroot(config.mount_point);
//
// Prepare arg vector for the make process
//
char *child_args[argc0+1];
memcpy(child_args, argv0, sizeof(char*)*argc0);
child_args[0] = const_cast<char *>(config.tool_name);
child_args[argc0] = NULL;
if( execvp(config.tool_name, child_args) == -1 )
throw SystemException("Failed to spawn make", errno);
}
int main(int argc, char *argv[])
{
umask(0);
const pid_t child_pid = fork();
if (child_pid > 0) {
intromake_main(child_pid);
}
else if (child_pid == 0) /* this is the child */ {
spawned_make_main(argc, argv);
}
else {
throw SystemException("fork() failed", errno);
}
return 0;
}