1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

r14877: added support for the kernel inotify mechanism. This passes basic

tests, but still needs some more work to ensure we correctly cope with
events that may generate both a system inotify event and a internal
notify event. The system inotify events won't handle recursion, and
don't understand things like streams.

This also adds the ntvfs/sysdep/ directory, which is meant for system
dependent code that is not tied to a particular ntvfs backend. The
inotify code is a good example of that.
(This used to be commit eadadbb44a)
This commit is contained in:
Andrew Tridgell 2006-04-03 06:46:55 +00:00 committed by Gerald (Jerry) Carter
parent 69e40fa99a
commit 6d98076c15
10 changed files with 591 additions and 53 deletions

View File

@ -42,6 +42,7 @@ sinclude(lib/socket_wrapper/config.m4)
sinclude(web_server/config.m4)
sinclude(auth/config.m4)
sinclude(kdc/config.m4)
sinclude(ntvfs/sysdep/config.m4)
AC_ARG_ENABLE(dso,
[ --enable-dso Enable building internal libraries as DSO's (experimental)],

View File

@ -33,6 +33,7 @@
#include "lib/messaging/irpc.h"
#include "librpc/gen_ndr/ndr_notify.h"
#include "dlinklist.h"
#include "ntvfs/sysdep/sys_notify.h"
struct notify_context {
struct tdb_wrap *w;
@ -41,6 +42,7 @@ struct notify_context {
struct notify_list *list;
struct notify_array *array;
int seqnum;
struct sys_notify_context *sys_notify_ctx;
};
@ -48,6 +50,7 @@ struct notify_list {
struct notify_list *next, *prev;
void *private;
void (*callback)(void *, const struct notify_event *);
void *sys_notify_handle;
};
#define NOTIFY_KEY "notify array"
@ -73,7 +76,8 @@ static int notify_destructor(void *p)
via internal messages
*/
struct notify_context *notify_init(TALLOC_CTX *mem_ctx, uint32_t server,
struct messaging_context *messaging_ctx)
struct messaging_context *messaging_ctx,
struct event_context *ev)
{
char *path;
struct notify_context *notify;
@ -106,6 +110,8 @@ struct notify_context *notify_init(TALLOC_CTX *mem_ctx, uint32_t server,
messaging_register(notify->messaging_ctx, notify,
MSG_PVFS_NOTIFY, notify_handler);
notify->sys_notify_ctx = sys_notify_init(-1, notify, ev);
return notify;
}
@ -166,6 +172,14 @@ static NTSTATUS notify_load(struct notify_context *notify)
return status;
}
/*
compare notify entries for sorting
*/
static int notify_compare(const void *p1, const void *p2)
{
const struct notify_entry *e1 = p1, *e2 = p2;
return strcmp(e1->path, e2->path);
}
/*
save the notify array
@ -186,6 +200,11 @@ static NTSTATUS notify_save(struct notify_context *notify)
return NT_STATUS_OK;
}
if (notify->array->num_entries > 1) {
qsort(notify->array->entries, notify->array->num_entries,
sizeof(struct notify_entry), notify_compare);
}
tmp_ctx = talloc_new(notify);
status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array,
@ -237,6 +256,17 @@ static void notify_handler(struct messaging_context *msg_ctx, void *private,
talloc_free(tmp_ctx);
}
/*
callback from sys_notify telling us about changes from the OS
*/
static void sys_notify_callback(struct sys_notify_context *ctx,
void *ptr, struct notify_event *ev)
{
struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
ev->private = listel;
listel->callback(listel->private, ev);
}
/*
add a notify watch. This is called when a notify is first setup on a open
directory handle.
@ -274,33 +304,42 @@ NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e,
path = talloc_strndup(notify, e->path, len-2);
}
notify->array->entries[notify->array->num_entries] = *e;
notify->array->entries[notify->array->num_entries].private = private;
notify->array->entries[notify->array->num_entries].server = notify->server;
if (path) {
notify->array->entries[notify->array->num_entries].path = path;
}
notify->array->num_entries++;
status = notify_save(notify);
notify_unlock(notify);
NT_STATUS_NOT_OK_RETURN(status);
if (path) {
talloc_free(path);
}
listel = talloc(notify, struct notify_list);
listel = talloc_zero(notify, struct notify_list);
NT_STATUS_HAVE_NO_MEMORY(listel);
listel->private = private;
listel->callback = callback;
DLIST_ADD(notify->list, listel);
/* ignore failures from sys_notify */
status = sys_notify_watch(notify->sys_notify_ctx, e->path, e->filter,
sys_notify_callback, listel,
&listel->sys_notify_handle);
if (NT_STATUS_IS_OK(status)) {
talloc_steal(listel, listel->sys_notify_handle);
notify_unlock(notify);
} else {
notify->array->entries[notify->array->num_entries] = *e;
notify->array->entries[notify->array->num_entries].private = private;
notify->array->entries[notify->array->num_entries].server = notify->server;
if (path) {
notify->array->entries[notify->array->num_entries].path = path;
}
notify->array->num_entries++;
status = notify_save(notify);
notify_unlock(notify);
NT_STATUS_NOT_OK_RETURN(status);
}
if (path) {
talloc_free(path);
}
return status;
}
@ -323,6 +362,8 @@ NTSTATUS notify_remove(struct notify_context *notify, void *private)
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
talloc_free(listel);
status = notify_lock(notify);
NT_STATUS_NOT_OK_RETURN(status);
@ -397,6 +438,35 @@ static NTSTATUS notify_remove_all(struct notify_context *notify)
}
/*
send a notify message to another messaging server
*/
static void notify_send(struct notify_context *notify, struct notify_entry *e,
const char *path, uint32_t action)
{
struct notify_event ev;
DATA_BLOB data;
NTSTATUS status;
TALLOC_CTX *tmp_ctx;
ev.action = action;
ev.path = path;
ev.private = e->private;
tmp_ctx = talloc_new(notify);
status = ndr_push_struct_blob(&data, tmp_ctx, &ev,
(ndr_push_flags_fn_t)ndr_push_notify_event);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return;
}
status = messaging_send(notify->messaging_ctx, e->server,
MSG_PVFS_NOTIFY, &data);
talloc_free(tmp_ctx);
}
/*
see if a notify event matches
*/
@ -429,35 +499,6 @@ static BOOL notify_match(struct notify_context *notify, struct notify_entry *e,
}
/*
send a notify message to another messaging server
*/
static void notify_send(struct notify_context *notify, struct notify_entry *e,
const char *path, uint32_t action)
{
struct notify_event ev;
DATA_BLOB data;
NTSTATUS status;
TALLOC_CTX *tmp_ctx;
ev.action = action;
ev.path = path;
ev.private = e->private;
tmp_ctx = talloc_new(notify);
status = ndr_push_struct_blob(&data, tmp_ctx, &ev,
(ndr_push_flags_fn_t)ndr_push_notify_event);
if (!NT_STATUS_IS_OK(status)) {
talloc_free(tmp_ctx);
return;
}
status = messaging_send(notify->messaging_ctx, e->server,
MSG_PVFS_NOTIFY, &data);
talloc_free(tmp_ctx);
}
/*
trigger a notify message for anyone waiting on a matching event
*/
@ -472,7 +513,7 @@ void notify_trigger(struct notify_context *notify,
return;
}
/* this needs to be changed to a log(n) search */
/* TODO: this needs to be changed to a log(n) search */
for (i=0;i<notify->array->num_entries;i++) {
if (notify_match(notify, &notify->array->entries[i], path, filter)) {
notify_send(notify, &notify->array->entries[i],

View File

@ -2,6 +2,7 @@
include posix/config.mk
include common/config.mk
include unixuid/config.mk
include sysdep/config.mk
################################################
# Start MODULE ntvfs_cifs

View File

@ -30,6 +30,7 @@
#include "lib/tdb/include/tdb.h"
#include "db_wrap.h"
#include "libcli/security/security.h"
#include "lib/events/events.h"
/*
@ -186,7 +187,8 @@ static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
pvfs->notify_context = notify_init(pvfs,
pvfs->ntvfs->ctx->server_id,
pvfs->ntvfs->ctx->msg_ctx);
pvfs->ntvfs->ctx->msg_ctx,
event_context_find(pvfs));
if (pvfs->notify_context == NULL) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}

View File

@ -0,0 +1,5 @@
This directory contains OS depdendent interfaces to facilities that
are only available on a few of our target systems, and require
substantial code to abstract.

View File

@ -0,0 +1,7 @@
AC_CHECK_HEADERS(linux/inotify.h asm/unistd.h)
AC_CHECK_FUNC(inotify_init)
SMB_ENABLE(ntvfs_inotify, NO)
if test x"$ac_cv_header_linux_inotify_h" = x"yes"; then
SMB_ENABLE(ntvfs_inotify, YES)
fi

View File

@ -0,0 +1,20 @@
################################################
# Start MODULE ntvfs_sys_notify
[MODULE::ntvfs_sys_notify]
SUBSYSTEM = ntvfs
OBJ_FILES = \
sys_notify.o
# End MODULE ntvfs_sys_notify
################################################
################################################
# Start MODULE ntvfs_inotify
[MODULE::ntvfs_inotify]
SUBSYSTEM = ntvfs
INIT_FUNCTION = ntvfs_inotify_init
OBJ_FILES = \
inotify.o
# End MODULE ntvfs_inotify
################################################

View File

@ -0,0 +1,313 @@
/*
Unix SMB/CIFS implementation.
Copyright (C) Andrew Tridgell 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
notify implementation using inotify
*/
#include "includes.h"
#include "system/filesys.h"
#include "ntvfs/sysdep/sys_notify.h"
#include "lib/events/events.h"
#include "smb.h"
#include "dlinklist.h"
#include <linux/inotify.h>
#include <asm/unistd.h>
#ifndef HAVE_INOTIFY_INIT
/*
glibc doesn't define these functions yet (as of March 2006)
*/
static int inotify_init(void)
{
return syscall(__NR_inotify_init);
}
static int inotify_add_watch(int fd, const char *path, __u32 mask)
{
return syscall(__NR_inotify_add_watch, fd, path, mask);
}
static int inotify_rm_watch(int fd, int wd)
{
return syscall(__NR_inotify_rm_watch, fd, wd);
}
#endif
/* older glibc headers don't have these defines either */
#ifndef IN_ONLYDIR
#define IN_ONLYDIR 0x01000000
#endif
#ifndef IN_MASK_ADD
#define IN_MASK_ADD 0x20000000
#endif
struct inotify_private {
struct sys_notify_context *ctx;
int fd;
struct watch_context *watches;
};
struct watch_context {
struct watch_context *next, *prev;
struct inotify_private *in;
int wd;
sys_notify_callback_t callback;
void *private;
uint32_t mask;
};
/*
destroy the inotify private context
*/
static int inotify_destructor(void *ptr)
{
struct inotify_private *in = talloc_get_type(ptr, struct inotify_private);
close(in->fd);
return 0;
}
/*
dispatch one inotify event
*/
static void inotify_dispatch(struct inotify_private *in, struct inotify_event *e)
{
struct watch_context *w;
struct notify_event ne;
/* ignore extraneous events, such as unmount and IN_IGNORED events */
if ((e->mask & (IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO)) == 0) {
return;
}
/* map the inotify mask to a action */
if (e->mask & IN_CREATE) {
ne.action = NOTIFY_ACTION_ADDED;
} else if (e->mask & IN_DELETE) {
ne.action = NOTIFY_ACTION_REMOVED;
} else {
ne.action = NOTIFY_ACTION_MODIFIED;
}
ne.path = e->name;
/* find any watches that have this watch descriptor */
for (w=in->watches;w;w=w->next) {
/* checking the mask copes with multiple watches */
if (w->wd == e->wd && (e->mask & w->mask) != 0) {
w->callback(in->ctx, w->private, &ne);
}
}
}
/*
called when the kernel has some events for us
*/
static void inotify_handler(struct event_context *ev, struct fd_event *fde,
uint16_t flags, void *private)
{
struct inotify_private *in = talloc_get_type(private, struct inotify_private);
int bufsize = 0;
struct inotify_event *e0, *e;
/*
we must use FIONREAD as we cannot predict the length of the
filenames, and thus can't know how much to allocate
otherwise
*/
if (ioctl(in->fd, FIONREAD, &bufsize) != 0 ||
bufsize == 0) {
DEBUG(0,("No data on inotify fd?!\n"));
return;
}
e0 = e = talloc_size(in, bufsize);
if (e == NULL) return;
if (read(in->fd, e0, bufsize) != bufsize) {
DEBUG(0,("Failed to read all inotify data\n"));
talloc_free(e0);
return;
}
/* we can get more than one event in the buffer */
while (bufsize >= sizeof(*e)) {
inotify_dispatch(in, e);
bufsize -= e->len + sizeof(*e);
e = (struct inotify_event *)(e->len + sizeof(*e) + (char *)e);
}
talloc_free(e0);
}
/*
setup the inotify handle - called the first time a watch is added on
this context
*/
static NTSTATUS inotify_setup(struct sys_notify_context *ctx)
{
struct inotify_private *in;
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)));
talloc_free(in);
return map_nt_error_from_unix(errno);
}
in->ctx = ctx;
in->watches = NULL;
ctx->private = in;
talloc_set_destructor(in, inotify_destructor);
/* add a event waiting for the inotify fd to be readable */
event_add_fd(ctx->ev, in, in->fd, EVENT_FD_READ, inotify_handler, in);
return NT_STATUS_OK;
}
/*
map from a change notify mask to a inotify mask. Approximate only :(
*/
static const struct {
uint32_t notify_mask;
uint32_t inotify_mask;
} inotify_mapping[] = {
{FILE_NOTIFY_CHANGE_FILE_NAME, IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
{FILE_NOTIFY_CHANGE_ATTRIBUTES, IN_ATTRIB},
{FILE_NOTIFY_CHANGE_SIZE, IN_MODIFY},
{FILE_NOTIFY_CHANGE_LAST_WRITE, IN_ATTRIB},
{FILE_NOTIFY_CHANGE_EA, IN_ATTRIB},
{FILE_NOTIFY_CHANGE_SECURITY, IN_ATTRIB}
};
static uint32_t inotify_map(uint32_t mask)
{
int i;
uint32_t out=0;
for (i=0;i<ARRAY_SIZE(inotify_mapping);i++) {
if (inotify_mapping[i].notify_mask & mask) {
out |= inotify_mapping[i].inotify_mask;
}
}
return out;
}
/*
destroy a watch
*/
static int watch_destructor(void *ptr)
{
struct watch_context *w = talloc_get_type(ptr, struct watch_context);
struct inotify_private *in = w->in;
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) {
inotify_rm_watch(in->fd, wd);
}
return 0;
}
/*
add a watch. The watch is removed when the caller calls
talloc_free() on handle
*/
static NTSTATUS inotify_watch(struct sys_notify_context *ctx, const char *dirpath,
uint32_t filter, sys_notify_callback_t callback,
void *private, void **handle)
{
struct inotify_private *in;
int wd;
uint32_t mask;
struct watch_context *w;
/* maybe setup the inotify fd */
if (ctx->private == NULL) {
NTSTATUS status;
status = inotify_setup(ctx);
NT_STATUS_NOT_OK_RETURN(status);
}
in = talloc_get_type(ctx->private, struct inotify_private);
mask = inotify_map(filter);
if (mask == 0) {
/* this filter can't be handled by inotify */
return NT_STATUS_INVALID_PARAMETER;
}
/* using IN_MASK_ADD allows us to cope with inotify() returning the same
watch descriptor for muliple 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, dirpath, mask);
if (wd == -1) {
return map_nt_error_from_unix(errno);
}
w = talloc(in, struct watch_context);
if (w == NULL) {
inotify_rm_watch(in->fd, wd);
return NT_STATUS_NO_MEMORY;
}
w->in = in;
w->wd = wd;
w->callback = callback;
w->private = private;
w->mask = mask;
(*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;
}
static struct sys_notify_backend inotify = {
.name = "inotify",
.notify_watch = inotify_watch
};
/*
initialialise the inotify module
*/
NTSTATUS ntvfs_inotify_init(void)
{
/* register ourselves as a system inotify module */
return sys_notify_register(&inotify);
}

View File

@ -0,0 +1,96 @@
/*
Unix SMB/CIFS implementation.
Copyright (C) Andrew Tridgell 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
abstract the various kernel interfaces to change notify into a
single Samba friendly interface
*/
#include "includes.h"
#include "system/filesys.h"
#include "ntvfs/sysdep/sys_notify.h"
#include "lib/events/events.h"
#include "dlinklist.h"
/* list of registered backends */
static struct sys_notify_backend *backends;
/*
initialise a system change notify backend
*/
struct sys_notify_context *sys_notify_init(int snum,
TALLOC_CTX *mem_ctx,
struct event_context *ev)
{
struct sys_notify_context *ctx;
const char *bname;
struct sys_notify_backend *b;
if (backends == NULL) {
return NULL;
}
if (ev == NULL) {
ev = event_context_find(mem_ctx);
}
ctx = talloc_zero(mem_ctx, struct sys_notify_context);
if (ctx == NULL) {
return NULL;
}
ctx->ev = ev;
bname = lp_parm_string(snum, "notify", "backend");
if (bname == NULL) {
bname = backends->name;
}
for (b=backends;b;b=b->next) {
if (strcasecmp(b->name, bname) == 0) break;
}
if (b != NULL) {
ctx->name = b->name;
ctx->notify_watch = b->notify_watch;
}
return ctx;
}
/*
add a watch
*/
NTSTATUS sys_notify_watch(struct sys_notify_context *ctx, const char *dirpath,
uint32_t filter, sys_notify_callback_t callback,
void *private, void **handle)
{
return ctx->notify_watch(ctx, dirpath, filter, callback, private, handle);
}
/*
register a notify backend
*/
NTSTATUS sys_notify_register(struct sys_notify_backend *backend)
{
DLIST_ADD(backends, backend);
return NT_STATUS_OK;
}

View File

@ -0,0 +1,52 @@
/*
Unix SMB/CIFS implementation.
Copyright (C) Andrew Tridgell 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "librpc/gen_ndr/notify.h"
struct sys_notify_context;
typedef void (*sys_notify_callback_t)(struct sys_notify_context *,
void *, struct notify_event *ev);
struct sys_notify_context {
struct event_context *ev;
void *private; /* for use of backend */
NTSTATUS (*notify_watch)(struct sys_notify_context *ctx, const char *dirpath,
uint32_t filter, sys_notify_callback_t callback,
void *private, void **handle);
const char *name;
};
struct sys_notify_backend {
struct sys_notify_backend *next, *prev;
const char *name;
NTSTATUS (*notify_watch)(struct sys_notify_context *ctx, const char *dirpath,
uint32_t filter, sys_notify_callback_t callback,
void *private, void **handle);
};
NTSTATUS sys_notify_register(struct sys_notify_backend *backend);
struct sys_notify_context *sys_notify_init(int snum,
TALLOC_CTX *mem_ctx,
struct event_context *ev);
NTSTATUS sys_notify_watch(struct sys_notify_context *ctx, const char *dirpath,
uint32_t filter, sys_notify_callback_t callback,
void *private, void **handle);