2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) 2007 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/>.
21 #include "lib/messages_ctdbd.h"
22 #include "lib/util/server_id.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"
31 struct messaging_ctdbd_context {
32 struct ctdbd_connection *conn;
33 struct tevent_fd *fde;
37 * This is a Samba3 hack/optimization. Routines like process_exists need to
38 * talk to ctdbd, and they don't get handed a messaging context.
40 static struct ctdbd_connection *global_ctdbd_connection;
41 static int global_ctdb_connection_pid;
43 struct ctdbd_connection *messaging_ctdbd_connection(void)
45 if (!lp_clustering()) {
49 if (global_ctdb_connection_pid == 0 &&
50 global_ctdbd_connection == NULL) {
51 struct tevent_context *ev;
52 struct messaging_context *msg;
54 ev = samba_tevent_context_init(NULL);
56 DEBUG(0,("samba_tevent_context_init failed\n"));
60 msg = messaging_init(NULL, ev);
62 DEBUG(0,("messaging_init failed\n"));
67 if (global_ctdb_connection_pid != getpid()) {
68 DEBUG(0,("messaging_ctdbd_connection():"
69 "valid for pid[%jd] but it's [%jd]\n",
70 (intmax_t)global_ctdb_connection_pid,
72 smb_panic("messaging_ctdbd_connection() invalid process\n");
75 return global_ctdbd_connection;
78 static int messaging_ctdb_send(struct server_id src,
79 struct server_id pid, int msg_type,
80 const struct iovec *iov, int iovlen,
81 const int *fds, size_t num_fds,
82 struct messaging_backend *backend)
84 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
85 backend->private_data, struct messaging_ctdbd_context);
86 uint8_t hdr[MESSAGE_HDR_LENGTH];
87 struct iovec iov2[iovlen+1];
93 message_hdr_put(hdr, msg_type, src, pid);
94 iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
95 memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
97 return ctdbd_messaging_send_iov(ctx->conn, pid.vnn, pid.pid,
101 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
104 * The global connection just went away
106 global_ctdb_connection_pid = 0;
107 global_ctdbd_connection = NULL;
111 static int messaging_ctdb_recv(
112 uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
113 const uint8_t *msg, size_t msg_len, void *private_data)
115 struct messaging_context *msg_ctx = talloc_get_type_abort(
116 private_data, struct messaging_context);
117 struct server_id me = messaging_server_id(msg_ctx);
120 struct server_id src, dst;
121 enum messaging_type msg_type;
122 struct server_id_buf idbuf;
124 if (msg_len < MESSAGE_HDR_LENGTH) {
125 DEBUG(1, ("%s: message too short: %u\n", __func__,
130 message_hdr_get(&msg_type, &src, &dst, msg);
132 iov = (struct iovec) {
133 .iov_base = discard_const_p(uint8_t, msg) + MESSAGE_HDR_LENGTH,
134 .iov_len = msg_len - MESSAGE_HDR_LENGTH
137 DEBUG(10, ("%s: Received message 0x%x len %u from %s\n",
138 __func__, (unsigned)msg_type, (unsigned)msg_len,
139 server_id_str_buf(src, &idbuf)));
141 if (!server_id_same_process(&me, &dst)) {
142 struct server_id_buf id1, id2;
144 DEBUG(10, ("%s: I'm %s, ignoring msg to %s\n", __func__,
145 server_id_str_buf(me, &id1),
146 server_id_str_buf(dst, &id2)));
151 * Go through the event loop
154 ret = messaging_send_iov_from(msg_ctx, src, dst, msg_type,
158 DEBUG(10, ("%s: messaging_send_iov_from failed: %s\n",
159 __func__, strerror(ret)));
165 static void messaging_ctdbd_readable(struct tevent_context *ev,
166 struct tevent_fd *fde,
170 struct ctdbd_connection *conn = talloc_get_type_abort(
171 private_data, struct ctdbd_connection);
173 if ((flags & TEVENT_FD_READ) == 0) {
176 ctdbd_socket_readable(conn);
179 static int messaging_ctdbd_init_internal(struct messaging_context *msg_ctx,
181 struct messaging_ctdbd_context *ctx,
184 struct tevent_context *ev;
188 TALLOC_FREE(ctx->fde);
190 ret = ctdbd_reinit_connection(ctx,
195 DBG_ERR("ctdbd_reinit_connection failed: %s\n",
200 ret = ctdbd_init_connection(ctx,
205 DBG_ERR("ctdbd_init_connection failed: %s\n",
211 ret = register_with_ctdbd(ctx->conn, MSG_SRVID_SAMBA, NULL, NULL);
213 DBG_DEBUG("Could not register MSG_SRVID_SAMBA: %s\n",
218 ret = register_with_ctdbd(ctx->conn, getpid(),
219 messaging_ctdb_recv, msg_ctx);
221 DEBUG(10, ("register_with_ctdbd failed: %s\n",
226 ctdb_fd = ctdbd_conn_get_fd(ctx->conn);
227 ev = messaging_tevent_context(msg_ctx);
229 ctx->fde = tevent_add_fd(ev, ctx, ctdb_fd, TEVENT_FD_READ,
230 messaging_ctdbd_readable, ctx->conn);
231 if (ctx->fde == NULL) {
235 global_ctdb_connection_pid = getpid();
236 global_ctdbd_connection = ctx->conn;
237 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
239 set_my_vnn(ctdbd_vnn(ctx->conn));
244 int messaging_ctdbd_init(struct messaging_context *msg_ctx,
246 struct messaging_backend **presult)
248 struct messaging_backend *result;
249 struct messaging_ctdbd_context *ctx;
252 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
253 DEBUG(0, ("talloc failed\n"));
257 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
258 DEBUG(0, ("talloc failed\n"));
263 ret = messaging_ctdbd_init_internal(msg_ctx, mem_ctx, ctx, false);
269 result->send_fn = messaging_ctdb_send;
270 result->private_data = (void *)ctx;
276 int messaging_ctdbd_reinit(struct messaging_context *msg_ctx,
278 struct messaging_backend *backend)
280 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
281 backend->private_data, struct messaging_ctdbd_context);
284 ret = messaging_ctdbd_init_internal(msg_ctx, mem_ctx, ctx, true);