ldb:ldb.c/"ldb_request" - make "ldb_request" always return an error string
[Samba/id10ts.git] / source3 / lib / messages_ctdbd.c
blob9e092bc699f89dfd736c096eeec6e26e206554ed
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"
24 #ifdef CLUSTER_SUPPORT
27 * It is not possible to include ctdb.h and tdb_compat.h (included via
28 * some other include above) without warnings. This fixes those
29 * warnings.
32 #ifdef typesafe_cb
33 #undef typesafe_cb
34 #endif
36 #ifdef typesafe_cb_preargs
37 #undef typesafe_cb_preargs
38 #endif
40 #ifdef typesafe_cb_postargs
41 #undef typesafe_cb_postargs
42 #endif
44 #include "ctdb.h"
45 #include "ctdb_private.h"
46 #include "ctdbd_conn.h"
49 struct messaging_ctdbd_context {
50 struct ctdbd_connection *conn;
54 * This is a Samba3 hack/optimization. Routines like process_exists need to
55 * talk to ctdbd, and they don't get handed a messaging context.
57 static struct ctdbd_connection *global_ctdbd_connection;
58 static int global_ctdb_connection_pid;
60 struct ctdbd_connection *messaging_ctdbd_connection(void)
62 if (global_ctdb_connection_pid == 0 &&
63 global_ctdbd_connection == NULL) {
64 struct event_context *ev;
65 struct messaging_context *msg;
67 ev = event_context_init(NULL);
68 if (!ev) {
69 DEBUG(0,("event_context_init failed\n"));
72 msg = messaging_init(NULL, procid_self(), ev);
73 if (!msg) {
74 DEBUG(0,("messaging_init failed\n"));
75 return NULL;
79 if (global_ctdb_connection_pid != getpid()) {
80 DEBUG(0,("messaging_ctdbd_connection():"
81 "valid for pid[%d] but it's [%d]\n",
82 global_ctdb_connection_pid, getpid()));
83 smb_panic("messaging_ctdbd_connection() invalid process\n");
86 return global_ctdbd_connection;
89 static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
90 struct server_id pid, int msg_type,
91 const DATA_BLOB *data,
92 struct messaging_backend *backend)
94 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
95 backend->private_data, struct messaging_ctdbd_context);
97 struct messaging_rec msg;
99 msg.msg_version = MESSAGE_VERSION;
100 msg.msg_type = msg_type;
101 msg.dest = pid;
102 msg.src = msg_ctx->id;
103 msg.buf = *data;
105 return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
108 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
111 * The global connection just went away
113 global_ctdb_connection_pid = 0;
114 global_ctdbd_connection = NULL;
115 return 0;
118 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
119 TALLOC_CTX *mem_ctx,
120 struct messaging_backend **presult)
122 struct messaging_backend *result;
123 struct messaging_ctdbd_context *ctx;
124 NTSTATUS status;
126 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
127 DEBUG(0, ("talloc failed\n"));
128 return NT_STATUS_NO_MEMORY;
131 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
132 DEBUG(0, ("talloc failed\n"));
133 TALLOC_FREE(result);
134 return NT_STATUS_NO_MEMORY;
137 status = ctdbd_messaging_connection(ctx, &ctx->conn);
139 if (!NT_STATUS_IS_OK(status)) {
140 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
141 nt_errstr(status)));
142 TALLOC_FREE(result);
143 return status;
146 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
148 if (!NT_STATUS_IS_OK(status)) {
149 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
150 nt_errstr(status)));
151 TALLOC_FREE(result);
152 return status;
155 global_ctdb_connection_pid = getpid();
156 global_ctdbd_connection = ctx->conn;
157 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
159 set_my_vnn(ctdbd_vnn(ctx->conn));
161 result->send_fn = messaging_ctdb_send;
162 result->private_data = (void *)ctx;
164 *presult = result;
165 return NT_STATUS_OK;
168 #else
170 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
171 TALLOC_CTX *mem_ctx,
172 struct messaging_backend **presult)
174 return NT_STATUS_NOT_IMPLEMENTED;
177 #endif