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/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
);
53 uint32_t mask
; /* the inotify mask */
54 uint32_t filter
; /* the windows completion filter */
60 destroy the inotify private context
62 static int inotify_destructor(struct inotify_private
*in
)
70 see if a particular event from inotify really does match a requested
73 static bool filter_match(struct inotify_watch_context
*w
,
74 struct inotify_event
*e
)
78 DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
79 e
->mask
, w
->mask
, w
->filter
));
81 if ((e
->mask
& w
->mask
) == 0) {
82 /* this happens because inotify_add_watch() coalesces watches on the same
83 path, oring their masks together */
87 /* SMB separates the filters for files and directories */
88 if (e
->mask
& IN_ISDIR
) {
89 ok
= ((w
->filter
& FILE_NOTIFY_CHANGE_DIR_NAME
) != 0);
93 if ((e
->mask
& IN_ATTRIB
) &&
94 (w
->filter
& (FILE_NOTIFY_CHANGE_ATTRIBUTES
|
95 FILE_NOTIFY_CHANGE_LAST_WRITE
|
96 FILE_NOTIFY_CHANGE_LAST_ACCESS
|
97 FILE_NOTIFY_CHANGE_EA
|
98 FILE_NOTIFY_CHANGE_SECURITY
))) {
101 if ((e
->mask
& IN_MODIFY
) &&
102 (w
->filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)) {
106 ok
= ((w
->filter
& FILE_NOTIFY_CHANGE_FILE_NAME
) != 0);
113 dispatch one inotify event
115 the cookies are used to correctly handle renames
117 static void inotify_dispatch(struct inotify_private
*in
,
118 struct inotify_event
*e
,
120 uint32_t prev_cookie
,
121 struct inotify_event
*e2
)
123 struct inotify_watch_context
*w
, *next
;
124 struct notify_event ne
;
126 DEBUG(10, ("inotify_dispatch called with mask=%x, name=[%s]\n",
127 e
->mask
, e
->len
? e
->name
: ""));
129 /* ignore extraneous events, such as unmount and IN_IGNORED events */
130 if ((e
->mask
& (IN_ATTRIB
|IN_MODIFY
|IN_CREATE
|IN_DELETE
|
131 IN_MOVED_FROM
|IN_MOVED_TO
)) == 0) {
135 /* map the inotify mask to a action. This gets complicated for
137 if (e
->mask
& IN_CREATE
) {
138 ne
.action
= NOTIFY_ACTION_ADDED
;
139 } else if (e
->mask
& IN_DELETE
) {
140 ne
.action
= NOTIFY_ACTION_REMOVED
;
141 } else if (e
->mask
& IN_MOVED_FROM
) {
142 if (e2
!= NULL
&& e2
->cookie
== e
->cookie
&&
144 ne
.action
= NOTIFY_ACTION_OLD_NAME
;
146 ne
.action
= NOTIFY_ACTION_REMOVED
;
148 } else if (e
->mask
& IN_MOVED_TO
) {
149 if ((e
->cookie
== prev_cookie
) && (e
->wd
== prev_wd
)) {
150 ne
.action
= NOTIFY_ACTION_NEW_NAME
;
152 ne
.action
= NOTIFY_ACTION_ADDED
;
155 ne
.action
= NOTIFY_ACTION_MODIFIED
;
159 DEBUG(10, ("inotify_dispatch: ne.action = %d, ne.path = %s\n",
160 ne
.action
, ne
.path
));
162 /* find any watches that have this watch descriptor */
163 for (w
=in
->watches
;w
;w
=next
) {
165 if (w
->wd
== e
->wd
&& filter_match(w
, e
)) {
167 w
->callback(in
->ctx
, w
->private_data
, &ne
);
171 if ((ne
.action
== NOTIFY_ACTION_NEW_NAME
) &&
172 ((e
->mask
& IN_ISDIR
) == 0)) {
175 * SMB expects a file rename to generate three events, two for
176 * the rename and the other for a modify of the
177 * destination. Strange!
180 ne
.action
= NOTIFY_ACTION_MODIFIED
;
183 for (w
=in
->watches
;w
;w
=next
) {
185 if (w
->wd
== e
->wd
&& filter_match(w
, e
) &&
186 !(w
->filter
& FILE_NOTIFY_CHANGE_CREATION
)) {
188 w
->callback(in
->ctx
, w
->private_data
, &ne
);
195 called when the kernel has some events for us
197 static void inotify_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
198 uint16_t flags
, void *private_data
)
200 struct inotify_private
*in
= talloc_get_type(private_data
,
201 struct inotify_private
);
203 struct inotify_event
*e0
, *e
;
204 uint32_t prev_cookie
=0;
209 we must use FIONREAD as we cannot predict the length of the
210 filenames, and thus can't know how much to allocate
213 if (ioctl(in
->fd
, FIONREAD
, &bufsize
) != 0 ||
215 DEBUG(0,("No data on inotify fd?!\n"));
220 e0
= e
= (struct inotify_event
*)TALLOC_SIZE(in
, bufsize
+ 1);
221 if (e
== NULL
) return;
222 ((uint8_t *)e
)[bufsize
] = '\0';
224 ret
= read_data(in
->fd
, e0
, bufsize
);
225 if (ret
!= bufsize
) {
226 DEBUG(0, ("Failed to read all inotify data - %s\n",
229 /* the inotify fd will now be out of sync,
230 * can't keep reading data off it */
235 /* we can get more than one event in the buffer */
236 while (e
&& (bufsize
>= sizeof(*e
))) {
237 struct inotify_event
*e2
= NULL
;
238 bufsize
-= e
->len
+ sizeof(*e
);
239 if (bufsize
>= sizeof(*e
)) {
240 e2
= (struct inotify_event
*)(e
->len
+ sizeof(*e
) + (char *)e
);
242 inotify_dispatch(in
, e
, prev_wd
, prev_cookie
, e2
);
244 prev_cookie
= e
->cookie
;
252 setup the inotify handle - called the first time a watch is added on
255 static int inotify_setup(struct sys_notify_context
*ctx
)
257 struct inotify_private
*in
;
258 struct tevent_fd
*fde
;
260 in
= talloc(ctx
, struct inotify_private
);
265 in
->fd
= inotify_init();
268 DEBUG(0, ("Failed to init inotify - %s\n", strerror(ret
)));
275 ctx
->private_data
= in
;
276 talloc_set_destructor(in
, inotify_destructor
);
278 /* add a event waiting for the inotify fd to be readable */
279 fde
= tevent_add_fd(ctx
->ev
, in
, in
->fd
, TEVENT_FD_READ
,
280 inotify_handler
, in
);
282 ctx
->private_data
= NULL
;
291 map from a change notify mask to a inotify mask. Remove any bits
294 static const struct {
295 uint32_t notify_mask
;
296 uint32_t inotify_mask
;
297 } inotify_mapping
[] = {
298 {FILE_NOTIFY_CHANGE_FILE_NAME
, IN_CREATE
|IN_DELETE
|IN_MOVED_FROM
|IN_MOVED_TO
},
299 {FILE_NOTIFY_CHANGE_DIR_NAME
, IN_CREATE
|IN_DELETE
|IN_MOVED_FROM
|IN_MOVED_TO
},
300 {FILE_NOTIFY_CHANGE_ATTRIBUTES
, IN_ATTRIB
|IN_MOVED_TO
|IN_MOVED_FROM
|IN_MODIFY
},
301 {FILE_NOTIFY_CHANGE_LAST_WRITE
, IN_ATTRIB
},
302 {FILE_NOTIFY_CHANGE_LAST_ACCESS
, IN_ATTRIB
},
303 {FILE_NOTIFY_CHANGE_EA
, IN_ATTRIB
},
304 {FILE_NOTIFY_CHANGE_SECURITY
, IN_ATTRIB
}
307 static uint32_t inotify_map(uint32_t *filter
)
311 for (i
=0;i
<ARRAY_SIZE(inotify_mapping
);i
++) {
312 if (inotify_mapping
[i
].notify_mask
& *filter
) {
313 out
|= inotify_mapping
[i
].inotify_mask
;
314 *filter
&= ~inotify_mapping
[i
].notify_mask
;
323 static int watch_destructor(struct inotify_watch_context
*w
)
325 struct inotify_private
*in
= w
->in
;
327 DLIST_REMOVE(w
->in
->watches
, w
);
329 for (w
=in
->watches
;w
;w
=w
->next
) {
332 * Another inotify_watch_context listens on this path,
333 * leave the kernel level watch in place
339 DEBUG(10, ("Deleting inotify watch %d\n", wd
));
340 if (inotify_rm_watch(in
->fd
, wd
) == -1) {
341 DEBUG(1, ("inotify_rm_watch returned %s\n", strerror(errno
)));
348 add a watch. The watch is removed when the caller calls
349 talloc_free() on *handle
351 int inotify_watch(TALLOC_CTX
*mem_ctx
,
352 struct sys_notify_context
*ctx
,
355 uint32_t *subdir_filter
,
356 void (*callback
)(struct sys_notify_context
*ctx
,
358 struct notify_event
*ev
),
362 struct inotify_private
*in
;
364 struct inotify_watch_context
*w
;
365 uint32_t orig_filter
= *filter
;
366 void **handle
= (void **)handle_p
;
368 /* maybe setup the inotify fd */
369 if (ctx
->private_data
== NULL
) {
371 ret
= inotify_setup(ctx
);
377 in
= talloc_get_type(ctx
->private_data
, struct inotify_private
);
379 mask
= inotify_map(filter
);
381 /* this filter can't be handled by inotify */
385 /* using IN_MASK_ADD allows us to cope with inotify() returning the same
386 watch descriptor for multiple watches on the same path */
387 mask
|= (IN_MASK_ADD
| IN_ONLYDIR
);
389 w
= talloc(mem_ctx
, struct inotify_watch_context
);
391 *filter
= orig_filter
;
396 w
->callback
= callback
;
397 w
->private_data
= private_data
;
399 w
->filter
= orig_filter
;
400 w
->path
= talloc_strdup(w
, path
);
401 if (w
->path
== NULL
) {
402 *filter
= orig_filter
;
407 /* get a new watch descriptor for this path */
408 w
->wd
= inotify_add_watch(in
->fd
, path
, mask
);
411 *filter
= orig_filter
;
413 DEBUG(1, ("inotify_add_watch returned %s\n", strerror(err
)));
417 DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
422 DLIST_ADD(in
->watches
, w
);
424 /* the caller frees the handle to stop watching */
425 talloc_set_destructor(w
, watch_destructor
);