r14800: use tdb_get_seqnum() in the change notify code to avoid reloading the
[ira/wip.git] / source4 / ntvfs / common / 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   this is the change notify database. It implements mechanisms for
23   storing current change notify waiters in a tdb, and checking if a
24   given event matches any of the stored notify waiiters.
25 */
26
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "lib/tdb/include/tdb.h"
30 #include "lib/tdb/include/tdbutil.h"
31 #include "messaging/messaging.h"
32 #include "db_wrap.h"
33 #include "lib/messaging/irpc.h"
34 #include "librpc/gen_ndr/ndr_notify.h"
35 #include "dlinklist.h"
36
37 struct notify_context {
38         struct tdb_wrap *w;
39         uint32_t server;
40         struct messaging_context *messaging_ctx;
41         struct notify_list *list;
42         struct notify_array *array;
43         int seqnum;
44 };
45
46
47 struct notify_list {
48         struct notify_list *next, *prev;
49         void *private;
50         void (*callback)(void *, const struct notify_event *);
51 };
52
53 #define NOTIFY_KEY "notify array"
54
55 static NTSTATUS notify_remove_all(struct notify_context *notify);
56 static void notify_handler(struct messaging_context *msg_ctx, void *private, 
57                            uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
58
59 /*
60   destroy the notify context
61 */
62 static int notify_destructor(void *p)
63 {
64         struct notify_context *notify = talloc_get_type(p, struct notify_context);
65         messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
66         notify_remove_all(notify);
67         return 0;
68 }
69
70 /*
71   Open up the notify.tdb database. You should close it down using
72   talloc_free(). We need the messaging_ctx to allow for notifications
73   via internal messages
74 */
75 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, uint32_t server, 
76                                    struct messaging_context *messaging_ctx)
77 {
78         char *path;
79         struct notify_context *notify;
80
81         notify = talloc(mem_ctx, struct notify_context);
82         if (notify == NULL) {
83                 return NULL;
84         }
85
86         path = smbd_tmp_path(notify, "notify.tdb");
87         notify->w = tdb_wrap_open(notify, path, 0,  
88                                   TDB_SEQNUM,
89                                   O_RDWR|O_CREAT, 0600);
90         talloc_free(path);
91         if (notify->w == NULL) {
92                 talloc_free(notify);
93                 return NULL;
94         }
95
96         notify->server = server;
97         notify->messaging_ctx = messaging_ctx;
98         notify->list = NULL;
99         notify->array = NULL;
100         notify->seqnum = tdb_get_seqnum(notify->w->tdb);
101
102         talloc_set_destructor(notify, notify_destructor);
103
104         /* register with the messaging subsystem for the notify
105            message type */
106         messaging_register(notify->messaging_ctx, notify, 
107                            MSG_PVFS_NOTIFY, notify_handler);
108
109         return notify;
110 }
111
112 /*
113   load the notify array
114 */
115 static NTSTATUS notify_load(struct notify_context *notify)
116 {
117         TDB_DATA dbuf;
118         DATA_BLOB blob;
119         NTSTATUS status;
120         int seqnum;
121
122         seqnum = tdb_get_seqnum(notify->w->tdb);
123
124         if (seqnum == notify->seqnum && notify->array != NULL) {
125                 return NT_STATUS_OK;
126         }
127
128         notify->seqnum = seqnum;
129
130         talloc_free(notify->array);
131         notify->array = talloc_zero(notify, struct notify_array);
132         NT_STATUS_HAVE_NO_MEMORY(notify->array);
133
134         dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
135         if (dbuf.dptr == NULL) {
136                 return NT_STATUS_OK;
137         }
138
139         blob.data = dbuf.dptr;
140         blob.length = dbuf.dsize;
141
142         status = ndr_pull_struct_blob(&blob, notify->array, notify->array, 
143                                       (ndr_pull_flags_fn_t)ndr_pull_notify_array);
144         free(dbuf.dptr);
145
146         return status;
147 }
148
149
150 /*
151   save the notify array
152 */
153 static NTSTATUS notify_save(struct notify_context *notify)
154 {
155         TDB_DATA dbuf;
156         DATA_BLOB blob;
157         NTSTATUS status;
158         int ret;
159         TALLOC_CTX *tmp_ctx;
160
161         if (notify->array->num_entries == 0) {
162                 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
163                 if (ret != 0) {
164                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
165                 }
166                 return NT_STATUS_OK;
167         }
168
169         tmp_ctx = talloc_new(notify);
170
171         status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array, 
172                                       (ndr_push_flags_fn_t)ndr_push_notify_array);
173         if (!NT_STATUS_IS_OK(status)) {
174                 talloc_free(tmp_ctx);
175                 return status;
176         }
177
178         dbuf.dptr = blob.data;
179         dbuf.dsize = blob.length;
180                 
181         ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
182         talloc_free(tmp_ctx);
183         if (ret != 0) {
184                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
185         }
186
187         return NT_STATUS_OK;
188 }
189
190
191 /*
192   handle incoming notify messages
193 */
194 static void notify_handler(struct messaging_context *msg_ctx, void *private, 
195                            uint32_t msg_type, uint32_t server_id, DATA_BLOB *data)
196 {
197         struct notify_context *notify = talloc_get_type(private, struct notify_context);
198         NTSTATUS status;
199         struct notify_event ev;
200         TALLOC_CTX *tmp_ctx = talloc_new(notify);
201         struct notify_list *listel;
202
203         status = ndr_pull_struct_blob(data, tmp_ctx, &ev, 
204                                       (ndr_pull_flags_fn_t)ndr_pull_notify_event);
205         if (!NT_STATUS_IS_OK(status)) {
206                 talloc_free(tmp_ctx);
207                 return;
208         }
209
210         for (listel=notify->list;listel;listel=listel->next) {
211                 if (listel->private == ev.private) {
212                         listel->callback(listel->private, &ev);
213                         break;
214                 }
215         }
216
217         talloc_free(tmp_ctx);   
218 }
219
220 /*
221   add a notify watch. This is called when a notify is first setup on a open
222   directory handle.
223 */
224 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e,
225                     void (*callback)(void *, const struct notify_event *), 
226                     void *private)
227 {
228         NTSTATUS status;
229         struct notify_list *listel;
230
231         status = notify_load(notify);
232         NT_STATUS_NOT_OK_RETURN(status);
233
234         notify->array->entries = talloc_realloc(notify->array, notify->array->entries, 
235                                                 struct notify_entry,
236                                                 notify->array->num_entries+1);
237
238         if (notify->array->entries == NULL) {
239                 return NT_STATUS_NO_MEMORY;
240         }
241
242         notify->array->entries[notify->array->num_entries] = *e;
243         notify->array->entries[notify->array->num_entries].private = private;
244         notify->array->entries[notify->array->num_entries].server = notify->server;
245         notify->array->num_entries++;
246
247         status = notify_save(notify);
248         NT_STATUS_NOT_OK_RETURN(status);
249
250         listel = talloc(notify, struct notify_list);
251         NT_STATUS_HAVE_NO_MEMORY(listel);
252
253         listel->private = private;
254         listel->callback = callback;
255         DLIST_ADD(notify->list, listel);
256
257         return status;
258 }
259
260 /*
261   remove a notify watch. Called when the directory handle is closed
262 */
263 NTSTATUS notify_remove(struct notify_context *notify, void *private)
264 {
265         NTSTATUS status;
266         struct notify_list *listel;
267         int i;
268
269         for (listel=notify->list;listel;listel=listel->next) {
270                 if (listel->private == private) {
271                         DLIST_REMOVE(notify->list, listel);
272                         break;
273                 }
274         }
275         if (listel == NULL) {
276                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
277         }
278
279         status = notify_load(notify);
280         NT_STATUS_NOT_OK_RETURN(status);
281
282         for (i=0;i<notify->array->num_entries;i++) {
283                 if (notify->server == notify->array->entries[i].server && 
284                     private == notify->array->entries[i].private) {
285                         break;
286                 }
287         }
288         if (i == notify->array->num_entries) {
289                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
290         }
291
292         if (i < notify->array->num_entries-1) {
293                 memmove(&notify->array->entries[i], &notify->array->entries[i+1], 
294                         sizeof(notify->array->entries[i])*(notify->array->num_entries-(i+1)));
295         }
296         notify->array->num_entries--;
297
298         return notify_save(notify);
299 }
300
301 /*
302   remove all notify watches for this messaging server
303 */
304 static NTSTATUS notify_remove_all(struct notify_context *notify)
305 {
306         NTSTATUS status;
307         int i;
308
309         if (notify->list == NULL) {
310                 return NT_STATUS_OK;
311         }
312
313         status = notify_load(notify);
314         NT_STATUS_NOT_OK_RETURN(status);
315
316         for (i=0;i<notify->array->num_entries;i++) {
317                 if (notify->server == notify->array->entries[i].server) {
318                         if (i < notify->array->num_entries-1) {
319                                 memmove(&notify->array->entries[i], &notify->array->entries[i+1], 
320                                         sizeof(notify->array->entries[i])*(notify->array->num_entries-(i+1)));
321                         }
322                         i--;
323                         notify->array->num_entries--;
324                 }
325         }
326
327
328         return notify_save(notify);
329 }
330
331
332 /*
333   see if a notify event matches
334 */
335 static BOOL notify_match(struct notify_context *notify, struct notify_entry *e,
336                          const char *path, uint32_t filter)
337 {
338         size_t len;
339
340         if (!(filter & e->filter)) {
341                 return False;
342         }
343
344         len = strlen(e->path);
345
346         if (strncmp(path, e->path, len) != 0) {
347                 return False;
348         }
349
350         if (path[len] != '/') {
351                 return False;
352         }
353
354         if (!e->recursive) {
355                 if (strchr(&path[len+1], '/') != NULL) {
356                         return False;
357                 }
358         }
359
360         return True;
361 }
362
363
364 /*
365   send a notify message to another messaging server
366 */
367 static void notify_send(struct notify_context *notify, struct notify_entry *e,
368                         const char *path, uint32_t action)
369 {
370         struct notify_event ev;
371         DATA_BLOB data;
372         NTSTATUS status;
373         TALLOC_CTX *tmp_ctx;
374
375         ev.action = action;
376         ev.path = path;
377         ev.private = e->private;
378
379         tmp_ctx = talloc_new(notify);
380
381         status = ndr_push_struct_blob(&data, tmp_ctx, &ev, 
382                                       (ndr_push_flags_fn_t)ndr_push_notify_event);
383         if (!NT_STATUS_IS_OK(status)) {
384                 talloc_free(tmp_ctx);
385                 return;
386         }
387
388         status = messaging_send(notify->messaging_ctx, e->server, 
389                                 MSG_PVFS_NOTIFY, &data);
390         talloc_free(tmp_ctx);
391 }
392
393 /*
394   trigger a notify message for anyone waiting on a matching event
395 */
396 void notify_trigger(struct notify_context *notify,
397                     uint32_t action, uint32_t filter, const char *path)
398 {
399         NTSTATUS status;
400         int i;
401
402         status = notify_load(notify);
403         if (!NT_STATUS_IS_OK(status)) {
404                 return;
405         }
406
407         /* this needs to be changed to a log(n) search */
408         for (i=0;i<notify->array->num_entries;i++) {
409                 if (notify_match(notify, &notify->array->entries[i], path, filter)) {
410                         notify_send(notify, &notify->array->entries[i], 
411                                     path + strlen(notify->array->entries[i].path) + 1, 
412                                     action);
413                 }
414         }
415 }