2 ctdb_message protocol code
4 Copyright (C) Andrew Tridgell 2007
5 Copyright (C) Amitay Isaacs 2013
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 see http://wiki.samba.org/index.php/Samba_%26_Clustering for
22 protocol design and packet details
25 #include "lib/tevent/tevent.h"
26 #include "lib/tdb/include/tdb.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "../include/ctdb_private.h"
30 #include "lib/util/dlinklist.h"
32 static int message_list_db_init(struct ctdb_context *ctdb)
34 ctdb->message_list_indexdb = tdb_open("messagedb", 8192,
35 TDB_INTERNAL|TDB_DISALLOW_NESTING,
37 if (ctdb->message_list_indexdb == NULL) {
38 DEBUG(DEBUG_ERR, ("Failed to create message list indexdb\n"));
45 static int message_list_db_add(struct ctdb_context *ctdb, uint64_t srvid,
46 struct ctdb_message_list_header *h)
51 if (ctdb->message_list_indexdb == NULL) {
52 ret = message_list_db_init(ctdb);
58 key.dptr = (uint8_t *)&srvid;
59 key.dsize = sizeof(uint64_t);
61 data.dptr = (uint8_t *)&h;
62 data.dsize = sizeof(struct ctdb_message_list_header *);
64 ret = tdb_store(ctdb->message_list_indexdb, key, data, TDB_INSERT);
66 DEBUG(DEBUG_ERR, ("Failed to add message list handler (%s)\n",
67 tdb_errorstr(ctdb->message_list_indexdb)));
74 static int message_list_db_delete(struct ctdb_context *ctdb, uint64_t srvid)
79 if (ctdb->message_list_indexdb == NULL) {
83 key.dptr = (uint8_t *)&srvid;
84 key.dsize = sizeof(uint64_t);
86 ret = tdb_delete(ctdb->message_list_indexdb, key);
88 DEBUG(DEBUG_ERR, ("Failed to delete message list handler (%s)\n",
89 tdb_errorstr(ctdb->message_list_indexdb)));
96 static int message_list_db_fetch(struct ctdb_context *ctdb, uint64_t srvid,
97 struct ctdb_message_list_header **h)
101 if (ctdb->message_list_indexdb == NULL) {
105 key.dptr = (uint8_t *)&srvid;
106 key.dsize = sizeof(uint64_t);
108 data = tdb_fetch(ctdb->message_list_indexdb, key);
109 if (data.dsize != sizeof(struct ctdb_message_list_header *)) {
110 talloc_free(data.dptr);
114 *h = *(struct ctdb_message_list_header **)data.dptr;
115 talloc_free(data.dptr);
121 this dispatches the messages to the registered ctdb message handler
123 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
125 struct ctdb_message_list_header *h;
126 struct ctdb_message_list *m;
127 uint64_t srvid_all = CTDB_SRVID_ALL;
130 ret = message_list_db_fetch(ctdb, srvid, &h);
132 for (m=h->m; m; m=m->next) {
133 m->message_handler(ctdb, srvid, data, m->message_private);
137 ret = message_list_db_fetch(ctdb, srvid_all, &h);
139 for(m=h->m; m; m=m->next) {
140 m->message_handler(ctdb, srvid, data, m->message_private);
148 called when a CTDB_REQ_MESSAGE packet comes in
150 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
152 struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
155 data.dsize = c->datalen;
156 data.dptr = talloc_memdup(c, &c->data[0], c->datalen);
158 ctdb_dispatch_message(ctdb, c->srvid, data);
162 * When header is freed, remove all the srvid handlers
164 static int message_header_destructor(struct ctdb_message_list_header *h)
166 struct ctdb_message_list *m;
168 while (h->m != NULL) {
170 DLIST_REMOVE(h->m, m);
174 message_list_db_delete(h->ctdb, h->srvid);
175 DLIST_REMOVE(h->ctdb->message_list_header, h);
181 when a client goes away, we need to remove its srvid handler from the list
183 static int message_handler_destructor(struct ctdb_message_list *m)
185 struct ctdb_message_list_header *h = m->h;
187 DLIST_REMOVE(h->m, m);
195 setup handler for receipt of ctdb messages from ctdb_send_message()
197 int ctdb_register_message_handler(struct ctdb_context *ctdb,
200 ctdb_msg_fn_t handler,
203 struct ctdb_message_list_header *h;
204 struct ctdb_message_list *m;
207 m = talloc_zero(mem_ctx, struct ctdb_message_list);
208 CTDB_NO_MEMORY(ctdb, m);
210 m->message_handler = handler;
211 m->message_private = private_data;
213 ret = message_list_db_fetch(ctdb, srvid, &h);
215 /* srvid not registered yet */
216 h = talloc_zero(ctdb, struct ctdb_message_list_header);
217 CTDB_NO_MEMORY(ctdb, h);
222 ret = message_list_db_add(ctdb, srvid, h);
229 DLIST_ADD(ctdb->message_list_header, h);
230 talloc_set_destructor(h, message_header_destructor);
235 talloc_set_destructor(m, message_handler_destructor);
241 setup handler for receipt of ctdb messages from ctdb_send_message()
243 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data)
245 struct ctdb_message_list_header *h;
246 struct ctdb_message_list *m;
249 ret = message_list_db_fetch(ctdb, srvid, &h);
254 for (m=h->m; m; m=m->next) {
255 if (m->message_private == private_data) {
266 * check if the given srvid exists
268 bool ctdb_check_message_handler(struct ctdb_context *ctdb, uint64_t srvid)
270 struct ctdb_message_list_header *h;
273 ret = message_list_db_fetch(ctdb, srvid, &h);
274 if (ret != 0 || h->m == NULL) {