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