summaryrefslogtreecommitdiffstats
path: root/source3/lib/messages_ctdb.c
blob: d55b53bf601e16ce5cc82f1c18e895abec349967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Unix SMB/CIFS implementation.
 * Samba internal messaging functions
 * Copyright (C) 2017 by Volker Lendecke
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "lib/messages_ctdb.h"
#include "lib/util/server_id.h"
#include "messages.h"
#include "util_tdb.h"
#include "lib/util/iov_buf.h"
#include "lib/messages_util.h"
#include "ctdbd_conn.h"
#include "lib/cluster_support.h"
#include "ctdb_srvids.h"

struct messaging_ctdb_context;

/*
 * We can only have one tevent_fd per ctdb_context and per
 * tevent_context. Maintain a list of registered tevent_contexts per
 * ctdb_context.
 */
struct messaging_ctdb_fde_ev {
	struct messaging_ctdb_fde_ev *prev, *next;

	/*
	 * Backreference to enable DLIST_REMOVE from our
	 * destructor. Also, set to NULL when the ctdb_context dies
	 * before the messaging_ctdb_fde_ev.
	 */
	struct messaging_ctdb_context *ctx;

	struct tevent_context *ev;
	struct tevent_fd *fde;
};

struct messaging_ctdb_context {
	struct ctdbd_connection *conn;

	void (*recv_cb)(struct tevent_context *ev,
			const uint8_t *msg, size_t msg_len,
			int *fds, size_t num_fds,
			void *private_data);
	void *recv_cb_private_data;

	struct messaging_ctdb_fde_ev *fde_evs;
};

static int messaging_ctdb_recv(
	struct tevent_context *ev,
	uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
	const uint8_t *msg, size_t msg_len, void *private_data)
{
	struct messaging_ctdb_context *state = talloc_get_type_abort(
		private_data, struct messaging_ctdb_context);

	state->recv_cb(ev, msg, msg_len, NULL, 0, state->recv_cb_private_data);

	return 0;
}

struct messaging_ctdb_context *global_ctdb_context;

static int global_ctdb_ctx_destructor(struct messaging_ctdb_context *ctx)
{
	if (ctx != NULL) {
		struct messaging_ctdb_fde_ev *fde_ev = NULL;
		for (fde_ev = ctx->fde_evs;
		     fde_ev != NULL;
		     fde_ev = fde_ev->next) {
			if (fde_ev->ctx == ctx) {
				fde_ev->ctx = NULL;
			}
		}
	}
	return 0;
}

int messaging_ctdb_init(const char *sockname, int timeout, uint64_t unique_id,
			void (*recv_cb)(struct tevent_context *ev,
					const uint8_t *msg, size_t msg_len,
					int *fds, size_t num_fds,
					void *private_data),
			void *private_data)
{
	struct messaging_ctdb_context *ctx;
	int ret;

	if (global_ctdb_context != NULL) {
		return EBUSY;
	}

	ctx = talloc_zero(NULL, struct messaging_ctdb_context);
	if (ctx == NULL) {
		return ENOMEM;
	}

	talloc_set_destructor(ctx,
			      global_ctdb_ctx_destructor);

	ctx->recv_cb = recv_cb;
	ctx->recv_cb_private_data = private_data;

	ret = ctdbd_init_connection(ctx, sockname, timeout, &ctx->conn);
	if (ret != 0) {
		DBG_DEBUG("ctdbd_init_connection returned %s\n",
			  strerror(ret));
		goto fail;
	}

	ret = register_with_ctdbd(ctx->conn, tevent_cached_getpid(), messaging_ctdb_recv,
				  ctx);
	if (ret != 0) {
		DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
			  strerror(ret), ret);
		goto fail;
	}

	ret = register_with_ctdbd(ctx->conn, CTDB_SRVID_SAMBA_PROCESS,
				  messaging_ctdb_recv, ctx);
	if (ret != 0) {
		DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
			  strerror(ret), ret);
		goto fail;
	}

	ret = register_with_ctdbd(ctx->conn, unique_id, NULL, NULL);
	if (ret != 0) {
		DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
			  strerror(ret), ret);
		goto fail;
	}

	set_my_vnn(ctdbd_vnn(ctx->conn));

	global_ctdb_context = ctx;
	return 0;
fail:
	TALLOC_FREE(ctx);
	return ret;
}

void messaging_ctdb_destroy(void)
{
	TALLOC_FREE(global_ctdb_context);
}

int messaging_ctdb_send(uint32_t dst_vnn, uint64_t dst_srvid,
			const struct iovec *iov, int iovlen)
{
	struct messaging_ctdb_context *ctx = global_ctdb_context;
	int ret;

	if (ctx == NULL) {
		return ENOTCONN;
	}

	ret = ctdbd_messaging_send_iov(ctx->conn, dst_vnn, dst_srvid,
				       iov, iovlen);
	return ret;
}

static void messaging_ctdb_read_handler(struct tevent_context *ev,
					struct tevent_fd *fde,
					uint16_t flags,
					void *private_data)
{
	struct messaging_ctdb_context *ctx = talloc_get_type_abort(
		private_data, struct messaging_ctdb_context);

	if ((flags & TEVENT_FD_READ) == 0) {
		return;
	}
	ctdbd_socket_readable(ev, ctx->conn);
}

struct messaging_ctdb_fde {
	struct tevent_fd *fde;
};

static int messaging_ctdb_fde_ev_destructor(
	struct messaging_ctdb_fde_ev *fde_ev)
{
	if (fde_ev->ctx != NULL) {
		DLIST_REMOVE(fde_ev->ctx->fde_evs, fde_ev);
		fde_ev->ctx = NULL;
	}
	return 0;
}

/*
 * Reference counter for a struct tevent_fd messaging read event
 * (with callback function) on a struct tevent_context registered
 * on a messaging context.
 *
 * If we've already registered this struct tevent_context before
 * (so already have a read event), just increase the reference count.
 *
 * Otherwise create a new struct tevent_fd messaging read event on the
 * previously unseen struct tevent_context - this is what drives
 * the message receive processing.
 *
 */

struct messaging_ctdb_fde *messaging_ctdb_register_tevent_context(
	TALLOC_CTX *mem_ctx, struct tevent_context *ev)
{
	struct messaging_ctdb_context *ctx = global_ctdb_context;
	struct messaging_ctdb_fde_ev *fde_ev;
	struct messaging_ctdb_fde *fde;

	if (ctx == NULL) {
		return NULL;
	}

	fde = talloc(mem_ctx, struct messaging_ctdb_fde);
	if (fde == NULL) {
		return NULL;
	}

	for (fde_ev = ctx->fde_evs; fde_ev != NULL; fde_ev = fde_ev->next) {
		if (tevent_fd_get_flags(fde_ev->fde) == 0) {
			/*
			 * If the event context got deleted,
			 * tevent_fd_get_flags() will return 0
			 * for the stale fde.
			 *
			 * In that case we should not
			 * use fde_ev->ev anymore.
			 */
			continue;
		}
		if (fde_ev->ev == ev) {
			break;
		}
	}

	if (fde_ev == NULL) {
		int sock = ctdbd_conn_get_fd(ctx->conn);

		fde_ev = talloc(fde, struct messaging_ctdb_fde_ev);
		if (fde_ev == NULL) {
			return NULL;
		}
		fde_ev->fde = tevent_add_fd(
			ev, fde_ev, sock, TEVENT_FD_READ,
			messaging_ctdb_read_handler, ctx);
		if (fde_ev->fde == NULL) {
			TALLOC_FREE(fde);
			return NULL;
		}
		fde_ev->ev = ev;
		fde_ev->ctx = ctx;
		DLIST_ADD(ctx->fde_evs, fde_ev);
		talloc_set_destructor(
			fde_ev, messaging_ctdb_fde_ev_destructor);
	} else {
		/*
		 * Same trick as with tdb_wrap: The caller will never
		 * see the talloc_referenced object, the
		 * messaging_ctdb_fde_ev, so problems with
		 * talloc_unlink will not happen.
		 */
		if (talloc_reference(fde, fde_ev) == NULL) {
			TALLOC_FREE(fde);
			return NULL;
		}
	}

	fde->fde = fde_ev->fde;
	return fde;
}

bool messaging_ctdb_fde_active(struct messaging_ctdb_fde *fde)
{
	uint16_t flags;

	if (fde == NULL) {
		return false;
	}
	flags = tevent_fd_get_flags(fde->fde);
	return (flags != 0);
}

struct ctdbd_connection *messaging_ctdb_connection(void)
{
	if (global_ctdb_context == NULL) {
		smb_panic("messaging not initialized\n");
	}
	return global_ctdb_context->conn;
}