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
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,
35 TDB_INCOMPATIBLE_HASH
|
38 if (ctdb
->message_list_indexdb
== NULL
) {
39 DEBUG(DEBUG_ERR
, ("Failed to create message list indexdb\n"));
46 static int message_list_db_add(struct ctdb_context
*ctdb
, uint64_t srvid
,
47 struct ctdb_message_list_header
*h
)
52 if (ctdb
->message_list_indexdb
== NULL
) {
53 ret
= message_list_db_init(ctdb
);
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
);
67 DEBUG(DEBUG_ERR
, ("Failed to add message list handler (%s)\n",
68 tdb_errorstr(ctdb
->message_list_indexdb
)));
75 static int message_list_db_delete(struct ctdb_context
*ctdb
, uint64_t srvid
)
80 if (ctdb
->message_list_indexdb
== NULL
) {
84 key
.dptr
= (uint8_t *)&srvid
;
85 key
.dsize
= sizeof(uint64_t);
87 ret
= tdb_delete(ctdb
->message_list_indexdb
, key
);
89 DEBUG(DEBUG_ERR
, ("Failed to delete message list handler (%s)\n",
90 tdb_errorstr(ctdb
->message_list_indexdb
)));
97 static int message_list_db_fetch_parser(TDB_DATA key
, TDB_DATA 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
*)) {
107 *h
= *(struct ctdb_message_list_header
**)data
.dptr
;
111 static int message_list_db_fetch(struct ctdb_context
*ctdb
, uint64_t srvid
,
112 struct ctdb_message_list_header
**h
)
116 if (ctdb
->message_list_indexdb
== NULL
) {
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
;
137 ret
= message_list_db_fetch(ctdb
, srvid
, &h
);
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
);
146 for(m
=h
->m
; m
; m
=m
->next
) {
147 m
->message_handler(ctdb
, srvid
, data
, m
->message_private
);
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
;
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
) {
177 DLIST_REMOVE(h
->m
, m
);
181 message_list_db_delete(h
->ctdb
, h
->srvid
);
182 DLIST_REMOVE(h
->ctdb
->message_list_header
, h
);
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
);
202 setup handler for receipt of ctdb messages from ctdb_send_message()
204 int ctdb_register_message_handler(struct ctdb_context
*ctdb
,
207 ctdb_msg_fn_t handler
,
210 struct ctdb_message_list_header
*h
;
211 struct ctdb_message_list
*m
;
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
);
222 /* srvid not registered yet */
223 h
= talloc_zero(ctdb
, struct ctdb_message_list_header
);
224 CTDB_NO_MEMORY(ctdb
, h
);
229 ret
= message_list_db_add(ctdb
, srvid
, h
);
236 DLIST_ADD(ctdb
->message_list_header
, h
);
237 talloc_set_destructor(h
, message_header_destructor
);
242 talloc_set_destructor(m
, message_handler_destructor
);
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
;
256 ret
= message_list_db_fetch(ctdb
, srvid
, &h
);
261 for (m
=h
->m
; m
; m
=m
->next
) {
262 if (m
->message_private
== private_data
) {
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
;
280 ret
= message_list_db_fetch(ctdb
, srvid
, &h
);
281 if (ret
!= 0 || h
->m
== NULL
) {