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 "lib/util/tevent_unix.h"
30 #include "common/reqid.h"
31 #include "common/srvid.h"
32 #include "common/comm.h"
34 #include "protocol/protocol.h"
35 #include "protocol/protocol_api.h"
37 #include "client/client_private.h"
38 #include "client/client.h"
42 * Handle REQ_CALL and REPLY_CALL
45 struct ctdb_client_call_state {
46 struct ctdb_client_context *client;
48 struct ctdb_reply_call *reply;
49 struct tevent_req *req;
52 static int ctdb_client_call_state_destructor(
53 struct ctdb_client_call_state *state);
54 static void ctdb_client_call_done(struct tevent_req *subreq);
56 struct tevent_req *ctdb_client_call_send(TALLOC_CTX *mem_ctx,
57 struct tevent_context *ev,
58 struct ctdb_client_context *client,
59 struct ctdb_req_call *request)
61 struct ctdb_req_header h;
62 struct tevent_req *req, *subreq;
63 struct ctdb_client_call_state *state;
66 size_t datalen, buflen;
69 req = tevent_req_create(mem_ctx, &state,
70 struct ctdb_client_call_state);
75 reqid = reqid_new(client->idr, state);
76 if (reqid == REQID_INVALID) {
81 state->client = client;
84 state->reply = talloc_zero(state, struct ctdb_reply_call);
85 if (tevent_req_nomem(state->reply, req)) {
86 return tevent_req_post(req, ev);
89 talloc_set_destructor(state, ctdb_client_call_state_destructor);
91 ctdb_req_header_fill(&h, 0, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
94 datalen = ctdb_req_call_len(&h, request);
95 ret = ctdb_allocate_pkt(state, datalen, &buf, &buflen);
97 tevent_req_error(req, ret);
98 return tevent_req_post(req, ev);
101 ret = ctdb_req_call_push(&h, request, buf, &buflen);
103 tevent_req_error(req, ret);
104 return tevent_req_post(req, ev);
107 subreq = comm_write_send(state, ev, client->comm, buf, buflen);
108 if (tevent_req_nomem(subreq, req)) {
109 return tevent_req_post(req, ev);
111 tevent_req_set_callback(subreq, ctdb_client_call_done, req);
116 static int ctdb_client_call_state_destructor(
117 struct ctdb_client_call_state *state)
119 reqid_remove(state->client->idr, state->reqid);
123 static void ctdb_client_call_done(struct tevent_req *subreq)
125 struct tevent_req *req = tevent_req_callback_data(
126 subreq, struct tevent_req);
130 status = comm_write_recv(subreq, &ret);
133 tevent_req_error(req, ret);
137 /* wait for the reply */
140 void ctdb_client_reply_call(struct ctdb_client_context *client,
141 uint8_t *buf, size_t buflen, uint32_t reqid)
143 struct ctdb_req_header h;
144 struct ctdb_client_call_state *state;
147 state = reqid_find(client->idr, reqid, struct ctdb_client_call_state);
152 if (reqid != state->reqid) {
156 ret = ctdb_reply_call_pull(buf, buflen, &h, state, state->reply);
158 tevent_req_error(state->req, ret);
162 tevent_req_done(state->req);
165 bool ctdb_client_call_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
166 struct ctdb_reply_call **reply, int *perr)
168 struct ctdb_client_call_state *state = tevent_req_data(
169 req, struct ctdb_client_call_state);
172 if (tevent_req_is_unix_error(req, &err)) {
180 *reply = talloc_steal(mem_ctx, state->reply);