source: branches/samba-3.2.x/source/smbd/notify_inotify.c@ 198

Last change on this file since 198 was 133, checked in by Paul Smedley, 18 years ago

Update trunk to 3.2.0pre3

File size: 10.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Copyright (C) Andrew Tridgell 2006
5
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.
10
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.
15
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/>.
18*/
19
20/*
21 notify implementation using inotify
22*/
23
24#include "includes.h"
25
26#ifdef HAVE_INOTIFY
27
28#ifdef HAVE_ASM_TYPES_H
29#include <asm/types.h>
30#endif
31
32#ifndef HAVE_INOTIFY_INIT
33
34#include <linux/inotify.h>
35#include <asm/unistd.h>
36
37
38/*
39 glibc doesn't define these functions yet (as of March 2006)
40*/
41static int inotify_init(void)
42{
43 return syscall(__NR_inotify_init);
44}
45
46static int inotify_add_watch(int fd, const char *path, __u32 mask)
47{
48 return syscall(__NR_inotify_add_watch, fd, path, mask);
49}
50
51static int inotify_rm_watch(int fd, int wd)
52{
53 return syscall(__NR_inotify_rm_watch, fd, wd);
54}
55#else
56
57#include <sys/inotify.h>
58
59#endif
60
61
62/* older glibc headers don't have these defines either */
63#ifndef IN_ONLYDIR
64#define IN_ONLYDIR 0x01000000
65#endif
66#ifndef IN_MASK_ADD
67#define IN_MASK_ADD 0x20000000
68#endif
69
70struct inotify_private {
71 struct sys_notify_context *ctx;
72 int fd;
73 struct inotify_watch_context *watches;
74};
75
76struct inotify_watch_context {
77 struct inotify_watch_context *next, *prev;
78 struct inotify_private *in;
79 int wd;
80 void (*callback)(struct sys_notify_context *ctx,
81 void *private_data,
82 struct notify_event *ev);
83 void *private_data;
84 uint32_t mask; /* the inotify mask */
85 uint32_t filter; /* the windows completion filter */
86 const char *path;
87};
88
89
90/*
91 destroy the inotify private context
92*/
93static int inotify_destructor(struct inotify_private *in)
94{
95 close(in->fd);
96 return 0;
97}
98
99
100/*
101 see if a particular event from inotify really does match a requested
102 notify event in SMB
103*/
104static bool filter_match(struct inotify_watch_context *w,
105 struct inotify_event *e)
106{
107 DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
108 e->mask, w->mask, w->filter));
109
110 if ((e->mask & w->mask) == 0) {
111 /* this happens because inotify_add_watch() coalesces watches on the same
112 path, oring their masks together */
113 return False;
114 }
115
116 /* SMB separates the filters for files and directories */
117 if (e->mask & IN_ISDIR) {
118 if ((w->filter & FILE_NOTIFY_CHANGE_DIR_NAME) == 0) {
119 return False;
120 }
121 } else {
122 if ((e->mask & IN_ATTRIB) &&
123 (w->filter & (FILE_NOTIFY_CHANGE_ATTRIBUTES|
124 FILE_NOTIFY_CHANGE_LAST_WRITE|
125 FILE_NOTIFY_CHANGE_LAST_ACCESS|
126 FILE_NOTIFY_CHANGE_EA|
127 FILE_NOTIFY_CHANGE_SECURITY))) {
128 return True;
129 }
130 if ((e->mask & IN_MODIFY) &&
131 (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
132 return True;
133 }
134 if ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) == 0) {
135 return False;
136 }
137 }
138
139 return True;
140}
141
142
143
144/*
145 dispatch one inotify event
146
147 the cookies are used to correctly handle renames
148*/
149static void inotify_dispatch(struct inotify_private *in,
150 struct inotify_event *e,
151 uint32_t prev_cookie,
152 struct inotify_event *e2)
153{
154 struct inotify_watch_context *w, *next;
155 struct notify_event ne;
156
157 DEBUG(10, ("inotify_dispatch called with mask=%x, name=[%s]\n",
158 e->mask, e->len ? e->name : ""));
159
160 /* ignore extraneous events, such as unmount and IN_IGNORED events */
161 if ((e->mask & (IN_ATTRIB|IN_MODIFY|IN_CREATE|IN_DELETE|
162 IN_MOVED_FROM|IN_MOVED_TO)) == 0) {
163 return;
164 }
165
166 /* map the inotify mask to a action. This gets complicated for
167 renames */
168 if (e->mask & IN_CREATE) {
169 ne.action = NOTIFY_ACTION_ADDED;
170 } else if (e->mask & IN_DELETE) {
171 ne.action = NOTIFY_ACTION_REMOVED;
172 } else if (e->mask & IN_MOVED_FROM) {
173 if (e2 != NULL && e2->cookie == e->cookie) {
174 ne.action = NOTIFY_ACTION_OLD_NAME;
175 } else {
176 ne.action = NOTIFY_ACTION_REMOVED;
177 }
178 } else if (e->mask & IN_MOVED_TO) {
179 if (e->cookie == prev_cookie) {
180 ne.action = NOTIFY_ACTION_NEW_NAME;
181 } else {
182 ne.action = NOTIFY_ACTION_ADDED;
183 }
184 } else {
185 ne.action = NOTIFY_ACTION_MODIFIED;
186 }
187 ne.path = e->name;
188
189 DEBUG(10, ("inotify_dispatch: ne.action = %d, ne.path = %s\n",
190 ne.action, ne.path));
191
192 /* find any watches that have this watch descriptor */
193 for (w=in->watches;w;w=next) {
194 next = w->next;
195 if (w->wd == e->wd && filter_match(w, e)) {
196 w->callback(in->ctx, w->private_data, &ne);
197 }
198 }
199
200 /* SMB expects a file rename to generate three events, two for
201 the rename and the other for a modify of the
202 destination. Strange! */
203 if (ne.action != NOTIFY_ACTION_NEW_NAME ||
204 (e->mask & IN_ISDIR) != 0) {
205 return;
206 }
207
208 ne.action = NOTIFY_ACTION_MODIFIED;
209 e->mask = IN_ATTRIB;
210
211 for (w=in->watches;w;w=next) {
212 next = w->next;
213 if (w->wd == e->wd && filter_match(w, e) &&
214 !(w->filter & FILE_NOTIFY_CHANGE_CREATION)) {
215 w->callback(in->ctx, w->private_data, &ne);
216 }
217 }
218}
219
220/*
221 called when the kernel has some events for us
222*/
223static void inotify_handler(struct event_context *ev, struct fd_event *fde,
224 uint16_t flags, void *private_data)
225{
226 struct inotify_private *in = talloc_get_type(private_data,
227 struct inotify_private);
228 int bufsize = 0;
229 struct inotify_event *e0, *e;
230 uint32_t prev_cookie=0;
231
232 /*
233 we must use FIONREAD as we cannot predict the length of the
234 filenames, and thus can't know how much to allocate
235 otherwise
236 */
237 if (ioctl(in->fd, FIONREAD, &bufsize) != 0 ||
238 bufsize == 0) {
239 DEBUG(0,("No data on inotify fd?!\n"));
240 return;
241 }
242
243 e0 = e = (struct inotify_event *)TALLOC_SIZE(in, bufsize);
244 if (e == NULL) return;
245
246 if (read(in->fd, e0, bufsize) != bufsize) {
247 DEBUG(0,("Failed to read all inotify data\n"));
248 talloc_free(e0);
249 return;
250 }
251
252 /* we can get more than one event in the buffer */
253 while (bufsize >= sizeof(*e)) {
254 struct inotify_event *e2 = NULL;
255 bufsize -= e->len + sizeof(*e);
256 if (bufsize >= sizeof(*e)) {
257 e2 = (struct inotify_event *)(e->len + sizeof(*e) + (char *)e);
258 }
259 inotify_dispatch(in, e, prev_cookie, e2);
260 prev_cookie = e->cookie;
261 e = e2;
262 }
263
264 talloc_free(e0);
265}
266
267/*
268 setup the inotify handle - called the first time a watch is added on
269 this context
270*/
271static NTSTATUS inotify_setup(struct sys_notify_context *ctx)
272{
273 struct inotify_private *in;
274
275 if (!lp_parm_bool(-1, "notify", "inotify", True)) {
276 return NT_STATUS_INVALID_SYSTEM_SERVICE;
277 }
278
279 in = talloc(ctx, struct inotify_private);
280 NT_STATUS_HAVE_NO_MEMORY(in);
281 in->fd = inotify_init();
282 if (in->fd == -1) {
283 DEBUG(0,("Failed to init inotify - %s\n", strerror(errno)));
284 talloc_free(in);
285 return map_nt_error_from_unix(errno);
286 }
287 in->ctx = ctx;
288 in->watches = NULL;
289
290 ctx->private_data = in;
291 talloc_set_destructor(in, inotify_destructor);
292
293 /* add a event waiting for the inotify fd to be readable */
294 event_add_fd(ctx->ev, in, in->fd, EVENT_FD_READ, inotify_handler, in);
295
296 return NT_STATUS_OK;
297}
298
299
300/*
301 map from a change notify mask to a inotify mask. Remove any bits
302 which we can handle
303*/
304static const struct {
305 uint32_t notify_mask;
306 uint32_t inotify_mask;
307} inotify_mapping[] = {
308 {FILE_NOTIFY_CHANGE_FILE_NAME, IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
309 {FILE_NOTIFY_CHANGE_DIR_NAME, IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
310 {FILE_NOTIFY_CHANGE_ATTRIBUTES, IN_ATTRIB|IN_MOVED_TO|IN_MOVED_FROM|IN_MODIFY},
311 {FILE_NOTIFY_CHANGE_LAST_WRITE, IN_ATTRIB},
312 {FILE_NOTIFY_CHANGE_LAST_ACCESS, IN_ATTRIB},
313 {FILE_NOTIFY_CHANGE_EA, IN_ATTRIB},
314 {FILE_NOTIFY_CHANGE_SECURITY, IN_ATTRIB}
315};
316
317static uint32_t inotify_map(struct notify_entry *e)
318{
319 int i;
320 uint32_t out=0;
321 for (i=0;i<ARRAY_SIZE(inotify_mapping);i++) {
322 if (inotify_mapping[i].notify_mask & e->filter) {
323 out |= inotify_mapping[i].inotify_mask;
324 e->filter &= ~inotify_mapping[i].notify_mask;
325 }
326 }
327 return out;
328}
329
330/*
331 destroy a watch
332*/
333static int watch_destructor(struct inotify_watch_context *w)
334{
335 struct inotify_private *in = w->in;
336 int wd = w->wd;
337 DLIST_REMOVE(w->in->watches, w);
338
339 /* only rm the watch if its the last one with this wd */
340 for (w=in->watches;w;w=w->next) {
341 if (w->wd == wd) break;
342 }
343 if (w == NULL) {
344 DEBUG(10, ("Deleting inotify watch %d\n", wd));
345 if (inotify_rm_watch(in->fd, wd) == -1) {
346 DEBUG(1, ("inotify_rm_watch returned %s\n",
347 strerror(errno)));
348 }
349
350 }
351 return 0;
352}
353
354
355/*
356 add a watch. The watch is removed when the caller calls
357 talloc_free() on *handle
358*/
359NTSTATUS inotify_watch(struct sys_notify_context *ctx,
360 struct notify_entry *e,
361 void (*callback)(struct sys_notify_context *ctx,
362 void *private_data,
363 struct notify_event *ev),
364 void *private_data,
365 void *handle_p)
366{
367 struct inotify_private *in;
368 int wd;
369 uint32_t mask;
370 struct inotify_watch_context *w;
371 uint32_t filter = e->filter;
372 void **handle = (void **)handle_p;
373
374 /* maybe setup the inotify fd */
375 if (ctx->private_data == NULL) {
376 NTSTATUS status;
377 status = inotify_setup(ctx);
378 NT_STATUS_NOT_OK_RETURN(status);
379 }
380
381 in = talloc_get_type(ctx->private_data, struct inotify_private);
382
383 mask = inotify_map(e);
384 if (mask == 0) {
385 /* this filter can't be handled by inotify */
386 return NT_STATUS_INVALID_PARAMETER;
387 }
388
389 /* using IN_MASK_ADD allows us to cope with inotify() returning the same
390 watch descriptor for muliple watches on the same path */
391 mask |= (IN_MASK_ADD | IN_ONLYDIR);
392
393 /* get a new watch descriptor for this path */
394 wd = inotify_add_watch(in->fd, e->path, mask);
395 if (wd == -1) {
396 e->filter = filter;
397 DEBUG(1, ("inotify_add_watch returned %s\n", strerror(errno)));
398 return map_nt_error_from_unix(errno);
399 }
400
401 DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
402 e->path, mask, wd));
403
404 w = talloc(in, struct inotify_watch_context);
405 if (w == NULL) {
406 inotify_rm_watch(in->fd, wd);
407 e->filter = filter;
408 return NT_STATUS_NO_MEMORY;
409 }
410
411 w->in = in;
412 w->wd = wd;
413 w->callback = callback;
414 w->private_data = private_data;
415 w->mask = mask;
416 w->filter = filter;
417 w->path = talloc_strdup(w, e->path);
418 if (w->path == NULL) {
419 inotify_rm_watch(in->fd, wd);
420 e->filter = filter;
421 return NT_STATUS_NO_MEMORY;
422 }
423
424 (*handle) = w;
425
426 DLIST_ADD(in->watches, w);
427
428 /* the caller frees the handle to stop watching */
429 talloc_set_destructor(w, watch_destructor);
430
431 return NT_STATUS_OK;
432}
433
434#endif
Note: See TracBrowser for help on using the repository browser.