-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathdevfsev.c
258 lines (239 loc) · 6.03 KB
/
devfsev.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
/* fsmon -- MIT - Copyright NowSecure 2015-2016 - pancake@nowsecure.com */
#if __APPLE__
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <errno.h>
#include "fsmon.h"
#define WIP 1
#define FM_DEV "/dev/fsevents"
#define FM_BUFSIZE 4096
#define R_MIN(x,y) (((x)>(y))?(y):(x))
typedef struct __attribute__ ((__packed__)) {
uint16_t type;
uint16_t len;
union {
uint32_t u32;
uint64_t u64;
void *ptr;
uint16_t words[4];
} val;
} FMEventStruct;
static void fsevent_free (FileMonitorEvent *ev) {
memset (ev, 0, sizeof (FileMonitorEvent));
ev->type = -1;
}
static int parse_event(FileMonitorEvent *ev, FMEventStruct *fme) {
int len = fme->val.words[3];
switch (fme->type) {
case 0:
break;
case FSE_ARG_RAW:
case FSE_ARG_VNODE:
case FSE_ARG_INT32:
/* not yet handled */
break;
case FSE_ARG_INT64: // This is a timestamp field on the FSEvent
// Event IDs are monotonically increasing per system, even
// across reboots and drives coming and going. They bear
// no relation to any particular clock or timebase.
ev->tstamp = fme->val.u64;
break;
case FSE_ARG_STRING: // This is a filename, for move/rename (Type 3)
// ev->newfile = (const char *)(fme) + 12;
break;
case FSE_ARG_DEV: // Block Device associated with the mounted fs
{
uint32_t val = fme->val.u32;
ev->dev_major = major (val);
ev->dev_minor = minor (val);
}
break;
case FSE_ARG_MODE:
ev->mode = fme->val.u32;
break;
case FSE_ARG_INO:
ev->inode = fme->val.u32;
break;
case FSE_ARG_UID:
ev->uid = fme->val.u32;
break;
case FSE_ARG_GID: // 0xb // 11 // This shuold be ARG_STRING or ARG_PATH
ev->gid = fme->val.u32;
if (ev->type == FSE_RENAME) {
ev->newfile = (const char *)(fme) + 12;
if (*ev->newfile != '/') {
ev->newfile = NULL;
ev->type = FSE_CREATE_FILE;
}
} else {
ev->newfile = NULL;
}
break;
case FSE_ARG_PATH:
//ev->newfile = (const char *)(fme) + 12;
break;
case FSE_ARG_FINFO:
// Not handling this yet.. Not really used, either..
break;
case FSE_ARG_DONE:
return -1;
case FSE_EVENTS_DROPPED: // 999 / 0x3e7
/* do nothing */
return 8;
default:
eprintf ("ERROR unknown type %d\n", fme->type);
/* ERROR */
return 0;
}
return len + sizeof (FMEventStruct);
}
static int fdsetup(int fd) {
fsevent_clone_args clone_args = {0};
int8_t events[FSE_MAX_EVENTS];
int rc, cloned_fd = -1;
memset (events, FSE_REPORT, FSE_MAX_EVENTS); // FSE_IGNORE
clone_args.fd = &cloned_fd; // This is the descriptor we get back
clone_args.event_queue_depth = 10;
clone_args.event_list = events;
clone_args.num_events = FSE_MAX_EVENTS;
rc = ioctl (fd, FSEVENTS_CLONE, &clone_args);
close (fd);
if (rc < 0) {
perror ("ioctl");
return -1;
}
/* get extended info from events */
if ((rc = ioctl (cloned_fd, FSEVENTS_WANT_EXTENDED_INFO, NULL)) < 0) {
perror ("ioctl");
close (cloned_fd);
return -1;
}
return cloned_fd;
}
static bool fm_begin (FileMonitor *fm) {
int fd;
fm->fd = -1;
fd = open (FM_DEV, O_RDONLY);
if (fd == -1) {
perror ("open "FM_DEV);
return false;
}
fd = fdsetup (fd);
if (fd == -1) {
perror ("fdclone");
return false;
}
fm->fd = fd;
return true;
}
static bool fm_loop (FileMonitor *fm, FileMonitorCallback cb) {
FileMonitorEvent ev = {0};
uint8_t buf[FM_BUFSIZE] = {0};
int arg_len, rc, buf_idx = 0, buf_end = -1;
if (sizeof (FMEventStruct) != 12) {
eprintf ("Invalid FMEventStruct, check your compiler\n");
return false;
}
for (; fm->running; ) {
if (buf_idx == buf_end) {
buf_idx = 0;
} else if (buf_idx > 0) {
if (buf_idx > buf_end) {
eprintf ("Overflow detected and corrected (%d, %d)\n", buf_idx, buf_end);
buf_idx = 0;
} else {
memmove (buf, buf + buf_idx, (buf_end - buf_idx));
buf_idx = (buf_end - buf_idx);
}
}
if (buf_idx > FM_BUFSIZE) {
eprintf ("Warning: Some data is lost in fsevents data read (%d, %d)\n", buf_idx, FM_BUFSIZE);
buf_idx = 0;
}
memset (buf + buf_idx, 0x00, FM_BUFSIZE - buf_idx);
rc = read (fm->fd, buf + buf_idx, FM_BUFSIZE - buf_idx);
// hexdump (buf+buf_idx, rc, 0); //arg_len + 2, 0);
if (rc < 1) {
if (errno != EINTR) {
perror ("read");
}
return false;
}
buf_idx = 0;
buf_end = buf_idx + rc;
if (buf_end >= sizeof (buf))
buf_end = sizeof (buf);
while (buf_idx + 1 < buf_end) {
// hexdump (buf+buf_idx, sizeof (FMEventStruct) + 32, 0);
FMEventStruct *fme = (FMEventStruct*) (buf + buf_idx);
/* initialize on first set */
if (ev.type == -1) {
ev.type = fme->type;
ev.pid = fme->val.u32;
ev.ppid = 0;
ev.proc = get_proc_name (ev.pid, &ev.ppid);
ev.file = (const char *)buf + buf_idx + sizeof (FMEventStruct);
}
/* parse data packet */
arg_len = parse_event (&ev, fme);
if (arg_len == -1) {
if (ev.pid && ev.type != -1 && cb) {
cb (fm, &ev);
}
fsevent_free (&ev);
arg_len = 2;
} else if (arg_len < 1) {
arg_len = sizeof (FMEventStruct);
} else if (arg_len > (buf_end - buf_idx)) {
#if WIP
int i;
bool found = false;
for (i = buf_idx + 1; i + sizeof (FMEventStruct) < buf_end; i++) {
if (!memcmp ("\x00\x00\xd4\x8f", buf +i, 4)) {
found = true;
break;
}
}
if (found) {
arg_len = i - buf_idx;
eprintf ("FCU %d\n", arg_len);
} else {
arg_len = sizeof (FMEventStruct) + 2;
eprintf ("Invalid length in fsevents data packet (%d, %d)\n",
arg_len, buf_end - buf_idx);
//arg_len += 12;
// hexdump (buf + buf_idx, buf_end - buf_idx, 0); //arg_len + 2, 0);
}
#else
arg_len = sizeof (FMEventStruct) + 2;
eprintf ("Invalid length in fsevents data packet (%d, %d)\n",
arg_len, buf_end - buf_idx);
#endif
}
buf_idx += arg_len;
}
}
return true;
}
static bool fm_end(FileMonitor *fm) {
if (fm && fm->fd != -1) {
close (fm->fd);
fm->fd = -1;
return true;
}
return false;
}
FileMonitorBackend fmb_devfsev = {
.name = "devfsev",
.begin = fm_begin,
.loop = fm_loop,
.end = fm_end,
};
#endif