78fb654cf8d3fadb2bf2b16ff5379645da232510
[kai/samba-autobuild/.git] / source3 / smbd / notify_inotify.c
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 #include "../librpc/gen_ndr/notify.h"
26 #include "smbd/smbd.h"
27 #include "lib/util/sys_rw_data.h"
28
29 #include <sys/inotify.h>
30
31 /* glibc < 2.5 headers don't have these defines */
32 #ifndef IN_ONLYDIR
33 #define IN_ONLYDIR 0x01000000
34 #endif
35 #ifndef IN_MASK_ADD
36 #define IN_MASK_ADD 0x20000000
37 #endif
38
39 struct inotify_private {
40         struct sys_notify_context *ctx;
41         int fd;
42         struct inotify_watch_context *watches;
43 };
44
45 struct inotify_watch_context {
46         struct inotify_watch_context *next, *prev;
47         struct inotify_private *in;
48         int wd;
49         void (*callback)(struct sys_notify_context *ctx, 
50                          void *private_data,
51                          struct notify_event *ev);
52         void *private_data;
53         uint32_t mask; /* the inotify mask */
54         uint32_t filter; /* the windows completion filter */
55         const char *path;
56 };
57
58
59 /*
60   destroy the inotify private context
61 */
62 static int inotify_destructor(struct inotify_private *in)
63 {
64         close(in->fd);
65         return 0;
66 }
67
68
69 /*
70   see if a particular event from inotify really does match a requested
71   notify event in SMB
72 */
73 static bool filter_match(struct inotify_watch_context *w,
74                          struct inotify_event *e)
75 {
76         bool ok;
77
78         DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
79                    e->mask, w->mask, w->filter));
80
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 */
84                 return False;
85         }
86
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);
90                 return ok;
91         }
92
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))) {
99                 return True;
100         }
101         if ((e->mask & IN_MODIFY) &&
102             (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
103                 return True;
104         }
105
106         ok = ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) != 0);
107         return ok;
108 }
109
110
111
112 /*
113   dispatch one inotify event
114
115   the cookies are used to correctly handle renames
116 */
117 static void inotify_dispatch(struct inotify_private *in, 
118                              struct inotify_event *e, 
119                              int prev_wd,
120                              uint32_t prev_cookie,
121                              struct inotify_event *e2)
122 {
123         struct inotify_watch_context *w, *next;
124         struct notify_event ne;
125
126         DEBUG(10, ("inotify_dispatch called with mask=%x, name=[%s]\n",
127                    e->mask, e->len ? e->name : ""));
128
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) {
132                 return;
133         }
134
135         /* map the inotify mask to a action. This gets complicated for
136            renames */
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 &&
143                     e2->wd == e->wd) {
144                         ne.action = NOTIFY_ACTION_OLD_NAME;
145                 } else {
146                         ne.action = NOTIFY_ACTION_REMOVED;
147                 }
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;
151                 } else {
152                         ne.action = NOTIFY_ACTION_ADDED;
153                 }
154         } else {
155                 ne.action = NOTIFY_ACTION_MODIFIED;
156         }
157         ne.path = e->name;
158
159         DEBUG(10, ("inotify_dispatch: ne.action = %d, ne.path = %s\n",
160                    ne.action, ne.path));
161
162         /* find any watches that have this watch descriptor */
163         for (w=in->watches;w;w=next) {
164                 next = w->next;
165                 if (w->wd == e->wd && filter_match(w, e)) {
166                         ne.dir = w->path;
167                         w->callback(in->ctx, w->private_data, &ne);
168                 }
169         }
170
171         if ((ne.action == NOTIFY_ACTION_NEW_NAME) &&
172             ((e->mask & IN_ISDIR) == 0)) {
173
174                 /*
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!
178                  */
179
180                 ne.action = NOTIFY_ACTION_MODIFIED;
181                 e->mask = IN_ATTRIB;
182
183                 for (w=in->watches;w;w=next) {
184                         next = w->next;
185                         if (w->wd == e->wd && filter_match(w, e) &&
186                             !(w->filter & FILE_NOTIFY_CHANGE_CREATION)) {
187                                 ne.dir = w->path;
188                                 w->callback(in->ctx, w->private_data, &ne);
189                         }
190                 }
191         }
192 }
193
194 /*
195   called when the kernel has some events for us
196 */
197 static void inotify_handler(struct tevent_context *ev, struct tevent_fd *fde,
198                             uint16_t flags, void *private_data)
199 {
200         struct inotify_private *in = talloc_get_type(private_data,
201                                                      struct inotify_private);
202         int bufsize = 0;
203         struct inotify_event *e0, *e;
204         uint32_t prev_cookie=0;
205         int prev_wd = -1;
206         ssize_t ret;
207
208         /*
209           we must use FIONREAD as we cannot predict the length of the
210           filenames, and thus can't know how much to allocate
211           otherwise
212         */
213         if (ioctl(in->fd, FIONREAD, &bufsize) != 0 || 
214             bufsize == 0) {
215                 DEBUG(0,("No data on inotify fd?!\n"));
216                 TALLOC_FREE(fde);
217                 return;
218         }
219
220         e0 = e = (struct inotify_event *)TALLOC_SIZE(in, bufsize + 1);
221         if (e == NULL) return;
222         ((uint8_t *)e)[bufsize] = '\0';
223
224         ret = read_data(in->fd, e0, bufsize);
225         if (ret != bufsize) {
226                 DEBUG(0, ("Failed to read all inotify data - %s\n",
227                           strerror(errno)));
228                 talloc_free(e0);
229                 /* the inotify fd will now be out of sync,
230                  * can't keep reading data off it */
231                 TALLOC_FREE(fde);
232                 return;
233         }
234
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);
241                 }
242                 inotify_dispatch(in, e, prev_wd, prev_cookie, e2);
243                 prev_wd = e->wd;
244                 prev_cookie = e->cookie;
245                 e = e2;
246         }
247
248         talloc_free(e0);
249 }
250
251 /*
252   setup the inotify handle - called the first time a watch is added on
253   this context
254 */
255 static int inotify_setup(struct sys_notify_context *ctx)
256 {
257         struct inotify_private *in;
258         struct tevent_fd *fde;
259
260         in = talloc(ctx, struct inotify_private);
261         if (in == NULL) {
262                 return ENOMEM;
263         }
264
265         in->fd = inotify_init();
266         if (in->fd == -1) {
267                 int ret = errno;
268                 DEBUG(0, ("Failed to init inotify - %s\n", strerror(ret)));
269                 talloc_free(in);
270                 return ret;
271         }
272         in->ctx = ctx;
273         in->watches = NULL;
274
275         ctx->private_data = in;
276         talloc_set_destructor(in, inotify_destructor);
277
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);
281         if (fde == NULL) {
282                 ctx->private_data = NULL;
283                 TALLOC_FREE(in);
284                 return ENOMEM;
285         }
286         return 0;
287 }
288
289
290 /*
291   map from a change notify mask to a inotify mask. Remove any bits
292   which we can handle
293 */
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}
305 };
306
307 static uint32_t inotify_map(uint32_t *filter)
308 {
309         int i;
310         uint32_t out=0;
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;
315                 }
316         }
317         return out;
318 }
319
320 /*
321   destroy a watch
322 */
323 static int watch_destructor(struct inotify_watch_context *w)
324 {
325         struct inotify_private *in = w->in;
326         int wd = w->wd;
327         DLIST_REMOVE(w->in->watches, w);
328
329         for (w=in->watches;w;w=w->next) {
330                 if (w->wd == wd) {
331                         /*
332                          * Another inotify_watch_context listens on this path,
333                          * leave the kernel level watch in place
334                          */
335                         return 0;
336                 }
337         }
338
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)));
342         }
343         return 0;
344 }
345
346
347 /*
348   add a watch. The watch is removed when the caller calls
349   talloc_free() on *handle
350 */
351 int inotify_watch(TALLOC_CTX *mem_ctx,
352                   struct sys_notify_context *ctx,
353                   const char *path,
354                   uint32_t *filter,
355                   uint32_t *subdir_filter,
356                   void (*callback)(struct sys_notify_context *ctx,
357                                    void *private_data,
358                                    struct notify_event *ev),
359                   void *private_data,
360                   void *handle_p)
361 {
362         struct inotify_private *in;
363         uint32_t mask;
364         struct inotify_watch_context *w;
365         uint32_t orig_filter = *filter;
366         void **handle = (void **)handle_p;
367
368         /* maybe setup the inotify fd */
369         if (ctx->private_data == NULL) {
370                 int ret;
371                 ret = inotify_setup(ctx);
372                 if (ret != 0) {
373                         return ret;
374                 }
375         }
376
377         in = talloc_get_type(ctx->private_data, struct inotify_private);
378
379         mask = inotify_map(filter);
380         if (mask == 0) {
381                 /* this filter can't be handled by inotify */
382                 return EINVAL;
383         }
384
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);
388
389         w = talloc(mem_ctx, struct inotify_watch_context);
390         if (w == NULL) {
391                 *filter = orig_filter;
392                 return ENOMEM;
393         }
394
395         w->in = in;
396         w->callback = callback;
397         w->private_data = private_data;
398         w->mask = mask;
399         w->filter = orig_filter;
400         w->path = talloc_strdup(w, path);
401         if (w->path == NULL) {
402                 *filter = orig_filter;
403                 TALLOC_FREE(w);
404                 return ENOMEM;
405         }
406
407         /* get a new watch descriptor for this path */
408         w->wd = inotify_add_watch(in->fd, path, mask);
409         if (w->wd == -1) {
410                 int err = errno;
411                 *filter = orig_filter;
412                 TALLOC_FREE(w);
413                 DEBUG(1, ("inotify_add_watch returned %s\n", strerror(err)));
414                 return err;
415         }
416
417         DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
418                    path, mask, w->wd));
419
420         (*handle) = w;
421
422         DLIST_ADD(in->watches, w);
423
424         /* the caller frees the handle to stop watching */
425         talloc_set_destructor(w, watch_destructor);
426
427         return 0;
428 }