tests: Fix calling of ctdb tool from test
[ctdb.git] / common / ctdb_message.c
1 /* 
2    ctdb_message protocol code
3
4    Copyright (C) Andrew Tridgell  2007
5    Copyright (C) Amitay Isaacs  2013
6
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.
11    
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.
16    
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/>.
19 */
20 /*
21   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
22   protocol design and packet details
23 */
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"
30
31 static int message_list_db_init(struct ctdb_context *ctdb)
32 {
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;
41         }
42
43         return 0;
44 }
45
46 static int message_list_db_add(struct ctdb_context *ctdb, uint64_t srvid,
47                                struct ctdb_message_list_header *h)
48 {
49         int ret;
50         TDB_DATA key, data;
51
52         if (ctdb->message_list_indexdb == NULL) {
53                 ret = message_list_db_init(ctdb);
54                 if (ret < 0) {
55                         return -1;
56                 }
57         }
58
59         key.dptr = (uint8_t *)&srvid;
60         key.dsize = sizeof(uint64_t);
61
62         data.dptr = (uint8_t *)&h;
63         data.dsize = sizeof(struct ctdb_message_list_header *);
64
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;
70         }
71
72         return 0;
73 }
74
75 static int message_list_db_delete(struct ctdb_context *ctdb, uint64_t srvid)
76 {
77         int ret;
78         TDB_DATA key;
79
80         if (ctdb->message_list_indexdb == NULL) {
81                 return -1;
82         }
83
84         key.dptr = (uint8_t *)&srvid;
85         key.dsize = sizeof(uint64_t);
86
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;
92         }
93
94         return 0;
95 }
96
97 static int message_list_db_fetch_parser(TDB_DATA key, TDB_DATA data,
98                                         void *private_data)
99 {
100         struct ctdb_message_list_header **h =
101                 (struct ctdb_message_list_header **)private_data;
102
103         if (data.dsize != sizeof(struct ctdb_message_list_header *)) {
104                 return -1;
105         }
106
107         *h = *(struct ctdb_message_list_header **)data.dptr;
108         return 0;
109 }
110
111 static int message_list_db_fetch(struct ctdb_context *ctdb, uint64_t srvid,
112                                  struct ctdb_message_list_header **h)
113 {
114         TDB_DATA key;
115
116         if (ctdb->message_list_indexdb == NULL) {
117                 return -1;
118         }
119
120         key.dptr = (uint8_t *)&srvid;
121         key.dsize = sizeof(uint64_t);
122
123         return tdb_parse_record(ctdb->message_list_indexdb, key,
124                                 message_list_db_fetch_parser, h);
125 }
126
127 /*
128   this dispatches the messages to the registered ctdb message handler
129 */
130 int ctdb_dispatch_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
131 {
132         struct ctdb_message_list_header *h;
133         struct ctdb_message_list *m;
134         uint64_t srvid_all = CTDB_SRVID_ALL;
135         int ret;
136
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);
141                 }
142         }
143
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);
148                 }
149         }
150
151         return 0;
152 }
153
154 /*
155   called when a CTDB_REQ_MESSAGE packet comes in
156 */
157 void ctdb_request_message(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
158 {
159         struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
160         TDB_DATA data;
161
162         data.dsize = c->datalen;
163         data.dptr = talloc_memdup(c, &c->data[0], c->datalen);
164
165         ctdb_dispatch_message(ctdb, c->srvid, data);
166 }
167
168 /*
169  * When header is freed, remove all the srvid handlers
170  */
171 static int message_header_destructor(struct ctdb_message_list_header *h)
172 {
173         struct ctdb_message_list *m;
174
175         while (h->m != NULL) {
176                 m = h->m;
177                 DLIST_REMOVE(h->m, m);
178                 TALLOC_FREE(m);
179         }
180
181         message_list_db_delete(h->ctdb, h->srvid);
182         DLIST_REMOVE(h->ctdb->message_list_header, h);
183
184         return 0;
185 }
186
187 /*
188   when a client goes away, we need to remove its srvid handler from the list
189  */
190 static int message_handler_destructor(struct ctdb_message_list *m)
191 {
192         struct ctdb_message_list_header *h = m->h;
193
194         DLIST_REMOVE(h->m, m);
195         if (h->m == NULL) {
196                 talloc_free(h);
197         }
198         return 0;
199 }
200
201 /*
202   setup handler for receipt of ctdb messages from ctdb_send_message()
203 */
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)
209 {
210         struct ctdb_message_list_header *h;
211         struct ctdb_message_list *m;
212         int ret;
213
214         m = talloc_zero(mem_ctx, struct ctdb_message_list);
215         CTDB_NO_MEMORY(ctdb, m);
216
217         m->message_handler = handler;
218         m->message_private = private_data;
219
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);
225
226                 h->ctdb = ctdb;
227                 h->srvid = srvid;
228
229                 ret = message_list_db_add(ctdb, srvid, h);
230                 if (ret < 0) {
231                         talloc_free(m);
232                         talloc_free(h);
233                         return -1;
234                 }
235
236                 DLIST_ADD(ctdb->message_list_header, h);
237                 talloc_set_destructor(h, message_header_destructor);
238         }
239
240         m->h = h;
241         DLIST_ADD(h->m, m);
242         talloc_set_destructor(m, message_handler_destructor);
243         return 0;
244 }
245
246
247 /*
248   setup handler for receipt of ctdb messages from ctdb_send_message()
249 */
250 int ctdb_deregister_message_handler(struct ctdb_context *ctdb, uint64_t srvid, void *private_data)
251 {
252         struct ctdb_message_list_header *h;
253         struct ctdb_message_list *m;
254         int ret;
255
256         ret = message_list_db_fetch(ctdb, srvid, &h);
257         if (ret != 0) {
258                 return -1;
259         }
260
261         for (m=h->m; m; m=m->next) {
262                 if (m->message_private == private_data) {
263                         talloc_free(m);
264                         return 0;
265                 }
266         }
267
268         return -1;
269 }
270
271
272 /*
273  * check if the given srvid exists
274  */
275 bool ctdb_check_message_handler(struct ctdb_context *ctdb, uint64_t srvid)
276 {
277         struct ctdb_message_list_header *h;
278         int ret;
279
280         ret = message_list_db_fetch(ctdb, srvid, &h);
281         if (ret != 0 || h->m == NULL) {
282                 return false;
283         }
284
285         return true;
286 }