ctdb-client: Add client API for sending message to multiple nodes
[kai/samba-autobuild/.git] / ctdb / client / client_connect.c
1 /*
2    CTDB client code
3
4    Copyright (C) Amitay Isaacs  2015
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
23
24 #include <talloc.h>
25 #include <tevent.h>
26 #include <tdb.h>
27
28 #include "common/reqid.h"
29 #include "common/srvid.h"
30 #include "common/comm.h"
31 #include "common/logging.h"
32
33 #include "lib/util/tevent_unix.h"
34 #include "lib/util/debug.h"
35
36 #include "protocol/protocol.h"
37 #include "protocol/protocol_api.h"
38
39 #include "client/client_private.h"
40 #include "client/client.h"
41
42 static int ctdb_client_connect(struct ctdb_client_context *client,
43                                struct tevent_context *ev,
44                                const char *sockpath);
45
46 static int ctdb_client_context_destructor(struct ctdb_client_context *client);
47
48 int ctdb_client_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
49                      const char *sockpath, struct ctdb_client_context **out)
50 {
51         struct ctdb_client_context *client;
52         int ret;
53
54         client = talloc_zero(mem_ctx, struct ctdb_client_context);
55         if (client == NULL) {
56                 DEBUG(DEBUG_ERR, (__location__ " memory allocation error\n"));
57                 return ENOMEM;
58         }
59
60         ret = reqid_init(client, INT_MAX-200, &client->idr);
61         if (ret != 0) {
62                 DEBUG(DEBUG_ERR, ("reqid_init() failed, ret=%d\n", ret));
63                 talloc_free(client);
64                 return ret;
65         }
66
67         ret = srvid_init(client, &client->srv);
68         if (ret != 0) {
69                 DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret));
70                 talloc_free(client);
71                 return ret;
72         }
73
74         client->fd = -1;
75         client->pnn = CTDB_UNKNOWN_PNN;
76
77         ret = ctdb_client_connect(client, ev, sockpath);
78         if (ret != 0) {
79                 talloc_free(client);
80                 return ret;
81         }
82
83         talloc_set_destructor(client, ctdb_client_context_destructor);
84
85         *out = client;
86         return 0;
87 }
88
89 static int ctdb_client_context_destructor(struct ctdb_client_context *client)
90 {
91         if (client->fd != -1) {
92                 close(client->fd);
93                 client->fd = -1;
94         }
95         return 0;
96 }
97
98 static void client_read_handler(uint8_t *buf, size_t buflen,
99                                 void *private_data);
100 static void client_dead_handler(void *private_data);
101
102 static int ctdb_client_connect(struct ctdb_client_context *client,
103                                struct tevent_context *ev, const char *sockpath)
104 {
105         struct sockaddr_un addr;
106         size_t len;
107         int fd, ret;
108
109         if (sockpath == NULL) {
110                 DEBUG(DEBUG_ERR, ("socket path cannot be NULL\n"));
111                 return EINVAL;
112         }
113
114         memset(&addr, 0, sizeof(addr));
115         addr.sun_family = AF_UNIX;
116         len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
117         if (len != strlen(sockpath)) {
118                 DEBUG(DEBUG_ERR, ("socket path too long, len=%zu\n",
119                                   strlen(sockpath)));
120                 return ENAMETOOLONG;
121         }
122
123         fd = socket(AF_UNIX, SOCK_STREAM, 0);
124         if (fd == -1) {
125                 ret = errno;
126                 DEBUG(DEBUG_ERR, ("socket() failed, errno=%d\n", ret));
127                 return ret;
128         }
129
130         ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
131         if (ret == -1) {
132                 ret = errno;
133                 DEBUG(DEBUG_ERR, ("connect() failed, errno=%d\n", ret));
134                 close(fd);
135                 return ret;
136         }
137         client->fd = fd;
138
139         ret = comm_setup(client, ev, fd, client_read_handler, client,
140                          client_dead_handler, client, &client->comm);
141         if (ret != 0) {
142                 DEBUG(DEBUG_ERR, ("comm_setup() failed, ret=%d\n", ret));
143                 close(fd);
144                 client->fd = -1;
145                 return ret;
146         }
147
148         ret = ctdb_ctrl_get_pnn(client, ev, client, CTDB_CURRENT_NODE,
149                                 tevent_timeval_zero(), &client->pnn);
150         if (ret != 0) {
151                 DEBUG(DEBUG_ERR, ("failed to get current node pnn\n"));
152                 close(fd);
153                 client->fd = -1;
154                 TALLOC_FREE(client->comm);
155                 return ret;
156         }
157
158         return 0;
159 }
160
161 static void client_read_handler(uint8_t *buf, size_t buflen,
162                                 void *private_data)
163 {
164         struct ctdb_client_context *client = talloc_get_type_abort(
165                 private_data, struct ctdb_client_context);
166         struct ctdb_req_header hdr;
167         int ret;
168
169         ret = ctdb_req_header_pull(discard_const(buf), buflen, &hdr);
170         if (ret != 0) {
171                 DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret));
172                 return;
173         }
174
175         if (buflen != hdr.length) {
176                 DEBUG(DEBUG_WARNING, ("packet size mismatch %zu != %d\n",
177                                       buflen, hdr.length));
178                 return;
179         }
180
181         ret = ctdb_req_header_verify(&hdr, 0);
182         if (ret != 0) {
183                 DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret));
184                 return;
185         }
186
187         switch (hdr.operation) {
188         case CTDB_REPLY_CALL:
189                 ctdb_client_reply_call(client, buf, buflen, hdr.reqid);
190                 break;
191
192         case CTDB_REQ_MESSAGE:
193                 ctdb_client_req_message(client, buf, buflen, hdr.reqid);
194                 break;
195
196         case CTDB_REPLY_CONTROL:
197                 ctdb_client_reply_control(client, buf, buflen, hdr.reqid);
198                 break;
199
200         default:
201                 break;
202         }
203 }
204
205 static void client_dead_handler(void *private_data)
206 {
207         struct ctdb_client_context *client = talloc_get_type_abort(
208                 private_data, struct ctdb_client_context);
209         ctdb_client_callback_func_t callback = client->callback;
210         void *callback_data = client->private_data;
211
212         talloc_free(client);
213         if (callback != NULL) {
214                 callback(callback_data);
215                 return;
216         }
217
218         DEBUG(DEBUG_NOTICE, ("connection to daemon closed, exiting\n"));
219         exit(1);
220 }
221
222 void ctdb_client_set_disconnect_callback(struct ctdb_client_context *client,
223                                          ctdb_client_callback_func_t callback,
224                                          void *private_data)
225 {
226         client->callback = callback;
227         client->private_data = private_data;
228 }
229
230 uint32_t ctdb_client_pnn(struct ctdb_client_context *client)
231 {
232         return client->pnn;
233 }
234
235 void ctdb_client_wait(struct tevent_context *ev, bool *done)
236 {
237         while (! (*done)) {
238                 tevent_loop_once(ev);
239         }
240 }
241
242 static void ctdb_client_wait_timeout_handler(struct tevent_context *ev,
243                                              struct tevent_timer *te,
244                                              struct timeval t,
245                                              void *private_data)
246 {
247         bool *timed_out = (bool *)private_data;
248
249         *timed_out = true;
250 }
251
252 int ctdb_client_wait_timeout(struct tevent_context *ev, bool *done,
253                              struct timeval timeout)
254 {
255         TALLOC_CTX *mem_ctx;
256         struct tevent_timer *timer;
257         bool timed_out = false;
258
259         mem_ctx = talloc_new(ev);
260         if (mem_ctx == NULL) {
261                 return ENOMEM;
262         }
263
264         timer = tevent_add_timer(ev, mem_ctx, timeout,
265                                  ctdb_client_wait_timeout_handler,
266                                  &timed_out);
267         if (timer == NULL) {
268                 talloc_free(mem_ctx);
269                 return ENOMEM;
270         }
271
272         while (! (*done) && ! timed_out) {
273                 tevent_loop_once(ev);
274         }
275
276         talloc_free(mem_ctx);
277
278         if (timed_out) {
279                 return ETIME;
280         }
281
282         return 0;
283 }
284
285 struct ctdb_recovery_wait_state {
286         struct tevent_context *ev;
287         struct ctdb_client_context *client;
288 };
289
290 static void ctdb_recovery_wait_recmode(struct tevent_req *subreq);
291 static void ctdb_recovery_wait_retry(struct tevent_req *subreq);
292
293 struct tevent_req *ctdb_recovery_wait_send(TALLOC_CTX *mem_ctx,
294                                            struct tevent_context *ev,
295                                            struct ctdb_client_context *client)
296 {
297         struct tevent_req *req, *subreq;
298         struct ctdb_recovery_wait_state *state;
299         struct ctdb_req_control request;
300
301         req = tevent_req_create(mem_ctx, &state,
302                                 struct ctdb_recovery_wait_state);
303         if (req == NULL) {
304                 return NULL;
305         }
306
307         state->ev = ev;
308         state->client = client;
309
310         ctdb_req_control_get_recmode(&request);
311         subreq = ctdb_client_control_send(state, ev, client, client->pnn,
312                                           tevent_timeval_zero(), &request);
313         if (tevent_req_nomem(subreq, req)) {
314                 return tevent_req_post(req, ev);
315         }
316         tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req);
317
318         return req;
319 }
320
321 static void ctdb_recovery_wait_recmode(struct tevent_req *subreq)
322 {
323         struct tevent_req *req = tevent_req_callback_data(
324                 subreq, struct tevent_req);
325         struct ctdb_recovery_wait_state *state = tevent_req_data(
326                 req, struct ctdb_recovery_wait_state);
327         struct ctdb_reply_control *reply;
328         int recmode;
329         int ret;
330         bool status;
331
332         status = ctdb_client_control_recv(subreq, &ret, state, &reply);
333         TALLOC_FREE(subreq);
334         if (! status) {
335                 tevent_req_error(req, ret);
336                 return;
337         }
338
339         ret = ctdb_reply_control_get_recmode(reply, &recmode);
340         if (ret != 0) {
341                 tevent_req_error(req, ret);
342                 return;
343         }
344
345         if (recmode == CTDB_RECOVERY_NORMAL) {
346                 tevent_req_done(req);
347                 return;
348         }
349
350         subreq = tevent_wakeup_send(state, state->ev,
351                                     tevent_timeval_current_ofs(1, 0));
352         if (tevent_req_nomem(subreq, req)) {
353                 return;
354         }
355         tevent_req_set_callback(subreq, ctdb_recovery_wait_retry, req);
356 }
357
358 static void ctdb_recovery_wait_retry(struct tevent_req *subreq)
359 {
360         struct tevent_req *req = tevent_req_callback_data(
361                 subreq, struct tevent_req);
362         struct ctdb_recovery_wait_state *state = tevent_req_data(
363                 req, struct ctdb_recovery_wait_state);
364         struct ctdb_req_control request;
365         bool status;
366
367         status = tevent_wakeup_recv(subreq);
368         TALLOC_FREE(subreq);
369         if (! status) {
370                 tevent_req_error(req, ENOMEM);
371                 return;
372         }
373
374         ctdb_req_control_get_recmode(&request);
375         subreq = ctdb_client_control_send(state, state->ev, state->client,
376                                           state->client->pnn,
377                                           tevent_timeval_zero(), &request);
378         if (tevent_req_nomem(subreq, req)) {
379                 return;
380         }
381         tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req);
382 }
383
384 bool ctdb_recovery_wait_recv(struct tevent_req *req, int *perr)
385 {
386         int err;
387
388         if (tevent_req_is_unix_error(req, &err)) {
389                 if (perr != NULL) {
390                         *perr = err;
391                 }
392                 return false;
393         }
394
395         return true;
396 }
397
398 bool ctdb_recovery_wait(struct tevent_context *ev,
399                         struct ctdb_client_context *client)
400 {
401         TALLOC_CTX *mem_ctx;
402         struct tevent_req *req;
403         bool status;
404
405         mem_ctx = talloc_new(client);
406         if (mem_ctx == NULL) {
407                 return false;
408         }
409
410         req = ctdb_recovery_wait_send(mem_ctx, ev, client);
411         if (req == NULL) {
412                 return false;
413         }
414
415         tevent_req_poll(req, ev);
416
417         status = ctdb_recovery_wait_recv(req, NULL);
418
419         talloc_free(mem_ctx);
420         return status;
421 }