r14959: allow change notify to be disabled completely using
[kai/samba-autobuild/.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 #include "ntvfs/sysdep/sys_notify.h"
37
38 struct notify_context {
39         struct tdb_wrap *w;
40         uint32_t server;
41         struct messaging_context *messaging_ctx;
42         struct notify_list *list;
43         struct notify_array *array;
44         int seqnum;
45         struct sys_notify_context *sys_notify_ctx;
46 };
47
48
49 struct notify_list {
50         struct notify_list *next, *prev;
51         void *private;
52         void (*callback)(void *, const struct notify_event *);
53         void *sys_notify_handle;
54         int depth;
55 };
56
57 #define NOTIFY_KEY "notify array"
58
59 static NTSTATUS notify_remove_all(struct notify_context *notify);
60 static void notify_handler(struct messaging_context *msg_ctx, void *private, 
61                            uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
62
63 /*
64   destroy the notify context
65 */
66 static int notify_destructor(void *p)
67 {
68         struct notify_context *notify = talloc_get_type(p, struct notify_context);
69         messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
70         notify_remove_all(notify);
71         return 0;
72 }
73
74 /*
75   Open up the notify.tdb database. You should close it down using
76   talloc_free(). We need the messaging_ctx to allow for notifications
77   via internal messages
78 */
79 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, uint32_t server, 
80                                    struct messaging_context *messaging_ctx,
81                                    struct event_context *ev, int snum)
82 {
83         char *path;
84         struct notify_context *notify;
85
86         if (lp_parm_bool(snum, "notify", "enable", True) != True) {
87                 return NULL;
88         }
89
90         notify = talloc(mem_ctx, struct notify_context);
91         if (notify == NULL) {
92                 return NULL;
93         }
94
95         path = smbd_tmp_path(notify, "notify.tdb");
96         notify->w = tdb_wrap_open(notify, path, 0,  
97                                   TDB_SEQNUM,
98                                   O_RDWR|O_CREAT, 0600);
99         talloc_free(path);
100         if (notify->w == NULL) {
101                 talloc_free(notify);
102                 return NULL;
103         }
104
105         notify->server = server;
106         notify->messaging_ctx = messaging_ctx;
107         notify->list = NULL;
108         notify->array = NULL;
109         notify->seqnum = tdb_get_seqnum(notify->w->tdb);
110
111         talloc_set_destructor(notify, notify_destructor);
112
113         /* register with the messaging subsystem for the notify
114            message type */
115         messaging_register(notify->messaging_ctx, notify, 
116                            MSG_PVFS_NOTIFY, notify_handler);
117
118         notify->sys_notify_ctx = sys_notify_init(snum, notify, ev);
119
120         return notify;
121 }
122
123
124 /*
125   lock the notify db
126 */
127 static NTSTATUS notify_lock(struct notify_context *notify)
128 {
129         if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
130                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
131         }
132         return NT_STATUS_OK;
133 }
134
135 /*
136   unlock the notify db
137 */
138 static void notify_unlock(struct notify_context *notify)
139 {
140         tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
141 }
142
143 /*
144   load the notify array
145 */
146 static NTSTATUS notify_load(struct notify_context *notify)
147 {
148         TDB_DATA dbuf;
149         DATA_BLOB blob;
150         NTSTATUS status;
151         int seqnum;
152
153         seqnum = tdb_get_seqnum(notify->w->tdb);
154
155         if (seqnum == notify->seqnum && notify->array != NULL) {
156                 return NT_STATUS_OK;
157         }
158
159         notify->seqnum = seqnum;
160
161         talloc_free(notify->array);
162         notify->array = talloc_zero(notify, struct notify_array);
163         NT_STATUS_HAVE_NO_MEMORY(notify->array);
164
165         dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
166         if (dbuf.dptr == NULL) {
167                 return NT_STATUS_OK;
168         }
169
170         blob.data = dbuf.dptr;
171         blob.length = dbuf.dsize;
172
173         status = ndr_pull_struct_blob(&blob, notify->array, notify->array, 
174                                       (ndr_pull_flags_fn_t)ndr_pull_notify_array);
175         free(dbuf.dptr);
176
177         return status;
178 }
179
180 /*
181   compare notify entries for sorting
182 */
183 static int notify_compare(const void *p1, const void *p2)
184 {
185         const struct notify_entry *e1 = p1, *e2 = p2;
186         return strcmp(e1->path, e2->path);
187 }
188
189 /*
190   save the notify array
191 */
192 static NTSTATUS notify_save(struct notify_context *notify)
193 {
194         TDB_DATA dbuf;
195         DATA_BLOB blob;
196         NTSTATUS status;
197         int ret;
198         TALLOC_CTX *tmp_ctx;
199
200         /* if possible, remove some depth arrays */
201         while (notify->array->num_depths > 0 &&
202                notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
203                 notify->array->num_depths--;
204         }
205
206         /* we might just be able to delete the record */
207         if (notify->array->num_depths == 0) {
208                 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
209                 if (ret != 0) {
210                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
211                 }
212                 return NT_STATUS_OK;
213         }
214
215         tmp_ctx = talloc_new(notify);
216
217         status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array, 
218                                       (ndr_push_flags_fn_t)ndr_push_notify_array);
219         if (!NT_STATUS_IS_OK(status)) {
220                 talloc_free(tmp_ctx);
221                 return status;
222         }
223
224         dbuf.dptr = blob.data;
225         dbuf.dsize = blob.length;
226                 
227         ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
228         talloc_free(tmp_ctx);
229         if (ret != 0) {
230                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
231         }
232
233         return NT_STATUS_OK;
234 }
235
236
237 /*
238   handle incoming notify messages
239 */
240 static void notify_handler(struct messaging_context *msg_ctx, void *private, 
241                            uint32_t msg_type, uint32_t server_id, DATA_BLOB *data)
242 {
243         struct notify_context *notify = talloc_get_type(private, struct notify_context);
244         NTSTATUS status;
245         struct notify_event ev;
246         TALLOC_CTX *tmp_ctx = talloc_new(notify);
247         struct notify_list *listel;
248
249         status = ndr_pull_struct_blob(data, tmp_ctx, &ev, 
250                                       (ndr_pull_flags_fn_t)ndr_pull_notify_event);
251         if (!NT_STATUS_IS_OK(status)) {
252                 talloc_free(tmp_ctx);
253                 return;
254         }
255
256         for (listel=notify->list;listel;listel=listel->next) {
257                 if (listel->private == ev.private) {
258                         listel->callback(listel->private, &ev);
259                         break;
260                 }
261         }
262
263         talloc_free(tmp_ctx);   
264 }
265
266 /*
267   callback from sys_notify telling us about changes from the OS
268 */
269 static void sys_notify_callback(struct sys_notify_context *ctx, 
270                                 void *ptr, struct notify_event *ev)
271 {
272         struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
273         ev->private = listel;
274         listel->callback(listel->private, ev);
275 }
276
277 /*
278   add an entry to the notify array
279 */
280 static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
281                                  void *private, int depth)
282 {
283         int i;
284         struct notify_depth *d;
285         struct notify_entry *ee;
286
287         /* possibly expand the depths array */
288         if (depth >= notify->array->num_depths) {
289                 d = talloc_realloc(notify->array, notify->array->depth, 
290                                    struct notify_depth, depth+1);
291                 NT_STATUS_HAVE_NO_MEMORY(d);
292                 for (i=notify->array->num_depths;i<=depth;i++) {
293                         ZERO_STRUCT(d[i]);
294                 }
295                 notify->array->depth = d;
296                 notify->array->num_depths = depth+1;
297         }
298         d = &notify->array->depth[depth];
299
300         /* expand the entries array */
301         ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
302                             d->num_entries+1);
303         NT_STATUS_HAVE_NO_MEMORY(ee);
304         d->entries = ee;
305
306         d->entries[d->num_entries] = *e;
307         d->entries[d->num_entries].private = private;
308         d->entries[d->num_entries].server = notify->server;
309         d->entries[d->num_entries].path_len = strlen(e->path);
310         d->num_entries++;
311
312         d->max_mask |= e->filter;
313         d->max_mask_subdir |= e->subdir_filter;
314
315         if (d->num_entries > 1) {
316                 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
317         }
318
319         /* recalculate the maximum masks */
320         d->max_mask = 0;
321         d->max_mask_subdir = 0;
322
323         for (i=0;i<d->num_entries;i++) {
324                 d->max_mask |= d->entries[i].filter;
325                 d->max_mask_subdir |= d->entries[i].subdir_filter;
326         }
327
328         return notify_save(notify);
329 }
330
331 /*
332   add a notify watch. This is called when a notify is first setup on a open
333   directory handle.
334 */
335 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
336                     void (*callback)(void *, const struct notify_event *), 
337                     void *private)
338 {
339         struct notify_entry e = *e0;
340         NTSTATUS status;
341         char *tmp_path = NULL;
342         struct notify_list *listel;
343         size_t len;
344         int depth;
345
346         /* see if change notify is enabled at all */
347         if (notify == NULL) {
348                 return NT_STATUS_NOT_IMPLEMENTED;
349         }
350
351         status = notify_lock(notify);
352         NT_STATUS_NOT_OK_RETURN(status);
353
354         status = notify_load(notify);
355         if (!NT_STATUS_IS_OK(status)) {
356                 goto done;
357         }
358
359         /* cope with /. on the end of the path */
360         len = strlen(e.path);
361         if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
362                 tmp_path = talloc_strndup(notify, e.path, len-2);
363                 if (tmp_path == NULL) {
364                         status = NT_STATUS_NO_MEMORY;
365                         goto done;
366                 }
367                 e.path = tmp_path;
368         }
369
370         depth = count_chars(e.path, '/');
371
372         listel = talloc_zero(notify, struct notify_list);
373         if (listel == NULL) {
374                 status = NT_STATUS_NO_MEMORY;
375                 goto done;
376         }
377
378         listel->private = private;
379         listel->callback = callback;
380         listel->depth = depth;
381         DLIST_ADD(notify->list, listel);
382
383         /* ignore failures from sys_notify */
384         if (notify->sys_notify_ctx != NULL) {
385                 /*
386                   this call will modify e.filter and e.subdir_filter
387                   to remove bits handled by the backend
388                 */
389                 status = sys_notify_watch(notify->sys_notify_ctx, &e,
390                                           sys_notify_callback, listel, 
391                                           &listel->sys_notify_handle);
392                 if (NT_STATUS_IS_OK(status)) {
393                         talloc_steal(listel, listel->sys_notify_handle);
394                 }
395         }
396
397         /* if the system notify handler couldn't handle some of the
398            filter bits, or couldn't handle a request for recursion
399            then we need to install it in the array used for the
400            intra-samba notify handling */
401         if (e.filter != 0 || e.subdir_filter != 0) {
402                 status = notify_add_array(notify, &e, private, depth);
403         }
404
405 done:
406         notify_unlock(notify);
407         talloc_free(tmp_path);
408
409         return status;
410 }
411
412 /*
413   remove a notify watch. Called when the directory handle is closed
414 */
415 NTSTATUS notify_remove(struct notify_context *notify, void *private)
416 {
417         NTSTATUS status;
418         struct notify_list *listel;
419         int i, depth;
420         struct notify_depth *d;
421
422         /* see if change notify is enabled at all */
423         if (notify == NULL) {
424                 return NT_STATUS_NOT_IMPLEMENTED;
425         }
426
427         for (listel=notify->list;listel;listel=listel->next) {
428                 if (listel->private == private) {
429                         DLIST_REMOVE(notify->list, listel);
430                         break;
431                 }
432         }
433         if (listel == NULL) {
434                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
435         }
436
437         depth = listel->depth;
438
439         talloc_free(listel);
440
441         status = notify_lock(notify);
442         NT_STATUS_NOT_OK_RETURN(status);
443
444         status = notify_load(notify);
445         if (!NT_STATUS_IS_OK(status)) {
446                 notify_unlock(notify);
447                 return status;
448         }
449
450         /* we only have to search at the depth of this element */
451         d = &notify->array->depth[depth];
452
453         for (i=0;i<d->num_entries;i++) {
454                 if (private == d->entries[i].private &&
455                     notify->server == d->entries[i].server) {
456                         break;
457                 }
458         }
459         if (i == d->num_entries) {
460                 notify_unlock(notify);
461                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
462         }
463
464         if (i < d->num_entries-1) {
465                 memmove(&d->entries[i], &d->entries[i+1], 
466                         sizeof(d->entries[i])*(d->num_entries-(i+1)));
467         }
468         d->num_entries--;
469
470         status = notify_save(notify);
471
472         notify_unlock(notify);
473
474         return status;
475 }
476
477 /*
478   remove all notify watches for this messaging server
479 */
480 static NTSTATUS notify_remove_all(struct notify_context *notify)
481 {
482         NTSTATUS status;
483         int i, depth, del_count=0;
484
485         if (notify->list == NULL) {
486                 return NT_STATUS_OK;
487         }
488
489         status = notify_lock(notify);
490         NT_STATUS_NOT_OK_RETURN(status);
491
492         status = notify_load(notify);
493         if (!NT_STATUS_IS_OK(status)) {
494                 notify_unlock(notify);
495                 return status;
496         }
497
498         /* we have to search for all entries across all depths, looking for matches
499            for our server id */
500         for (depth=0;depth<notify->array->num_depths;depth++) {
501                 struct notify_depth *d = &notify->array->depth[depth];
502                 for (i=0;i<d->num_entries;i++) {
503                         if (notify->server == d->entries[i].server) {
504                                 if (i < d->num_entries-1) {
505                                         memmove(&d->entries[i], &d->entries[i+1], 
506                                                 sizeof(d->entries[i])*(d->num_entries-(i+1)));
507                                 }
508                                 i--;
509                                 d->num_entries--;
510                                 del_count++;
511                         }
512                 }
513         }
514
515         if (del_count > 0) {
516                 status = notify_save(notify);
517         }
518
519         notify_unlock(notify);
520
521         return status;
522 }
523
524
525 /*
526   send a notify message to another messaging server
527 */
528 static void notify_send(struct notify_context *notify, struct notify_entry *e,
529                         const char *path, uint32_t action)
530 {
531         struct notify_event ev;
532         DATA_BLOB data;
533         NTSTATUS status;
534         TALLOC_CTX *tmp_ctx;
535
536         ev.action = action;
537         ev.path = path;
538         ev.private = e->private;
539
540         tmp_ctx = talloc_new(notify);
541
542         status = ndr_push_struct_blob(&data, tmp_ctx, &ev, 
543                                       (ndr_push_flags_fn_t)ndr_push_notify_event);
544         if (!NT_STATUS_IS_OK(status)) {
545                 talloc_free(tmp_ctx);
546                 return;
547         }
548
549         status = messaging_send(notify->messaging_ctx, e->server, 
550                                 MSG_PVFS_NOTIFY, &data);
551         talloc_free(tmp_ctx);
552 }
553
554
555 /*
556   trigger a notify message for anyone waiting on a matching event
557
558   This function is called a lot, and needs to be very fast. The unusual data structure
559   and traversal is designed to be fast in the average case, even for large numbers of
560   notifies
561 */
562 void notify_trigger(struct notify_context *notify,
563                     uint32_t action, uint32_t filter, const char *path)
564 {
565         NTSTATUS status;
566         int depth;
567         const char *p, *next_p;
568
569         /* see if change notify is enabled at all */
570         if (notify == NULL) {
571                 return;
572         }
573
574         status = notify_load(notify);
575         if (!NT_STATUS_IS_OK(status)) {
576                 return;
577         }
578
579         /* loop along the given path, working with each directory depth separately */
580         for (depth=0,p=path;
581              p && depth < notify->array->num_depths;
582              p=next_p,depth++) {
583                 int p_len = p - path;
584                 int min_i, max_i, i;
585                 struct notify_depth *d = &notify->array->depth[depth];
586                 next_p = strchr(p+1, '/');
587
588                 /* see if there are any entries at this depth */
589                 if (d->num_entries == 0) continue;
590                 
591                 /* try to skip based on the maximum mask. If next_p is
592                  NULL then we know it will be a 'this directory'
593                  match, otherwise it must be a subdir match */
594                 if (next_p != NULL) {
595                         if (0 == (filter & d->max_mask_subdir)) {
596                                 continue;
597                         }
598                 } else {
599                         if (0 == (filter & d->max_mask)) {
600                                 continue;
601                         }
602                 }
603
604                 /* we know there is an entry here worth looking
605                  for. Use a bisection search to find the first entry
606                  with a matching path */
607                 min_i = 0;
608                 max_i = d->num_entries-1;
609
610                 while (min_i < max_i) {
611                         struct notify_entry *e;
612                         i = (min_i+max_i)/2;
613                         e = &d->entries[i];
614                         int cmp = strncmp(path, e->path, p_len);
615                         if (cmp == 0) {
616                                 if (p_len == e->path_len) {
617                                         max_i = i;
618                                 } else {
619                                         max_i = i-1;
620                                 }
621                         } else if (cmp < 0) {
622                                 max_i = i-1;
623                         } else {
624                                 min_i = i+1;
625                         }
626                 }
627
628                 if (min_i != max_i) {
629                         /* none match */
630                         continue;
631                 }
632
633                 /* we now know that the entries start at min_i */
634                 for (i=min_i;i<d->num_entries;i++) {
635                         struct notify_entry *e = &d->entries[i];
636                         if (p_len != e->path_len ||
637                             strncmp(path, e->path, p_len) != 0) break;
638                         if (next_p != NULL) {
639                                 if (0 == (filter & e->subdir_filter)) {
640                                         continue;
641                                 }
642                         } else {
643                                 if (0 == (filter & e->filter)) {
644                                         continue;
645                                 }
646                         }
647                         notify_send(notify, e, path + e->path_len + 1, action);
648                 }
649         }
650 }