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/>.
21 #include "librpc/gen_ndr/notify.h"
22 #include "librpc/gen_ndr/messaging.h"
23 #include "librpc/gen_ndr/server_id.h"
24 #include "lib/dbwrap/dbwrap.h"
25 #include "lib/dbwrap/dbwrap_rbt.h"
31 #include "lib/util/server_id_db.h"
32 #include "lib/util/tevent_unix.h"
33 #include "ctdbd_conn.h"
34 #include "ctdb_srvids.h"
35 #include "source3/smbd/proto.h"
36 #include "server_id_db_util.h"
37 #include "lib/util/iov_buf.h"
38 #include "messages_util.h"
40 #ifdef CLUSTER_SUPPORT
41 #include "ctdb_protocol.h"
47 * All of notifyd's state
50 struct notifyd_state {
51 struct tevent_context *ev;
52 struct messaging_context *msg_ctx;
53 struct ctdbd_connection *ctdbd_conn;
56 * Database of everything clients show interest in. Indexed by
57 * absolute path. The database keys are not 0-terminated
58 * because the criticial operation, notifyd_trigger, can walk
59 * the structure from the top without adding intermediate 0s.
60 * The database records contain an array of
62 * struct notifyd_instance
64 * to be maintained by parsed by notifyd_entry_parse()
66 struct db_context *entries;
69 * In the cluster case, this is the place where we store a log
70 * of all MSG_SMB_NOTIFY_REC_CHANGE messages. We just 1:1
71 * forward them to our peer notifyd's in the cluster once a
72 * second or when the log grows too large.
75 struct messaging_reclog *log;
78 * Array of companion notifyd's in a cluster. Every notifyd
79 * broadcasts its messaging_reclog to every other notifyd in
80 * the cluster. This is done by making ctdb send a message to
81 * srvid CTDB_SRVID_SAMBA_NOTIFY_PROXY with destination node
82 * number CTDB_BROADCAST_VNNMAP. Everybody in the cluster who
83 * had called register_with_ctdbd this srvid will receive the
86 * Database replication happens via these broadcasts. Also,
87 * they serve as liveness indication. If a notifyd receives a
88 * broadcast from an unknown peer, it will create one for this
89 * srvid. Also when we don't hear anything from a peer for a
90 * while, we will discard it.
93 struct notifyd_peer **peers;
96 sys_notify_watch_fn sys_notify_watch;
97 struct sys_notify_context *sys_notify_ctx;
101 * notifyd's representation of a notify instance
103 struct notifyd_instance {
104 struct server_id client;
105 struct notify_instance instance;
107 void *sys_watch; /* inotify/fam/etc handle */
110 * Filters after sys_watch took responsibility of some bits
112 uint32_t internal_filter;
113 uint32_t internal_subdir_filter;
116 struct notifyd_peer {
117 struct notifyd_state *state;
118 struct server_id pid;
120 struct db_context *db;
121 time_t last_broadcast;
124 static bool notifyd_rec_change(struct messaging_context *msg_ctx,
125 struct messaging_rec **prec,
127 static bool notifyd_trigger(struct messaging_context *msg_ctx,
128 struct messaging_rec **prec,
130 static bool notifyd_get_db(struct messaging_context *msg_ctx,
131 struct messaging_rec **prec,
133 static bool notifyd_got_db(struct messaging_context *msg_ctx,
134 struct messaging_rec **prec,
137 #ifdef CLUSTER_SUPPORT
138 static void notifyd_broadcast_reclog(struct ctdbd_connection *ctdbd_conn,
139 struct server_id src,
140 struct messaging_reclog *log);
142 static void notifyd_sys_callback(struct sys_notify_context *ctx,
143 void *private_data, struct notify_event *ev);
145 #ifdef CLUSTER_SUPPORT
146 static struct tevent_req *notifyd_broadcast_reclog_send(
147 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
148 struct ctdbd_connection *ctdbd_conn, struct server_id src,
149 struct messaging_reclog *log);
150 static int notifyd_broadcast_reclog_recv(struct tevent_req *req);
152 static struct tevent_req *notifyd_clean_peers_send(
153 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
154 struct notifyd_state *notifyd);
155 static int notifyd_clean_peers_recv(struct tevent_req *req);
158 static int sys_notify_watch_dummy(
160 struct sys_notify_context *ctx,
163 uint32_t *subdir_filter,
164 void (*callback)(struct sys_notify_context *ctx,
166 struct notify_event *ev),
170 void **handle = handle_p;
175 static void notifyd_handler_done(struct tevent_req *subreq);
177 #ifdef CLUSTER_SUPPORT
178 static void notifyd_broadcast_reclog_finished(struct tevent_req *subreq);
179 static void notifyd_clean_peers_finished(struct tevent_req *subreq);
180 static int notifyd_snoop_broadcast(uint32_t src_vnn, uint32_t dst_vnn,
182 const uint8_t *msg, size_t msglen,
186 struct tevent_req *notifyd_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
187 struct messaging_context *msg_ctx,
188 struct ctdbd_connection *ctdbd_conn,
189 sys_notify_watch_fn sys_notify_watch,
190 struct sys_notify_context *sys_notify_ctx)
192 struct tevent_req *req, *subreq;
193 struct notifyd_state *state;
194 struct server_id_db *names_db;
197 req = tevent_req_create(mem_ctx, &state, struct notifyd_state);
202 state->msg_ctx = msg_ctx;
203 state->ctdbd_conn = ctdbd_conn;
205 if (sys_notify_watch == NULL) {
206 sys_notify_watch = sys_notify_watch_dummy;
209 state->sys_notify_watch = sys_notify_watch;
210 state->sys_notify_ctx = sys_notify_ctx;
212 state->entries = db_open_rbt(state);
213 if (tevent_req_nomem(state->entries, req)) {
214 return tevent_req_post(req, ev);
217 subreq = messaging_handler_send(state, ev, msg_ctx,
218 MSG_SMB_NOTIFY_REC_CHANGE,
219 notifyd_rec_change, state);
220 if (tevent_req_nomem(subreq, req)) {
221 return tevent_req_post(req, ev);
223 tevent_req_set_callback(subreq, notifyd_handler_done, req);
225 subreq = messaging_handler_send(state, ev, msg_ctx,
226 MSG_SMB_NOTIFY_TRIGGER,
227 notifyd_trigger, state);
228 if (tevent_req_nomem(subreq, req)) {
229 return tevent_req_post(req, ev);
231 tevent_req_set_callback(subreq, notifyd_handler_done, req);
233 subreq = messaging_handler_send(state, ev, msg_ctx,
234 MSG_SMB_NOTIFY_GET_DB,
235 notifyd_get_db, state);
236 if (tevent_req_nomem(subreq, req)) {
237 return tevent_req_post(req, ev);
239 tevent_req_set_callback(subreq, notifyd_handler_done, req);
241 subreq = messaging_handler_send(state, ev, msg_ctx,
243 notifyd_got_db, state);
244 if (tevent_req_nomem(subreq, req)) {
245 return tevent_req_post(req, ev);
247 tevent_req_set_callback(subreq, notifyd_handler_done, req);
249 names_db = messaging_names_db(msg_ctx);
251 ret = server_id_db_set_exclusive(names_db, "notify-daemon");
253 DEBUG(10, ("%s: server_id_db_add failed: %s\n",
254 __func__, strerror(ret)));
255 tevent_req_error(req, ret);
256 return tevent_req_post(req, ev);
259 /* Block those signals that we are not handling */
260 BlockSignals(True, SIGHUP);
261 BlockSignals(True, SIGUSR1);
263 if (ctdbd_conn == NULL) {
265 * No cluster around, skip the database replication
271 #ifdef CLUSTER_SUPPORT
272 state->log = talloc_zero(state, struct messaging_reclog);
273 if (tevent_req_nomem(state->log, req)) {
274 return tevent_req_post(req, ev);
277 subreq = notifyd_broadcast_reclog_send(
278 state->log, ev, ctdbd_conn, messaging_server_id(msg_ctx),
280 if (tevent_req_nomem(subreq, req)) {
281 return tevent_req_post(req, ev);
283 tevent_req_set_callback(subreq, notifyd_broadcast_reclog_finished,
286 subreq = notifyd_clean_peers_send(state, ev, state);
287 if (tevent_req_nomem(subreq, req)) {
288 return tevent_req_post(req, ev);
290 tevent_req_set_callback(subreq, notifyd_clean_peers_finished,
293 ret = register_with_ctdbd(ctdbd_conn, CTDB_SRVID_SAMBA_NOTIFY_PROXY,
294 notifyd_snoop_broadcast, state);
296 tevent_req_error(req, ret);
297 return tevent_req_post(req, ev);
304 static void notifyd_handler_done(struct tevent_req *subreq)
306 struct tevent_req *req = tevent_req_callback_data(
307 subreq, struct tevent_req);
310 ret = messaging_handler_recv(subreq);
312 tevent_req_error(req, ret);
315 #ifdef CLUSTER_SUPPORT
317 static void notifyd_broadcast_reclog_finished(struct tevent_req *subreq)
319 struct tevent_req *req = tevent_req_callback_data(
320 subreq, struct tevent_req);
323 ret = notifyd_broadcast_reclog_recv(subreq);
325 tevent_req_error(req, ret);
328 static void notifyd_clean_peers_finished(struct tevent_req *subreq)
330 struct tevent_req *req = tevent_req_callback_data(
331 subreq, struct tevent_req);
334 ret = notifyd_clean_peers_recv(subreq);
336 tevent_req_error(req, ret);
341 int notifyd_recv(struct tevent_req *req)
343 return tevent_req_simple_recv_unix(req);
347 * Parse an entry in the notifyd_context->entries database
350 static bool notifyd_parse_entry(uint8_t *buf, size_t buflen,
351 struct notifyd_instance **instances,
352 size_t *num_instances)
354 if ((buflen % sizeof(struct notifyd_instance)) != 0) {
355 DEBUG(1, ("%s: invalid buffer size: %u\n",
356 __func__, (unsigned)buflen));
360 if (instances != NULL) {
361 *instances = (struct notifyd_instance *)buf;
363 if (num_instances != NULL) {
364 *num_instances = buflen / sizeof(struct notifyd_instance);
369 static bool notifyd_apply_rec_change(
370 const struct server_id *client,
371 const char *path, size_t pathlen,
372 const struct notify_instance *chg,
373 struct db_context *entries,
374 sys_notify_watch_fn sys_notify_watch,
375 struct sys_notify_context *sys_notify_ctx,
376 struct messaging_context *msg_ctx)
378 struct db_record *rec;
379 struct notifyd_instance *instances;
380 size_t num_instances;
382 struct notifyd_instance *instance;
388 DEBUG(1, ("%s: pathlen==0\n", __func__));
391 if (path[pathlen-1] != '\0') {
392 DEBUG(1, ("%s: path not 0-terminated\n", __func__));
396 DEBUG(10, ("%s: path=%s, filter=%u, subdir_filter=%u, "
397 "private_data=%p\n", __func__, path,
398 (unsigned)chg->filter, (unsigned)chg->subdir_filter,
401 rec = dbwrap_fetch_locked(
403 make_tdb_data((const uint8_t *)path, pathlen-1));
406 DEBUG(1, ("%s: dbwrap_fetch_locked failed\n", __func__));
411 value = dbwrap_record_get_value(rec);
413 if (value.dsize != 0) {
414 if (!notifyd_parse_entry(value.dptr, value.dsize, NULL,
421 * Overallocate by one instance to avoid a realloc when adding
423 instances = talloc_array(rec, struct notifyd_instance,
425 if (instances == NULL) {
426 DEBUG(1, ("%s: talloc failed\n", __func__));
430 if (value.dsize != 0) {
431 memcpy(instances, value.dptr, value.dsize);
434 for (i=0; i<num_instances; i++) {
435 instance = &instances[i];
437 if (server_id_equal(&instance->client, client) &&
438 (instance->instance.private_data == chg->private_data)) {
443 if (i < num_instances) {
444 instance->instance = *chg;
447 * We've overallocated for one instance
449 instance = &instances[num_instances];
451 *instance = (struct notifyd_instance) {
454 .internal_filter = chg->filter,
455 .internal_subdir_filter = chg->subdir_filter
461 if ((instance->instance.filter != 0) ||
462 (instance->instance.subdir_filter != 0)) {
465 TALLOC_FREE(instance->sys_watch);
467 ret = sys_notify_watch(entries, sys_notify_ctx, path,
468 &instance->internal_filter,
469 &instance->internal_subdir_filter,
470 notifyd_sys_callback, msg_ctx,
471 &instance->sys_watch);
473 DEBUG(1, ("%s: inotify_watch returned %s\n",
474 __func__, strerror(errno)));
478 if ((instance->instance.filter == 0) &&
479 (instance->instance.subdir_filter == 0)) {
480 /* This is a delete request */
481 TALLOC_FREE(instance->sys_watch);
482 *instance = instances[num_instances-1];
486 DEBUG(10, ("%s: %s has %u instances\n", __func__,
487 path, (unsigned)num_instances));
489 if (num_instances == 0) {
490 status = dbwrap_record_delete(rec);
491 if (!NT_STATUS_IS_OK(status)) {
492 DEBUG(1, ("%s: dbwrap_record_delete returned %s\n",
493 __func__, nt_errstr(status)));
497 value = make_tdb_data(
498 (uint8_t *)instances,
499 sizeof(struct notifyd_instance) * num_instances);
501 status = dbwrap_record_store(rec, value, 0);
502 if (!NT_STATUS_IS_OK(status)) {
503 DEBUG(1, ("%s: dbwrap_record_store returned %s\n",
504 __func__, nt_errstr(status)));
515 static void notifyd_sys_callback(struct sys_notify_context *ctx,
516 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 bool notifyd_rec_change(struct messaging_context *msg_ctx,
569 struct messaging_rec **prec,
572 struct notifyd_state *state = talloc_get_type_abort(
573 private_data, struct notifyd_state);
574 struct server_id_buf idbuf;
575 struct messaging_rec *rec = *prec;
576 struct notify_rec_change_msg *msg;
580 DEBUG(10, ("%s: Got %d bytes from %s\n", __func__,
581 (unsigned)rec->buf.length,
582 server_id_str_buf(rec->src, &idbuf)));
584 ok = notifyd_parse_rec_change(rec->buf.data, rec->buf.length,
590 ok = notifyd_apply_rec_change(
591 &rec->src, msg->path, pathlen, &msg->instance,
592 state->entries, state->sys_notify_watch, state->sys_notify_ctx,
595 DEBUG(1, ("%s: notifyd_apply_rec_change failed, ignoring\n",
600 if ((state->log == NULL) || (state->ctdbd_conn == NULL)) {
604 #ifdef CLUSTER_SUPPORT
607 struct messaging_rec **tmp;
608 struct messaging_reclog *log;
612 tmp = talloc_realloc(log, log->recs, struct messaging_rec *,
615 DEBUG(1, ("%s: talloc_realloc failed, ignoring\n", __func__));
620 log->recs[log->num_recs] = talloc_move(log->recs, prec);
623 if (log->num_recs >= 100) {
625 * Don't let the log grow too large
627 notifyd_broadcast_reclog(state->ctdbd_conn,
628 messaging_server_id(msg_ctx), log);
637 struct notifyd_trigger_state {
638 struct messaging_context *msg_ctx;
639 struct notify_trigger_msg *msg;
641 bool covered_by_sys_notify;
644 static void notifyd_trigger_parser(TDB_DATA key, TDB_DATA data,
647 static bool notifyd_trigger(struct messaging_context *msg_ctx,
648 struct messaging_rec **prec,
651 struct notifyd_state *state = talloc_get_type_abort(
652 private_data, struct notifyd_state);
653 struct server_id my_id = messaging_server_id(msg_ctx);
654 struct messaging_rec *rec = *prec;
655 struct notifyd_trigger_state tstate;
657 const char *p, *next_p;
659 if (rec->buf.length < offsetof(struct notify_trigger_msg, path) + 1) {
660 DEBUG(1, ("message too short, ignoring: %u\n",
661 (unsigned)rec->buf.length));
664 if (rec->buf.data[rec->buf.length-1] != 0) {
665 DEBUG(1, ("%s: path not 0-terminated, ignoring\n", __func__));
669 tstate.msg_ctx = msg_ctx;
671 tstate.covered_by_sys_notify = (rec->src.vnn == my_id.vnn);
672 tstate.covered_by_sys_notify &= !server_id_equal(&rec->src, &my_id);
674 tstate.msg = (struct notify_trigger_msg *)rec->buf.data;
675 path = tstate.msg->path;
677 DEBUG(10, ("%s: Got trigger_msg action=%u, filter=%u, path=%s\n",
678 __func__, (unsigned)tstate.msg->action,
679 (unsigned)tstate.msg->filter, path));
681 if (path[0] != '/') {
682 DEBUG(1, ("%s: path %s does not start with /, ignoring\n",
687 for (p = strchr(path+1, '/'); p != NULL; p = next_p) {
688 ptrdiff_t path_len = p - path;
692 next_p = strchr(p+1, '/');
693 tstate.recursive = (next_p != NULL);
695 DEBUG(10, ("%s: Trying path %.*s\n", __func__,
696 (int)path_len, path));
698 key = (TDB_DATA) { .dptr = discard_const_p(uint8_t, path),
701 dbwrap_parse_record(state->entries, key,
702 notifyd_trigger_parser, &tstate);
704 if (state->peers == NULL) {
708 if (rec->src.vnn != my_id.vnn) {
712 for (i=0; i<state->num_peers; i++) {
713 if (state->peers[i]->db == NULL) {
715 * Inactive peer, did not get a db yet
719 dbwrap_parse_record(state->peers[i]->db, key,
720 notifyd_trigger_parser, &tstate);
727 static void notifyd_send_delete(struct messaging_context *msg_ctx,
729 struct notifyd_instance *instance);
731 static void notifyd_trigger_parser(TDB_DATA key, TDB_DATA data,
735 struct notifyd_trigger_state *tstate = private_data;
736 struct notify_event_msg msg = { .action = tstate->msg->action };
738 size_t path_len = key.dsize;
739 struct notifyd_instance *instances = NULL;
740 size_t num_instances = 0;
743 if (!notifyd_parse_entry(data.dptr, data.dsize, &instances,
745 DEBUG(1, ("%s: Could not parse notifyd_entry\n", __func__));
749 DEBUG(10, ("%s: Found %u instances for %.*s\n", __func__,
750 (unsigned)num_instances, (int)key.dsize,
753 iov[0].iov_base = &msg;
754 iov[0].iov_len = offsetof(struct notify_event_msg, path);
755 iov[1].iov_base = tstate->msg->path + path_len + 1;
756 iov[1].iov_len = strlen((char *)(iov[1].iov_base)) + 1;
758 for (i=0; i<num_instances; i++) {
759 struct notifyd_instance *instance = &instances[i];
760 struct server_id_buf idbuf;
764 if (tstate->covered_by_sys_notify) {
765 if (tstate->recursive) {
766 i_filter = instance->internal_subdir_filter;
768 i_filter = instance->internal_filter;
771 if (tstate->recursive) {
772 i_filter = instance->instance.subdir_filter;
774 i_filter = instance->instance.filter;
778 if ((i_filter & tstate->msg->filter) == 0) {
782 msg.private_data = instance->instance.private_data;
784 status = messaging_send_iov(
785 tstate->msg_ctx, instance->client,
786 MSG_PVFS_NOTIFY, iov, ARRAY_SIZE(iov), NULL, 0);
788 DEBUG(10, ("%s: messaging_send_iov to %s returned %s\n",
790 server_id_str_buf(instance->client, &idbuf),
793 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
794 procid_is_local(&instance->client)) {
796 * That process has died
798 notifyd_send_delete(tstate->msg_ctx, key, instance);
802 if (!NT_STATUS_IS_OK(status)) {
803 DEBUG(1, ("%s: messaging_send_iov returned %s\n",
804 __func__, nt_errstr(status)));
810 * Send a delete request to ourselves to properly discard a notify
811 * record for an smbd that has died.
814 static void notifyd_send_delete(struct messaging_context *msg_ctx,
816 struct notifyd_instance *instance)
818 struct notify_rec_change_msg msg = {
819 .instance.private_data = instance->instance.private_data
826 * Send a rec_change to ourselves to delete a dead entry
829 iov[0] = (struct iovec) {
831 .iov_len = offsetof(struct notify_rec_change_msg, path) };
832 iov[1] = (struct iovec) { .iov_base = key.dptr, .iov_len = key.dsize };
833 iov[2] = (struct iovec) { .iov_base = &nul, .iov_len = sizeof(nul) };
835 ret = messaging_send_iov_from(
836 msg_ctx, instance->client, messaging_server_id(msg_ctx),
837 MSG_SMB_NOTIFY_REC_CHANGE, iov, ARRAY_SIZE(iov), NULL, 0);
840 DEBUG(10, ("%s: messaging_send_iov_from returned %s\n",
841 __func__, strerror(ret)));
845 static bool notifyd_get_db(struct messaging_context *msg_ctx,
846 struct messaging_rec **prec,
849 struct notifyd_state *state = talloc_get_type_abort(
850 private_data, struct notifyd_state);
851 struct messaging_rec *rec = *prec;
852 struct server_id_buf id1, id2;
854 uint64_t rec_index = UINT64_MAX;
855 uint8_t index_buf[sizeof(uint64_t)];
860 dbsize = dbwrap_marshall(state->entries, NULL, 0);
862 buf = talloc_array(rec, uint8_t, dbsize);
864 DEBUG(1, ("%s: talloc_array(%ju) failed\n",
865 __func__, (uintmax_t)dbsize));
869 dbsize = dbwrap_marshall(state->entries, buf, dbsize);
871 if (dbsize != talloc_get_size(buf)) {
872 DEBUG(1, ("%s: dbsize changed: %ju->%ju\n", __func__,
873 (uintmax_t)talloc_get_size(buf),
879 if (state->log != NULL) {
880 rec_index = state->log->rec_index;
882 SBVAL(index_buf, 0, rec_index);
884 iov[0] = (struct iovec) { .iov_base = index_buf,
885 .iov_len = sizeof(index_buf) };
886 iov[1] = (struct iovec) { .iov_base = buf,
889 DEBUG(10, ("%s: Sending %ju bytes to %s->%s\n", __func__,
890 (uintmax_t)iov_buflen(iov, ARRAY_SIZE(iov)),
891 server_id_str_buf(messaging_server_id(msg_ctx), &id1),
892 server_id_str_buf(rec->src, &id2)));
894 status = messaging_send_iov(msg_ctx, rec->src, MSG_SMB_NOTIFY_DB,
895 iov, ARRAY_SIZE(iov), NULL, 0);
897 if (!NT_STATUS_IS_OK(status)) {
898 DEBUG(1, ("%s: messaging_send_iov failed: %s\n",
899 __func__, nt_errstr(status)));
905 static int notifyd_add_proxy_syswatches(struct db_record *rec,
908 static bool notifyd_got_db(struct messaging_context *msg_ctx,
909 struct messaging_rec **prec,
912 struct notifyd_state *state = talloc_get_type_abort(
913 private_data, struct notifyd_state);
914 struct messaging_rec *rec = *prec;
915 struct notifyd_peer *p = NULL;
916 struct server_id_buf idbuf;
921 for (i=0; i<state->num_peers; i++) {
922 if (server_id_equal(&rec->src, &state->peers[i]->pid)) {
929 DEBUG(10, ("%s: Did not find peer for db from %s\n",
930 __func__, server_id_str_buf(rec->src, &idbuf)));
934 if (rec->buf.length < 8) {
935 DEBUG(10, ("%s: Got short db length %u from %s\n", __func__,
936 (unsigned)rec->buf.length,
937 server_id_str_buf(rec->src, &idbuf)));
942 p->rec_index = BVAL(rec->buf.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, rec->buf.data + 8,
952 rec->buf.length - 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(rec->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(rec->src, &idbuf), count));
970 #ifdef CLUSTER_SUPPORT
972 static void notifyd_broadcast_reclog(struct ctdbd_connection *ctdbd_conn,
973 struct server_id src,
974 struct messaging_reclog *log)
976 enum ndr_err_code ndr_err;
977 uint8_t msghdr[MESSAGE_HDR_LENGTH];
986 DEBUG(10, ("%s: rec_index=%ju, num_recs=%u\n", __func__,
987 (uintmax_t)log->rec_index, (unsigned)log->num_recs));
989 message_hdr_put(msghdr, MSG_SMB_NOTIFY_REC_CHANGES, src,
990 (struct server_id) {0 });
991 iov[0] = (struct iovec) { .iov_base = msghdr,
992 .iov_len = sizeof(msghdr) };
994 ndr_err = ndr_push_struct_blob(
996 (ndr_push_flags_fn_t)ndr_push_messaging_reclog);
997 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
998 DEBUG(1, ("%s: ndr_push_messaging_recs failed: %s\n",
999 __func__, ndr_errstr(ndr_err)));
1002 iov[1] = (struct iovec) { .iov_base = blob.data,
1003 .iov_len = blob.length };
1005 ret = ctdbd_messaging_send_iov(
1006 ctdbd_conn, CTDB_BROADCAST_VNNMAP,
1007 CTDB_SRVID_SAMBA_NOTIFY_PROXY, iov, ARRAY_SIZE(iov));
1008 TALLOC_FREE(blob.data);
1010 DEBUG(1, ("%s: ctdbd_messaging_send failed: %s\n",
1011 __func__, strerror(ret)));
1015 log->rec_index += 1;
1019 TALLOC_FREE(log->recs);
1022 struct notifyd_broadcast_reclog_state {
1023 struct tevent_context *ev;
1024 struct ctdbd_connection *ctdbd_conn;
1025 struct server_id src;
1026 struct messaging_reclog *log;
1029 static void notifyd_broadcast_reclog_next(struct tevent_req *subreq);
1031 static struct tevent_req *notifyd_broadcast_reclog_send(
1032 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1033 struct ctdbd_connection *ctdbd_conn, struct server_id src,
1034 struct messaging_reclog *log)
1036 struct tevent_req *req, *subreq;
1037 struct notifyd_broadcast_reclog_state *state;
1039 req = tevent_req_create(mem_ctx, &state,
1040 struct notifyd_broadcast_reclog_state);
1045 state->ctdbd_conn = ctdbd_conn;
1049 subreq = tevent_wakeup_send(state, state->ev,
1050 timeval_current_ofs_msec(1000));
1051 if (tevent_req_nomem(subreq, req)) {
1052 return tevent_req_post(req, ev);
1054 tevent_req_set_callback(subreq, notifyd_broadcast_reclog_next, req);
1058 static void notifyd_broadcast_reclog_next(struct tevent_req *subreq)
1060 struct tevent_req *req = tevent_req_callback_data(
1061 subreq, struct tevent_req);
1062 struct notifyd_broadcast_reclog_state *state = tevent_req_data(
1063 req, struct notifyd_broadcast_reclog_state);
1066 ok = tevent_wakeup_recv(subreq);
1067 TALLOC_FREE(subreq);
1069 tevent_req_oom(req);
1073 notifyd_broadcast_reclog(state->ctdbd_conn, state->src, state->log);
1075 subreq = tevent_wakeup_send(state, state->ev,
1076 timeval_current_ofs_msec(1000));
1077 if (tevent_req_nomem(subreq, req)) {
1080 tevent_req_set_callback(subreq, notifyd_broadcast_reclog_next, req);
1083 static int notifyd_broadcast_reclog_recv(struct tevent_req *req)
1085 return tevent_req_simple_recv_unix(req);
1088 struct notifyd_clean_peers_state {
1089 struct tevent_context *ev;
1090 struct notifyd_state *notifyd;
1093 static void notifyd_clean_peers_next(struct tevent_req *subreq);
1095 static struct tevent_req *notifyd_clean_peers_send(
1096 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1097 struct notifyd_state *notifyd)
1099 struct tevent_req *req, *subreq;
1100 struct notifyd_clean_peers_state *state;
1102 req = tevent_req_create(mem_ctx, &state,
1103 struct notifyd_clean_peers_state);
1108 state->notifyd = notifyd;
1110 subreq = tevent_wakeup_send(state, state->ev,
1111 timeval_current_ofs_msec(30000));
1112 if (tevent_req_nomem(subreq, req)) {
1113 return tevent_req_post(req, ev);
1115 tevent_req_set_callback(subreq, notifyd_clean_peers_next, req);
1119 static void notifyd_clean_peers_next(struct tevent_req *subreq)
1121 struct tevent_req *req = tevent_req_callback_data(
1122 subreq, struct tevent_req);
1123 struct notifyd_clean_peers_state *state = tevent_req_data(
1124 req, struct notifyd_clean_peers_state);
1125 struct notifyd_state *notifyd = state->notifyd;
1128 time_t now = time(NULL);
1130 ok = tevent_wakeup_recv(subreq);
1131 TALLOC_FREE(subreq);
1133 tevent_req_oom(req);
1138 while (i < notifyd->num_peers) {
1139 struct notifyd_peer *p = notifyd->peers[i];
1141 if ((now - p->last_broadcast) > 60) {
1142 struct server_id_buf idbuf;
1145 * Haven't heard for more than 60 seconds. Call this
1149 DEBUG(10, ("%s: peer %s died\n", __func__,
1150 server_id_str_buf(p->pid, &idbuf)));
1152 * This implicitly decrements notifyd->num_peers
1160 subreq = tevent_wakeup_send(state, state->ev,
1161 timeval_current_ofs_msec(30000));
1162 if (tevent_req_nomem(subreq, req)) {
1165 tevent_req_set_callback(subreq, notifyd_clean_peers_next, req);
1168 static int notifyd_clean_peers_recv(struct tevent_req *req)
1170 return tevent_req_simple_recv_unix(req);
1175 static int notifyd_add_proxy_syswatches(struct db_record *rec,
1178 struct notifyd_state *state = talloc_get_type_abort(
1179 private_data, struct notifyd_state);
1180 struct db_context *db = dbwrap_record_get_db(rec);
1181 TDB_DATA key = dbwrap_record_get_key(rec);
1182 TDB_DATA value = dbwrap_record_get_value(rec);
1183 struct notifyd_instance *instances = NULL;
1184 size_t num_instances = 0;
1186 char path[key.dsize+1];
1189 memcpy(path, key.dptr, key.dsize);
1190 path[key.dsize] = '\0';
1192 ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
1195 DEBUG(1, ("%s: Could not parse notifyd entry for %s\n",
1200 for (i=0; i<num_instances; i++) {
1201 struct notifyd_instance *instance = &instances[i];
1202 uint32_t filter = instance->instance.filter;
1203 uint32_t subdir_filter = instance->instance.subdir_filter;
1206 ret = state->sys_notify_watch(
1207 db, state->sys_notify_ctx, path,
1208 &filter, &subdir_filter,
1209 notifyd_sys_callback, state->msg_ctx,
1210 &instance->sys_watch);
1212 DEBUG(1, ("%s: inotify_watch returned %s\n",
1213 __func__, strerror(errno)));
1220 #ifdef CLUSTER_SUPPORT
1222 static int notifyd_db_del_syswatches(struct db_record *rec, void *private_data)
1224 TDB_DATA key = dbwrap_record_get_key(rec);
1225 TDB_DATA value = dbwrap_record_get_value(rec);
1226 struct notifyd_instance *instances = NULL;
1227 size_t num_instances = 0;
1231 ok = notifyd_parse_entry(value.dptr, value.dsize, &instances,
1234 DEBUG(1, ("%s: Could not parse notifyd entry for %.*s\n",
1235 __func__, (int)key.dsize, (char *)key.dptr));
1238 for (i=0; i<num_instances; i++) {
1239 TALLOC_FREE(instances[i].sys_watch);
1244 static int notifyd_peer_destructor(struct notifyd_peer *p)
1246 struct notifyd_state *state = p->state;
1249 if (p->db != NULL) {
1250 dbwrap_traverse_read(p->db, notifyd_db_del_syswatches,
1254 for (i = 0; i<state->num_peers; i++) {
1255 if (p == state->peers[i]) {
1256 state->peers[i] = state->peers[state->num_peers-1];
1257 state->num_peers -= 1;
1264 static struct notifyd_peer *notifyd_peer_new(
1265 struct notifyd_state *state, struct server_id pid)
1267 struct notifyd_peer *p, **tmp;
1269 tmp = talloc_realloc(state, state->peers, struct notifyd_peer *,
1270 state->num_peers+1);
1276 p = talloc_zero(state->peers, struct notifyd_peer);
1283 state->peers[state->num_peers] = p;
1284 state->num_peers += 1;
1286 talloc_set_destructor(p, notifyd_peer_destructor);
1291 static void notifyd_apply_reclog(struct notifyd_peer *peer,
1292 const uint8_t *msg, size_t msglen)
1294 struct notifyd_state *state = peer->state;
1295 DATA_BLOB blob = { .data = discard_const_p(uint8_t, msg),
1297 struct server_id_buf idbuf;
1298 struct messaging_reclog *log;
1299 enum ndr_err_code ndr_err;
1302 if (peer->db == NULL) {
1309 log = talloc(peer, struct messaging_reclog);
1311 DEBUG(10, ("%s: talloc failed\n", __func__));
1315 ndr_err = ndr_pull_struct_blob_all(
1317 (ndr_pull_flags_fn_t)ndr_pull_messaging_reclog);
1318 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1319 DEBUG(10, ("%s: ndr_pull_messaging_reclog failed: %s\n",
1320 __func__, ndr_errstr(ndr_err)));
1324 DEBUG(10, ("%s: Got %u recs index %ju from %s\n", __func__,
1325 (unsigned)log->num_recs, (uintmax_t)log->rec_index,
1326 server_id_str_buf(peer->pid, &idbuf)));
1328 if (log->rec_index != peer->rec_index) {
1329 DEBUG(3, ("%s: Got rec index %ju from %s, expected %ju\n",
1330 __func__, (uintmax_t)log->rec_index,
1331 server_id_str_buf(peer->pid, &idbuf),
1332 (uintmax_t)peer->rec_index));
1336 for (i=0; i<log->num_recs; i++) {
1337 struct messaging_rec *r = log->recs[i];
1338 struct notify_rec_change_msg *chg;
1342 ok = notifyd_parse_rec_change(r->buf.data, r->buf.length,
1345 DEBUG(3, ("%s: notifyd_parse_rec_change failed\n",
1350 ok = notifyd_apply_rec_change(&r->src, chg->path, pathlen,
1351 &chg->instance, peer->db,
1352 state->sys_notify_watch,
1353 state->sys_notify_ctx,
1356 DEBUG(3, ("%s: notifyd_apply_rec_change failed\n",
1362 peer->rec_index += 1;
1363 peer->last_broadcast = time(NULL);
1369 DEBUG(10, ("%s: Dropping peer %s\n", __func__,
1370 server_id_str_buf(peer->pid, &idbuf)));
1375 * Receive messaging_reclog (log of MSG_SMB_NOTIFY_REC_CHANGE
1376 * messages) broadcasts by other notifyds. Several cases:
1378 * We don't know the source. This creates a new peer. Creating a peer
1379 * involves asking the peer for its full database. We assume ordered
1380 * messages, so the new database will arrive before the next broadcast
1383 * We know the source and the log index matches. We will apply the log
1384 * locally to our peer's db as if we had received it from a local
1387 * We know the source but the log index does not match. This means we
1388 * lost a message. We just drop the whole peer and wait for the next
1389 * broadcast, which will then trigger a fresh database pull.
1392 static int notifyd_snoop_broadcast(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);