2 CTDB event daemon - handle requests
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/>.
22 #include "lib/util/debug.h"
23 #include "lib/util/tevent_unix.h"
25 #include "common/logging.h"
27 #include "event/event_private.h"
28 #include "event/event_protocol_api.h"
30 struct event_request_state
{
31 struct ctdb_event_request
*request
;
32 struct ctdb_event_reply
*reply
;
35 static void event_request_done(struct tevent_req
*subreq
);
37 static struct tevent_req
*event_request_send(
39 struct tevent_context
*ev
,
40 struct event_context
*eventd
,
41 struct ctdb_event_header
*header
,
42 struct ctdb_event_request
*request
)
44 struct tevent_req
*req
, *subreq
;
45 struct event_request_state
*state
;
47 req
= tevent_req_create(mem_ctx
, &state
, struct event_request_state
);
52 state
->request
= request
;
54 subreq
= event_cmd_dispatch_send(state
, ev
, eventd
, request
);
55 if (tevent_req_nomem(subreq
, req
)) {
56 return tevent_req_post(req
, ev
);
58 tevent_req_set_callback(subreq
, event_request_done
, req
);
63 static void event_request_done(struct tevent_req
*subreq
)
65 struct tevent_req
*req
= tevent_req_callback_data(
66 subreq
, struct tevent_req
);
67 struct event_request_state
*state
= tevent_req_data(
68 req
, struct event_request_state
);
72 ok
= event_cmd_dispatch_recv(subreq
, &ret
, state
, &state
->reply
);
75 D_ERR("Command %s failed, ret=%d\n",
76 ctdb_event_command_to_string(state
->request
->cmd
), ret
);
78 state
->reply
= talloc_zero(state
, struct ctdb_event_reply
);
79 if (tevent_req_nomem(state
->reply
, req
)) {
83 state
->reply
->cmd
= state
->request
->cmd
;
84 state
->reply
->result
= EIO
;
90 static bool event_request_recv(struct tevent_req
*req
,
93 struct ctdb_event_reply
**reply
)
95 struct event_request_state
*state
= tevent_req_data(
96 req
, struct event_request_state
);
98 if (tevent_req_is_unix_error(req
, perr
)) {
102 *reply
= talloc_steal(mem_ctx
, state
->reply
);
107 struct event_pkt_state
{
108 struct ctdb_event_header header
;
109 struct ctdb_event_request
*request
;
114 static void event_pkt_done(struct tevent_req
*subreq
);
116 struct tevent_req
*event_pkt_send(TALLOC_CTX
*mem_ctx
,
117 struct tevent_context
*ev
,
118 struct event_context
*eventd
,
122 struct tevent_req
*req
, *subreq
;
123 struct event_pkt_state
*state
;
126 req
= tevent_req_create(mem_ctx
, &state
, struct event_pkt_state
);
131 ret
= ctdb_event_request_pull(buf
,
137 /* Ignore invalid packets */
138 D_ERR("Invalid packet received, buflen=%zu\n", buflen
);
139 tevent_req_error(req
, EPROTO
);
140 return tevent_req_post(req
, ev
);
143 subreq
= event_request_send(state
,
148 if (tevent_req_nomem(subreq
, req
)) {
149 return tevent_req_post(req
, ev
);
151 tevent_req_set_callback(subreq
, event_pkt_done
, req
);
156 static void event_pkt_done(struct tevent_req
*subreq
)
158 struct tevent_req
*req
= tevent_req_callback_data(
159 subreq
, struct tevent_req
);
160 struct event_pkt_state
*state
= tevent_req_data(
161 req
, struct event_pkt_state
);
162 struct ctdb_event_header header
;
163 struct ctdb_event_reply
*reply
;
167 ok
= event_request_recv(subreq
, &ret
, state
, &reply
);
169 TALLOC_FREE(state
->request
);
171 tevent_req_error(req
, ret
);
175 header
= (struct ctdb_event_header
) {
176 .reqid
= state
->header
.reqid
,
179 state
->buflen
= ctdb_event_reply_len(&header
, reply
);
180 state
->buf
= talloc_zero_size(state
, state
->buflen
);
181 if (tevent_req_nomem(state
->buf
, req
)) {
186 ret
= ctdb_event_reply_push(&header
,
192 talloc_free(state
->buf
);
193 tevent_req_error(req
, ret
);
197 tevent_req_done(req
);
200 bool event_pkt_recv(struct tevent_req
*req
,
206 struct event_pkt_state
*state
= tevent_req_data(
207 req
, struct event_pkt_state
);
209 if (tevent_req_is_unix_error(req
, perr
)) {
213 *buf
= talloc_steal(mem_ctx
, state
->buf
);
214 *buflen
= state
->buflen
;