r14956: change the notify search to be much more efficient by using a
[samba.git] / source4 / ntvfs / sysdep / sys_notify.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   abstract the various kernel interfaces to change notify into a
23   single Samba friendly interface
24 */
25
26 #include "includes.h"
27 #include "system/filesys.h"
28 #include "ntvfs/sysdep/sys_notify.h"
29 #include "lib/events/events.h"
30 #include "dlinklist.h"
31
32 /* list of registered backends */
33 static struct sys_notify_backend *backends;
34 static uint32_t num_backends;
35
36 /*
37   initialise a system change notify backend
38 */
39 struct sys_notify_context *sys_notify_init(int snum,
40                                            TALLOC_CTX *mem_ctx, 
41                                            struct event_context *ev)
42 {
43         struct sys_notify_context *ctx;
44         const char *bname;
45         int i;
46
47         if (num_backends == 0) {
48                 return NULL;
49         }
50
51         if (ev == NULL) {
52                 ev = event_context_find(mem_ctx);
53         }
54
55         ctx = talloc_zero(mem_ctx, struct sys_notify_context);
56         if (ctx == NULL) {
57                 return NULL;
58         }
59
60         ctx->ev = ev;
61
62         bname = lp_parm_string(snum, "notify", "backend");
63         if (!bname) {
64                 if (num_backends) {
65                         bname = backends[0].name;
66                 } else {
67                         bname = "__unknown__";
68                 }
69         }
70
71         for (i=0;i<num_backends;i++) {
72                 if (strcasecmp(backends[i].name, bname) == 0) {
73                         bname = backends[i].name;
74                         break;
75                 }
76         }
77
78         ctx->name = bname;
79         ctx->notify_watch = NULL;
80
81         if (i < num_backends) {
82                 ctx->notify_watch = backends[i].notify_watch;
83         }
84
85         return ctx;
86 }
87
88 /*
89   add a watch
90
91   note that this call must modify the e->filter and e->subdir_filter
92   bits to remove ones handled by this backend. Any remaining bits will
93   be handled by the generic notify layer
94 */
95 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx, struct notify_entry *e,
96                           sys_notify_callback_t callback, void *private, void **handle)
97 {
98         if (!ctx->notify_watch) {
99                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
100         }
101         return ctx->notify_watch(ctx, e, callback, private, handle);
102 }
103
104 /*
105   register a notify backend
106 */
107 NTSTATUS sys_notify_register(struct sys_notify_backend *backend)
108 {
109         struct sys_notify_backend *b;
110         b = talloc_realloc(talloc_autofree_context(), backends, 
111                            struct sys_notify_backend, num_backends+1);
112         NT_STATUS_HAVE_NO_MEMORY(b);
113         backends = b;
114         backends[num_backends] = *backend;
115         num_backends++;
116         return NT_STATUS_OK;
117 }