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 NTSTATUS
messaging_ctdbd_init(struct messaging_context
*msg_ctx
,
133 struct messaging_backend
**presult
)
135 struct messaging_backend
*result
;
136 struct messaging_ctdbd_context
*ctx
;
139 if (!(result
= talloc(mem_ctx
, struct messaging_backend
))) {
140 DEBUG(0, ("talloc failed\n"));
141 return NT_STATUS_NO_MEMORY
;
144 if (!(ctx
= talloc(result
, struct messaging_ctdbd_context
))) {
145 DEBUG(0, ("talloc failed\n"));
147 return NT_STATUS_NO_MEMORY
;
150 status
= ctdbd_messaging_connection(ctx
, &ctx
->conn
);
152 if (!NT_STATUS_IS_OK(status
)) {
153 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
159 status
= ctdbd_register_msg_ctx(ctx
->conn
, msg_ctx
);
161 if (!NT_STATUS_IS_OK(status
)) {
162 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
168 global_ctdb_connection_pid
= getpid();
169 global_ctdbd_connection
= ctx
->conn
;
170 talloc_set_destructor(ctx
, messaging_ctdbd_destructor
);
172 set_my_vnn(ctdbd_vnn(ctx
->conn
));
174 result
->send_fn
= messaging_ctdb_send
;
175 result
->private_data
= (void *)ctx
;