s3:libsmb: make use of get_cmdline_auth_info_* helper functions in get_ipc_connect()
[Samba.git] / source3 / smbd / notify_inotify.c
blob3848dd670563fee55c7465fa3caa90163bb370e4
1 /*
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
24 #include "includes.h"
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 */
32 #ifndef IN_ONLYDIR
33 #define IN_ONLYDIR 0x01000000
34 #endif
35 #ifndef IN_MASK_ADD
36 #define IN_MASK_ADD 0x20000000
37 #endif
39 struct inotify_private {
40 struct sys_notify_context *ctx;
41 int fd;
42 struct inotify_watch_context *watches;
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 uint32_t filter);
53 void *private_data;
54 uint32_t mask; /* the inotify mask */
55 uint32_t filter; /* the windows completion filter */
56 const char *path;
61 map from a change notify mask to a inotify mask. Remove any bits
62 which we can handle
64 static const struct {
65 uint32_t notify_mask;
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)
79 int i;
80 uint32_t out=0;
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;
87 return out;
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)
96 int i;
97 uint32_t filter = 0;
99 for (i = 0; i < ARRAY_SIZE(inotify_mapping); i++) {
100 if (inotify_mapping[0].inotify_mask & mask) {
101 filter |= inotify_mapping[i].notify_mask;
105 if (mask & IN_ISDIR) {
106 filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
107 } else {
108 filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
111 return filter;
115 destroy the inotify private context
117 static int inotify_destructor(struct inotify_private *in)
119 close(in->fd);
120 return 0;
125 see if a particular event from inotify really does match a requested
126 notify event in SMB
128 static bool filter_match(struct inotify_watch_context *w,
129 struct inotify_event *e)
131 bool ok;
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 */
139 return False;
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);
145 return ok;
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))) {
154 return True;
156 if ((e->mask & IN_MODIFY) &&
157 (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
158 return True;
161 ok = ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) != 0);
162 return ok;
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,
174 int prev_wd,
175 uint32_t prev_cookie,
176 struct inotify_event *e2)
178 struct inotify_watch_context *w, *next;
179 struct notify_event ne;
180 uint32_t filter;
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) {
188 return;
191 /* map the inotify mask to a action. This gets complicated for
192 renames */
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 &&
199 e2->wd == e->wd) {
200 ne.action = NOTIFY_ACTION_OLD_NAME;
201 } else {
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;
207 } else {
208 ne.action = NOTIFY_ACTION_ADDED;
210 } else {
211 ne.action = NOTIFY_ACTION_MODIFIED;
213 ne.path = e->name;
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) {
222 next = w->next;
223 if (w->wd == e->wd && filter_match(w, e)) {
224 ne.dir = w->path;
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;
239 e->mask = IN_ATTRIB;
241 for (w=in->watches;w;w=next) {
242 next = w->next;
243 if (w->wd == e->wd && filter_match(w, e) &&
244 !(w->filter & FILE_NOTIFY_CHANGE_CREATION)) {
245 ne.dir = w->path;
246 w->callback(in->ctx, w->private_data, &ne,
247 filter);
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);
261 int bufsize = 0;
262 struct inotify_event *e0, *e;
263 uint32_t prev_cookie=0;
264 int prev_wd = -1;
265 ssize_t ret;
268 we must use FIONREAD as we cannot predict the length of the
269 filenames, and thus can't know how much to allocate
270 otherwise
272 if (ioctl(in->fd, FIONREAD, &bufsize) != 0 ||
273 bufsize == 0) {
274 DEBUG(0,("No data on inotify fd?!\n"));
275 TALLOC_FREE(fde);
276 return;
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",
286 strerror(errno)));
287 talloc_free(e0);
288 /* the inotify fd will now be out of sync,
289 * can't keep reading data off it */
290 TALLOC_FREE(fde);
291 return;
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);
302 prev_wd = e->wd;
303 prev_cookie = e->cookie;
304 e = e2;
307 talloc_free(e0);
311 setup the inotify handle - called the first time a watch is added on
312 this context
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);
320 if (in == NULL) {
321 return ENOMEM;
324 in->fd = inotify_init();
325 if (in->fd == -1) {
326 int ret = errno;
327 DEBUG(0, ("Failed to init inotify - %s\n", strerror(ret)));
328 talloc_free(in);
329 return ret;
331 in->ctx = ctx;
332 in->watches = NULL;
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);
340 if (fde == NULL) {
341 ctx->private_data = NULL;
342 TALLOC_FREE(in);
343 return ENOMEM;
345 return 0;
349 destroy a watch
351 static int watch_destructor(struct inotify_watch_context *w)
353 struct inotify_private *in = w->in;
354 int wd = w->wd;
355 DLIST_REMOVE(w->in->watches, w);
357 for (w=in->watches;w;w=w->next) {
358 if (w->wd == wd) {
360 * Another inotify_watch_context listens on this path,
361 * leave the kernel level watch in place
363 return 0;
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)));
371 return 0;
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,
381 const char *path,
382 uint32_t *filter,
383 uint32_t *subdir_filter,
384 void (*callback)(struct sys_notify_context *ctx,
385 void *private_data,
386 struct notify_event *ev,
387 uint32_t filter),
388 void *private_data,
389 void *handle_p)
391 struct inotify_private *in;
392 uint32_t mask;
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) {
399 int ret;
400 ret = inotify_setup(ctx);
401 if (ret != 0) {
402 return ret;
406 in = talloc_get_type(ctx->private_data, struct inotify_private);
408 mask = inotify_map(filter);
409 if (mask == 0) {
410 /* this filter can't be handled by inotify */
411 return EINVAL;
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);
419 if (w == NULL) {
420 *filter = orig_filter;
421 return ENOMEM;
424 w->in = in;
425 w->callback = callback;
426 w->private_data = private_data;
427 w->mask = mask;
428 w->filter = orig_filter;
429 w->path = talloc_strdup(w, path);
430 if (w->path == NULL) {
431 *filter = orig_filter;
432 TALLOC_FREE(w);
433 return ENOMEM;
436 /* get a new watch descriptor for this path */
437 w->wd = inotify_add_watch(in->fd, path, mask);
438 if (w->wd == -1) {
439 int err = errno;
440 *filter = orig_filter;
441 TALLOC_FREE(w);
442 DEBUG(1, ("inotify_add_watch returned %s\n", strerror(err)));
443 return err;
446 DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
447 path, mask, w->wd));
449 (*handle) = w;
451 DLIST_ADD(in->watches, w);
453 /* the caller frees the handle to stop watching */
454 talloc_set_destructor(w, watch_destructor);
456 return 0;