summaryrefslogtreecommitdiffstats
path: root/ctdb/client/client_event.c
blob: 7111fe7a952fc1c40d49737632c02b3cc4307b94 (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
   Eventd client api

   Copyright (C) Amitay Isaacs  2016

   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 "system/network.h"

#include <talloc.h>
#include <tevent.h>

#include "lib/util/debug.h"
#include "lib/util/tevent_unix.h"

#include "common/logging.h"
#include "common/sock_client.h"

#include "protocol/protocol_api.h"

#include "client/client_event.h"

struct ctdb_event_context {
	struct sock_client_context *sockc;
};

static int ctdb_event_msg_request_push(void *request_data, uint32_t reqid,
				       TALLOC_CTX *mem_ctx,
				       uint8_t **buf, size_t *buflen,
				       void *private_data)
{
	struct ctdb_event_request *request =
		(struct ctdb_event_request *)request_data;
	int ret;

	sock_packet_header_set_reqid(&request->header, reqid);

	*buflen = ctdb_event_request_len(request);
	*buf = talloc_size(mem_ctx, *buflen);
	if (*buf == NULL) {
		return ENOMEM;
	}

	ret = ctdb_event_request_push(request, *buf, buflen);
	if (ret != 0) {
		return ret;
	}

	return 0;
}

static int ctdb_event_msg_reply_pull(uint8_t *buf, size_t buflen,
				     TALLOC_CTX *mem_ctx, void **reply_data,
				     void *private_data)
{
	struct ctdb_event_reply *reply;
	int ret;

	reply = talloc_zero(mem_ctx, struct ctdb_event_reply);
	if (reply == NULL) {
		return ENOMEM;
	}

	ret = ctdb_event_reply_pull(buf, buflen, reply, reply);
	if (ret != 0) {
		talloc_free(reply);
		return ret;
	}

	*reply_data = reply;
	return 0;
}

static int ctdb_event_msg_reply_reqid(uint8_t *buf, size_t buflen,
				      uint32_t *reqid, void *private_data)
{
	struct sock_packet_header header;
	size_t np;
	int ret;

	ret = sock_packet_header_pull(buf, buflen, &header, &np);
	if (ret != 0) {
		return ret;
	}

	*reqid = header.reqid;
	return 0;
}

struct sock_client_proto_funcs event_proto_funcs = {
	.request_push = ctdb_event_msg_request_push,
	.reply_pull = ctdb_event_msg_reply_pull,
	.reply_reqid = ctdb_event_msg_reply_reqid,
};


int ctdb_event_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
		    const char *sockpath, struct ctdb_event_context **out)
{
	struct ctdb_event_context *eclient;
	int ret;

	eclient = talloc_zero(mem_ctx, struct ctdb_event_context);
	if (eclient == NULL) {
		DEBUG(DEBUG_ERR, (__location__ " memory allocation error\n"));
		return ENOMEM;
	}

	ret = sock_client_setup(eclient, ev, sockpath,
				&event_proto_funcs, eclient,
				&eclient->sockc);
	if (ret != 0) {
		talloc_free(eclient);
		return ret;
	}

	*out = eclient;
	return 0;
}

void ctdb_event_set_disconnect_callback(struct ctdb_event_context *eclient,
					ctdb_client_callback_func_t callback,
					void *private_data)
{
	sock_client_set_disconnect_callback(eclient->sockc,
					    callback, private_data);
}

/*
 * Handle eventd_request and eventd_reply
 */

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;

	req = sock_client_msg_send(mem_ctx, ev, eclient->sockc,
				   tevent_timeval_zero(), request);
	return req;
}

bool ctdb_event_msg_recv(struct tevent_req *req, int *perr,
			 TALLOC_CTX *mem_ctx,
			 struct ctdb_event_reply **reply)
{
	void *reply_data;
	bool status;

	status = sock_client_msg_recv(req, perr, mem_ctx, &reply_data);

	if (status && reply != NULL) {
		*reply = talloc_get_type_abort(
				reply_data, struct ctdb_event_reply);
	}

	return status;
}

/*
 * Run an event
 */

struct tevent_req *ctdb_event_run_send(TALLOC_CTX *mem_ctx,
				       struct tevent_context *ev,
				       struct ctdb_event_context *eclient,
				       enum ctdb_event event,
				       uint32_t timeout, const char *arg_str)
{
	struct ctdb_event_request request;
	struct ctdb_event_request_run rdata;

	rdata.event = event;
	rdata.timeout = timeout;
	rdata.arg_str = arg_str;

	request.rdata.command = CTDB_EVENT_COMMAND_RUN;
	request.rdata.data.run = &rdata;

	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;
	int ret;
	bool status;

	status = ctdb_event_msg_recv(req, &ret, req, &reply);
	if (! status) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	if (reply->rdata.command != CTDB_EVENT_COMMAND_RUN) {
		if (perr != NULL) {
			*perr = EPROTO;
		}
		talloc_free(reply);
		return false;
	}

	if (result != NULL) {
		*result = reply->rdata.result;
	}

	talloc_free(reply);
	return true;
}

/*
 * Get event status
 */

struct tevent_req *ctdb_event_status_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  struct ctdb_event_context *eclient,
					  enum ctdb_event event,
					  enum ctdb_event_status_state state)
{
	struct ctdb_event_request request;
	struct ctdb_event_request_status rdata;

	rdata.event = event;
	rdata.state = state;

	request.rdata.command = CTDB_EVENT_COMMAND_STATUS;
	request.rdata.data.status = &rdata;

	return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}

bool ctdb_event_status_recv(struct tevent_req *req, int *perr,
			    int32_t *result, int *event_status,
			    TALLOC_CTX *mem_ctx,
			    struct ctdb_script_list **script_list)
{
	struct ctdb_event_reply *reply;
	int ret;
	bool status;

	status = ctdb_event_msg_recv(req, &ret, req, &reply);
	if (! status) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	if (reply->rdata.command != CTDB_EVENT_COMMAND_STATUS) {
		if (perr != NULL) {
			*perr = EPROTO;
		}
		talloc_free(reply);
		return false;
	}

	if (result != NULL) {
		*result = reply->rdata.result;
	}
	if (event_status != NULL) {
		*event_status = reply->rdata.data.status->status;
	}
	if (script_list != NULL) {
		*script_list = talloc_steal(mem_ctx,
					reply->rdata.data.status->script_list);
	}

	talloc_free(reply);
	return true;
}

/*
 * Get script list
 */

struct tevent_req *ctdb_event_script_list_send(
					TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					struct ctdb_event_context *eclient)
{
	struct ctdb_event_request request;

	request.rdata.command = CTDB_EVENT_COMMAND_SCRIPT_LIST;

	return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}

bool ctdb_event_script_list_recv(struct tevent_req *req, int *perr,
				 int32_t *result, TALLOC_CTX *mem_ctx,
				 struct ctdb_script_list **script_list)
{
	struct ctdb_event_reply *reply;
	int ret;
	bool status;

	status = ctdb_event_msg_recv(req, &ret, req, &reply);
	if (! status) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	if (reply->rdata.command != CTDB_EVENT_COMMAND_SCRIPT_LIST) {
		if (perr != NULL) {
			*perr = EPROTO;
		}
		talloc_free(reply);
		return false;
	}

	if (result != NULL) {
		*result = reply->rdata.result;
	}
	if (script_list != NULL) {
		*script_list = talloc_steal(mem_ctx,
				reply->rdata.data.script_list->script_list);
	}

	talloc_free(reply);
	return true;
}

/*
 * Enable a script
 */

struct tevent_req *ctdb_event_script_enable_send(
					TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					struct ctdb_event_context *eclient,
					const char *script_name)
{
	struct ctdb_event_request request;
	struct ctdb_event_request_script_enable rdata;

	rdata.script_name = script_name;

	request.rdata.command = CTDB_EVENT_COMMAND_SCRIPT_ENABLE;
	request.rdata.data.script_enable = &rdata;

	return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}

bool ctdb_event_script_enable_recv(struct tevent_req *req, int *perr,
				   int *result)
{
	struct ctdb_event_reply *reply;
	int ret;
	bool status;

	status = ctdb_event_msg_recv(req, &ret, req, &reply);
	if (! status) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	if (reply->rdata.command != CTDB_EVENT_COMMAND_SCRIPT_ENABLE) {
		if (perr != NULL) {
			*perr = EPROTO;
		}
		talloc_free(reply);
		return false;
	}

	if (result != NULL) {
		*result = reply->rdata.result;
	}

	talloc_free(reply);
	return true;
}

/*
 * Disable a script
 */

struct tevent_req *ctdb_event_script_disable_send(
					TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					struct ctdb_event_context *eclient,
					const char *script_name)
{
	struct ctdb_event_request request;
	struct ctdb_event_request_script_disable rdata;

	rdata.script_name = script_name;

	request.rdata.command = CTDB_EVENT_COMMAND_SCRIPT_DISABLE;
	request.rdata.data.script_disable = &rdata;

	return ctdb_event_msg_send(mem_ctx, ev, eclient, &request);
}

bool ctdb_event_script_disable_recv(struct tevent_req *req, int *perr,
				    int *result)
{
	struct ctdb_event_reply *reply;
	int ret;
	bool status;

	status = ctdb_event_msg_recv(req, &ret, req, &reply);
	if (! status) {
		if (perr != NULL) {
			*perr = ret;
		}
		return false;
	}

	if (reply->rdata.command != CTDB_EVENT_COMMAND_SCRIPT_DISABLE) {
		if (perr != NULL) {
			*perr = EPROTO;
		}
		talloc_free(reply);
		return false;
	}

	if (result != NULL) {
		*result = reply->rdata.result;
	}

	talloc_free(reply);
	return true;
}