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