Line 0
Link Here
|
|
|
1 |
/*- |
2 |
* Copyright (c) 2016 - 2017 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/stat.h> |
34 |
#include <sys/fcntl.h> /* open, fcntl */ |
35 |
|
36 |
#include <inttypes.h> |
37 |
#include <stdlib.h> /* malloc, exit */ |
38 |
#include <unistd.h> /* close, write, sysconf */ |
39 |
#include <string.h> /* bcopy, bzero, memcpy, memmove, memset, strerror... */ |
40 |
#include <errno.h> |
41 |
#include <dirent.h> // opendir, readdir |
42 |
|
43 |
#include "kqueue_fnm.h" |
44 |
|
45 |
|
46 |
/* Preallocate items count. */ |
47 |
#ifndef FILES_ALLOC_BLK_SIZE |
48 |
# define FILES_ALLOC_BLK_SIZE 8 |
49 |
#endif |
50 |
|
51 |
typedef struct file_info_s { /* Directory file. */ |
52 |
struct dirent de; /* d_reclen used for action. */ |
53 |
struct stat sb; |
54 |
} file_info_t, *file_info_p; |
55 |
|
56 |
|
57 |
typedef struct kqueue_file_mon_data_s { |
58 |
int fd; /* fd for notify kqueue(). */ |
59 |
int is_dir; |
60 |
char path[(PATH_MAX + 2)]; |
61 |
size_t path_size; |
62 |
size_t name_offset; /* Parent path size. */ |
63 |
void *udata; |
64 |
kqueue_fnm_p kfnm; |
65 |
/* For dir. */ |
66 |
file_info_p files; |
67 |
volatile size_t files_count; |
68 |
size_t files_allocated; |
69 |
} kqueue_file_mon_data_t; |
70 |
|
71 |
|
72 |
typedef struct kqueue_file_nonify_monitor_s { |
73 |
int fd; /* kqueue() fd. */ |
74 |
int pfd[2]; /* pipe queue specific. */ |
75 |
} kqueue_fnm_t; |
76 |
|
77 |
|
78 |
typedef void (*kq_msg_cb)(kqueue_file_mon_data_p fmd); |
79 |
|
80 |
typedef struct kqueue_file_mon_msg_pkt_s { |
81 |
size_t magic; |
82 |
kq_msg_cb msg_cb; |
83 |
kqueue_file_mon_data_p fmd; |
84 |
size_t chk_sum; |
85 |
} kqueue_fnm_msg_pkt_t, *kqueue_fnm_msg_pkt_p; |
86 |
|
87 |
#define KF_MSG_PKT_MAGIC 0xffddaa00 |
88 |
#define KF_MSG_PKT_CHK_SUM_SET(__msg_pkt) \ |
89 |
(__msg_pkt)->chk_sum = (((size_t)(__msg_pkt)->msg_cb) ^ ((size_t)(__msg_pkt)->fmd)) |
90 |
#define KF_MSG_PKT_IS_VALID(__msg_pkt) \ |
91 |
(KF_MSG_PKT_MAGIC == (__msg_pkt)->magic && \ |
92 |
(((size_t)(__msg_pkt)->msg_cb) ^ ((size_t)(__msg_pkt)->fmd)) == (__msg_pkt)->chk_sum) |
93 |
|
94 |
|
95 |
#ifndef O_NOATIME |
96 |
# define O_NOATIME 0 |
97 |
#endif |
98 |
#ifndef O_EVTONLY |
99 |
# define O_EVTONLY O_RDONLY |
100 |
#endif |
101 |
#define OPEN_FILE_FLAGS (O_EVTONLY | O_NONBLOCK | O_NOFOLLOW | O_NOATIME) |
102 |
|
103 |
#define EVFILT_VNODE_FLAGS_ALL (NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE) |
104 |
|
105 |
|
106 |
|
107 |
#define IS_NAME_DOTS(__name) ('.' == (__name)[0] && \ |
108 |
('\0' == (__name)[1] || \ |
109 |
('.' == (__name)[1] && '\0' == (__name)[2]))) |
110 |
#define zalloc(__size) calloc(1, (__size)) |
111 |
|
112 |
|
113 |
static inline int |
114 |
mem_cmpn(const void *buf1, const size_t buf1_size, |
115 |
const void *buf2, const size_t buf2_size) { |
116 |
|
117 |
if (buf1_size != buf2_size) |
118 |
return (((buf1_size > buf2_size) ? 127 : -127)); |
119 |
if (0 == buf1_size || buf1 == buf2) |
120 |
return (0); |
121 |
if (NULL == buf1) |
122 |
return (-127); |
123 |
if (NULL == buf2) |
124 |
return (127); |
125 |
return (memcmp(buf1, buf2, buf1_size)); |
126 |
} |
127 |
|
128 |
#if !defined(__FreeBSD_version) || __FreeBSD_version < 1100000 |
129 |
static inline void * |
130 |
reallocarray(void *buf, const size_t nmemb, const size_t size) { |
131 |
size_t nmemb_size; |
132 |
|
133 |
nmemb_size = (nmemb * size); |
134 |
if (0 == nmemb_size) { |
135 |
if (0 != nmemb && |
136 |
0 != size) { /* Overflow. */ |
137 |
errno = ENOMEM; |
138 |
return (NULL); |
139 |
} |
140 |
nmemb_size ++; |
141 |
} else if (((nmemb | size) & (SIZE_T_MAX << (sizeof(size_t) * 4))) && |
142 |
(nmemb_size / size) != nmemb) { /* size_t overflow. */ |
143 |
errno = ENOMEM; |
144 |
return (NULL); |
145 |
} |
146 |
return (realloc(buf, nmemb_size)); |
147 |
} |
148 |
#endif |
149 |
|
150 |
static inline int |
151 |
realloc_items(void **items, const size_t item_size, |
152 |
size_t *allocated, const size_t alloc_blk_cnt, const size_t count) { |
153 |
size_t allocated_prev, allocated_new; |
154 |
uint8_t *items_new; |
155 |
|
156 |
if (NULL == items || NULL == allocated || 0 == alloc_blk_cnt) |
157 |
return (EINVAL); |
158 |
allocated_prev = (*allocated); |
159 |
if (NULL != (*items) && |
160 |
allocated_prev > count && |
161 |
allocated_prev <= (count + alloc_blk_cnt)) |
162 |
return (0); |
163 |
allocated_new = (((count / alloc_blk_cnt) + 1) * alloc_blk_cnt); |
164 |
items_new = (uint8_t*)reallocarray((*items), item_size, allocated_new); |
165 |
if (NULL == items_new) /* Realloc fail! */ |
166 |
return (ENOMEM); |
167 |
if (allocated_new > allocated_prev) { /* Init new mem. */ |
168 |
bzero((items_new + (allocated_prev * item_size)), |
169 |
((allocated_new - allocated_prev) * item_size)); |
170 |
} |
171 |
(*items) = items_new; |
172 |
(*allocated) = allocated_new; |
173 |
|
174 |
return (0); |
175 |
} |
176 |
|
177 |
|
178 |
static void |
179 |
kqueue_file_mon_data_clean(kqueue_file_mon_data_p fmd) { |
180 |
|
181 |
if (NULL == fmd) |
182 |
return; |
183 |
close(fmd->fd); |
184 |
fmd->fd = -1; |
185 |
fmd->files_count = 0; |
186 |
realloc_items((void**)&fmd->files, sizeof(file_info_t), |
187 |
&fmd->files_allocated, FILES_ALLOC_BLK_SIZE, fmd->files_count); |
188 |
} |
189 |
|
190 |
static void |
191 |
kqueue_file_mon_data_free(kqueue_file_mon_data_p fmd) { |
192 |
|
193 |
if (NULL == fmd) |
194 |
return; |
195 |
close(fmd->fd); |
196 |
free(fmd->files); |
197 |
free(fmd); |
198 |
} |
199 |
|
200 |
static kqueue_file_mon_data_p |
201 |
kqueue_file_mon_data_alloc(kqueue_fnm_p kfnm, const char *path, void *udata) { |
202 |
kqueue_file_mon_data_p fmd; |
203 |
|
204 |
if (NULL == kfnm || NULL == path) |
205 |
return (NULL); |
206 |
fmd = zalloc(sizeof(kqueue_file_mon_data_t)); |
207 |
if (NULL == fmd) |
208 |
return (NULL); |
209 |
/* Remember args. */ |
210 |
fmd->path_size = strlcpy(fmd->path, path, sizeof(fmd->path)); |
211 |
fmd->name_offset = fmd->path_size; |
212 |
fmd->udata = udata; |
213 |
fmd->kfnm = kfnm; |
214 |
|
215 |
return (fmd); |
216 |
} |
217 |
|
218 |
static void |
219 |
kqueue_file_mon_data_init(kqueue_file_mon_data_p fmd) { |
220 |
DIR *d = NULL; |
221 |
struct dirent *de; |
222 |
struct stat sb; |
223 |
struct kevent kev; |
224 |
struct timespec ke_timeout; |
225 |
|
226 |
if (NULL == fmd) |
227 |
return; |
228 |
fmd->fd = open(fmd->path, OPEN_FILE_FLAGS); |
229 |
if (-1 == fmd->fd) |
230 |
return; |
231 |
if (0 != fstat(fmd->fd, &sb)) |
232 |
goto err_out; |
233 |
/* Get parent folder name. */ |
234 |
if (S_ISDIR(sb.st_mode)) { |
235 |
fmd->is_dir = 1; |
236 |
/* Be sure that folder contain trailing '/'. */ |
237 |
if ('/' != fmd->path[(fmd->path_size - 1)]) { |
238 |
fmd->path[fmd->path_size] = '/'; |
239 |
fmd->path[(fmd->path_size + 1)] = 0; |
240 |
fmd->path_size ++; |
241 |
} |
242 |
/* Skip last '/' for parent dir search. */ |
243 |
fmd->name_offset = (fmd->path_size - 1); |
244 |
} |
245 |
/* Find parent dir path size. */ |
246 |
while (0 < fmd->name_offset && '/' != fmd->path[(fmd->name_offset - 1)]) { |
247 |
fmd->name_offset --; |
248 |
} |
249 |
|
250 |
/* Dir special processing. */ |
251 |
if (0 != fmd->is_dir) { |
252 |
/* Read and remember dir content. */ |
253 |
d = opendir(fmd->path); |
254 |
if (NULL == d) |
255 |
goto err_out; |
256 |
for (;;) { |
257 |
if (0 != realloc_items((void**)&fmd->files, |
258 |
sizeof(file_info_t), &fmd->files_allocated, |
259 |
FILES_ALLOC_BLK_SIZE, fmd->files_count)) |
260 |
goto err_out; |
261 |
/* Get file name from folder. */ |
262 |
if (0 != readdir_r(d, &fmd->files[fmd->files_count].de, &de)) |
263 |
break; |
264 |
if (NULL == de) |
265 |
break; |
266 |
if (IS_NAME_DOTS(de->d_name)) |
267 |
continue; |
268 |
/* Get file attrs. */ |
269 |
if (0 != fstatat(fmd->fd, de->d_name, |
270 |
&fmd->files[fmd->files_count].sb, |
271 |
AT_SYMLINK_NOFOLLOW)) { |
272 |
bzero(&fmd->files[fmd->files_count].sb, |
273 |
sizeof(struct stat)); |
274 |
} |
275 |
fmd->files_count ++; |
276 |
} |
277 |
closedir(d); |
278 |
} |
279 |
/* Add to kqueue. */ |
280 |
kev.ident = (uintptr_t)fmd->fd; |
281 |
kev.filter = EVFILT_VNODE; |
282 |
kev.flags = (EV_ADD | EV_CLEAR); |
283 |
kev.fflags = EVFILT_VNODE_FLAGS_ALL; |
284 |
kev.udata = (void*)fmd; |
285 |
bzero(&ke_timeout, sizeof(ke_timeout)); |
286 |
if (-1 == kevent(fmd->kfnm->fd, &kev, 1, NULL, 0, &ke_timeout)) |
287 |
goto err_out; |
288 |
return; /* OK. */ |
289 |
|
290 |
err_out: |
291 |
if (NULL != d) { |
292 |
closedir(d); |
293 |
} |
294 |
kqueue_file_mon_data_clean(fmd); |
295 |
} |
296 |
|
297 |
|
298 |
static inline int |
299 |
kqueue_file_mon_data_find_de(kqueue_file_mon_data_p fmd, struct dirent *de, |
300 |
size_t *idx) { |
301 |
size_t i; |
302 |
|
303 |
if (NULL == fmd || NULL == de || NULL == idx) |
304 |
return (0); |
305 |
for (i = 0; i < fmd->files_count; i ++) { |
306 |
if (de->d_fileno != fmd->files[i].de.d_fileno || |
307 |
de->d_type != fmd->files[i].de.d_type) |
308 |
continue; |
309 |
(*idx) = i; |
310 |
return (1); |
311 |
} |
312 |
(*idx) = fmd->files_count; |
313 |
return (0); |
314 |
} |
315 |
|
316 |
static void |
317 |
kqueue_handle_changes(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd, |
318 |
kfnm_event_handler_cb cb_func) { |
319 |
DIR *d; |
320 |
struct dirent *de; |
321 |
file_info_t fi; |
322 |
size_t i, j, k; |
323 |
|
324 |
if (NULL == kfnm || NULL == fmd || NULL == cb_func) |
325 |
return; |
326 |
if (0 != fstat(fmd->fd, &fi.sb) || |
327 |
0 == fi.sb.st_nlink) { |
328 |
kqueue_file_mon_data_clean(fmd); |
329 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
330 |
fmd->path, "", NULL); |
331 |
return; |
332 |
} |
333 |
if (0 == fmd->is_dir) { |
334 |
fmd->path[fmd->name_offset] = 0; |
335 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, fmd->path, |
336 |
(fmd->path + fmd->name_offset), NULL); |
337 |
fmd->path[fmd->name_offset] = '/'; |
338 |
return; |
339 |
} |
340 |
/* Dir processing. */ |
341 |
d = opendir(fmd->path); |
342 |
if (NULL == d) |
343 |
return; |
344 |
/* Mark all as removed. */ |
345 |
for (i = 0; i < fmd->files_count; i ++) { |
346 |
fmd->files[i].de.d_reclen = KF_EVENT_DELETED; |
347 |
} |
348 |
|
349 |
/* Update dir. */ |
350 |
while (0 == readdir_r(d, &fi.de, &de)) { |
351 |
if (NULL == de) |
352 |
break; |
353 |
if (IS_NAME_DOTS(de->d_name)) |
354 |
continue; |
355 |
/* Get file stat. */ |
356 |
if (0 != fstatat(fmd->fd, de->d_name, &fi.sb, AT_SYMLINK_NOFOLLOW)) { |
357 |
bzero(&fi.sb, sizeof(struct stat)); /* Fail, set to zero. */ |
358 |
} |
359 |
/* Is new file/folder? */ |
360 |
if (0 == kqueue_file_mon_data_find_de(fmd, de, &i)) { /* Add new. */ |
361 |
if (0 != realloc_items((void**)&fmd->files, sizeof(file_info_t), |
362 |
&fmd->files_allocated, FILES_ALLOC_BLK_SIZE, fmd->files_count)) |
363 |
goto err_out; |
364 |
memcpy(&fmd->files[fmd->files_count], &fi, sizeof(file_info_t)); |
365 |
fmd->files[fmd->files_count].de.d_reclen = KF_EVENT_CREATED; |
366 |
fmd->files_count ++; |
367 |
/* Notify. */ |
368 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CREATED, |
369 |
fmd->path, de->d_name, NULL); |
370 |
continue; |
371 |
} |
372 |
/* Is renamed? */ |
373 |
if (0 != mem_cmpn(de->d_name, de->d_namlen, |
374 |
fmd->files[i].de.d_name, fmd->files[i].de.d_namlen)) { |
375 |
/* Notify. */ |
376 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED, |
377 |
fmd->path, fmd->files[i].de.d_name, de->d_name); |
378 |
/* Update. */ |
379 |
memcpy(&fmd->files[i], &fi, sizeof(file_info_t)); |
380 |
fmd->files[i].de.d_reclen = KF_EVENT_RENAMED; |
381 |
continue; |
382 |
} |
383 |
/* Is modified? */ |
384 |
if (0 != memcmp(&fmd->files[i].sb, &fi.sb, sizeof(struct stat))) { |
385 |
memcpy(&fmd->files[i].sb, &fi.sb, sizeof(struct stat)); /* Update stat. */ |
386 |
fmd->files[i].de.d_reclen = KF_EVENT_CHANGED; |
387 |
/* Notify. */ |
388 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_CHANGED, |
389 |
fmd->path, de->d_name, NULL); |
390 |
continue; |
391 |
} |
392 |
/* Not changed. */ |
393 |
fmd->files[i].de.d_reclen = KF_EVENT_NOT_CHANGED; |
394 |
} |
395 |
/* Remove marked as removed. */ |
396 |
for (i = 0; i < fmd->files_count; i ++) { |
397 |
if (KF_EVENT_DELETED != fmd->files[i].de.d_reclen) |
398 |
continue; |
399 |
/* Look for next non removed item + notify. */ |
400 |
for (j = (i + 1); j < fmd->files_count; j ++) { |
401 |
if (KF_EVENT_DELETED != fmd->files[j].de.d_reclen) |
402 |
break; |
403 |
} |
404 |
/* Notify. */ |
405 |
for (k = i; k < j; k ++) { |
406 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
407 |
fmd->path, fmd->files[k].de.d_name, NULL); |
408 |
} |
409 |
memmove(&fmd->files[i], &fmd->files[j], |
410 |
(sizeof(file_info_t) * (fmd->files_count - j))); |
411 |
fmd->files_count -= (j - i); |
412 |
} |
413 |
|
414 |
|
415 |
err_out: |
416 |
if (NULL != d) { |
417 |
closedir(d); |
418 |
} |
419 |
return; |
420 |
} |
421 |
|
422 |
static void |
423 |
kqueue_handle_rename(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd, |
424 |
kfnm_event_handler_cb cb_func) { |
425 |
DIR *d; |
426 |
struct dirent *de, dedat; |
427 |
struct stat sb; |
428 |
char old_filename[(MAXNAMLEN + 2)]; |
429 |
size_t old_filename_size; |
430 |
|
431 |
if (NULL == kfnm || NULL == fmd || NULL == cb_func) |
432 |
return; |
433 |
if (0 != fstat(fmd->fd, &sb) || |
434 |
0 == sb.st_nlink) { |
435 |
notify_removed: |
436 |
kqueue_file_mon_data_clean(fmd); |
437 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
438 |
fmd->path, "", NULL); |
439 |
return; |
440 |
} |
441 |
/* Save old file name. */ |
442 |
old_filename_size = (fmd->path_size - fmd->name_offset - fmd->is_dir); |
443 |
memcpy(old_filename, |
444 |
(fmd->path + fmd->name_offset), |
445 |
old_filename_size); |
446 |
old_filename[old_filename_size] = 0; |
447 |
|
448 |
/* Get parent folder name. */ |
449 |
fmd->path[fmd->name_offset] = 0; |
450 |
/* Try to open. */ |
451 |
d = opendir(fmd->path); |
452 |
/* Restore '/' after parent folder. */ |
453 |
fmd->path[fmd->name_offset] = '/'; |
454 |
if (NULL == d) |
455 |
return; |
456 |
/* Find new name by inode. */ |
457 |
while (0 == readdir_r(d, &dedat, &de)) { |
458 |
if (NULL == de) |
459 |
break; |
460 |
if (de->d_fileno == sb.st_ino) |
461 |
break; |
462 |
} |
463 |
closedir(d); |
464 |
if (NULL == de) |
465 |
goto notify_removed; /* Not found. */ |
466 |
/* Update name. */ |
467 |
if ((sizeof(fmd->path) - 2) <= (fmd->name_offset + de->d_namlen)) |
468 |
return; /* Too long. */ |
469 |
memcpy((fmd->path + fmd->name_offset), de->d_name, de->d_namlen); |
470 |
fmd->path_size = (fmd->name_offset + de->d_namlen); |
471 |
/* Add last '/' for dir. */ |
472 |
fmd->path[fmd->path_size] = '/'; |
473 |
fmd->path_size += fmd->is_dir; |
474 |
fmd->path[fmd->path_size] = 0; |
475 |
/* Notify. */ |
476 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_RENAMED, |
477 |
fmd->path, old_filename, de->d_name); |
478 |
} |
479 |
|
480 |
|
481 |
static void |
482 |
kqueue_fnm_delay_call_process(kqueue_fnm_p kfnm, kq_msg_cb forced_msg_cb) { |
483 |
ssize_t rd; |
484 |
kqueue_fnm_msg_pkt_t msg; |
485 |
|
486 |
for (;;) { |
487 |
rd = read(kfnm->pfd[0], &msg, sizeof(msg)); |
488 |
if (0 >= rd) |
489 |
return; |
490 |
if (0 == KF_MSG_PKT_IS_VALID(&msg)) |
491 |
continue; |
492 |
if (NULL != forced_msg_cb) { |
493 |
forced_msg_cb(msg.fmd); |
494 |
continue; |
495 |
} |
496 |
if (NULL == msg.msg_cb) |
497 |
continue; |
498 |
msg.msg_cb(msg.fmd); |
499 |
} |
500 |
} |
501 |
|
502 |
static int |
503 |
kqueue_fnm_delay_call(kqueue_fnm_p kfnm, kq_msg_cb msg_cb, |
504 |
kqueue_file_mon_data_p fmd) { |
505 |
kqueue_fnm_msg_pkt_t msg; |
506 |
|
507 |
if (NULL == kfnm || NULL == fmd) |
508 |
return (EINVAL); |
509 |
msg.magic = KF_MSG_PKT_MAGIC; |
510 |
msg.msg_cb = msg_cb; |
511 |
msg.fmd = fmd; |
512 |
KF_MSG_PKT_CHK_SUM_SET(&msg); |
513 |
if (sizeof(msg) == write(kfnm->pfd[1], &msg, sizeof(msg))) |
514 |
return (0); |
515 |
return (errno); |
516 |
} |
517 |
|
518 |
|
519 |
void |
520 |
kqueue_fnm_free(kqueue_fnm_p kfnm) { |
521 |
|
522 |
if (NULL == kfnm) |
523 |
return; |
524 |
close(kfnm->fd); |
525 |
kfnm->fd = -1; |
526 |
/* Free all in delay calls queue. */ |
527 |
kqueue_fnm_delay_call_process(kfnm, kqueue_file_mon_data_free); |
528 |
close(kfnm->pfd[0]); |
529 |
close(kfnm->pfd[1]); |
530 |
kfnm->pfd[0] = -1; |
531 |
kfnm->pfd[1] = -1; |
532 |
free(kfnm); |
533 |
} |
534 |
|
535 |
kqueue_fnm_p |
536 |
kqueue_fnm_create(void) { |
537 |
kqueue_fnm_p kfnm; |
538 |
struct kevent kev; |
539 |
struct timespec ke_timeout; |
540 |
|
541 |
kfnm = zalloc(sizeof(kqueue_fnm_t)); |
542 |
if (NULL == kfnm) |
543 |
return (NULL); |
544 |
kfnm->fd = kqueue(); |
545 |
if (-1 == kfnm->fd) |
546 |
goto err_out; |
547 |
if (-1 == pipe2(kfnm->pfd, O_NONBLOCK)) |
548 |
goto err_out; |
549 |
kev.ident = kfnm->pfd[0]; |
550 |
kev.filter = EVFILT_READ; |
551 |
kev.flags = (EV_ADD | EV_ENABLE); |
552 |
kev.fflags = 0; |
553 |
kev.udata = NULL; |
554 |
bzero(&ke_timeout, sizeof(ke_timeout)); |
555 |
if (-1 == kevent(kfnm->fd, &kev, 1, NULL, 0, &ke_timeout)) |
556 |
goto err_out; |
557 |
return (kfnm); |
558 |
|
559 |
err_out: |
560 |
kqueue_fnm_free(kfnm); |
561 |
return (NULL); |
562 |
} |
563 |
|
564 |
kqueue_file_mon_data_p |
565 |
kqueue_fnm_add(kqueue_fnm_p kfnm, const char *path, void *udata) { |
566 |
int error; |
567 |
kqueue_file_mon_data_p fmd; |
568 |
|
569 |
if (NULL == kfnm || NULL == path) |
570 |
return (NULL); |
571 |
fmd = (void*)kqueue_file_mon_data_alloc(kfnm, path, udata); |
572 |
if (NULL == fmd) |
573 |
return (NULL); |
574 |
/* Shedule delay call to init. */ |
575 |
error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_init, fmd); |
576 |
if (0 != error) { /* Error, init directly. */ |
577 |
kqueue_file_mon_data_init(fmd); |
578 |
} |
579 |
return (fmd); |
580 |
} |
581 |
|
582 |
void |
583 |
kqueue_fnm_del(kqueue_fnm_p kfnm, kqueue_file_mon_data_p fmd) { |
584 |
int error; |
585 |
|
586 |
if (NULL == kfnm || NULL == fmd) |
587 |
return; |
588 |
/* Cancel notifications. */ |
589 |
close(fmd->fd); |
590 |
fmd->fd = -1; |
591 |
/* Shedule delay call to free. */ |
592 |
error = kqueue_fnm_delay_call(kfnm, kqueue_file_mon_data_free, fmd); |
593 |
if (0 == error) |
594 |
return; |
595 |
/* Error, free directly. */ |
596 |
kqueue_file_mon_data_free(fmd); |
597 |
} |
598 |
|
599 |
|
600 |
int |
601 |
kqueue_fnm_get_ev_recv_fd(kqueue_fnm_p kfnm) { |
602 |
|
603 |
if (NULL == kfnm) |
604 |
return (-1); |
605 |
return (kfnm->fd); |
606 |
} |
607 |
|
608 |
void |
609 |
kqueue_fnm_proccess_events(kqueue_fnm_p kfnm, kfnm_event_handler_cb cb_func) { |
610 |
struct kevent kev; |
611 |
struct timespec ke_timeout; |
612 |
kqueue_file_mon_data_p fmd; |
613 |
|
614 |
if (NULL == kfnm || NULL == cb_func) |
615 |
return; |
616 |
/* Get and proccess events. */ |
617 |
bzero(&ke_timeout, sizeof(ke_timeout)); |
618 |
while (0 < kevent(kfnm->fd, NULL, 0, &kev, 1, &ke_timeout)) { |
619 |
if (kev.ident == (uintptr_t)kfnm->pfd[0] && |
620 |
kev.filter == EVFILT_READ) { /* Handle delay calls. */ |
621 |
kqueue_fnm_delay_call_process(kfnm, NULL); |
622 |
continue; |
623 |
} |
624 |
if (EVFILT_VNODE != kev.filter || |
625 |
0 == kev.udata) |
626 |
continue; /* Unknown event or no associated data, skip. */ |
627 |
fmd = (kqueue_file_mon_data_p)kev.udata; |
628 |
|
629 |
if (EV_ERROR & kev.flags) { |
630 |
kev.flags |= NOTE_REVOKE; /* Treat error as unmount. */ |
631 |
} |
632 |
if (NOTE_RENAME & kev.fflags) { |
633 |
kqueue_handle_rename(kfnm, fmd, cb_func); |
634 |
} |
635 |
if ((NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB) & kev.fflags) { |
636 |
kqueue_handle_changes(kfnm, fmd, cb_func); |
637 |
} |
638 |
if ((NOTE_DELETE | NOTE_REVOKE) & kev.fflags) { |
639 |
kqueue_file_mon_data_clean(fmd); |
640 |
cb_func(kfnm, fmd, fmd->udata, KF_EVENT_DELETED, |
641 |
fmd->path, "", NULL); |
642 |
} |
643 |
} |
644 |
} |