| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Samba internal messaging functions
|
|---|
| 4 | Copyright (C) 2007 by Volker Lendecke
|
|---|
| 5 | Copyright (C) 2007 by Andrew Tridgell
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 | #include "util_tdb.h"
|
|---|
| 23 |
|
|---|
| 24 | #ifdef CLUSTER_SUPPORT
|
|---|
| 25 |
|
|---|
| 26 | #include "ctdbd_conn.h"
|
|---|
| 27 | #include "packet.h"
|
|---|
| 28 | #include "messages.h"
|
|---|
| 29 |
|
|---|
| 30 | /* paths to these include files come from --with-ctdb= in configure */
|
|---|
| 31 | #include "ctdb.h"
|
|---|
| 32 | #include "ctdb_private.h"
|
|---|
| 33 |
|
|---|
| 34 | struct ctdbd_connection {
|
|---|
| 35 | struct messaging_context *msg_ctx;
|
|---|
| 36 | uint32 reqid;
|
|---|
| 37 | uint32 our_vnn;
|
|---|
| 38 | uint64 rand_srvid;
|
|---|
| 39 | struct packet_context *pkt;
|
|---|
| 40 | struct fd_event *fde;
|
|---|
| 41 |
|
|---|
| 42 | void (*release_ip_handler)(const char *ip_addr, void *private_data);
|
|---|
| 43 | void *release_ip_priv;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
|
|---|
| 47 | uint32_t vnn, uint32 opcode,
|
|---|
| 48 | uint64_t srvid, uint32_t flags, TDB_DATA data,
|
|---|
| 49 | TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
|
|---|
| 50 | int *cstatus);
|
|---|
| 51 |
|
|---|
| 52 | /*
|
|---|
| 53 | * exit on fatal communications errors with the ctdbd daemon
|
|---|
| 54 | */
|
|---|
| 55 | static void cluster_fatal(const char *why)
|
|---|
| 56 | {
|
|---|
| 57 | DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
|
|---|
| 58 | /* we don't use smb_panic() as we don't want to delay to write
|
|---|
| 59 | a core file. We need to release this process id immediately
|
|---|
| 60 | so that someone else can take over without getting sharing
|
|---|
| 61 | violations */
|
|---|
| 62 | _exit(1);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | *
|
|---|
| 67 | */
|
|---|
| 68 | static void ctdb_packet_dump(struct ctdb_req_header *hdr)
|
|---|
| 69 | {
|
|---|
| 70 | if (DEBUGLEVEL < 10) {
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 | DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
|
|---|
| 74 | (int)hdr->length, (int)hdr->ctdb_magic,
|
|---|
| 75 | (int)hdr->ctdb_version, (int)hdr->generation,
|
|---|
| 76 | (int)hdr->operation, (int)hdr->reqid));
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /*
|
|---|
| 80 | * Register a srvid with ctdbd
|
|---|
| 81 | */
|
|---|
| 82 | static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
|
|---|
| 83 | uint64_t srvid)
|
|---|
| 84 | {
|
|---|
| 85 |
|
|---|
| 86 | int cstatus;
|
|---|
| 87 | return ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 88 | CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
|
|---|
| 89 | tdb_null, NULL, NULL, &cstatus);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /*
|
|---|
| 93 | * get our vnn from the cluster
|
|---|
| 94 | */
|
|---|
| 95 | static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
|
|---|
| 96 | {
|
|---|
| 97 | int32_t cstatus=-1;
|
|---|
| 98 | NTSTATUS status;
|
|---|
| 99 | status = ctdbd_control(conn,
|
|---|
| 100 | CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
|
|---|
| 101 | tdb_null, NULL, NULL, &cstatus);
|
|---|
| 102 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 103 | cluster_fatal("ctdbd_control failed\n");
|
|---|
| 104 | }
|
|---|
| 105 | *vnn = (uint32_t)cstatus;
|
|---|
| 106 | return status;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /*
|
|---|
| 110 | * Are we active (i.e. not banned or stopped?)
|
|---|
| 111 | */
|
|---|
| 112 | static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
|
|---|
| 113 | {
|
|---|
| 114 | int32_t cstatus=-1;
|
|---|
| 115 | NTSTATUS status;
|
|---|
| 116 | TDB_DATA outdata;
|
|---|
| 117 | struct ctdb_node_map *m;
|
|---|
| 118 | uint32_t failure_flags;
|
|---|
| 119 | bool ret = false;
|
|---|
| 120 | int i;
|
|---|
| 121 |
|
|---|
| 122 | status = ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 123 | CTDB_CONTROL_GET_NODEMAP, 0, 0,
|
|---|
| 124 | tdb_null, talloc_tos(), &outdata, &cstatus);
|
|---|
| 125 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 126 | cluster_fatal("ctdbd_control failed\n");
|
|---|
| 127 | }
|
|---|
| 128 | if ((cstatus != 0) || (outdata.dptr == NULL)) {
|
|---|
| 129 | DEBUG(2, ("Received invalid ctdb data\n"));
|
|---|
| 130 | return false;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | m = (struct ctdb_node_map *)outdata.dptr;
|
|---|
| 134 |
|
|---|
| 135 | for (i=0; i<m->num; i++) {
|
|---|
| 136 | if (vnn == m->nodes[i].pnn) {
|
|---|
| 137 | break;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | if (i == m->num) {
|
|---|
| 142 | DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
|
|---|
| 143 | (int)vnn));
|
|---|
| 144 | goto fail;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
|
|---|
| 148 | | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
|
|---|
| 149 |
|
|---|
| 150 | if ((m->nodes[i].flags & failure_flags) != 0) {
|
|---|
| 151 | DEBUG(2, ("Node has status %x, not active\n",
|
|---|
| 152 | (int)m->nodes[i].flags));
|
|---|
| 153 | goto fail;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | ret = true;
|
|---|
| 157 | fail:
|
|---|
| 158 | TALLOC_FREE(outdata.dptr);
|
|---|
| 159 | return ret;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
|
|---|
| 163 | {
|
|---|
| 164 | return conn->our_vnn;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /*
|
|---|
| 168 | * Get us a ctdb connection
|
|---|
| 169 | */
|
|---|
| 170 |
|
|---|
| 171 | static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
|
|---|
| 172 | struct packet_context **presult)
|
|---|
| 173 | {
|
|---|
| 174 | struct packet_context *result;
|
|---|
| 175 | const char *sockname = lp_ctdbd_socket();
|
|---|
| 176 | struct sockaddr_un addr;
|
|---|
| 177 | int fd;
|
|---|
| 178 |
|
|---|
| 179 | if (!sockname || !*sockname) {
|
|---|
| 180 | sockname = CTDB_PATH;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|---|
| 184 | if (fd == -1) {
|
|---|
| 185 | DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
|
|---|
| 186 | return map_nt_error_from_unix(errno);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | ZERO_STRUCT(addr);
|
|---|
| 190 | addr.sun_family = AF_UNIX;
|
|---|
| 191 | strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
|
|---|
| 192 |
|
|---|
| 193 | if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
|
|---|
| 194 | DEBUG(1, ("connect(%s) failed: %s\n", sockname,
|
|---|
| 195 | strerror(errno)));
|
|---|
| 196 | close(fd);
|
|---|
| 197 | return map_nt_error_from_unix(errno);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | if (!(result = packet_init(mem_ctx, fd))) {
|
|---|
| 201 | close(fd);
|
|---|
| 202 | return NT_STATUS_NO_MEMORY;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | *presult = result;
|
|---|
| 206 | return NT_STATUS_OK;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /*
|
|---|
| 210 | * Do we have a complete ctdb packet in the queue?
|
|---|
| 211 | */
|
|---|
| 212 |
|
|---|
| 213 | static bool ctdb_req_complete(const uint8_t *buf, size_t available,
|
|---|
| 214 | size_t *length,
|
|---|
| 215 | void *private_data)
|
|---|
| 216 | {
|
|---|
| 217 | uint32 msglen;
|
|---|
| 218 |
|
|---|
| 219 | if (available < sizeof(msglen)) {
|
|---|
| 220 | return False;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | msglen = *((uint32 *)buf);
|
|---|
| 224 |
|
|---|
| 225 | DEBUG(10, ("msglen = %d\n", msglen));
|
|---|
| 226 |
|
|---|
| 227 | if (msglen < sizeof(struct ctdb_req_header)) {
|
|---|
| 228 | DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
|
|---|
| 229 | "the req_header\n", (int)msglen,
|
|---|
| 230 | (int)sizeof(struct ctdb_req_header)));
|
|---|
| 231 | cluster_fatal("ctdbd protocol error\n");
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | if (available < msglen) {
|
|---|
| 235 | return false;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | *length = msglen;
|
|---|
| 239 | return true;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /*
|
|---|
| 243 | * State necessary to defer an incoming message while we are waiting for a
|
|---|
| 244 | * ctdb reply.
|
|---|
| 245 | */
|
|---|
| 246 |
|
|---|
| 247 | struct deferred_msg_state {
|
|---|
| 248 | struct messaging_context *msg_ctx;
|
|---|
| 249 | struct messaging_rec *rec;
|
|---|
| 250 | };
|
|---|
| 251 |
|
|---|
| 252 | /*
|
|---|
| 253 | * Timed event handler for the deferred message
|
|---|
| 254 | */
|
|---|
| 255 |
|
|---|
| 256 | static void deferred_message_dispatch(struct event_context *event_ctx,
|
|---|
| 257 | struct timed_event *te,
|
|---|
| 258 | struct timeval now,
|
|---|
| 259 | void *private_data)
|
|---|
| 260 | {
|
|---|
| 261 | struct deferred_msg_state *state = talloc_get_type_abort(
|
|---|
| 262 | private_data, struct deferred_msg_state);
|
|---|
| 263 |
|
|---|
| 264 | messaging_dispatch_rec(state->msg_ctx, state->rec);
|
|---|
| 265 | TALLOC_FREE(state);
|
|---|
| 266 | TALLOC_FREE(te);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | struct req_pull_state {
|
|---|
| 270 | TALLOC_CTX *mem_ctx;
|
|---|
| 271 | DATA_BLOB req;
|
|---|
| 272 | };
|
|---|
| 273 |
|
|---|
| 274 | /*
|
|---|
| 275 | * Pull a ctdb request out of the incoming packet queue
|
|---|
| 276 | */
|
|---|
| 277 |
|
|---|
| 278 | static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
|
|---|
| 279 | void *private_data)
|
|---|
| 280 | {
|
|---|
| 281 | struct req_pull_state *state = (struct req_pull_state *)private_data;
|
|---|
| 282 |
|
|---|
| 283 | state->req.data = talloc_move(state->mem_ctx, &buf);
|
|---|
| 284 | state->req.length = length;
|
|---|
| 285 | return NT_STATUS_OK;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /*
|
|---|
| 289 | * Fetch a messaging_rec from an incoming ctdb style message
|
|---|
| 290 | */
|
|---|
| 291 |
|
|---|
| 292 | static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
|
|---|
| 293 | size_t overall_length,
|
|---|
| 294 | struct ctdb_req_message *msg)
|
|---|
| 295 | {
|
|---|
| 296 | struct messaging_rec *result;
|
|---|
| 297 | DATA_BLOB blob;
|
|---|
| 298 | enum ndr_err_code ndr_err;
|
|---|
| 299 |
|
|---|
| 300 | if ((overall_length < offsetof(struct ctdb_req_message, data))
|
|---|
| 301 | || (overall_length
|
|---|
| 302 | < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
|
|---|
| 303 |
|
|---|
| 304 | cluster_fatal("got invalid msg length");
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
|
|---|
| 308 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 309 | return NULL;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | blob = data_blob_const(msg->data, msg->datalen);
|
|---|
| 313 |
|
|---|
| 314 | ndr_err = ndr_pull_struct_blob(
|
|---|
| 315 | &blob, result, result,
|
|---|
| 316 | (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
|
|---|
| 317 |
|
|---|
| 318 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
|---|
| 319 | DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
|
|---|
| 320 | ndr_errstr(ndr_err)));
|
|---|
| 321 | TALLOC_FREE(result);
|
|---|
| 322 | return NULL;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | if (DEBUGLEVEL >= 10) {
|
|---|
| 326 | DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
|
|---|
| 327 | NDR_PRINT_DEBUG(messaging_rec, result);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | return result;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | static NTSTATUS ctdb_packet_fd_read_sync(struct packet_context *ctx)
|
|---|
| 334 | {
|
|---|
| 335 | int timeout = lp_ctdb_timeout();
|
|---|
| 336 |
|
|---|
| 337 | if (timeout == 0) {
|
|---|
| 338 | timeout = -1;
|
|---|
| 339 | }
|
|---|
| 340 | return packet_fd_read_sync(ctx, timeout);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /*
|
|---|
| 344 | * Read a full ctdbd request. If we have a messaging context, defer incoming
|
|---|
| 345 | * messages that might come in between.
|
|---|
| 346 | */
|
|---|
| 347 |
|
|---|
| 348 | static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
|
|---|
| 349 | TALLOC_CTX *mem_ctx, void *result)
|
|---|
| 350 | {
|
|---|
| 351 | struct ctdb_req_header *hdr;
|
|---|
| 352 | struct req_pull_state state;
|
|---|
| 353 | NTSTATUS status;
|
|---|
| 354 |
|
|---|
| 355 | again:
|
|---|
| 356 |
|
|---|
| 357 | status = ctdb_packet_fd_read_sync(conn->pkt);
|
|---|
| 358 |
|
|---|
| 359 | if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
|
|---|
| 360 | /* EAGAIN */
|
|---|
| 361 | goto again;
|
|---|
| 362 | } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
|
|---|
| 363 | /* EAGAIN */
|
|---|
| 364 | goto again;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 368 | DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
|
|---|
| 369 | cluster_fatal("ctdbd died\n");
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | next_pkt:
|
|---|
| 373 |
|
|---|
| 374 | ZERO_STRUCT(state);
|
|---|
| 375 | state.mem_ctx = mem_ctx;
|
|---|
| 376 |
|
|---|
| 377 | if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
|
|---|
| 378 | &state, &status)) {
|
|---|
| 379 | /*
|
|---|
| 380 | * Not enough data
|
|---|
| 381 | */
|
|---|
| 382 | DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
|
|---|
| 383 | goto again;
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 387 | DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
|
|---|
| 388 | cluster_fatal("ctdbd died\n");
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | hdr = (struct ctdb_req_header *)state.req.data;
|
|---|
| 392 |
|
|---|
| 393 | DEBUG(10, ("Received ctdb packet\n"));
|
|---|
| 394 | ctdb_packet_dump(hdr);
|
|---|
| 395 |
|
|---|
| 396 | if (hdr->operation == CTDB_REQ_MESSAGE) {
|
|---|
| 397 | struct timed_event *evt;
|
|---|
| 398 | struct deferred_msg_state *msg_state;
|
|---|
| 399 | struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
|
|---|
| 400 |
|
|---|
| 401 | if (conn->msg_ctx == NULL) {
|
|---|
| 402 | DEBUG(1, ("Got a message without having a msg ctx, "
|
|---|
| 403 | "dropping msg %llu\n",
|
|---|
| 404 | (long long unsigned)msg->srvid));
|
|---|
| 405 | goto next_pkt;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | if ((conn->release_ip_handler != NULL)
|
|---|
| 409 | && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
|
|---|
| 410 | /* must be dispatched immediately */
|
|---|
| 411 | DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
|
|---|
| 412 | conn->release_ip_handler((const char *)msg->data,
|
|---|
| 413 | conn->release_ip_priv);
|
|---|
| 414 | TALLOC_FREE(hdr);
|
|---|
| 415 | goto next_pkt;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
|
|---|
| 419 | || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
|
|---|
| 420 |
|
|---|
| 421 | DEBUG(1, ("ctdb_read_req: Got %s message\n",
|
|---|
| 422 | (msg->srvid == CTDB_SRVID_RECONFIGURE)
|
|---|
| 423 | ? "cluster reconfigure" : "SAMBA_NOTIFY"));
|
|---|
| 424 |
|
|---|
| 425 | messaging_send(conn->msg_ctx,
|
|---|
| 426 | messaging_server_id(conn->msg_ctx),
|
|---|
| 427 | MSG_SMB_BRL_VALIDATE, &data_blob_null);
|
|---|
| 428 | messaging_send(conn->msg_ctx,
|
|---|
| 429 | messaging_server_id(conn->msg_ctx),
|
|---|
| 430 | MSG_DBWRAP_G_LOCK_RETRY,
|
|---|
| 431 | &data_blob_null);
|
|---|
| 432 | TALLOC_FREE(hdr);
|
|---|
| 433 | goto next_pkt;
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | msg_state = TALLOC_P(NULL, struct deferred_msg_state);
|
|---|
| 437 | if (msg_state == NULL) {
|
|---|
| 438 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 439 | TALLOC_FREE(hdr);
|
|---|
| 440 | goto next_pkt;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | if (!(msg_state->rec = ctdb_pull_messaging_rec(
|
|---|
| 444 | msg_state, state.req.length, msg))) {
|
|---|
| 445 | DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
|
|---|
| 446 | TALLOC_FREE(msg_state);
|
|---|
| 447 | TALLOC_FREE(hdr);
|
|---|
| 448 | goto next_pkt;
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | TALLOC_FREE(hdr);
|
|---|
| 452 |
|
|---|
| 453 | msg_state->msg_ctx = conn->msg_ctx;
|
|---|
| 454 |
|
|---|
| 455 | /*
|
|---|
| 456 | * We're waiting for a call reply, but an async message has
|
|---|
| 457 | * crossed. Defer dispatching to the toplevel event loop.
|
|---|
| 458 | */
|
|---|
| 459 | evt = event_add_timed(conn->msg_ctx->event_ctx,
|
|---|
| 460 | conn->msg_ctx->event_ctx,
|
|---|
| 461 | timeval_zero(),
|
|---|
| 462 | deferred_message_dispatch,
|
|---|
| 463 | msg_state);
|
|---|
| 464 | if (evt == NULL) {
|
|---|
| 465 | DEBUG(0, ("event_add_timed failed\n"));
|
|---|
| 466 | TALLOC_FREE(msg_state);
|
|---|
| 467 | TALLOC_FREE(hdr);
|
|---|
| 468 | goto next_pkt;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | goto next_pkt;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | if (hdr->reqid != reqid) {
|
|---|
| 475 | /* we got the wrong reply */
|
|---|
| 476 | DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
|
|---|
| 477 | "been %u\n", hdr->reqid, reqid));
|
|---|
| 478 | TALLOC_FREE(hdr);
|
|---|
| 479 | goto again;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | *((void **)result) = talloc_move(mem_ctx, &hdr);
|
|---|
| 483 |
|
|---|
| 484 | return NT_STATUS_OK;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | /*
|
|---|
| 488 | * Get us a ctdbd connection
|
|---|
| 489 | */
|
|---|
| 490 |
|
|---|
| 491 | static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
|
|---|
| 492 | struct ctdbd_connection **pconn)
|
|---|
| 493 | {
|
|---|
| 494 | struct ctdbd_connection *conn;
|
|---|
| 495 | NTSTATUS status;
|
|---|
| 496 |
|
|---|
| 497 | if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
|
|---|
| 498 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 499 | return NT_STATUS_NO_MEMORY;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | status = ctdbd_connect(conn, &conn->pkt);
|
|---|
| 503 |
|
|---|
| 504 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 505 | DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
|
|---|
| 506 | goto fail;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | status = get_cluster_vnn(conn, &conn->our_vnn);
|
|---|
| 510 |
|
|---|
| 511 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 512 | DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
|
|---|
| 513 | goto fail;
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | if (!ctdbd_working(conn, conn->our_vnn)) {
|
|---|
| 517 | DEBUG(2, ("Node is not working, can not connect\n"));
|
|---|
| 518 | status = NT_STATUS_INTERNAL_DB_ERROR;
|
|---|
| 519 | goto fail;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | generate_random_buffer((unsigned char *)&conn->rand_srvid,
|
|---|
| 523 | sizeof(conn->rand_srvid));
|
|---|
| 524 |
|
|---|
| 525 | status = register_with_ctdbd(conn, conn->rand_srvid);
|
|---|
| 526 |
|
|---|
| 527 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 528 | DEBUG(5, ("Could not register random srvid: %s\n",
|
|---|
| 529 | nt_errstr(status)));
|
|---|
| 530 | goto fail;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | *pconn = conn;
|
|---|
| 534 | return NT_STATUS_OK;
|
|---|
| 535 |
|
|---|
| 536 | fail:
|
|---|
| 537 | TALLOC_FREE(conn);
|
|---|
| 538 | return status;
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | /*
|
|---|
| 542 | * Get us a ctdbd connection and register us as a process
|
|---|
| 543 | */
|
|---|
| 544 |
|
|---|
| 545 | NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
|
|---|
| 546 | struct ctdbd_connection **pconn)
|
|---|
| 547 | {
|
|---|
| 548 | struct ctdbd_connection *conn;
|
|---|
| 549 | NTSTATUS status;
|
|---|
| 550 |
|
|---|
| 551 | status = ctdbd_init_connection(mem_ctx, &conn);
|
|---|
| 552 |
|
|---|
| 553 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 554 | return status;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
|
|---|
| 558 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 559 | goto fail;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
|
|---|
| 563 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 564 | goto fail;
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
|
|---|
| 568 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 569 | goto fail;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | *pconn = conn;
|
|---|
| 573 | return NT_STATUS_OK;
|
|---|
| 574 |
|
|---|
| 575 | fail:
|
|---|
| 576 | TALLOC_FREE(conn);
|
|---|
| 577 | return status;
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
|
|---|
| 581 | {
|
|---|
| 582 | return conn->msg_ctx;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
|
|---|
| 586 | {
|
|---|
| 587 | return packet_get_fd(conn->pkt);
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | /*
|
|---|
| 591 | * Packet handler to receive and handle a ctdb message
|
|---|
| 592 | */
|
|---|
| 593 | static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
|
|---|
| 594 | void *private_data)
|
|---|
| 595 | {
|
|---|
| 596 | struct ctdbd_connection *conn = talloc_get_type_abort(
|
|---|
| 597 | private_data, struct ctdbd_connection);
|
|---|
| 598 | struct ctdb_req_message *msg;
|
|---|
| 599 | struct messaging_rec *msg_rec;
|
|---|
| 600 |
|
|---|
| 601 | msg = (struct ctdb_req_message *)buf;
|
|---|
| 602 |
|
|---|
| 603 | if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
|
|---|
| 604 | DEBUG(0, ("Received async msg of type %u, discarding\n",
|
|---|
| 605 | msg->hdr.operation));
|
|---|
| 606 | TALLOC_FREE(buf);
|
|---|
| 607 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | if ((conn->release_ip_handler != NULL)
|
|---|
| 611 | && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
|
|---|
| 612 | /* must be dispatched immediately */
|
|---|
| 613 | DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
|
|---|
| 614 | conn->release_ip_handler((const char *)msg->data,
|
|---|
| 615 | conn->release_ip_priv);
|
|---|
| 616 | TALLOC_FREE(buf);
|
|---|
| 617 | return NT_STATUS_OK;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | SMB_ASSERT(conn->msg_ctx != NULL);
|
|---|
| 621 |
|
|---|
| 622 | if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
|
|---|
| 623 | || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
|
|---|
| 624 | DEBUG(0,("Got cluster reconfigure message\n"));
|
|---|
| 625 | /*
|
|---|
| 626 | * when the cluster is reconfigured or someone of the
|
|---|
| 627 | * family has passed away (SAMBA_NOTIFY), we need to
|
|---|
| 628 | * clean the brl database
|
|---|
| 629 | */
|
|---|
| 630 | messaging_send(conn->msg_ctx,
|
|---|
| 631 | messaging_server_id(conn->msg_ctx),
|
|---|
| 632 | MSG_SMB_BRL_VALIDATE, &data_blob_null);
|
|---|
| 633 |
|
|---|
| 634 | messaging_send(conn->msg_ctx,
|
|---|
| 635 | messaging_server_id(conn->msg_ctx),
|
|---|
| 636 | MSG_DBWRAP_G_LOCK_RETRY,
|
|---|
| 637 | &data_blob_null);
|
|---|
| 638 |
|
|---|
| 639 | TALLOC_FREE(buf);
|
|---|
| 640 | return NT_STATUS_OK;
|
|---|
| 641 | }
|
|---|
| 642 |
|
|---|
| 643 | /* only messages to our pid or the broadcast are valid here */
|
|---|
| 644 | if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
|
|---|
| 645 | DEBUG(0,("Got unexpected message with srvid=%llu\n",
|
|---|
| 646 | (unsigned long long)msg->srvid));
|
|---|
| 647 | TALLOC_FREE(buf);
|
|---|
| 648 | return NT_STATUS_OK;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
|
|---|
| 652 | DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
|
|---|
| 653 | TALLOC_FREE(buf);
|
|---|
| 654 | return NT_STATUS_NO_MEMORY;
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | messaging_dispatch_rec(conn->msg_ctx, msg_rec);
|
|---|
| 658 |
|
|---|
| 659 | TALLOC_FREE(msg_rec);
|
|---|
| 660 | TALLOC_FREE(buf);
|
|---|
| 661 | return NT_STATUS_OK;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | /*
|
|---|
| 665 | * The ctdbd socket is readable asynchronuously
|
|---|
| 666 | */
|
|---|
| 667 |
|
|---|
| 668 | static void ctdbd_socket_handler(struct event_context *event_ctx,
|
|---|
| 669 | struct fd_event *event,
|
|---|
| 670 | uint16 flags,
|
|---|
| 671 | void *private_data)
|
|---|
| 672 | {
|
|---|
| 673 | struct ctdbd_connection *conn = talloc_get_type_abort(
|
|---|
| 674 | private_data, struct ctdbd_connection);
|
|---|
| 675 |
|
|---|
| 676 | NTSTATUS status;
|
|---|
| 677 |
|
|---|
| 678 | status = packet_fd_read(conn->pkt);
|
|---|
| 679 |
|
|---|
| 680 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 681 | DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
|
|---|
| 682 | cluster_fatal("ctdbd died\n");
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | while (packet_handler(conn->pkt, ctdb_req_complete,
|
|---|
| 686 | ctdb_handle_message, conn, &status)) {
|
|---|
| 687 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 688 | DEBUG(10, ("could not handle incoming message: %s\n",
|
|---|
| 689 | nt_errstr(status)));
|
|---|
| 690 | }
|
|---|
| 691 | }
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | /*
|
|---|
| 695 | * Prepare a ctdbd connection to receive messages
|
|---|
| 696 | */
|
|---|
| 697 |
|
|---|
| 698 | NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
|
|---|
| 699 | struct messaging_context *msg_ctx)
|
|---|
| 700 | {
|
|---|
| 701 | SMB_ASSERT(conn->msg_ctx == NULL);
|
|---|
| 702 | SMB_ASSERT(conn->fde == NULL);
|
|---|
| 703 |
|
|---|
| 704 | if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
|
|---|
| 705 | packet_get_fd(conn->pkt),
|
|---|
| 706 | EVENT_FD_READ,
|
|---|
| 707 | ctdbd_socket_handler,
|
|---|
| 708 | conn))) {
|
|---|
| 709 | DEBUG(0, ("event_add_fd failed\n"));
|
|---|
| 710 | return NT_STATUS_NO_MEMORY;
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | conn->msg_ctx = msg_ctx;
|
|---|
| 714 |
|
|---|
| 715 | return NT_STATUS_OK;
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | /*
|
|---|
| 719 | * Send a messaging message across a ctdbd
|
|---|
| 720 | */
|
|---|
| 721 |
|
|---|
| 722 | NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
|
|---|
| 723 | uint32 dst_vnn, uint64 dst_srvid,
|
|---|
| 724 | struct messaging_rec *msg)
|
|---|
| 725 | {
|
|---|
| 726 | struct ctdb_req_message r;
|
|---|
| 727 | TALLOC_CTX *mem_ctx;
|
|---|
| 728 | DATA_BLOB blob;
|
|---|
| 729 | NTSTATUS status;
|
|---|
| 730 | enum ndr_err_code ndr_err;
|
|---|
| 731 |
|
|---|
| 732 | if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
|
|---|
| 733 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 734 | return NT_STATUS_NO_MEMORY;
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | ndr_err = ndr_push_struct_blob(
|
|---|
| 738 | &blob, mem_ctx, msg,
|
|---|
| 739 | (ndr_push_flags_fn_t)ndr_push_messaging_rec);
|
|---|
| 740 |
|
|---|
| 741 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
|---|
| 742 | DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
|
|---|
| 743 | ndr_errstr(ndr_err)));
|
|---|
| 744 | status = ndr_map_error2ntstatus(ndr_err);
|
|---|
| 745 | goto fail;
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
|
|---|
| 749 | r.hdr.ctdb_magic = CTDB_MAGIC;
|
|---|
| 750 | r.hdr.ctdb_version = CTDB_VERSION;
|
|---|
| 751 | r.hdr.generation = 1;
|
|---|
| 752 | r.hdr.operation = CTDB_REQ_MESSAGE;
|
|---|
| 753 | r.hdr.destnode = dst_vnn;
|
|---|
| 754 | r.hdr.srcnode = conn->our_vnn;
|
|---|
| 755 | r.hdr.reqid = 0;
|
|---|
| 756 | r.srvid = dst_srvid;
|
|---|
| 757 | r.datalen = blob.length;
|
|---|
| 758 |
|
|---|
| 759 | DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
|
|---|
| 760 | ctdb_packet_dump(&r.hdr);
|
|---|
| 761 |
|
|---|
| 762 | status = packet_send(
|
|---|
| 763 | conn->pkt, 2,
|
|---|
| 764 | data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
|
|---|
| 765 | blob);
|
|---|
| 766 |
|
|---|
| 767 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 768 | DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
|
|---|
| 769 | goto fail;
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | status = packet_flush(conn->pkt);
|
|---|
| 773 |
|
|---|
| 774 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 775 | DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
|
|---|
| 776 | cluster_fatal("cluster dispatch daemon msg write error\n");
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | status = NT_STATUS_OK;
|
|---|
| 780 | fail:
|
|---|
| 781 | TALLOC_FREE(mem_ctx);
|
|---|
| 782 | return status;
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | /*
|
|---|
| 786 | * send/recv a generic ctdb control message
|
|---|
| 787 | */
|
|---|
| 788 | static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
|
|---|
| 789 | uint32_t vnn, uint32 opcode,
|
|---|
| 790 | uint64_t srvid, uint32_t flags,
|
|---|
| 791 | TDB_DATA data,
|
|---|
| 792 | TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
|
|---|
| 793 | int *cstatus)
|
|---|
| 794 | {
|
|---|
| 795 | struct ctdb_req_control req;
|
|---|
| 796 | struct ctdb_reply_control *reply = NULL;
|
|---|
| 797 | struct ctdbd_connection *new_conn = NULL;
|
|---|
| 798 | NTSTATUS status;
|
|---|
| 799 |
|
|---|
| 800 | /* the samba3 ctdb code can't handle NOREPLY yet */
|
|---|
| 801 | flags &= ~CTDB_CTRL_FLAG_NOREPLY;
|
|---|
| 802 |
|
|---|
| 803 | if (conn == NULL) {
|
|---|
| 804 | status = ctdbd_init_connection(NULL, &new_conn);
|
|---|
| 805 |
|
|---|
| 806 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 807 | DEBUG(10, ("Could not init temp connection: %s\n",
|
|---|
| 808 | nt_errstr(status)));
|
|---|
| 809 | goto fail;
|
|---|
| 810 | }
|
|---|
| 811 |
|
|---|
| 812 | conn = new_conn;
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | ZERO_STRUCT(req);
|
|---|
| 816 | req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
|
|---|
| 817 | req.hdr.ctdb_magic = CTDB_MAGIC;
|
|---|
| 818 | req.hdr.ctdb_version = CTDB_VERSION;
|
|---|
| 819 | req.hdr.operation = CTDB_REQ_CONTROL;
|
|---|
| 820 | req.hdr.reqid = ++conn->reqid;
|
|---|
| 821 | req.hdr.destnode = vnn;
|
|---|
| 822 | req.opcode = opcode;
|
|---|
| 823 | req.srvid = srvid;
|
|---|
| 824 | req.datalen = data.dsize;
|
|---|
| 825 | req.flags = flags;
|
|---|
| 826 |
|
|---|
| 827 | DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
|
|---|
| 828 | ctdb_packet_dump(&req.hdr);
|
|---|
| 829 |
|
|---|
| 830 | status = packet_send(
|
|---|
| 831 | conn->pkt, 2,
|
|---|
| 832 | data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
|
|---|
| 833 | data_blob_const(data.dptr, data.dsize));
|
|---|
| 834 |
|
|---|
| 835 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 836 | DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
|
|---|
| 837 | goto fail;
|
|---|
| 838 | }
|
|---|
| 839 |
|
|---|
| 840 | status = packet_flush(conn->pkt);
|
|---|
| 841 |
|
|---|
| 842 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 843 | DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
|
|---|
| 844 | cluster_fatal("cluster dispatch daemon control write error\n");
|
|---|
| 845 | }
|
|---|
| 846 |
|
|---|
| 847 | if (flags & CTDB_CTRL_FLAG_NOREPLY) {
|
|---|
| 848 | TALLOC_FREE(new_conn);
|
|---|
| 849 | if (cstatus) {
|
|---|
| 850 | *cstatus = 0;
|
|---|
| 851 | }
|
|---|
| 852 | return NT_STATUS_OK;
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
|
|---|
| 856 |
|
|---|
| 857 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 858 | DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
|
|---|
| 859 | goto fail;
|
|---|
| 860 | }
|
|---|
| 861 |
|
|---|
| 862 | if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
|
|---|
| 863 | DEBUG(0, ("received invalid reply\n"));
|
|---|
| 864 | goto fail;
|
|---|
| 865 | }
|
|---|
| 866 |
|
|---|
| 867 | if (outdata) {
|
|---|
| 868 | if (!(outdata->dptr = (uint8 *)talloc_memdup(
|
|---|
| 869 | mem_ctx, reply->data, reply->datalen))) {
|
|---|
| 870 | TALLOC_FREE(reply);
|
|---|
| 871 | return NT_STATUS_NO_MEMORY;
|
|---|
| 872 | }
|
|---|
| 873 | outdata->dsize = reply->datalen;
|
|---|
| 874 | }
|
|---|
| 875 | if (cstatus) {
|
|---|
| 876 | (*cstatus) = reply->status;
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| 879 | status = NT_STATUS_OK;
|
|---|
| 880 |
|
|---|
| 881 | fail:
|
|---|
| 882 | TALLOC_FREE(new_conn);
|
|---|
| 883 | TALLOC_FREE(reply);
|
|---|
| 884 | return status;
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | /*
|
|---|
| 888 | * see if a remote process exists
|
|---|
| 889 | */
|
|---|
| 890 | bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
|
|---|
| 891 | {
|
|---|
| 892 | NTSTATUS status;
|
|---|
| 893 | TDB_DATA data;
|
|---|
| 894 | int32_t cstatus;
|
|---|
| 895 |
|
|---|
| 896 | data.dptr = (uint8_t*)&pid;
|
|---|
| 897 | data.dsize = sizeof(pid);
|
|---|
| 898 |
|
|---|
| 899 | status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
|
|---|
| 900 | data, NULL, NULL, &cstatus);
|
|---|
| 901 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 902 | DEBUG(0, (__location__ " ctdb_control for process_exists "
|
|---|
| 903 | "failed\n"));
|
|---|
| 904 | return False;
|
|---|
| 905 | }
|
|---|
| 906 |
|
|---|
| 907 | return cstatus == 0;
|
|---|
| 908 | }
|
|---|
| 909 |
|
|---|
| 910 | /*
|
|---|
| 911 | * Get a db path
|
|---|
| 912 | */
|
|---|
| 913 | char *ctdbd_dbpath(struct ctdbd_connection *conn,
|
|---|
| 914 | TALLOC_CTX *mem_ctx, uint32_t db_id)
|
|---|
| 915 | {
|
|---|
| 916 | NTSTATUS status;
|
|---|
| 917 | TDB_DATA data;
|
|---|
| 918 | int32_t cstatus;
|
|---|
| 919 |
|
|---|
| 920 | data.dptr = (uint8_t*)&db_id;
|
|---|
| 921 | data.dsize = sizeof(db_id);
|
|---|
| 922 |
|
|---|
| 923 | status = ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 924 | CTDB_CONTROL_GETDBPATH, 0, 0, data,
|
|---|
| 925 | mem_ctx, &data, &cstatus);
|
|---|
| 926 | if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
|
|---|
| 927 | DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
|
|---|
| 928 | return NULL;
|
|---|
| 929 | }
|
|---|
| 930 |
|
|---|
| 931 | return (char *)data.dptr;
|
|---|
| 932 | }
|
|---|
| 933 |
|
|---|
| 934 | /*
|
|---|
| 935 | * attach to a ctdb database
|
|---|
| 936 | */
|
|---|
| 937 | NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
|
|---|
| 938 | const char *name, uint32_t *db_id, int tdb_flags)
|
|---|
| 939 | {
|
|---|
| 940 | NTSTATUS status;
|
|---|
| 941 | TDB_DATA data;
|
|---|
| 942 | int32_t cstatus;
|
|---|
| 943 | bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
|
|---|
| 944 |
|
|---|
| 945 | data.dptr = (uint8_t*)name;
|
|---|
| 946 | data.dsize = strlen(name)+1;
|
|---|
| 947 |
|
|---|
| 948 | status = ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 949 | persistent
|
|---|
| 950 | ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
|
|---|
| 951 | : CTDB_CONTROL_DB_ATTACH,
|
|---|
| 952 | tdb_flags, 0, data, NULL, &data, &cstatus);
|
|---|
| 953 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 954 | DEBUG(0, (__location__ " ctdb_control for db_attach "
|
|---|
| 955 | "failed: %s\n", nt_errstr(status)));
|
|---|
| 956 | return status;
|
|---|
| 957 | }
|
|---|
| 958 |
|
|---|
| 959 | if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
|
|---|
| 960 | DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
|
|---|
| 961 | return NT_STATUS_INTERNAL_ERROR;
|
|---|
| 962 | }
|
|---|
| 963 |
|
|---|
| 964 | *db_id = *(uint32_t *)data.dptr;
|
|---|
| 965 | talloc_free(data.dptr);
|
|---|
| 966 |
|
|---|
| 967 | if (!(tdb_flags & TDB_SEQNUM)) {
|
|---|
| 968 | return NT_STATUS_OK;
|
|---|
| 969 | }
|
|---|
| 970 |
|
|---|
| 971 | data.dptr = (uint8_t *)db_id;
|
|---|
| 972 | data.dsize = sizeof(*db_id);
|
|---|
| 973 |
|
|---|
| 974 | status = ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 975 | CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
|
|---|
| 976 | NULL, NULL, &cstatus);
|
|---|
| 977 | if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
|
|---|
| 978 | DEBUG(0,(__location__ " ctdb_control for enable seqnum "
|
|---|
| 979 | "failed\n"));
|
|---|
| 980 | return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
|
|---|
| 981 | status;
|
|---|
| 982 | }
|
|---|
| 983 |
|
|---|
| 984 | return NT_STATUS_OK;
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | /*
|
|---|
| 988 | * force the migration of a record to this node
|
|---|
| 989 | */
|
|---|
| 990 | NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
|
|---|
| 991 | TDB_DATA key)
|
|---|
| 992 | {
|
|---|
| 993 | struct ctdb_req_call req;
|
|---|
| 994 | struct ctdb_reply_call *reply;
|
|---|
| 995 | NTSTATUS status;
|
|---|
| 996 |
|
|---|
| 997 | ZERO_STRUCT(req);
|
|---|
| 998 |
|
|---|
| 999 | req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
|
|---|
| 1000 | req.hdr.ctdb_magic = CTDB_MAGIC;
|
|---|
| 1001 | req.hdr.ctdb_version = CTDB_VERSION;
|
|---|
| 1002 | req.hdr.operation = CTDB_REQ_CALL;
|
|---|
| 1003 | req.hdr.reqid = ++conn->reqid;
|
|---|
| 1004 | req.flags = CTDB_IMMEDIATE_MIGRATION;
|
|---|
| 1005 | req.callid = CTDB_NULL_FUNC;
|
|---|
| 1006 | req.db_id = db_id;
|
|---|
| 1007 | req.keylen = key.dsize;
|
|---|
| 1008 |
|
|---|
| 1009 | DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
|
|---|
| 1010 | ctdb_packet_dump(&req.hdr);
|
|---|
| 1011 |
|
|---|
| 1012 | status = packet_send(
|
|---|
| 1013 | conn->pkt, 2,
|
|---|
| 1014 | data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
|
|---|
| 1015 | data_blob_const(key.dptr, key.dsize));
|
|---|
| 1016 |
|
|---|
| 1017 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1018 | DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
|
|---|
| 1019 | return status;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | status = packet_flush(conn->pkt);
|
|---|
| 1023 |
|
|---|
| 1024 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1025 | DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
|
|---|
| 1026 | cluster_fatal("cluster dispatch daemon control write error\n");
|
|---|
| 1027 | }
|
|---|
| 1028 |
|
|---|
| 1029 | status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
|
|---|
| 1030 |
|
|---|
| 1031 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1032 | DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
|
|---|
| 1033 | goto fail;
|
|---|
| 1034 | }
|
|---|
| 1035 |
|
|---|
| 1036 | if (reply->hdr.operation != CTDB_REPLY_CALL) {
|
|---|
| 1037 | DEBUG(0, ("received invalid reply\n"));
|
|---|
| 1038 | status = NT_STATUS_INTERNAL_ERROR;
|
|---|
| 1039 | goto fail;
|
|---|
| 1040 | }
|
|---|
| 1041 |
|
|---|
| 1042 | status = NT_STATUS_OK;
|
|---|
| 1043 | fail:
|
|---|
| 1044 |
|
|---|
| 1045 | TALLOC_FREE(reply);
|
|---|
| 1046 | return status;
|
|---|
| 1047 | }
|
|---|
| 1048 |
|
|---|
| 1049 | /*
|
|---|
| 1050 | * remotely fetch a record without locking it or forcing a migration
|
|---|
| 1051 | */
|
|---|
| 1052 | NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
|
|---|
| 1053 | TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
|
|---|
| 1054 | {
|
|---|
| 1055 | struct ctdb_req_call req;
|
|---|
| 1056 | struct ctdb_reply_call *reply;
|
|---|
| 1057 | NTSTATUS status;
|
|---|
| 1058 |
|
|---|
| 1059 | ZERO_STRUCT(req);
|
|---|
| 1060 |
|
|---|
| 1061 | req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
|
|---|
| 1062 | req.hdr.ctdb_magic = CTDB_MAGIC;
|
|---|
| 1063 | req.hdr.ctdb_version = CTDB_VERSION;
|
|---|
| 1064 | req.hdr.operation = CTDB_REQ_CALL;
|
|---|
| 1065 | req.hdr.reqid = ++conn->reqid;
|
|---|
| 1066 | req.flags = 0;
|
|---|
| 1067 | req.callid = CTDB_FETCH_FUNC;
|
|---|
| 1068 | req.db_id = db_id;
|
|---|
| 1069 | req.keylen = key.dsize;
|
|---|
| 1070 |
|
|---|
| 1071 | status = packet_send(
|
|---|
| 1072 | conn->pkt, 2,
|
|---|
| 1073 | data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
|
|---|
| 1074 | data_blob_const(key.dptr, key.dsize));
|
|---|
| 1075 |
|
|---|
| 1076 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1077 | DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
|
|---|
| 1078 | return status;
|
|---|
| 1079 | }
|
|---|
| 1080 |
|
|---|
| 1081 | status = packet_flush(conn->pkt);
|
|---|
| 1082 |
|
|---|
| 1083 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1084 | DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
|
|---|
| 1085 | cluster_fatal("cluster dispatch daemon control write error\n");
|
|---|
| 1086 | }
|
|---|
| 1087 |
|
|---|
| 1088 | status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
|
|---|
| 1089 |
|
|---|
| 1090 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1091 | DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
|
|---|
| 1092 | goto fail;
|
|---|
| 1093 | }
|
|---|
| 1094 |
|
|---|
| 1095 | if (reply->hdr.operation != CTDB_REPLY_CALL) {
|
|---|
| 1096 | DEBUG(0, ("received invalid reply\n"));
|
|---|
| 1097 | status = NT_STATUS_INTERNAL_ERROR;
|
|---|
| 1098 | goto fail;
|
|---|
| 1099 | }
|
|---|
| 1100 |
|
|---|
| 1101 | data->dsize = reply->datalen;
|
|---|
| 1102 | if (data->dsize == 0) {
|
|---|
| 1103 | data->dptr = NULL;
|
|---|
| 1104 | goto done;
|
|---|
| 1105 | }
|
|---|
| 1106 |
|
|---|
| 1107 | data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
|
|---|
| 1108 | reply->datalen);
|
|---|
| 1109 | if (data->dptr == NULL) {
|
|---|
| 1110 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1111 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 1112 | goto fail;
|
|---|
| 1113 | }
|
|---|
| 1114 |
|
|---|
| 1115 | done:
|
|---|
| 1116 | status = NT_STATUS_OK;
|
|---|
| 1117 | fail:
|
|---|
| 1118 | TALLOC_FREE(reply);
|
|---|
| 1119 | return status;
|
|---|
| 1120 | }
|
|---|
| 1121 |
|
|---|
| 1122 | struct ctdbd_traverse_state {
|
|---|
| 1123 | void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
|
|---|
| 1124 | void *private_data;
|
|---|
| 1125 | };
|
|---|
| 1126 |
|
|---|
| 1127 | /*
|
|---|
| 1128 | * Handle a traverse record coming in on the ctdbd connection
|
|---|
| 1129 | */
|
|---|
| 1130 |
|
|---|
| 1131 | static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
|
|---|
| 1132 | void *private_data)
|
|---|
| 1133 | {
|
|---|
| 1134 | struct ctdbd_traverse_state *state =
|
|---|
| 1135 | (struct ctdbd_traverse_state *)private_data;
|
|---|
| 1136 |
|
|---|
| 1137 | struct ctdb_req_message *m;
|
|---|
| 1138 | struct ctdb_rec_data *d;
|
|---|
| 1139 | TDB_DATA key, data;
|
|---|
| 1140 |
|
|---|
| 1141 | m = (struct ctdb_req_message *)buf;
|
|---|
| 1142 |
|
|---|
| 1143 | if (length < sizeof(*m) || m->hdr.length != length) {
|
|---|
| 1144 | DEBUG(0, ("Got invalid message of length %d\n", (int)length));
|
|---|
| 1145 | TALLOC_FREE(buf);
|
|---|
| 1146 | return NT_STATUS_UNEXPECTED_IO_ERROR;
|
|---|
| 1147 | }
|
|---|
| 1148 |
|
|---|
| 1149 | d = (struct ctdb_rec_data *)&m->data[0];
|
|---|
| 1150 | if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
|
|---|
| 1151 | DEBUG(0, ("Got invalid traverse data of length %d\n",
|
|---|
| 1152 | (int)m->datalen));
|
|---|
| 1153 | TALLOC_FREE(buf);
|
|---|
| 1154 | return NT_STATUS_UNEXPECTED_IO_ERROR;
|
|---|
| 1155 | }
|
|---|
| 1156 |
|
|---|
| 1157 | key.dsize = d->keylen;
|
|---|
| 1158 | key.dptr = &d->data[0];
|
|---|
| 1159 | data.dsize = d->datalen;
|
|---|
| 1160 | data.dptr = &d->data[d->keylen];
|
|---|
| 1161 |
|
|---|
| 1162 | if (key.dsize == 0 && data.dsize == 0) {
|
|---|
| 1163 | /* end of traverse */
|
|---|
| 1164 | return NT_STATUS_END_OF_FILE;
|
|---|
| 1165 | }
|
|---|
| 1166 |
|
|---|
| 1167 | if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
|
|---|
| 1168 | DEBUG(0, ("Got invalid ltdb header length %d\n",
|
|---|
| 1169 | (int)data.dsize));
|
|---|
| 1170 | TALLOC_FREE(buf);
|
|---|
| 1171 | return NT_STATUS_UNEXPECTED_IO_ERROR;
|
|---|
| 1172 | }
|
|---|
| 1173 | data.dsize -= sizeof(struct ctdb_ltdb_header);
|
|---|
| 1174 | data.dptr += sizeof(struct ctdb_ltdb_header);
|
|---|
| 1175 |
|
|---|
| 1176 | if (state->fn) {
|
|---|
| 1177 | state->fn(key, data, state->private_data);
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | TALLOC_FREE(buf);
|
|---|
| 1181 | return NT_STATUS_OK;
|
|---|
| 1182 | }
|
|---|
| 1183 |
|
|---|
| 1184 | /*
|
|---|
| 1185 | Traverse a ctdb database. This uses a kind-of hackish way to open a second
|
|---|
| 1186 | connection to ctdbd to avoid the hairy recursive and async problems with
|
|---|
| 1187 | everything in-line.
|
|---|
| 1188 | */
|
|---|
| 1189 |
|
|---|
| 1190 | NTSTATUS ctdbd_traverse(uint32 db_id,
|
|---|
| 1191 | void (*fn)(TDB_DATA key, TDB_DATA data,
|
|---|
| 1192 | void *private_data),
|
|---|
| 1193 | void *private_data)
|
|---|
| 1194 | {
|
|---|
| 1195 | struct ctdbd_connection *conn;
|
|---|
| 1196 | NTSTATUS status;
|
|---|
| 1197 |
|
|---|
| 1198 | TDB_DATA data;
|
|---|
| 1199 | struct ctdb_traverse_start t;
|
|---|
| 1200 | int cstatus;
|
|---|
| 1201 | struct ctdbd_traverse_state state;
|
|---|
| 1202 |
|
|---|
| 1203 | become_root();
|
|---|
| 1204 | status = ctdbd_init_connection(NULL, &conn);
|
|---|
| 1205 | unbecome_root();
|
|---|
| 1206 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1207 | DEBUG(0, ("ctdbd_init_connection failed: %s\n",
|
|---|
| 1208 | nt_errstr(status)));
|
|---|
| 1209 | return status;
|
|---|
| 1210 | }
|
|---|
| 1211 |
|
|---|
| 1212 | t.db_id = db_id;
|
|---|
| 1213 | t.srvid = conn->rand_srvid;
|
|---|
| 1214 | t.reqid = ++conn->reqid;
|
|---|
| 1215 |
|
|---|
| 1216 | data.dptr = (uint8_t *)&t;
|
|---|
| 1217 | data.dsize = sizeof(t);
|
|---|
| 1218 |
|
|---|
| 1219 | status = ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 1220 | CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
|
|---|
| 1221 | data, NULL, NULL, &cstatus);
|
|---|
| 1222 |
|
|---|
| 1223 | if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
|
|---|
| 1224 |
|
|---|
| 1225 | DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
|
|---|
| 1226 | cstatus));
|
|---|
| 1227 |
|
|---|
| 1228 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 1229 | /*
|
|---|
| 1230 | * We need a mapping here
|
|---|
| 1231 | */
|
|---|
| 1232 | status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 1233 | }
|
|---|
| 1234 | goto done;
|
|---|
| 1235 | }
|
|---|
| 1236 |
|
|---|
| 1237 | state.fn = fn;
|
|---|
| 1238 | state.private_data = private_data;
|
|---|
| 1239 |
|
|---|
| 1240 | while (True) {
|
|---|
| 1241 |
|
|---|
| 1242 | status = NT_STATUS_OK;
|
|---|
| 1243 |
|
|---|
| 1244 | if (packet_handler(conn->pkt, ctdb_req_complete,
|
|---|
| 1245 | ctdb_traverse_handler, &state, &status)) {
|
|---|
| 1246 |
|
|---|
| 1247 | if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
|
|---|
| 1248 | status = NT_STATUS_OK;
|
|---|
| 1249 | break;
|
|---|
| 1250 | }
|
|---|
| 1251 |
|
|---|
| 1252 | /*
|
|---|
| 1253 | * There might be more in the queue
|
|---|
| 1254 | */
|
|---|
| 1255 | continue;
|
|---|
| 1256 | }
|
|---|
| 1257 |
|
|---|
| 1258 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1259 | break;
|
|---|
| 1260 | }
|
|---|
| 1261 |
|
|---|
| 1262 | status = ctdb_packet_fd_read_sync(conn->pkt);
|
|---|
| 1263 |
|
|---|
| 1264 | if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
|
|---|
| 1265 | /*
|
|---|
| 1266 | * There might be more in the queue
|
|---|
| 1267 | */
|
|---|
| 1268 | continue;
|
|---|
| 1269 | }
|
|---|
| 1270 |
|
|---|
| 1271 | if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
|
|---|
| 1272 | status = NT_STATUS_OK;
|
|---|
| 1273 | break;
|
|---|
| 1274 | }
|
|---|
| 1275 |
|
|---|
| 1276 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1277 | DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
|
|---|
| 1278 | cluster_fatal("ctdbd died\n");
|
|---|
| 1279 | }
|
|---|
| 1280 | }
|
|---|
| 1281 |
|
|---|
| 1282 | done:
|
|---|
| 1283 | TALLOC_FREE(conn);
|
|---|
| 1284 | return status;
|
|---|
| 1285 | }
|
|---|
| 1286 |
|
|---|
| 1287 | /*
|
|---|
| 1288 | This is used to canonicalize a ctdb_sock_addr structure.
|
|---|
| 1289 | */
|
|---|
| 1290 | static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
|
|---|
| 1291 | struct sockaddr_storage *out)
|
|---|
| 1292 | {
|
|---|
| 1293 | memcpy(out, in, sizeof (*out));
|
|---|
| 1294 |
|
|---|
| 1295 | #ifdef HAVE_IPV6
|
|---|
| 1296 | if (in->ss_family == AF_INET6) {
|
|---|
| 1297 | const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
|
|---|
| 1298 | const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
|
|---|
| 1299 | struct sockaddr_in *out4 = (struct sockaddr_in *)out;
|
|---|
| 1300 | if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
|
|---|
| 1301 | memset(out, 0, sizeof(*out));
|
|---|
| 1302 | #ifdef HAVE_SOCK_SIN_LEN
|
|---|
| 1303 | out4->sin_len = sizeof(*out);
|
|---|
| 1304 | #endif
|
|---|
| 1305 | out4->sin_family = AF_INET;
|
|---|
| 1306 | out4->sin_port = in6->sin6_port;
|
|---|
| 1307 | memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
|
|---|
| 1308 | }
|
|---|
| 1309 | }
|
|---|
| 1310 | #endif
|
|---|
| 1311 | }
|
|---|
| 1312 |
|
|---|
| 1313 | /*
|
|---|
| 1314 | * Register us as a server for a particular tcp connection
|
|---|
| 1315 | */
|
|---|
| 1316 |
|
|---|
| 1317 | NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
|
|---|
| 1318 | const struct sockaddr_storage *_server,
|
|---|
| 1319 | const struct sockaddr_storage *_client,
|
|---|
| 1320 | void (*release_ip_handler)(const char *ip_addr,
|
|---|
| 1321 | void *private_data),
|
|---|
| 1322 | void *private_data)
|
|---|
| 1323 | {
|
|---|
| 1324 | /*
|
|---|
| 1325 | * we still use ctdb_control_tcp for ipv4
|
|---|
| 1326 | * because we want to work against older ctdb
|
|---|
| 1327 | * versions at runtime
|
|---|
| 1328 | */
|
|---|
| 1329 | struct ctdb_control_tcp p4;
|
|---|
| 1330 | #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
|
|---|
| 1331 | struct ctdb_control_tcp_addr p;
|
|---|
| 1332 | #endif
|
|---|
| 1333 | TDB_DATA data;
|
|---|
| 1334 | NTSTATUS status;
|
|---|
| 1335 | struct sockaddr_storage client;
|
|---|
| 1336 | struct sockaddr_storage server;
|
|---|
| 1337 |
|
|---|
| 1338 | /*
|
|---|
| 1339 | * Only one connection so far
|
|---|
| 1340 | */
|
|---|
| 1341 | SMB_ASSERT(conn->release_ip_handler == NULL);
|
|---|
| 1342 |
|
|---|
| 1343 | smbd_ctdb_canonicalize_ip(_client, &client);
|
|---|
| 1344 | smbd_ctdb_canonicalize_ip(_server, &server);
|
|---|
| 1345 |
|
|---|
| 1346 | switch (client.ss_family) {
|
|---|
| 1347 | case AF_INET:
|
|---|
| 1348 | p4.dest = *(struct sockaddr_in *)(void *)&server;
|
|---|
| 1349 | p4.src = *(struct sockaddr_in *)(void *)&client;
|
|---|
| 1350 | data.dptr = (uint8_t *)&p4;
|
|---|
| 1351 | data.dsize = sizeof(p4);
|
|---|
| 1352 | break;
|
|---|
| 1353 | #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
|
|---|
| 1354 | case AF_INET6:
|
|---|
| 1355 | p.dest.ip6 = *(struct sockaddr_in6 *)(void *)&server;
|
|---|
| 1356 | p.src.ip6 = *(struct sockaddr_in6 *)(void *)&client;
|
|---|
| 1357 | data.dptr = (uint8_t *)&p;
|
|---|
| 1358 | data.dsize = sizeof(p);
|
|---|
| 1359 | break;
|
|---|
| 1360 | #endif
|
|---|
| 1361 | default:
|
|---|
| 1362 | return NT_STATUS_INTERNAL_ERROR;
|
|---|
| 1363 | }
|
|---|
| 1364 |
|
|---|
| 1365 | conn->release_ip_handler = release_ip_handler;
|
|---|
| 1366 | /*
|
|---|
| 1367 | * store the IP address of the server socket for later
|
|---|
| 1368 | * comparison in release_ip()
|
|---|
| 1369 | */
|
|---|
| 1370 | conn->release_ip_priv = private_data;
|
|---|
| 1371 |
|
|---|
| 1372 | /*
|
|---|
| 1373 | * We want to be told about IP releases
|
|---|
| 1374 | */
|
|---|
| 1375 |
|
|---|
| 1376 | status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
|
|---|
| 1377 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1378 | return status;
|
|---|
| 1379 | }
|
|---|
| 1380 |
|
|---|
| 1381 | /*
|
|---|
| 1382 | * inform ctdb of our tcp connection, so if IP takeover happens ctdb
|
|---|
| 1383 | * can send an extra ack to trigger a reset for our client, so it
|
|---|
| 1384 | * immediately reconnects
|
|---|
| 1385 | */
|
|---|
| 1386 | return ctdbd_control(conn, CTDB_CURRENT_NODE,
|
|---|
| 1387 | CTDB_CONTROL_TCP_CLIENT, 0,
|
|---|
| 1388 | CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
|
|---|
| 1389 | }
|
|---|
| 1390 |
|
|---|
| 1391 | /*
|
|---|
| 1392 | * We want to handle reconfigure events
|
|---|
| 1393 | */
|
|---|
| 1394 | NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
|
|---|
| 1395 | {
|
|---|
| 1396 | return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
|
|---|
| 1397 | }
|
|---|
| 1398 |
|
|---|
| 1399 | /*
|
|---|
| 1400 | call a control on the local node
|
|---|
| 1401 | */
|
|---|
| 1402 | NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
|
|---|
| 1403 | uint64_t srvid, uint32_t flags, TDB_DATA data,
|
|---|
| 1404 | TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
|
|---|
| 1405 | int *cstatus)
|
|---|
| 1406 | {
|
|---|
| 1407 | return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
|
|---|
| 1408 | }
|
|---|
| 1409 |
|
|---|
| 1410 | NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
|
|---|
| 1411 | {
|
|---|
| 1412 | struct ctdb_client_notify_register reg_data;
|
|---|
| 1413 | size_t struct_len;
|
|---|
| 1414 | NTSTATUS status;
|
|---|
| 1415 | int cstatus;
|
|---|
| 1416 |
|
|---|
| 1417 | reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
|
|---|
| 1418 | reg_data.len = 1;
|
|---|
| 1419 | reg_data.notify_data[0] = 0;
|
|---|
| 1420 |
|
|---|
| 1421 | struct_len = offsetof(struct ctdb_client_notify_register,
|
|---|
| 1422 | notify_data) + reg_data.len;
|
|---|
| 1423 |
|
|---|
| 1424 | status = ctdbd_control_local(
|
|---|
| 1425 | conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
|
|---|
| 1426 | make_tdb_data((uint8_t *)®_data, struct_len),
|
|---|
| 1427 | NULL, NULL, &cstatus);
|
|---|
| 1428 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1429 | DEBUG(1, ("ctdbd_control_local failed: %s\n",
|
|---|
| 1430 | nt_errstr(status)));
|
|---|
| 1431 | }
|
|---|
| 1432 | return status;
|
|---|
| 1433 | }
|
|---|
| 1434 |
|
|---|
| 1435 | NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
|
|---|
| 1436 | {
|
|---|
| 1437 | struct ctdb_client_notify_deregister dereg_data;
|
|---|
| 1438 | NTSTATUS status;
|
|---|
| 1439 | int cstatus;
|
|---|
| 1440 |
|
|---|
| 1441 | dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
|
|---|
| 1442 |
|
|---|
| 1443 | status = ctdbd_control_local(
|
|---|
| 1444 | conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
|
|---|
| 1445 | make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
|
|---|
| 1446 | NULL, NULL, &cstatus);
|
|---|
| 1447 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1448 | DEBUG(1, ("ctdbd_control_local failed: %s\n",
|
|---|
| 1449 | nt_errstr(status)));
|
|---|
| 1450 | }
|
|---|
| 1451 | return status;
|
|---|
| 1452 | }
|
|---|
| 1453 |
|
|---|
| 1454 | #endif
|
|---|