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
352
353
354
355
356
357
358
|
/*
CTDB event daemon - command handling
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 "event/event_private.h"
struct event_cmd_state {
struct event_context *eventd;
struct ctdb_event_request *request;
struct ctdb_event_reply *reply;
};
/*
* CTDB_EVENT_CMD_RUN
*/
static void event_cmd_run_done(struct tevent_req *subreq);
static struct tevent_req *event_cmd_run_send(
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct event_context *eventd,
struct ctdb_event_request *request,
struct ctdb_event_reply *reply)
{
struct tevent_req *req, *subreq;
struct event_cmd_state *state;
struct run_event_context *run_ctx;
struct ctdb_event_request_run *rdata;
int ret;
bool continue_on_failure = false;
req = tevent_req_create(mem_ctx, &state, struct event_cmd_state);
if (req == NULL) {
return NULL;
}
state->eventd = eventd;
state->request = request;
state->reply = reply;
rdata = request->data.run;
ret = eventd_run_ctx(eventd, rdata->component, &run_ctx);
if (ret != 0) {
state->reply->result = ret;
tevent_req_done(req);
return tevent_req_post(req, ev);
}
if (rdata->flags & CTDB_EVENT_RUN_ALL) {
continue_on_failure = true;
}
subreq = run_event_send(state,
ev,
run_ctx,
rdata->event,
rdata->args,
tevent_timeval_current_ofs(rdata->timeout,0),
continue_on_failure);
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
}
tevent_req_set_callback(subreq, event_cmd_run_done, req);
return req;
}
static void event_cmd_run_done(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct event_cmd_state *state = tevent_req_data(
req, struct event_cmd_state);
struct run_event_script_list *script_list = NULL;
struct ctdb_event_request_run *rdata;
int ret;
bool ok;
ok = run_event_recv(subreq, &ret, state, &script_list);
TALLOC_FREE(subreq);
if (!ok) {
state->reply->result = ret;
goto done;
}
if (script_list == NULL) {
state->reply->result = EIO;
goto done;
}
if (script_list->summary == -ECANCELED) {
state->reply->result = ECANCELED;
goto done;
}
rdata = state->request->data.run;
ret = eventd_set_event_result(state->eventd,
rdata->component,
rdata->event,
script_list);
if (ret != 0) {
state->reply->result = ret;
goto done;
}
if (script_list->summary == -ETIMEDOUT) {
state->reply->result = ETIMEDOUT;
} else if (script_list->summary != 0) {
state->reply->result = ENOEXEC;
}
done:
tevent_req_done(req);
}
/*
* CTDB_EVENT_CMD_STATUS
*/
static struct tevent_req *event_cmd_status_send(
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct event_context *eventd,
struct ctdb_event_request *request,
struct ctdb_event_reply *reply)
{
struct tevent_req *req;
struct event_cmd_state *state;
struct ctdb_event_request_run *rdata;
struct run_event_script_list *script_list;
int ret;
req = tevent_req_create(mem_ctx, &state, struct event_cmd_state);
if (req == NULL) {
return NULL;
}
reply->data.status = talloc_zero(reply,
struct ctdb_event_reply_status);
if (tevent_req_nomem(reply->data.status, req)) {
reply->result = ENOMEM;
goto done;
}
rdata = request->data.run;
ret = eventd_get_event_result(eventd,
rdata->component,
rdata->event,
&script_list);
if (ret != 0) {
reply->result = ret;
goto done;
}
reply->data.status->script_list = eventd_script_list(reply,
script_list);
if (reply->data.status->script_list == NULL) {
reply->result = ENOMEM;
goto done;
}
reply->data.status->summary = script_list->summary;
reply->result = 0;
done:
tevent_req_done(req);
return tevent_req_post(req, ev);
}
/*
* CTDB_EVENT_CMD_SCRIPT
*/
static struct tevent_req *event_cmd_script_send(
TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct event_context *eventd,
struct ctdb_event_request *request,
struct ctdb_event_reply *reply)
{
struct tevent_req *req;
struct event_cmd_state *state;
struct run_event_context *run_ctx;
struct ctdb_event_request_script *rdata;
int ret;
req = tevent_req_create(mem_ctx, &state, struct event_cmd_state);
if (req == NULL) {
return NULL;
}
rdata = request->data.script;
ret = eventd_run_ctx(eventd, rdata->component, &run_ctx);
if (ret != 0) {
reply->result = ret;
goto done;
}
if (rdata->action == CTDB_EVENT_SCRIPT_DISABLE) {
ret = run_event_script_disable(run_ctx, rdata->script);
} else if (rdata->action == CTDB_EVENT_SCRIPT_ENABLE) {
ret = run_event_script_enable(run_ctx, rdata->script);
} else {
D_ERR("Invalid action specified\n");
reply->result = EPROTO;
goto done;
}
if (ret != 0) {
reply->result = ret;
goto done;
}
reply->result = 0;
done:
tevent_req_done(req);
return tevent_req_post(req, ev);
}
static bool event_cmd_recv(struct tevent_req *req, int *perr)
{
if (tevent_req_is_unix_error(req, perr)) {
return false;
}
return true;
}
struct event_cmd_dispatch_state {
struct ctdb_event_reply *reply;
};
static void event_cmd_dispatch_done(struct tevent_req *subreq);
struct tevent_req *event_cmd_dispatch_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct event_context *eventd,
struct ctdb_event_request *request)
{
struct tevent_req *req, *subreq;
struct event_cmd_dispatch_state *state;
req = tevent_req_create(mem_ctx,
&state,
struct event_cmd_dispatch_state);
if (req == NULL) {
return NULL;
}
state->reply = talloc_zero(state, struct ctdb_event_reply);
if (tevent_req_nomem(state->reply, req)) {
return tevent_req_post(req, ev);
}
state->reply->cmd = request->cmd;
switch (request->cmd) {
case CTDB_EVENT_CMD_RUN:
subreq = event_cmd_run_send(state,
ev,
eventd,
request,
state->reply);
break;
case CTDB_EVENT_CMD_STATUS:
subreq = event_cmd_status_send(state,
ev,
eventd,
request,
state->reply);
break;
case CTDB_EVENT_CMD_SCRIPT:
subreq = event_cmd_script_send(state,
ev,
eventd,
request,
state->reply);
break;
default:
state->reply->result = EPROTO;
tevent_req_done(req);
return tevent_req_post(req, ev);
}
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
}
tevent_req_set_callback(subreq, event_cmd_dispatch_done, req);
return req;
}
static void event_cmd_dispatch_done(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
int ret;
bool ok;
ok = event_cmd_recv(subreq, &ret);
TALLOC_FREE(subreq);
if (!ok) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
bool event_cmd_dispatch_recv(struct tevent_req *req,
int *perr,
TALLOC_CTX *mem_ctx,
struct ctdb_event_reply **reply)
{
struct event_cmd_dispatch_state *state = tevent_req_data(
req, struct event_cmd_dispatch_state);
if (tevent_req_is_unix_error(req, perr)) {
return false;
}
*reply = talloc_steal(mem_ctx, state->reply);
return true;
}
|