4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
21 #include "system/network.h"
22 #include "system/filesys.h"
28 #include "common/reqid.h"
29 #include "common/srvid.h"
30 #include "common/comm.h"
31 #include "common/logging.h"
33 #include "lib/util/tevent_unix.h"
34 #include "lib/util/debug.h"
36 #include "protocol/protocol.h"
37 #include "protocol/protocol_api.h"
39 #include "client/client_private.h"
40 #include "client/client.h"
41 #include "client/client_sync.h"
43 static int ctdb_client_connect(struct ctdb_client_context *client,
44 struct tevent_context *ev,
45 const char *sockpath);
47 static int ctdb_client_context_destructor(struct ctdb_client_context *client);
49 int ctdb_client_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
50 const char *sockpath, struct ctdb_client_context **out)
52 struct ctdb_client_context *client;
55 client = talloc_zero(mem_ctx, struct ctdb_client_context);
57 DEBUG(DEBUG_ERR, (__location__ " memory allocation error\n"));
61 ret = reqid_init(client, INT_MAX-200, &client->idr);
63 DEBUG(DEBUG_ERR, ("reqid_init() failed, ret=%d\n", ret));
68 ret = srvid_init(client, &client->srv);
70 DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret));
75 ret = srvid_init(client, &client->tunnels);
77 DEBUG(DEBUG_ERR, ("srvid_init() failed, ret=%d\n", ret));
83 client->pnn = CTDB_UNKNOWN_PNN;
85 ret = ctdb_client_connect(client, ev, sockpath);
91 talloc_set_destructor(client, ctdb_client_context_destructor);
97 static int ctdb_client_context_destructor(struct ctdb_client_context *client)
99 if (client->fd != -1) {
106 static void client_read_handler(uint8_t *buf, size_t buflen,
108 static void client_dead_handler(void *private_data);
110 static int ctdb_client_connect(struct ctdb_client_context *client,
111 struct tevent_context *ev, const char *sockpath)
113 struct sockaddr_un addr;
117 if (sockpath == NULL) {
118 DEBUG(DEBUG_ERR, ("socket path cannot be NULL\n"));
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",
131 fd = socket(AF_UNIX, SOCK_STREAM, 0);
134 DEBUG(DEBUG_ERR, ("socket() failed, errno=%d\n", ret));
138 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
141 DEBUG(DEBUG_ERR, ("connect() failed, errno=%d\n", ret));
147 ret = comm_setup(client, ev, fd, client_read_handler, client,
148 client_dead_handler, client, &client->comm);
150 DEBUG(DEBUG_ERR, ("comm_setup() failed, ret=%d\n", ret));
156 ret = ctdb_ctrl_get_pnn(client, ev, client, CTDB_CURRENT_NODE,
157 tevent_timeval_zero(), &client->pnn);
159 DEBUG(DEBUG_ERR, ("failed to get current node pnn\n"));
162 TALLOC_FREE(client->comm);
169 static void client_read_handler(uint8_t *buf, size_t buflen,
172 struct ctdb_client_context *client = talloc_get_type_abort(
173 private_data, struct ctdb_client_context);
174 struct ctdb_req_header hdr;
178 ret = ctdb_req_header_pull(buf, buflen, &hdr, &np);
180 DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret));
184 if (buflen != hdr.length) {
185 DEBUG(DEBUG_WARNING, ("packet size mismatch %zu != %d\n",
186 buflen, hdr.length));
190 ret = ctdb_req_header_verify(&hdr, 0);
192 DEBUG(DEBUG_WARNING, ("invalid header, ret=%d\n", ret));
196 switch (hdr.operation) {
197 case CTDB_REPLY_CALL:
198 ctdb_client_reply_call(client, buf, buflen, hdr.reqid);
201 case CTDB_REQ_MESSAGE:
202 ctdb_client_req_message(client, buf, buflen, hdr.reqid);
205 case CTDB_REPLY_CONTROL:
206 ctdb_client_reply_control(client, buf, buflen, hdr.reqid);
209 case CTDB_REQ_TUNNEL:
210 ctdb_client_req_tunnel(client, buf, buflen, hdr.reqid);
218 static void client_dead_handler(void *private_data)
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;
226 if (callback != NULL) {
227 callback(callback_data);
231 DEBUG(DEBUG_NOTICE, ("connection to daemon closed, exiting\n"));
235 void ctdb_client_set_disconnect_callback(struct ctdb_client_context *client,
236 ctdb_client_callback_func_t callback,
239 client->callback = callback;
240 client->private_data = private_data;
243 uint32_t ctdb_client_pnn(struct ctdb_client_context *client)
248 void ctdb_client_wait(struct tevent_context *ev, bool *done)
251 tevent_loop_once(ev);
255 static void ctdb_client_wait_timeout_handler(struct tevent_context *ev,
256 struct tevent_timer *te,
260 bool *timed_out = (bool *)private_data;
265 int ctdb_client_wait_timeout(struct tevent_context *ev, bool *done,
266 struct timeval timeout)
269 struct tevent_timer *timer;
270 bool timed_out = false;
272 mem_ctx = talloc_new(ev);
273 if (mem_ctx == NULL) {
277 timer = tevent_add_timer(ev, mem_ctx, timeout,
278 ctdb_client_wait_timeout_handler,
281 talloc_free(mem_ctx);
285 while (! (*done) && ! timed_out) {
286 tevent_loop_once(ev);
289 talloc_free(mem_ctx);
298 struct ctdb_recovery_wait_state {
299 struct tevent_context *ev;
300 struct ctdb_client_context *client;
303 static void ctdb_recovery_wait_recmode(struct tevent_req *subreq);
304 static void ctdb_recovery_wait_retry(struct tevent_req *subreq);
306 struct tevent_req *ctdb_recovery_wait_send(TALLOC_CTX *mem_ctx,
307 struct tevent_context *ev,
308 struct ctdb_client_context *client)
310 struct tevent_req *req, *subreq;
311 struct ctdb_recovery_wait_state *state;
312 struct ctdb_req_control request;
314 req = tevent_req_create(mem_ctx, &state,
315 struct ctdb_recovery_wait_state);
321 state->client = client;
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);
329 tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req);
334 static void ctdb_recovery_wait_recmode(struct tevent_req *subreq)
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;
345 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
348 tevent_req_error(req, ret);
352 ret = ctdb_reply_control_get_recmode(reply, &recmode);
354 tevent_req_error(req, ret);
358 if (recmode == CTDB_RECOVERY_NORMAL) {
359 tevent_req_done(req);
363 subreq = tevent_wakeup_send(state, state->ev,
364 tevent_timeval_current_ofs(1, 0));
365 if (tevent_req_nomem(subreq, req)) {
368 tevent_req_set_callback(subreq, ctdb_recovery_wait_retry, req);
371 static void ctdb_recovery_wait_retry(struct tevent_req *subreq)
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;
380 status = tevent_wakeup_recv(subreq);
383 tevent_req_error(req, ENOMEM);
387 ctdb_req_control_get_recmode(&request);
388 subreq = ctdb_client_control_send(state, state->ev, state->client,
390 tevent_timeval_zero(), &request);
391 if (tevent_req_nomem(subreq, req)) {
394 tevent_req_set_callback(subreq, ctdb_recovery_wait_recmode, req);
397 bool ctdb_recovery_wait_recv(struct tevent_req *req, int *perr)
401 if (tevent_req_is_unix_error(req, &err)) {
411 bool ctdb_recovery_wait(struct tevent_context *ev,
412 struct ctdb_client_context *client)
415 struct tevent_req *req;
418 mem_ctx = talloc_new(client);
419 if (mem_ctx == NULL) {
423 req = ctdb_recovery_wait_send(mem_ctx, ev, client);
428 tevent_req_poll(req, ev);
430 status = ctdb_recovery_wait_recv(req, NULL);
432 talloc_free(mem_ctx);