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 "librpc/gen_ndr/ndr_notify.h"
|
---|
28 |
|
---|
29 | struct notify_context {
|
---|
30 | struct db_context *db_recursive;
|
---|
31 | struct db_context *db_onelevel;
|
---|
32 | struct server_id server;
|
---|
33 | struct messaging_context *messaging_ctx;
|
---|
34 | struct notify_list *list;
|
---|
35 | struct notify_array *array;
|
---|
36 | int seqnum;
|
---|
37 | struct sys_notify_context *sys_notify_ctx;
|
---|
38 | TDB_DATA key;
|
---|
39 | };
|
---|
40 |
|
---|
41 |
|
---|
42 | struct notify_list {
|
---|
43 | struct notify_list *next, *prev;
|
---|
44 | void *private_data;
|
---|
45 | void (*callback)(void *, const struct notify_event *);
|
---|
46 | void *sys_notify_handle;
|
---|
47 | int depth;
|
---|
48 | };
|
---|
49 |
|
---|
50 | #define NOTIFY_KEY "notify array"
|
---|
51 |
|
---|
52 | #define NOTIFY_ENABLE "notify:enable"
|
---|
53 | #define NOTIFY_ENABLE_DEFAULT True
|
---|
54 |
|
---|
55 | static NTSTATUS notify_remove_all(struct notify_context *notify,
|
---|
56 | const struct server_id *server);
|
---|
57 | static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
|
---|
58 | uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
|
---|
59 |
|
---|
60 | /*
|
---|
61 | destroy the notify context
|
---|
62 | */
|
---|
63 | static int notify_destructor(struct notify_context *notify)
|
---|
64 | {
|
---|
65 | messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
|
---|
66 |
|
---|
67 | if (notify->list != NULL) {
|
---|
68 | notify_remove_all(notify, ¬ify->server);
|
---|
69 | }
|
---|
70 |
|
---|
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, struct server_id server,
|
---|
80 | struct messaging_context *messaging_ctx,
|
---|
81 | struct event_context *ev,
|
---|
82 | connection_struct *conn)
|
---|
83 | {
|
---|
84 | struct notify_context *notify;
|
---|
85 |
|
---|
86 | if (!lp_change_notify(conn->params)) {
|
---|
87 | return NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | notify = talloc(mem_ctx, struct notify_context);
|
---|
91 | if (notify == NULL) {
|
---|
92 | return NULL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | notify->db_recursive = db_open(notify, lock_path("notify.tdb"),
|
---|
96 | 0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST,
|
---|
97 | O_RDWR|O_CREAT, 0644);
|
---|
98 | if (notify->db_recursive == NULL) {
|
---|
99 | talloc_free(notify);
|
---|
100 | return NULL;
|
---|
101 | }
|
---|
102 |
|
---|
103 | notify->db_onelevel = db_open(notify, lock_path("notify_onelevel.tdb"),
|
---|
104 | 0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST,
|
---|
105 | O_RDWR|O_CREAT, 0644);
|
---|
106 | if (notify->db_onelevel == NULL) {
|
---|
107 | talloc_free(notify);
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | notify->server = server;
|
---|
112 | notify->messaging_ctx = messaging_ctx;
|
---|
113 | notify->list = NULL;
|
---|
114 | notify->array = NULL;
|
---|
115 | notify->seqnum = notify->db_recursive->get_seqnum(
|
---|
116 | notify->db_recursive);
|
---|
117 | notify->key = string_term_tdb_data(NOTIFY_KEY);
|
---|
118 |
|
---|
119 | talloc_set_destructor(notify, notify_destructor);
|
---|
120 |
|
---|
121 | /* register with the messaging subsystem for the notify
|
---|
122 | message type */
|
---|
123 | messaging_register(notify->messaging_ctx, notify,
|
---|
124 | MSG_PVFS_NOTIFY, notify_handler);
|
---|
125 |
|
---|
126 | notify->sys_notify_ctx = sys_notify_context_create(conn, notify, ev);
|
---|
127 |
|
---|
128 | return notify;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*
|
---|
132 | lock and fetch the record
|
---|
133 | */
|
---|
134 | static NTSTATUS notify_fetch_locked(struct notify_context *notify, struct db_record **rec)
|
---|
135 | {
|
---|
136 | *rec = notify->db_recursive->fetch_locked(notify->db_recursive,
|
---|
137 | notify, notify->key);
|
---|
138 | if (*rec == NULL) {
|
---|
139 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
140 | }
|
---|
141 | return NT_STATUS_OK;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*
|
---|
145 | load the notify array
|
---|
146 | */
|
---|
147 | static NTSTATUS notify_load(struct notify_context *notify, struct db_record *rec)
|
---|
148 | {
|
---|
149 | TDB_DATA dbuf;
|
---|
150 | DATA_BLOB blob;
|
---|
151 | NTSTATUS status;
|
---|
152 | int seqnum;
|
---|
153 |
|
---|
154 | seqnum = notify->db_recursive->get_seqnum(notify->db_recursive);
|
---|
155 |
|
---|
156 | if (seqnum == notify->seqnum && notify->array != NULL) {
|
---|
157 | return NT_STATUS_OK;
|
---|
158 | }
|
---|
159 |
|
---|
160 | notify->seqnum = seqnum;
|
---|
161 |
|
---|
162 | talloc_free(notify->array);
|
---|
163 | notify->array = TALLOC_ZERO_P(notify, struct notify_array);
|
---|
164 | NT_STATUS_HAVE_NO_MEMORY(notify->array);
|
---|
165 |
|
---|
166 | if (!rec) {
|
---|
167 | if (notify->db_recursive->fetch(notify->db_recursive, notify,
|
---|
168 | notify->key, &dbuf) != 0) {
|
---|
169 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
170 | }
|
---|
171 | } else {
|
---|
172 | dbuf = rec->value;
|
---|
173 | }
|
---|
174 |
|
---|
175 | blob.data = (uint8 *)dbuf.dptr;
|
---|
176 | blob.length = dbuf.dsize;
|
---|
177 |
|
---|
178 | status = NT_STATUS_OK;
|
---|
179 | if (blob.length > 0) {
|
---|
180 | enum ndr_err_code ndr_err;
|
---|
181 | ndr_err = ndr_pull_struct_blob(&blob, notify->array, NULL, notify->array,
|
---|
182 | (ndr_pull_flags_fn_t)ndr_pull_notify_array);
|
---|
183 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
184 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (DEBUGLEVEL >= 10) {
|
---|
189 | DEBUG(10, ("notify_load:\n"));
|
---|
190 | NDR_PRINT_DEBUG(notify_array, notify->array);
|
---|
191 | }
|
---|
192 |
|
---|
193 | if (!rec) {
|
---|
194 | talloc_free(dbuf.dptr);
|
---|
195 | }
|
---|
196 |
|
---|
197 | return status;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | compare notify entries for sorting
|
---|
202 | */
|
---|
203 | static int notify_compare(const void *p1, const void *p2)
|
---|
204 | {
|
---|
205 | const struct notify_entry *e1 = (const struct notify_entry *)p1;
|
---|
206 | const struct notify_entry *e2 = (const struct notify_entry *)p2;
|
---|
207 | return strcmp(e1->path, e2->path);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*
|
---|
211 | save the notify array
|
---|
212 | */
|
---|
213 | static NTSTATUS notify_save(struct notify_context *notify, struct db_record *rec)
|
---|
214 | {
|
---|
215 | TDB_DATA dbuf;
|
---|
216 | DATA_BLOB blob;
|
---|
217 | NTSTATUS status;
|
---|
218 | enum ndr_err_code ndr_err;
|
---|
219 | TALLOC_CTX *tmp_ctx;
|
---|
220 |
|
---|
221 | /* if possible, remove some depth arrays */
|
---|
222 | while (notify->array->num_depths > 0 &&
|
---|
223 | notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
|
---|
224 | notify->array->num_depths--;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* we might just be able to delete the record */
|
---|
228 | if (notify->array->num_depths == 0) {
|
---|
229 | return rec->delete_rec(rec);
|
---|
230 | }
|
---|
231 |
|
---|
232 | tmp_ctx = talloc_new(notify);
|
---|
233 | NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
|
---|
234 |
|
---|
235 | ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL, notify->array,
|
---|
236 | (ndr_push_flags_fn_t)ndr_push_notify_array);
|
---|
237 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
238 | talloc_free(tmp_ctx);
|
---|
239 | return ndr_map_error2ntstatus(ndr_err);
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (DEBUGLEVEL >= 10) {
|
---|
243 | DEBUG(10, ("notify_save:\n"));
|
---|
244 | NDR_PRINT_DEBUG(notify_array, notify->array);
|
---|
245 | }
|
---|
246 |
|
---|
247 | dbuf.dptr = blob.data;
|
---|
248 | dbuf.dsize = blob.length;
|
---|
249 |
|
---|
250 | status = rec->store(rec, dbuf, TDB_REPLACE);
|
---|
251 | talloc_free(tmp_ctx);
|
---|
252 |
|
---|
253 | return status;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /*
|
---|
258 | handle incoming notify messages
|
---|
259 | */
|
---|
260 | static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
|
---|
261 | uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
|
---|
262 | {
|
---|
263 | struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
|
---|
264 | enum ndr_err_code ndr_err;
|
---|
265 | struct notify_event ev;
|
---|
266 | TALLOC_CTX *tmp_ctx = talloc_new(notify);
|
---|
267 | struct notify_list *listel;
|
---|
268 |
|
---|
269 | if (tmp_ctx == NULL) {
|
---|
270 | return;
|
---|
271 | }
|
---|
272 |
|
---|
273 | ndr_err = ndr_pull_struct_blob(data, tmp_ctx, NULL, &ev,
|
---|
274 | (ndr_pull_flags_fn_t)ndr_pull_notify_event);
|
---|
275 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
276 | talloc_free(tmp_ctx);
|
---|
277 | return;
|
---|
278 | }
|
---|
279 |
|
---|
280 | for (listel=notify->list;listel;listel=listel->next) {
|
---|
281 | if (listel->private_data == ev.private_data) {
|
---|
282 | listel->callback(listel->private_data, &ev);
|
---|
283 | break;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | talloc_free(tmp_ctx);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /*
|
---|
291 | callback from sys_notify telling us about changes from the OS
|
---|
292 | */
|
---|
293 | static void sys_notify_callback(struct sys_notify_context *ctx,
|
---|
294 | void *ptr, struct notify_event *ev)
|
---|
295 | {
|
---|
296 | struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
|
---|
297 | ev->private_data = listel;
|
---|
298 | DEBUG(10, ("sys_notify_callback called with action=%d, for %s\n",
|
---|
299 | ev->action, ev->path));
|
---|
300 | listel->callback(listel->private_data, ev);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /*
|
---|
304 | add an entry to the notify array
|
---|
305 | */
|
---|
306 | static NTSTATUS notify_add_array(struct notify_context *notify, struct db_record *rec,
|
---|
307 | struct notify_entry *e,
|
---|
308 | void *private_data, int depth)
|
---|
309 | {
|
---|
310 | int i;
|
---|
311 | struct notify_depth *d;
|
---|
312 | struct notify_entry *ee;
|
---|
313 |
|
---|
314 | /* possibly expand the depths array */
|
---|
315 | if (depth >= notify->array->num_depths) {
|
---|
316 | d = talloc_realloc(notify->array, notify->array->depth,
|
---|
317 | struct notify_depth, depth+1);
|
---|
318 | NT_STATUS_HAVE_NO_MEMORY(d);
|
---|
319 | for (i=notify->array->num_depths;i<=depth;i++) {
|
---|
320 | ZERO_STRUCT(d[i]);
|
---|
321 | }
|
---|
322 | notify->array->depth = d;
|
---|
323 | notify->array->num_depths = depth+1;
|
---|
324 | }
|
---|
325 | d = ¬ify->array->depth[depth];
|
---|
326 |
|
---|
327 | /* expand the entries array */
|
---|
328 | ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
|
---|
329 | d->num_entries+1);
|
---|
330 | NT_STATUS_HAVE_NO_MEMORY(ee);
|
---|
331 | d->entries = ee;
|
---|
332 |
|
---|
333 | d->entries[d->num_entries] = *e;
|
---|
334 | d->entries[d->num_entries].private_data = private_data;
|
---|
335 | d->entries[d->num_entries].server = notify->server;
|
---|
336 | d->entries[d->num_entries].path_len = strlen(e->path);
|
---|
337 | d->num_entries++;
|
---|
338 |
|
---|
339 | d->max_mask |= e->filter;
|
---|
340 | d->max_mask_subdir |= e->subdir_filter;
|
---|
341 |
|
---|
342 | if (d->num_entries > 1) {
|
---|
343 | qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* recalculate the maximum masks */
|
---|
347 | d->max_mask = 0;
|
---|
348 | d->max_mask_subdir = 0;
|
---|
349 |
|
---|
350 | for (i=0;i<d->num_entries;i++) {
|
---|
351 | d->max_mask |= d->entries[i].filter;
|
---|
352 | d->max_mask_subdir |= d->entries[i].subdir_filter;
|
---|
353 | }
|
---|
354 |
|
---|
355 | return notify_save(notify, rec);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /*
|
---|
359 | Add a non-recursive watch
|
---|
360 | */
|
---|
361 |
|
---|
362 | static void notify_add_onelevel(struct notify_context *notify,
|
---|
363 | struct notify_entry *e, void *private_data)
|
---|
364 | {
|
---|
365 | struct notify_entry_array *array;
|
---|
366 | struct db_record *rec;
|
---|
367 | DATA_BLOB blob;
|
---|
368 | TDB_DATA dbuf;
|
---|
369 | enum ndr_err_code ndr_err;
|
---|
370 | NTSTATUS status;
|
---|
371 |
|
---|
372 | array = talloc_zero(talloc_tos(), struct notify_entry_array);
|
---|
373 | if (array == NULL) {
|
---|
374 | return;
|
---|
375 | }
|
---|
376 |
|
---|
377 | rec = notify->db_onelevel->fetch_locked(
|
---|
378 | notify->db_onelevel, talloc_tos(),
|
---|
379 | make_tdb_data((uint8_t *)&e->dir_id, sizeof(e->dir_id)));
|
---|
380 | if (rec == NULL) {
|
---|
381 | DEBUG(10, ("notify_add_onelevel: fetch_locked for %s failed"
|
---|
382 | "\n", file_id_string_tos(&e->dir_id)));
|
---|
383 | TALLOC_FREE(array);
|
---|
384 | return;
|
---|
385 | }
|
---|
386 |
|
---|
387 | blob.data = (uint8_t *)rec->value.dptr;
|
---|
388 | blob.length = rec->value.dsize;
|
---|
389 |
|
---|
390 | if (blob.length > 0) {
|
---|
391 | ndr_err = ndr_pull_struct_blob(
|
---|
392 | &blob, array, NULL, array,
|
---|
393 | (ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
|
---|
394 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
395 | DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
|
---|
396 | ndr_errstr(ndr_err)));
|
---|
397 | TALLOC_FREE(array);
|
---|
398 | return;
|
---|
399 | }
|
---|
400 | if (DEBUGLEVEL >= 10) {
|
---|
401 | DEBUG(10, ("notify_add_onelevel:\n"));
|
---|
402 | NDR_PRINT_DEBUG(notify_entry_array, array);
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | array->entries = talloc_realloc(array, array->entries,
|
---|
407 | struct notify_entry,
|
---|
408 | array->num_entries+1);
|
---|
409 | if (array->entries == NULL) {
|
---|
410 | TALLOC_FREE(array);
|
---|
411 | return;
|
---|
412 | }
|
---|
413 | array->entries[array->num_entries] = *e;
|
---|
414 | array->entries[array->num_entries].private_data = private_data;
|
---|
415 | array->entries[array->num_entries].server = notify->server;
|
---|
416 | array->num_entries += 1;
|
---|
417 |
|
---|
418 | ndr_err = ndr_push_struct_blob(
|
---|
419 | &blob, rec, NULL, array,
|
---|
420 | (ndr_push_flags_fn_t)ndr_push_notify_entry_array);
|
---|
421 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
422 | DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
|
---|
423 | ndr_errstr(ndr_err)));
|
---|
424 | TALLOC_FREE(array);
|
---|
425 | return;
|
---|
426 | }
|
---|
427 |
|
---|
428 | if (DEBUGLEVEL >= 10) {
|
---|
429 | DEBUG(10, ("notify_add_onelevel:\n"));
|
---|
430 | NDR_PRINT_DEBUG(notify_entry_array, array);
|
---|
431 | }
|
---|
432 |
|
---|
433 | dbuf.dptr = blob.data;
|
---|
434 | dbuf.dsize = blob.length;
|
---|
435 |
|
---|
436 | status = rec->store(rec, dbuf, TDB_REPLACE);
|
---|
437 | TALLOC_FREE(array);
|
---|
438 | if (!NT_STATUS_IS_OK(status)) {
|
---|
439 | DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
|
---|
440 | nt_errstr(status)));
|
---|
441 | return;
|
---|
442 | }
|
---|
443 | e->filter = 0;
|
---|
444 | return;
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | /*
|
---|
449 | add a notify watch. This is called when a notify is first setup on a open
|
---|
450 | directory handle.
|
---|
451 | */
|
---|
452 | NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
|
---|
453 | void (*callback)(void *, const struct notify_event *),
|
---|
454 | void *private_data)
|
---|
455 | {
|
---|
456 | struct notify_entry e = *e0;
|
---|
457 | NTSTATUS status;
|
---|
458 | char *tmp_path = NULL;
|
---|
459 | struct notify_list *listel;
|
---|
460 | size_t len;
|
---|
461 | int depth;
|
---|
462 | struct db_record *rec;
|
---|
463 |
|
---|
464 | /* see if change notify is enabled at all */
|
---|
465 | if (notify == NULL) {
|
---|
466 | return NT_STATUS_NOT_IMPLEMENTED;
|
---|
467 | }
|
---|
468 |
|
---|
469 | status = notify_fetch_locked(notify, &rec);
|
---|
470 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
471 |
|
---|
472 | status = notify_load(notify, rec);
|
---|
473 | if (!NT_STATUS_IS_OK(status)) {
|
---|
474 | talloc_free(rec);
|
---|
475 | return status;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /* cope with /. on the end of the path */
|
---|
479 | len = strlen(e.path);
|
---|
480 | if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
|
---|
481 | tmp_path = talloc_strndup(notify, e.path, len-2);
|
---|
482 | if (tmp_path == NULL) {
|
---|
483 | status = NT_STATUS_NO_MEMORY;
|
---|
484 | goto done;
|
---|
485 | }
|
---|
486 | e.path = tmp_path;
|
---|
487 | }
|
---|
488 |
|
---|
489 | depth = count_chars(e.path, '/');
|
---|
490 |
|
---|
491 | listel = TALLOC_ZERO_P(notify, struct notify_list);
|
---|
492 | if (listel == NULL) {
|
---|
493 | status = NT_STATUS_NO_MEMORY;
|
---|
494 | goto done;
|
---|
495 | }
|
---|
496 |
|
---|
497 | listel->private_data = private_data;
|
---|
498 | listel->callback = callback;
|
---|
499 | listel->depth = depth;
|
---|
500 | DLIST_ADD(notify->list, listel);
|
---|
501 |
|
---|
502 | /* ignore failures from sys_notify */
|
---|
503 | if (notify->sys_notify_ctx != NULL) {
|
---|
504 | /*
|
---|
505 | this call will modify e.filter and e.subdir_filter
|
---|
506 | to remove bits handled by the backend
|
---|
507 | */
|
---|
508 | status = sys_notify_watch(notify->sys_notify_ctx, &e,
|
---|
509 | sys_notify_callback, listel,
|
---|
510 | &listel->sys_notify_handle);
|
---|
511 | if (NT_STATUS_IS_OK(status)) {
|
---|
512 | talloc_steal(listel, listel->sys_notify_handle);
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (e.filter != 0) {
|
---|
517 | notify_add_onelevel(notify, &e, private_data);
|
---|
518 | status = NT_STATUS_OK;
|
---|
519 | }
|
---|
520 |
|
---|
521 | /* if the system notify handler couldn't handle some of the
|
---|
522 | filter bits, or couldn't handle a request for recursion
|
---|
523 | then we need to install it in the array used for the
|
---|
524 | intra-samba notify handling */
|
---|
525 | if (e.filter != 0 || e.subdir_filter != 0) {
|
---|
526 | status = notify_add_array(notify, rec, &e, private_data, depth);
|
---|
527 | }
|
---|
528 |
|
---|
529 | done:
|
---|
530 | talloc_free(rec);
|
---|
531 | talloc_free(tmp_path);
|
---|
532 |
|
---|
533 | return status;
|
---|
534 | }
|
---|
535 |
|
---|
536 | NTSTATUS notify_remove_onelevel(struct notify_context *notify,
|
---|
537 | const struct file_id *fid,
|
---|
538 | void *private_data)
|
---|
539 | {
|
---|
540 | struct notify_entry_array *array;
|
---|
541 | struct db_record *rec;
|
---|
542 | DATA_BLOB blob;
|
---|
543 | TDB_DATA dbuf;
|
---|
544 | enum ndr_err_code ndr_err;
|
---|
545 | NTSTATUS status;
|
---|
546 | int i;
|
---|
547 |
|
---|
548 | if (notify == NULL) {
|
---|
549 | return NT_STATUS_NOT_IMPLEMENTED;
|
---|
550 | }
|
---|
551 |
|
---|
552 | array = talloc_zero(talloc_tos(), struct notify_entry_array);
|
---|
553 | if (array == NULL) {
|
---|
554 | return NT_STATUS_NO_MEMORY;
|
---|
555 | }
|
---|
556 |
|
---|
557 | rec = notify->db_onelevel->fetch_locked(
|
---|
558 | notify->db_onelevel, array,
|
---|
559 | make_tdb_data((uint8_t *)fid, sizeof(*fid)));
|
---|
560 | if (rec == NULL) {
|
---|
561 | DEBUG(10, ("notify_remove_onelevel: fetch_locked for %s failed"
|
---|
562 | "\n", file_id_string_tos(fid)));
|
---|
563 | TALLOC_FREE(array);
|
---|
564 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
565 | }
|
---|
566 |
|
---|
567 | blob.data = (uint8_t *)rec->value.dptr;
|
---|
568 | blob.length = rec->value.dsize;
|
---|
569 |
|
---|
570 | if (blob.length > 0) {
|
---|
571 | ndr_err = ndr_pull_struct_blob(
|
---|
572 | &blob, array, NULL, array,
|
---|
573 | (ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
|
---|
574 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
575 | DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
|
---|
576 | ndr_errstr(ndr_err)));
|
---|
577 | TALLOC_FREE(array);
|
---|
578 | return ndr_map_error2ntstatus(ndr_err);
|
---|
579 | }
|
---|
580 | if (DEBUGLEVEL >= 10) {
|
---|
581 | DEBUG(10, ("notify_remove_onelevel:\n"));
|
---|
582 | NDR_PRINT_DEBUG(notify_entry_array, array);
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | for (i=0; i<array->num_entries; i++) {
|
---|
587 | if ((private_data == array->entries[i].private_data) &&
|
---|
588 | cluster_id_equal(¬ify->server,
|
---|
589 | &array->entries[i].server)) {
|
---|
590 | break;
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 | if (i == array->num_entries) {
|
---|
595 | TALLOC_FREE(array);
|
---|
596 | return NT_STATUS_OBJECT_NAME_NOT_FOUND;
|
---|
597 | }
|
---|
598 |
|
---|
599 | array->entries[i] = array->entries[array->num_entries-1];
|
---|
600 | array->num_entries -= 1;
|
---|
601 |
|
---|
602 | if (array->num_entries == 0) {
|
---|
603 | rec->delete_rec(rec);
|
---|
604 | TALLOC_FREE(array);
|
---|
605 | return NT_STATUS_OK;
|
---|
606 | }
|
---|
607 |
|
---|
608 | ndr_err = ndr_push_struct_blob(
|
---|
609 | &blob, rec, NULL, array,
|
---|
610 | (ndr_push_flags_fn_t)ndr_push_notify_entry_array);
|
---|
611 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
612 | DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
|
---|
613 | ndr_errstr(ndr_err)));
|
---|
614 | TALLOC_FREE(array);
|
---|
615 | return ndr_map_error2ntstatus(ndr_err);
|
---|
616 | }
|
---|
617 |
|
---|
618 | if (DEBUGLEVEL >= 10) {
|
---|
619 | DEBUG(10, ("notify_add_onelevel:\n"));
|
---|
620 | NDR_PRINT_DEBUG(notify_entry_array, array);
|
---|
621 | }
|
---|
622 |
|
---|
623 | dbuf.dptr = blob.data;
|
---|
624 | dbuf.dsize = blob.length;
|
---|
625 |
|
---|
626 | status = rec->store(rec, dbuf, TDB_REPLACE);
|
---|
627 | TALLOC_FREE(array);
|
---|
628 | if (!NT_STATUS_IS_OK(status)) {
|
---|
629 | DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
|
---|
630 | nt_errstr(status)));
|
---|
631 | return status;
|
---|
632 | }
|
---|
633 | return NT_STATUS_OK;
|
---|
634 | }
|
---|
635 |
|
---|
636 | /*
|
---|
637 | remove a notify watch. Called when the directory handle is closed
|
---|
638 | */
|
---|
639 | NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
|
---|
640 | {
|
---|
641 | NTSTATUS status;
|
---|
642 | struct notify_list *listel;
|
---|
643 | int i, depth;
|
---|
644 | struct notify_depth *d;
|
---|
645 | struct db_record *rec;
|
---|
646 |
|
---|
647 | /* see if change notify is enabled at all */
|
---|
648 | if (notify == NULL) {
|
---|
649 | return NT_STATUS_NOT_IMPLEMENTED;
|
---|
650 | }
|
---|
651 |
|
---|
652 | for (listel=notify->list;listel;listel=listel->next) {
|
---|
653 | if (listel->private_data == private_data) {
|
---|
654 | DLIST_REMOVE(notify->list, listel);
|
---|
655 | break;
|
---|
656 | }
|
---|
657 | }
|
---|
658 | if (listel == NULL) {
|
---|
659 | return NT_STATUS_OBJECT_NAME_NOT_FOUND;
|
---|
660 | }
|
---|
661 |
|
---|
662 | depth = listel->depth;
|
---|
663 |
|
---|
664 | talloc_free(listel);
|
---|
665 |
|
---|
666 | status = notify_fetch_locked(notify, &rec);
|
---|
667 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
668 |
|
---|
669 | status = notify_load(notify, rec);
|
---|
670 | if (!NT_STATUS_IS_OK(status)) {
|
---|
671 | talloc_free(rec);
|
---|
672 | return status;
|
---|
673 | }
|
---|
674 |
|
---|
675 | if (depth >= notify->array->num_depths) {
|
---|
676 | talloc_free(rec);
|
---|
677 | return NT_STATUS_OBJECT_NAME_NOT_FOUND;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /* we only have to search at the depth of this element */
|
---|
681 | d = ¬ify->array->depth[depth];
|
---|
682 |
|
---|
683 | for (i=0;i<d->num_entries;i++) {
|
---|
684 | if (private_data == d->entries[i].private_data &&
|
---|
685 | cluster_id_equal(¬ify->server, &d->entries[i].server)) {
|
---|
686 | break;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | if (i == d->num_entries) {
|
---|
690 | talloc_free(rec);
|
---|
691 | return NT_STATUS_OBJECT_NAME_NOT_FOUND;
|
---|
692 | }
|
---|
693 |
|
---|
694 | if (i < d->num_entries-1) {
|
---|
695 | memmove(&d->entries[i], &d->entries[i+1],
|
---|
696 | sizeof(d->entries[i])*(d->num_entries-(i+1)));
|
---|
697 | }
|
---|
698 | d->num_entries--;
|
---|
699 |
|
---|
700 | status = notify_save(notify, rec);
|
---|
701 |
|
---|
702 | talloc_free(rec);
|
---|
703 |
|
---|
704 | return status;
|
---|
705 | }
|
---|
706 |
|
---|
707 | /*
|
---|
708 | remove all notify watches for a messaging server
|
---|
709 | */
|
---|
710 | static NTSTATUS notify_remove_all(struct notify_context *notify,
|
---|
711 | const struct server_id *server)
|
---|
712 | {
|
---|
713 | NTSTATUS status;
|
---|
714 | int i, depth, del_count=0;
|
---|
715 | struct db_record *rec;
|
---|
716 |
|
---|
717 | status = notify_fetch_locked(notify, &rec);
|
---|
718 | NT_STATUS_NOT_OK_RETURN(status);
|
---|
719 |
|
---|
720 | status = notify_load(notify, rec);
|
---|
721 | if (!NT_STATUS_IS_OK(status)) {
|
---|
722 | talloc_free(rec);
|
---|
723 | return status;
|
---|
724 | }
|
---|
725 |
|
---|
726 | /* we have to search for all entries across all depths, looking for matches
|
---|
727 | for the server id */
|
---|
728 | for (depth=0;depth<notify->array->num_depths;depth++) {
|
---|
729 | struct notify_depth *d = ¬ify->array->depth[depth];
|
---|
730 | for (i=0;i<d->num_entries;i++) {
|
---|
731 | if (cluster_id_equal(server, &d->entries[i].server)) {
|
---|
732 | if (i < d->num_entries-1) {
|
---|
733 | memmove(&d->entries[i], &d->entries[i+1],
|
---|
734 | sizeof(d->entries[i])*(d->num_entries-(i+1)));
|
---|
735 | }
|
---|
736 | i--;
|
---|
737 | d->num_entries--;
|
---|
738 | del_count++;
|
---|
739 | }
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | if (del_count > 0) {
|
---|
744 | status = notify_save(notify, rec);
|
---|
745 | }
|
---|
746 |
|
---|
747 | talloc_free(rec);
|
---|
748 |
|
---|
749 | return status;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | /*
|
---|
754 | send a notify message to another messaging server
|
---|
755 | */
|
---|
756 | static NTSTATUS notify_send(struct notify_context *notify, struct notify_entry *e,
|
---|
757 | const char *path, uint32_t action)
|
---|
758 | {
|
---|
759 | struct notify_event ev;
|
---|
760 | DATA_BLOB data;
|
---|
761 | NTSTATUS status;
|
---|
762 | enum ndr_err_code ndr_err;
|
---|
763 | TALLOC_CTX *tmp_ctx;
|
---|
764 |
|
---|
765 | ev.action = action;
|
---|
766 | ev.path = path;
|
---|
767 | ev.private_data = e->private_data;
|
---|
768 |
|
---|
769 | tmp_ctx = talloc_new(notify);
|
---|
770 |
|
---|
771 | ndr_err = ndr_push_struct_blob(&data, tmp_ctx, NULL, &ev,
|
---|
772 | (ndr_push_flags_fn_t)ndr_push_notify_event);
|
---|
773 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
774 | talloc_free(tmp_ctx);
|
---|
775 | return ndr_map_error2ntstatus(ndr_err);
|
---|
776 | }
|
---|
777 |
|
---|
778 | status = messaging_send(notify->messaging_ctx, e->server,
|
---|
779 | MSG_PVFS_NOTIFY, &data);
|
---|
780 | talloc_free(tmp_ctx);
|
---|
781 | return status;
|
---|
782 | }
|
---|
783 |
|
---|
784 | void notify_onelevel(struct notify_context *notify, uint32_t action,
|
---|
785 | uint32_t filter, struct file_id fid, const char *name)
|
---|
786 | {
|
---|
787 | struct notify_entry_array *array;
|
---|
788 | TDB_DATA dbuf;
|
---|
789 | DATA_BLOB blob;
|
---|
790 | bool have_dead_entries = false;
|
---|
791 | int i;
|
---|
792 |
|
---|
793 | if (notify == NULL) {
|
---|
794 | return;
|
---|
795 | }
|
---|
796 |
|
---|
797 | array = talloc_zero(talloc_tos(), struct notify_entry_array);
|
---|
798 | if (array == NULL) {
|
---|
799 | return;
|
---|
800 | }
|
---|
801 |
|
---|
802 | if (notify->db_onelevel->fetch(
|
---|
803 | notify->db_onelevel, array,
|
---|
804 | make_tdb_data((uint8_t *)&fid, sizeof(fid)),
|
---|
805 | &dbuf) == -1) {
|
---|
806 | TALLOC_FREE(array);
|
---|
807 | return;
|
---|
808 | }
|
---|
809 |
|
---|
810 | blob.data = (uint8 *)dbuf.dptr;
|
---|
811 | blob.length = dbuf.dsize;
|
---|
812 |
|
---|
813 | if (blob.length > 0) {
|
---|
814 | enum ndr_err_code ndr_err;
|
---|
815 | ndr_err = ndr_pull_struct_blob(
|
---|
816 | &blob, array, NULL, array,
|
---|
817 | (ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
|
---|
818 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
819 | DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
|
---|
820 | ndr_errstr(ndr_err)));
|
---|
821 | TALLOC_FREE(array);
|
---|
822 | return;
|
---|
823 | }
|
---|
824 | if (DEBUGLEVEL >= 10) {
|
---|
825 | DEBUG(10, ("notify_onelevel:\n"));
|
---|
826 | NDR_PRINT_DEBUG(notify_entry_array, array);
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | for (i=0; i<array->num_entries; i++) {
|
---|
831 | struct notify_entry *e = &array->entries[i];
|
---|
832 |
|
---|
833 | if ((e->filter & filter) != 0) {
|
---|
834 | NTSTATUS status;
|
---|
835 |
|
---|
836 | status = notify_send(notify, e, name, action);
|
---|
837 | if (NT_STATUS_EQUAL(
|
---|
838 | status, NT_STATUS_INVALID_HANDLE)) {
|
---|
839 | /*
|
---|
840 | * Mark the entry as dead. All entries have a
|
---|
841 | * path set. The marker used here is setting
|
---|
842 | * that to NULL.
|
---|
843 | */
|
---|
844 | e->path = NULL;
|
---|
845 | have_dead_entries = true;
|
---|
846 | }
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | if (!have_dead_entries) {
|
---|
851 | TALLOC_FREE(array);
|
---|
852 | return;
|
---|
853 | }
|
---|
854 |
|
---|
855 | for (i=0; i<array->num_entries; i++) {
|
---|
856 | struct notify_entry *e = &array->entries[i];
|
---|
857 | if (e->path != NULL) {
|
---|
858 | continue;
|
---|
859 | }
|
---|
860 | DEBUG(10, ("Deleting notify entries for process %s because "
|
---|
861 | "it's gone\n", procid_str_static(&e->server)));
|
---|
862 | /*
|
---|
863 | * Potential TODO: This might need optimizing,
|
---|
864 | * notify_remove_onelevel() does a fetch_locked() operation at
|
---|
865 | * every call. But this would only matter if a process with
|
---|
866 | * MANY notifies has died without shutting down properly.
|
---|
867 | */
|
---|
868 | notify_remove_onelevel(notify, &e->dir_id, e->private_data);
|
---|
869 | }
|
---|
870 |
|
---|
871 | TALLOC_FREE(array);
|
---|
872 | return;
|
---|
873 | }
|
---|
874 |
|
---|
875 | /*
|
---|
876 | trigger a notify message for anyone waiting on a matching event
|
---|
877 |
|
---|
878 | This function is called a lot, and needs to be very fast. The unusual data structure
|
---|
879 | and traversal is designed to be fast in the average case, even for large numbers of
|
---|
880 | notifies
|
---|
881 | */
|
---|
882 | void notify_trigger(struct notify_context *notify,
|
---|
883 | uint32_t action, uint32_t filter, const char *path)
|
---|
884 | {
|
---|
885 | NTSTATUS status;
|
---|
886 | int depth;
|
---|
887 | const char *p, *next_p;
|
---|
888 |
|
---|
889 | DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
|
---|
890 | "path=%s\n", (unsigned)action, (unsigned)filter, path));
|
---|
891 |
|
---|
892 | /* see if change notify is enabled at all */
|
---|
893 | if (notify == NULL) {
|
---|
894 | return;
|
---|
895 | }
|
---|
896 |
|
---|
897 | again:
|
---|
898 | status = notify_load(notify, NULL);
|
---|
899 | if (!NT_STATUS_IS_OK(status)) {
|
---|
900 | return;
|
---|
901 | }
|
---|
902 |
|
---|
903 | /* loop along the given path, working with each directory depth separately */
|
---|
904 | for (depth=0,p=path;
|
---|
905 | p && depth < notify->array->num_depths;
|
---|
906 | p=next_p,depth++) {
|
---|
907 | int p_len = p - path;
|
---|
908 | int min_i, max_i, i;
|
---|
909 | struct notify_depth *d = ¬ify->array->depth[depth];
|
---|
910 | next_p = strchr(p+1, '/');
|
---|
911 |
|
---|
912 | /* see if there are any entries at this depth */
|
---|
913 | if (d->num_entries == 0) continue;
|
---|
914 |
|
---|
915 | /* try to skip based on the maximum mask. If next_p is
|
---|
916 | NULL then we know it will be a 'this directory'
|
---|
917 | match, otherwise it must be a subdir match */
|
---|
918 | if (next_p != NULL) {
|
---|
919 | if (0 == (filter & d->max_mask_subdir)) {
|
---|
920 | continue;
|
---|
921 | }
|
---|
922 | } else {
|
---|
923 | if (0 == (filter & d->max_mask)) {
|
---|
924 | continue;
|
---|
925 | }
|
---|
926 | }
|
---|
927 |
|
---|
928 | /* we know there is an entry here worth looking
|
---|
929 | for. Use a bisection search to find the first entry
|
---|
930 | with a matching path */
|
---|
931 | min_i = 0;
|
---|
932 | max_i = d->num_entries-1;
|
---|
933 |
|
---|
934 | while (min_i < max_i) {
|
---|
935 | struct notify_entry *e;
|
---|
936 | int cmp;
|
---|
937 | i = (min_i+max_i)/2;
|
---|
938 | e = &d->entries[i];
|
---|
939 | cmp = strncmp(path, e->path, p_len);
|
---|
940 | if (cmp == 0) {
|
---|
941 | if (p_len == e->path_len) {
|
---|
942 | max_i = i;
|
---|
943 | } else {
|
---|
944 | max_i = i-1;
|
---|
945 | }
|
---|
946 | } else if (cmp < 0) {
|
---|
947 | max_i = i-1;
|
---|
948 | } else {
|
---|
949 | min_i = i+1;
|
---|
950 | }
|
---|
951 | }
|
---|
952 |
|
---|
953 | if (min_i != max_i) {
|
---|
954 | /* none match */
|
---|
955 | continue;
|
---|
956 | }
|
---|
957 |
|
---|
958 | /* we now know that the entries start at min_i */
|
---|
959 | for (i=min_i;i<d->num_entries;i++) {
|
---|
960 | struct notify_entry *e = &d->entries[i];
|
---|
961 | if (p_len != e->path_len ||
|
---|
962 | strncmp(path, e->path, p_len) != 0) break;
|
---|
963 | if (next_p != NULL) {
|
---|
964 | if (0 == (filter & e->subdir_filter)) {
|
---|
965 | continue;
|
---|
966 | }
|
---|
967 | } else {
|
---|
968 | if (0 == (filter & e->filter)) {
|
---|
969 | continue;
|
---|
970 | }
|
---|
971 | }
|
---|
972 | status = notify_send(notify, e, path + e->path_len + 1,
|
---|
973 | action);
|
---|
974 |
|
---|
975 | if (NT_STATUS_EQUAL(
|
---|
976 | status, NT_STATUS_INVALID_HANDLE)) {
|
---|
977 | struct server_id server = e->server;
|
---|
978 |
|
---|
979 | DEBUG(10, ("Deleting notify entries for "
|
---|
980 | "process %s because it's gone\n",
|
---|
981 | procid_str_static(&e->server)));
|
---|
982 | notify_remove_all(notify, &server);
|
---|
983 | goto again;
|
---|
984 | }
|
---|
985 | }
|
---|
986 | }
|
---|
987 | }
|
---|