ctdbd_conn: Move message handling out of ctdbd_conn.c
[Samba.git] / source3 / lib / messages_ctdbd.c
blob430dc51dbffb606da3234a57f9803ec5acb337a4
1 /*
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/>.
20 #include "includes.h"
21 #include "messages.h"
22 #include "util_tdb.h"
23 #include "lib/util/iov_buf.h"
25 #include "ctdb.h"
26 #include "ctdb_private.h"
27 #include "ctdbd_conn.h"
30 struct messaging_ctdbd_context {
31 struct ctdbd_connection *conn;
35 * This is a Samba3 hack/optimization. Routines like process_exists need to
36 * talk to ctdbd, and they don't get handed a messaging context.
38 static struct ctdbd_connection *global_ctdbd_connection;
39 static int global_ctdb_connection_pid;
41 struct ctdbd_connection *messaging_ctdbd_connection(void)
43 if (!lp_clustering()) {
44 return NULL;
47 if (global_ctdb_connection_pid == 0 &&
48 global_ctdbd_connection == NULL) {
49 struct tevent_context *ev;
50 struct messaging_context *msg;
52 ev = samba_tevent_context_init(NULL);
53 if (!ev) {
54 DEBUG(0,("samba_tevent_context_init failed\n"));
57 msg = messaging_init(NULL, ev);
58 if (!msg) {
59 DEBUG(0,("messaging_init failed\n"));
60 return NULL;
64 if (global_ctdb_connection_pid != getpid()) {
65 DEBUG(0,("messaging_ctdbd_connection():"
66 "valid for pid[%jd] but it's [%jd]\n",
67 (intmax_t)global_ctdb_connection_pid,
68 (intmax_t)getpid()));
69 smb_panic("messaging_ctdbd_connection() invalid process\n");
72 return global_ctdbd_connection;
75 static int messaging_ctdb_send(struct server_id src,
76 struct server_id pid, int msg_type,
77 const struct iovec *iov, int iovlen,
78 const int *fds, size_t num_fds,
79 struct messaging_backend *backend)
81 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
82 backend->private_data, struct messaging_ctdbd_context);
83 struct messaging_rec msg;
84 uint8_t *buf;
85 ssize_t buflen;
86 NTSTATUS status;
88 if (num_fds > 0) {
89 return ENOSYS;
92 buflen = iov_buflen(iov, iovlen);
93 if (buflen == -1) {
94 return EMSGSIZE;
97 buf = talloc_array(talloc_tos(), uint8_t, buflen);
98 if (buf == NULL) {
99 return ENOMEM;
101 iov_buf(iov, iovlen, buf, buflen);
103 msg = (struct messaging_rec) {
104 .msg_version = MESSAGE_VERSION,
105 .msg_type = msg_type,
106 .dest = pid,
107 .src = src,
108 .buf = data_blob_const(buf, talloc_get_size(buf)),
111 status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
113 TALLOC_FREE(buf);
115 if (NT_STATUS_IS_OK(status)) {
116 return 0;
118 return map_errno_from_nt_status(status);
121 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
124 * The global connection just went away
126 global_ctdb_connection_pid = 0;
127 global_ctdbd_connection = NULL;
128 return 0;
131 static struct messaging_rec *ctdb_pull_messaging_rec(
132 TALLOC_CTX *mem_ctx, const struct ctdb_req_message *msg)
134 struct messaging_rec *result;
135 DATA_BLOB blob;
136 enum ndr_err_code ndr_err;
137 size_t len = msg->hdr.length;
139 if (len < offsetof(struct ctdb_req_message, data)) {
140 return NULL;
142 len -= offsetof(struct ctdb_req_message, data);
144 if (len < msg->datalen) {
145 return NULL;
148 result = talloc(mem_ctx, struct messaging_rec);
149 if (result == NULL) {
150 return NULL;
153 blob = data_blob_const(msg->data, msg->datalen);
155 ndr_err = ndr_pull_struct_blob_all(
156 &blob, result, result,
157 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
159 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
160 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
161 ndr_errstr(ndr_err)));
162 TALLOC_FREE(result);
163 return NULL;
166 if (DEBUGLEVEL >= 11) {
167 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
168 NDR_PRINT_DEBUG(messaging_rec, result);
171 return result;
174 static void messaging_ctdb_recv(struct ctdb_req_message *msg,
175 void *private_data)
177 struct messaging_context *msg_ctx = talloc_get_type_abort(
178 private_data, struct messaging_context);
179 struct server_id me = messaging_server_id(msg_ctx);
180 struct messaging_rec *rec;
181 NTSTATUS status;
182 struct iovec iov;
184 rec = ctdb_pull_messaging_rec(msg_ctx, msg);
185 if (rec == NULL) {
186 DEBUG(10, ("%s: ctdb_pull_messaging_rec failed\n", __func__));
187 return;
190 if (!server_id_same_process(&me, &rec->dest)) {
191 struct server_id_buf id1, id2;
193 DEBUG(10, ("%s: I'm %s, ignoring msg to %s\n", __func__,
194 server_id_str_buf(me, &id1),
195 server_id_str_buf(rec->dest, &id2)));
196 TALLOC_FREE(rec);
197 return;
200 iov = (struct iovec) { .iov_base = rec->buf.data,
201 .iov_len = rec->buf.length };
203 status = messaging_send_iov_from(msg_ctx, rec->src, rec->dest,
204 rec->msg_type, &iov, 1, NULL, 0);
205 TALLOC_FREE(rec);
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(10, ("%s: messaging_send_iov_from failed: %s\n",
209 __func__, nt_errstr(status)));
213 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
214 TALLOC_CTX *mem_ctx,
215 struct messaging_backend **presult)
217 struct messaging_backend *result;
218 struct messaging_ctdbd_context *ctx;
219 NTSTATUS status;
221 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
222 DEBUG(0, ("talloc failed\n"));
223 return NT_STATUS_NO_MEMORY;
226 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
227 DEBUG(0, ("talloc failed\n"));
228 TALLOC_FREE(result);
229 return NT_STATUS_NO_MEMORY;
232 status = ctdbd_messaging_connection(ctx, &ctx->conn);
234 if (!NT_STATUS_IS_OK(status)) {
235 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
236 nt_errstr(status)));
237 TALLOC_FREE(result);
238 return status;
241 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
243 if (!NT_STATUS_IS_OK(status)) {
244 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
245 nt_errstr(status)));
246 TALLOC_FREE(result);
247 return status;
250 status = register_with_ctdbd(ctx->conn, getpid(),
251 messaging_ctdb_recv, msg_ctx);
253 global_ctdb_connection_pid = getpid();
254 global_ctdbd_connection = ctx->conn;
255 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
257 set_my_vnn(ctdbd_vnn(ctx->conn));
259 result->send_fn = messaging_ctdb_send;
260 result->private_data = (void *)ctx;
262 *presult = result;
263 return NT_STATUS_OK;