s3:utils: Fix 'Usage:' for 'net ads enctypes'
[samba4-gss.git] / source3 / lib / messages_ctdb.c
blob5cbaf1c4b0e70382f76bff657d52c8891e189d7a
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/messages_util.h"
26 #include "ctdbd_conn.h"
27 #include "lib/cluster_support.h"
28 #include "ctdb_srvids.h"
30 struct messaging_ctdb_context;
33 * We can only have one tevent_fd per ctdb_context and per
34 * tevent_context. Maintain a list of registered tevent_contexts per
35 * ctdb_context.
37 struct messaging_ctdb_fde_ev {
38 struct messaging_ctdb_fde_ev *prev, *next;
41 * Backreference to enable DLIST_REMOVE from our
42 * destructor. Also, set to NULL when the ctdb_context dies
43 * before the messaging_ctdb_fde_ev.
45 struct messaging_ctdb_context *ctx;
47 struct tevent_context *ev;
48 struct tevent_fd *fde;
51 struct messaging_ctdb_context {
52 struct ctdbd_connection *conn;
54 void (*recv_cb)(struct tevent_context *ev,
55 const uint8_t *msg, size_t msg_len,
56 int *fds, size_t num_fds,
57 void *private_data);
58 void *recv_cb_private_data;
60 struct messaging_ctdb_fde_ev *fde_evs;
63 static int messaging_ctdb_recv(
64 struct tevent_context *ev,
65 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
66 const uint8_t *msg, size_t msg_len, void *private_data)
68 struct messaging_ctdb_context *state = talloc_get_type_abort(
69 private_data, struct messaging_ctdb_context);
71 state->recv_cb(ev, msg, msg_len, NULL, 0, state->recv_cb_private_data);
73 return 0;
76 struct messaging_ctdb_context *global_ctdb_context;
78 static int global_ctdb_ctx_destructor(struct messaging_ctdb_context *ctx)
80 if (ctx != NULL) {
81 struct messaging_ctdb_fde_ev *fde_ev = NULL;
82 for (fde_ev = ctx->fde_evs;
83 fde_ev != NULL;
84 fde_ev = fde_ev->next) {
85 if (fde_ev->ctx == ctx) {
86 fde_ev->ctx = NULL;
90 return 0;
93 int messaging_ctdb_init(const char *sockname, int timeout, uint64_t unique_id,
94 void (*recv_cb)(struct tevent_context *ev,
95 const uint8_t *msg, size_t msg_len,
96 int *fds, size_t num_fds,
97 void *private_data),
98 void *private_data)
100 struct messaging_ctdb_context *ctx;
101 int ret;
103 if (global_ctdb_context != NULL) {
104 return EBUSY;
107 ctx = talloc_zero(NULL, struct messaging_ctdb_context);
108 if (ctx == NULL) {
109 return ENOMEM;
112 talloc_set_destructor(ctx,
113 global_ctdb_ctx_destructor);
115 ctx->recv_cb = recv_cb;
116 ctx->recv_cb_private_data = private_data;
118 ret = ctdbd_init_connection(ctx, sockname, timeout, &ctx->conn);
119 if (ret != 0) {
120 DBG_DEBUG("ctdbd_init_connection returned %s\n",
121 strerror(ret));
122 goto fail;
125 ret = register_with_ctdbd(ctx->conn, tevent_cached_getpid(), messaging_ctdb_recv,
126 ctx);
127 if (ret != 0) {
128 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
129 strerror(ret), ret);
130 goto fail;
133 ret = register_with_ctdbd(ctx->conn, CTDB_SRVID_SAMBA_PROCESS,
134 messaging_ctdb_recv, ctx);
135 if (ret != 0) {
136 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
137 strerror(ret), ret);
138 goto fail;
141 ret = register_with_ctdbd(ctx->conn, unique_id, NULL, NULL);
142 if (ret != 0) {
143 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
144 strerror(ret), ret);
145 goto fail;
148 set_my_vnn(ctdbd_vnn(ctx->conn));
150 global_ctdb_context = ctx;
151 return 0;
152 fail:
153 TALLOC_FREE(ctx);
154 return ret;
157 void messaging_ctdb_destroy(void)
159 TALLOC_FREE(global_ctdb_context);
162 int messaging_ctdb_send(uint32_t dst_vnn, uint64_t dst_srvid,
163 const struct iovec *iov, int iovlen)
165 struct messaging_ctdb_context *ctx = global_ctdb_context;
166 int ret;
168 if (ctx == NULL) {
169 return ENOTCONN;
172 ret = ctdbd_messaging_send_iov(ctx->conn, dst_vnn, dst_srvid,
173 iov, iovlen);
174 return ret;
177 static void messaging_ctdb_read_handler(struct tevent_context *ev,
178 struct tevent_fd *fde,
179 uint16_t flags,
180 void *private_data)
182 struct messaging_ctdb_context *ctx = talloc_get_type_abort(
183 private_data, struct messaging_ctdb_context);
185 if ((flags & TEVENT_FD_READ) == 0) {
186 return;
188 ctdbd_socket_readable(ev, ctx->conn);
191 struct messaging_ctdb_fde {
192 struct tevent_fd *fde;
195 static int messaging_ctdb_fde_ev_destructor(
196 struct messaging_ctdb_fde_ev *fde_ev)
198 if (fde_ev->ctx != NULL) {
199 DLIST_REMOVE(fde_ev->ctx->fde_evs, fde_ev);
200 fde_ev->ctx = NULL;
202 return 0;
206 * Reference counter for a struct tevent_fd messaging read event
207 * (with callback function) on a struct tevent_context registered
208 * on a messaging context.
210 * If we've already registered this struct tevent_context before
211 * (so already have a read event), just increase the reference count.
213 * Otherwise create a new struct tevent_fd messaging read event on the
214 * previously unseen struct tevent_context - this is what drives
215 * the message receive processing.
219 struct messaging_ctdb_fde *messaging_ctdb_register_tevent_context(
220 TALLOC_CTX *mem_ctx, struct tevent_context *ev)
222 struct messaging_ctdb_context *ctx = global_ctdb_context;
223 struct messaging_ctdb_fde_ev *fde_ev;
224 struct messaging_ctdb_fde *fde;
226 if (ctx == NULL) {
227 return NULL;
230 fde = talloc(mem_ctx, struct messaging_ctdb_fde);
231 if (fde == NULL) {
232 return NULL;
235 for (fde_ev = ctx->fde_evs; fde_ev != NULL; fde_ev = fde_ev->next) {
236 if (tevent_fd_get_flags(fde_ev->fde) == 0) {
238 * If the event context got deleted,
239 * tevent_fd_get_flags() will return 0
240 * for the stale fde.
242 * In that case we should not
243 * use fde_ev->ev anymore.
245 continue;
247 if (fde_ev->ev == ev) {
248 break;
252 if (fde_ev == NULL) {
253 int sock = ctdbd_conn_get_fd(ctx->conn);
255 fde_ev = talloc(fde, struct messaging_ctdb_fde_ev);
256 if (fde_ev == NULL) {
257 return NULL;
259 fde_ev->fde = tevent_add_fd(
260 ev, fde_ev, sock, TEVENT_FD_READ,
261 messaging_ctdb_read_handler, ctx);
262 if (fde_ev->fde == NULL) {
263 TALLOC_FREE(fde);
264 return NULL;
266 fde_ev->ev = ev;
267 fde_ev->ctx = ctx;
268 DLIST_ADD(ctx->fde_evs, fde_ev);
269 talloc_set_destructor(
270 fde_ev, messaging_ctdb_fde_ev_destructor);
271 } else {
273 * Same trick as with tdb_wrap: The caller will never
274 * see the talloc_referenced object, the
275 * messaging_ctdb_fde_ev, so problems with
276 * talloc_unlink will not happen.
278 if (talloc_reference(fde, fde_ev) == NULL) {
279 TALLOC_FREE(fde);
280 return NULL;
284 fde->fde = fde_ev->fde;
285 return fde;
288 bool messaging_ctdb_fde_active(struct messaging_ctdb_fde *fde)
290 uint16_t flags;
292 if (fde == NULL) {
293 return false;
295 flags = tevent_fd_get_flags(fde->fde);
296 return (flags != 0);
299 struct ctdbd_connection *messaging_ctdb_connection(void)
301 if (global_ctdb_context == NULL) {
302 smb_panic("messaging not initialized\n");
304 return global_ctdb_context->conn;