ctdb-daemon: Do not allow database detach if AllowClientDBAttach=1
[Samba.git] / source3 / lib / messages_ctdbd.c
blob230560f8cfee03039ac9e149feb6f81e3b4d5a07
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"
25 * It is not possible to include ctdb.h and tdb_compat.h (included via
26 * some other include above) without warnings. This fixes those
27 * warnings.
30 #ifdef typesafe_cb
31 #undef typesafe_cb
32 #endif
34 #ifdef typesafe_cb_preargs
35 #undef typesafe_cb_preargs
36 #endif
38 #ifdef typesafe_cb_postargs
39 #undef typesafe_cb_postargs
40 #endif
42 #include "ctdb.h"
43 #include "ctdb_private.h"
44 #include "ctdbd_conn.h"
47 struct messaging_ctdbd_context {
48 struct ctdbd_connection *conn;
52 * This is a Samba3 hack/optimization. Routines like process_exists need to
53 * talk to ctdbd, and they don't get handed a messaging context.
55 static struct ctdbd_connection *global_ctdbd_connection;
56 static int global_ctdb_connection_pid;
58 struct ctdbd_connection *messaging_ctdbd_connection(void)
60 if (!lp_clustering()) {
61 return NULL;
64 if (global_ctdb_connection_pid == 0 &&
65 global_ctdbd_connection == NULL) {
66 struct tevent_context *ev;
67 struct messaging_context *msg;
69 ev = samba_tevent_context_init(NULL);
70 if (!ev) {
71 DEBUG(0,("samba_tevent_context_init failed\n"));
74 msg = messaging_init(NULL, ev);
75 if (!msg) {
76 DEBUG(0,("messaging_init failed\n"));
77 return NULL;
81 if (global_ctdb_connection_pid != getpid()) {
82 DEBUG(0,("messaging_ctdbd_connection():"
83 "valid for pid[%d] but it's [%d]\n",
84 global_ctdb_connection_pid, getpid()));
85 smb_panic("messaging_ctdbd_connection() invalid process\n");
88 return global_ctdbd_connection;
91 static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
92 struct server_id pid, int msg_type,
93 const DATA_BLOB *data,
94 struct messaging_backend *backend)
96 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
97 backend->private_data, struct messaging_ctdbd_context);
99 struct messaging_rec msg;
101 msg.msg_version = MESSAGE_VERSION;
102 msg.msg_type = msg_type;
103 msg.dest = pid;
104 msg.src = msg_ctx->id;
105 msg.buf = *data;
107 return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
110 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
113 * The global connection just went away
115 global_ctdb_connection_pid = 0;
116 global_ctdbd_connection = NULL;
117 return 0;
120 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
121 TALLOC_CTX *mem_ctx,
122 struct messaging_backend **presult)
124 struct messaging_backend *result;
125 struct messaging_ctdbd_context *ctx;
126 NTSTATUS status;
128 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
129 DEBUG(0, ("talloc failed\n"));
130 return NT_STATUS_NO_MEMORY;
133 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
134 DEBUG(0, ("talloc failed\n"));
135 TALLOC_FREE(result);
136 return NT_STATUS_NO_MEMORY;
139 status = ctdbd_messaging_connection(ctx, &ctx->conn);
141 if (!NT_STATUS_IS_OK(status)) {
142 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
143 nt_errstr(status)));
144 TALLOC_FREE(result);
145 return status;
148 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
150 if (!NT_STATUS_IS_OK(status)) {
151 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
152 nt_errstr(status)));
153 TALLOC_FREE(result);
154 return status;
157 global_ctdb_connection_pid = getpid();
158 global_ctdbd_connection = ctx->conn;
159 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
161 set_my_vnn(ctdbd_vnn(ctx->conn));
163 result->send_fn = messaging_ctdb_send;
164 result->private_data = (void *)ctx;
166 *presult = result;
167 return NT_STATUS_OK;