2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Volker Lendecke 2014
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/>.
22 #include "lib/util/server_id.h"
23 #include "lib/util/data_blob.h"
24 #include "librpc/gen_ndr/notify.h"
25 #include "librpc/gen_ndr/messaging.h"
26 #include "librpc/gen_ndr/server_id.h"
27 #include "lib/dbwrap/dbwrap.h"
28 #include "lib/dbwrap/dbwrap_rbt.h"
33 #include "lib/util/server_id_db.h"
34 #include "lib/util/tevent_unix.h"
35 #include "lib/util/tevent_ntstatus.h"
36 #include "ctdbd_conn.h"
37 #include "ctdb_srvids.h"
38 #include "server_id_db_util.h"
39 #include "lib/util/iov_buf.h"
40 #include "messages_util.h"
42 #ifdef CLUSTER_SUPPORT
43 #include "ctdb_protocol.h"
49 * All of notifyd's state
52 struct notifyd_state {
53 struct tevent_context *ev;
54 struct messaging_context *msg_ctx;
55 struct ctdbd_connection *ctdbd_conn;
58 * Database of everything clients show interest in. Indexed by
59 * absolute path. The database keys are not 0-terminated
60 * to allow the criticial operation, notifyd_trigger, to walk
61 * the structure from the top without adding intermediate 0s.
62 * The database records contain an array of
64 * struct notifyd_instance
66 * to be maintained and parsed by notifyd_entry_parse()
68 struct db_context *entries;
71 * In the cluster case, this is the place where we store a log
72 * of all MSG_SMB_NOTIFY_REC_CHANGE messages. We just 1:1
73 * forward them to our peer notifyd's in the cluster once a
74 * second or when the log grows too large.
77 struct messaging_reclog *log;
80 * Array of companion notifyd's in a cluster. Every notifyd
81 * broadcasts its messaging_reclog to every other notifyd in
82 * the cluster. This is done by making ctdb send a message to
83 * srvid CTDB_SRVID_SAMBA_NOTIFY_PROXY with destination node
84 * number CTDB_BROADCAST_CONNECTED. Everybody in the cluster who
85 * had called register_with_ctdbd this srvid will receive the
88 * Database replication happens via these broadcasts. Also,
89 * they serve as liveness indication. If a notifyd receives a
90 * broadcast from an unknown peer, it will create one for this
91 * srvid. Also when we don't hear anything from a peer for a
92 * while, we will discard it.
95 struct notifyd_peer **peers;
98 sys_notify_watch_fn sys_notify_watch;
99 struct sys_notify_context *sys_notify_ctx;
103 * notifyd's representation of a notify instance
105 struct notifyd_instance {
106 struct server_id client;
107 struct notify_instance instance;
109 void *sys_watch; /* inotify/fam/etc handle */
112 * Filters after sys_watch took responsibility of some bits
114 uint32_t internal_filter;
115 uint32_t internal_subdir_filter;
118 struct notifyd_peer {
119 struct notifyd_state *state;
120 struct server_id pid;
122 struct db_context *db;
123 time_t last_broadcast;
126 static void notifyd_rec_change(struct messaging_context *msg_ctx,
127 void *private_data, uint32_t msg_type,
128 struct server_id src, DATA_BLOB *data);
129 static void notifyd_trigger(struct messaging_context *msg_ctx,
130 void *private_data, uint32_t msg_type,
131 struct server_id src, DATA_BLOB *data);
132 static void notifyd_get_db(struct messaging_context *msg_ctx,
133 void *private_data, uint32_t msg_type,
134 struct server_id src, DATA_BLOB *data);
136 #ifdef CLUSTER_SUPPORT
137 static void notifyd_got_db(struct messaging_context *msg_ctx,
138 void *private_data, uint32_t msg_type,
139 struct server_id src, DATA_BLOB *data);
140 static void notifyd_broadcast_reclog(struct ctdbd_connection *ctdbd_conn,
141 struct server_id src,
142 struct messaging_reclog *log);
144 static void notifyd_sys_callback(struct sys_notify_context *ctx,
145 void *private_data, struct notify_event *ev,
148 #ifdef CLUSTER_SUPPORT
149 static struct tevent_req *notifyd_broadcast_reclog_send(
150 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
151 struct ctdbd_connection *ctdbd_conn, struct server_id src,
152 struct messaging_reclog *log);
153 static int notifyd_broadcast_reclog_recv(struct tevent_req *req);
155 static struct tevent_req *notifyd_clean_peers_send(
156 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
157 struct notifyd_state *notifyd);
158 static int notifyd_clean_peers_recv(struct tevent_req *req);
161 static int sys_notify_watch_dummy(
163 struct sys_notify_context *ctx,
166 uint32_t *subdir_filter,
167 void (*callback)(struct sys_notify_context *ctx,
169 struct notify_event *ev,
174 void **handle = handle_p;
179 #ifdef CLUSTER_SUPPORT
180 static void notifyd_broadcast_reclog_finished(struct tevent_req *subreq);
181 static void notifyd_clean_peers_finished(struct tevent_req *subreq);
182 static int notifyd_snoop_broadcast(struct tevent_context *ev,
183 uint32_t src_vnn, uint32_t dst_vnn,
185 const uint8_t *msg, size_t msglen,
189 struct tevent_req *notifyd_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
190 struct messaging_context *msg_ctx,
191 struct ctdbd_connection *ctdbd_conn,
192 sys_notify_watch_fn sys_notify_watch,
193 struct sys_notify_context *sys_notify_ctx)
195 struct tevent_req *req;
196 #ifdef CLUSTER_SUPPORT
197 struct tevent_req *subreq;
199 struct notifyd_state *state;
200 struct server_id_db *names_db;
204 req = tevent_req_create(mem_ctx, &state, struct notifyd_state);
209 state->msg_ctx = msg_ctx;
210 state->ctdbd_conn = ctdbd_conn;
212 if (sys_notify_watch == NULL) {
213 sys_notify_watch = sys_notify_watch_dummy;
216 state->sys_notify_watch = sys_notify_watch;
217 state->sys_notify_ctx = sys_notify_ctx;
219 state->entries = db_open_rbt(state);
220 if (tevent_req_nomem(state->entries, req)) {
221 return tevent_req_post(req, ev);
224 status = messaging_register(msg_ctx, state, MSG_SMB_NOTIFY_REC_CHANGE,
226 if (tevent_req_nterror(req, status)) {
227 return tevent_req_post(req, ev);
230 status = messaging_register(msg_ctx, state, MSG_SMB_NOTIFY_TRIGGER,
232 if (tevent_req_nterror(req, status)) {
233 goto deregister_rec_change;
236 status = messaging_register(msg_ctx, state, MSG_SMB_NOTIFY_GET_DB,
238 if (tevent_req_nterror(req, status)) {
239 goto deregister_trigger;
242 names_db = messaging_names_db(msg_ctx);
244 ret = server_id_db_set_exclusive(names_db, "notify-daemon");
246 DEBUG(10, ("%s: server_id_db_add failed: %s\n",
247 __func__, strerror(ret)));
248 tevent_req_error(req, ret);
249 goto deregister_get_db;
252 if (ctdbd_conn == NULL) {
254 * No cluster around, skip the database replication
260 #ifdef CLUSTER_SUPPORT
261 status = messaging_register(msg_ctx, state, MSG_SMB_NOTIFY_DB,
263 if (tevent_req_nterror(req, status)) {
264 goto deregister_get_db;
267 state->log = talloc_zero(state, struct messaging_reclog);
268 if (tevent_req_nomem(state->log, req)) {
272 subreq = notifyd_broadcast_reclog_send(
273 state->log, ev, ctdbd_conn,
274 messaging_server_id(msg_ctx),
276 if (tevent_req_nomem(subreq, req)) {
279 tevent_req_set_callback(subreq,
280 notifyd_broadcast_reclog_finished,
283 subreq = notifyd_clean_peers_send(state, ev, state);
284 if (tevent_req_nomem(subreq, req)) {
287 tevent_req_set_callback(subreq, notifyd_clean_peers_finished,
290 ret = register_with_ctdbd(ctdbd_conn,
291 CTDB_SRVID_SAMBA_NOTIFY_PROXY,
292 notifyd_snoop_broadcast, state);
294 tevent_req_error(req, ret);
301 #ifdef CLUSTER_SUPPORT
303 messaging_deregister(msg_ctx, MSG_SMB_NOTIFY_DB, state);
306 messaging_deregister(msg_ctx, MSG_SMB_NOTIFY_GET_DB, state);
308 messaging_deregister(msg_ctx, MSG_SMB_NOTIFY_TRIGGER, state);
309 deregister_rec_change:
310 messaging_deregister(msg_ctx, MSG_SMB_NOTIFY_REC_CHANGE, state);
311 return tevent_req_post(req, ev);
314 #ifdef CLUSTER_SUPPORT
316 static void notifyd_broadcast_reclog_finished(struct tevent_req *subreq)
318 struct tevent_req *req = tevent_req_callback_data(
319 subreq, struct tevent_req);
322 ret = notifyd_broadcast_reclog_recv(subreq);
324 tevent_req_error(req, ret);
327 static void notifyd_clean_peers_finished(struct tevent_req *subreq)
329 struct tevent_req *req = tevent_req_callback_data(
330 subreq, struct tevent_req);
333 ret = notifyd_clean_peers_recv(subreq);
335 tevent_req_error(req, ret);
340 int notifyd_recv(struct tevent_req *req)
342 return tevent_req_simple_recv_unix(req);
346 * Parse an entry in the notifyd_context->entries database
349 static bool notifyd_parse_entry(uint8_t *buf, size_t buflen,
350 struct notifyd_instance **instances,
351 size_t *num_instances)
353 if ((buflen % sizeof(struct notifyd_instance)) != 0) {
354 DEBUG(1, ("%s: invalid buffer size: %u\n",
355 __func__, (unsigned)buflen));
359 if (instances != NULL) {
360 *instances = (struct notifyd_instance *)buf;
362 if (num_instances != NULL) {
363 *num_instances = buflen / sizeof(struct notifyd_instance);
368 static bool notifyd_apply_rec_change(
369 const struct server_id *client,
370 const char *path, size_t pathlen,
371 const struct notify_instance *chg,
372 struct db_context *entries,
373 sys_notify_watch_fn sys_notify_watch,
374 struct sys_notify_context *sys_notify_ctx,
375 struct messaging_context *msg_ctx)
377 struct db_record *rec;
378 struct notifyd_instance *instances;
379 size_t num_instances;
381 struct notifyd_instance *instance;
387 DEBUG(1, ("%s: pathlen==0\n", __func__));
390 if (path[pathlen-1] != '\0') {
391 DEBUG(1, ("%s: path not 0-terminated\n", __func__));
395 DEBUG(10, ("%s: path=%s, filter=%u, subdir_filter=%u, "
396 "private_data=%p\n", __func__, path,
397 (unsigned)chg->filter, (unsigned)chg->subdir_filter,
400 rec = dbwrap_fetch_locked(
402 make_tdb_data((const uint8_t *)path, pathlen-1));
405 DEBUG(1, ("%s: dbwrap_fetch_locked failed\n", __func__));
410 value = dbwrap_record_get_value(rec);
412 if (value.dsize != 0) {
413 if (!notifyd_parse_entry(value.dptr, value.dsize, NULL,
420 * Overallocate by one instance to avoid a realloc when adding
422 instances = talloc_array(rec, struct notifyd_instance,
424 if (instances == NULL) {
425 DEBUG(1, ("%s: talloc failed\n", __func__));
429 if (value.dsize != 0) {
430 memcpy(instances, value.dptr, value.dsize);
433 for (i=0; i<num_instances; i++) {
434 instance = &instances[i];
436 if (server_id_equal(&instance->client, client) &&
437 (instance->instance.private_data == chg->private_data)) {
442 if (i < num_instances) {
443 instance->instance = *chg;
446 * We've overallocated for one instance
448 instance = &instances[num_instances];
450 *instance = (struct notifyd_instance) {
453 .internal_filter = chg->filter,
454 .internal_subdir_filter = chg->subdir_filter
460 if ((instance->instance.filter != 0) ||
461 (instance->instance.subdir_filter != 0)) {
464 TALLOC_FREE(instance->sys_watch);
466 ret = sys_notify_watch(entries, sys_notify_ctx, path,
467 &instance->internal_filter,
468 &instance->internal_subdir_filter,
469 notifyd_sys_callback, msg_ctx,
470 &instance->sys_watch);
472 DBG_WARNING("sys_notify_watch for [%s] returned %s\n",
473 path, strerror(errno));
477 if ((instance->instance.filter == 0) &&
478 (instance->instance.subdir_filter == 0)) {
479 /* This is a delete request */
480 TALLOC_FREE(instance->sys_watch);
481 *instance = instances[num_instances-1];
485 DEBUG(10, ("%s: %s has %u instances\n", __func__,
486 path, (unsigned)num_instances));
488 if (num_instances == 0) {
489 status = dbwrap_record_delete(rec);
490 if (!NT_STATUS_IS_OK(status)) {
491 DEBUG(1, ("%s: dbwrap_record_delete returned %s\n",
492 __func__, nt_errstr(status)));
496 value = make_tdb_data(
497 (uint8_t *)instances,
498 sizeof(struct notifyd_instance) * num_instances);
500 status = dbwrap_record_store(rec, value, 0);
501 if (!NT_STATUS_IS_OK(status)) {
502 DEBUG(1, ("%s: dbwrap_record_store returned %s\n",
503 __func__, nt_errstr(status)));
514 static void notifyd_sys_callback(struct sys_notify_context *ctx,
515 void *private_data, struct notify_event *ev,
518 struct messaging_context *msg_ctx = talloc_get_type_abort(
519 private_data, struct messaging_context);
520 struct notify_trigger_msg msg;
524 msg = (struct notify_trigger_msg) {
525 .when = timespec_current(),
526 .action = ev->action,
530 iov[0].iov_base = &msg;
531 iov[0].iov_len = offsetof(struct notify_trigger_msg, path);
532 iov[1].iov_base = discard_const_p(char, ev->dir);
533 iov[1].iov_len = strlen(ev->dir);
534 iov[2].iov_base = &slash;
536 iov[3].iov_base = discard_const_p(char, ev->path);
537 iov[3].iov_len = strlen(ev->path)+1;
540 msg_ctx, messaging_server_id(msg_ctx),
541 MSG_SMB_NOTIFY_TRIGGER, iov, ARRAY_SIZE(iov), NULL, 0);
544 static bool notifyd_parse_rec_change(uint8_t *buf, size_t bufsize,
545 struct notify_rec_change_msg **pmsg,
548 struct notify_rec_change_msg *msg;
550 if (bufsize < offsetof(struct notify_rec_change_msg, path) + 1) {
551 DEBUG(1, ("%s: message too short, ignoring: %u\n", __func__,
556 *pmsg = msg = (struct notify_rec_change_msg *)buf;
557 *pathlen = bufsize - offsetof(struct notify_rec_change_msg, path);
559 DEBUG(10, ("%s: Got rec_change_msg filter=%u, subdir_filter=%u, "
560 "private_data=%p, path=%.*s\n",
561 __func__, (unsigned)msg->instance.filter,
562 (unsigned)msg->instance.subdir_filter,
563 msg->instance.private_data, (int)(*pathlen), msg->path));
568 static void notifyd_rec_change(struct messaging_context *msg_ctx,
569 void *private_data, uint32_t msg_type,
570 struct server_id src, DATA_BLOB *data)
572 struct notifyd_state *state = talloc_get_type_abort(
573 private_data, struct notifyd_state);
574 struct server_id_buf idbuf;
575 struct notify_rec_change_msg *msg;
579 DBG_DEBUG("Got %zu bytes from %s\n", data->length,
580 server_id_str_buf(src, &idbuf));
582 ok = notifyd_parse_rec_change(data->data, data->length,
588 ok = notifyd_apply_rec_change(
589 &src, msg->path, pathlen, &msg->instance,
590 state->entries, state->sys_notify_watch, state->sys_notify_ctx,
593 DEBUG(1, ("%s: notifyd_apply_rec_change failed, ignoring\n",
598 if ((state->log == NULL) || (state->ctdbd_conn == NULL)) {
602 #ifdef CLUSTER_SUPPORT
605 struct messaging_rec **tmp;
606 struct messaging_reclog *log;
607 struct iovec iov = { .iov_base = data->data, .iov_len = data->length };
611 tmp = talloc_realloc(log, log->recs, struct messaging_rec *,
614 DEBUG(1, ("%s: talloc_realloc failed, ignoring\n", __func__));
619 log->recs[log->num_recs] = messaging_rec_create(
620 log->recs, src, messaging_server_id(msg_ctx),
621 msg_type, &iov, 1, NULL, 0);
623 if (log->recs[log->num_recs] == NULL) {
624 DBG_WARNING("messaging_rec_create failed, ignoring\n");
630 if (log->num_recs >= 100) {
632 * Don't let the log grow too large
634 notifyd_broadcast_reclog(state->ctdbd_conn,
635 messaging_server_id(msg_ctx), log);
642 struct notifyd_trigger_state {
643 struct messaging_context *msg_ctx;
644 struct notify_trigger_msg *msg;
646 bool covered_by_sys_notify;
649 static void notifyd_trigger_parser(TDB_DATA key, TDB_DATA data,
652 static void notifyd_trigger(struct messaging_context *msg_ctx,
653 void *private_data, uint32_t msg_type,
654 struct server_id src, DATA_BLOB *data)
656 struct notifyd_state *state = talloc_get_type_abort(
657 private_data, struct notifyd_state);
658 struct server_id my_id = messaging_server_id(msg_ctx);
659 struct notifyd_trigger_state tstate;
661 const char *p, *next_p;
663 if (data->length < offsetof(struct notify_trigger_msg, path) + 1) {
664 DBG_WARNING("message too short, ignoring: %zu\n",
668 if (data->data[data->length-1] != 0) {
669 DEBUG(1, ("%s: path not 0-terminated, ignoring\n", __func__));
673 tstate.msg_ctx = msg_ctx;
675 tstate.covered_by_sys_notify = (src.vnn == my_id.vnn);
676 tstate.covered_by_sys_notify &= !server_id_equal(&src, &my_id);
678 tstate.msg = (struct notify_trigger_msg *)data->data;
679 path = tstate.msg->path;
681 DEBUG(10, ("%s: Got trigger_msg action=%u, filter=%u, path=%s\n",
682 __func__, (unsigned)tstate.msg->action,
683 (unsigned)tstate.msg->filter, path));
685 if (path[0] != '/') {
686 DEBUG(1, ("%s: path %s does not start with /, ignoring\n",
691 for (p = strchr(path+1, '/'); p != NULL; p = next_p) {
692 ptrdiff_t path_len = p - path;
696 next_p = strchr(p+1, '/');
697 tstate.recursive = (next_p != NULL);
699 DEBUG(10, ("%s: Trying path %.*s\n", __func__,
700 (int)path_len, path));
702 key = (TDB_DATA) { .dptr = discard_const_p(uint8_t, path),
705 dbwrap_parse_record(state->entries, key,
706 notifyd_trigger_parser, &tstate);
708 if (state->peers == NULL) {
712 if (src.vnn != my_id.vnn) {
716 for (i=0; i<state->num_peers; i++) {
717 if (state->peers[i]->db == NULL) {
719 * Inactive peer, did not get a db yet
723 dbwrap_parse_record(state->peers[i]->db, key,
724 notifyd_trigger_parser, &tstate);
729 static void notifyd_send_delete(struct messaging_context *msg_ctx,
731 struct notifyd_instance *instance);
733 static void notifyd_trigger_parser(TDB_DATA key, TDB_DATA data,
737 struct notifyd_trigger_state *tstate = private_data;
738 struct notify_event_msg msg = { .action = tstate->msg->action,
739 .when = tstate->msg->when };
741 size_t path_len = key.dsize;
742 struct notifyd_instance *instances = NULL;
743 size_t num_instances = 0;
746 if (!notifyd_parse_entry(data.dptr, data.dsize, &instances,
748 DEBUG(1, ("%s: Could not parse notifyd_entry\n", __func__));
752 DEBUG(10, ("%s: Found %u instances for %.*s\n", __func__,
753 (unsigned)num_instances, (int)key.dsize,
756 iov[0].iov_base = &msg;
757 iov[0].iov_len = offsetof(struct notify_event_msg, path);
758 iov[1].iov_base = tstate->msg->path + path_len + 1;
759 iov[1].iov_len = strlen((char *)(iov[1].iov_base)) + 1;
761 for (i=0; i<num_instances; i++) {
762 struct notifyd_instance *instance = &instances[i];
763 struct server_id_buf idbuf;
767 if (tstate->covered_by_sys_notify) {
768 if (tstate->recursive) {
769 i_filter = instance->internal_subdir_filter;
771 i_filter = instance->internal_filter;
774 if (tstate->recursive) {
775 i_filter = instance->instance.subdir_filter;
777 i_filter = instance->instance.filter;
781 if ((i_filter & tstate->msg->filter) == 0) {
785 msg.private_data = instance->instance.private_data;
787 status = messaging_send_iov(
788 tstate->msg_ctx, instance->client,
789 MSG_PVFS_NOTIFY, iov, ARRAY_SIZE(iov), NULL, 0);
791 DEBUG(10, ("%s: messaging_send_iov to %s returned %s\n",
793 server_id_str_buf(instance->client, &idbuf),
796 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
797 procid_is_local(&instance->client)) {
799 * That process has died
801 notifyd_send_delete(tstate->msg_ctx, key, instance);
805 if (!NT_STATUS_IS_OK(status)) {
806 DEBUG(1, ("%s: messaging_send_iov returned %s\n",
807 __func__, nt_errstr(status)));
813 * Send a delete request to ourselves to properly discard a notify
814 * record for an smbd that has died.
817 static void notifyd_send_delete(struct messaging_context *msg_ctx,
819 struct notifyd_instance *instance)
821 struct notify_rec_change_msg msg = {
822 .instance.private_data = instance->instance.private_data
829 * Send a rec_change to ourselves to delete a dead entry
832 iov[0] = (struct iovec) {
834 .iov_len = offsetof(struct notify_rec_change_msg, path) };
835 iov[1] = (struct iovec) { .iov_base = key.dptr, .iov_len = key.dsize };
836 iov[2] = (struct iovec) { .iov_base = &nul, .iov_len = sizeof(nul) };
838 ret = messaging_send_iov_from(
839 msg_ctx, instance->client, messaging_server_id(msg_ctx),
840 MSG_SMB_NOTIFY_REC_CHANGE, iov, ARRAY_SIZE(iov), NULL, 0);
843 DEBUG(10, ("%s: messaging_send_iov_from returned %s\n",
844 __func__, strerror(ret)));
848 static void notifyd_get_db(struct messaging_context *msg_ctx,
849 void *private_data, uint32_t msg_type,
850 struct server_id src, DATA_BLOB *data)
852 struct notifyd_state *state = talloc_get_type_abort(
853 private_data, struct notifyd_state);
854 struct server_id_buf id1, id2;
856 uint64_t rec_index = UINT64_MAX;
857 uint8_t index_buf[sizeof(uint64_t)];
862 dbsize = dbwrap_marshall(state->entries, NULL, 0);
864 buf = talloc_array(talloc_tos(), uint8_t, dbsize);
866 DEBUG(1, ("%s: talloc_array(%ju) failed\n",
867 __func__, (uintmax_t)dbsize));
871 dbsize = dbwrap_marshall(state->entries, buf, dbsize);
873 if (dbsize != talloc_get_size(buf)) {
874 DEBUG(1, ("%s: dbsize changed: %ju->%ju\n", __func__,
875 (uintmax_t)talloc_get_size(buf),
881 if (state->log != NULL) {
882 rec_index = state->log->rec_index;
884 SBVAL(index_buf, 0, rec_index);
886 iov[0] = (struct iovec) { .iov_base = index_buf,
887 .iov_len = sizeof(index_buf) };
888 iov[1] = (struct iovec) { .iov_base = buf,
891 DEBUG(10, ("%s: Sending %ju bytes to %s->%s\n", __func__,
892 (uintmax_t)iov_buflen(iov, ARRAY_SIZE(iov)),
893 server_id_str_buf(messaging_server_id(msg_ctx), &id1),
894 server_id_str_buf(src, &id2)));
896 status = messaging_send_iov(msg_ctx, src, MSG_SMB_NOTIFY_DB,
897 iov, ARRAY_SIZE(iov), NULL, 0);
899 if (!NT_STATUS_IS_OK(status)) {
900 DEBUG(1, ("%s: messaging_send_iov failed: %s\n",
901 __func__, nt_errstr(status)));
905 #ifdef CLUSTER_SUPPORT
907 static int notifyd_add_proxy_syswatches(struct db_record *rec,
910 static void notifyd_got_db(struct messaging_context *msg_ctx,
911 void *private_data, uint32_t msg_type,
912 struct server_id src, DATA_BLOB *data)
914 struct notifyd_state *state = talloc_get_type_abort(
915 private_data, struct notifyd_state);
916 struct notifyd_peer *p = NULL;
917 struct server_id_buf idbuf;
922 for (i=0; i<state->num_peers; i++) {
923 if (server_id_equal(&src, &state->peers[i]->pid)) {
930 DBG_DEBUG("Did not find peer for db from %s\n",
931 server_id_str_buf(src, &idbuf));
935 if (data->length < 8) {
936 DBG_DEBUG("Got short db length %zu from %s\n", data->length,
937 server_id_str_buf(src, &idbuf));
942 p->rec_index = BVAL(data->data, 0);
944 p->db = db_open_rbt(p);
946 DEBUG(10, ("%s: db_open_rbt failed\n", __func__));
951 status = dbwrap_unmarshall(p->db, data->data + 8,
953 if (!NT_STATUS_IS_OK(status)) {
954 DEBUG(10, ("%s: dbwrap_unmarshall returned %s for db %s\n",
955 __func__, nt_errstr(status),
956 server_id_str_buf(src, &idbuf)));
961 dbwrap_traverse_read(p->db, notifyd_add_proxy_syswatches, state,
964 DEBUG(10, ("%s: Database from %s contained %d records\n", __func__,
965 server_id_str_buf(src, &idbuf), count));
968 static void notifyd_broadcast_reclog(struct ctdbd_connection *ctdbd_conn,
969 struct server_id src,
970 struct messaging_reclog *log)
972 enum ndr_err_code ndr_err;
973 uint8_t msghdr[MESSAGE_HDR_LENGTH];
982 DEBUG(10, ("%s: rec_index=%ju, num_recs=%u\n", __func__,
983 (uintmax_t)log->rec_index, (unsigned)log->num_recs));
985 message_hdr_put(msghdr, MSG_SMB_NOTIFY_REC_CHANGES, src,
986 (struct server_id) {0 });
987 iov[0] = (struct iovec) { .iov_base = msghdr,
988 .iov_len = sizeof(msghdr) };
990 ndr_err = ndr_push_struct_blob(
992 (ndr_push_flags_fn_t)ndr_push_messaging_reclog);
993 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
994 DEBUG(1, ("%s: ndr_push_messaging_recs failed: %s\n",
995 __func__, ndr_errstr(ndr_err)));
998 iov[1] = (struct iovec) { .iov_base = blob.data,
999 .iov_len = blob.length };
1001 ret = ctdbd_messaging_send_iov(
1002 ctdbd_conn, CTDB_BROADCAST_CONNECTED,
1003 CTDB_SRVID_SAMBA_NOTIFY_PROXY, iov, ARRAY_SIZE(iov));
1004 TALLOC_FREE(blob.data);
1006 DEBUG(1, ("%s: ctdbd_messaging_send failed: %s\n",
1007 __func__, strerror(ret)));
1011 log->rec_index += 1;
1015 TALLOC_FREE(log->recs);
1018 struct notifyd_broadcast_reclog_state {
1019 struct tevent_context *ev;
1020 struct ctdbd_connection *ctdbd_conn;
1021 struct server_id src;
1022 struct messaging_reclog *log;
1025 static void notifyd_broadcast_reclog_next(struct tevent_req *subreq);
1027 static struct tevent_req *notifyd_broadcast_reclog_send(
1028 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1029 struct ctdbd_connection *ctdbd_conn, struct server_id src,
1030 struct messaging_reclog *log)
1032 struct tevent_req *req, *subreq;
1033 struct notifyd_broadcast_reclog_state *state;
1035 req = tevent_req_create(mem_ctx, &state,
1036 struct notifyd_broadcast_reclog_state);
1041 state->ctdbd_conn = ctdbd_conn;
1045 subreq = tevent_wakeup_send(state, state->ev,
1046 timeval_current_ofs_msec(1000));
1047 if (tevent_req_nomem(subreq, req)) {
1048 return tevent_req_post(req, ev);
1050 tevent_req_set_callback(subreq, notifyd_broadcast_reclog_next, req);
1054 static void notifyd_broadcast_reclog_next(struct tevent_req *subreq)
1056 struct tevent_req *req = tevent_req_callback_data(
1057 subreq, struct tevent_req);
1058 struct notifyd_broadcast_reclog_state *state = tevent_req_data(
1059 req, struct notifyd_broadcast_reclog_state);
1062 ok = tevent_wakeup_recv(subreq);
1063 TALLOC_FREE(subreq);
1065 tevent_req_oom(req);
1069 notifyd_broadcast_reclog(state->ctdbd_conn, state->src, state->log);
1071 subreq = tevent_wakeup_send(state, state->ev,
1072 timeval_current_ofs_msec(1000));
1073 if (tevent_req_nomem(subreq, req)) {
1076 tevent_req_set_callback(subreq, notifyd_broadcast_reclog_next, req);
1079 static int notifyd_broadcast_reclog_recv(struct tevent_req *req)
1081 return tevent_req_simple_recv_unix(req);
1084 struct notifyd_clean_peers_state {
1085 struct tevent_context *ev;
1086 struct notifyd_state *notifyd;
1089 static void notifyd_clean_peers_next(struct tevent_req *subreq);
1091 static struct tevent_req *notifyd_clean_peers_send(
1092 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1093 struct notifyd_state *notifyd)
1095 struct tevent_req *req, *subreq;
1096 struct notifyd_clean_peers_state *state;
1098 req = tevent_req_create(mem_ctx, &state,
1099 struct notifyd_clean_peers_state);
1104 state->notifyd = notifyd;
1106 subreq = tevent_wakeup_send(state, state->ev,
1107 timeval_current_ofs_msec(30000));
1108 if (tevent_req_nomem(subreq, req)) {
1109 return tevent_req_post(req, ev);
1111 tevent_req_set_callback(subreq, notifyd_clean_peers_next, req);
1115 static void notifyd_clean_peers_next(struct tevent_req *subreq)
1117 struct tevent_req *req = tevent_req_callback_data(
1118 subreq, struct tevent_req);
1119 struct notifyd_clean_peers_state *state = tevent_req_data(
1120 req, struct notifyd_clean_peers_state);
1121 struct notifyd_state *notifyd = state->notifyd;
1124 time_t now = time(NULL);
1126 ok = tevent_wakeup_recv(subreq);
1127 TALLOC_FREE(subreq);
1129 tevent_req_oom(req);
1134 while (i < notifyd->num_peers) {
1135 struct notifyd_peer *p = notifyd->peers[i];
1137 if ((now - p->last_broadcast) > 60) {
1138 struct server_id_buf idbuf;
1141 * Haven't heard for more than 60 seconds. Call this
1145 DEBUG(10, ("%s: peer %s died\n", __func__,
1146 server_id_str_buf(p->pid, &idbuf)));
1148 * This implicitly decrements notifyd->num_peers
1156 subreq = tevent_wakeup_send(state, state->ev,
1157 timeval_current_ofs_msec(30000));
1158 if (tevent_req_nomem(subreq, req)) {
1161 tevent_req_set_callback(subreq, notifyd_clean_peers_next, req);
1164 static int notifyd_clean_peers_recv(struct tevent_req *req)
1166 return tevent_req_simple_recv_unix(req);
1169 static int notifyd_add_proxy_syswatches(struct db_record *rec,
1172 struct notifyd_state *state = talloc_get_type_abort(
1173 private_data, struct notifyd_state);
1174 struct db_context *db = dbwrap_record_get_db(rec);
1175 TDB_DATA key = dbwrap_record_get_key(rec);
1176 TDB_DATA value = dbwrap_record_get_value(rec);
1177 struct notifyd_instance *instances = NULL;
1178 size_t num_instances = 0;
1180 char path[key.dsize+1];
1183 memcpy(path, key.dptr, key.dsize);
1184 path[key.dsize] = '\0';
1186 ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
1189 DEBUG(1, ("%s: Could not parse notifyd entry for %s\n",
1194 for (i=0; i<num_instances; i++) {
1195 struct notifyd_instance *instance = &instances[i];
1196 uint32_t filter = instance->instance.filter;
1197 uint32_t subdir_filter = instance->instance.subdir_filter;
1201 * This is a remote database. Pointers that we were
1202 * given don't make sense locally. Initialize to NULL
1203 * in case sys_notify_watch fails.
1205 instances[i].sys_watch = NULL;
1207 ret = state->sys_notify_watch(
1208 db, state->sys_notify_ctx, path,
1209 &filter, &subdir_filter,
1210 notifyd_sys_callback, state->msg_ctx,
1211 &instance->sys_watch);
1213 DEBUG(1, ("%s: inotify_watch returned %s\n",
1214 __func__, strerror(errno)));
1221 static int notifyd_db_del_syswatches(struct db_record *rec, void *private_data)
1223 TDB_DATA key = dbwrap_record_get_key(rec);
1224 TDB_DATA value = dbwrap_record_get_value(rec);
1225 struct notifyd_instance *instances = NULL;
1226 size_t num_instances = 0;
1230 ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
1233 DEBUG(1, ("%s: Could not parse notifyd entry for %.*s\n",
1234 __func__, (int)key.dsize, (char *)key.dptr));
1237 for (i=0; i<num_instances; i++) {
1238 TALLOC_FREE(instances[i].sys_watch);
1243 static int notifyd_peer_destructor(struct notifyd_peer *p)
1245 struct notifyd_state *state = p->state;
1248 if (p->db != NULL) {
1249 dbwrap_traverse_read(p->db, notifyd_db_del_syswatches,
1253 for (i = 0; i<state->num_peers; i++) {
1254 if (p == state->peers[i]) {
1255 state->peers[i] = state->peers[state->num_peers-1];
1256 state->num_peers -= 1;
1263 static struct notifyd_peer *notifyd_peer_new(
1264 struct notifyd_state *state, struct server_id pid)
1266 struct notifyd_peer *p, **tmp;
1268 tmp = talloc_realloc(state, state->peers, struct notifyd_peer *,
1269 state->num_peers+1);
1275 p = talloc_zero(state->peers, struct notifyd_peer);
1282 state->peers[state->num_peers] = p;
1283 state->num_peers += 1;
1285 talloc_set_destructor(p, notifyd_peer_destructor);
1290 static void notifyd_apply_reclog(struct notifyd_peer *peer,
1291 const uint8_t *msg, size_t msglen)
1293 struct notifyd_state *state = peer->state;
1294 DATA_BLOB blob = { .data = discard_const_p(uint8_t, msg),
1296 struct server_id_buf idbuf;
1297 struct messaging_reclog *log;
1298 enum ndr_err_code ndr_err;
1301 if (peer->db == NULL) {
1308 log = talloc(peer, struct messaging_reclog);
1310 DEBUG(10, ("%s: talloc failed\n", __func__));
1314 ndr_err = ndr_pull_struct_blob_all(
1316 (ndr_pull_flags_fn_t)ndr_pull_messaging_reclog);
1317 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1318 DEBUG(10, ("%s: ndr_pull_messaging_reclog failed: %s\n",
1319 __func__, ndr_errstr(ndr_err)));
1323 DEBUG(10, ("%s: Got %u recs index %ju from %s\n", __func__,
1324 (unsigned)log->num_recs, (uintmax_t)log->rec_index,
1325 server_id_str_buf(peer->pid, &idbuf)));
1327 if (log->rec_index != peer->rec_index) {
1328 DEBUG(3, ("%s: Got rec index %ju from %s, expected %ju\n",
1329 __func__, (uintmax_t)log->rec_index,
1330 server_id_str_buf(peer->pid, &idbuf),
1331 (uintmax_t)peer->rec_index));
1335 for (i=0; i<log->num_recs; i++) {
1336 struct messaging_rec *r = log->recs[i];
1337 struct notify_rec_change_msg *chg;
1341 ok = notifyd_parse_rec_change(r->buf.data, r->buf.length,
1344 DEBUG(3, ("%s: notifyd_parse_rec_change failed\n",
1349 ok = notifyd_apply_rec_change(&r->src, chg->path, pathlen,
1350 &chg->instance, peer->db,
1351 state->sys_notify_watch,
1352 state->sys_notify_ctx,
1355 DEBUG(3, ("%s: notifyd_apply_rec_change failed\n",
1361 peer->rec_index += 1;
1362 peer->last_broadcast = time(NULL);
1368 DEBUG(10, ("%s: Dropping peer %s\n", __func__,
1369 server_id_str_buf(peer->pid, &idbuf)));
1374 * Receive messaging_reclog (log of MSG_SMB_NOTIFY_REC_CHANGE
1375 * messages) broadcasts by other notifyds. Several cases:
1377 * We don't know the source. This creates a new peer. Creating a peer
1378 * involves asking the peer for its full database. We assume ordered
1379 * messages, so the new database will arrive before the next broadcast
1382 * We know the source and the log index matches. We will apply the log
1383 * locally to our peer's db as if we had received it from a local
1386 * We know the source but the log index does not match. This means we
1387 * lost a message. We just drop the whole peer and wait for the next
1388 * broadcast, which will then trigger a fresh database pull.
1391 static int notifyd_snoop_broadcast(struct tevent_context *ev,
1392 uint32_t src_vnn, uint32_t dst_vnn,
1394 const uint8_t *msg, size_t msglen,
1397 struct notifyd_state *state = talloc_get_type_abort(
1398 private_data, struct notifyd_state);
1399 struct server_id my_id = messaging_server_id(state->msg_ctx);
1400 struct notifyd_peer *p;
1403 struct server_id src, dst;
1404 struct server_id_buf idbuf;
1407 if (msglen < MESSAGE_HDR_LENGTH) {
1408 DEBUG(10, ("%s: Got short broadcast\n", __func__));
1411 message_hdr_get(&msg_type, &src, &dst, msg);
1413 if (msg_type != MSG_SMB_NOTIFY_REC_CHANGES) {
1414 DEBUG(10, ("%s Got message %u, ignoring\n", __func__,
1415 (unsigned)msg_type));
1418 if (server_id_equal(&src, &my_id)) {
1419 DEBUG(10, ("%s: Ignoring my own broadcast\n", __func__));
1423 DEBUG(10, ("%s: Got MSG_SMB_NOTIFY_REC_CHANGES from %s\n",
1424 __func__, server_id_str_buf(src, &idbuf)));
1426 for (i=0; i<state->num_peers; i++) {
1427 if (server_id_equal(&state->peers[i]->pid, &src)) {
1429 DEBUG(10, ("%s: Applying changes to peer %u\n",
1430 __func__, (unsigned)i));
1432 notifyd_apply_reclog(state->peers[i],
1433 msg + MESSAGE_HDR_LENGTH,
1434 msglen - MESSAGE_HDR_LENGTH);
1439 DEBUG(10, ("%s: Creating new peer for %s\n", __func__,
1440 server_id_str_buf(src, &idbuf)));
1442 p = notifyd_peer_new(state, src);
1444 DEBUG(10, ("%s: notifyd_peer_new failed\n", __func__));
1448 status = messaging_send_buf(state->msg_ctx, src, MSG_SMB_NOTIFY_GET_DB,
1450 if (!NT_STATUS_IS_OK(status)) {
1451 DEBUG(10, ("%s: messaging_send_buf failed: %s\n",
1452 __func__, nt_errstr(status)));
1461 struct notifyd_parse_db_state {
1462 bool (*fn)(const char *path,
1463 struct server_id server,
1464 const struct notify_instance *instance,
1465 void *private_data);
1469 static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
1472 struct notifyd_parse_db_state *state = private_data;
1473 char path[key.dsize+1];
1474 struct notifyd_instance *instances = NULL;
1475 size_t num_instances = 0;
1479 memcpy(path, key.dptr, key.dsize);
1480 path[key.dsize] = 0;
1482 ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
1485 DEBUG(10, ("%s: Could not parse entry for path %s\n",
1490 for (i=0; i<num_instances; i++) {
1491 ok = state->fn(path, instances[i].client,
1492 &instances[i].instance,
1493 state->private_data);
1502 int notifyd_parse_db(const uint8_t *buf, size_t buflen,
1503 uint64_t *log_index,
1504 bool (*fn)(const char *path,
1505 struct server_id server,
1506 const struct notify_instance *instance,
1507 void *private_data),
1510 struct notifyd_parse_db_state state = {
1511 .fn = fn, .private_data = private_data
1518 *log_index = BVAL(buf, 0);
1523 status = dbwrap_parse_marshall_buf(
1524 buf, buflen, notifyd_parse_db_parser, &state);
1525 if (!NT_STATUS_IS_OK(status)) {
1526 return map_errno_from_nt_status(status);