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