2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
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.
27 #include "system/filesys.h"
28 #include "librpc/gen_ndr/ndr_notify.h"
30 #include "smbd/smbd.h"
32 #include "lib/util/tdb_wrap.h"
35 struct notify_context
{
36 struct db_context
*db_recursive
;
37 struct db_context
*db_onelevel
;
38 struct server_id server
;
39 struct messaging_context
*messaging_ctx
;
40 struct notify_list
*list
;
41 struct notify_array
*array
;
43 struct sys_notify_context
*sys_notify_ctx
;
49 struct notify_list
*next
, *prev
;
51 void (*callback
)(void *, const struct notify_event
*);
52 void *sys_notify_handle
;
56 #define NOTIFY_KEY "notify array"
58 #define NOTIFY_ENABLE "notify:enable"
59 #define NOTIFY_ENABLE_DEFAULT True
61 static NTSTATUS
notify_remove_all(struct notify_context
*notify
,
62 const struct server_id
*server
);
63 static void notify_handler(struct messaging_context
*msg_ctx
, void *private_data
,
64 uint32_t msg_type
, struct server_id server_id
, DATA_BLOB
*data
);
67 destroy the notify context
69 static int notify_destructor(struct notify_context
*notify
)
71 messaging_deregister(notify
->messaging_ctx
, MSG_PVFS_NOTIFY
, notify
);
73 if (notify
->list
!= NULL
) {
74 notify_remove_all(notify
, ¬ify
->server
);
81 Open up the notify.tdb database. You should close it down using
82 talloc_free(). We need the messaging_ctx to allow for notifications
85 struct notify_context
*notify_init(TALLOC_CTX
*mem_ctx
, struct server_id server
,
86 struct messaging_context
*messaging_ctx
,
87 struct event_context
*ev
,
88 connection_struct
*conn
)
90 struct notify_context
*notify
;
92 if (!lp_change_notify(conn
->params
)) {
96 notify
= talloc(mem_ctx
, struct notify_context
);
101 notify
->db_recursive
= db_open(notify
, lock_path("notify.tdb"),
102 0, TDB_SEQNUM
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
103 O_RDWR
|O_CREAT
, 0644);
104 if (notify
->db_recursive
== NULL
) {
109 notify
->db_onelevel
= db_open(notify
, lock_path("notify_onelevel.tdb"),
110 0, TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
111 O_RDWR
|O_CREAT
, 0644);
112 if (notify
->db_onelevel
== NULL
) {
117 notify
->server
= server
;
118 notify
->messaging_ctx
= messaging_ctx
;
120 notify
->array
= NULL
;
121 notify
->seqnum
= notify
->db_recursive
->get_seqnum(
122 notify
->db_recursive
);
123 notify
->key
= string_term_tdb_data(NOTIFY_KEY
);
125 talloc_set_destructor(notify
, notify_destructor
);
127 /* register with the messaging subsystem for the notify
129 messaging_register(notify
->messaging_ctx
, notify
,
130 MSG_PVFS_NOTIFY
, notify_handler
);
132 notify
->sys_notify_ctx
= sys_notify_context_create(conn
, notify
, ev
);
137 bool notify_internal_parent_init(TALLOC_CTX
*mem_ctx
)
139 struct tdb_wrap
*db1
, *db2
;
141 if (lp_clustering()) {
146 * Open the tdbs in the parent process (smbd) so that our
147 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
151 db1
= tdb_wrap_open(mem_ctx
, lock_path("notify.tdb"),
152 0, TDB_SEQNUM
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
153 O_RDWR
|O_CREAT
, 0644);
155 DEBUG(1, ("could not open notify.tdb: %s\n", strerror(errno
)));
158 db2
= tdb_wrap_open(mem_ctx
, lock_path("notify_onelevel.tdb"),
159 0, TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
, 0644);
161 DEBUG(1, ("could not open notify_onelevel.tdb: %s\n",
170 lock and fetch the record
172 static NTSTATUS
notify_fetch_locked(struct notify_context
*notify
, struct db_record
**rec
)
174 *rec
= notify
->db_recursive
->fetch_locked(notify
->db_recursive
,
175 notify
, notify
->key
);
177 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
183 load the notify array
185 static NTSTATUS
notify_load(struct notify_context
*notify
, struct db_record
*rec
)
192 seqnum
= notify
->db_recursive
->get_seqnum(notify
->db_recursive
);
194 if (seqnum
== notify
->seqnum
&& notify
->array
!= NULL
) {
198 notify
->seqnum
= seqnum
;
200 talloc_free(notify
->array
);
201 notify
->array
= TALLOC_ZERO_P(notify
, struct notify_array
);
202 NT_STATUS_HAVE_NO_MEMORY(notify
->array
);
205 if (notify
->db_recursive
->fetch(notify
->db_recursive
, notify
,
206 notify
->key
, &dbuf
) != 0) {
207 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
213 blob
.data
= (uint8
*)dbuf
.dptr
;
214 blob
.length
= dbuf
.dsize
;
216 status
= NT_STATUS_OK
;
217 if (blob
.length
> 0) {
218 enum ndr_err_code ndr_err
;
219 ndr_err
= ndr_pull_struct_blob(&blob
, notify
->array
, notify
->array
,
220 (ndr_pull_flags_fn_t
)ndr_pull_notify_array
);
221 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
222 /* 1. log that we got a corrupt notify_array
223 * 2. clear the variable the garbage was stored into to not trip
224 * over it next time this method is entered with the same seqnum
225 * 3. delete it from the database */
226 DEBUG(2, ("notify_array is corrupt, discarding it\n"));
228 ZERO_STRUCTP(notify
->array
);
230 rec
->delete_rec(rec
);
234 if (DEBUGLEVEL
>= 10) {
235 DEBUG(10, ("notify_load:\n"));
236 NDR_PRINT_DEBUG(notify_array
, notify
->array
);
243 talloc_free(dbuf
.dptr
);
250 compare notify entries for sorting
252 static int notify_compare(const struct notify_entry
*e1
, const struct notify_entry
*e2
)
254 return strcmp(e1
->path
, e2
->path
);
258 save the notify array
260 static NTSTATUS
notify_save(struct notify_context
*notify
, struct db_record
*rec
)
265 enum ndr_err_code ndr_err
;
268 /* if possible, remove some depth arrays */
269 while (notify
->array
->num_depths
> 0 &&
270 notify
->array
->depth
[notify
->array
->num_depths
-1].num_entries
== 0) {
271 notify
->array
->num_depths
--;
274 /* we might just be able to delete the record */
275 if (notify
->array
->num_depths
== 0) {
276 return rec
->delete_rec(rec
);
279 tmp_ctx
= talloc_new(notify
);
280 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx
);
282 ndr_err
= ndr_push_struct_blob(&blob
, tmp_ctx
, notify
->array
,
283 (ndr_push_flags_fn_t
)ndr_push_notify_array
);
284 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
285 talloc_free(tmp_ctx
);
286 return ndr_map_error2ntstatus(ndr_err
);
289 if (DEBUGLEVEL
>= 10) {
290 DEBUG(10, ("notify_save:\n"));
291 NDR_PRINT_DEBUG(notify_array
, notify
->array
);
294 dbuf
.dptr
= blob
.data
;
295 dbuf
.dsize
= blob
.length
;
297 status
= rec
->store(rec
, dbuf
, TDB_REPLACE
);
298 talloc_free(tmp_ctx
);
305 handle incoming notify messages
307 static void notify_handler(struct messaging_context
*msg_ctx
, void *private_data
,
308 uint32_t msg_type
, struct server_id server_id
, DATA_BLOB
*data
)
310 struct notify_context
*notify
= talloc_get_type(private_data
, struct notify_context
);
311 enum ndr_err_code ndr_err
;
312 struct notify_event ev
;
313 TALLOC_CTX
*tmp_ctx
= talloc_new(notify
);
314 struct notify_list
*listel
;
316 if (tmp_ctx
== NULL
) {
320 ndr_err
= ndr_pull_struct_blob(data
, tmp_ctx
, &ev
,
321 (ndr_pull_flags_fn_t
)ndr_pull_notify_event
);
322 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
323 talloc_free(tmp_ctx
);
327 for (listel
=notify
->list
;listel
;listel
=listel
->next
) {
328 if (listel
->private_data
== ev
.private_data
) {
329 listel
->callback(listel
->private_data
, &ev
);
334 talloc_free(tmp_ctx
);
338 callback from sys_notify telling us about changes from the OS
340 static void sys_notify_callback(struct sys_notify_context
*ctx
,
341 void *ptr
, struct notify_event
*ev
)
343 struct notify_list
*listel
= talloc_get_type(ptr
, struct notify_list
);
344 ev
->private_data
= listel
;
345 DEBUG(10, ("sys_notify_callback called with action=%d, for %s\n",
346 ev
->action
, ev
->path
));
347 listel
->callback(listel
->private_data
, ev
);
351 add an entry to the notify array
353 static NTSTATUS
notify_add_array(struct notify_context
*notify
, struct db_record
*rec
,
354 struct notify_entry
*e
,
355 void *private_data
, int depth
)
358 struct notify_depth
*d
;
359 struct notify_entry
*ee
;
361 /* possibly expand the depths array */
362 if (depth
>= notify
->array
->num_depths
) {
363 d
= talloc_realloc(notify
->array
, notify
->array
->depth
,
364 struct notify_depth
, depth
+1);
365 NT_STATUS_HAVE_NO_MEMORY(d
);
366 for (i
=notify
->array
->num_depths
;i
<=depth
;i
++) {
369 notify
->array
->depth
= d
;
370 notify
->array
->num_depths
= depth
+1;
372 d
= ¬ify
->array
->depth
[depth
];
374 /* expand the entries array */
375 ee
= talloc_realloc(notify
->array
->depth
, d
->entries
, struct notify_entry
,
377 NT_STATUS_HAVE_NO_MEMORY(ee
);
380 d
->entries
[d
->num_entries
] = *e
;
381 d
->entries
[d
->num_entries
].private_data
= private_data
;
382 d
->entries
[d
->num_entries
].server
= notify
->server
;
383 d
->entries
[d
->num_entries
].path_len
= strlen(e
->path
);
386 d
->max_mask
|= e
->filter
;
387 d
->max_mask_subdir
|= e
->subdir_filter
;
389 TYPESAFE_QSORT(d
->entries
, d
->num_entries
, notify_compare
);
391 /* recalculate the maximum masks */
393 d
->max_mask_subdir
= 0;
395 for (i
=0;i
<d
->num_entries
;i
++) {
396 d
->max_mask
|= d
->entries
[i
].filter
;
397 d
->max_mask_subdir
|= d
->entries
[i
].subdir_filter
;
400 return notify_save(notify
, rec
);
404 Add a non-recursive watch
407 static void notify_add_onelevel(struct notify_context
*notify
,
408 struct notify_entry
*e
, void *private_data
)
410 struct notify_entry_array
*array
;
411 struct db_record
*rec
;
414 enum ndr_err_code ndr_err
;
417 array
= talloc_zero(talloc_tos(), struct notify_entry_array
);
422 rec
= notify
->db_onelevel
->fetch_locked(
423 notify
->db_onelevel
, talloc_tos(),
424 make_tdb_data((uint8_t *)&e
->dir_id
, sizeof(e
->dir_id
)));
426 DEBUG(10, ("notify_add_onelevel: fetch_locked for %s failed"
427 "\n", file_id_string_tos(&e
->dir_id
)));
432 blob
.data
= (uint8_t *)rec
->value
.dptr
;
433 blob
.length
= rec
->value
.dsize
;
435 if (blob
.length
> 0) {
436 ndr_err
= ndr_pull_struct_blob(&blob
, array
, array
,
437 (ndr_pull_flags_fn_t
)ndr_pull_notify_entry_array
);
438 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
439 DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
440 ndr_errstr(ndr_err
)));
444 if (DEBUGLEVEL
>= 10) {
445 DEBUG(10, ("notify_add_onelevel:\n"));
446 NDR_PRINT_DEBUG(notify_entry_array
, array
);
450 array
->entries
= talloc_realloc(array
, array
->entries
,
452 array
->num_entries
+1);
453 if (array
->entries
== NULL
) {
457 array
->entries
[array
->num_entries
] = *e
;
458 array
->entries
[array
->num_entries
].private_data
= private_data
;
459 array
->entries
[array
->num_entries
].server
= notify
->server
;
460 array
->num_entries
+= 1;
462 ndr_err
= ndr_push_struct_blob(&blob
, rec
, array
,
463 (ndr_push_flags_fn_t
)ndr_push_notify_entry_array
);
464 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
465 DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
466 ndr_errstr(ndr_err
)));
471 if (DEBUGLEVEL
>= 10) {
472 DEBUG(10, ("notify_add_onelevel:\n"));
473 NDR_PRINT_DEBUG(notify_entry_array
, array
);
476 dbuf
.dptr
= blob
.data
;
477 dbuf
.dsize
= blob
.length
;
479 status
= rec
->store(rec
, dbuf
, TDB_REPLACE
);
481 if (!NT_STATUS_IS_OK(status
)) {
482 DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
492 add a notify watch. This is called when a notify is first setup on a open
495 NTSTATUS
notify_add(struct notify_context
*notify
, struct notify_entry
*e0
,
496 void (*callback
)(void *, const struct notify_event
*),
499 struct notify_entry e
= *e0
;
501 char *tmp_path
= NULL
;
502 struct notify_list
*listel
;
505 struct db_record
*rec
;
507 /* see if change notify is enabled at all */
508 if (notify
== NULL
) {
509 return NT_STATUS_NOT_IMPLEMENTED
;
512 status
= notify_fetch_locked(notify
, &rec
);
513 NT_STATUS_NOT_OK_RETURN(status
);
515 status
= notify_load(notify
, rec
);
516 if (!NT_STATUS_IS_OK(status
)) {
521 /* cope with /. on the end of the path */
522 len
= strlen(e
.path
);
523 if (len
> 1 && e
.path
[len
-1] == '.' && e
.path
[len
-2] == '/') {
524 tmp_path
= talloc_strndup(notify
, e
.path
, len
-2);
525 if (tmp_path
== NULL
) {
526 status
= NT_STATUS_NO_MEMORY
;
532 depth
= count_chars(e
.path
, '/');
534 listel
= TALLOC_ZERO_P(notify
, struct notify_list
);
535 if (listel
== NULL
) {
536 status
= NT_STATUS_NO_MEMORY
;
540 listel
->private_data
= private_data
;
541 listel
->callback
= callback
;
542 listel
->depth
= depth
;
543 DLIST_ADD(notify
->list
, listel
);
545 /* ignore failures from sys_notify */
546 if (notify
->sys_notify_ctx
!= NULL
) {
548 this call will modify e.filter and e.subdir_filter
549 to remove bits handled by the backend
551 status
= sys_notify_watch(notify
->sys_notify_ctx
, &e
,
552 sys_notify_callback
, listel
,
553 &listel
->sys_notify_handle
);
554 if (NT_STATUS_IS_OK(status
)) {
555 talloc_steal(listel
, listel
->sys_notify_handle
);
560 notify_add_onelevel(notify
, &e
, private_data
);
561 status
= NT_STATUS_OK
;
564 /* if the system notify handler couldn't handle some of the
565 filter bits, or couldn't handle a request for recursion
566 then we need to install it in the array used for the
567 intra-samba notify handling */
568 if (e
.filter
!= 0 || e
.subdir_filter
!= 0) {
569 status
= notify_add_array(notify
, rec
, &e
, private_data
, depth
);
574 talloc_free(tmp_path
);
579 NTSTATUS
notify_remove_onelevel(struct notify_context
*notify
,
580 const struct file_id
*fid
,
583 struct notify_entry_array
*array
;
584 struct db_record
*rec
;
587 enum ndr_err_code ndr_err
;
591 if (notify
== NULL
) {
592 return NT_STATUS_NOT_IMPLEMENTED
;
595 array
= talloc_zero(talloc_tos(), struct notify_entry_array
);
597 return NT_STATUS_NO_MEMORY
;
600 rec
= notify
->db_onelevel
->fetch_locked(
601 notify
->db_onelevel
, array
,
602 make_tdb_data((const uint8_t *)fid
, sizeof(*fid
)));
604 DEBUG(10, ("notify_remove_onelevel: fetch_locked for %s failed"
605 "\n", file_id_string_tos(fid
)));
607 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
610 blob
.data
= (uint8_t *)rec
->value
.dptr
;
611 blob
.length
= rec
->value
.dsize
;
613 if (blob
.length
> 0) {
614 ndr_err
= ndr_pull_struct_blob(&blob
, array
, array
,
615 (ndr_pull_flags_fn_t
)ndr_pull_notify_entry_array
);
616 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
617 DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
618 ndr_errstr(ndr_err
)));
620 return ndr_map_error2ntstatus(ndr_err
);
622 if (DEBUGLEVEL
>= 10) {
623 DEBUG(10, ("notify_remove_onelevel:\n"));
624 NDR_PRINT_DEBUG(notify_entry_array
, array
);
628 for (i
=0; i
<array
->num_entries
; i
++) {
629 if ((private_data
== array
->entries
[i
].private_data
) &&
630 cluster_id_equal(¬ify
->server
,
631 &array
->entries
[i
].server
)) {
636 if (i
== array
->num_entries
) {
638 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
641 array
->entries
[i
] = array
->entries
[array
->num_entries
-1];
642 array
->num_entries
-= 1;
644 if (array
->num_entries
== 0) {
645 rec
->delete_rec(rec
);
650 ndr_err
= ndr_push_struct_blob(&blob
, rec
, array
,
651 (ndr_push_flags_fn_t
)ndr_push_notify_entry_array
);
652 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
653 DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
654 ndr_errstr(ndr_err
)));
656 return ndr_map_error2ntstatus(ndr_err
);
659 if (DEBUGLEVEL
>= 10) {
660 DEBUG(10, ("notify_add_onelevel:\n"));
661 NDR_PRINT_DEBUG(notify_entry_array
, array
);
664 dbuf
.dptr
= blob
.data
;
665 dbuf
.dsize
= blob
.length
;
667 status
= rec
->store(rec
, dbuf
, TDB_REPLACE
);
669 if (!NT_STATUS_IS_OK(status
)) {
670 DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
678 remove a notify watch. Called when the directory handle is closed
680 NTSTATUS
notify_remove(struct notify_context
*notify
, void *private_data
)
683 struct notify_list
*listel
;
685 struct notify_depth
*d
;
686 struct db_record
*rec
;
688 /* see if change notify is enabled at all */
689 if (notify
== NULL
) {
690 return NT_STATUS_NOT_IMPLEMENTED
;
693 for (listel
=notify
->list
;listel
;listel
=listel
->next
) {
694 if (listel
->private_data
== private_data
) {
695 DLIST_REMOVE(notify
->list
, listel
);
699 if (listel
== NULL
) {
700 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
703 depth
= listel
->depth
;
707 status
= notify_fetch_locked(notify
, &rec
);
708 NT_STATUS_NOT_OK_RETURN(status
);
710 status
= notify_load(notify
, rec
);
711 if (!NT_STATUS_IS_OK(status
)) {
716 if (depth
>= notify
->array
->num_depths
) {
718 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
721 /* we only have to search at the depth of this element */
722 d
= ¬ify
->array
->depth
[depth
];
724 for (i
=0;i
<d
->num_entries
;i
++) {
725 if (private_data
== d
->entries
[i
].private_data
&&
726 cluster_id_equal(¬ify
->server
, &d
->entries
[i
].server
)) {
730 if (i
== d
->num_entries
) {
732 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
735 if (i
< d
->num_entries
-1) {
736 memmove(&d
->entries
[i
], &d
->entries
[i
+1],
737 sizeof(d
->entries
[i
])*(d
->num_entries
-(i
+1)));
741 status
= notify_save(notify
, rec
);
749 remove all notify watches for a messaging server
751 static NTSTATUS
notify_remove_all(struct notify_context
*notify
,
752 const struct server_id
*server
)
755 int i
, depth
, del_count
=0;
756 struct db_record
*rec
;
758 status
= notify_fetch_locked(notify
, &rec
);
759 NT_STATUS_NOT_OK_RETURN(status
);
761 status
= notify_load(notify
, rec
);
762 if (!NT_STATUS_IS_OK(status
)) {
767 /* we have to search for all entries across all depths, looking for matches
769 for (depth
=0;depth
<notify
->array
->num_depths
;depth
++) {
770 struct notify_depth
*d
= ¬ify
->array
->depth
[depth
];
771 for (i
=0;i
<d
->num_entries
;i
++) {
772 if (cluster_id_equal(server
, &d
->entries
[i
].server
)) {
773 if (i
< d
->num_entries
-1) {
774 memmove(&d
->entries
[i
], &d
->entries
[i
+1],
775 sizeof(d
->entries
[i
])*(d
->num_entries
-(i
+1)));
785 status
= notify_save(notify
, rec
);
795 send a notify message to another messaging server
797 static NTSTATUS
notify_send(struct notify_context
*notify
, struct notify_entry
*e
,
798 const char *path
, uint32_t action
)
800 struct notify_event ev
;
803 enum ndr_err_code ndr_err
;
808 ev
.private_data
= e
->private_data
;
810 tmp_ctx
= talloc_new(notify
);
812 ndr_err
= ndr_push_struct_blob(&data
, tmp_ctx
, &ev
,
813 (ndr_push_flags_fn_t
)ndr_push_notify_event
);
814 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
815 talloc_free(tmp_ctx
);
816 return ndr_map_error2ntstatus(ndr_err
);
819 status
= messaging_send(notify
->messaging_ctx
, e
->server
,
820 MSG_PVFS_NOTIFY
, &data
);
821 talloc_free(tmp_ctx
);
825 void notify_onelevel(struct notify_context
*notify
, uint32_t action
,
826 uint32_t filter
, struct file_id fid
, const char *name
)
828 struct notify_entry_array
*array
;
831 bool have_dead_entries
= false;
834 if (notify
== NULL
) {
838 array
= talloc_zero(talloc_tos(), struct notify_entry_array
);
843 if (notify
->db_onelevel
->fetch(
844 notify
->db_onelevel
, array
,
845 make_tdb_data((uint8_t *)&fid
, sizeof(fid
)),
851 blob
.data
= (uint8
*)dbuf
.dptr
;
852 blob
.length
= dbuf
.dsize
;
854 if (blob
.length
> 0) {
855 enum ndr_err_code ndr_err
;
856 ndr_err
= ndr_pull_struct_blob(&blob
, array
, array
,
857 (ndr_pull_flags_fn_t
)ndr_pull_notify_entry_array
);
858 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
859 DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
860 ndr_errstr(ndr_err
)));
864 if (DEBUGLEVEL
>= 10) {
865 DEBUG(10, ("notify_onelevel:\n"));
866 NDR_PRINT_DEBUG(notify_entry_array
, array
);
870 for (i
=0; i
<array
->num_entries
; i
++) {
871 struct notify_entry
*e
= &array
->entries
[i
];
873 if ((e
->filter
& filter
) != 0) {
876 status
= notify_send(notify
, e
, name
, action
);
878 status
, NT_STATUS_INVALID_HANDLE
)) {
880 * Mark the entry as dead. All entries have a
881 * path set. The marker used here is setting
885 have_dead_entries
= true;
890 if (!have_dead_entries
) {
895 for (i
=0; i
<array
->num_entries
; i
++) {
896 struct notify_entry
*e
= &array
->entries
[i
];
897 if (e
->path
!= NULL
) {
900 DEBUG(10, ("Deleting notify entries for process %s because "
901 "it's gone\n", procid_str_static(&e
->server
)));
903 * Potential TODO: This might need optimizing,
904 * notify_remove_onelevel() does a fetch_locked() operation at
905 * every call. But this would only matter if a process with
906 * MANY notifies has died without shutting down properly.
908 notify_remove_onelevel(notify
, &e
->dir_id
, e
->private_data
);
916 trigger a notify message for anyone waiting on a matching event
918 This function is called a lot, and needs to be very fast. The unusual data structure
919 and traversal is designed to be fast in the average case, even for large numbers of
922 void notify_trigger(struct notify_context
*notify
,
923 uint32_t action
, uint32_t filter
, const char *path
)
927 const char *p
, *next_p
;
929 DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
930 "path=%s\n", (unsigned)action
, (unsigned)filter
, path
));
932 /* see if change notify is enabled at all */
933 if (notify
== NULL
) {
938 status
= notify_load(notify
, NULL
);
939 if (!NT_STATUS_IS_OK(status
)) {
943 /* loop along the given path, working with each directory depth separately */
945 p
&& depth
< notify
->array
->num_depths
;
947 int p_len
= p
- path
;
949 struct notify_depth
*d
= ¬ify
->array
->depth
[depth
];
950 next_p
= strchr(p
+1, '/');
952 /* see if there are any entries at this depth */
953 if (d
->num_entries
== 0) continue;
955 /* try to skip based on the maximum mask. If next_p is
956 NULL then we know it will be a 'this directory'
957 match, otherwise it must be a subdir match */
958 if (next_p
!= NULL
) {
959 if (0 == (filter
& d
->max_mask_subdir
)) {
963 if (0 == (filter
& d
->max_mask
)) {
968 /* we know there is an entry here worth looking
969 for. Use a bisection search to find the first entry
970 with a matching path */
972 max_i
= d
->num_entries
-1;
974 while (min_i
< max_i
) {
975 struct notify_entry
*e
;
979 cmp
= strncmp(path
, e
->path
, p_len
);
981 if (p_len
== e
->path_len
) {
986 } else if (cmp
< 0) {
993 if (min_i
!= max_i
) {
998 /* we now know that the entries start at min_i */
999 for (i
=min_i
;i
<d
->num_entries
;i
++) {
1000 struct notify_entry
*e
= &d
->entries
[i
];
1001 if (p_len
!= e
->path_len
||
1002 strncmp(path
, e
->path
, p_len
) != 0) break;
1003 if (next_p
!= NULL
) {
1004 if (0 == (filter
& e
->subdir_filter
)) {
1008 if (0 == (filter
& e
->filter
)) {
1012 status
= notify_send(notify
, e
, path
+ e
->path_len
+ 1,
1015 if (NT_STATUS_EQUAL(
1016 status
, NT_STATUS_INVALID_HANDLE
)) {
1017 struct server_id server
= e
->server
;
1019 DEBUG(10, ("Deleting notify entries for "
1020 "process %s because it's gone\n",
1021 procid_str_static(&e
->server
)));
1022 notify_remove_all(notify
, &server
);