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