#include "includes.h"
#include "../librpc/gen_ndr/notify.h"
#include "smbd/smbd.h"
+#include "lib/sys_rw_data.h"
#ifdef HAVE_INOTIFY
static bool filter_match(struct inotify_watch_context *w,
struct inotify_event *e)
{
+ bool ok;
+
DEBUG(10, ("filter_match: e->mask=%x, w->mask=%x, w->filter=%x\n",
e->mask, w->mask, w->filter));
/* SMB separates the filters for files and directories */
if (e->mask & IN_ISDIR) {
- if ((w->filter & FILE_NOTIFY_CHANGE_DIR_NAME) == 0) {
- return False;
- }
- } else {
- if ((e->mask & IN_ATTRIB) &&
- (w->filter & (FILE_NOTIFY_CHANGE_ATTRIBUTES|
- FILE_NOTIFY_CHANGE_LAST_WRITE|
- FILE_NOTIFY_CHANGE_LAST_ACCESS|
- FILE_NOTIFY_CHANGE_EA|
- FILE_NOTIFY_CHANGE_SECURITY))) {
- return True;
- }
- if ((e->mask & IN_MODIFY) &&
- (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
- return True;
- }
- if ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) == 0) {
- return False;
- }
+ ok = ((w->filter & FILE_NOTIFY_CHANGE_DIR_NAME) != 0);
+ return ok;
+ }
+
+ if ((e->mask & IN_ATTRIB) &&
+ (w->filter & (FILE_NOTIFY_CHANGE_ATTRIBUTES|
+ FILE_NOTIFY_CHANGE_LAST_WRITE|
+ FILE_NOTIFY_CHANGE_LAST_ACCESS|
+ FILE_NOTIFY_CHANGE_EA|
+ FILE_NOTIFY_CHANGE_SECURITY))) {
+ return True;
+ }
+ if ((e->mask & IN_MODIFY) &&
+ (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
+ return True;
}
- return True;
+ ok = ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) != 0);
+ return ok;
}
for (w=in->watches;w;w=next) {
next = w->next;
if (w->wd == e->wd && filter_match(w, e)) {
+ ne.dir = w->path;
w->callback(in->ctx, w->private_data, &ne);
}
}
next = w->next;
if (w->wd == e->wd && filter_match(w, e) &&
!(w->filter & FILE_NOTIFY_CHANGE_CREATION)) {
+ ne.dir = w->path;
w->callback(in->ctx, w->private_data, &ne);
}
}
struct inotify_event *e0, *e;
uint32_t prev_cookie=0;
int prev_wd = -1;
- NTSTATUS status;
+ ssize_t ret;
/*
we must use FIONREAD as we cannot predict the length of the
if (e == NULL) return;
((uint8_t *)e)[bufsize] = '\0';
- status = read_data(in->fd, (char *)e0, bufsize);
- if (!NT_STATUS_IS_OK(status)) {
- DEBUG(0,("Failed to read all inotify data - %s\n",
- nt_errstr(status)));
+ ret = read_data(in->fd, e0, bufsize);
+ if (ret != bufsize) {
+ DEBUG(0, ("Failed to read all inotify data - %s\n",
+ strerror(errno)));
talloc_free(e0);
/* the inotify fd will now be out of sync,
* can't keep reading data off it */
setup the inotify handle - called the first time a watch is added on
this context
*/
-static NTSTATUS inotify_setup(struct sys_notify_context *ctx)
+static int inotify_setup(struct sys_notify_context *ctx)
{
struct inotify_private *in;
+ struct tevent_fd *fde;
- if (!lp_parm_bool(-1, "notify", "inotify", True)) {
- return NT_STATUS_INVALID_SYSTEM_SERVICE;
+ in = talloc(ctx, struct inotify_private);
+ if (in == NULL) {
+ return ENOMEM;
}
- in = talloc(ctx, struct inotify_private);
- NT_STATUS_HAVE_NO_MEMORY(in);
in->fd = inotify_init();
if (in->fd == -1) {
- DEBUG(0,("Failed to init inotify - %s\n", strerror(errno)));
+ int ret = errno;
+ DEBUG(0, ("Failed to init inotify - %s\n", strerror(ret)));
talloc_free(in);
- return map_nt_error_from_unix(errno);
+ return ret;
}
in->ctx = ctx;
in->watches = NULL;
talloc_set_destructor(in, inotify_destructor);
/* add a event waiting for the inotify fd to be readable */
- tevent_add_fd(ctx->ev, in, in->fd, TEVENT_FD_READ, inotify_handler, in);
-
- return NT_STATUS_OK;
+ fde = tevent_add_fd(ctx->ev, in, in->fd, TEVENT_FD_READ,
+ inotify_handler, in);
+ if (fde == NULL) {
+ ctx->private_data = NULL;
+ TALLOC_FREE(in);
+ return ENOMEM;
+ }
+ return 0;
}
int wd = w->wd;
DLIST_REMOVE(w->in->watches, w);
- /* only rm the watch if its the last one with this wd */
for (w=in->watches;w;w=w->next) {
- if (w->wd == wd) break;
- }
- if (w == NULL) {
- DEBUG(10, ("Deleting inotify watch %d\n", wd));
- if (inotify_rm_watch(in->fd, wd) == -1) {
- DEBUG(1, ("inotify_rm_watch returned %s\n",
- strerror(errno)));
+ if (w->wd == wd) {
+ /*
+ * Another inotify_watch_context listens on this path,
+ * leave the kernel level watch in place
+ */
+ return 0;
}
+ }
+ DEBUG(10, ("Deleting inotify watch %d\n", wd));
+ if (inotify_rm_watch(in->fd, wd) == -1) {
+ DEBUG(1, ("inotify_rm_watch returned %s\n", strerror(errno)));
}
return 0;
}
add a watch. The watch is removed when the caller calls
talloc_free() on *handle
*/
-NTSTATUS inotify_watch(struct sys_notify_context *ctx,
- const char *path,
- uint32_t *filter,
- uint32_t *subdir_filter,
- void (*callback)(struct sys_notify_context *ctx,
- void *private_data,
- struct notify_event *ev),
- void *private_data,
- void *handle_p)
+int inotify_watch(struct sys_notify_context *ctx,
+ const char *path,
+ uint32_t *filter,
+ uint32_t *subdir_filter,
+ void (*callback)(struct sys_notify_context *ctx,
+ void *private_data,
+ struct notify_event *ev),
+ void *private_data,
+ void *handle_p)
{
struct inotify_private *in;
- int wd;
uint32_t mask;
struct inotify_watch_context *w;
uint32_t orig_filter = *filter;
/* maybe setup the inotify fd */
if (ctx->private_data == NULL) {
- NTSTATUS status;
- status = inotify_setup(ctx);
- NT_STATUS_NOT_OK_RETURN(status);
+ int ret;
+ ret = inotify_setup(ctx);
+ if (ret != 0) {
+ return ret;
+ }
}
in = talloc_get_type(ctx->private_data, struct inotify_private);
mask = inotify_map(filter);
if (mask == 0) {
/* this filter can't be handled by inotify */
- return NT_STATUS_INVALID_PARAMETER;
+ return EINVAL;
}
/* using IN_MASK_ADD allows us to cope with inotify() returning the same
watch descriptor for multiple watches on the same path */
mask |= (IN_MASK_ADD | IN_ONLYDIR);
- /* get a new watch descriptor for this path */
- wd = inotify_add_watch(in->fd, path, mask);
- if (wd == -1) {
- *filter = orig_filter;
- DEBUG(1, ("inotify_add_watch returned %s\n", strerror(errno)));
- return map_nt_error_from_unix(errno);
- }
-
- DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
- path, mask, wd));
-
w = talloc(in, struct inotify_watch_context);
if (w == NULL) {
- inotify_rm_watch(in->fd, wd);
*filter = orig_filter;
- return NT_STATUS_NO_MEMORY;
+ return ENOMEM;
}
w->in = in;
- w->wd = wd;
w->callback = callback;
w->private_data = private_data;
w->mask = mask;
w->filter = orig_filter;
w->path = talloc_strdup(w, path);
if (w->path == NULL) {
- inotify_rm_watch(in->fd, wd);
*filter = orig_filter;
TALLOC_FREE(w);
- return NT_STATUS_NO_MEMORY;
+ return ENOMEM;
}
+ /* get a new watch descriptor for this path */
+ w->wd = inotify_add_watch(in->fd, path, mask);
+ if (w->wd == -1) {
+ int err = errno;
+ *filter = orig_filter;
+ TALLOC_FREE(w);
+ DEBUG(1, ("inotify_add_watch returned %s\n", strerror(err)));
+ return err;
+ }
+
+ DEBUG(10, ("inotify_add_watch for %s mask %x returned wd %d\n",
+ path, mask, w->wd));
+
(*handle) = w;
DLIST_ADD(in->watches, w);
/* the caller frees the handle to stop watching */
talloc_set_destructor(w, watch_destructor);
- return NT_STATUS_OK;
+ return 0;
}
#endif