r14918: cleaner handling of systems without inotify
[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 };
55
56 #define NOTIFY_KEY "notify array"
57
58 static NTSTATUS notify_remove_all(struct notify_context *notify);
59 static void notify_handler(struct messaging_context *msg_ctx, void *private, 
60                            uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
61
62 /*
63   destroy the notify context
64 */
65 static int notify_destructor(void *p)
66 {
67         struct notify_context *notify = talloc_get_type(p, struct notify_context);
68         messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
69         notify_remove_all(notify);
70         return 0;
71 }
72
73 /*
74   Open up the notify.tdb database. You should close it down using
75   talloc_free(). We need the messaging_ctx to allow for notifications
76   via internal messages
77 */
78 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, uint32_t server, 
79                                    struct messaging_context *messaging_ctx,
80                                    struct event_context *ev)
81 {
82         char *path;
83         struct notify_context *notify;
84
85         notify = talloc(mem_ctx, struct notify_context);
86         if (notify == NULL) {
87                 return NULL;
88         }
89
90         path = smbd_tmp_path(notify, "notify.tdb");
91         notify->w = tdb_wrap_open(notify, path, 0,  
92                                   TDB_SEQNUM,
93                                   O_RDWR|O_CREAT, 0600);
94         talloc_free(path);
95         if (notify->w == NULL) {
96                 talloc_free(notify);
97                 return NULL;
98         }
99
100         notify->server = server;
101         notify->messaging_ctx = messaging_ctx;
102         notify->list = NULL;
103         notify->array = NULL;
104         notify->seqnum = tdb_get_seqnum(notify->w->tdb);
105
106         talloc_set_destructor(notify, notify_destructor);
107
108         /* register with the messaging subsystem for the notify
109            message type */
110         messaging_register(notify->messaging_ctx, notify, 
111                            MSG_PVFS_NOTIFY, notify_handler);
112
113         notify->sys_notify_ctx = sys_notify_init(-1, notify, ev);
114
115         return notify;
116 }
117
118
119 /*
120   lock the notify db
121 */
122 static NTSTATUS notify_lock(struct notify_context *notify)
123 {
124         if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
125                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
126         }
127         return NT_STATUS_OK;
128 }
129
130 /*
131   unlock the notify db
132 */
133 static void notify_unlock(struct notify_context *notify)
134 {
135         tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
136 }
137
138 /*
139   load the notify array
140 */
141 static NTSTATUS notify_load(struct notify_context *notify)
142 {
143         TDB_DATA dbuf;
144         DATA_BLOB blob;
145         NTSTATUS status;
146         int seqnum;
147
148         seqnum = tdb_get_seqnum(notify->w->tdb);
149
150         if (seqnum == notify->seqnum && notify->array != NULL) {
151                 return NT_STATUS_OK;
152         }
153
154         notify->seqnum = seqnum;
155
156         talloc_free(notify->array);
157         notify->array = talloc_zero(notify, struct notify_array);
158         NT_STATUS_HAVE_NO_MEMORY(notify->array);
159
160         dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
161         if (dbuf.dptr == NULL) {
162                 return NT_STATUS_OK;
163         }
164
165         blob.data = dbuf.dptr;
166         blob.length = dbuf.dsize;
167
168         status = ndr_pull_struct_blob(&blob, notify->array, notify->array, 
169                                       (ndr_pull_flags_fn_t)ndr_pull_notify_array);
170         free(dbuf.dptr);
171
172         return status;
173 }
174
175 /*
176   compare notify entries for sorting
177 */
178 static int notify_compare(const void *p1, const void *p2)
179 {
180         const struct notify_entry *e1 = p1, *e2 = p2;
181         return strcmp(e1->path, e2->path);
182 }
183
184 /*
185   save the notify array
186 */
187 static NTSTATUS notify_save(struct notify_context *notify)
188 {
189         TDB_DATA dbuf;
190         DATA_BLOB blob;
191         NTSTATUS status;
192         int ret;
193         TALLOC_CTX *tmp_ctx;
194
195         if (notify->array->num_entries == 0) {
196                 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
197                 if (ret != 0) {
198                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
199                 }
200                 return NT_STATUS_OK;
201         }
202
203         if (notify->array->num_entries > 1) {
204                 qsort(notify->array->entries, notify->array->num_entries, 
205                       sizeof(struct notify_entry), notify_compare);
206         }
207
208         tmp_ctx = talloc_new(notify);
209
210         status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array, 
211                                       (ndr_push_flags_fn_t)ndr_push_notify_array);
212         if (!NT_STATUS_IS_OK(status)) {
213                 talloc_free(tmp_ctx);
214                 return status;
215         }
216
217         dbuf.dptr = blob.data;
218         dbuf.dsize = blob.length;
219                 
220         ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
221         talloc_free(tmp_ctx);
222         if (ret != 0) {
223                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
224         }
225
226         return NT_STATUS_OK;
227 }
228
229
230 /*
231   handle incoming notify messages
232 */
233 static void notify_handler(struct messaging_context *msg_ctx, void *private, 
234                            uint32_t msg_type, uint32_t server_id, DATA_BLOB *data)
235 {
236         struct notify_context *notify = talloc_get_type(private, struct notify_context);
237         NTSTATUS status;
238         struct notify_event ev;
239         TALLOC_CTX *tmp_ctx = talloc_new(notify);
240         struct notify_list *listel;
241
242         status = ndr_pull_struct_blob(data, tmp_ctx, &ev, 
243                                       (ndr_pull_flags_fn_t)ndr_pull_notify_event);
244         if (!NT_STATUS_IS_OK(status)) {
245                 talloc_free(tmp_ctx);
246                 return;
247         }
248
249         for (listel=notify->list;listel;listel=listel->next) {
250                 if (listel->private == ev.private) {
251                         listel->callback(listel->private, &ev);
252                         break;
253                 }
254         }
255
256         talloc_free(tmp_ctx);   
257 }
258
259 /*
260   callback from sys_notify telling us about changes from the OS
261 */
262 static void sys_notify_callback(struct sys_notify_context *ctx, 
263                                 void *ptr, struct notify_event *ev)
264 {
265         struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
266         ev->private = listel;
267         listel->callback(listel->private, ev);
268 }
269
270 /*
271   add an entry to the notify array
272 */
273 static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
274                                  const char *path, void *private)
275 {
276         notify->array->entries[notify->array->num_entries] = *e;
277         notify->array->entries[notify->array->num_entries].private = private;
278         notify->array->entries[notify->array->num_entries].server = notify->server;
279         
280         if (path) {
281                 notify->array->entries[notify->array->num_entries].path = path;
282         }
283
284         notify->array->num_entries++;
285         
286         return notify_save(notify);
287 }
288
289 /*
290   add a notify watch. This is called when a notify is first setup on a open
291   directory handle.
292 */
293 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e,
294                     void (*callback)(void *, const struct notify_event *), 
295                     void *private)
296 {
297         NTSTATUS status;
298         struct notify_list *listel;
299         char *path = NULL;
300         size_t len;
301
302         status = notify_lock(notify);
303         NT_STATUS_NOT_OK_RETURN(status);
304
305         status = notify_load(notify);
306         if (!NT_STATUS_IS_OK(status)) {
307                 notify_unlock(notify);
308                 return status;
309         }
310
311         notify->array->entries = talloc_realloc(notify->array, notify->array->entries, 
312                                                 struct notify_entry,
313                                                 notify->array->num_entries+1);
314
315         if (notify->array->entries == NULL) {
316                 notify_unlock(notify);
317                 return NT_STATUS_NO_MEMORY;
318         }
319
320         /* cope with /. on the end of the path */
321         len = strlen(e->path);
322         if (len > 1 && e->path[len-1] == '.' && e->path[len-2] == '/') {
323                 path = talloc_strndup(notify, e->path, len-2);
324         }
325
326         listel = talloc_zero(notify, struct notify_list);
327         NT_STATUS_HAVE_NO_MEMORY(listel);
328
329         listel->private = private;
330         listel->callback = callback;
331         DLIST_ADD(notify->list, listel);
332
333         /* ignore failures from sys_notify */
334         if (notify->sys_notify_ctx != NULL) {
335                 status = sys_notify_watch(notify->sys_notify_ctx, e->path, e->filter, 
336                                           sys_notify_callback, listel, 
337                                           &listel->sys_notify_handle);
338                 if (NT_STATUS_IS_OK(status)) {
339                         /* if the kernel handler has said it can handle this notify then
340                            we don't need to add it to the array */
341                         talloc_steal(listel, listel->sys_notify_handle);
342                         goto done;
343                 }
344         }
345
346         status = notify_add_array(notify, e, path, private);
347
348 done:
349         notify_unlock(notify);
350         talloc_free(path);
351
352         return status;
353 }
354
355 /*
356   remove a notify watch. Called when the directory handle is closed
357 */
358 NTSTATUS notify_remove(struct notify_context *notify, void *private)
359 {
360         NTSTATUS status;
361         struct notify_list *listel;
362         int i;
363
364         for (listel=notify->list;listel;listel=listel->next) {
365                 if (listel->private == private) {
366                         DLIST_REMOVE(notify->list, listel);
367                         break;
368                 }
369         }
370         if (listel == NULL) {
371                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
372         }
373
374         talloc_free(listel);
375
376         status = notify_lock(notify);
377         NT_STATUS_NOT_OK_RETURN(status);
378
379         status = notify_load(notify);
380         if (!NT_STATUS_IS_OK(status)) {
381                 notify_unlock(notify);
382                 return status;
383         }
384
385         for (i=0;i<notify->array->num_entries;i++) {
386                 if (notify->server == notify->array->entries[i].server && 
387                     private == notify->array->entries[i].private) {
388                         break;
389                 }
390         }
391         if (i == notify->array->num_entries) {
392                 notify_unlock(notify);
393                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
394         }
395
396         if (i < notify->array->num_entries-1) {
397                 memmove(&notify->array->entries[i], &notify->array->entries[i+1], 
398                         sizeof(notify->array->entries[i])*(notify->array->num_entries-(i+1)));
399         }
400         notify->array->num_entries--;
401
402         status = notify_save(notify);
403
404         notify_unlock(notify);
405
406         return status;
407 }
408
409 /*
410   remove all notify watches for this messaging server
411 */
412 static NTSTATUS notify_remove_all(struct notify_context *notify)
413 {
414         NTSTATUS status;
415         int i;
416
417         if (notify->list == NULL) {
418                 return NT_STATUS_OK;
419         }
420
421         status = notify_lock(notify);
422         NT_STATUS_NOT_OK_RETURN(status);
423
424         status = notify_load(notify);
425         if (!NT_STATUS_IS_OK(status)) {
426                 notify_unlock(notify);
427                 return status;
428         }
429
430         for (i=0;i<notify->array->num_entries;i++) {
431                 if (notify->server == notify->array->entries[i].server) {
432                         if (i < notify->array->num_entries-1) {
433                                 memmove(&notify->array->entries[i], &notify->array->entries[i+1], 
434                                         sizeof(notify->array->entries[i])*(notify->array->num_entries-(i+1)));
435                         }
436                         i--;
437                         notify->array->num_entries--;
438                 }
439         }
440
441
442         status = notify_save(notify);
443
444         notify_unlock(notify);
445
446         return status;
447 }
448
449
450 /*
451   send a notify message to another messaging server
452 */
453 static void notify_send(struct notify_context *notify, struct notify_entry *e,
454                         const char *path, uint32_t action)
455 {
456         struct notify_event ev;
457         DATA_BLOB data;
458         NTSTATUS status;
459         TALLOC_CTX *tmp_ctx;
460
461         ev.action = action;
462         ev.path = path;
463         ev.private = e->private;
464
465         tmp_ctx = talloc_new(notify);
466
467         status = ndr_push_struct_blob(&data, tmp_ctx, &ev, 
468                                       (ndr_push_flags_fn_t)ndr_push_notify_event);
469         if (!NT_STATUS_IS_OK(status)) {
470                 talloc_free(tmp_ctx);
471                 return;
472         }
473
474         status = messaging_send(notify->messaging_ctx, e->server, 
475                                 MSG_PVFS_NOTIFY, &data);
476         talloc_free(tmp_ctx);
477 }
478
479 /*
480   see if a notify event matches
481 */
482 static BOOL notify_match(struct notify_context *notify, struct notify_entry *e,
483                          const char *path, uint32_t filter)
484 {
485         size_t len;
486
487         if (!(filter & e->filter)) {
488                 return False;
489         }
490
491         len = strlen(e->path);
492
493         if (strncmp(path, e->path, len) != 0) {
494                 return False;
495         }
496
497         if (path[len] != '/') {
498                 return False;
499         }
500
501         if (!e->recursive) {
502                 if (strchr(&path[len+1], '/') != NULL) {
503                         return False;
504                 }
505         }
506
507         return True;
508 }
509
510
511 /*
512   trigger a notify message for anyone waiting on a matching event
513 */
514 void notify_trigger(struct notify_context *notify,
515                     uint32_t action, uint32_t filter, const char *path)
516 {
517         NTSTATUS status;
518         int i;
519
520         status = notify_load(notify);
521         if (!NT_STATUS_IS_OK(status)) {
522                 return;
523         }
524
525         /* TODO: this needs to be changed to a log(n) search */
526         for (i=0;i<notify->array->num_entries;i++) {
527                 if (notify_match(notify, &notify->array->entries[i], path, filter)) {
528                         notify_send(notify, &notify->array->entries[i], 
529                                     path + strlen(notify->array->entries[i].path) + 1, 
530                                     action);
531                 }
532         }
533 }