ctdbd_conn: move CTDB_CONTROL_ENABLE_SEQNUM control to db_open_ctdb
[vlendec/samba-autobuild/.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 "lib/messages_ctdbd.h"
22 #include "lib/util/server_id.h"
23 #include "messages.h"
24 #include "util_tdb.h"
25 #include "lib/util/iov_buf.h"
26 #include "lib/messages_util.h"
27 #include "ctdbd_conn.h"
28 #include "lib/cluster_support.h"
29
30
31 struct messaging_ctdbd_context {
32         struct ctdbd_connection *conn;
33         struct tevent_fd *fde;
34 };
35
36 /*
37  * This is a Samba3 hack/optimization. Routines like process_exists need to
38  * talk to ctdbd, and they don't get handed a messaging context.
39  */
40 static struct ctdbd_connection *global_ctdbd_connection;
41 static int global_ctdb_connection_pid;
42
43 struct ctdbd_connection *messaging_ctdbd_connection(void)
44 {
45         if (!lp_clustering()) {
46                 return NULL;
47         }
48
49         if (global_ctdb_connection_pid == 0 &&
50             global_ctdbd_connection == NULL) {
51                 struct tevent_context *ev;
52                 struct messaging_context *msg;
53
54                 ev = samba_tevent_context_init(NULL);
55                 if (!ev) {
56                         DEBUG(0,("samba_tevent_context_init failed\n"));
57                         return NULL;
58                 }
59
60                 msg = messaging_init(NULL, ev);
61                 if (!msg) {
62                         DEBUG(0,("messaging_init failed\n"));
63                         return NULL;
64                 }
65         }
66
67         if (global_ctdb_connection_pid != getpid()) {
68                 DEBUG(0,("messaging_ctdbd_connection():"
69                          "valid for pid[%jd] but it's [%jd]\n",
70                          (intmax_t)global_ctdb_connection_pid,
71                          (intmax_t)getpid()));
72                 smb_panic("messaging_ctdbd_connection() invalid process\n");
73         }
74
75         return global_ctdbd_connection;
76 }
77
78 static int messaging_ctdb_send(struct server_id src,
79                                struct server_id pid, int msg_type,
80                                const struct iovec *iov, int iovlen,
81                                const int *fds, size_t num_fds,
82                                struct messaging_backend *backend)
83 {
84         struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
85                 backend->private_data, struct messaging_ctdbd_context);
86         uint8_t hdr[MESSAGE_HDR_LENGTH];
87         struct iovec iov2[iovlen+1];
88
89         if (num_fds > 0) {
90                 return ENOSYS;
91         }
92
93         message_hdr_put(hdr, msg_type, src, pid);
94         iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
95         memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
96
97         return ctdbd_messaging_send_iov(ctx->conn, pid.vnn, pid.pid,
98                                         iov2, iovlen+1);
99 }
100
101 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
102 {
103         /*
104          * The global connection just went away
105          */
106         global_ctdb_connection_pid = 0;
107         global_ctdbd_connection = NULL;
108         return 0;
109 }
110
111 static int 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)
114 {
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         int ret;
119         struct iovec iov;
120         struct server_id src, dst;
121         enum messaging_type msg_type;
122         struct server_id_buf idbuf;
123
124         if (msg_len < MESSAGE_HDR_LENGTH) {
125                 DEBUG(1, ("%s: message too short: %u\n", __func__,
126                           (unsigned)msg_len));
127                 return 0;
128         }
129
130         message_hdr_get(&msg_type, &src, &dst, msg);
131
132         iov = (struct iovec) {
133                 .iov_base = discard_const_p(uint8_t, msg) + MESSAGE_HDR_LENGTH,
134                 .iov_len = msg_len - MESSAGE_HDR_LENGTH
135         };
136
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)));
140
141         if (!server_id_same_process(&me, &dst)) {
142                 struct server_id_buf id1, id2;
143
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 0;
148         }
149
150         /*
151          * Go through the event loop
152          */
153
154         ret = messaging_send_iov_from(msg_ctx, src, dst, msg_type,
155                                       &iov, 1, NULL, 0);
156
157         if (ret != 0) {
158                 DEBUG(10, ("%s: messaging_send_iov_from failed: %s\n",
159                            __func__, strerror(ret)));
160         }
161
162         return 0;
163 }
164
165 static void messaging_ctdbd_readable(struct tevent_context *ev,
166                                      struct tevent_fd *fde,
167                                      uint16_t flags,
168                                      void *private_data)
169 {
170         struct ctdbd_connection *conn = talloc_get_type_abort(
171                 private_data, struct ctdbd_connection);
172
173         if ((flags & TEVENT_FD_READ) == 0) {
174                 return;
175         }
176         ctdbd_socket_readable(conn);
177 }
178
179 static int messaging_ctdbd_init_internal(struct messaging_context *msg_ctx,
180                                          TALLOC_CTX *mem_ctx,
181                                          struct messaging_ctdbd_context *ctx,
182                                          bool reinit)
183 {
184         struct tevent_context *ev;
185         int ret, ctdb_fd;
186
187         if (reinit) {
188                 TALLOC_FREE(ctx->fde);
189
190                 ret = ctdbd_reinit_connection(ctx,
191                                               lp_ctdbd_socket(),
192                                               lp_ctdb_timeout(),
193                                               ctx->conn);
194                 if (ret != 0) {
195                         DBG_ERR("ctdbd_reinit_connection failed: %s\n",
196                                 strerror(ret));
197                         return ret;
198                 }
199         } else {
200                 ret = ctdbd_init_connection(ctx,
201                                             lp_ctdbd_socket(),
202                                             lp_ctdb_timeout(),
203                                             &ctx->conn);
204                 if (ret != 0) {
205                         DBG_ERR("ctdbd_init_connection failed: %s\n",
206                                 strerror(ret));
207                         return ret;
208                 }
209         }
210
211         ret = register_with_ctdbd(ctx->conn, MSG_SRVID_SAMBA, NULL, NULL);
212         if (ret != 0) {
213                 DBG_DEBUG("Could not register MSG_SRVID_SAMBA: %s\n",
214                           strerror(ret));
215                 return ret;
216         }
217
218         ret = register_with_ctdbd(ctx->conn, getpid(),
219                                   messaging_ctdb_recv, msg_ctx);
220         if (ret != 0) {
221                 DEBUG(10, ("register_with_ctdbd failed: %s\n",
222                            strerror(ret)));
223                 return ret;
224         }
225
226         ctdb_fd = ctdbd_conn_get_fd(ctx->conn);
227         ev = messaging_tevent_context(msg_ctx);
228
229         ctx->fde = tevent_add_fd(ev, ctx, ctdb_fd, TEVENT_FD_READ,
230                                  messaging_ctdbd_readable, ctx->conn);
231         if (ctx->fde == NULL) {
232                 return ENOMEM;
233         }
234
235         global_ctdb_connection_pid = getpid();
236         global_ctdbd_connection = ctx->conn;
237         talloc_set_destructor(ctx, messaging_ctdbd_destructor);
238
239         set_my_vnn(ctdbd_vnn(ctx->conn));
240
241         return 0;
242 }
243
244 int messaging_ctdbd_init(struct messaging_context *msg_ctx,
245                          TALLOC_CTX *mem_ctx,
246                          struct messaging_backend **presult)
247 {
248         struct messaging_backend *result;
249         struct messaging_ctdbd_context *ctx;
250         int ret;
251
252         if (!(result = talloc(mem_ctx, struct messaging_backend))) {
253                 DEBUG(0, ("talloc failed\n"));
254                 return ENOMEM;
255         }
256
257         if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
258                 DEBUG(0, ("talloc failed\n"));
259                 TALLOC_FREE(result);
260                 return ENOMEM;
261         }
262
263         ret = messaging_ctdbd_init_internal(msg_ctx, mem_ctx, ctx, false);
264         if (ret != 0) {
265                 TALLOC_FREE(result);
266                 return ret;
267         }
268
269         result->send_fn = messaging_ctdb_send;
270         result->private_data = (void *)ctx;
271
272         *presult = result;
273         return 0;
274 }
275
276 int messaging_ctdbd_reinit(struct messaging_context *msg_ctx,
277                            TALLOC_CTX *mem_ctx,
278                            struct messaging_backend *backend)
279 {
280         struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
281                 backend->private_data, struct messaging_ctdbd_context);
282         int ret;
283
284         ret = messaging_ctdbd_init_internal(msg_ctx, mem_ctx, ctx, true);
285         if (ret != 0) {
286                 return ret;
287         }
288
289         return 0;
290 }