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 "lib/util/tevent_unix.h"
|
---|
29 |
|
---|
30 | #include "common/reqid.h"
|
---|
31 | #include "common/srvid.h"
|
---|
32 | #include "common/comm.h"
|
---|
33 |
|
---|
34 | #include "protocol/protocol.h"
|
---|
35 | #include "protocol/protocol_api.h"
|
---|
36 |
|
---|
37 | #include "client/client_private.h"
|
---|
38 | #include "client/client.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * Handle REQ_CALL and REPLY_CALL
|
---|
43 | */
|
---|
44 |
|
---|
45 | struct ctdb_client_call_state {
|
---|
46 | struct ctdb_client_context *client;
|
---|
47 | uint32_t reqid;
|
---|
48 | struct ctdb_reply_call *reply;
|
---|
49 | struct tevent_req *req;
|
---|
50 | };
|
---|
51 |
|
---|
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);
|
---|
55 |
|
---|
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)
|
---|
60 | {
|
---|
61 | struct ctdb_req_header h;
|
---|
62 | struct tevent_req *req, *subreq;
|
---|
63 | struct ctdb_client_call_state *state;
|
---|
64 | uint32_t reqid;
|
---|
65 | uint8_t *buf;
|
---|
66 | size_t buflen;
|
---|
67 | int ret;
|
---|
68 |
|
---|
69 | req = tevent_req_create(mem_ctx, &state,
|
---|
70 | struct ctdb_client_call_state);
|
---|
71 | if (req == NULL) {
|
---|
72 | return NULL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | reqid = reqid_new(client->idr, state);
|
---|
76 | if (reqid == REQID_INVALID) {
|
---|
77 | talloc_free(req);
|
---|
78 | return NULL;
|
---|
79 | }
|
---|
80 |
|
---|
81 | state->client = client;
|
---|
82 | state->reqid = reqid;
|
---|
83 | state->req = req;
|
---|
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);
|
---|
87 | }
|
---|
88 |
|
---|
89 | talloc_set_destructor(state, ctdb_client_call_state_destructor);
|
---|
90 |
|
---|
91 | ctdb_req_header_fill(&h, 0, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
|
---|
92 | client->pnn, reqid);
|
---|
93 |
|
---|
94 | ret = ctdb_req_call_push(&h, request, state, &buf, &buflen);
|
---|
95 | if (ret != 0) {
|
---|
96 | tevent_req_error(req, ret);
|
---|
97 | return tevent_req_post(req, ev);
|
---|
98 | }
|
---|
99 |
|
---|
100 | subreq = comm_write_send(state, ev, client->comm, buf, buflen);
|
---|
101 | if (tevent_req_nomem(subreq, req)) {
|
---|
102 | return tevent_req_post(req, ev);
|
---|
103 | }
|
---|
104 | tevent_req_set_callback(subreq, ctdb_client_call_done, req);
|
---|
105 |
|
---|
106 | return req;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static int ctdb_client_call_state_destructor(
|
---|
110 | struct ctdb_client_call_state *state)
|
---|
111 | {
|
---|
112 | reqid_remove(state->client->idr, state->reqid);
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void ctdb_client_call_done(struct tevent_req *subreq)
|
---|
117 | {
|
---|
118 | struct tevent_req *req = tevent_req_callback_data(
|
---|
119 | subreq, struct tevent_req);
|
---|
120 | bool status;
|
---|
121 | int ret;
|
---|
122 |
|
---|
123 | status = comm_write_recv(subreq, &ret);
|
---|
124 | TALLOC_FREE(subreq);
|
---|
125 | if (! status) {
|
---|
126 | tevent_req_error(req, ret);
|
---|
127 | return;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* wait for the reply */
|
---|
131 | }
|
---|
132 |
|
---|
133 | void ctdb_client_reply_call(struct ctdb_client_context *client,
|
---|
134 | uint8_t *buf, size_t buflen, uint32_t reqid)
|
---|
135 | {
|
---|
136 | struct ctdb_req_header h;
|
---|
137 | struct ctdb_client_call_state *state;
|
---|
138 | int ret;
|
---|
139 |
|
---|
140 | state = reqid_find(client->idr, reqid, struct ctdb_client_call_state);
|
---|
141 | if (state == NULL) {
|
---|
142 | return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (reqid != state->reqid) {
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 | ret = ctdb_reply_call_pull(buf, buflen, &h, state, state->reply);
|
---|
150 | if (ret != 0) {
|
---|
151 | tevent_req_error(state->req, ret);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | tevent_req_done(state->req);
|
---|
156 | }
|
---|
157 |
|
---|
158 | bool ctdb_client_call_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
|
---|
159 | struct ctdb_reply_call **reply, int *perr)
|
---|
160 | {
|
---|
161 | struct ctdb_client_call_state *state = tevent_req_data(
|
---|
162 | req, struct ctdb_client_call_state);
|
---|
163 | int err;
|
---|
164 |
|
---|
165 | if (tevent_req_is_unix_error(req, &err)) {
|
---|
166 | if (perr != NULL) {
|
---|
167 | *perr = err;
|
---|
168 | }
|
---|
169 | return false;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (reply != NULL) {
|
---|
173 | *reply = talloc_steal(mem_ctx, state->reply);
|
---|
174 | }
|
---|
175 |
|
---|
176 | return true;
|
---|
177 | }
|
---|