Revert "rpc_parse: Use UUIDs from librpc/gen_ndr/ when possible to reduce"
[Samba.git] / source / smbd / notify_inotify.c
blobfa0f0ed51dc9202e4c355fa0fba0587fb864e7cc
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"
26 #ifdef HAVE_INOTIFY
28 #ifdef HAVE_ASM_TYPES_H
29 #include <asm/types.h>
30 #endif
32 #ifndef HAVE_INOTIFY_INIT
34 #include <linux/inotify.h>
35 #include <asm/unistd.h>
39 glibc doesn't define these functions yet (as of March 2006)
41 static int inotify_init(void)
43 return syscall(__NR_inotify_init);
46 static int inotify_add_watch(int fd, const char *path, __u32 mask)
48 return syscall(__NR_inotify_add_watch, fd, path, mask);
51 static int inotify_rm_watch(int fd, int wd)
53 return syscall(__NR_inotify_rm_watch, fd, wd);
55 #else
57 #include <sys/inotify.h>
59 #endif
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
70 struct inotify_private {
71 struct sys_notify_context *ctx;
72 int fd;
73 struct inotify_watch_context *watches;
76 struct 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;
91 destroy the inotify private context
93 static int inotify_destructor(struct inotify_private *in)
95 close(in->fd);
96 return 0;
101 see if a particular event from inotify really does match a requested
102 notify event in SMB
104 static bool filter_match(struct inotify_watch_context *w,
105 struct inotify_event *e)
107 DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
108 e->mask, w->mask, w->filter));
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;
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;
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;
130 if ((e->mask & IN_MODIFY) &&
131 (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
132 return True;
134 if ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) == 0) {
135 return False;
139 return True;
145 dispatch one inotify event
147 the cookies are used to correctly handle renames
149 static void inotify_dispatch(struct inotify_private *in,
150 struct inotify_event *e,
151 uint32_t prev_cookie,
152 struct inotify_event *e2)
154 struct inotify_watch_context *w, *next;
155 struct notify_event ne;
157 DEBUG(10, ("inotify_dispatch called with mask=%x, name=[%s]\n",
158 e->mask, e->len ? e->name : ""));
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;
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;
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;
184 } else {
185 ne.action = NOTIFY_ACTION_MODIFIED;
187 ne.path = e->name;
189 DEBUG(10, ("inotify_dispatch: ne.action = %d, ne.path = %s\n",
190 ne.action, ne.path));
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);
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;
208 ne.action = NOTIFY_ACTION_MODIFIED;
209 e->mask = IN_ATTRIB;
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);
221 called when the kernel has some events for us
223 static void inotify_handler(struct event_context *ev, struct fd_event *fde,
224 uint16_t flags, void *private_data)
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;
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
237 if (ioctl(in->fd, FIONREAD, &bufsize) != 0 ||
238 bufsize == 0) {
239 DEBUG(0,("No data on inotify fd?!\n"));
240 return;
243 e0 = e = (struct inotify_event *)TALLOC_SIZE(in, bufsize);
244 if (e == NULL) return;
246 if (read(in->fd, e0, bufsize) != bufsize) {
247 DEBUG(0,("Failed to read all inotify data\n"));
248 talloc_free(e0);
249 return;
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);
259 inotify_dispatch(in, e, prev_cookie, e2);
260 prev_cookie = e->cookie;
261 e = e2;
264 talloc_free(e0);
268 setup the inotify handle - called the first time a watch is added on
269 this context
271 static NTSTATUS inotify_setup(struct sys_notify_context *ctx)
273 struct inotify_private *in;
275 if (!lp_parm_bool(-1, "notify", "inotify", True)) {
276 return NT_STATUS_INVALID_SYSTEM_SERVICE;
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);
287 in->ctx = ctx;
288 in->watches = NULL;
290 ctx->private_data = in;
291 talloc_set_destructor(in, inotify_destructor);
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);
296 return NT_STATUS_OK;
301 map from a change notify mask to a inotify mask. Remove any bits
302 which we can handle
304 static 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}
317 static uint32_t inotify_map(struct notify_entry *e)
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;
327 return out;
331 destroy a watch
333 static int watch_destructor(struct inotify_watch_context *w)
335 struct inotify_private *in = w->in;
336 int wd = w->wd;
337 DLIST_REMOVE(w->in->watches, w);
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;
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)));
351 return 0;
356 add a watch. The watch is removed when the caller calls
357 talloc_free() on *handle
359 NTSTATUS 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)
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;
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);
381 in = talloc_get_type(ctx->private_data, struct inotify_private);
383 mask = inotify_map(e);
384 if (mask == 0) {
385 /* this filter can't be handled by inotify */
386 return NT_STATUS_INVALID_PARAMETER;
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);
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);
401 DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
402 e->path, mask, wd));
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;
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;
424 (*handle) = w;
426 DLIST_ADD(in->watches, w);
428 /* the caller frees the handle to stop watching */
429 talloc_set_destructor(w, watch_destructor);
431 return NT_STATUS_OK;
434 #endif