-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess-order.c
More file actions
155 lines (126 loc) · 2.74 KB
/
Copy pathaccess-order.c
File metadata and controls
155 lines (126 loc) · 2.74 KB
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
#if 0
exec musl-gcc -static access-order.c -o access-order -Wall -Wextra -pedantic -std=c99 -O2
exit 1
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fanotify.h>
#include <sys/mount.h>
#include <unistd.h>
#include "uthash.h"
struct entry {
const char *path;
unsigned int priority;
UT_hash_handle hh;
};
static int run = 1;
void
quit(int sig)
{
run = 0;
}
int
priority_cmp(struct entry *a, struct entry *b)
{
return (a->priority > b->priority) - (a->priority < b->priority);
}
int
main(int argc, char *argv[])
{
struct fanotify_event_metadata *meta;
struct fanotify_event_metadata buf[200];
pid_t pid;
ssize_t len;
char procfd_path[PATH_MAX];
char path[PATH_MAX];
ssize_t path_len;
unsigned int priority = 0;
struct entry *file_tab = NULL;
pid = getpid();
if(pid == 1) {
mount("proc", "/proc", "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, 0);
}
int fd = fanotify_init(FAN_CLOEXEC, O_RDONLY);
// DONT_FOLLOW?
fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_ACCESS, 0, "/");
if(pid == 1) {
pid = fork();
if(pid < 0) {
return -1;
}
if(pid > 0) {
execl("/sbin/init", "/sbin/init");
return -1;
}
}
signal(SIGINT, quit);
while(run) {
do {
len = read(fd, buf, sizeof(buf));
} while(run && len == -1 && errno == EAGAIN);
if(!run) {
break;
}
if(len == -1) {
perror("read");
return -1;
} else if(len == 0) {
break;
}
meta = buf;
while(FAN_EVENT_OK(meta, len)) {
if(meta->vers != FANOTIFY_METADATA_VERSION) {
fputs("Mismatch of fanotify metadata version.\n", stderr);
return -1;
}
if(meta->fd == FAN_NOFD) {
goto next;
}
snprintf(procfd_path, sizeof(procfd_path),
"/proc/self/fd/%d", meta->fd);
path_len = readlink(procfd_path, path, sizeof(path) - 1);
close(meta->fd);
if(path_len == -1) {
perror("readlink");
goto next;
}
path[path_len] = '\0';
if(path[0] != '/') {
goto next;
}
struct entry *found;
HASH_FIND_STR(file_tab, path, found);
if(found == NULL) {
struct entry *entry = malloc(sizeof(*entry));
entry->path = strdup(path);
entry->priority = priority;
HASH_ADD_STR(file_tab, path, entry);
if(priority == INT_MAX) {
run = 0;
break;
}
priority++;
}
next:
meta = FAN_EVENT_NEXT(meta, len);
}
}
close(fd);
HASH_SORT(file_tab, priority_cmp);
int sortfd = open("/var/log/access-order.log", O_WRONLY|O_CREAT|O_TRUNC, 0644);
FILE *sort = fdopen(sortfd, "w");
struct entry *entry;
struct entry *tmp;
HASH_ITER(hh, file_tab, entry, tmp) {
fprintf(sort, "%s %d\n", entry->path, entry->priority);
}
fclose(sort);
return 0;
}