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