2 CTDB event daemon client
4 Copyright (C) Amitay Isaacs 2018
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/>.
25 #include "lib/util/debug.h"
26 #include "lib/util/tevent_unix.h"
28 #include "common/logging.h"
29 #include "common/path.h"
30 #include "common/sock_client.h"
32 #include "event/event_protocol_api.h"
33 #include "event/event.h"
35 struct ctdb_event_context
{
37 struct sock_client_context
*sockc
;
40 struct event_request
{
41 struct ctdb_event_header header
;
42 struct ctdb_event_request
*request
;
46 struct ctdb_event_header header
;
47 struct ctdb_event_reply
*reply
;
50 static int event_request_push(void *request_data
,
57 struct event_request
*r
= (struct event_request
*)request_data
;
60 r
->header
.reqid
= reqid
;
62 *buflen
= ctdb_event_request_len(&r
->header
, r
->request
);
63 *buf
= talloc_size(mem_ctx
, *buflen
);
68 ret
= ctdb_event_request_push(&r
->header
, r
->request
, *buf
, buflen
);
77 static int event_reply_pull(uint8_t *buf
,
83 struct event_reply
*r
;
86 r
= talloc_zero(mem_ctx
, struct event_reply
);
91 ret
= ctdb_event_reply_pull(buf
, buflen
, &r
->header
, r
, &r
->reply
);
102 static int event_reply_reqid(uint8_t *buf
,
107 struct ctdb_event_header header
;
110 ret
= ctdb_event_header_extract(buf
, buflen
, &header
);
115 *reqid
= header
.reqid
;
119 struct sock_client_proto_funcs event_proto_funcs
= {
120 .request_push
= event_request_push
,
121 .reply_pull
= event_reply_pull
,
122 .reply_reqid
= event_reply_reqid
,
125 int ctdb_event_init(TALLOC_CTX
*mem_ctx
,
126 struct tevent_context
*ev
,
127 struct ctdb_event_context
**result
)
129 struct ctdb_event_context
*eclient
;
132 eclient
= talloc_zero(mem_ctx
, struct ctdb_event_context
);
133 if (eclient
== NULL
) {
137 eclient
->socket
= path_socket(eclient
, "eventd");
138 if (eclient
->socket
== NULL
) {
139 talloc_free(eclient
);
143 ret
= sock_client_setup(eclient
,
150 talloc_free(eclient
);
159 * Handle request and reply
162 struct ctdb_event_msg_state
{
163 struct event_request e_request
;
164 struct event_reply
*e_reply
;
167 static void ctdb_event_msg_done(struct tevent_req
*subreq
);
169 static struct tevent_req
*ctdb_event_msg_send(
171 struct tevent_context
*ev
,
172 struct ctdb_event_context
*eclient
,
173 struct ctdb_event_request
*request
)
175 struct tevent_req
*req
, *subreq
;
176 struct ctdb_event_msg_state
*state
;
178 req
= tevent_req_create(mem_ctx
, &state
, struct ctdb_event_msg_state
);
183 state
->e_request
.request
= request
;
185 subreq
= sock_client_msg_send(mem_ctx
,
188 tevent_timeval_zero(),
190 if (tevent_req_nomem(subreq
, req
)) {
191 return tevent_req_post(req
, ev
);
193 tevent_req_set_callback(subreq
, ctdb_event_msg_done
, req
);
198 static void ctdb_event_msg_done(struct tevent_req
*subreq
)
200 struct tevent_req
*req
= tevent_req_callback_data(
201 subreq
, struct tevent_req
);
202 struct ctdb_event_msg_state
*state
= tevent_req_data(
203 req
, struct ctdb_event_msg_state
);
207 ok
= sock_client_msg_recv(subreq
, &ret
, state
, &state
->e_reply
);
210 tevent_req_error(req
, ret
);
214 tevent_req_done(req
);
217 static bool ctdb_event_msg_recv(struct tevent_req
*req
,
220 struct ctdb_event_reply
**reply
)
222 struct ctdb_event_msg_state
*state
= tevent_req_data(
223 req
, struct ctdb_event_msg_state
);
226 if (tevent_req_is_unix_error(req
, &ret
)) {
233 *reply
= talloc_steal(mem_ctx
, state
->e_reply
->reply
);
242 struct tevent_req
*ctdb_event_run_send(TALLOC_CTX
*mem_ctx
,
243 struct tevent_context
*ev
,
244 struct ctdb_event_context
*eclient
,
245 struct ctdb_event_request_run
*run
)
247 struct ctdb_event_request request
;
249 request
.cmd
= CTDB_EVENT_CMD_RUN
;
250 request
.data
.run
= run
;
252 return ctdb_event_msg_send(mem_ctx
, ev
, eclient
, &request
);
255 bool ctdb_event_run_recv(struct tevent_req
*req
, int *perr
, int *result
)
257 struct ctdb_event_reply
*reply
= NULL
;
260 ok
= ctdb_event_msg_recv(req
, perr
, req
, &reply
);
265 if (reply
->cmd
!= CTDB_EVENT_CMD_RUN
) {
268 *result
= reply
->result
;
275 struct tevent_req
*ctdb_event_status_send(
277 struct tevent_context
*ev
,
278 struct ctdb_event_context
*eclient
,
279 struct ctdb_event_request_status
*status
)
281 struct ctdb_event_request request
;
283 request
.cmd
= CTDB_EVENT_CMD_STATUS
;
284 request
.data
.status
= status
;
286 return ctdb_event_msg_send(mem_ctx
, ev
, eclient
, &request
);
289 bool ctdb_event_status_recv(struct tevent_req
*req
,
293 struct ctdb_event_reply_status
**status
)
295 struct ctdb_event_reply
*reply
= NULL
;
298 ok
= ctdb_event_msg_recv(req
, perr
, req
, &reply
);
303 if (reply
->cmd
!= CTDB_EVENT_CMD_STATUS
) {
306 *result
= reply
->result
;
309 if (reply
->result
== 0) {
310 *status
= talloc_steal(mem_ctx
, reply
->data
.status
);
319 struct tevent_req
*ctdb_event_script_send(
321 struct tevent_context
*ev
,
322 struct ctdb_event_context
*eclient
,
323 struct ctdb_event_request_script
*script
)
325 struct ctdb_event_request request
;
327 request
.cmd
= CTDB_EVENT_CMD_SCRIPT
;
328 request
.data
.script
= script
;
330 return ctdb_event_msg_send(mem_ctx
, ev
, eclient
, &request
);
333 bool ctdb_event_script_recv(struct tevent_req
*req
, int *perr
, int *result
)
335 struct ctdb_event_reply
*reply
= NULL
;
338 ok
= ctdb_event_msg_recv(req
, perr
, req
, &reply
);
343 if (reply
->cmd
!= CTDB_EVENT_CMD_SCRIPT
) {
346 *result
= reply
->result
;