2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 notify implementation using inotify
25 #include "../librpc/gen_ndr/notify.h"
26 #include "smbd/smbd.h"
27 #include "lib/util/sys_rw_data.h"
29 #include <sys/inotify.h>
31 /* glibc < 2.5 headers don't have these defines */
33 #define IN_ONLYDIR 0x01000000
36 #define IN_MASK_ADD 0x20000000
39 struct inotify_private
{
40 struct sys_notify_context
*ctx
;
42 struct inotify_watch_context
*watches
;
45 struct inotify_watch_context
{
46 struct inotify_watch_context
*next
, *prev
;
47 struct inotify_private
*in
;
49 void (*callback
)(struct sys_notify_context
*ctx
,
51 struct notify_event
*ev
,
54 uint32_t mask
; /* the inotify mask */
55 uint32_t filter
; /* the windows completion filter */
61 map from a change notify mask to a inotify mask. Remove any bits
66 uint32_t inotify_mask
;
67 } inotify_mapping
[] = {
68 {FILE_NOTIFY_CHANGE_FILE_NAME
, IN_CREATE
|IN_DELETE
|IN_MOVED_FROM
|IN_MOVED_TO
},
69 {FILE_NOTIFY_CHANGE_DIR_NAME
, IN_CREATE
|IN_DELETE
|IN_MOVED_FROM
|IN_MOVED_TO
},
70 {FILE_NOTIFY_CHANGE_ATTRIBUTES
, IN_ATTRIB
|IN_MOVED_TO
|IN_MOVED_FROM
|IN_MODIFY
},
71 {FILE_NOTIFY_CHANGE_LAST_WRITE
, IN_ATTRIB
},
72 {FILE_NOTIFY_CHANGE_LAST_ACCESS
, IN_ATTRIB
},
73 {FILE_NOTIFY_CHANGE_EA
, IN_ATTRIB
},
74 {FILE_NOTIFY_CHANGE_SECURITY
, IN_ATTRIB
}
77 static uint32_t inotify_map(uint32_t *filter
)
81 for (i
=0;i
<ARRAY_SIZE(inotify_mapping
);i
++) {
82 if (inotify_mapping
[i
].notify_mask
& *filter
) {
83 out
|= inotify_mapping
[i
].inotify_mask
;
84 *filter
&= ~inotify_mapping
[i
].notify_mask
;
91 * Map inotify mask back to filter. This returns all filters that
92 * could have created the inotify watch.
94 static uint32_t inotify_map_mask_to_filter(uint32_t mask
)
99 for (i
= 0; i
< ARRAY_SIZE(inotify_mapping
); i
++) {
100 if (inotify_mapping
[i
].inotify_mask
& mask
) {
101 filter
|= inotify_mapping
[i
].notify_mask
;
105 if (mask
& IN_ISDIR
) {
106 filter
&= ~FILE_NOTIFY_CHANGE_FILE_NAME
;
108 filter
&= ~FILE_NOTIFY_CHANGE_DIR_NAME
;
115 destroy the inotify private context
117 static int inotify_destructor(struct inotify_private
*in
)
125 see if a particular event from inotify really does match a requested
128 static bool filter_match(struct inotify_watch_context
*w
,
129 struct inotify_event
*e
)
133 DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
134 e
->mask
, w
->mask
, w
->filter
));
136 if ((e
->mask
& w
->mask
) == 0) {
137 /* this happens because inotify_add_watch() coalesces watches on the same
138 path, oring their masks together */
142 /* SMB separates the filters for files and directories */
143 if (e
->mask
& IN_ISDIR
) {
144 ok
= ((w
->filter
& FILE_NOTIFY_CHANGE_DIR_NAME
) != 0);
148 if ((e
->mask
& IN_ATTRIB
) &&
149 (w
->filter
& (FILE_NOTIFY_CHANGE_ATTRIBUTES
|
150 FILE_NOTIFY_CHANGE_LAST_WRITE
|
151 FILE_NOTIFY_CHANGE_LAST_ACCESS
|
152 FILE_NOTIFY_CHANGE_EA
|
153 FILE_NOTIFY_CHANGE_SECURITY
))) {
156 if ((e
->mask
& IN_MODIFY
) &&
157 (w
->filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)) {
161 ok
= ((w
->filter
& FILE_NOTIFY_CHANGE_FILE_NAME
) != 0);
168 dispatch one inotify event
170 the cookies are used to correctly handle renames
172 static void inotify_dispatch(struct inotify_private
*in
,
173 struct inotify_event
*e
,
175 uint32_t prev_cookie
,
176 struct inotify_event
*e2
)
178 struct inotify_watch_context
*w
, *next
;
179 struct notify_event ne
;
182 DEBUG(10, ("inotify_dispatch called with mask=%x, name=[%s]\n",
183 e
->mask
, e
->len
? e
->name
: ""));
185 /* ignore extraneous events, such as unmount and IN_IGNORED events */
186 if ((e
->mask
& (IN_ATTRIB
|IN_MODIFY
|IN_CREATE
|IN_DELETE
|
187 IN_MOVED_FROM
|IN_MOVED_TO
)) == 0) {
191 /* map the inotify mask to a action. This gets complicated for
193 if (e
->mask
& IN_CREATE
) {
194 ne
.action
= NOTIFY_ACTION_ADDED
;
195 } else if (e
->mask
& IN_DELETE
) {
196 ne
.action
= NOTIFY_ACTION_REMOVED
;
197 } else if (e
->mask
& IN_MOVED_FROM
) {
198 if (e2
!= NULL
&& e2
->cookie
== e
->cookie
&&
200 ne
.action
= NOTIFY_ACTION_OLD_NAME
;
202 ne
.action
= NOTIFY_ACTION_REMOVED
;
204 } else if (e
->mask
& IN_MOVED_TO
) {
205 if ((e
->cookie
== prev_cookie
) && (e
->wd
== prev_wd
)) {
206 ne
.action
= NOTIFY_ACTION_NEW_NAME
;
208 ne
.action
= NOTIFY_ACTION_ADDED
;
211 ne
.action
= NOTIFY_ACTION_MODIFIED
;
215 filter
= inotify_map_mask_to_filter(e
->mask
);
217 DBG_DEBUG("ne.action = %d, ne.path = %s, filter = %d\n",
218 ne
.action
, ne
.path
, filter
);
220 /* find any watches that have this watch descriptor */
221 for (w
=in
->watches
;w
;w
=next
) {
223 if (w
->wd
== e
->wd
&& filter_match(w
, e
)) {
225 w
->callback(in
->ctx
, w
->private_data
, &ne
, filter
);
229 if ((ne
.action
== NOTIFY_ACTION_NEW_NAME
) &&
230 ((e
->mask
& IN_ISDIR
) == 0)) {
233 * SMB expects a file rename to generate three events, two for
234 * the rename and the other for a modify of the
235 * destination. Strange!
238 ne
.action
= NOTIFY_ACTION_MODIFIED
;
241 for (w
=in
->watches
;w
;w
=next
) {
243 if (w
->wd
== e
->wd
&& filter_match(w
, e
) &&
244 !(w
->filter
& FILE_NOTIFY_CHANGE_CREATION
)) {
246 w
->callback(in
->ctx
, w
->private_data
, &ne
,
254 called when the kernel has some events for us
256 static void inotify_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
257 uint16_t flags
, void *private_data
)
259 struct inotify_private
*in
= talloc_get_type(private_data
,
260 struct inotify_private
);
262 struct inotify_event
*e0
, *e
;
263 uint32_t prev_cookie
=0;
268 we must use FIONREAD as we cannot predict the length of the
269 filenames, and thus can't know how much to allocate
272 if (ioctl(in
->fd
, FIONREAD
, &bufsize
) != 0 ||
274 DEBUG(0,("No data on inotify fd?!\n"));
279 e0
= e
= (struct inotify_event
*)TALLOC_SIZE(in
, bufsize
+ 1);
280 if (e
== NULL
) return;
281 ((uint8_t *)e
)[bufsize
] = '\0';
283 ret
= read_data(in
->fd
, e0
, bufsize
);
284 if (ret
!= bufsize
) {
285 DEBUG(0, ("Failed to read all inotify data - %s\n",
288 /* the inotify fd will now be out of sync,
289 * can't keep reading data off it */
294 /* we can get more than one event in the buffer */
295 while (e
&& (bufsize
>= sizeof(*e
))) {
296 struct inotify_event
*e2
= NULL
;
297 bufsize
-= e
->len
+ sizeof(*e
);
298 if (bufsize
>= sizeof(*e
)) {
299 e2
= (struct inotify_event
*)(e
->len
+ sizeof(*e
) + (char *)e
);
301 inotify_dispatch(in
, e
, prev_wd
, prev_cookie
, e2
);
303 prev_cookie
= e
->cookie
;
311 setup the inotify handle - called the first time a watch is added on
314 static int inotify_setup(struct sys_notify_context
*ctx
)
316 struct inotify_private
*in
;
317 struct tevent_fd
*fde
;
319 in
= talloc(ctx
, struct inotify_private
);
324 in
->fd
= inotify_init();
327 DEBUG(0, ("Failed to init inotify - %s\n", strerror(ret
)));
334 ctx
->private_data
= in
;
335 talloc_set_destructor(in
, inotify_destructor
);
337 /* add a event waiting for the inotify fd to be readable */
338 fde
= tevent_add_fd(ctx
->ev
, in
, in
->fd
, TEVENT_FD_READ
,
339 inotify_handler
, in
);
341 ctx
->private_data
= NULL
;
351 static int watch_destructor(struct inotify_watch_context
*w
)
353 struct inotify_private
*in
= w
->in
;
355 DLIST_REMOVE(w
->in
->watches
, w
);
357 for (w
=in
->watches
;w
;w
=w
->next
) {
360 * Another inotify_watch_context listens on this path,
361 * leave the kernel level watch in place
367 DEBUG(10, ("Deleting inotify watch %d\n", wd
));
368 if (inotify_rm_watch(in
->fd
, wd
) == -1) {
369 DEBUG(1, ("inotify_rm_watch returned %s\n", strerror(errno
)));
376 add a watch. The watch is removed when the caller calls
377 talloc_free() on *handle
379 int inotify_watch(TALLOC_CTX
*mem_ctx
,
380 struct sys_notify_context
*ctx
,
383 uint32_t *subdir_filter
,
384 void (*callback
)(struct sys_notify_context
*ctx
,
386 struct notify_event
*ev
,
391 struct inotify_private
*in
;
393 struct inotify_watch_context
*w
;
394 uint32_t orig_filter
= *filter
;
395 void **handle
= (void **)handle_p
;
397 /* maybe setup the inotify fd */
398 if (ctx
->private_data
== NULL
) {
400 ret
= inotify_setup(ctx
);
406 in
= talloc_get_type(ctx
->private_data
, struct inotify_private
);
408 mask
= inotify_map(filter
);
410 /* this filter can't be handled by inotify */
414 /* using IN_MASK_ADD allows us to cope with inotify() returning the same
415 watch descriptor for multiple watches on the same path */
416 mask
|= (IN_MASK_ADD
| IN_ONLYDIR
);
418 w
= talloc(mem_ctx
, struct inotify_watch_context
);
420 *filter
= orig_filter
;
425 w
->callback
= callback
;
426 w
->private_data
= private_data
;
428 w
->filter
= orig_filter
;
429 w
->path
= talloc_strdup(w
, path
);
430 if (w
->path
== NULL
) {
431 *filter
= orig_filter
;
436 /* get a new watch descriptor for this path */
437 w
->wd
= inotify_add_watch(in
->fd
, path
, mask
);
440 *filter
= orig_filter
;
442 DEBUG(1, ("inotify_add_watch returned %s\n", strerror(err
)));
446 DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
451 DLIST_ADD(in
->watches
, w
);
453 /* the caller frees the handle to stop watching */
454 talloc_set_destructor(w
, watch_destructor
);