1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
CTDB event daemon client
Copyright (C) Amitay Isaacs 2018
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include <talloc.h>
#include <tevent.h>
#include "lib/util/debug.h"
#include "lib/util/tevent_unix.h"
#include "common/logging.h"
#include "common/path.h"
#include "common/sock_client.h"
#include "event/event_protocol_api.h"
#include "event/event.h"
struct ctdb_event_context {
char *socket;
struct sock_client_context *sockc;
};
struct event_request {
struct ctdb_event_header header;
struct ctdb_event_request *request;
};
struct event_reply {
struct ctdb_event_header header;
struct ctdb_event_reply *reply;
};
static int event_request_push(void *request_data,
uint32_t reqid,
TALLOC_CTX *mem_ctx,
uint8_t **buf,
size_t *buflen,
void *private_data)
{
struct event_request *r = (struct event_request *)request_data;
int ret;
r->header.reqid = reqid;
*buflen = ctdb_event_request_len(&r->header, r->request);
*buf = talloc_size(mem_ctx, *buflen);
if (*buf == NULL) {
return ENOMEM;
}
ret = ctdb_event_request_push(&r->header, r->request, *buf, buflen);
if (ret != 0) {
talloc_free(*buf);
return ret;
}
return 0;
}
static int event_reply_pull(uint8_t *buf,
size_t buflen,
TALLOC_CTX *mem_ctx,
void **reply_data,
void *private_data)
{
struct event_reply *r;
int ret;
r = talloc_zero(mem_ctx, struct event_reply);
if (r == NULL) {
return ENOMEM;
}
ret = ctdb_event_reply_pull(buf, buflen, &r->header, r, &r->reply);
if (ret != 0) {
talloc_free(r);
return ret;
}
*reply_data = r;
return 0;
}
static int event_reply_reqid(uint8_t *buf,
size_t buflen,
uint32_t *reqid,
void *private_data)
{
struct ctdb_event_header header;
int ret;
ret = ctdb_event_header_extract(buf, buflen, &header);
if (ret != 0) {
return ret;
}
*reqid = header.reqid;
return 0;
}
struct sock_client_proto_funcs event_proto_funcs = {
.request_push = event_request_push,
.reply_pull = event_reply_pull,
.reply_reqid = event_reply_reqid,
};
int ctdb_event_init(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct ctdb_event_context **result)
{
struct ctdb_event_context *eclient;
int ret;
eclient = talloc_zero(mem_ctx, struct ctdb_event_context);
if (eclient == NULL) {
return ENOMEM;
}
eclient->socket = path_socket(eclient, "eventd");
if (eclient->socket == NULL) {
talloc_free(eclient);
return ENOMEM;
}
ret = sock_client_setup(eclient,
ev,
eclient->socket,
&event_proto_funcs,
eclient,
&eclient->sockc);
if (ret != 0) {
talloc_free(eclient);
return ret;
}
*result = eclient;
return 0;
}
/*
* Handle request and reply
*/
struct ctdb_event_msg_state {
struct event_request e_request;
struct event_reply *e_reply;
};
static void ctdb_event_msg_done(struct tevent_req *subreq);
static struct tevent_req *ctdb_event_msg_send(
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct ctdb_event_context *eclient,
struct ctdb_event_request *request)
{
struct tevent_req *req, *subreq;
struct ctdb_event_msg_state *state;
req = tevent_req_create(mem_ctx, &state, struct ctdb_event_msg_state);
if (req == NULL) {
return NULL;
}
state->e_request.request = request;
subreq = sock_client_msg_send(mem_ctx,
ev,
eclient->sockc,
tevent_timeval_zero(),
&state->e_request);
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
}
tevent_req_set_callback(subreq, ctdb_event_msg_done, req);
return req;
}
static void ctdb_event_msg_done(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct ctdb_event_msg_state *state = tevent_req_data(
req, struct ctdb_event_msg_state);
int ret = 0;
bool ok;
ok = sock_client_msg_recv(subreq, &ret, state, &state->e_reply);
TALLOC_FREE(subreq);
if (!ok) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
static bool ctdb_event_msg_recv(struct tevent_req *req,
int *perr,
TALLOC_CTX *mem_ctx,
struct ctdb_event_reply **reply)
{
struct ctdb_event_msg_state *state = tevent_req_data(
req, struct ctdb_event_msg_state);
int ret = 0;
if (tevent_req_is_unix_error(req, &ret)) {
if (perr != NULL) {
*perr = ret;
}
return false;
}
*reply = talloc_steal(mem_ctx, state->e_reply->reply);
return true;
}
/*
* API functions
*/
struct tevent_req *ctdb_event_run_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct ctdb_event_context *eclient,
struct ctdb_event_request_run *run)
{
struct ctdb_event_request request;
request.cmd = CTDB_EVENT_CMD_RUN;
request.data.run = run;
return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}
bool ctdb_event_run_recv(struct tevent_req *req, int *perr, int *result)
{
struct ctdb_event_reply *reply = NULL;
bool ok;
ok = ctdb_event_msg_recv(req, perr, req, &reply);
if (!ok) {
return false;
}
if (reply->cmd != CTDB_EVENT_CMD_RUN) {
*result = EPROTO;
} else {
*result = reply->result;
}
talloc_free(reply);
return true;
}
struct tevent_req *ctdb_event_status_send(
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct ctdb_event_context *eclient,
struct ctdb_event_request_status *status)
{
struct ctdb_event_request request;
request.cmd = CTDB_EVENT_CMD_STATUS;
request.data.status = status;
return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}
bool ctdb_event_status_recv(struct tevent_req *req,
int *perr,
int *result,
TALLOC_CTX *mem_ctx,
struct ctdb_event_reply_status **status)
{
struct ctdb_event_reply *reply = NULL;
bool ok;
ok = ctdb_event_msg_recv(req, perr, req, &reply);
if (!ok) {
return false;
}
if (reply->cmd != CTDB_EVENT_CMD_STATUS) {
*result = EPROTO;
} else {
*result = reply->result;
}
if (reply->result == 0) {
*status = talloc_steal(mem_ctx, reply->data.status);
} else {
*status = NULL;
}
talloc_free(reply);
return true;
}
struct tevent_req *ctdb_event_script_send(
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct ctdb_event_context *eclient,
struct ctdb_event_request_script *script)
{
struct ctdb_event_request request;
request.cmd = CTDB_EVENT_CMD_SCRIPT;
request.data.script = script;
return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}
bool ctdb_event_script_recv(struct tevent_req *req, int *perr, int *result)
{
struct ctdb_event_reply *reply = NULL;
bool ok;
ok = ctdb_event_msg_recv(req, perr, req, &reply);
if (!ok) {
return false;
}
if (reply->cmd != CTDB_EVENT_CMD_SCRIPT) {
*result = EPROTO;
} else {
*result = reply->result;
}
talloc_free(reply);
return true;
}
|