torture: convert torture_comment() -> torture_result() so we can knownfail flapping...
[Samba/wip.git] / ctdb / common / ctdb_message.c
blob0e19761da3d47d8c1efaeb1689f04e76b28dcec3
1 /*
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
24 #include "includes.h"
25 #include "tdb.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28 #include "../include/ctdb_private.h"
29 #include "lib/util/dlinklist.h"
31 static int message_list_db_init(struct ctdb_context *ctdb)
33 ctdb->message_list_indexdb = tdb_open("messagedb", 8192,
34 TDB_INTERNAL|
35 TDB_INCOMPATIBLE_HASH|
36 TDB_DISALLOW_NESTING,
37 O_RDWR|O_CREAT, 0);
38 if (ctdb->message_list_indexdb == NULL) {
39 DEBUG(DEBUG_ERR, ("Failed to create message list indexdb\n"));
40 return -1;
43 return 0;
46 static int message_list_db_add(struct ctdb_context *ctdb, uint64_t srvid,
47 struct ctdb_message_list_header *h)
49 int ret;
50 TDB_DATA key, data;
52 if (ctdb->message_list_indexdb == NULL) {
53 ret = message_list_db_init(ctdb);
54 if (ret < 0) {
55 return -1;
59 key.dptr = (uint8_t *)&srvid;
60 key.dsize = sizeof(uint64_t);
62 data.dptr = (uint8_t *)&h;
63 data.dsize = sizeof(struct ctdb_message_list_header *);
65 ret = tdb_store(ctdb->message_list_indexdb, key, data, TDB_INSERT);
66 if (ret < 0) {
67 DEBUG(DEBUG_ERR, ("Failed to add message list handler (%s)\n",
68 tdb_errorstr(ctdb->message_list_indexdb)));
69 return -1;
72 return 0;
75 static int message_list_db_delete(struct ctdb_context *ctdb, uint64_t srvid)
77 int ret;
78 TDB_DATA key;
80 if (ctdb->message_list_indexdb == NULL) {
81 return -1;
84 key.dptr = (uint8_t *)&srvid;
85 key.dsize = sizeof(uint64_t);
87 ret = tdb_delete(ctdb->message_list_indexdb, key);
88 if (ret < 0) {
89 DEBUG(DEBUG_ERR, ("Failed to delete message list handler (%s)\n",
90 tdb_errorstr(ctdb->message_list_indexdb)));
91 return -1;
94 return 0;
97 static int message_list_db_fetch_parser(TDB_DATA key, TDB_DATA data,
98 void *private_data)
100 struct ctdb_message_list_header **h =
101 (struct ctdb_message_list_header **)private_data;
103 if (data.dsize != sizeof(struct ctdb_message_list_header *)) {
104 return -1;
107 *h = *(struct ctdb_message_list_header **)data.dptr;
108 return 0;
111 static int message_list_db_fetch(struct ctdb_context *ctdb, uint64_t srvid,
112 struct ctdb_message_list_header **h)
114 TDB_DATA key;
116 if (ctdb->message_list_indexdb == NULL) {
117 return -1;
120 key.dptr = (uint8_t *)&srvid;
121 key.dsize = sizeof(uint64_t);
123 return tdb_parse_record(ctdb->message_list_indexdb, key,
124 message_list_db_fetch_parser, h);
128 this dispatches the messages to the registered ctdb message handler
130 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
132 struct ctdb_message_list_header *h;
133 struct ctdb_message_list *m;
134 uint64_t srvid_all = CTDB_SRVID_ALL;
135 int ret;
137 ret = message_list_db_fetch(ctdb, srvid, &h);
138 if (ret == 0) {
139 for (m=h->m; m; m=m->next) {
140 m->message_handler(ctdb, srvid, data, m->message_private);
144 ret = message_list_db_fetch(ctdb, srvid_all, &h);
145 if (ret == 0) {
146 for(m=h->m; m; m=m->next) {
147 m->message_handler(ctdb, srvid, data, m->message_private);
151 return 0;
155 called when a CTDB_REQ_MESSAGE packet comes in
157 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
159 struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
160 TDB_DATA data;
162 data.dsize = c->datalen;
163 data.dptr = talloc_memdup(c, &c->data[0], c->datalen);
165 ctdb_dispatch_message(ctdb, c->srvid, data);
169 * When header is freed, remove all the srvid handlers
171 static int message_header_destructor(struct ctdb_message_list_header *h)
173 struct ctdb_message_list *m;
175 while (h->m != NULL) {
176 m = h->m;
177 DLIST_REMOVE(h->m, m);
178 TALLOC_FREE(m);
181 message_list_db_delete(h->ctdb, h->srvid);
182 DLIST_REMOVE(h->ctdb->message_list_header, h);
184 return 0;
188 when a client goes away, we need to remove its srvid handler from the list
190 static int message_handler_destructor(struct ctdb_message_list *m)
192 struct ctdb_message_list_header *h = m->h;
194 DLIST_REMOVE(h->m, m);
195 if (h->m == NULL) {
196 talloc_free(h);
198 return 0;
202 setup handler for receipt of ctdb messages from ctdb_send_message()
204 int ctdb_register_message_handler(struct ctdb_context *ctdb,
205 TALLOC_CTX *mem_ctx,
206 uint64_t srvid,
207 ctdb_msg_fn_t handler,
208 void *private_data)
210 struct ctdb_message_list_header *h;
211 struct ctdb_message_list *m;
212 int ret;
214 m = talloc_zero(mem_ctx, struct ctdb_message_list);
215 CTDB_NO_MEMORY(ctdb, m);
217 m->message_handler = handler;
218 m->message_private = private_data;
220 ret = message_list_db_fetch(ctdb, srvid, &h);
221 if (ret != 0) {
222 /* srvid not registered yet */
223 h = talloc_zero(ctdb, struct ctdb_message_list_header);
224 CTDB_NO_MEMORY(ctdb, h);
226 h->ctdb = ctdb;
227 h->srvid = srvid;
229 ret = message_list_db_add(ctdb, srvid, h);
230 if (ret < 0) {
231 talloc_free(m);
232 talloc_free(h);
233 return -1;
236 DLIST_ADD(ctdb->message_list_header, h);
237 talloc_set_destructor(h, message_header_destructor);
240 m->h = h;
241 DLIST_ADD(h->m, m);
242 talloc_set_destructor(m, message_handler_destructor);
243 return 0;
248 setup handler for receipt of ctdb messages from ctdb_send_message()
250 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data)
252 struct ctdb_message_list_header *h;
253 struct ctdb_message_list *m;
254 int ret;
256 ret = message_list_db_fetch(ctdb, srvid, &h);
257 if (ret != 0) {
258 return -1;
261 for (m=h->m; m; m=m->next) {
262 if (m->message_private == private_data) {
263 talloc_free(m);
264 return 0;
268 return -1;
273 * check if the given srvid exists
275 bool ctdb_check_message_handler(struct ctdb_context *ctdb, uint64_t srvid)
277 struct ctdb_message_list_header *h;
278 int ret;
280 ret = message_list_db_fetch(ctdb, srvid, &h);
281 if (ret != 0 || h->m == NULL) {
282 return false;
285 return true;