r14964: - move sidmap code from ntvfs_common to SAMDB
[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 #include "build.h"
32
33 /* list of registered backends */
34 static struct sys_notify_backend *backends;
35 static uint32_t num_backends;
36
37 /*
38   initialise a system change notify backend
39 */
40 struct sys_notify_context *sys_notify_context_create(int snum,
41                                                      TALLOC_CTX *mem_ctx, 
42                                                      struct event_context *ev)
43 {
44         struct sys_notify_context *ctx;
45         const char *bname;
46         int i;
47
48         if (num_backends == 0) {
49                 return NULL;
50         }
51
52         if (ev == NULL) {
53                 ev = event_context_find(mem_ctx);
54         }
55
56         ctx = talloc_zero(mem_ctx, struct sys_notify_context);
57         if (ctx == NULL) {
58                 return NULL;
59         }
60
61         ctx->ev = ev;
62
63         bname = lp_parm_string(snum, "notify", "backend");
64         if (!bname) {
65                 if (num_backends) {
66                         bname = backends[0].name;
67                 } else {
68                         bname = "__unknown__";
69                 }
70         }
71
72         for (i=0;i<num_backends;i++) {
73                 if (strcasecmp(backends[i].name, bname) == 0) {
74                         bname = backends[i].name;
75                         break;
76                 }
77         }
78
79         ctx->name = bname;
80         ctx->notify_watch = NULL;
81
82         if (i < num_backends) {
83                 ctx->notify_watch = backends[i].notify_watch;
84         }
85
86         return ctx;
87 }
88
89 /*
90   add a watch
91
92   note that this call must modify the e->filter and e->subdir_filter
93   bits to remove ones handled by this backend. Any remaining bits will
94   be handled by the generic notify layer
95 */
96 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx, struct notify_entry *e,
97                           sys_notify_callback_t callback, void *private, void **handle)
98 {
99         if (!ctx->notify_watch) {
100                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
101         }
102         return ctx->notify_watch(ctx, e, callback, private, handle);
103 }
104
105 /*
106   register a notify backend
107 */
108 NTSTATUS sys_notify_register(struct sys_notify_backend *backend)
109 {
110         struct sys_notify_backend *b;
111         b = talloc_realloc(talloc_autofree_context(), backends, 
112                            struct sys_notify_backend, num_backends+1);
113         NT_STATUS_HAVE_NO_MEMORY(b);
114         backends = b;
115         backends[num_backends] = *backend;
116         num_backends++;
117         return NT_STATUS_OK;
118 }
119
120 NTSTATUS sys_notify_init(void)
121 {
122         static BOOL initialized = False;
123
124         init_module_fn static_init[] = STATIC_sys_notify_MODULES;
125         init_module_fn *shared_init;
126
127         if (initialized) return NT_STATUS_OK;
128         initialized = True;
129
130         shared_init = load_samba_modules(NULL, "sys_notify");
131
132         run_init_functions(static_init);
133         run_init_functions(shared_init);
134
135         talloc_free(shared_init);
136         
137         return NT_STATUS_OK;
138 }