messages_ctdbd: make sure a ctdbd connection is only used in the process that created it
[Samba.git] / source / lib / messages_ctdbd.c
blob0c8a2aa8e4404c9096969b53f64b78e3348fad78
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"
22 #ifdef CLUSTER_SUPPORT
24 #include "librpc/gen_ndr/messaging.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 (global_ctdb_connection_pid != getpid()) {
44 DEBUG(0,("messaging_ctdbd_connection():"
45 "valid for pid[%d] but it's [%d]\n",
46 global_ctdb_connection_pid, getpid()));
47 smb_panic("messaging_ctdbd_connection() invalid process\n");
50 return global_ctdbd_connection;
53 static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
54 struct server_id pid, int msg_type,
55 const DATA_BLOB *data,
56 struct messaging_backend *backend)
58 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
59 backend->private_data, struct messaging_ctdbd_context);
61 struct messaging_rec msg;
63 msg.msg_version = MESSAGE_VERSION;
64 msg.msg_type = msg_type;
65 msg.dest = pid;
66 msg.src = procid_self();
67 msg.buf = *data;
69 return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
72 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
75 * The global connection just went away
77 global_ctdb_connection_pid = 0;
78 global_ctdbd_connection = NULL;
79 return 0;
82 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
83 TALLOC_CTX *mem_ctx,
84 struct messaging_backend **presult)
86 struct messaging_backend *result;
87 struct messaging_ctdbd_context *ctx;
88 NTSTATUS status;
90 if (!(result = TALLOC_P(mem_ctx, struct messaging_backend))) {
91 DEBUG(0, ("talloc failed\n"));
92 return NT_STATUS_NO_MEMORY;
95 if (!(ctx = TALLOC_P(result, struct messaging_ctdbd_context))) {
96 DEBUG(0, ("talloc failed\n"));
97 TALLOC_FREE(result);
98 return NT_STATUS_NO_MEMORY;
101 status = ctdbd_messaging_connection(ctx, &ctx->conn);
103 if (!NT_STATUS_IS_OK(status)) {
104 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
105 nt_errstr(status)));
106 TALLOC_FREE(result);
107 return status;
110 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
112 if (!NT_STATUS_IS_OK(status)) {
113 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
114 nt_errstr(status)));
115 TALLOC_FREE(result);
116 return status;
119 global_ctdb_connection_pid = getpid();
120 global_ctdbd_connection = ctx->conn;
121 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
123 set_my_vnn(ctdbd_vnn(ctx->conn));
125 result->send_fn = messaging_ctdb_send;
126 result->private_data = (void *)ctx;
128 *presult = result;
129 return NT_STATUS_OK;
132 #else
134 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
135 TALLOC_CTX *mem_ctx,
136 struct messaging_backend **presult)
138 return NT_STATUS_NOT_IMPLEMENTED;
141 #endif