libcli/smb: make sure we remove the writev_send() request when a request is destroyed
[Samba.git] / source3 / lib / messages_ctdbd.c
blobb4f4d63c6dbad8746b6a15c9aed6ee550f6713a9
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"
24 #include "lib/messages_util.h"
25 #include "ctdbd_conn.h"
28 struct messaging_ctdbd_context {
29 struct ctdbd_connection *conn;
33 * This is a Samba3 hack/optimization. Routines like process_exists need to
34 * talk to ctdbd, and they don't get handed a messaging context.
36 static struct ctdbd_connection *global_ctdbd_connection;
37 static int global_ctdb_connection_pid;
39 struct ctdbd_connection *messaging_ctdbd_connection(void)
41 if (!lp_clustering()) {
42 return NULL;
45 if (global_ctdb_connection_pid == 0 &&
46 global_ctdbd_connection == NULL) {
47 struct tevent_context *ev;
48 struct messaging_context *msg;
50 ev = samba_tevent_context_init(NULL);
51 if (!ev) {
52 DEBUG(0,("samba_tevent_context_init failed\n"));
55 msg = messaging_init(NULL, ev);
56 if (!msg) {
57 DEBUG(0,("messaging_init failed\n"));
58 return NULL;
62 if (global_ctdb_connection_pid != getpid()) {
63 DEBUG(0,("messaging_ctdbd_connection():"
64 "valid for pid[%jd] but it's [%jd]\n",
65 (intmax_t)global_ctdb_connection_pid,
66 (intmax_t)getpid()));
67 smb_panic("messaging_ctdbd_connection() invalid process\n");
70 return global_ctdbd_connection;
73 static int messaging_ctdb_send(struct server_id src,
74 struct server_id pid, int msg_type,
75 const struct iovec *iov, int iovlen,
76 const int *fds, size_t num_fds,
77 struct messaging_backend *backend)
79 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
80 backend->private_data, struct messaging_ctdbd_context);
81 uint8_t hdr[MESSAGE_HDR_LENGTH];
82 struct iovec iov2[iovlen+1];
83 NTSTATUS status;
85 if (num_fds > 0) {
86 return ENOSYS;
89 message_hdr_put(hdr, msg_type, src, pid);
90 iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
91 memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
93 status = ctdbd_messaging_send_iov(ctx->conn, pid.vnn, pid.pid,
94 iov2, iovlen+1);
95 if (NT_STATUS_IS_OK(status)) {
96 return 0;
98 return map_errno_from_nt_status(status);
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;
108 return 0;
111 static void 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);
118 NTSTATUS status;
119 struct iovec iov;
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__,
126 (unsigned)msg_len));
127 return;
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)));
147 return;
151 * Go through the event loop
154 status = messaging_send_iov_from(msg_ctx, src, dst, msg_type,
155 &iov, 1, NULL, 0);
157 if (!NT_STATUS_IS_OK(status)) {
158 DEBUG(10, ("%s: messaging_send_iov_from failed: %s\n",
159 __func__, nt_errstr(status)));
163 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
164 TALLOC_CTX *mem_ctx,
165 struct messaging_backend **presult)
167 struct messaging_backend *result;
168 struct messaging_ctdbd_context *ctx;
169 NTSTATUS status;
171 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
172 DEBUG(0, ("talloc failed\n"));
173 return NT_STATUS_NO_MEMORY;
176 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
177 DEBUG(0, ("talloc failed\n"));
178 TALLOC_FREE(result);
179 return NT_STATUS_NO_MEMORY;
182 status = ctdbd_messaging_connection(ctx, &ctx->conn);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
186 nt_errstr(status)));
187 TALLOC_FREE(result);
188 return status;
191 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
195 nt_errstr(status)));
196 TALLOC_FREE(result);
197 return status;
200 status = register_with_ctdbd(ctx->conn, getpid(),
201 messaging_ctdb_recv, msg_ctx);
203 global_ctdb_connection_pid = getpid();
204 global_ctdbd_connection = ctx->conn;
205 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
207 set_my_vnn(ctdbd_vnn(ctx->conn));
209 result->send_fn = messaging_ctdb_send;
210 result->private_data = (void *)ctx;
212 *presult = result;
213 return NT_STATUS_OK;