Revert "smbd: add an effective connection_struct->user_ev_ctx that holds the event...
[samba.git] / source3 / lib / messages_ctdb.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba internal messaging functions
4  * Copyright (C) 2017 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_ctdb.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 #include "ctdb_srvids.h"
30
31 struct messaging_ctdb_context;
32
33 /*
34  * We can only have one tevent_fd per ctdb_context and per
35  * tevent_context. Maintain a list of registered tevent_contexts per
36  * ctdb_context.
37  */
38 struct messaging_ctdb_fde_ev {
39         struct messaging_ctdb_fde_ev *prev, *next;
40
41         /*
42          * Backreference to enable DLIST_REMOVE from our
43          * destructor. Also, set to NULL when the ctdb_context dies
44          * before the messaging_ctdb_fde_ev.
45          */
46         struct messaging_ctdb_context *ctx;
47
48         struct tevent_context *ev;
49         struct tevent_fd *fde;
50 };
51
52 struct messaging_ctdb_context {
53         struct ctdbd_connection *conn;
54
55         void (*recv_cb)(struct tevent_context *ev,
56                         const uint8_t *msg, size_t msg_len,
57                         int *fds, size_t num_fds,
58                         void *private_data);
59         void *recv_cb_private_data;
60
61         struct messaging_ctdb_fde_ev *fde_evs;
62 };
63
64 static int messaging_ctdb_recv(
65         struct tevent_context *ev,
66         uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
67         const uint8_t *msg, size_t msg_len, void *private_data)
68 {
69         struct messaging_ctdb_context *state = talloc_get_type_abort(
70                 private_data, struct messaging_ctdb_context);
71
72         state->recv_cb(ev, msg, msg_len, NULL, 0, state->recv_cb_private_data);
73
74         return 0;
75 }
76
77 struct messaging_ctdb_context *global_ctdb_context;
78
79 int messaging_ctdb_init(const char *sockname, int timeout, uint64_t unique_id,
80                         void (*recv_cb)(struct tevent_context *ev,
81                                         const uint8_t *msg, size_t msg_len,
82                                         int *fds, size_t num_fds,
83                                         void *private_data),
84                         void *private_data)
85 {
86         struct messaging_ctdb_context *ctx;
87         int ret;
88
89         if (global_ctdb_context != NULL) {
90                 return EBUSY;
91         }
92
93         ctx = talloc_zero(NULL, struct messaging_ctdb_context);
94         if (ctx == NULL) {
95                 return ENOMEM;
96         }
97         ctx->recv_cb = recv_cb;
98         ctx->recv_cb_private_data = private_data;
99
100         ret = ctdbd_init_connection(ctx, sockname, timeout, &ctx->conn);
101         if (ret != 0) {
102                 DBG_DEBUG("ctdbd_init_connection returned %s\n",
103                           strerror(ret));
104                 goto fail;
105         }
106
107         ret = register_with_ctdbd(ctx->conn, getpid(), messaging_ctdb_recv,
108                                   ctx);
109         if (ret != 0) {
110                 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
111                           strerror(ret), ret);
112                 goto fail;
113         }
114
115         ret = register_with_ctdbd(ctx->conn, CTDB_SRVID_SAMBA_PROCESS,
116                                   messaging_ctdb_recv, ctx);
117         if (ret != 0) {
118                 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
119                           strerror(ret), ret);
120                 goto fail;
121         }
122
123         ret = register_with_ctdbd(ctx->conn, unique_id, NULL, NULL);
124         if (ret != 0) {
125                 DBG_DEBUG("register_with_ctdbd returned %s (%d)\n",
126                           strerror(ret), ret);
127                 goto fail;
128         }
129
130         set_my_vnn(ctdbd_vnn(ctx->conn));
131
132         global_ctdb_context = ctx;
133         return 0;
134 fail:
135         TALLOC_FREE(ctx);
136         return ret;
137 }
138
139 void messaging_ctdb_destroy(void)
140 {
141         TALLOC_FREE(global_ctdb_context);
142 }
143
144 int messaging_ctdb_send(uint32_t dst_vnn, uint64_t dst_srvid,
145                         const struct iovec *iov, int iovlen)
146 {
147         struct messaging_ctdb_context *ctx = global_ctdb_context;
148         int ret;
149
150         if (ctx == NULL) {
151                 return ENOTCONN;
152         }
153
154         ret = ctdbd_messaging_send_iov(ctx->conn, dst_vnn, dst_srvid,
155                                        iov, iovlen);
156         return ret;
157 }
158
159 static void messaging_ctdb_read_handler(struct tevent_context *ev,
160                                         struct tevent_fd *fde,
161                                         uint16_t flags,
162                                         void *private_data)
163 {
164         struct messaging_ctdb_context *ctx = talloc_get_type_abort(
165                 private_data, struct messaging_ctdb_context);
166
167         if ((flags & TEVENT_FD_READ) == 0) {
168                 return;
169         }
170         ctdbd_socket_readable(ev, ctx->conn);
171 }
172
173 struct messaging_ctdb_fde {
174         struct tevent_fd *fde;
175 };
176
177 static int messaging_ctdb_fde_ev_destructor(
178         struct messaging_ctdb_fde_ev *fde_ev)
179 {
180         if (fde_ev->ctx != NULL) {
181                 DLIST_REMOVE(fde_ev->ctx->fde_evs, fde_ev);
182                 fde_ev->ctx = NULL;
183         }
184         return 0;
185 }
186
187 /*
188  * Reference counter for a struct tevent_fd messaging read event
189  * (with callback function) on a struct tevent_context registered
190  * on a messaging context.
191  *
192  * If we've already registered this struct tevent_context before
193  * (so already have a read event), just increase the reference count.
194  *
195  * Otherwise create a new struct tevent_fd messaging read event on the
196  * previously unseen struct tevent_context - this is what drives
197  * the message receive processing.
198  *
199  */
200
201 struct messaging_ctdb_fde *messaging_ctdb_register_tevent_context(
202         TALLOC_CTX *mem_ctx, struct tevent_context *ev)
203 {
204         struct messaging_ctdb_context *ctx = global_ctdb_context;
205         struct messaging_ctdb_fde_ev *fde_ev;
206         struct messaging_ctdb_fde *fde;
207
208         if (ctx == NULL) {
209                 return NULL;
210         }
211
212         fde = talloc(mem_ctx, struct messaging_ctdb_fde);
213         if (fde == NULL) {
214                 return NULL;
215         }
216
217         for (fde_ev = ctx->fde_evs; fde_ev != NULL; fde_ev = fde_ev->next) {
218                 if (tevent_fd_get_flags(fde_ev->fde) == 0) {
219                         /*
220                          * If the event context got deleted,
221                          * tevent_fd_get_flags() will return 0
222                          * for the stale fde.
223                          *
224                          * In that case we should not
225                          * use fde_ev->ev anymore.
226                          */
227                         continue;
228                 }
229                 if (fde_ev->ev == ev) {
230                         break;
231                 }
232         }
233
234         if (fde_ev == NULL) {
235                 int sock = ctdbd_conn_get_fd(ctx->conn);
236
237                 fde_ev = talloc(fde, struct messaging_ctdb_fde_ev);
238                 if (fde_ev == NULL) {
239                         return NULL;
240                 }
241                 fde_ev->fde = tevent_add_fd(
242                         ev, fde_ev, sock, TEVENT_FD_READ,
243                         messaging_ctdb_read_handler, ctx);
244                 if (fde_ev->fde == NULL) {
245                         TALLOC_FREE(fde);
246                         return NULL;
247                 }
248                 fde_ev->ev = ev;
249                 fde_ev->ctx = ctx;
250                 DLIST_ADD(ctx->fde_evs, fde_ev);
251                 talloc_set_destructor(
252                         fde_ev, messaging_ctdb_fde_ev_destructor);
253         } else {
254                 /*
255                  * Same trick as with tdb_wrap: The caller will never
256                  * see the talloc_referenced object, the
257                  * messaging_ctdb_fde_ev, so problems with
258                  * talloc_unlink will not happen.
259                  */
260                 if (talloc_reference(fde, fde_ev) == NULL) {
261                         TALLOC_FREE(fde);
262                         return NULL;
263                 }
264         }
265
266         fde->fde = fde_ev->fde;
267         return fde;
268 }
269
270 bool messaging_ctdb_fde_active(struct messaging_ctdb_fde *fde)
271 {
272         uint16_t flags;
273
274         if (fde == NULL) {
275                 return false;
276         }
277         flags = tevent_fd_get_flags(fde->fde);
278         return (flags != 0);
279 }
280
281 struct ctdbd_connection *messaging_ctdb_connection(void)
282 {
283         if (global_ctdb_context == NULL) {
284                 smb_panic("messaging not initialized\n");
285         }
286         return global_ctdb_context->conn;
287 }