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 | };
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * This is a Samba3 hack/optimization. Routines like process_exists need to
|
---|
35 | * talk to ctdbd, and they don't get handed a messaging context.
|
---|
36 | */
|
---|
37 | static struct ctdbd_connection *global_ctdbd_connection;
|
---|
38 | static int global_ctdb_connection_pid;
|
---|
39 |
|
---|
40 | struct ctdbd_connection *messaging_ctdbd_connection(void)
|
---|
41 | {
|
---|
42 | if (!lp_clustering()) {
|
---|
43 | return NULL;
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (global_ctdb_connection_pid == 0 &&
|
---|
47 | global_ctdbd_connection == NULL) {
|
---|
48 | struct tevent_context *ev;
|
---|
49 | struct messaging_context *msg;
|
---|
50 |
|
---|
51 | ev = samba_tevent_context_init(NULL);
|
---|
52 | if (!ev) {
|
---|
53 | DEBUG(0,("samba_tevent_context_init failed\n"));
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 |
|
---|
57 | msg = messaging_init(NULL, ev);
|
---|
58 | if (!msg) {
|
---|
59 | DEBUG(0,("messaging_init failed\n"));
|
---|
60 | return NULL;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (global_ctdb_connection_pid != getpid()) {
|
---|
65 | DEBUG(0,("messaging_ctdbd_connection():"
|
---|
66 | "valid for pid[%jd] but it's [%jd]\n",
|
---|
67 | (intmax_t)global_ctdb_connection_pid,
|
---|
68 | (intmax_t)getpid()));
|
---|
69 | smb_panic("messaging_ctdbd_connection() invalid process\n");
|
---|
70 | }
|
---|
71 |
|
---|
72 | return global_ctdbd_connection;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static int messaging_ctdb_send(struct server_id src,
|
---|
76 | struct server_id pid, int msg_type,
|
---|
77 | const struct iovec *iov, int iovlen,
|
---|
78 | const int *fds, size_t num_fds,
|
---|
79 | struct messaging_backend *backend)
|
---|
80 | {
|
---|
81 | struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
|
---|
82 | backend->private_data, struct messaging_ctdbd_context);
|
---|
83 | uint8_t hdr[MESSAGE_HDR_LENGTH];
|
---|
84 | struct iovec iov2[iovlen+1];
|
---|
85 |
|
---|
86 | if (num_fds > 0) {
|
---|
87 | return ENOSYS;
|
---|
88 | }
|
---|
89 |
|
---|
90 | message_hdr_put(hdr, msg_type, src, pid);
|
---|
91 | iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
|
---|
92 | memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
|
---|
93 |
|
---|
94 | return ctdbd_messaging_send_iov(ctx->conn, pid.vnn, pid.pid,
|
---|
95 | iov2, iovlen+1);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * The global connection just went away
|
---|
102 | */
|
---|
103 | global_ctdb_connection_pid = 0;
|
---|
104 | global_ctdbd_connection = NULL;
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | static int messaging_ctdb_recv(
|
---|
109 | uint32_t src_vnn, uint32_t dst_vnn, uint64_t dst_srvid,
|
---|
110 | const uint8_t *msg, size_t msg_len, void *private_data)
|
---|
111 | {
|
---|
112 | struct messaging_context *msg_ctx = talloc_get_type_abort(
|
---|
113 | private_data, struct messaging_context);
|
---|
114 | struct server_id me = messaging_server_id(msg_ctx);
|
---|
115 | int ret;
|
---|
116 | struct iovec iov;
|
---|
117 | struct server_id src, dst;
|
---|
118 | enum messaging_type msg_type;
|
---|
119 | struct server_id_buf idbuf;
|
---|
120 |
|
---|
121 | if (msg_len < MESSAGE_HDR_LENGTH) {
|
---|
122 | DEBUG(1, ("%s: message too short: %u\n", __func__,
|
---|
123 | (unsigned)msg_len));
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | message_hdr_get(&msg_type, &src, &dst, msg);
|
---|
128 |
|
---|
129 | iov = (struct iovec) {
|
---|
130 | .iov_base = discard_const_p(uint8_t, msg) + MESSAGE_HDR_LENGTH,
|
---|
131 | .iov_len = msg_len - MESSAGE_HDR_LENGTH
|
---|
132 | };
|
---|
133 |
|
---|
134 | DEBUG(10, ("%s: Received message 0x%x len %u from %s\n",
|
---|
135 | __func__, (unsigned)msg_type, (unsigned)msg_len,
|
---|
136 | server_id_str_buf(src, &idbuf)));
|
---|
137 |
|
---|
138 | if (!server_id_same_process(&me, &dst)) {
|
---|
139 | struct server_id_buf id1, id2;
|
---|
140 |
|
---|
141 | DEBUG(10, ("%s: I'm %s, ignoring msg to %s\n", __func__,
|
---|
142 | server_id_str_buf(me, &id1),
|
---|
143 | server_id_str_buf(dst, &id2)));
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Go through the event loop
|
---|
149 | */
|
---|
150 |
|
---|
151 | ret = messaging_send_iov_from(msg_ctx, src, dst, msg_type,
|
---|
152 | &iov, 1, NULL, 0);
|
---|
153 |
|
---|
154 | if (ret != 0) {
|
---|
155 | DEBUG(10, ("%s: messaging_send_iov_from failed: %s\n",
|
---|
156 | __func__, strerror(ret)));
|
---|
157 | }
|
---|
158 |
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int messaging_ctdbd_init(struct messaging_context *msg_ctx,
|
---|
163 | TALLOC_CTX *mem_ctx,
|
---|
164 | struct messaging_backend **presult)
|
---|
165 | {
|
---|
166 | struct messaging_backend *result;
|
---|
167 | struct messaging_ctdbd_context *ctx;
|
---|
168 | int ret;
|
---|
169 |
|
---|
170 | if (!(result = talloc(mem_ctx, struct messaging_backend))) {
|
---|
171 | DEBUG(0, ("talloc failed\n"));
|
---|
172 | return ENOMEM;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
|
---|
176 | DEBUG(0, ("talloc failed\n"));
|
---|
177 | TALLOC_FREE(result);
|
---|
178 | return ENOMEM;
|
---|
179 | }
|
---|
180 |
|
---|
181 | ret = ctdbd_messaging_connection(ctx, lp_ctdbd_socket(),
|
---|
182 | lp_ctdb_timeout(), &ctx->conn);
|
---|
183 |
|
---|
184 | if (ret != 0) {
|
---|
185 | DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
|
---|
186 | strerror(ret)));
|
---|
187 | TALLOC_FREE(result);
|
---|
188 | return ret;
|
---|
189 | }
|
---|
190 |
|
---|
191 | ret = ctdbd_register_msg_ctx(ctx->conn, msg_ctx,
|
---|
192 | messaging_tevent_context(msg_ctx));
|
---|
193 |
|
---|
194 | if (ret != 0) {
|
---|
195 | DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
|
---|
196 | strerror(ret)));
|
---|
197 | TALLOC_FREE(result);
|
---|
198 | return ret;
|
---|
199 | }
|
---|
200 |
|
---|
201 | ret = register_with_ctdbd(ctx->conn, getpid(),
|
---|
202 | messaging_ctdb_recv, msg_ctx);
|
---|
203 | if (ret != 0) {
|
---|
204 | DEBUG(10, ("register_with_ctdbd failed: %s\n",
|
---|
205 | strerror(ret)));
|
---|
206 | TALLOC_FREE(result);
|
---|
207 | return ret;
|
---|
208 | }
|
---|
209 |
|
---|
210 | global_ctdb_connection_pid = getpid();
|
---|
211 | global_ctdbd_connection = ctx->conn;
|
---|
212 | talloc_set_destructor(ctx, messaging_ctdbd_destructor);
|
---|
213 |
|
---|
214 | set_my_vnn(ctdbd_vnn(ctx->conn));
|
---|
215 |
|
---|
216 | result->send_fn = messaging_ctdb_send;
|
---|
217 | result->private_data = (void *)ctx;
|
---|
218 |
|
---|
219 | *presult = result;
|
---|
220 | return 0;
|
---|
221 | }
|
---|