s3: Slightly simplify db_ctdb_marshall_loop_next
[kai/samba.git] / source3 / lib / messages_ctdbd.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba internal messaging functions
4    Copyright (C) 2007 by Volker Lendecke
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "messages.h"
22 #include "util_tdb.h"
23
24 #ifdef CLUSTER_SUPPORT
25
26 /*
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.
30  */
31
32 #ifdef typesafe_cb
33 #undef typesafe_cb
34 #endif
35
36 #ifdef typesafe_cb_preargs
37 #undef typesafe_cb_preargs
38 #endif
39
40 #ifdef typesafe_cb_postargs
41 #undef typesafe_cb_postargs
42 #endif
43
44 #include "ctdb.h"
45 #include "ctdb_private.h"
46 #include "ctdbd_conn.h"
47
48
49 struct messaging_ctdbd_context {
50         struct ctdbd_connection *conn;
51 };
52
53 /*
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.
56  */
57 static struct ctdbd_connection *global_ctdbd_connection;
58 static int global_ctdb_connection_pid;
59
60 struct ctdbd_connection *messaging_ctdbd_connection(void)
61 {
62         if (!lp_clustering()) {
63                 return NULL;
64         }
65
66         if (global_ctdb_connection_pid == 0 &&
67             global_ctdbd_connection == NULL) {
68                 struct event_context *ev;
69                 struct messaging_context *msg;
70
71                 ev = event_context_init(NULL);
72                 if (!ev) {
73                         DEBUG(0,("event_context_init failed\n"));
74                 }
75
76                 msg = messaging_init(NULL, ev);
77                 if (!msg) {
78                         DEBUG(0,("messaging_init failed\n"));
79                         return NULL;
80                 }
81         }
82
83         if (global_ctdb_connection_pid != getpid()) {
84                 DEBUG(0,("messaging_ctdbd_connection():"
85                          "valid for pid[%d] but it's [%d]\n",
86                          global_ctdb_connection_pid, getpid()));
87                 smb_panic("messaging_ctdbd_connection() invalid process\n");
88         }
89
90         return global_ctdbd_connection;
91 }
92
93 static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
94                                     struct server_id pid, int msg_type,
95                                     const DATA_BLOB *data,
96                                     struct messaging_backend *backend)
97 {
98         struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
99                 backend->private_data, struct messaging_ctdbd_context);
100
101         struct messaging_rec msg;
102
103         msg.msg_version = MESSAGE_VERSION;
104         msg.msg_type    = msg_type;
105         msg.dest        = pid;
106         msg.src         = msg_ctx->id;
107         msg.buf         = *data;
108
109         return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
110 }
111
112 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
113 {
114         /*
115          * The global connection just went away
116          */
117         global_ctdb_connection_pid = 0;
118         global_ctdbd_connection = NULL;
119         return 0;
120 }
121
122 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
123                               TALLOC_CTX *mem_ctx,
124                               struct messaging_backend **presult)
125 {
126         struct messaging_backend *result;
127         struct messaging_ctdbd_context *ctx;
128         NTSTATUS status;
129
130         if (!(result = talloc(mem_ctx, struct messaging_backend))) {
131                 DEBUG(0, ("talloc failed\n"));
132                 return NT_STATUS_NO_MEMORY;
133         }
134
135         if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
136                 DEBUG(0, ("talloc failed\n"));
137                 TALLOC_FREE(result);
138                 return NT_STATUS_NO_MEMORY;
139         }
140
141         status = ctdbd_messaging_connection(ctx, &ctx->conn);
142
143         if (!NT_STATUS_IS_OK(status)) {
144                 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
145                            nt_errstr(status)));
146                 TALLOC_FREE(result);
147                 return status;
148         }
149
150         status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
151
152         if (!NT_STATUS_IS_OK(status)) {
153                 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
154                            nt_errstr(status)));
155                 TALLOC_FREE(result);
156                 return status;
157         }
158
159         global_ctdb_connection_pid = getpid();
160         global_ctdbd_connection = ctx->conn;
161         talloc_set_destructor(ctx, messaging_ctdbd_destructor);
162
163         set_my_vnn(ctdbd_vnn(ctx->conn));
164
165         result->send_fn = messaging_ctdb_send;
166         result->private_data = (void *)ctx;
167
168         *presult = result;
169         return NT_STATUS_OK;
170 }
171
172 #else
173
174 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
175                               TALLOC_CTX *mem_ctx,
176                               struct messaging_backend **presult)
177 {
178         return NT_STATUS_NOT_IMPLEMENTED;
179 }
180
181 struct ctdbd_connection *messaging_ctdbd_connection(void)
182 {
183         return NULL;
184 }
185
186 #endif