vfs_fruit: pass handle to ad_convert_finderinfo()
[Samba.git] / ctdb / event / event_client.c
blobd0bee0d0841ac6138a002ef54110ae60191ca1d7
1 /*
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/>.
20 #include "replace.h"
22 #include <talloc.h>
23 #include <tevent.h>
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 {
36 char *socket;
37 struct sock_client_context *sockc;
40 struct event_request {
41 struct ctdb_event_header header;
42 struct ctdb_event_request *request;
45 struct event_reply {
46 struct ctdb_event_header header;
47 struct ctdb_event_reply *reply;
50 static int event_request_push(void *request_data,
51 uint32_t reqid,
52 TALLOC_CTX *mem_ctx,
53 uint8_t **buf,
54 size_t *buflen,
55 void *private_data)
57 struct event_request *r = (struct event_request *)request_data;
58 int ret;
60 r->header.reqid = reqid;
62 *buflen = ctdb_event_request_len(&r->header, r->request);
63 *buf = talloc_size(mem_ctx, *buflen);
64 if (*buf == NULL) {
65 return ENOMEM;
68 ret = ctdb_event_request_push(&r->header, r->request, *buf, buflen);
69 if (ret != 0) {
70 talloc_free(*buf);
71 return ret;
74 return 0;
77 static int event_reply_pull(uint8_t *buf,
78 size_t buflen,
79 TALLOC_CTX *mem_ctx,
80 void **reply_data,
81 void *private_data)
83 struct event_reply *r;
84 int ret;
86 r = talloc_zero(mem_ctx, struct event_reply);
87 if (r == NULL) {
88 return ENOMEM;
91 ret = ctdb_event_reply_pull(buf, buflen, &r->header, r, &r->reply);
92 if (ret != 0) {
93 talloc_free(r);
94 return ret;
97 *reply_data = r;
99 return 0;
102 static int event_reply_reqid(uint8_t *buf,
103 size_t buflen,
104 uint32_t *reqid,
105 void *private_data)
107 struct ctdb_event_header header;
108 int ret;
110 ret = ctdb_event_header_extract(buf, buflen, &header);
111 if (ret != 0) {
112 return ret;
115 *reqid = header.reqid;
116 return 0;
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;
130 int ret;
132 eclient = talloc_zero(mem_ctx, struct ctdb_event_context);
133 if (eclient == NULL) {
134 return ENOMEM;
137 eclient->socket = path_socket(eclient, "eventd");
138 if (eclient->socket == NULL) {
139 talloc_free(eclient);
140 return ENOMEM;
143 ret = sock_client_setup(eclient,
145 eclient->socket,
146 &event_proto_funcs,
147 eclient,
148 &eclient->sockc);
149 if (ret != 0) {
150 talloc_free(eclient);
151 return ret;
154 *result = eclient;
155 return 0;
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(
170 TALLOC_CTX *mem_ctx,
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);
179 if (req == NULL) {
180 return NULL;
183 state->e_request.request = request;
185 subreq = sock_client_msg_send(mem_ctx,
187 eclient->sockc,
188 tevent_timeval_zero(),
189 &state->e_request);
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);
195 return 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);
204 int ret = 0;
205 bool ok;
207 ok = sock_client_msg_recv(subreq, &ret, state, &state->e_reply);
208 TALLOC_FREE(subreq);
209 if (!ok) {
210 tevent_req_error(req, ret);
211 return;
214 tevent_req_done(req);
217 static bool ctdb_event_msg_recv(struct tevent_req *req,
218 int *perr,
219 TALLOC_CTX *mem_ctx,
220 struct ctdb_event_reply **reply)
222 struct ctdb_event_msg_state *state = tevent_req_data(
223 req, struct ctdb_event_msg_state);
224 int ret = 0;
226 if (tevent_req_is_unix_error(req, &ret)) {
227 if (perr != NULL) {
228 *perr = ret;
230 return false;
233 *reply = talloc_steal(mem_ctx, state->e_reply->reply);
235 return true;
239 * API functions
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;
258 bool ok;
260 ok = ctdb_event_msg_recv(req, perr, req, &reply);
261 if (!ok) {
262 return false;
265 if (reply->cmd != CTDB_EVENT_CMD_RUN) {
266 *result = EPROTO;
267 } else {
268 *result = reply->result;
271 talloc_free(reply);
272 return true;
275 struct tevent_req *ctdb_event_status_send(
276 TALLOC_CTX *mem_ctx,
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,
290 int *perr,
291 int *result,
292 TALLOC_CTX *mem_ctx,
293 struct ctdb_event_reply_status **status)
295 struct ctdb_event_reply *reply = NULL;
296 bool ok;
298 ok = ctdb_event_msg_recv(req, perr, req, &reply);
299 if (!ok) {
300 return false;
303 if (reply->cmd != CTDB_EVENT_CMD_STATUS) {
304 *result = EPROTO;
305 } else {
306 *result = reply->result;
309 if (reply->result == 0) {
310 *status = talloc_steal(mem_ctx, reply->data.status);
311 } else {
312 *status = NULL;
315 talloc_free(reply);
316 return true;
319 struct tevent_req *ctdb_event_script_send(
320 TALLOC_CTX *mem_ctx,
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;
336 bool ok;
338 ok = ctdb_event_msg_recv(req, perr, req, &reply);
339 if (!ok) {
340 return false;
343 if (reply->cmd != CTDB_EVENT_CMD_SCRIPT) {
344 *result = EPROTO;
345 } else {
346 *result = reply->result;
349 talloc_free(reply);
350 return true;