s3:rpcd_witness.idl: introduce definitions for rpcd_witness_registration.tdb records
[Samba.git] / source3 / lib / messages_ctdb.c
blobd55b53bf601e16ce5cc82f1c18e895abec349967
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba internal messaging functions
4 * Copyright (C) 2017 by Volker Lendecke
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 "includes.h"
21 #include "lib/messages_ctdb.h"
22 #include "lib/util/server_id.h"
23 #include "messages.h"
24 #include "util_tdb.h"
25 #include "lib/util/iov_buf.h"
26 #include "lib/messages_util.h"
27 #include "ctdbd_conn.h"
28 #include "lib/cluster_support.h"
29 #include "ctdb_srvids.h"
31 struct messaging_ctdb_context;
34 * We can only have one tevent_fd per ctdb_context and per
35 * tevent_context. Maintain a list of registered tevent_contexts per
36 * ctdb_context.
38 struct messaging_ctdb_fde_ev {
39 struct messaging_ctdb_fde_ev *prev, *next;
42 * Backreference to enable DLIST_REMOVE from our
43 * destructor. Also, set to NULL when the ctdb_context dies
44 * before the messaging_ctdb_fde_ev.
46 struct messaging_ctdb_context *ctx;
48 struct tevent_context *ev;
49 struct tevent_fd *fde;
52 struct messaging_ctdb_context {
53 struct ctdbd_connection *conn;
55 void (*recv_cb)(struct tevent_context *ev,
56 const uint8_t *msg, size_t msg_len,
57 int *fds, size_t num_fds,
58 void *private_data);
59 void *recv_cb_private_data;
61 struct messaging_ctdb_fde_ev *fde_evs;
64 static int messaging_ctdb_recv(
65 struct tevent_context *ev,
66 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
67 const uint8_t *msg, size_t msg_len, void *private_data)
69 struct messaging_ctdb_context *state = talloc_get_type_abort(
70 private_data, struct messaging_ctdb_context);
72 state->recv_cb(ev, msg, msg_len, NULL, 0, state->recv_cb_private_data);
74 return 0;
77 struct messaging_ctdb_context *global_ctdb_context;
79 static int global_ctdb_ctx_destructor(struct messaging_ctdb_context *ctx)
81 if (ctx != NULL) {
82 struct messaging_ctdb_fde_ev *fde_ev = NULL;
83 for (fde_ev = ctx->fde_evs;
84 fde_ev != NULL;
85 fde_ev = fde_ev->next) {
86 if (fde_ev->ctx == ctx) {
87 fde_ev->ctx = NULL;
91 return 0;
94 int messaging_ctdb_init(const char *sockname, int timeout, uint64_t unique_id,
95 void (*recv_cb)(struct tevent_context *ev,
96 const uint8_t *msg, size_t msg_len,
97 int *fds, size_t num_fds,
98 void *private_data),
99 void *private_data)
101 struct messaging_ctdb_context *ctx;
102 int ret;
104 if (global_ctdb_context != NULL) {
105 return EBUSY;
108 ctx = talloc_zero(NULL, struct messaging_ctdb_context);
109 if (ctx == NULL) {
110 return ENOMEM;
113 talloc_set_destructor(ctx,
114 global_ctdb_ctx_destructor);
116 ctx->recv_cb = recv_cb;
117 ctx->recv_cb_private_data = private_data;
119 ret = ctdbd_init_connection(ctx, sockname, timeout, &ctx->conn);
120 if (ret != 0) {
121 DBG_DEBUG("ctdbd_init_connection returned %s\n",
122 strerror(ret));
123 goto fail;
126 ret = register_with_ctdbd(ctx->conn, tevent_cached_getpid(), messaging_ctdb_recv,
127 ctx);
128 if (ret != 0) {
129 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
130 strerror(ret), ret);
131 goto fail;
134 ret = register_with_ctdbd(ctx->conn, CTDB_SRVID_SAMBA_PROCESS,
135 messaging_ctdb_recv, ctx);
136 if (ret != 0) {
137 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
138 strerror(ret), ret);
139 goto fail;
142 ret = register_with_ctdbd(ctx->conn, unique_id, NULL, NULL);
143 if (ret != 0) {
144 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
145 strerror(ret), ret);
146 goto fail;
149 set_my_vnn(ctdbd_vnn(ctx->conn));
151 global_ctdb_context = ctx;
152 return 0;
153 fail:
154 TALLOC_FREE(ctx);
155 return ret;
158 void messaging_ctdb_destroy(void)
160 TALLOC_FREE(global_ctdb_context);
163 int messaging_ctdb_send(uint32_t dst_vnn, uint64_t dst_srvid,
164 const struct iovec *iov, int iovlen)
166 struct messaging_ctdb_context *ctx = global_ctdb_context;
167 int ret;
169 if (ctx == NULL) {
170 return ENOTCONN;
173 ret = ctdbd_messaging_send_iov(ctx->conn, dst_vnn, dst_srvid,
174 iov, iovlen);
175 return ret;
178 static void messaging_ctdb_read_handler(struct tevent_context *ev,
179 struct tevent_fd *fde,
180 uint16_t flags,
181 void *private_data)
183 struct messaging_ctdb_context *ctx = talloc_get_type_abort(
184 private_data, struct messaging_ctdb_context);
186 if ((flags & TEVENT_FD_READ) == 0) {
187 return;
189 ctdbd_socket_readable(ev, ctx->conn);
192 struct messaging_ctdb_fde {
193 struct tevent_fd *fde;
196 static int messaging_ctdb_fde_ev_destructor(
197 struct messaging_ctdb_fde_ev *fde_ev)
199 if (fde_ev->ctx != NULL) {
200 DLIST_REMOVE(fde_ev->ctx->fde_evs, fde_ev);
201 fde_ev->ctx = NULL;
203 return 0;
207 * Reference counter for a struct tevent_fd messaging read event
208 * (with callback function) on a struct tevent_context registered
209 * on a messaging context.
211 * If we've already registered this struct tevent_context before
212 * (so already have a read event), just increase the reference count.
214 * Otherwise create a new struct tevent_fd messaging read event on the
215 * previously unseen struct tevent_context - this is what drives
216 * the message receive processing.
220 struct messaging_ctdb_fde *messaging_ctdb_register_tevent_context(
221 TALLOC_CTX *mem_ctx, struct tevent_context *ev)
223 struct messaging_ctdb_context *ctx = global_ctdb_context;
224 struct messaging_ctdb_fde_ev *fde_ev;
225 struct messaging_ctdb_fde *fde;
227 if (ctx == NULL) {
228 return NULL;
231 fde = talloc(mem_ctx, struct messaging_ctdb_fde);
232 if (fde == NULL) {
233 return NULL;
236 for (fde_ev = ctx->fde_evs; fde_ev != NULL; fde_ev = fde_ev->next) {
237 if (tevent_fd_get_flags(fde_ev->fde) == 0) {
239 * If the event context got deleted,
240 * tevent_fd_get_flags() will return 0
241 * for the stale fde.
243 * In that case we should not
244 * use fde_ev->ev anymore.
246 continue;
248 if (fde_ev->ev == ev) {
249 break;
253 if (fde_ev == NULL) {
254 int sock = ctdbd_conn_get_fd(ctx->conn);
256 fde_ev = talloc(fde, struct messaging_ctdb_fde_ev);
257 if (fde_ev == NULL) {
258 return NULL;
260 fde_ev->fde = tevent_add_fd(
261 ev, fde_ev, sock, TEVENT_FD_READ,
262 messaging_ctdb_read_handler, ctx);
263 if (fde_ev->fde == NULL) {
264 TALLOC_FREE(fde);
265 return NULL;
267 fde_ev->ev = ev;
268 fde_ev->ctx = ctx;
269 DLIST_ADD(ctx->fde_evs, fde_ev);
270 talloc_set_destructor(
271 fde_ev, messaging_ctdb_fde_ev_destructor);
272 } else {
274 * Same trick as with tdb_wrap: The caller will never
275 * see the talloc_referenced object, the
276 * messaging_ctdb_fde_ev, so problems with
277 * talloc_unlink will not happen.
279 if (talloc_reference(fde, fde_ev) == NULL) {
280 TALLOC_FREE(fde);
281 return NULL;
285 fde->fde = fde_ev->fde;
286 return fde;
289 bool messaging_ctdb_fde_active(struct messaging_ctdb_fde *fde)
291 uint16_t flags;
293 if (fde == NULL) {
294 return false;
296 flags = tevent_fd_get_flags(fde->fde);
297 return (flags != 0);
300 struct ctdbd_connection *messaging_ctdb_connection(void)
302 if (global_ctdb_context == NULL) {
303 smb_panic("messaging not initialized\n");
305 return global_ctdb_context->conn;