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/>.
23 #include "lib/util/iov_buf.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()) {
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
);
54 DEBUG(0,("samba_tevent_context_init failed\n"));
57 msg
= messaging_init(NULL
, ev
);
59 DEBUG(0,("messaging_init failed\n"));
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
,
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
;
92 buflen
= iov_buflen(iov
, iovlen
);
97 buf
= talloc_array(talloc_tos(), uint8_t, buflen
);
101 iov_buf(iov
, iovlen
, buf
, buflen
);
103 msg
= (struct messaging_rec
) {
104 .msg_version
= MESSAGE_VERSION
,
105 .msg_type
= msg_type
,
108 .buf
= data_blob_const(buf
, talloc_get_size(buf
)),
111 status
= ctdbd_messaging_send(ctx
->conn
, pid
.vnn
, pid
.pid
, &msg
);
115 if (NT_STATUS_IS_OK(status
)) {
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
;
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
;
136 enum ndr_err_code ndr_err
;
137 size_t len
= msg
->hdr
.length
;
139 if (len
< offsetof(struct ctdb_req_message
, data
)) {
142 len
-= offsetof(struct ctdb_req_message
, data
);
144 if (len
< msg
->datalen
) {
148 result
= talloc(mem_ctx
, struct messaging_rec
);
149 if (result
== 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
)));
166 if (DEBUGLEVEL
>= 11) {
167 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
168 NDR_PRINT_DEBUG(messaging_rec
, result
);
174 static void messaging_ctdb_recv(struct ctdb_req_message
*msg
,
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
;
184 rec
= ctdb_pull_messaging_rec(msg_ctx
, msg
);
186 DEBUG(10, ("%s: ctdb_pull_messaging_rec failed\n", __func__
));
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
)));
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);
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
,
215 struct messaging_backend
**presult
)
217 struct messaging_backend
*result
;
218 struct messaging_ctdbd_context
*ctx
;
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"));
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",
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",
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
;