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