Line 0
Link Here
|
|
|
1 |
/*- |
2 |
* Copyright (c) 2016 - 2019 Rozhuk Ivan <rozhuk.im@gmail.com> |
3 |
* All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions |
7 |
* are met: |
8 |
* 1. Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* |
14 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 |
* SUCH DAMAGE. |
25 |
* |
26 |
* Author: Rozhuk Ivan <rozhuk.im@gmail.com> |
27 |
* |
28 |
*/ |
29 |
|
30 |
#include <sys/param.h> |
31 |
#include <sys/types.h> |
32 |
#include <sys/event.h> |
33 |
#include <sys/time.h> |
34 |
#include <sys/stat.h> |
35 |
#include <sys/ucred.h> |
36 |
#include <sys/mount.h> |
37 |
#include <sys/fcntl.h> /* open, fcntl */ |
38 |
#include <sys/queue.h> |
39 |
|
40 |
#include <inttypes.h> |
41 |
#include <stdlib.h> /* malloc, exit */ |
42 |
#include <unistd.h> /* close, write, sysconf */ |
43 |
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */ |
44 |
#include <dirent.h> /* opendir, readdir */ |
45 |
#include <errno.h> |
46 |
#include <pthread.h> |
47 |
#include <glib.h> |
48 |
|
49 |
#include "kqueue_fnm.h" |
50 |
|
51 |
|
52 |
/* Preallocate items count. */ |
53 |
#ifndef FILES_ALLOC_BLK_SIZE |
54 |
# define FILES_ALLOC_BLK_SIZE 32 |
55 |
#endif |
56 |
|
57 |
|
58 |
typedef struct readdir_context_s { |
59 |
int fd; |
60 |
uint8_t *buf; |
61 |
size_t buf_size; |
62 |
size_t buf_used; |
63 |
size_t buf_pos; |
64 |
} readdir_ctx_t, *readdir_ctx_p; |
65 |
|
66 |
|
67 |
typedef struct file_info_s { /* Directory file. */ |
68 |
int fd; /* For notify kqueue(). */ |
69 |
struct dirent de; /* d_reclen used for action. */ |
70 |
struct stat sb; |
71 |
} file_info_t, *file_info_p; |
72 |
|
73 |
|
74 |
typedef struct kq_file_nonify_monitor_obj_s *kq_fnmo_p; |
75 |
|
76 |
typedef struct kq_file_nonify_monitor_entry_s { |
77 |
TAILQ_ENTRY(kq_file_nonify_monitor_entry_s) next; |
78 |
kq_fnmo_p fnmo; |
79 |
void *udata; |
80 |
volatile int enabled; |
81 |
} kq_fnme_t; |
82 |
|
83 |
TAILQ_HEAD(kq_fnme_head, kq_file_nonify_monitor_entry_s); |
84 |
|
85 |
typedef struct kq_file_nonify_monitor_obj_s { |
86 |
int fd; /* For notify kqueue(). */ |
87 |
int is_dir; |
88 |
int is_local; /* Is file system local. */ |
89 |
int is_removed; /* File/dir deleted, reported and can be only free. */ |
90 |
int is_cached; /* Added to fnmo_cache. */ |
91 |
struct stat sb; |
92 |
char path[(PATH_MAX + sizeof(void*))]; |
93 |
size_t path_size; |
94 |
size_t name_offset; /* Parent path size. */ |
95 |
uint32_t rate_lim_cur_interval; /* From rate_limit_time_init to rate_limit_time_max. 0 disabled. */ |
96 |
size_t rate_lim_ev_cnt; /* Events count then rate_lim_cur_interval != 0 since last report. */ |
97 |
sbintime_t rate_lim_ev_last; /* Last event time. */ |
98 |
void *udata; |
99 |
kq_fnm_p kfnm; |
100 |
struct kq_fnme_head entry_head; |
101 |
/* For dir. */ |
102 |
file_info_p files; |
103 |
volatile size_t files_count; |
104 |
size_t files_allocated; |
105 |
} kq_fnmo_t; |
106 |
|
107 |
|
108 |
typedef struct kq_file_nonify_monitor_s { |
109 |
int fd; /* kqueue() fd. */ |
110 |
int pfd[2]; /* pipe queue specific. */ |
111 |
GHashTable *fnmo_cache; |
112 |
struct statfs *mounts; |
113 |
size_t mounts_count; |
114 |
kfnm_event_handler_cb cb_func; /* Callback on dir/file change. */ |
115 |
kq_file_mon_settings_t s; |
116 |
sbintime_t rate_lim_time_init; /* rate_limit_time_init */ |
117 |
pthread_t tid; |
118 |
} kq_fnm_t; |
119 |
|
120 |
|
121 |
typedef void (*kq_msg_cb)(void *arg); |
122 |
|
123 |
typedef struct kq_file_mon_msg_pkt_s { |
124 |
size_t magic; |
125 |
kq_msg_cb msg_cb; |
126 |
void *arg; |
127 |
size_t chk_sum; |
128 |
} kq_fnm_msg_pkt_t, *kq_fnm_msg_pkt_p; |
129 |
|
130 |
#define KF_MSG_PKT_MAGIC 0xffddaa00 |
131 |
|
132 |
|
133 |
#ifndef O_NOATIME |
134 |
# define O_NOATIME 0 |
135 |
#endif |
136 |
#ifndef O_EVTONLY |
137 |
# define O_EVTONLY O_RDONLY |
138 |
#endif |
139 |
#define OPEN_FILE_FLAGS (O_EVTONLY | O_NONBLOCK | O_NOFOLLOW | O_NOATIME | O_CLOEXEC) |
140 |
|
141 |
#ifndef NOTE_CLOSE_WRITE |
142 |
# define NOTE_CLOSE_WRITE 0 |
143 |
#endif |
144 |
#define EVFILT_VNODE_SUB_FLAGS (NOTE_WRITE | \ |
145 |
NOTE_EXTEND | \ |
146 |
NOTE_ATTRIB | \ |
147 |
NOTE_LINK | \ |
148 |
NOTE_CLOSE_WRITE) |
149 |
|
150 |
#define EVFILT_VNODE_FLAGS_ALL (NOTE_DELETE | \ |
151 |
EVFILT_VNODE_SUB_FLAGS | \ |
152 |
NOTE_RENAME | \ |
153 |
NOTE_REVOKE) |
154 |
|
155 |
#ifndef _GENERIC_DIRSIZ |
156 |
# define _GENERIC_DIRSIZ(__de) MIN((__de)->d_reclen, sizeof(struct dirent)) |
157 |
#endif |
158 |
|
159 |
#define IS_NAME_DOTS(__name) ('.' == (__name)[0] && \ |
160 |
('\0' == (__name)[1] || \ |
161 |
('.' == (__name)[1] && '\0' == (__name)[2]))) |
162 |
#define IS_DE_NAME_EQ(__de1, __de2) (0 == mem_cmpn((__de1)->d_name, \ |
163 |
(__de1)->d_namlen, \ |
164 |
(__de2)->d_name, \ |
165 |
(__de2)->d_namlen)) |
166 |
#define zalloc(__size) calloc(1, (__size)) |
167 |
|
168 |
#if (!defined(HAVE_REALLOCARRAY) && (!defined(__FreeBSD_version) || __FreeBSD_version < 1100000)) |
169 |
# define reallocarray(__mem, __size, __count) realloc((__mem), ((__size) * (__count))) |
170 |
#endif |
171 |
|
172 |
/* To not depend from compiler version. */ |
173 |
#define MSTOSBT(__ms) ((sbintime_t)((((uint64_t)1 << 32) * (uint64_t)(__ms)) / 1000)) |
174 |
|
175 |
#ifndef TAILQ_FOREACH_SAFE |
176 |
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ |
177 |
for ((var) = TAILQ_FIRST((head)); \ |
178 |
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ |
179 |
(var) = (tvar)) |
180 |
#endif |
181 |
|
182 |
#ifndef CLOCK_MONOTONIC_FAST |
183 |
# define CLOCK_MONOTONIC_FAST CLOCK_MONOTONIC |
184 |
#endif |
185 |
|
186 |
|
187 |
void *kq_fnm_proccess_events_proc(void *data); |
188 |
|
189 |
static inline int |
190 |
mem_cmpn(const void *buf1, const size_t buf1_size, |
191 |
const void *buf2, const size_t buf2_size) { |
192 |
|
193 |
if (buf1_size != buf2_size) |
194 |
return (((buf1_size > buf2_size) ? 127 : -127)); |
195 |
if (0 == buf1_size || buf1 == buf2) |
196 |
return (0); |
197 |
if (NULL == buf1) |
198 |
return (-127); |
199 |
if (NULL == buf2) |
200 |
return (127); |
201 |
return (memcmp(buf1, buf2, buf1_size)); |
202 |
} |
203 |
|
204 |
static int |
205 |
realloc_items(void **items, const size_t item_size, |
206 |
size_t *allocated, const size_t alloc_blk_cnt, const size_t count) { |
207 |
size_t allocated_prev, allocated_new; |
208 |
uint8_t *items_new; |
209 |
|
210 |
if (NULL == items || 0 == item_size || NULL == allocated || |
211 |
0 == alloc_blk_cnt) |
212 |
return (EINVAL); |
213 |
allocated_prev = (*allocated); |
214 |
if (NULL != (*items) && |
215 |
allocated_prev > count && |
216 |
allocated_prev <= (count + alloc_blk_cnt)) |
217 |
return (0); |
218 |
allocated_new = (((count / alloc_blk_cnt) + 1) * alloc_blk_cnt); |
219 |
items_new = (uint8_t*)reallocarray((*items), item_size, allocated_new); |
220 |
if (NULL == items_new) /* Realloc fail! */ |
221 |
return (ENOMEM); |
222 |
|
223 |
if (allocated_new > allocated_prev) { /* Init new mem. */ |
224 |
memset((items_new + (allocated_prev * item_size)), 0x00, |
225 |
((allocated_new - allocated_prev) * item_size)); |
226 |
} |
227 |
(*items) = items_new; |
228 |
(*allocated) = allocated_new; |
229 |
|
230 |
return (0); |
231 |
} |
232 |
|
233 |
/* Like getmntinfo() but allow free mem. */ |
234 |
static int |
235 |
getmntinfo_ex(struct statfs **mntbufp, int mode) { |
236 |
int ret; |
237 |
struct statfs *buf; |
238 |
|
239 |
if (NULL == mntbufp) |
240 |
return (EINVAL); |
241 |
/* Request count. */ |
242 |
ret = getfsstat(NULL, 0, mode); |
243 |
if (-1 == ret) |
244 |
return (ret); |
245 |
/* Alloc mem. */ |
246 |
buf = calloc((ret + 1), sizeof(struct statfs)); |
247 |
if (NULL == buf) |
248 |
return (-1); |
249 |
/* Request data. */ |
250 |
ret = getfsstat(buf, ((ret + 1) * sizeof(struct statfs)), mode); |
251 |
if (-1 == ret) { |
252 |
free(buf); |
253 |
buf = NULL; |
254 |
} |
255 |
(*mntbufp) = buf; |
256 |
|
257 |
return (ret); |
258 |
} |
259 |
|
260 |
static int |
261 |
mounts_find_name(struct statfs *mounts, size_t mounts_count, |
262 |
const char *mntonname) { |
263 |
size_t i; |
264 |
|
265 |
if (NULL == mounts || NULL == mntonname) |
266 |
return (0); |
267 |
for (i = 0; i < mounts_count; i ++) { |
268 |
if (0 != strncmp(mntonname, mounts[i].f_mntonname, MNAMELEN)) |
269 |
continue; |
270 |
return (1); |
271 |
} |
272 |
return (0); |
273 |
} |
274 |
|
275 |
|
276 |
static int |
277 |
readdir_start(int fd, struct stat *sb, size_t exp_count, readdir_ctx_p rdd) { |
278 |
size_t buf_size; |
279 |
|
280 |
if (-1 == fd || NULL == sb || NULL == rdd) |
281 |
return (EINVAL); |
282 |
if (-1 == lseek(fd, 0, SEEK_SET)) |
283 |
return (errno); |
284 |
/* Calculate buf size for getdents(). */ |
285 |
buf_size = MAX((size_t)sb->st_size, (exp_count * sizeof(struct dirent))); |
286 |
if (0 == buf_size) { |
287 |
buf_size = (16 * PAGE_SIZE); |
288 |
} |
289 |
/* Make buf size well aligned. */ |
290 |
if (0 != sb->st_blksize) { |
291 |
if (powerof2(sb->st_blksize)) { |
292 |
buf_size = roundup2(buf_size, sb->st_blksize); |
293 |
} else { |
294 |
buf_size = roundup(buf_size, sb->st_blksize); |
295 |
} |
296 |
} else { |
297 |
buf_size = round_page(buf_size); |
298 |
} |
299 |
/* Init. */ |
300 |
memset(rdd, 0x00, sizeof(readdir_ctx_t)); |
301 |
rdd->buf = malloc(buf_size); |
302 |
if (NULL == rdd->buf) |
303 |
return (ENOMEM); |
304 |
rdd->buf_size = buf_size; |
305 |
rdd->fd = fd; |
306 |
|
307 |
return (0); |
308 |
} |
309 |
|
310 |
static void |
311 |
readdir_free(readdir_ctx_p rdd) { |
312 |
|
313 |
if (NULL == rdd || NULL == rdd->buf) |
314 |
return; |
315 |
free(rdd->buf); |
316 |
memset(rdd, 0x00, sizeof(readdir_ctx_t)); |
317 |
} |
318 |
|
319 |
static int |
320 |
readdir_next(readdir_ctx_p rdd, struct dirent *de) { |
321 |
int error = 0; |
322 |
ssize_t ios; |
323 |
uint8_t *ptr; |
324 |
|
325 |
if (NULL == rdd || NULL == rdd->buf || NULL == de) |
326 |
return (EINVAL); |
327 |
|
328 |
for (;;) { |
329 |
if (rdd->buf_used <= rdd->buf_pos) { |
330 |
/* Called once if buf size calculated ok. */ |
331 |
ios = getdents(rdd->fd, (char*)rdd->buf, rdd->buf_size); |
332 |
if (-1 == ios) { |
333 |
error = errno; |
334 |
break; |
335 |
} |
336 |
if (0 == ios) { |
337 |
error = ESPIPE; /* EOF. */ |
338 |
break; |
339 |
} |
340 |
rdd->buf_used = (size_t)ios; |
341 |
rdd->buf_pos = 0; |
342 |
} |
343 |
/* Keep data aligned. */ |
344 |
ptr = (rdd->buf + rdd->buf_pos); |
345 |
memcpy(de, ptr, (sizeof(struct dirent) - sizeof(de->d_name))); |
346 |
if (0 == de->d_reclen) { |
347 |
error = ESPIPE; /* EOF. */ |
348 |
break; |
349 |
} |
350 |
rdd->buf_pos += de->d_reclen; |
351 |
#ifdef DT_WHT |
352 |
if (DT_WHT == de->d_type) |
353 |
continue; |
354 |
#endif |
355 |
if (0 == de->d_namlen) |
356 |
continue; /* Empty. */ |
357 |
memcpy(de, ptr, _GENERIC_DIRSIZ(de)); |
358 |
if (0 == de->d_name[0]) |
359 |
continue; /* Empty. */ |
360 |
if (IS_NAME_DOTS(de->d_name)) |
361 |
continue; /* Dots. */ |
362 |
return (0); /* OK. */ |
363 |
} |
364 |
|
365 |
/* Err or no more files. */ |
366 |
readdir_free(rdd); |
367 |
|
368 |
return (error); |
369 |
} |
370 |
|
371 |
|
372 |
static int |
373 |
file_info_find_ni(file_info_p files, size_t files_count, |
374 |
file_info_p fi, size_t *idx) { |
375 |
size_t i; |
376 |
mode_t st_ftype; |
377 |
|
378 |
if (NULL == files || NULL == fi || NULL == idx) |
379 |
return (0); |
380 |
st_ftype = (S_IFMT & fi->sb.st_mode); |
381 |
for (i = 0; i < files_count; i ++) { |
382 |
if ((S_IFMT & files[i].sb.st_mode) != st_ftype) |
383 |
continue; |
384 |
if ((fi->sb.st_ino != files[i].sb.st_ino || |
385 |
fi->de.d_fileno != files[i].de.d_fileno) && |
386 |
0 == IS_DE_NAME_EQ(&fi->de, &files[i].de)) |
387 |
continue; |
388 |
(*idx) = i; |
389 |
return (1); |
390 |
} |
391 |
(*idx) = files_count; |
392 |
return (0); |
393 |
} |
394 |
|
395 |
static int |
396 |
file_info_find_ino(file_info_p files, size_t files_count, |
397 |
file_info_p fi, size_t *idx) { |
398 |
size_t i; |
399 |
mode_t st_ftype; |
400 |
|
401 |
if (NULL == files || NULL == fi || NULL == idx) |
402 |
return (0); |
403 |
st_ftype = (S_IFMT & fi->sb.st_mode); |
404 |
for (i = 0; i < files_count; i ++) { |
405 |
if ((S_IFMT & files[i].sb.st_mode) != st_ftype || |
406 |
fi->sb.st_ino != files[i].sb.st_ino || |
407 |
fi->de.d_fileno != files[i].de.d_fileno) |
408 |
continue; |
409 |
(*idx) = i; |
410 |
return (1); |
411 |
} |
412 |
(*idx) = files_count; |
413 |
return (0); |
414 |
} |
415 |
|
416 |
static int |
417 |
file_info_find_name(file_info_p files, size_t files_count, |
418 |
file_info_p fi, size_t *idx) { |
419 |
size_t i; |
420 |
mode_t st_ftype; |
421 |
|
422 |
if (NULL == files || NULL == fi || NULL == idx) |
423 |
return (0); |
424 |
st_ftype = (S_IFMT & fi->sb.st_mode); |
425 |
for (i = 0; i < files_count; i ++) { |
426 |
if ((S_IFMT & files[i].sb.st_mode) != st_ftype || |
427 |
0 == IS_DE_NAME_EQ(&fi->de, &files[i].de)) |
428 |
continue; |
429 |
(*idx) = i; |
430 |
return (1); |
431 |
} |
432 |
(*idx) = files_count; |
433 |
return (0); |
434 |
} |
435 |
|
436 |
static void |
437 |
file_info_fd_close(file_info_p files, size_t files_count) { |
438 |
size_t i; |
439 |
|
440 |
if (NULL == files || 0 == files_count) |
441 |
return; |
442 |
for (i = 0; i < files_count; i ++) { |
443 |
if (-1 == files[i].fd) |
444 |
continue; |
445 |
close(files[i].fd); |
446 |
files[i].fd = -1; |
447 |
} |
448 |
} |
449 |
|
450 |
|
451 |
static int |
452 |
is_fs_local(struct statfs *stfs, const char **local_fs, const char **non_local_fs) { |
453 |
size_t i; |
454 |
|
455 |
if (NULL == stfs) |
456 |
return (0); |
457 |
/* White listed fs. */ |
458 |
if (NULL != local_fs) { |
459 |
for (i = 0; NULL != local_fs[i]; i ++) { |
460 |
if (0 == strncmp(stfs->f_fstypename, local_fs[i], |
461 |
sizeof(stfs->f_fstypename))) |
462 |
return (1); |
463 |
} |
464 |
} |
465 |
if (0 == (MNT_LOCAL & stfs->f_flags)) |
466 |
return (0); |
467 |
/* Filter out black listed fs. */ |
468 |
if (NULL != non_local_fs) { |
469 |
for (i = 0; NULL != non_local_fs[i]; i ++) { |
470 |
if (0 == strncmp(stfs->f_fstypename, non_local_fs[i], |
471 |
sizeof(stfs->f_fstypename))) |
472 |
return (0); |
473 |
} |
474 |
} |
475 |
return (1); |
476 |
} |
477 |
|
478 |
|
479 |
static void |
480 |
kq_fnmo_rate_lim_stop(kq_fnmo_p fnmo) { |
481 |
struct kevent kev; |
482 |
|
483 |
if (NULL == fnmo || 0 == fnmo->rate_lim_cur_interval) |
484 |
return; |
485 |
fnmo->rate_lim_cur_interval = 0; |
486 |
fnmo->rate_lim_ev_cnt = 0; |
487 |
EV_SET(&kev, (uintptr_t)fnmo, EVFILT_TIMER, |
488 |
(EV_DELETE | EV_CLEAR), 0, 0, NULL); |
489 |
kevent(fnmo->kfnm->fd, &kev, 1, NULL, 0, NULL); |
490 |
} |
491 |
|
492 |
static int |
493 |
kq_fnmo_rate_lim_shedule_next(kq_fnmo_p fnmo) { |
494 |
u_short flags = (EV_ADD | EV_CLEAR | EV_ONESHOT); |
495 |
struct kevent kev; |
496 |
|
497 |
if (NULL == fnmo || -1 == fnmo->fd || |
498 |
0 == fnmo->kfnm->s.rate_limit_time_init) |
499 |
return (EINVAL); |
500 |
if (0 == fnmo->rate_lim_cur_interval) { /* First call. */ |
501 |
fnmo->rate_lim_cur_interval = fnmo->kfnm->s.rate_limit_time_init; |
502 |
} else { |
503 |
if (fnmo->rate_lim_cur_interval == fnmo->kfnm->s.rate_limit_time_max) |
504 |
return (0); /* No need to modify timer. */ |
505 |
/* Increase rate limit interval. */ |
506 |
fnmo->rate_lim_cur_interval *= fnmo->kfnm->s.rate_limit_time_mul; |
507 |
} |
508 |
if (fnmo->rate_lim_cur_interval >= fnmo->kfnm->s.rate_limit_time_max) { |
509 |
/* Check upper limit and shedule periodic timer with upper rate limit time. */ |
510 |
flags &= ~EV_ONESHOT; |
511 |
fnmo->rate_lim_cur_interval = fnmo->kfnm->s.rate_limit_time_max; |
512 |
} |
513 |
/* Setup timer. */ |
514 |
EV_SET(&kev, (uintptr_t)fnmo, EVFILT_TIMER, flags, |
515 |
NOTE_MSECONDS, fnmo->rate_lim_cur_interval, fnmo); |
516 |
if (-1 == kevent(fnmo->kfnm->fd, &kev, 1, NULL, 0, NULL)) { |
517 |
fnmo->rate_lim_cur_interval = 0; |
518 |
return (errno); |
519 |
} |
520 |
if (0 != (EV_ERROR & kev.flags)) { |
521 |
fnmo->rate_lim_cur_interval = 0; |
522 |
return ((int)kev.data); |
523 |
} |
524 |
return (0); |
525 |
} |
526 |
|
527 |
/* Return: |
528 |
* 0 for events that not handled |
529 |
* 1 for handled = rate limited |
530 |
* -1 on error. |
531 |
*/ |
532 |
static int |
533 |
kq_fnmo_rate_lim_check(kq_fnmo_p fnmo) { |
534 |
sbintime_t sbt, sbt_now; |
535 |
struct timespec ts; |
536 |
|
537 |
if (NULL == fnmo) |
538 |
return (-1); |
539 |
if (-1 == fnmo->fd || |
540 |
0 == fnmo->kfnm->s.rate_limit_time_init) |
541 |
return (0); |
542 |
if (0 != fnmo->rate_lim_cur_interval) { |
543 |
fnmo->rate_lim_ev_cnt ++; /* Count event, timer is active. */ |
544 |
return (1); |
545 |
} |
546 |
|
547 |
/* Do we need to enable rate limit? */ |
548 |
if (0 != clock_gettime(CLOCK_MONOTONIC_FAST, &ts)) |
549 |
return (-1); |
550 |
sbt_now = tstosbt(ts); |
551 |
sbt = (fnmo->rate_lim_ev_last + fnmo->kfnm->rate_lim_time_init); |
552 |
fnmo->rate_lim_ev_last = sbt_now; |
553 |
if (sbt < sbt_now) /* Events rate to low. */ |
554 |
return (0); |
555 |
/* Try to enable rate limit. */ |
556 |
if (0 != kq_fnmo_rate_lim_shedule_next(fnmo)) |
557 |
return (-1); |
558 |
/* Ok. */ |
559 |
fnmo->rate_lim_ev_cnt ++; |
560 |
|
561 |
return (1); |
562 |
} |
563 |
|
564 |
static void |
565 |
kq_fnmo_clean(kq_fnmo_p fnmo) { |
566 |
|
567 |
if (NULL == fnmo) |
568 |
return; |
569 |
|
570 |
kq_fnmo_rate_lim_stop(fnmo); |
571 |
if (-1 != fnmo->fd) { |
572 |
close(fnmo->fd); |
573 |
fnmo->fd = -1; |
574 |
} |
575 |
fnmo->is_removed ++; |
576 |
|
577 |
/* Stop monitoring files/dirs. */ |
578 |
file_info_fd_close(fnmo->files, fnmo->files_count); |
579 |
free(fnmo->files); |
580 |
fnmo->files = NULL; |
581 |
fnmo->files_count = 0; |
582 |
fnmo->files_allocated = 0; |
583 |
} |
584 |
|
585 |
static void |
586 |
kq_fnmo_free(kq_fnmo_p fnmo) { |
587 |
|
588 |
if (NULL == fnmo) |
589 |
return; |
590 |
|
591 |
kq_fnmo_clean(fnmo); |
592 |
|
593 |
if (0 != fnmo->is_cached && |
594 |
NULL != fnmo->kfnm && |
595 |
g_hash_table_lookup(fnmo->kfnm->fnmo_cache, fnmo->path) == fnmo) { |
596 |
g_hash_table_remove(fnmo->kfnm->fnmo_cache, fnmo->path); |
597 |
} |
598 |
free(fnmo); |
599 |
} |
600 |
|
601 |
static kq_fnmo_p |
602 |
kq_fnmo_alloc(kq_fnm_p kfnm, const char *path, kq_fnme_p fnme) { |
603 |
kq_fnmo_p fnmo; |
604 |
|
605 |
if (NULL == kfnm || NULL == path) |
606 |
return (NULL); |
607 |
fnmo = zalloc(sizeof(kq_fnmo_t)); |
608 |
if (NULL == fnmo) |
609 |
return (NULL); |
610 |
fnmo->fd = -1; |
611 |
/* Remember args. */ |
612 |
fnmo->path_size = strlcpy(fnmo->path, path, PATH_MAX); |
613 |
/* Make sure that no trailing '/'. */ |
614 |
while (1 < fnmo->path_size && '/' == fnmo->path[(fnmo->path_size - 1)]) { |
615 |
fnmo->path_size --; |
616 |
fnmo->path[fnmo->path_size] = 0; |
617 |
} |
618 |
/* Get parent folder name. */ |
619 |
fnmo->name_offset = fnmo->path_size; |
620 |
while (0 < fnmo->name_offset && '/' != fnmo->path[(fnmo->name_offset - 1)]) { |
621 |
fnmo->name_offset --; |
622 |
} |
623 |
fnmo->kfnm = kfnm; |
624 |
TAILQ_INIT(&fnmo->entry_head); |
625 |
if (NULL != fnme) { |
626 |
TAILQ_INSERT_HEAD(&fnmo->entry_head, fnme, next); |
627 |
} |
628 |
|
629 |
return (fnmo); |
630 |
} |
631 |
|
632 |
static void |
633 |
kq_fnmo_cb_func_call(kq_fnmo_p fnmo, uint32_t event, |
634 |
const char *base, const char *filename, const char *new_filename) { |
635 |
kq_fnm_p kfnm; |
636 |
kq_fnme_p fnme; |
637 |
|
638 |
if (NULL == fnmo) |
639 |
return; |
640 |
kfnm = fnmo->kfnm; |
641 |
TAILQ_FOREACH(fnme, &fnmo->entry_head, next) { |
642 |
if (0 == fnme->enabled) /* XXX: try lock here? */ |
643 |
continue; |
644 |
kfnm->cb_func(kfnm, fnme, fnme->udata, event, |
645 |
base, filename, new_filename); |
646 |
} |
647 |
|
648 |
} |
649 |
|
650 |
static int |
651 |
kq_fnmo_readdir(kq_fnmo_p fnmo, size_t exp_count) { |
652 |
int error; |
653 |
struct dirent *de; |
654 |
file_info_p tmfi; |
655 |
readdir_ctx_t rdd; |
656 |
|
657 |
if (NULL == fnmo || -1 == fnmo->fd || 0 == fnmo->is_dir) |
658 |
return (EINVAL); |
659 |
|
660 |
free(fnmo->files); |
661 |
fnmo->files = NULL; |
662 |
fnmo->files_count = 0; |
663 |
fnmo->files_allocated = 0; |
664 |
/* Pre allocate. */ |
665 |
if (0 != realloc_items((void**)&fnmo->files, |
666 |
sizeof(file_info_t), &fnmo->files_allocated, |
667 |
FILES_ALLOC_BLK_SIZE, (exp_count + 1))) |
668 |
return (ENOMEM); |
669 |
|
670 |
error = readdir_start(fnmo->fd, &fnmo->sb, exp_count, &rdd); |
671 |
if (0 != error) |
672 |
return (error); |
673 |
for (;;) { |
674 |
if (0 != realloc_items((void**)&fnmo->files, |
675 |
sizeof(file_info_t), &fnmo->files_allocated, |
676 |
FILES_ALLOC_BLK_SIZE, fnmo->files_count)) { |
677 |
free(fnmo->files); |
678 |
fnmo->files = NULL; |
679 |
fnmo->files_count = 0; |
680 |
fnmo->files_allocated = 0; |
681 |
readdir_free(&rdd); |
682 |
return (ENOMEM); |
683 |
} |
684 |
de = &fnmo->files[fnmo->files_count].de; /* Use short name. */ |
685 |
/* Get file name from folder. */ |
686 |
if (0 != readdir_next(&rdd, de)) |
687 |
break; |
688 |
/* Get file attrs. */ |
689 |
if (0 != fstatat(fnmo->fd, de->d_name, |
690 |
&fnmo->files[fnmo->files_count].sb, |
691 |
AT_SYMLINK_NOFOLLOW)) { |
692 |
if (ENOENT == errno) |
693 |
continue; /* File deleted. */ |
694 |
memset(&fnmo->files[fnmo->files_count].sb, 0x00, |
695 |
sizeof(struct stat)); |
696 |
} |
697 |
fnmo->files[fnmo->files_count].fd = -1; |
698 |
fnmo->files_count ++; |
699 |
} |
700 |
/* Mem compact. */ |
701 |
tmfi = reallocarray(fnmo->files, sizeof(file_info_t), (fnmo->files_count + 1)); |
702 |
if (NULL != tmfi) { /* realloc ok. */ |
703 |
fnmo->files = tmfi; |
704 |
fnmo->files_allocated = (fnmo->files_count + 1); |
705 |
} |
706 |
|
707 |
readdir_free(&rdd); |
708 |
|
709 |
return (0); /* OK. */ |
710 |
} |
711 |
|
712 |
|
713 |
static void |
714 |
kq_fnmo_fi_start(kq_fnmo_p fnmo, file_info_p fi) { |
715 |
struct kevent kev; |
716 |
|
717 |
if (NULL == fnmo || -1 == fnmo->fd || NULL == fi) |
718 |
return; |
719 |
fi->fd = openat(fnmo->fd, fi->de.d_name, OPEN_FILE_FLAGS); |
720 |
if (-1 == fi->fd) |
721 |
return; |
722 |
EV_SET(&kev, fi->fd, EVFILT_VNODE, |
723 |
(EV_ADD | EV_CLEAR), |
724 |
EVFILT_VNODE_SUB_FLAGS, 0, fnmo); |
725 |
kevent(fnmo->kfnm->fd, &kev, 1, NULL, 0, NULL); |
726 |
} |
727 |
|
728 |
static int |
729 |
kq_fnmo_is_fi_monitored(kq_fnmo_p fnmo, file_info_p fi) { |
730 |
|
731 |
if (NULL == fnmo) |
732 |
return (0); |
733 |
if (0 == fnmo->is_local || |
734 |
(0 != fnmo->kfnm->s.max_dir_files && |
735 |
fnmo->kfnm->s.max_dir_files < fnmo->files_count)) |
736 |
return (0); |
737 |
if (NULL != fi && |
738 |
0 == fnmo->kfnm->s.mon_local_subdirs && |
739 |
S_ISDIR(fi->sb.st_mode)) |
740 |
return (0); |
741 |
return (1); |
742 |
} |
743 |
|
744 |
static int |
745 |
kq_fnmo_reopen_fd(kq_fnmo_p fnmo) { |
746 |
int error, fd; |
747 |
struct statfs stfs; |
748 |
struct kevent kev; |
749 |
|
750 |
if (NULL == fnmo) |
751 |
return (EINVAL); |
752 |
|
753 |
/* Close fd and stop timer. */ |
754 |
kq_fnmo_rate_lim_stop(fnmo); |
755 |
if (-1 != fnmo->fd) { |
756 |
close(fnmo->fd); |
757 |
fnmo->fd = -1; |
758 |
} |
759 |
|
760 |
fd = open(fnmo->path, OPEN_FILE_FLAGS); |
761 |
if (-1 == fd) |
762 |
return (errno); |
763 |
if (0 != fstat(fd, &fnmo->sb)) { |
764 |
error = errno; |
765 |
goto err_out; |
766 |
} |
767 |
if (0 == fnmo->sb.st_nlink) { |
768 |
error = ENOENT; |
769 |
goto err_out; |
770 |
} |
771 |
if (S_ISDIR(fnmo->sb.st_mode)) { |
772 |
fnmo->is_dir = 1; |
773 |
/* Is file system local? */ |
774 |
if (0 != fnmo->kfnm->s.mon_local_subfiles && |
775 |
0 == fstatfs(fd, &stfs)) { |
776 |
fnmo->is_local = is_fs_local(&stfs, fnmo->kfnm->s.local_fs, |
777 |
fnmo->kfnm->s.non_local_fs); |
778 |
} |
779 |
} |
780 |
|
781 |
/* Add to kqueue. */ |
782 |
EV_SET(&kev, fd, EVFILT_VNODE, (EV_ADD | EV_CLEAR), |
783 |
EVFILT_VNODE_FLAGS_ALL, 0, fnmo); |
784 |
if (-1 == kevent(fnmo->kfnm->fd, &kev, 1, NULL, 0, NULL) || |
785 |
0 != (EV_ERROR & kev.flags)) { |
786 |
error = errno; |
787 |
goto err_out; |
788 |
} |
789 |
|
790 |
fnmo->is_removed = 0; |
791 |
fnmo->fd = fd; |
792 |
|
793 |
return (0); /* OK. */ |
794 |
|
795 |
err_out: |
796 |
close(fd); |
797 |
return (error); |
798 |
} |
799 |
|
800 |
static int |
801 |
kq_fnmo_init(kq_fnmo_p fnmo) { |
802 |
int error; |
803 |
size_t i; |
804 |
|
805 |
if (NULL == fnmo) |
806 |
return (EINVAL); |
807 |
|
808 |
error = kq_fnmo_reopen_fd(fnmo); |
809 |
if (0 != error) |
810 |
goto err_out; |
811 |
|
812 |
if (0 == fnmo->is_dir) |
813 |
return (0); /* OK. */ |
814 |
/* Dir special processing. */ |
815 |
|
816 |
/* Read and remember dir content. */ |
817 |
error = kq_fnmo_readdir(fnmo, 0); |
818 |
if (0 != error) |
819 |
goto err_out; |
820 |
/* Add monitor sub files/dirs, ignory errors. */ |
821 |
/* Check twice for performance reason. */ |
822 |
if (0 != kq_fnmo_is_fi_monitored(fnmo, NULL)) { |
823 |
for (i = 0; i < fnmo->files_count; i ++) { |
824 |
if (0 != kq_fnmo_is_fi_monitored(fnmo, &fnmo->files[i])) { |
825 |
kq_fnmo_fi_start(fnmo, &fnmo->files[i]); |
826 |
} |
827 |
} |
828 |
} |
829 |
|
830 |
return (0); /* OK. */ |
831 |
|
832 |
err_out: |
833 |
kq_fnmo_clean(fnmo); |
834 |
return (error); |
835 |
} |
836 |
|
837 |
static void |
838 |
kq_fnme_init_cb(void *arg) { |
839 |
kq_fnme_p fnme = arg; |
840 |
kq_fnmo_p fnmo; |
841 |
kq_fnm_p kfnm; |
842 |
|
843 |
if (NULL == fnme || NULL == fnme->fnmo) |
844 |
return; |
845 |
kfnm = fnme->fnmo->kfnm; |
846 |
/* Is in cache? */ |
847 |
fnmo = g_hash_table_lookup(kfnm->fnmo_cache, fnme->fnmo->path); |
848 |
if (NULL == fnmo) { |
849 |
/* Init and add to cache. */ |
850 |
g_hash_table_insert(kfnm->fnmo_cache, |
851 |
fnme->fnmo->path, fnme->fnmo); |
852 |
fnme->fnmo->is_cached ++; |
853 |
kq_fnmo_init(fnme->fnmo); |
854 |
return; |
855 |
} |
856 |
/* Found in cache, use it. */ |
857 |
TAILQ_REMOVE(&fnme->fnmo->entry_head, fnme, next); |
858 |
kq_fnmo_free(fnme->fnmo); |
859 |
fnme->fnmo = fnmo; |
860 |
TAILQ_INSERT_HEAD(&fnmo->entry_head, fnme, next); |
861 |
if (-1 == fnmo->fd) { |
862 |
/* Re init existing, no notify. */ |
863 |
kq_fnmo_init(fnme->fnmo); |
864 |
} |
865 |
} |
866 |
|
867 |
static void |
868 |
kq_fnme_free(kq_fnme_p fnme) { |
869 |
|
870 |
if (NULL == fnme) |
871 |
return; |
872 |
if (NULL != fnme->fnmo) { |
873 |
TAILQ_REMOVE(&fnme->fnmo->entry_head, fnme, next); |
874 |
if (TAILQ_EMPTY(&fnme->fnmo->entry_head)) { |
875 |
kq_fnmo_free(fnme->fnmo); |
876 |
} |
877 |
} |
878 |
free(fnme); |
879 |
} |
880 |
|
881 |
static void |
882 |
kq_fnme_free_cb(void *arg) { |
883 |
|
884 |
kq_fnme_free((kq_fnme_p)arg); |
885 |
} |
886 |
|
887 |
|
888 |
static void |
889 |
kq_handle_notify_removed(kq_fnmo_p fnmo) { |
890 |
size_t i; |
891 |
|
892 |
if (NULL == fnmo || 0 != fnmo->is_removed) |
893 |
return; |
894 |
|
895 |
if (0 != fnmo->is_dir) { |
896 |
/* Notify removed files first. */ |
897 |
for (i = 0; i < fnmo->files_count; i ++) { |
898 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_DELETED, |
899 |
fnmo->path, fnmo->files[i].de.d_name, NULL); |
900 |
} |
901 |
} |
902 |
fnmo->path[fnmo->name_offset - 1] = 0; |
903 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_DELETED, fnmo->path, |
904 |
(fnmo->path + fnmo->name_offset), NULL); |
905 |
fnmo->path[fnmo->name_offset - 1] = '/'; |
906 |
kq_fnmo_clean(fnmo); |
907 |
} |
908 |
|
909 |
static void |
910 |
kq_handle_changes(kq_fnmo_p fnmo) { |
911 |
size_t i, k, files_count; |
912 |
file_info_p files; |
913 |
|
914 |
if (NULL == fnmo || -1 == fnmo->fd) |
915 |
return; |
916 |
if (0 != fstat(fnmo->fd, &fnmo->sb) || |
917 |
0 == fnmo->sb.st_nlink) { |
918 |
kq_handle_notify_removed(fnmo); |
919 |
return; |
920 |
} |
921 |
if (0 == fnmo->is_dir) { |
922 |
fnmo->path[fnmo->name_offset - 1] = 0; |
923 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_CHANGED, fnmo->path, |
924 |
(fnmo->path + fnmo->name_offset), NULL); |
925 |
fnmo->path[fnmo->name_offset - 1] = '/'; |
926 |
return; |
927 |
} |
928 |
|
929 |
/* Dir processing. */ |
930 |
|
931 |
/* Save prev. */ |
932 |
files = fnmo->files; |
933 |
files_count = fnmo->files_count; |
934 |
fnmo->files = NULL; |
935 |
fnmo->files_count = 0; |
936 |
/* Update dir. */ |
937 |
if (0 != kq_fnmo_readdir(fnmo, files_count)) { |
938 |
/* Restore prev state on fail. */ |
939 |
fnmo->files = files; |
940 |
fnmo->files_count = files_count; |
941 |
return; |
942 |
} |
943 |
|
944 |
/* Notify removed first. */ |
945 |
for (i = 0; i < files_count; i ++) { |
946 |
if (0 != file_info_find_ni(fnmo->files, fnmo->files_count, |
947 |
&files[i], &k)) /* Deleted? */ |
948 |
continue; |
949 |
if (-1 != files[i].fd) { |
950 |
close(files[i].fd); |
951 |
files[i].fd = -1; |
952 |
} |
953 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_DELETED, fnmo->path, |
954 |
files[i].de.d_name, NULL); |
955 |
} |
956 |
/* Notify. */ |
957 |
for (i = 0; i < fnmo->files_count; i ++) { |
958 |
/* Is new file/folder? */ |
959 |
if (0 == file_info_find_ino(files, files_count, &fnmo->files[i], &k) && |
960 |
0 == file_info_find_name(files, files_count, &fnmo->files[i], &k)) { /* Add new. */ |
961 |
/* Add monitor sub files/dirs, ignory errors. */ |
962 |
if (0 != kq_fnmo_is_fi_monitored(fnmo, &fnmo->files[i])) { |
963 |
kq_fnmo_fi_start(fnmo, &fnmo->files[i]); |
964 |
} |
965 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_CREATED, |
966 |
fnmo->path, fnmo->files[i].de.d_name, NULL); |
967 |
continue; |
968 |
} |
969 |
/* Keep file fd. */ |
970 |
fnmo->files[i].fd = files[k].fd; |
971 |
files[k].fd = -1; |
972 |
/* Is renamed? */ |
973 |
if (0 == IS_DE_NAME_EQ(&files[k].de, &fnmo->files[i].de)) { |
974 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_RENAMED, |
975 |
fnmo->path, files[k].de.d_name, |
976 |
fnmo->files[i].de.d_name); |
977 |
continue; |
978 |
} |
979 |
/* Is modified? */ |
980 |
if (0 != memcmp(&fnmo->files[i].sb, &files[k].sb, sizeof(struct stat))) { |
981 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_CHANGED, |
982 |
fnmo->path, fnmo->files[i].de.d_name, NULL); |
983 |
continue; |
984 |
} |
985 |
/* Not changed. */ |
986 |
} |
987 |
|
988 |
/* Prevent FD leak die to race conditions. |
989 |
* All fd must be -1, check this while debuging. |
990 |
*/ |
991 |
file_info_fd_close(files, files_count); |
992 |
free(files); |
993 |
} |
994 |
|
995 |
static int |
996 |
kq_handle_rename(kq_fnmo_p fnmo) { |
997 |
int error, up_dir_fd, found = 0; |
998 |
readdir_ctx_t rdd; |
999 |
struct dirent de; |
1000 |
struct stat sb; |
1001 |
char old_filename[(MAXNAMLEN + sizeof(void*))]; |
1002 |
size_t old_filename_size; |
1003 |
|
1004 |
if (NULL == fnmo || -1 == fnmo->fd) |
1005 |
return (EINVAL); |
1006 |
if (0 != fstat(fnmo->fd, &fnmo->sb) || |
1007 |
0 == fnmo->sb.st_nlink) { |
1008 |
notify_removed_errno: |
1009 |
error = errno; |
1010 |
notify_removed: |
1011 |
kq_handle_notify_removed(fnmo); |
1012 |
return (error); |
1013 |
} |
1014 |
/* Save old file name. */ |
1015 |
old_filename_size = (fnmo->path_size - fnmo->name_offset); |
1016 |
memcpy(old_filename, |
1017 |
(fnmo->path + fnmo->name_offset), |
1018 |
old_filename_size); |
1019 |
old_filename[old_filename_size] = 0; |
1020 |
|
1021 |
/* Get parent folder name. */ |
1022 |
fnmo->path[fnmo->name_offset] = 0; |
1023 |
/* Try to open. */ |
1024 |
up_dir_fd = open(fnmo->path, (OPEN_FILE_FLAGS | O_DIRECTORY)); |
1025 |
/* Restore '/' after parent folder. */ |
1026 |
fnmo->path[fnmo->name_offset] = '/'; |
1027 |
if (-1 == up_dir_fd) |
1028 |
goto notify_removed_errno; |
1029 |
if (0 != fstat(up_dir_fd, &sb)) { |
1030 |
close(up_dir_fd); |
1031 |
goto notify_removed_errno; |
1032 |
} |
1033 |
error = readdir_start(up_dir_fd, &sb, 0, &rdd); |
1034 |
if (0 != error) { |
1035 |
close(up_dir_fd); |
1036 |
goto notify_removed; |
1037 |
} |
1038 |
/* Find new name by inode. */ |
1039 |
while (0 == readdir_next(&rdd, &de)) { |
1040 |
if (0 == fstatat(up_dir_fd, de.d_name, &sb, AT_SYMLINK_NOFOLLOW) && |
1041 |
0 == memcmp(&fnmo->sb, &sb, sizeof(struct stat))) { |
1042 |
found ++; |
1043 |
break; |
1044 |
} |
1045 |
} |
1046 |
close(up_dir_fd); |
1047 |
if (0 == found) { |
1048 |
error = ENOENT; |
1049 |
goto notify_removed; /* Not found. */ |
1050 |
} |
1051 |
/* Update name. */ |
1052 |
if (PATH_MAX < (fnmo->name_offset + de.d_namlen)) { |
1053 |
error = EINVAL; |
1054 |
goto notify_removed; |
1055 |
} |
1056 |
memcpy((fnmo->path + fnmo->name_offset), de.d_name, de.d_namlen); |
1057 |
fnmo->path_size = (fnmo->name_offset + de.d_namlen); |
1058 |
fnmo->path[fnmo->path_size] = 0; |
1059 |
/* Notify. */ |
1060 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_RENAMED, fnmo->path, |
1061 |
old_filename, de.d_name); |
1062 |
|
1063 |
return (0); |
1064 |
} |
1065 |
|
1066 |
|
1067 |
static void |
1068 |
kq_fnm_delay_call_process(kq_fnm_p kfnm, kq_msg_cb forced_msg_cb) { |
1069 |
ssize_t ios; |
1070 |
kq_fnm_msg_pkt_t msg; |
1071 |
|
1072 |
for (;;) { |
1073 |
ios = read(kfnm->pfd[0], &msg, sizeof(msg)); |
1074 |
if (0 >= ios) |
1075 |
return; |
1076 |
if (sizeof(msg) != ios || |
1077 |
KF_MSG_PKT_MAGIC != msg.magic || |
1078 |
(((size_t)msg.msg_cb) ^ ((size_t)msg.arg)) != msg.chk_sum) |
1079 |
continue; |
1080 |
if (NULL != forced_msg_cb) { |
1081 |
forced_msg_cb(msg.arg); |
1082 |
continue; |
1083 |
} |
1084 |
if (NULL == msg.msg_cb) |
1085 |
continue; |
1086 |
msg.msg_cb(msg.arg); |
1087 |
} |
1088 |
} |
1089 |
|
1090 |
static int |
1091 |
kq_fnm_delay_call(kq_fnm_p kfnm, kq_msg_cb msg_cb, |
1092 |
void *arg) { |
1093 |
kq_fnm_msg_pkt_t msg; |
1094 |
|
1095 |
if (NULL == kfnm || NULL == arg) |
1096 |
return (EINVAL); |
1097 |
msg.magic = KF_MSG_PKT_MAGIC; |
1098 |
msg.msg_cb = msg_cb; |
1099 |
msg.arg = arg; |
1100 |
msg.chk_sum = (((size_t)msg.msg_cb) ^ ((size_t)msg.arg)); |
1101 |
if (sizeof(msg) == write(kfnm->pfd[1], &msg, sizeof(msg))) |
1102 |
return (0); |
1103 |
return (errno); |
1104 |
} |
1105 |
|
1106 |
|
1107 |
static void |
1108 |
kq_fnm_free_cb(void *arg) { |
1109 |
kq_fnm_p kfnm = arg; |
1110 |
|
1111 |
if (NULL == kfnm) |
1112 |
return; |
1113 |
close(kfnm->fd); |
1114 |
kfnm->fd = -1; |
1115 |
} |
1116 |
void |
1117 |
kq_fnm_free(kq_fnm_p kfnm) { |
1118 |
GHashTableIter iter; |
1119 |
gpointer key; |
1120 |
kq_fnmo_p fnmo; |
1121 |
kq_fnme_p fnme, fnme_temp; |
1122 |
|
1123 |
if (NULL == kfnm) |
1124 |
return; |
1125 |
kq_fnm_delay_call(kfnm, kq_fnm_free_cb, kfnm); |
1126 |
pthread_join(kfnm->tid, NULL); |
1127 |
/* Free all in delay calls queue. */ |
1128 |
close(kfnm->pfd[1]); |
1129 |
kq_fnm_delay_call_process(kfnm, kq_fnme_free_cb); |
1130 |
close(kfnm->pfd[0]); |
1131 |
/* Remove all objects. */ |
1132 |
g_hash_table_iter_init(&iter, kfnm->fnmo_cache); |
1133 |
while (g_hash_table_iter_next(&iter, &key, (gpointer)&fnmo)) { |
1134 |
TAILQ_FOREACH_SAFE(fnme, &fnmo->entry_head, next, fnme_temp) { |
1135 |
TAILQ_REMOVE(&fnmo->entry_head, fnme, next); |
1136 |
fnme->fnmo = NULL; /* Do not free fnmo here. */ |
1137 |
kq_fnme_free(fnme); |
1138 |
} |
1139 |
g_hash_table_iter_remove(&iter); /* Remove from cache here. */ |
1140 |
/* Prevent remove from cache or g_hash_table_iter_next() will fail. */ |
1141 |
fnmo->is_cached = 0; |
1142 |
kq_fnmo_free(fnmo); |
1143 |
} |
1144 |
g_hash_table_destroy(kfnm->fnmo_cache); |
1145 |
|
1146 |
free(kfnm->mounts); |
1147 |
free(kfnm); |
1148 |
} |
1149 |
|
1150 |
kq_fnm_p |
1151 |
kq_fnm_create(kq_file_mon_settings_p s, kfnm_event_handler_cb cb_func) { |
1152 |
kq_fnm_p kfnm; |
1153 |
struct kevent kev; |
1154 |
|
1155 |
if (NULL == s || NULL == cb_func) |
1156 |
return (NULL); |
1157 |
kfnm = zalloc(sizeof(kq_fnm_t)); |
1158 |
if (NULL == kfnm) |
1159 |
return (NULL); |
1160 |
kfnm->fd = kqueue(); |
1161 |
if (-1 == kfnm->fd) |
1162 |
goto err_out; |
1163 |
if (-1 == pipe2(kfnm->pfd, O_NONBLOCK)) |
1164 |
goto err_out; |
1165 |
kfnm->fnmo_cache = g_hash_table_new(g_str_hash, g_str_equal); |
1166 |
kfnm->cb_func = cb_func; |
1167 |
memcpy(&kfnm->s, s, sizeof(kq_file_mon_settings_t)); |
1168 |
if (kfnm->s.rate_limit_time_init >= kfnm->s.rate_limit_time_max) { |
1169 |
kfnm->s.rate_limit_time_max = kfnm->s.rate_limit_time_init; |
1170 |
} |
1171 |
if (0 == kfnm->s.rate_limit_time_mul) { |
1172 |
kfnm->s.rate_limit_time_mul ++; |
1173 |
} |
1174 |
kfnm->rate_lim_time_init = MSTOSBT(kfnm->s.rate_limit_time_init); |
1175 |
|
1176 |
EV_SET(&kev, kfnm->pfd[0], EVFILT_READ, EV_ADD, 0, 0, NULL); |
1177 |
if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, NULL) || |
1178 |
0 != (EV_ERROR & kev.flags)) |
1179 |
goto err_out; |
1180 |
EV_SET(&kev, 0, EVFILT_FS, EV_ADD, 0, 0, NULL); |
1181 |
if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, NULL) || |
1182 |
0 != (EV_ERROR & kev.flags)) |
1183 |
goto err_out; |
1184 |
/* Get initial mounts points. */ |
1185 |
kfnm->mounts_count = (size_t)getmntinfo_ex(&kfnm->mounts, MNT_WAIT); |
1186 |
|
1187 |
if (0 != pthread_create(&kfnm->tid, NULL, |
1188 |
kq_fnm_proccess_events_proc, kfnm)) |
1189 |
goto err_out; |
1190 |
|
1191 |
return (kfnm); |
1192 |
|
1193 |
err_out: |
1194 |
kq_fnm_free(kfnm); |
1195 |
return (NULL); |
1196 |
} |
1197 |
|
1198 |
kq_fnme_p |
1199 |
kq_fnm_add(kq_fnm_p kfnm, const char *path, void *udata) { |
1200 |
int error; |
1201 |
kq_fnme_p fnme; |
1202 |
|
1203 |
if (NULL == kfnm || NULL == path) |
1204 |
return (NULL); |
1205 |
fnme = zalloc(sizeof(kq_fnme_t)); |
1206 |
if (NULL == fnme) |
1207 |
return (NULL); |
1208 |
fnme->fnmo = kq_fnmo_alloc(kfnm, path, fnme); |
1209 |
if (NULL == fnme->fnmo) |
1210 |
goto err_out; |
1211 |
fnme->udata = udata; |
1212 |
fnme->enabled = 1; |
1213 |
/* Shedule delay call to init. */ |
1214 |
error = kq_fnm_delay_call(kfnm, kq_fnme_init_cb, fnme); |
1215 |
if (0 != error) { /* Error, do no directly init to avoid freezes. */ |
1216 |
kq_fnmo_free(fnme->fnmo); |
1217 |
err_out: |
1218 |
fnme->fnmo = NULL; |
1219 |
kq_fnme_free(fnme); |
1220 |
return (NULL); |
1221 |
} |
1222 |
return (fnme); |
1223 |
} |
1224 |
|
1225 |
void |
1226 |
kq_fnm_del(kq_fnm_p kfnm, kq_fnme_p fnme) { |
1227 |
int error; |
1228 |
|
1229 |
if (NULL == kfnm || NULL == fnme) |
1230 |
return; |
1231 |
/* Cancel notifications. */ |
1232 |
/* XXX: lock here? */ |
1233 |
fnme->enabled = 0; |
1234 |
/* Shedule delay call to free. */ |
1235 |
error = kq_fnm_delay_call(kfnm, kq_fnme_free_cb, fnme); |
1236 |
if (0 == error) |
1237 |
return; |
1238 |
/* Error, free directly. */ |
1239 |
kq_fnme_free(fnme); |
1240 |
} |
1241 |
|
1242 |
|
1243 |
static void |
1244 |
kq_fnm_handle_mnt_point_change(kq_fnm_p kfnm, const char *mntonname) { |
1245 |
int error; |
1246 |
GHashTableIter iter; |
1247 |
gpointer key; |
1248 |
kq_fnmo_p fnmo; |
1249 |
size_t mntonname_size; |
1250 |
|
1251 |
if (NULL == kfnm || NULL == mntonname || 0 == mntonname[0]) |
1252 |
return; |
1253 |
mntonname_size = strnlen(mntonname, MNAMELEN); |
1254 |
/* Remove all objects. */ |
1255 |
g_hash_table_iter_init(&iter, kfnm->fnmo_cache); |
1256 |
while (g_hash_table_iter_next(&iter, &key, (gpointer)&fnmo)) { |
1257 |
if (NULL == fnmo || -1 == fnmo->fd) /* Do not reopen deleted. */ |
1258 |
continue; |
1259 |
if (mntonname_size > fnmo->path_size) |
1260 |
continue; |
1261 |
if (0 != memcmp(fnmo->path, mntonname, mntonname_size)) |
1262 |
continue; |
1263 |
/* Try to reopen mount point after unmount. */ |
1264 |
error = kq_fnmo_reopen_fd(fnmo); |
1265 |
if (0 != error) { |
1266 |
kq_handle_notify_removed(fnmo); |
1267 |
continue; |
1268 |
} |
1269 |
/* Reread dir and notify about deleted and new files. */ |
1270 |
kq_handle_changes(fnmo); |
1271 |
} |
1272 |
} |
1273 |
static void |
1274 |
kq_handle_mount_changes(kq_fnm_p kfnm) { |
1275 |
size_t i, mounts_count; |
1276 |
struct statfs *mounts; |
1277 |
|
1278 |
if (NULL == kfnm) |
1279 |
return; |
1280 |
|
1281 |
/* Save prev. */ |
1282 |
mounts = kfnm->mounts; |
1283 |
mounts_count = kfnm->mounts_count; |
1284 |
kfnm->mounts = NULL; |
1285 |
kfnm->mounts_count = (size_t)getmntinfo_ex(&kfnm->mounts, MNT_WAIT); |
1286 |
|
1287 |
/* Removed mounts. */ |
1288 |
for (i = 0; i < mounts_count; i ++) { |
1289 |
if (0 != mounts_find_name(kfnm->mounts, kfnm->mounts_count, |
1290 |
mounts[i].f_mntonname)) |
1291 |
continue; |
1292 |
kq_fnm_handle_mnt_point_change(kfnm, mounts[i].f_mntonname); |
1293 |
} |
1294 |
/* New mounts. */ |
1295 |
for (i = 0; i < kfnm->mounts_count; i ++) { |
1296 |
if (0 != mounts_find_name(mounts, mounts_count, |
1297 |
kfnm->mounts[i].f_mntonname)) |
1298 |
continue; |
1299 |
kq_fnm_handle_mnt_point_change(kfnm, kfnm->mounts[i].f_mntonname); |
1300 |
} |
1301 |
|
1302 |
/* Free old mounts. */ |
1303 |
free(mounts); |
1304 |
} |
1305 |
|
1306 |
|
1307 |
static void |
1308 |
kq_fnm_proccess_event(kq_fnm_p kfnm, struct kevent *kev) { |
1309 |
int error; |
1310 |
kq_fnmo_p fnmo; |
1311 |
file_info_p fi; |
1312 |
size_t i; |
1313 |
int is_rate_lim_checked = 0; |
1314 |
struct stat sb; |
1315 |
|
1316 |
if (NULL == kfnm || NULL == kev) |
1317 |
return; |
1318 |
|
1319 |
/* Handle delay calls. */ |
1320 |
if (kev->ident == (uintptr_t)kfnm->pfd[0]) { |
1321 |
if (EVFILT_READ == kev->filter) { |
1322 |
kq_fnm_delay_call_process(kfnm, NULL); |
1323 |
} |
1324 |
return; |
1325 |
} |
1326 |
|
1327 |
/* Handle mount/unmount events. */ |
1328 |
if (EVFILT_FS == kev->filter) { |
1329 |
kq_handle_mount_changes(kfnm); |
1330 |
return; |
1331 |
} |
1332 |
|
1333 |
if (0 == kev->udata) |
1334 |
return; /* No associated data, skip. */ |
1335 |
fnmo = (kq_fnmo_p)kev->udata; |
1336 |
|
1337 |
/* FS delayed notifications. */ |
1338 |
if (EVFILT_TIMER == kev->filter) { |
1339 |
if (0 == fnmo->rate_lim_ev_cnt) { |
1340 |
/* No delayed events, disable rate limit polling. */ |
1341 |
kq_fnmo_rate_lim_stop(fnmo); |
1342 |
return; |
1343 |
} |
1344 |
fnmo->rate_lim_ev_cnt = 0; /* Reset counter. */ |
1345 |
kq_fnmo_rate_lim_shedule_next(fnmo); |
1346 |
kq_handle_changes(fnmo); |
1347 |
return; |
1348 |
} |
1349 |
|
1350 |
/* FS notifications. */ |
1351 |
if (EVFILT_VNODE != kev->filter) |
1352 |
return; /* Unknown event, skip. */ |
1353 |
/* Subdir/file */ |
1354 |
if (kev->ident != (uintptr_t)fnmo->fd) { |
1355 |
/* Is files changes rate limited? */ |
1356 |
if (1 == kq_fnmo_rate_lim_check(fnmo)) |
1357 |
return; |
1358 |
is_rate_lim_checked ++; |
1359 |
/* Try to find file and check it, without call kq_handle_changes(). */ |
1360 |
fi = NULL; |
1361 |
for (i = 0; i < fnmo->files_count; i ++) { |
1362 |
if (kev->ident != (uintptr_t)fnmo->files[i].fd) |
1363 |
continue; |
1364 |
fi = &fnmo->files[i]; |
1365 |
break; |
1366 |
} |
1367 |
if (NULL != fi) { |
1368 |
/* Get file attrs. */ |
1369 |
if (0 != fstat(fi->fd, &sb)) { |
1370 |
memset(&sb, 0x00, sizeof(struct stat)); |
1371 |
} |
1372 |
/* Is modified? */ |
1373 |
if (0 != memcmp(&fi->sb, &sb, sizeof(struct stat))) { |
1374 |
memcpy(&fi->sb, &sb, sizeof(struct stat)); |
1375 |
kq_fnmo_cb_func_call(fnmo, KF_EVENT_CHANGED, |
1376 |
fnmo->path, fi->de.d_name, NULL); |
1377 |
return; |
1378 |
} |
1379 |
} |
1380 |
/* fd not found or changes not found, rescan dir. */ |
1381 |
kev->fflags = NOTE_WRITE; |
1382 |
} |
1383 |
/* Monitored object. */ |
1384 |
/* All flags from EVFILT_VNODE_FLAGS_ALL must be handled here. */ |
1385 |
if (EV_ERROR & kev->flags) { |
1386 |
kev->fflags |= NOTE_REVOKE; /* Treat error as unmount. */ |
1387 |
} |
1388 |
if ((NOTE_DELETE | NOTE_REVOKE) & kev->fflags) { |
1389 |
/* No ratelimit here. */ |
1390 |
if (0 != fnmo->is_dir) { |
1391 |
/* Try to reopen mount point after unmount. */ |
1392 |
error = kq_fnmo_reopen_fd(fnmo); |
1393 |
if (0 == error) { |
1394 |
/* This will reread dir and notify about |
1395 |
* deleted and new files. */ |
1396 |
kq_handle_changes(fnmo); |
1397 |
} |
1398 |
} else { |
1399 |
error = -1; |
1400 |
} |
1401 |
if (0 != error) { |
1402 |
kq_handle_notify_removed(fnmo); |
1403 |
} |
1404 |
return; /* All events processed. */ |
1405 |
} |
1406 |
if (NOTE_RENAME & kev->fflags) { |
1407 |
error = kq_handle_rename(fnmo); |
1408 |
if (0 != error) |
1409 |
return; |
1410 |
} |
1411 |
if ((NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_LINK | NOTE_CLOSE_WRITE) & kev->fflags) { |
1412 |
if (0 == is_rate_lim_checked && |
1413 |
1 != kq_fnmo_rate_lim_check(fnmo)) { |
1414 |
kq_handle_changes(fnmo); |
1415 |
} |
1416 |
} |
1417 |
} |
1418 |
|
1419 |
void * |
1420 |
kq_fnm_proccess_events_proc(void *data) { |
1421 |
struct kevent kev; |
1422 |
kq_fnm_p kfnm = data; |
1423 |
|
1424 |
if (NULL == kfnm) |
1425 |
return (NULL); |
1426 |
/* Get and proccess events, no wait. */ |
1427 |
while (0 < kevent(kfnm->fd, NULL, 0, &kev, 1, NULL)) { |
1428 |
kq_fnm_proccess_event(kfnm, &kev); |
1429 |
} |
1430 |
return (NULL); |
1431 |
} |