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