2 * Copyright (c) 2004-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/event.h> // for kqueue related stuff
32 #include <sys/fsevents.h>
35 #include <sys/namei.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/file_internal.h>
40 #include <sys/vnode_internal.h>
41 #include <sys/mount_internal.h>
42 #include <sys/proc_internal.h>
43 #include <sys/kauth.h>
45 #include <kern/kalloc.h>
46 #include <sys/dirent.h>
48 #include <sys/sysctl.h>
50 #include <machine/cons.h>
51 #include <miscfs/specfs/specdev.h>
52 #include <miscfs/devfs/devfs.h>
53 #include <sys/filio.h>
54 #include <kern/locks.h>
55 #include <libkern/OSAtomic.h>
56 #include <kern/zalloc.h>
57 #include <mach/mach_time.h>
58 #include <kern/thread_call.h>
59 #include <kern/clock.h>
61 #include <security/audit/audit.h>
62 #include <bsm/audit_kevents.h>
64 #include <pexpert/pexpert.h>
65 #include <libkern/section_keywords.h>
67 typedef struct kfs_event
{
68 LIST_ENTRY(kfs_event
) kevent_list
;
69 int16_t type
; // type code of this event
70 u_int16_t flags
, // per-event flags
71 len
; // the length of the path in "str"
72 int32_t refcount
; // number of clients referencing this
73 pid_t pid
; // pid of the process that did the op
75 uint64_t abstime
; // when this event happened (mach_absolute_time())
84 struct kfs_event
*dest
; // if this is a two-file op
87 // flags for the flags field
88 #define KFSE_COMBINED_EVENTS 0x0001
89 #define KFSE_CONTAINS_DROPPED_EVENTS 0x0002
90 #define KFSE_RECYCLED_EVENT 0x0004
91 #define KFSE_BEING_CREATED 0x0008
93 LIST_HEAD(kfse_list
, kfs_event
) kfse_list_head
= LIST_HEAD_INITIALIZER(x
);
94 int num_events_outstanding
= 0;
95 int num_pending_rename
= 0;
98 struct fsevent_handle
;
100 typedef struct fs_event_watcher
{
101 int8_t *event_list
; // the events we're interested in
103 dev_t
*devices_not_to_watch
;// report events from devices not in this list
104 uint32_t num_devices
;
106 kfs_event
**event_queue
;
107 int32_t eventq_size
; // number of event pointers in queue
109 int32_t rd
; // read index into the event_queue
110 int32_t wr
; // write index into the event_queue
113 uint32_t num_dropped
;
114 uint64_t max_event_id
;
115 struct fsevent_handle
*fseh
;
117 char proc_name
[(2 * MAXCOMLEN
) + 1];
120 // fs_event_watcher flags
121 #define WATCHER_DROPPED_EVENTS 0x0001
122 #define WATCHER_CLOSING 0x0002
123 #define WATCHER_WANTS_COMPACT_EVENTS 0x0004
124 #define WATCHER_WANTS_EXTENDED_INFO 0x0008
125 #define WATCHER_APPLE_SYSTEM_SERVICE 0x0010 // fseventsd, coreservicesd, mds, revisiond
127 #define MAX_WATCHERS 8
128 static fs_event_watcher
*watcher_table
[MAX_WATCHERS
];
130 #define DEFAULT_MAX_KFS_EVENTS 4096
131 static int max_kfs_events
= DEFAULT_MAX_KFS_EVENTS
;
133 // we allocate kfs_event structures out of this zone
134 static zone_t event_zone
;
135 static int fs_event_init
= 0;
138 // this array records whether anyone is interested in a
139 // particular type of event. if no one is, we bail out
140 // early from the event delivery
142 static int16_t fs_event_type_watchers
[FSE_MAX_EVENTS
];
144 // the device currently being unmounted:
145 static dev_t fsevent_unmount_dev
= 0;
146 // how many ACKs are still outstanding:
147 static int fsevent_unmount_ack_count
= 0;
149 static int watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
);
150 static void fsevents_wakeup(fs_event_watcher
*watcher
);
155 static LCK_ATTR_DECLARE(fsevent_lock_attr
, 0, 0);
156 static LCK_GRP_DECLARE(fsevent_mutex_group
, "fsevent-mutex");
157 static LCK_GRP_DECLARE(fsevent_rw_group
, "fsevent-rw");
159 static LCK_RW_DECLARE_ATTR(event_handling_lock
, // handles locking for event manipulation and recycling
160 &fsevent_rw_group
, &fsevent_lock_attr
);
161 static LCK_MTX_DECLARE_ATTR(watch_table_lock
,
162 &fsevent_mutex_group
, &fsevent_lock_attr
);
163 static LCK_MTX_DECLARE_ATTR(event_buf_lock
,
164 &fsevent_mutex_group
, &fsevent_lock_attr
);
165 static LCK_MTX_DECLARE_ATTR(event_writer_lock
,
166 &fsevent_mutex_group
, &fsevent_lock_attr
);
169 /* Explicitly declare qsort so compiler doesn't complain */
170 __private_extern__
void qsort(
174 int (*)(const void *, const void *));
177 is_ignored_directory(const char *path
)
183 #define IS_TLD(x) strnstr(__DECONST(char *, path), x, MAXPATHLEN)
184 if (IS_TLD("/.Spotlight-V100/") ||
185 IS_TLD("/.MobileBackups/") ||
186 IS_TLD("/Backups.backupdb/")) {
195 fsevents_internal_init(void)
199 if (fs_event_init
++ != 0) {
203 for (i
= 0; i
< FSE_MAX_EVENTS
; i
++) {
204 fs_event_type_watchers
[i
] = 0;
207 memset(watcher_table
, 0, sizeof(watcher_table
));
209 PE_get_default("kern.maxkfsevents", &max_kfs_events
, sizeof(max_kfs_events
));
211 event_zone
= zone_create_ext("fs-event-buf", sizeof(kfs_event
),
212 ZC_NOGC
| ZC_NOCALLOUT
, ZONE_ID_ANY
, ^(zone_t z
) {
213 // mark the zone as exhaustible so that it will not
214 // ever grow beyond what we initially filled it with
215 zone_set_exhaustible(z
, max_kfs_events
);
218 zone_fill_initially(event_zone
, max_kfs_events
);
222 lock_watch_table(void)
224 lck_mtx_lock(&watch_table_lock
);
228 unlock_watch_table(void)
230 lck_mtx_unlock(&watch_table_lock
);
234 lock_fs_event_list(void)
236 lck_mtx_lock(&event_buf_lock
);
240 unlock_fs_event_list(void)
242 lck_mtx_unlock(&event_buf_lock
);
246 static void release_event_ref(kfs_event
*kfse
);
249 watcher_cares_about_dev(fs_event_watcher
*watcher
, dev_t dev
)
253 // if devices_not_to_watch is NULL then we care about all
254 // events from all devices
255 if (watcher
->devices_not_to_watch
== NULL
) {
259 for (i
= 0; i
< watcher
->num_devices
; i
++) {
260 if (dev
== watcher
->devices_not_to_watch
[i
]) {
261 // found a match! that means we do not
262 // want events from this device.
267 // if we're here it's not in the devices_not_to_watch[]
268 // list so that means we do care about it
274 need_fsevent(int type
, vnode_t vp
)
276 if (type
>= 0 && type
< FSE_MAX_EVENTS
&& fs_event_type_watchers
[type
] == 0) {
280 // events in /dev aren't really interesting...
281 if (vp
->v_tag
== VT_DEVFS
) {
289 #define is_throw_away(x) ((x) == FSE_STAT_CHANGED || (x) == FSE_CONTENT_MODIFIED)
292 // Ways that an event can be reused:
294 // "combined" events mean that there were two events for
295 // the same vnode or path and we're combining both events
296 // into a single event. The primary event gets a bit that
297 // marks it as having been combined. The secondary event
298 // is essentially dropped and the kfse structure reused.
300 // "collapsed" means that multiple events below a given
301 // directory are collapsed into a single event. in this
302 // case, the directory that we collapse into and all of
303 // its children must be re-scanned.
305 // "recycled" means that we're completely blowing away
306 // the event since there are other events that have info
307 // about the same vnode or path (and one of those other
308 // events will be marked as combined or collapsed as
311 #define KFSE_COMBINED 0x0001
312 #define KFSE_COLLAPSED 0x0002
313 #define KFSE_RECYCLED 0x0004
316 int num_parent_switch
= 0;
317 int num_recycled_rename
= 0;
319 static struct timeval last_print
;
322 // These variables are used to track coalescing multiple identical
323 // events for the same vnode/pathname. If we get the same event
324 // type and same vnode/pathname as the previous event, we just drop
325 // the event since it's superfluous. This improves some micro-
326 // benchmarks considerably and actually has a real-world impact on
327 // tests like a Finder copy where multiple stat-changed events can
330 static int last_event_type
= -1;
331 static void *last_ptr
= NULL
;
332 static char last_str
[MAXPATHLEN
];
333 static int last_nlen
= 0;
334 static int last_vid
= -1;
335 static uint64_t last_coalesced_time
= 0;
336 static void *last_event_ptr
= NULL
;
337 static pid_t last_pid
= -1;
338 int last_coalesced
= 0;
339 static mach_timebase_info_data_t sTimebaseInfo
= { 0, 0 };
343 add_fsevent(int type
, vfs_context_t ctx
, ...)
345 struct proc
*p
= vfs_context_proc(ctx
);
346 int i
, arg_type
, ret
;
347 kfs_event
*kfse
, *kfse_dest
= NULL
, *cur
;
348 fs_event_watcher
*watcher
;
350 int error
= 0, did_alloc
= 0;
352 uint64_t now
, elapsed
;
353 char *pathbuff
= NULL
;
360 // ignore bogus event types..
361 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
365 // if no one cares about this type of event, bail out
366 if (fs_event_type_watchers
[type
] == 0) {
372 now
= mach_absolute_time();
374 // find a free event and snag it for our use
375 // NOTE: do not do anything that would block until
376 // the lock is dropped.
377 lock_fs_event_list();
380 // check if this event is identical to the previous one...
381 // (as long as it's not an event type that can never be the
382 // same as a previous event)
384 if (type
!= FSE_CREATE_FILE
&& type
!= FSE_DELETE
&& type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
&& type
!= FSE_CHOWN
&& type
!= FSE_DOCID_CHANGED
&& type
!= FSE_DOCID_CREATED
&& type
!= FSE_CLONE
) {
386 int vid
= 0, was_str
= 0, nlen
= 0;
388 for (arg_type
= va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
= va_arg(ap
, int32_t)) {
390 case FSE_ARG_VNODE
: {
391 ptr
= va_arg(ap
, void *);
392 vid
= vnode_vid((struct vnode
*)ptr
);
396 case FSE_ARG_STRING
: {
397 nlen
= va_arg(ap
, int32_t);
398 ptr
= va_arg(ap
, void *);
408 if (sTimebaseInfo
.denom
== 0) {
409 (void) clock_timebase_info(&sTimebaseInfo
);
412 elapsed
= (now
- last_coalesced_time
);
413 if (sTimebaseInfo
.denom
!= sTimebaseInfo
.numer
) {
414 if (sTimebaseInfo
.denom
== 1) {
415 elapsed
*= sTimebaseInfo
.numer
;
417 // this could overflow... the worst that will happen is that we'll
418 // send (or not send) an extra event so I'm not going to worry about
419 // doing the math right like dtrace_abs_to_nano() does.
420 elapsed
= (elapsed
* sTimebaseInfo
.numer
) / (uint64_t)sTimebaseInfo
.denom
;
424 if (type
== last_event_type
425 && (elapsed
< 1000000000)
426 && (last_pid
== p
->p_pid
)
428 ((vid
&& vid
== last_vid
&& last_ptr
== ptr
)
430 (last_str
[0] && last_nlen
== nlen
&& ptr
&& strcmp(last_str
, ptr
) == 0))
433 unlock_fs_event_list();
440 strlcpy(last_str
, ptr
, sizeof(last_str
));
444 last_event_type
= type
;
445 last_coalesced_time
= now
;
452 kfse
= zalloc_noblock(event_zone
);
453 if (kfse
&& (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
|| type
== FSE_CLONE
)) {
454 kfse_dest
= zalloc_noblock(event_zone
);
455 if (kfse_dest
== NULL
) {
457 zfree(event_zone
, kfse
);
463 if (kfse
== NULL
) { // yikes! no free events
464 unlock_fs_event_list();
467 for (i
= 0; i
< MAX_WATCHERS
; i
++) {
468 watcher
= watcher_table
[i
];
469 if (watcher
== NULL
) {
473 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
474 fsevents_wakeup(watcher
);
476 unlock_watch_table();
479 struct timeval current_tv
;
483 // only print a message at most once every 5 seconds
484 microuptime(¤t_tv
);
485 if ((current_tv
.tv_sec
- last_print
.tv_sec
) > 10) {
487 void *junkptr
= zalloc_noblock(event_zone
), *listhead
= kfse_list_head
.lh_first
;
489 printf("add_fsevent: event queue is full! dropping events (num dropped events: %d; num events outstanding: %d).\n", num_dropped
, num_events_outstanding
);
490 printf("add_fsevent: kfse_list head %p ; num_pending_rename %d\n", listhead
, num_pending_rename
);
491 printf("add_fsevent: zalloc sez: %p\n", junkptr
);
492 printf("add_fsevent: event_zone info: %d 0x%x\n", ((int *)event_zone
)[0], ((int *)event_zone
)[1]);
494 for (ii
= 0; ii
< MAX_WATCHERS
; ii
++) {
495 if (watcher_table
[ii
] == NULL
) {
499 printf("add_fsevent: watcher %s %p: rd %4d wr %4d q_size %4d flags 0x%x\n",
500 watcher_table
[ii
]->proc_name
,
502 watcher_table
[ii
]->rd
, watcher_table
[ii
]->wr
,
503 watcher_table
[ii
]->eventq_size
, watcher_table
[ii
]->flags
);
505 unlock_watch_table();
507 last_print
= current_tv
;
509 zfree(event_zone
, junkptr
);
515 release_pathbuff(pathbuff
);
521 memset(kfse
, 0, sizeof(kfs_event
));
523 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse
->flags
);
525 last_event_ptr
= kfse
;
526 kfse
->type
= (int16_t)type
;
528 kfse
->pid
= p
->p_pid
;
529 if (type
== FSE_RENAME
|| type
== FSE_EXCHANGE
|| type
== FSE_CLONE
) {
530 memset(kfse_dest
, 0, sizeof(kfs_event
));
531 kfse_dest
->refcount
= 1;
532 OSBitOrAtomic16(KFSE_BEING_CREATED
, &kfse_dest
->flags
);
533 kfse_dest
->type
= (int16_t)type
;
534 kfse_dest
->pid
= p
->p_pid
;
535 kfse_dest
->abstime
= now
;
537 kfse
->dest
= kfse_dest
;
540 num_events_outstanding
++;
541 if (kfse
->type
== FSE_RENAME
) {
542 num_pending_rename
++;
544 LIST_INSERT_HEAD(&kfse_list_head
, kfse
, kevent_list
);
546 if (kfse
->refcount
< 1) {
547 panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__
, kfse
->refcount
);
550 unlock_fs_event_list(); // at this point it's safe to unlock
553 // now process the arguments passed in and copy them into
559 if (type
== FSE_DOCID_CREATED
|| type
== FSE_DOCID_CHANGED
) {
563 // These events are special and not like the other events. They only
564 // have a dev_t, src inode #, dest inode #, and a doc-id. We use the
565 // fields that we can in the kfse but have to overlay the dest inode
566 // number and the doc-id on the other fields.
570 arg_type
= va_arg(ap
, int32_t);
571 if (arg_type
== FSE_ARG_DEV
) {
572 cur
->dev
= (dev_t
)(va_arg(ap
, dev_t
));
574 cur
->dev
= (dev_t
)0xbadc0de1;
577 // next the source inode #
578 arg_type
= va_arg(ap
, int32_t);
579 if (arg_type
== FSE_ARG_INO
) {
580 cur
->ino
= (ino64_t
)(va_arg(ap
, ino64_t
));
582 cur
->ino
= 0xbadc0de2;
585 // now the dest inode #
586 arg_type
= va_arg(ap
, int32_t);
587 if (arg_type
== FSE_ARG_INO
) {
588 val
= (ino64_t
)(va_arg(ap
, ino64_t
));
592 // overlay the dest inode number on the str/dest pointer fields
593 __nochk_memcpy(&cur
->str
, &val
, sizeof(ino64_t
));
596 // and last the document-id
597 arg_type
= va_arg(ap
, int32_t);
598 if (arg_type
== FSE_ARG_INT32
) {
599 val
= (uint64_t)va_arg(ap
, uint32_t);
600 } else if (arg_type
== FSE_ARG_INT64
) {
601 val
= (uint64_t)va_arg(ap
, uint64_t);
606 // the docid is 64-bit and overlays the uid/gid fields
607 static_assert(sizeof(cur
->uid
) + sizeof(cur
->gid
) == sizeof(val
), "gid/uid size mismatch");
608 static_assert(offsetof(struct kfs_event
, gid
) - offsetof(struct kfs_event
, uid
) == sizeof(cur
->uid
), "unexpected struct kfs_event layout");
609 memcpy(&cur
->uid
, &val
, sizeof(cur
->uid
));
610 memcpy(&cur
->gid
, (u_int8_t
*)&val
+ sizeof(cur
->uid
), sizeof(cur
->gid
));
615 if (type
== FSE_UNMOUNT_PENDING
) {
617 arg_type
= va_arg(ap
, int32_t);
618 if (arg_type
== FSE_ARG_DEV
) {
619 cur
->dev
= (dev_t
)(va_arg(ap
, dev_t
));
621 cur
->dev
= (dev_t
)0xbadc0de1;
627 for (arg_type
= va_arg(ap
, int32_t); arg_type
!= FSE_ARG_DONE
; arg_type
= va_arg(ap
, int32_t)) {
629 case FSE_ARG_VNODE
: {
630 // this expands out into multiple arguments to the client
632 struct vnode_attr va
;
634 if (kfse
->str
!= NULL
) {
638 vp
= va_arg(ap
, struct vnode
*);
640 panic("add_fsevent: you can't pass me a NULL vnode ptr (type %d)!\n",
645 VATTR_WANTED(&va
, va_fsid
);
646 VATTR_WANTED(&va
, va_fileid
);
647 VATTR_WANTED(&va
, va_mode
);
648 VATTR_WANTED(&va
, va_uid
);
649 VATTR_WANTED(&va
, va_gid
);
650 VATTR_WANTED(&va
, va_nlink
);
651 if ((ret
= vnode_getattr(vp
, &va
, vfs_context_kernel())) != 0) {
652 // printf("add_fsevent: failed to getattr on vp %p (%d)\n", cur->fref.vp, ret);
658 cur
->dev
= dev
= (dev_t
)va
.va_fsid
;
659 cur
->ino
= (ino64_t
)va
.va_fileid
;
660 cur
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | va
.va_mode
;
661 cur
->uid
= va
.va_uid
;
662 cur
->gid
= va
.va_gid
;
663 if (vp
->v_flag
& VISHARDLINK
) {
664 cur
->mode
|= FSE_MODE_HLINK
;
665 if ((vp
->v_type
== VDIR
&& va
.va_dirlinkcount
== 0) || (vp
->v_type
== VREG
&& va
.va_nlink
== 0)) {
666 cur
->mode
|= FSE_MODE_LAST_HLINK
;
670 // if we haven't gotten the path yet, get it.
671 if (pathbuff
== NULL
) {
672 pathbuff
= get_pathbuff();
673 pathbuff_len
= MAXPATHLEN
;
676 if ((ret
= vn_getpath_no_firmlink(vp
, pathbuff
, &pathbuff_len
)) != 0 || pathbuff
[0] == '\0') {
677 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
680 if (vp
->v_parent
!= NULL
) {
682 } else if (vp
->v_mount
) {
683 strlcpy(pathbuff
, vp
->v_mount
->mnt_vfsstat
.f_mntonname
, MAXPATHLEN
);
693 pathbuff_len
= MAXPATHLEN
;
694 ret
= vn_getpath_no_firmlink(vp
, pathbuff
, &pathbuff_len
);
695 } while (ret
== ENOSPC
);
697 if (ret
!= 0 || vp
== NULL
) {
704 // store the path by adding it to the global string table
705 cur
->len
= (u_int16_t
)pathbuff_len
;
706 cur
->str
= vfs_addname(pathbuff
, pathbuff_len
, 0, 0);
707 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
708 panic("add_fsevent: was not able to add path %s to event %p.\n", pathbuff
, cur
);
711 release_pathbuff(pathbuff
);
717 case FSE_ARG_FINFO
: {
720 fse
= va_arg(ap
, fse_info
*);
722 cur
->dev
= dev
= (dev_t
)fse
->dev
;
723 cur
->ino
= (ino64_t
)fse
->ino
;
724 cur
->mode
= (int32_t)fse
->mode
;
725 cur
->uid
= (uid_t
)fse
->uid
;
726 cur
->gid
= (uid_t
)fse
->gid
;
727 // if it's a hard-link and this is the last link, flag it
728 if ((fse
->mode
& FSE_MODE_HLINK
) && fse
->nlink
== 0) {
729 cur
->mode
|= FSE_MODE_LAST_HLINK
;
731 if (cur
->mode
& FSE_TRUNCATED_PATH
) {
732 cur
->flags
|= KFSE_CONTAINS_DROPPED_EVENTS
;
733 cur
->mode
&= ~FSE_TRUNCATED_PATH
;
739 if (kfse
->str
!= NULL
) {
743 cur
->len
= (int16_t)(va_arg(ap
, int32_t) & 0x7fff);
745 cur
->str
= vfs_addname(va_arg(ap
, char *), cur
->len
, 0, 0);
747 printf("add_fsevent: funny looking string length: %d\n", (int)cur
->len
);
749 cur
->str
= vfs_addname("/", cur
->len
, 0, 0);
751 if (cur
->str
[0] == 0) {
752 printf("add_fsevent: bogus looking string (len %d)\n", cur
->len
);
756 case FSE_ARG_INT32
: {
757 uint32_t ival
= (uint32_t)va_arg(ap
, int32_t);
763 printf("add_fsevent: unknown type %d\n", arg_type
);
764 // just skip one 32-bit word and hope we sync up...
765 (void)va_arg(ap
, int32_t);
772 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse
->flags
);
774 OSBitAndAtomic16(~KFSE_BEING_CREATED
, &kfse_dest
->flags
);
778 // now we have to go and let everyone know that
779 // is interested in this type of event
783 for (i
= 0; i
< MAX_WATCHERS
; i
++) {
784 watcher
= watcher_table
[i
];
785 if (watcher
== NULL
) {
789 if (type
< watcher
->num_events
790 && watcher
->event_list
[type
] == FSE_REPORT
791 && watcher_cares_about_dev(watcher
, dev
)) {
792 if (watcher_add_event(watcher
, kfse
) != 0) {
793 watcher
->num_dropped
++;
798 // if (kfse->refcount < 1) {
799 // panic("add_fsevent: line %d: kfse recount %d but should be at least 1\n", __LINE__, kfse->refcount);
803 unlock_watch_table();
808 release_pathbuff(pathbuff
);
812 release_event_ref(kfse
);
819 release_event_ref(kfs_event
*kfse
)
822 kfs_event copy
, dest_copy
;
825 old_refcount
= OSAddAtomic(-1, &kfse
->refcount
);
826 if (old_refcount
> 1) {
830 lock_fs_event_list();
831 if (last_event_ptr
== kfse
) {
832 last_event_ptr
= NULL
;
833 last_event_type
= -1;
834 last_coalesced_time
= 0;
837 if (kfse
->refcount
< 0) {
838 panic("release_event_ref: bogus kfse refcount %d\n", kfse
->refcount
);
841 if (kfse
->refcount
> 0 || kfse
->type
== FSE_INVALID
) {
842 // This is very subtle. Either of these conditions can
843 // be true if an event got recycled while we were waiting
844 // on the fs_event_list lock or the event got recycled,
845 // delivered, _and_ free'd by someone else while we were
846 // waiting on the fs event list lock. In either case
847 // we need to just unlock the list and return without
848 // doing anything because if the refcount is > 0 then
849 // someone else will take care of free'ing it and when
850 // the kfse->type is invalid then someone else already
851 // has handled free'ing the event (while we were blocked
852 // on the event list lock).
854 unlock_fs_event_list();
859 // make a copy of this so we can free things without
860 // holding the fs_event_buf lock
863 if (kfse
->type
!= FSE_DOCID_CREATED
&& kfse
->type
!= FSE_DOCID_CHANGED
&& kfse
->dest
&& OSAddAtomic(-1, &kfse
->dest
->refcount
) == 1) {
864 dest_copy
= *kfse
->dest
;
866 dest_copy
.str
= NULL
;
868 dest_copy
.type
= FSE_INVALID
;
871 kfse
->pid
= kfse
->type
; // save this off for debugging...
872 kfse
->uid
= (uid_t
)(long)kfse
->str
; // save this off for debugging...
873 kfse
->gid
= (gid_t
)(long)current_thread();
875 kfse
->str
= (char *)0xdeadbeef; // XXXdbg - catch any cheaters...
877 if (dest_copy
.type
!= FSE_INVALID
) {
878 kfse
->dest
->str
= (char *)0xbadc0de; // XXXdbg - catch any cheaters...
879 kfse
->dest
->type
= FSE_INVALID
;
881 if (kfse
->dest
->kevent_list
.le_prev
!= NULL
) {
882 num_events_outstanding
--;
883 LIST_REMOVE(kfse
->dest
, kevent_list
);
884 memset(&kfse
->dest
->kevent_list
, 0xa5, sizeof(kfse
->dest
->kevent_list
));
887 zfree(event_zone
, kfse
->dest
);
890 // mark this fsevent as invalid
895 kfse
->type
= FSE_INVALID
;
897 if (kfse
->kevent_list
.le_prev
!= NULL
) {
898 num_events_outstanding
--;
899 if (otype
== FSE_RENAME
) {
900 num_pending_rename
--;
902 LIST_REMOVE(kfse
, kevent_list
);
903 memset(&kfse
->kevent_list
, 0, sizeof(kfse
->kevent_list
));
907 zfree(event_zone
, kfse
);
909 unlock_fs_event_list();
911 // if we have a pointer in the union
912 if (copy
.str
&& copy
.type
!= FSE_DOCID_CREATED
&& copy
.type
!= FSE_DOCID_CHANGED
) {
913 if (copy
.len
== 0) { // and it's not a string
914 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
915 // vnode_rele_ext(copy.fref.vp, O_EVTONLY, 0);
916 } else { // else it's a string
917 vfs_removename(copy
.str
);
921 if (dest_copy
.type
!= FSE_INVALID
&& dest_copy
.str
) {
922 if (dest_copy
.len
== 0) {
923 panic("%s:%d: no more fref.vp!\n", __FILE__
, __LINE__
);
924 // vnode_rele_ext(dest_copy.fref.vp, O_EVTONLY, 0);
926 vfs_removename(dest_copy
.str
);
932 add_watcher(int8_t *event_list
, int32_t num_events
, int32_t eventq_size
, fs_event_watcher
**watcher_out
, void *fseh
)
935 fs_event_watcher
*watcher
;
937 if (eventq_size
<= 0 || eventq_size
> 100 * max_kfs_events
) {
938 eventq_size
= max_kfs_events
;
941 // Note: the event_queue follows the fs_event_watcher struct
942 // in memory so we only have to do one allocation
943 watcher
= kheap_alloc(KHEAP_DEFAULT
,
944 sizeof(fs_event_watcher
) + eventq_size
* sizeof(kfs_event
*), Z_WAITOK
);
945 if (watcher
== NULL
) {
949 watcher
->event_list
= event_list
;
950 watcher
->num_events
= num_events
;
951 watcher
->devices_not_to_watch
= NULL
;
952 watcher
->num_devices
= 0;
954 watcher
->event_queue
= (kfs_event
**)&watcher
[1];
955 watcher
->eventq_size
= eventq_size
;
958 watcher
->blockers
= 0;
959 watcher
->num_readers
= 0;
960 watcher
->max_event_id
= 0;
961 watcher
->fseh
= fseh
;
962 watcher
->pid
= proc_selfpid();
963 proc_selfname(watcher
->proc_name
, sizeof(watcher
->proc_name
));
965 watcher
->num_dropped
= 0; // XXXdbg - debugging
967 if (!strncmp(watcher
->proc_name
, "fseventsd", sizeof(watcher
->proc_name
)) ||
968 !strncmp(watcher
->proc_name
, "coreservicesd", sizeof(watcher
->proc_name
)) ||
969 !strncmp(watcher
->proc_name
, "revisiond", sizeof(watcher
->proc_name
)) ||
970 !strncmp(watcher
->proc_name
, "mds", sizeof(watcher
->proc_name
))) {
971 watcher
->flags
|= WATCHER_APPLE_SYSTEM_SERVICE
;
973 printf("fsevents: watcher %s (pid: %d) - Using /dev/fsevents directly is unsupported. Migrate to FSEventsFramework\n",
974 watcher
->proc_name
, watcher
->pid
);
979 // find a slot for the new watcher
980 for (i
= 0; i
< MAX_WATCHERS
; i
++) {
981 if (watcher_table
[i
] == NULL
) {
983 watcher_table
[i
] = watcher
;
988 if (i
>= MAX_WATCHERS
) {
989 printf("fsevents: too many watchers!\n");
990 unlock_watch_table();
991 kheap_free(KHEAP_DEFAULT
, watcher
,
992 sizeof(fs_event_watcher
) + watcher
->eventq_size
* sizeof(kfs_event
*));
996 // now update the global list of who's interested in
997 // events of a particular type...
998 for (i
= 0; i
< num_events
; i
++) {
999 if (event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1000 fs_event_type_watchers
[i
]++;
1004 unlock_watch_table();
1006 *watcher_out
= watcher
;
1014 remove_watcher(fs_event_watcher
*target
)
1016 int i
, j
, counter
= 0;
1017 fs_event_watcher
*watcher
;
1022 for (j
= 0; j
< MAX_WATCHERS
; j
++) {
1023 watcher
= watcher_table
[j
];
1024 if (watcher
!= target
) {
1028 watcher_table
[j
] = NULL
;
1030 for (i
= 0; i
< watcher
->num_events
; i
++) {
1031 if (watcher
->event_list
[i
] != FSE_IGNORE
&& i
< FSE_MAX_EVENTS
) {
1032 fs_event_type_watchers
[i
]--;
1036 if (watcher
->flags
& WATCHER_CLOSING
) {
1037 unlock_watch_table();
1041 // printf("fsevents: removing watcher %p (rd %d wr %d num_readers %d flags 0x%x)\n", watcher, watcher->rd, watcher->wr, watcher->num_readers, watcher->flags);
1042 watcher
->flags
|= WATCHER_CLOSING
;
1043 OSAddAtomic(1, &watcher
->num_readers
);
1045 unlock_watch_table();
1047 while (watcher
->num_readers
> 1 && counter
++ < 5000) {
1049 fsevents_wakeup(watcher
); // in case they're asleep
1050 unlock_watch_table();
1052 tsleep(watcher
, PRIBIO
, "fsevents-close", 1);
1054 if (counter
++ >= 5000) {
1055 // printf("fsevents: close: still have readers! (%d)\n", watcher->num_readers);
1056 panic("fsevents: close: still have readers! (%d)\n", watcher
->num_readers
);
1059 // drain the event_queue
1061 lck_rw_lock_exclusive(&event_handling_lock
);
1062 while (watcher
->rd
!= watcher
->wr
) {
1063 kfse
= watcher
->event_queue
[watcher
->rd
];
1064 watcher
->event_queue
[watcher
->rd
] = NULL
;
1065 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1067 if (kfse
!= NULL
&& kfse
->type
!= FSE_INVALID
&& kfse
->refcount
>= 1) {
1068 release_event_ref(kfse
);
1071 lck_rw_unlock_exclusive(&event_handling_lock
);
1073 kheap_free(KHEAP_DEFAULT
, watcher
->event_list
,
1074 watcher
->num_events
* sizeof(int8_t));
1075 kheap_free(KHEAP_DEFAULT
, watcher
->devices_not_to_watch
,
1076 watcher
->num_devices
* sizeof(dev_t
));
1077 kheap_free(KHEAP_DEFAULT
, watcher
,
1078 sizeof(fs_event_watcher
) + watcher
->eventq_size
* sizeof(kfs_event
*));
1082 unlock_watch_table();
1086 #define EVENT_DELAY_IN_MS 10
1087 static thread_call_t event_delivery_timer
= NULL
;
1088 static int timer_set
= 0;
1092 delayed_event_delivery(__unused
void *param0
, __unused
void *param1
)
1098 for (i
= 0; i
< MAX_WATCHERS
; i
++) {
1099 if (watcher_table
[i
] != NULL
&& watcher_table
[i
]->rd
!= watcher_table
[i
]->wr
) {
1100 fsevents_wakeup(watcher_table
[i
]);
1106 unlock_watch_table();
1111 // The watch table must be locked before calling this function.
1114 schedule_event_wakeup(void)
1118 if (event_delivery_timer
== NULL
) {
1119 event_delivery_timer
= thread_call_allocate((thread_call_func_t
)delayed_event_delivery
, NULL
);
1122 clock_interval_to_deadline(EVENT_DELAY_IN_MS
, 1000 * 1000, &deadline
);
1124 thread_call_enter_delayed(event_delivery_timer
, deadline
);
1130 #define MAX_NUM_PENDING 16
1133 // NOTE: the watch table must be locked before calling
1137 watcher_add_event(fs_event_watcher
*watcher
, kfs_event
*kfse
)
1139 if (kfse
->abstime
> watcher
->max_event_id
) {
1140 watcher
->max_event_id
= kfse
->abstime
;
1143 if (((watcher
->wr
+ 1) % watcher
->eventq_size
) == watcher
->rd
) {
1144 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1145 fsevents_wakeup(watcher
);
1149 OSAddAtomic(1, &kfse
->refcount
);
1150 watcher
->event_queue
[watcher
->wr
] = kfse
;
1152 watcher
->wr
= (watcher
->wr
+ 1) % watcher
->eventq_size
;
1155 // wake up the watcher if there are more than MAX_NUM_PENDING events.
1156 // otherwise schedule a timer (if one isn't already set) which will
1157 // send any pending events if no more are received in the next
1158 // EVENT_DELAY_IN_MS milli-seconds.
1160 int32_t num_pending
= 0;
1161 if (watcher
->rd
< watcher
->wr
) {
1162 num_pending
= watcher
->wr
- watcher
->rd
;
1165 if (watcher
->rd
> watcher
->wr
) {
1166 num_pending
= watcher
->wr
+ watcher
->eventq_size
- watcher
->rd
;
1169 if (num_pending
> (watcher
->eventq_size
* 3 / 4) && !(watcher
->flags
& WATCHER_APPLE_SYSTEM_SERVICE
)) {
1170 /* Non-Apple Service is falling behind, start dropping events for this process */
1171 lck_rw_lock_exclusive(&event_handling_lock
);
1172 while (watcher
->rd
!= watcher
->wr
) {
1173 kfse
= watcher
->event_queue
[watcher
->rd
];
1174 watcher
->event_queue
[watcher
->rd
] = NULL
;
1175 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1177 if (kfse
!= NULL
&& kfse
->type
!= FSE_INVALID
&& kfse
->refcount
>= 1) {
1178 release_event_ref(kfse
);
1181 watcher
->flags
|= WATCHER_DROPPED_EVENTS
;
1182 lck_rw_unlock_exclusive(&event_handling_lock
);
1184 printf("fsevents: watcher falling behind: %s (pid: %d) rd: %4d wr: %4d q_size: %4d flags: 0x%x\n",
1185 watcher
->proc_name
, watcher
->pid
, watcher
->rd
, watcher
->wr
,
1186 watcher
->eventq_size
, watcher
->flags
);
1188 fsevents_wakeup(watcher
);
1189 } else if (num_pending
> MAX_NUM_PENDING
) {
1190 fsevents_wakeup(watcher
);
1191 } else if (timer_set
== 0) {
1192 schedule_event_wakeup();
1199 fill_buff(uint16_t type
, int32_t size
, const void *data
,
1200 char *buff
, int32_t *_buff_idx
, int32_t buff_sz
,
1203 int32_t amt
, error
= 0, buff_idx
= *_buff_idx
;
1207 // the +1 on the size is to guarantee that the main data
1208 // copy loop will always copy at least 1 byte
1210 if ((buff_sz
- buff_idx
) <= (int)(2 * sizeof(uint16_t) + 1)) {
1211 if (buff_idx
> uio_resid(uio
)) {
1216 error
= uiomove(buff
, buff_idx
, uio
);
1223 // copy out the header (type & size)
1224 memcpy(&buff
[buff_idx
], &type
, sizeof(uint16_t));
1225 buff_idx
+= sizeof(uint16_t);
1227 tmp
= size
& 0xffff;
1228 memcpy(&buff
[buff_idx
], &tmp
, sizeof(uint16_t));
1229 buff_idx
+= sizeof(uint16_t);
1231 // now copy the body of the data, flushing along the way
1232 // if the buffer fills up.
1235 amt
= (size
< (buff_sz
- buff_idx
)) ? size
: (buff_sz
- buff_idx
);
1236 memcpy(&buff
[buff_idx
], data
, amt
);
1240 data
= (const char *)data
+ amt
;
1241 if (size
> (buff_sz
- buff_idx
)) {
1242 if (buff_idx
> uio_resid(uio
)) {
1246 error
= uiomove(buff
, buff_idx
, uio
);
1253 if (amt
== 0) { // just in case...
1259 *_buff_idx
= buff_idx
;
1265 static int copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
) __attribute__((noinline
));
1268 copy_out_kfse(fs_event_watcher
*watcher
, kfs_event
*kfse
, struct uio
*uio
)
1277 if (kfse
->type
== FSE_INVALID
) {
1278 panic("fsevents: copy_out_kfse: asked to copy out an invalid event (kfse %p, refcount %d fref ptr %p)\n", kfse
, kfse
->refcount
, kfse
->str
);
1281 if (kfse
->flags
& KFSE_BEING_CREATED
) {
1285 if (((kfse
->type
== FSE_RENAME
) || (kfse
->type
== FSE_CLONE
)) && kfse
->dest
== NULL
) {
1287 // This can happen if an event gets recycled but we had a
1288 // pointer to it in our event queue. The event is the
1289 // destination of a rename or clone which we'll process separately
1290 // (that is, another kfse points to this one so it's ok
1291 // to skip this guy because we'll process it when we process
1297 if (watcher
->flags
& WATCHER_WANTS_EXTENDED_INFO
) {
1298 type
= (kfse
->type
& 0xfff);
1300 if (kfse
->flags
& KFSE_CONTAINS_DROPPED_EVENTS
) {
1301 type
|= (FSE_CONTAINS_DROPPED_EVENTS
<< FSE_FLAG_SHIFT
);
1302 } else if (kfse
->flags
& KFSE_COMBINED_EVENTS
) {
1303 type
|= (FSE_COMBINED_EVENTS
<< FSE_FLAG_SHIFT
);
1306 type
= (int32_t)kfse
->type
;
1309 // copy out the type of the event
1310 memcpy(evbuff
, &type
, sizeof(int32_t));
1311 evbuff_idx
+= sizeof(int32_t);
1313 // copy out the pid of the person that generated the event
1314 memcpy(&evbuff
[evbuff_idx
], &kfse
->pid
, sizeof(pid_t
));
1315 evbuff_idx
+= sizeof(pid_t
);
1321 if (kfse
->type
== FSE_DOCID_CHANGED
|| kfse
->type
== FSE_DOCID_CREATED
) {
1322 dev_t dev
= cur
->dev
;
1323 ino64_t ino
= cur
->ino
;
1326 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1331 error
= fill_buff(FSE_ARG_INO
, sizeof(ino64_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1336 memcpy(&ino
, &cur
->str
, sizeof(ino64_t
));
1337 error
= fill_buff(FSE_ARG_INO
, sizeof(ino64_t
), &ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1342 memcpy(&ival
, &cur
->uid
, sizeof(uint64_t)); // the docid gets stuffed into the ino field
1343 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &ival
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1351 if (kfse
->type
== FSE_UNMOUNT_PENDING
) {
1352 dev_t dev
= cur
->dev
;
1354 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1362 if (cur
->str
== NULL
|| cur
->str
[0] == '\0') {
1363 printf("copy_out_kfse:2: empty/short path (%s)\n", cur
->str
);
1364 error
= fill_buff(FSE_ARG_STRING
, 2, "/", evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1366 error
= fill_buff(FSE_ARG_STRING
, cur
->len
, cur
->str
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1372 if (cur
->dev
== 0 && cur
->ino
== 0) {
1373 // this happens when a rename event happens and the
1374 // destination of the rename did not previously exist.
1375 // it thus has no other file info so skip copying out
1376 // the stuff below since it isn't initialized
1381 if (watcher
->flags
& WATCHER_WANTS_COMPACT_EVENTS
) {
1384 finfo_size
= sizeof(dev_t
) + sizeof(ino64_t
) + sizeof(int32_t) + sizeof(uid_t
) + sizeof(gid_t
);
1385 error
= fill_buff(FSE_ARG_FINFO
, finfo_size
, &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1390 error
= fill_buff(FSE_ARG_DEV
, sizeof(dev_t
), &cur
->dev
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1395 error
= fill_buff(FSE_ARG_INO
, sizeof(ino64_t
), &cur
->ino
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1400 error
= fill_buff(FSE_ARG_MODE
, sizeof(int32_t), &cur
->mode
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1405 error
= fill_buff(FSE_ARG_UID
, sizeof(uid_t
), &cur
->uid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1410 error
= fill_buff(FSE_ARG_GID
, sizeof(gid_t
), &cur
->gid
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1423 // very last thing: the time stamp
1424 error
= fill_buff(FSE_ARG_INT64
, sizeof(uint64_t), &cur
->abstime
, evbuff
, &evbuff_idx
, sizeof(evbuff
), uio
);
1429 // check if the FSE_ARG_DONE will fit
1430 if (sizeof(uint16_t) > sizeof(evbuff
) - evbuff_idx
) {
1431 if (evbuff_idx
> uio_resid(uio
)) {
1435 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1442 tmp16
= FSE_ARG_DONE
;
1443 memcpy(&evbuff
[evbuff_idx
], &tmp16
, sizeof(uint16_t));
1444 evbuff_idx
+= sizeof(uint16_t);
1446 // flush any remaining data in the buffer (and hopefully
1447 // in most cases this is the only uiomove we'll do)
1448 if (evbuff_idx
> uio_resid(uio
)) {
1451 error
= uiomove(evbuff
, evbuff_idx
, uio
);
1462 fmod_watch(fs_event_watcher
*watcher
, struct uio
*uio
)
1465 user_ssize_t last_full_event_resid
;
1470 last_full_event_resid
= uio_resid(uio
);
1472 // need at least 2048 bytes of space (maxpathlen + 1 event buf)
1473 if (uio_resid(uio
) < 2048 || watcher
== NULL
) {
1477 if (watcher
->flags
& WATCHER_CLOSING
) {
1481 if (OSAddAtomic(1, &watcher
->num_readers
) != 0) {
1482 // don't allow multiple threads to read from the fd at the same time
1483 OSAddAtomic(-1, &watcher
->num_readers
);
1488 if (watcher
->rd
== watcher
->wr
) {
1489 if (watcher
->flags
& WATCHER_CLOSING
) {
1490 OSAddAtomic(-1, &watcher
->num_readers
);
1493 OSAddAtomic(1, &watcher
->blockers
);
1495 // there's nothing to do, go to sleep
1496 error
= tsleep((caddr_t
)watcher
, PUSER
| PCATCH
, "fsevents_empty", 0);
1498 OSAddAtomic(-1, &watcher
->blockers
);
1500 if (error
!= 0 || (watcher
->flags
& WATCHER_CLOSING
)) {
1501 OSAddAtomic(-1, &watcher
->num_readers
);
1506 // if we dropped events, return that as an event first
1507 if (watcher
->flags
& WATCHER_DROPPED_EVENTS
) {
1508 int32_t val
= FSE_EVENTS_DROPPED
;
1510 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1512 val
= 0; // a fake pid
1513 error
= uiomove((caddr_t
)&val
, sizeof(int32_t), uio
);
1515 tmp16
= FSE_ARG_DONE
; // makes it a consistent msg
1516 error
= uiomove((caddr_t
)&tmp16
, sizeof(int16_t), uio
);
1518 last_full_event_resid
= uio_resid(uio
);
1522 OSAddAtomic(-1, &watcher
->num_readers
);
1526 watcher
->flags
&= ~WATCHER_DROPPED_EVENTS
;
1531 lck_rw_lock_shared(&event_handling_lock
);
1532 while (uio_resid(uio
) > 0 && watcher
->rd
!= watcher
->wr
) {
1533 if (watcher
->flags
& WATCHER_CLOSING
) {
1538 // check if the event is something of interest to us
1539 // (since it may have been recycled/reused and changed
1540 // its type or which device it is for)
1542 kfse
= watcher
->event_queue
[watcher
->rd
];
1543 if (!kfse
|| kfse
->type
== FSE_INVALID
|| kfse
->type
>= watcher
->num_events
|| kfse
->refcount
< 1) {
1547 if (watcher
->event_list
[kfse
->type
] == FSE_REPORT
) {
1548 boolean_t watcher_cares
;
1550 if (watcher
->devices_not_to_watch
== NULL
) {
1551 watcher_cares
= true;
1554 watcher_cares
= watcher_cares_about_dev(watcher
, kfse
->dev
);
1555 unlock_watch_table();
1558 if (watcher_cares
) {
1559 if (!(watcher
->flags
& WATCHER_APPLE_SYSTEM_SERVICE
) && kfse
->type
!= FSE_DOCID_CREATED
&& kfse
->type
!= FSE_DOCID_CHANGED
&& is_ignored_directory(kfse
->str
)) {
1560 // If this is not an Apple System Service, skip specified directories
1566 if (last_event_ptr
== kfse
) {
1567 last_event_ptr
= NULL
;
1568 last_event_type
= -1;
1569 last_coalesced_time
= 0;
1571 error
= copy_out_kfse(watcher
, kfse
, uio
);
1573 // if an event won't fit or encountered an error while
1574 // we were copying it out, then backup to the last full
1575 // event and just bail out. if the error was ENOENT
1576 // then we can continue regular processing, otherwise
1577 // we should unlock things and return.
1578 uio_setresid(uio
, last_full_event_resid
);
1579 if (error
!= ENOENT
) {
1580 lck_rw_unlock_shared(&event_handling_lock
);
1586 last_full_event_resid
= uio_resid(uio
);
1591 watcher
->event_queue
[watcher
->rd
] = NULL
;
1592 watcher
->rd
= (watcher
->rd
+ 1) % watcher
->eventq_size
;
1594 release_event_ref(kfse
);
1596 lck_rw_unlock_shared(&event_handling_lock
);
1598 if (skipped
&& error
== 0) {
1603 OSAddAtomic(-1, &watcher
->num_readers
);
1610 // Shoo watchers away from a volume that's about to be unmounted
1611 // (so that it can be cleanly unmounted).
1614 fsevent_unmount(__unused
struct mount
*mp
, __unused vfs_context_t ctx
)
1616 #if !defined(XNU_TARGET_OS_OSX)
1617 dev_t dev
= mp
->mnt_vfsstat
.f_fsid
.val
[0];
1618 int error
, waitcount
= 0;
1619 struct timespec ts
= {.tv_sec
= 1, .tv_nsec
= 0};
1621 // wait for any other pending unmounts to complete
1623 while (fsevent_unmount_dev
!= 0) {
1624 error
= msleep((caddr_t
)&fsevent_unmount_dev
, &watch_table_lock
, PRIBIO
, "fsevent_unmount_wait", &ts
);
1625 if (error
== EWOULDBLOCK
) {
1628 if (!error
&& (++waitcount
>= 10)) {
1629 error
= EWOULDBLOCK
;
1630 printf("timeout waiting to signal unmount pending for dev %d (fsevent_unmount_dev %d)\n", dev
, fsevent_unmount_dev
);
1633 // there's a problem, bail out
1634 unlock_watch_table();
1638 if (fs_event_type_watchers
[FSE_UNMOUNT_PENDING
] == 0) {
1639 // nobody watching for unmount pending events
1640 unlock_watch_table();
1643 // this is now the current unmount pending
1644 fsevent_unmount_dev
= dev
;
1645 fsevent_unmount_ack_count
= fs_event_type_watchers
[FSE_UNMOUNT_PENDING
];
1646 unlock_watch_table();
1648 // send an event to notify the watcher they need to get off the mount
1649 error
= add_fsevent(FSE_UNMOUNT_PENDING
, ctx
, FSE_ARG_DEV
, dev
, FSE_ARG_DONE
);
1651 // wait for acknowledgment(s) (give up if it takes too long)
1654 while (fsevent_unmount_dev
== dev
) {
1655 error
= msleep((caddr_t
)&fsevent_unmount_dev
, &watch_table_lock
, PRIBIO
, "fsevent_unmount_pending", &ts
);
1656 if (error
== EWOULDBLOCK
) {
1659 if (!error
&& (++waitcount
>= 10)) {
1660 error
= EWOULDBLOCK
;
1661 printf("unmount pending ack timeout for dev %d\n", dev
);
1664 // there's a problem, bail out
1665 if (fsevent_unmount_dev
== dev
) {
1666 fsevent_unmount_dev
= 0;
1667 fsevent_unmount_ack_count
= 0;
1669 wakeup((caddr_t
)&fsevent_unmount_dev
);
1673 unlock_watch_table();
1674 #endif /* ! XNU_TARGET_OS_OSX */
1679 // /dev/fsevents device code
1681 static int fsevents_installed
= 0;
1683 typedef struct fsevent_handle
{
1686 fs_event_watcher
*watcher
;
1687 struct klist knotes
;
1691 #define FSEH_CLOSING 0x0001
1694 fseventsf_read(struct fileproc
*fp
, struct uio
*uio
,
1695 __unused
int flags
, __unused vfs_context_t ctx
)
1697 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->fp_glob
->fg_data
;
1700 error
= fmod_watch(fseh
->watcher
, uio
);
1706 #pragma pack(push, 4)
1707 typedef struct fsevent_dev_filter_args32
{
1708 uint32_t num_devices
;
1709 user32_addr_t devices
;
1710 } fsevent_dev_filter_args32
;
1711 typedef struct fsevent_dev_filter_args64
{
1712 uint32_t num_devices
;
1713 user64_addr_t devices
;
1714 } fsevent_dev_filter_args64
;
1717 #define FSEVENTS_DEVICE_FILTER_32 _IOW('s', 100, fsevent_dev_filter_args32)
1718 #define FSEVENTS_DEVICE_FILTER_64 _IOW('s', 100, fsevent_dev_filter_args64)
1721 fseventsf_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
, vfs_context_t ctx
)
1723 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->fp_glob
->fg_data
;
1725 fsevent_dev_filter_args64
*devfilt_args
, _devfilt_args
;
1727 OSAddAtomic(1, &fseh
->active
);
1728 if (fseh
->flags
& FSEH_CLOSING
) {
1729 OSAddAtomic(-1, &fseh
->active
);
1738 case FSEVENTS_WANT_COMPACT_EVENTS
: {
1739 fseh
->watcher
->flags
|= WATCHER_WANTS_COMPACT_EVENTS
;
1743 case FSEVENTS_WANT_EXTENDED_INFO
: {
1744 fseh
->watcher
->flags
|= WATCHER_WANTS_EXTENDED_INFO
;
1748 case FSEVENTS_GET_CURRENT_ID
: {
1749 *(uint64_t *)data
= fseh
->watcher
->max_event_id
;
1754 case FSEVENTS_DEVICE_FILTER_32
: {
1755 if (proc_is64bit(vfs_context_proc(ctx
))) {
1759 fsevent_dev_filter_args32
*devfilt_args32
= (fsevent_dev_filter_args32
*)data
;
1761 devfilt_args
= &_devfilt_args
;
1762 memset(devfilt_args
, 0, sizeof(fsevent_dev_filter_args64
));
1763 devfilt_args
->num_devices
= devfilt_args32
->num_devices
;
1764 devfilt_args
->devices
= CAST_USER_ADDR_T(devfilt_args32
->devices
);
1765 goto handle_dev_filter
;
1768 case FSEVENTS_DEVICE_FILTER_64
:
1769 if (!proc_is64bit(vfs_context_proc(ctx
))) {
1773 devfilt_args
= (fsevent_dev_filter_args64
*)data
;
1777 int new_num_devices
, old_num_devices
= 0;
1778 dev_t
*devices_not_to_watch
, *tmp
= NULL
;
1780 if (devfilt_args
->num_devices
> 256) {
1785 new_num_devices
= devfilt_args
->num_devices
;
1786 if (new_num_devices
== 0) {
1789 tmp
= fseh
->watcher
->devices_not_to_watch
;
1790 fseh
->watcher
->devices_not_to_watch
= NULL
;
1791 old_num_devices
= fseh
->watcher
->num_devices
;
1792 fseh
->watcher
->num_devices
= new_num_devices
;
1794 unlock_watch_table();
1795 kheap_free(KHEAP_DEFAULT
, tmp
, old_num_devices
* sizeof(dev_t
));
1799 devices_not_to_watch
= kheap_alloc(KHEAP_DEFAULT
,
1800 new_num_devices
* sizeof(dev_t
), Z_WAITOK
);
1801 if (devices_not_to_watch
== NULL
) {
1806 ret
= copyin((user_addr_t
)devfilt_args
->devices
,
1807 (void *)devices_not_to_watch
,
1808 new_num_devices
* sizeof(dev_t
));
1810 kheap_free(KHEAP_DEFAULT
, devices_not_to_watch
,
1811 new_num_devices
* sizeof(dev_t
));
1816 old_num_devices
= fseh
->watcher
->num_devices
;
1817 fseh
->watcher
->num_devices
= new_num_devices
;
1818 tmp
= fseh
->watcher
->devices_not_to_watch
;
1819 fseh
->watcher
->devices_not_to_watch
= devices_not_to_watch
;
1820 unlock_watch_table();
1822 kheap_free(KHEAP_DEFAULT
, tmp
, old_num_devices
* sizeof(dev_t
));
1827 case FSEVENTS_UNMOUNT_PENDING_ACK
: {
1829 dev_t dev
= *(dev_t
*)data
;
1830 if (fsevent_unmount_dev
== dev
) {
1831 if (--fsevent_unmount_ack_count
<= 0) {
1832 fsevent_unmount_dev
= 0;
1833 wakeup((caddr_t
)&fsevent_unmount_dev
);
1836 printf("unexpected unmount pending ack %d (%d)\n", dev
, fsevent_unmount_dev
);
1839 unlock_watch_table();
1848 OSAddAtomic(-1, &fseh
->active
);
1854 fseventsf_select(struct fileproc
*fp
, int which
, __unused
void *wql
, vfs_context_t ctx
)
1856 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->fp_glob
->fg_data
;
1859 if ((which
!= FREAD
) || (fseh
->watcher
->flags
& WATCHER_CLOSING
)) {
1864 // if there's nothing in the queue, we're not ready
1865 if (fseh
->watcher
->rd
!= fseh
->watcher
->wr
) {
1870 selrecord(vfs_context_proc(ctx
), &fseh
->si
, wql
);
1879 fseventsf_stat(__unused
struct fileproc
*fp
, __unused
struct stat
*sb
, __unused vfs_context_t ctx
)
1886 fseventsf_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1888 fsevent_handle
*fseh
= (struct fsevent_handle
*)fg
->fg_data
;
1889 fs_event_watcher
*watcher
;
1891 OSBitOrAtomic(FSEH_CLOSING
, &fseh
->flags
);
1892 while (OSAddAtomic(0, &fseh
->active
) > 0) {
1893 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "fsevents-close", 1);
1896 watcher
= fseh
->watcher
;
1898 fseh
->watcher
= NULL
;
1900 remove_watcher(watcher
);
1901 kheap_free(KHEAP_DEFAULT
, fseh
, sizeof(fsevent_handle
));
1907 filt_fsevent_detach(struct knote
*kn
)
1909 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
1913 KNOTE_DETACH(&fseh
->knotes
, kn
);
1915 unlock_watch_table();
1919 * Determine whether this knote should be active
1921 * This is kind of subtle.
1922 * --First, notice if the vnode has been revoked: in so, override hint
1923 * --EVFILT_READ knotes are checked no matter what the hint is
1924 * --Other knotes activate based on hint.
1925 * --If hint is revoke, set special flags and activate
1928 filt_fsevent_common(struct knote
*kn
, struct kevent_qos_s
*kev
, long hint
)
1930 fsevent_handle
*fseh
= (struct fsevent_handle
*)kn
->kn_hook
;
1932 int32_t rd
, wr
, amt
;
1935 if (NOTE_REVOKE
== hint
) {
1936 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
1940 rd
= fseh
->watcher
->rd
;
1941 wr
= fseh
->watcher
->wr
;
1945 amt
= fseh
->watcher
->eventq_size
- (rd
- wr
);
1948 switch (kn
->kn_filter
) {
1951 activate
= (data
!= 0);
1954 /* Check events this note matches against the hint */
1955 if (kn
->kn_sfflags
& hint
) {
1956 kn
->kn_fflags
|= hint
; /* Set which event occurred */
1958 if (kn
->kn_fflags
!= 0) {
1967 if (activate
&& kev
) {
1968 knote_fill_kevent(kn
, kev
, data
);
1974 filt_fsevent(struct knote
*kn
, long hint
)
1976 return filt_fsevent_common(kn
, NULL
, hint
);
1980 filt_fsevent_touch(struct knote
*kn
, struct kevent_qos_s
*kev
)
1986 /* accept new fflags/data as saved */
1987 kn
->kn_sfflags
= kev
->fflags
;
1988 kn
->kn_sdata
= kev
->data
;
1990 /* restrict the current results to the (smaller?) set of new interest */
1992 * For compatibility with previous implementations, we leave kn_fflags
1993 * as they were before.
1995 //kn->kn_fflags &= kev->fflags;
1997 /* determine if the filter is now fired */
1998 res
= filt_fsevent_common(kn
, NULL
, 0);
2000 unlock_watch_table();
2006 filt_fsevent_process(struct knote
*kn
, struct kevent_qos_s
*kev
)
2012 res
= filt_fsevent_common(kn
, kev
, 0);
2014 unlock_watch_table();
2019 SECURITY_READ_ONLY_EARLY(struct filterops
) fsevent_filtops
= {
2022 .f_detach
= filt_fsevent_detach
,
2023 .f_event
= filt_fsevent
,
2024 .f_touch
= filt_fsevent_touch
,
2025 .f_process
= filt_fsevent_process
,
2029 fseventsf_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
2030 __unused
struct kevent_qos_s
*kev
)
2032 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->fp_glob
->fg_data
;
2035 kn
->kn_hook
= (void*)fseh
;
2036 kn
->kn_filtid
= EVFILTID_FSEVENT
;
2040 KNOTE_ATTACH(&fseh
->knotes
, kn
);
2042 /* check to see if it is fired already */
2043 res
= filt_fsevent_common(kn
, NULL
, 0);
2045 unlock_watch_table();
2052 fseventsf_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2055 fsevent_handle
*fseh
= (struct fsevent_handle
*)fp
->fp_glob
->fg_data
;
2057 // if there are people still waiting, sleep for 10ms to
2058 // let them clean up and get out of there. however we
2059 // also don't want to get stuck forever so if they don't
2060 // exit after 5 seconds we're tearing things down anyway.
2061 while (fseh
->watcher
->blockers
&& counter
++ < 500) {
2062 // issue wakeup in case anyone is blocked waiting for an event
2063 // do this each time we wakeup in case the blocker missed
2064 // the wakeup due to the unprotected test of WATCHER_CLOSING
2065 // and decision to tsleep in fmod_watch... this bit of
2066 // latency is a decent tradeoff against not having to
2067 // take and drop a lock in fmod_watch
2069 fsevents_wakeup(fseh
->watcher
);
2070 unlock_watch_table();
2072 tsleep((caddr_t
)fseh
->watcher
, PRIBIO
, "watcher-close", 1);
2080 fseventsopen(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2082 if (!kauth_cred_issuser(kauth_cred_get())) {
2090 fseventsclose(__unused dev_t dev
, __unused
int flag
, __unused
int mode
, __unused
struct proc
*p
)
2096 fseventsread(__unused dev_t dev
, __unused
struct uio
*uio
, __unused
int ioflag
)
2103 parse_buffer_and_add_events(const char *buffer
, size_t bufsize
, vfs_context_t ctx
, size_t *remainder
)
2105 const fse_info
*finfo
, *dest_finfo
;
2106 const char *path
, *ptr
, *dest_path
, *event_start
= buffer
;
2107 size_t path_len
, dest_path_len
;
2112 while ((ptr
+ sizeof(int) + sizeof(fse_info
) + 1) < buffer
+ bufsize
) {
2113 type
= *(const int *)ptr
;
2114 if (type
< 0 || type
>= FSE_MAX_EVENTS
) {
2121 finfo
= (const fse_info
*)ptr
;
2122 ptr
+= sizeof(fse_info
);
2125 while (ptr
< buffer
+ bufsize
&& *ptr
!= '\0') {
2129 if (ptr
>= buffer
+ bufsize
) {
2133 ptr
++; // advance over the trailing '\0'
2135 path_len
= ptr
- path
;
2137 if (type
!= FSE_RENAME
&& type
!= FSE_EXCHANGE
&& type
!= FSE_CLONE
) {
2138 event_start
= ptr
; // record where the next event starts
2140 err
= add_fsevent(type
, ctx
, FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
, FSE_ARG_DONE
);
2148 // if we're here we have to slurp up the destination finfo
2149 // and path so that we can pass them to the add_fsevent()
2150 // call. basically it's a copy of the above code.
2152 dest_finfo
= (const fse_info
*)ptr
;
2153 ptr
+= sizeof(fse_info
);
2156 while (ptr
< buffer
+ bufsize
&& *ptr
!= '\0') {
2160 if (ptr
>= buffer
+ bufsize
) {
2164 ptr
++; // advance over the trailing '\0'
2165 event_start
= ptr
; // record where the next event starts
2167 dest_path_len
= ptr
- dest_path
;
2169 // If the destination inode number is non-zero, generate a rename
2170 // with both source and destination FSE_ARG_FINFO. Otherwise generate
2171 // a rename with only one FSE_ARG_FINFO. If you need to inject an
2172 // exchange with an inode of zero, just make that inode (and its path)
2173 // come in as the first one, not the second.
2175 if (dest_finfo
->ino
) {
2176 err
= add_fsevent(type
, ctx
,
2177 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2178 FSE_ARG_STRING
, dest_path_len
, dest_path
, FSE_ARG_FINFO
, dest_finfo
,
2181 err
= add_fsevent(type
, ctx
,
2182 FSE_ARG_STRING
, path_len
, path
, FSE_ARG_FINFO
, finfo
,
2183 FSE_ARG_STRING
, dest_path_len
, dest_path
,
2192 // if the last event wasn't complete, set the remainder
2193 // to be the last event start boundary.
2195 *remainder
= (long)((buffer
+ bufsize
) - event_start
);
2202 // Note: this buffer size can not ever be less than
2203 // 2*MAXPATHLEN + 2*sizeof(fse_info) + sizeof(int)
2204 // because that is the max size for a single event.
2205 // I made it 4k to be a "nice" size. making it
2206 // smaller is not a good idea.
2208 #define WRITE_BUFFER_SIZE 4096
2209 char *write_buffer
= NULL
;
2212 fseventswrite(__unused dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
2215 size_t count
, offset
= 0, remainder
= 0;
2216 vfs_context_t ctx
= vfs_context_current();
2218 lck_mtx_lock(&event_writer_lock
);
2220 if (write_buffer
== NULL
) {
2221 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&write_buffer
, WRITE_BUFFER_SIZE
, VM_KERN_MEMORY_FILE
)) {
2222 lck_mtx_unlock(&event_writer_lock
);
2228 // this loop copies in and processes the events written.
2229 // it takes care to copy in reasonable size chunks and
2230 // process them. if there is an event that spans a chunk
2231 // boundary we're careful to copy those bytes down to the
2232 // beginning of the buffer and read the next chunk in just
2235 while (uio_resid(uio
)) {
2236 count
= MIN(WRITE_BUFFER_SIZE
- offset
, (size_t)uio_resid(uio
));
2238 error
= uiomove(write_buffer
+ offset
, (int)count
, uio
);
2243 error
= parse_buffer_and_add_events(write_buffer
, offset
+ count
, ctx
, &remainder
);
2249 // if there's any remainder, copy it down to the beginning
2250 // of the buffer so that it will get processed the next time
2251 // through the loop. note that the remainder always starts
2252 // at an event boundary.
2254 memmove(write_buffer
, (write_buffer
+ count
+ offset
) - remainder
, remainder
);
2258 lck_mtx_unlock(&event_writer_lock
);
2264 static const struct fileops fsevents_fops
= {
2265 .fo_type
= DTYPE_FSEVENTS
,
2266 .fo_read
= fseventsf_read
,
2267 .fo_write
= fo_no_write
,
2268 .fo_ioctl
= fseventsf_ioctl
,
2269 .fo_select
= fseventsf_select
,
2270 .fo_close
= fseventsf_close
,
2271 .fo_kqfilter
= fseventsf_kqfilter
,
2272 .fo_drain
= fseventsf_drain
,
2275 typedef struct fsevent_clone_args32
{
2276 user32_addr_t event_list
;
2278 int32_t event_queue_depth
;
2280 } fsevent_clone_args32
;
2282 typedef struct fsevent_clone_args64
{
2283 user64_addr_t event_list
;
2285 int32_t event_queue_depth
;
2287 } fsevent_clone_args64
;
2289 #define FSEVENTS_CLONE_32 _IOW('s', 1, fsevent_clone_args32)
2290 #define FSEVENTS_CLONE_64 _IOW('s', 1, fsevent_clone_args64)
2293 fseventsioctl(__unused dev_t dev
, u_long cmd
, caddr_t data
, __unused
int flag
, struct proc
*p
)
2297 fsevent_handle
*fseh
= NULL
;
2298 fsevent_clone_args64
*fse_clone_args
, _fse_clone
;
2300 int is64bit
= proc_is64bit(p
);
2303 case FSEVENTS_CLONE_32
: {
2307 fsevent_clone_args32
*args32
= (fsevent_clone_args32
*)data
;
2309 fse_clone_args
= &_fse_clone
;
2310 memset(fse_clone_args
, 0, sizeof(fsevent_clone_args64
));
2312 fse_clone_args
->event_list
= CAST_USER_ADDR_T(args32
->event_list
);
2313 fse_clone_args
->num_events
= args32
->num_events
;
2314 fse_clone_args
->event_queue_depth
= args32
->event_queue_depth
;
2315 fse_clone_args
->fd
= CAST_USER_ADDR_T(args32
->fd
);
2319 case FSEVENTS_CLONE_64
:
2323 fse_clone_args
= (fsevent_clone_args64
*)data
;
2326 if (fse_clone_args
->num_events
<= 0 || fse_clone_args
->num_events
> 4096) {
2330 fseh
= kheap_alloc(KHEAP_DEFAULT
, sizeof(fsevent_handle
), Z_WAITOK
| Z_ZERO
);
2335 klist_init(&fseh
->knotes
);
2337 event_list
= kheap_alloc(KHEAP_DEFAULT
,
2338 fse_clone_args
->num_events
* sizeof(int8_t), Z_WAITOK
);
2339 if (event_list
== NULL
) {
2340 kheap_free(KHEAP_DEFAULT
, fseh
, sizeof(fsevent_handle
));
2344 error
= copyin((user_addr_t
)fse_clone_args
->event_list
,
2346 fse_clone_args
->num_events
* sizeof(int8_t));
2348 kheap_free(KHEAP_DEFAULT
, event_list
,
2349 fse_clone_args
->num_events
* sizeof(int8_t));
2350 kheap_free(KHEAP_DEFAULT
, fseh
, sizeof(fsevent_handle
));
2355 * Lock down the user's "fd" result buffer so it's safe
2356 * to hold locks while we copy it out.
2358 error
= vslock((user_addr_t
)fse_clone_args
->fd
,
2361 kheap_free(KHEAP_DEFAULT
, event_list
,
2362 fse_clone_args
->num_events
* sizeof(int8_t));
2363 kheap_free(KHEAP_DEFAULT
, fseh
, sizeof(fsevent_handle
));
2367 error
= add_watcher(event_list
,
2368 fse_clone_args
->num_events
,
2369 fse_clone_args
->event_queue_depth
,
2373 vsunlock((user_addr_t
)fse_clone_args
->fd
,
2374 sizeof(int32_t), 0);
2375 kheap_free(KHEAP_DEFAULT
, event_list
,
2376 fse_clone_args
->num_events
* sizeof(int8_t));
2377 kheap_free(KHEAP_DEFAULT
, fseh
, sizeof(fsevent_handle
));
2381 fseh
->watcher
->fseh
= fseh
;
2383 error
= falloc(p
, &f
, &fd
, vfs_context_current());
2385 remove_watcher(fseh
->watcher
);
2386 vsunlock((user_addr_t
)fse_clone_args
->fd
,
2387 sizeof(int32_t), 0);
2388 kheap_free(KHEAP_DEFAULT
, event_list
,
2389 fse_clone_args
->num_events
* sizeof(int8_t));
2390 kheap_free(KHEAP_DEFAULT
, fseh
, sizeof(fsevent_handle
));
2394 f
->fp_glob
->fg_flag
= FREAD
| FWRITE
;
2395 f
->fp_glob
->fg_ops
= &fsevents_fops
;
2396 f
->fp_glob
->fg_data
= (caddr_t
) fseh
;
2398 * We can safely hold the proc_fdlock across this copyout()
2399 * because of the vslock() call above. The vslock() call
2400 * also ensures that we will never get an error, so assert
2403 error
= copyout((void *)&fd
, (user_addr_t
)fse_clone_args
->fd
, sizeof(int32_t));
2406 procfdtbl_releasefd(p
, fd
, NULL
);
2407 fp_drop(p
, fd
, f
, 1);
2410 vsunlock((user_addr_t
)fse_clone_args
->fd
,
2411 sizeof(int32_t), 1);
2423 fsevents_wakeup(fs_event_watcher
*watcher
)
2425 selwakeup(&watcher
->fseh
->si
);
2426 KNOTE(&watcher
->fseh
->knotes
, NOTE_WRITE
| NOTE_NONE
);
2427 wakeup((caddr_t
)watcher
);
2432 * A struct describing which functions will get invoked for certain
2435 static const struct cdevsw fsevents_cdevsw
=
2437 .d_open
= fseventsopen
,
2438 .d_close
= fseventsclose
,
2439 .d_read
= fseventsread
,
2440 .d_write
= fseventswrite
,
2441 .d_ioctl
= fseventsioctl
,
2442 .d_stop
= (stop_fcn_t
*)&nulldev
,
2443 .d_reset
= (reset_fcn_t
*)&nulldev
,
2444 .d_select
= eno_select
,
2446 .d_strategy
= eno_strat
,
2447 .d_reserved_1
= eno_getc
,
2448 .d_reserved_2
= eno_putc
,
2453 * Called to initialize our device,
2454 * and to register ourselves with devfs
2462 if (fsevents_installed
) {
2466 fsevents_installed
= 1;
2468 ret
= cdevsw_add(-1, &fsevents_cdevsw
);
2470 fsevents_installed
= 0;
2474 devfs_make_node(makedev(ret
, 0), DEVFS_CHAR
,
2475 UID_ROOT
, GID_WHEEL
, 0644, "fsevents", 0);
2477 fsevents_internal_init();
2484 return zalloc(ZV_NAMEI
);
2488 release_pathbuff(char *path
)
2493 zfree(ZV_NAMEI
, path
);
2497 get_fse_info(struct vnode
*vp
, fse_info
*fse
, __unused vfs_context_t ctx
)
2499 struct vnode_attr va
;
2502 VATTR_WANTED(&va
, va_fsid
);
2503 va
.va_vaflags
|= VA_REALFSID
;
2504 VATTR_WANTED(&va
, va_fileid
);
2505 VATTR_WANTED(&va
, va_mode
);
2506 VATTR_WANTED(&va
, va_uid
);
2507 VATTR_WANTED(&va
, va_gid
);
2508 if (vp
->v_flag
& VISHARDLINK
) {
2509 if (vp
->v_type
== VDIR
) {
2510 VATTR_WANTED(&va
, va_dirlinkcount
);
2512 VATTR_WANTED(&va
, va_nlink
);
2516 if (vnode_getattr(vp
, &va
, vfs_context_kernel()) != 0) {
2517 memset(fse
, 0, sizeof(fse_info
));
2521 return vnode_get_fse_info_from_vap(vp
, fse
, &va
);
2525 vnode_get_fse_info_from_vap(vnode_t vp
, fse_info
*fse
, struct vnode_attr
*vap
)
2527 fse
->ino
= (ino64_t
)vap
->va_fileid
;
2528 fse
->dev
= (dev_t
)vap
->va_fsid
;
2529 fse
->mode
= (int32_t)vnode_vttoif(vnode_vtype(vp
)) | vap
->va_mode
;
2530 fse
->uid
= (uid_t
)vap
->va_uid
;
2531 fse
->gid
= (gid_t
)vap
->va_gid
;
2532 if (vp
->v_flag
& VISHARDLINK
) {
2533 fse
->mode
|= FSE_MODE_HLINK
;
2534 if (vp
->v_type
== VDIR
) {
2535 fse
->nlink
= (uint64_t)vap
->va_dirlinkcount
;
2537 fse
->nlink
= (uint64_t)vap
->va_nlink
;
2545 create_fsevent_from_kevent(vnode_t vp
, uint32_t kevents
, struct vnode_attr
*vap
)
2547 int fsevent_type
= FSE_CONTENT_MODIFIED
, len
; // the default is the most pessimistic
2548 char pathbuf
[MAXPATHLEN
];
2552 if (kevents
& VNODE_EVENT_DELETE
) {
2553 fsevent_type
= FSE_DELETE
;
2554 } else if (kevents
& (VNODE_EVENT_EXTEND
| VNODE_EVENT_WRITE
)) {
2555 fsevent_type
= FSE_CONTENT_MODIFIED
;
2556 } else if (kevents
& VNODE_EVENT_LINK
) {
2557 fsevent_type
= FSE_CREATE_FILE
;
2558 } else if (kevents
& VNODE_EVENT_RENAME
) {
2559 fsevent_type
= FSE_CREATE_FILE
; // XXXdbg - should use FSE_RENAME but we don't have the destination info;
2560 } else if (kevents
& (VNODE_EVENT_FILE_CREATED
| VNODE_EVENT_FILE_REMOVED
| VNODE_EVENT_DIR_CREATED
| VNODE_EVENT_DIR_REMOVED
)) {
2561 fsevent_type
= FSE_STAT_CHANGED
; // XXXdbg - because vp is a dir and the thing created/removed lived inside it
2562 } else { // a catch all for VNODE_EVENT_PERMS, VNODE_EVENT_ATTRIB and anything else
2563 fsevent_type
= FSE_STAT_CHANGED
;
2566 // printf("convert_kevent: kevents 0x%x fsevent type 0x%x (for %s)\n", kevents, fsevent_type, vp->v_name ? vp->v_name : "(no-name)");
2568 fse
.dev
= vap
->va_fsid
;
2569 fse
.ino
= vap
->va_fileid
;
2570 fse
.mode
= vnode_vttoif(vnode_vtype(vp
)) | (uint32_t)vap
->va_mode
;
2571 if (vp
->v_flag
& VISHARDLINK
) {
2572 fse
.mode
|= FSE_MODE_HLINK
;
2573 if (vp
->v_type
== VDIR
) {
2574 fse
.nlink
= vap
->va_dirlinkcount
;
2576 fse
.nlink
= vap
->va_nlink
;
2580 if (vp
->v_type
== VDIR
) {
2581 fse
.mode
|= FSE_REMOTE_DIR_EVENT
;
2585 fse
.uid
= vap
->va_uid
;
2586 fse
.gid
= vap
->va_gid
;
2588 len
= sizeof(pathbuf
);
2589 if (vn_getpath_no_firmlink(vp
, pathbuf
, &len
) == 0) {
2590 add_fsevent(fsevent_type
, vfs_context_current(), FSE_ARG_STRING
, len
, pathbuf
, FSE_ARG_FINFO
, &fse
, FSE_ARG_DONE
);
2595 #else /* CONFIG_FSE */
2597 #include <sys/fsevents.h>
2600 * The get_pathbuff and release_pathbuff routines are used in places not
2601 * related to fsevents, and it's a handy abstraction, so define trivial
2602 * versions that don't cache a pool of buffers. This way, we don't have
2603 * to conditionalize the callers, and they still get the advantage of the
2604 * pool of buffers if CONFIG_FSE is turned on.
2609 return zalloc(ZV_NAMEI
);
2613 release_pathbuff(char *path
)
2615 zfree(ZV_NAMEI
, path
);
2619 add_fsevent(__unused
int type
, __unused vfs_context_t ctx
, ...)
2625 need_fsevent(__unused
int type
, __unused vnode_t vp
)
2630 #endif /* CONFIG_FSE */