s3:torture/test_smb2: check directory listing during reauth
[Samba.git] / source3 / lib / ctdbd_conn.c
blob9c7300207804fa3648d44c05a942b50d08a69bcb
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
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.
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.
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/>.
21 #include "includes.h"
22 #include "util_tdb.h"
23 #include "serverid.h"
25 #ifdef CLUSTER_SUPPORT
27 #include "ctdbd_conn.h"
28 #include "ctdb_packet.h"
29 #include "messages.h"
32 * It is not possible to include ctdb.h and tdb_compat.h (included via
33 * some other include above) without warnings. This fixes those
34 * warnings.
37 #ifdef typesafe_cb
38 #undef typesafe_cb
39 #endif
41 #ifdef typesafe_cb_preargs
42 #undef typesafe_cb_preargs
43 #endif
45 #ifdef typesafe_cb_postargs
46 #undef typesafe_cb_postargs
47 #endif
49 /* paths to these include files come from --with-ctdb= in configure */
51 #include "ctdb.h"
52 #include "ctdb_private.h"
54 struct ctdbd_connection {
55 struct messaging_context *msg_ctx;
56 uint32 reqid;
57 uint32 our_vnn;
58 uint64 rand_srvid;
59 struct ctdb_packet_context *pkt;
60 struct fd_event *fde;
62 void (*release_ip_handler)(const char *ip_addr, void *private_data);
63 void *release_ip_priv;
66 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
68 conn->reqid += 1;
69 if (conn->reqid == 0) {
70 conn->reqid += 1;
72 return conn->reqid;
75 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
76 uint32_t vnn, uint32 opcode,
77 uint64_t srvid, uint32_t flags, TDB_DATA data,
78 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
79 int *cstatus);
82 * exit on fatal communications errors with the ctdbd daemon
84 static void cluster_fatal(const char *why)
86 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
87 /* we don't use smb_panic() as we don't want to delay to write
88 a core file. We need to release this process id immediately
89 so that someone else can take over without getting sharing
90 violations */
91 _exit(1);
97 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
99 if (DEBUGLEVEL < 11) {
100 return;
102 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
103 (int)hdr->length, (int)hdr->ctdb_magic,
104 (int)hdr->ctdb_version, (int)hdr->generation,
105 (int)hdr->operation, (int)hdr->reqid));
109 * Register a srvid with ctdbd
111 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid)
114 int cstatus;
115 return ctdbd_control(conn, CTDB_CURRENT_NODE,
116 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
117 tdb_null, NULL, NULL, &cstatus);
121 * get our vnn from the cluster
123 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
125 int32_t cstatus=-1;
126 NTSTATUS status;
127 status = ctdbd_control(conn,
128 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
129 tdb_null, NULL, NULL, &cstatus);
130 if (!NT_STATUS_IS_OK(status)) {
131 cluster_fatal("ctdbd_control failed\n");
133 *vnn = (uint32_t)cstatus;
134 return status;
138 * Are we active (i.e. not banned or stopped?)
140 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
142 int32_t cstatus=-1;
143 NTSTATUS status;
144 TDB_DATA outdata;
145 struct ctdb_node_map *m;
146 uint32_t failure_flags;
147 bool ret = false;
148 int i;
150 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
151 CTDB_CONTROL_GET_NODEMAP, 0, 0,
152 tdb_null, talloc_tos(), &outdata, &cstatus);
153 if (!NT_STATUS_IS_OK(status)) {
154 cluster_fatal("ctdbd_control failed\n");
156 if ((cstatus != 0) || (outdata.dptr == NULL)) {
157 DEBUG(2, ("Received invalid ctdb data\n"));
158 return false;
161 m = (struct ctdb_node_map *)outdata.dptr;
163 for (i=0; i<m->num; i++) {
164 if (vnn == m->nodes[i].pnn) {
165 break;
169 if (i == m->num) {
170 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
171 (int)vnn));
172 goto fail;
175 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
176 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
178 if ((m->nodes[i].flags & failure_flags) != 0) {
179 DEBUG(2, ("Node has status %x, not active\n",
180 (int)m->nodes[i].flags));
181 goto fail;
184 ret = true;
185 fail:
186 TALLOC_FREE(outdata.dptr);
187 return ret;
190 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
192 return conn->our_vnn;
196 * Get us a ctdb connection
199 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
200 struct ctdb_packet_context **presult)
202 struct ctdb_packet_context *result;
203 const char *sockname = lp_ctdbd_socket();
204 struct sockaddr_un addr;
205 int fd;
207 if (!sockname || !*sockname) {
208 sockname = CTDB_PATH;
211 fd = socket(AF_UNIX, SOCK_STREAM, 0);
212 if (fd == -1) {
213 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
214 return map_nt_error_from_unix(errno);
217 ZERO_STRUCT(addr);
218 addr.sun_family = AF_UNIX;
219 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
221 if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
222 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
223 strerror(errno)));
224 close(fd);
225 return map_nt_error_from_unix(errno);
228 if (!(result = ctdb_packet_init(mem_ctx, fd))) {
229 close(fd);
230 return NT_STATUS_NO_MEMORY;
233 *presult = result;
234 return NT_STATUS_OK;
238 * Do we have a complete ctdb packet in the queue?
241 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
242 size_t *length,
243 void *private_data)
245 uint32 msglen;
247 if (available < sizeof(msglen)) {
248 return False;
251 msglen = *((uint32 *)buf);
253 DEBUG(11, ("msglen = %d\n", msglen));
255 if (msglen < sizeof(struct ctdb_req_header)) {
256 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
257 "the req_header\n", (int)msglen,
258 (int)sizeof(struct ctdb_req_header)));
259 cluster_fatal("ctdbd protocol error\n");
262 if (available < msglen) {
263 return false;
266 *length = msglen;
267 return true;
271 * State necessary to defer an incoming message while we are waiting for a
272 * ctdb reply.
275 struct deferred_msg_state {
276 struct messaging_context *msg_ctx;
277 struct messaging_rec *rec;
281 * Timed event handler for the deferred message
284 static void deferred_message_dispatch(struct event_context *event_ctx,
285 struct timed_event *te,
286 struct timeval now,
287 void *private_data)
289 struct deferred_msg_state *state = talloc_get_type_abort(
290 private_data, struct deferred_msg_state);
292 messaging_dispatch_rec(state->msg_ctx, state->rec);
293 TALLOC_FREE(state);
294 TALLOC_FREE(te);
297 struct req_pull_state {
298 TALLOC_CTX *mem_ctx;
299 DATA_BLOB req;
303 * Pull a ctdb request out of the incoming ctdb_packet queue
306 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
307 void *private_data)
309 struct req_pull_state *state = (struct req_pull_state *)private_data;
311 state->req.data = talloc_move(state->mem_ctx, &buf);
312 state->req.length = length;
313 return NT_STATUS_OK;
317 * Fetch a messaging_rec from an incoming ctdb style message
320 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
321 size_t overall_length,
322 struct ctdb_req_message *msg)
324 struct messaging_rec *result;
325 DATA_BLOB blob;
326 enum ndr_err_code ndr_err;
328 if ((overall_length < offsetof(struct ctdb_req_message, data))
329 || (overall_length
330 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
332 cluster_fatal("got invalid msg length");
335 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
336 DEBUG(0, ("talloc failed\n"));
337 return NULL;
340 blob = data_blob_const(msg->data, msg->datalen);
342 ndr_err = ndr_pull_struct_blob(
343 &blob, result, result,
344 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
346 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
347 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
348 ndr_errstr(ndr_err)));
349 TALLOC_FREE(result);
350 return NULL;
353 if (DEBUGLEVEL >= 11) {
354 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
355 NDR_PRINT_DEBUG(messaging_rec, result);
358 return result;
361 static NTSTATUS ctdb_packet_fd_read_sync(struct ctdb_packet_context *ctx)
363 int timeout = lp_ctdb_timeout();
365 if (timeout == 0) {
366 timeout = -1;
368 return ctdb_packet_fd_read_sync_timeout(ctx, timeout);
372 * Read a full ctdbd request. If we have a messaging context, defer incoming
373 * messages that might come in between.
376 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
377 TALLOC_CTX *mem_ctx, void *result)
379 struct ctdb_req_header *hdr;
380 struct req_pull_state state;
381 NTSTATUS status;
383 next_pkt:
384 ZERO_STRUCT(state);
385 state.mem_ctx = mem_ctx;
387 while (!ctdb_packet_handler(conn->pkt, ctdb_req_complete,
388 ctdb_req_pull, &state, &status)) {
390 * Not enough data
392 status = ctdb_packet_fd_read_sync(conn->pkt);
394 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
395 /* EAGAIN */
396 continue;
397 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
398 /* EAGAIN */
399 continue;
402 if (!NT_STATUS_IS_OK(status)) {
403 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
404 cluster_fatal("ctdbd died\n");
408 if (!NT_STATUS_IS_OK(status)) {
409 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status)));
410 cluster_fatal("ctdbd died\n");
413 hdr = (struct ctdb_req_header *)state.req.data;
415 DEBUG(11, ("Received ctdb packet\n"));
416 ctdb_packet_dump(hdr);
418 if (hdr->operation == CTDB_REQ_MESSAGE) {
419 struct timed_event *evt;
420 struct deferred_msg_state *msg_state;
421 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
423 if (conn->msg_ctx == NULL) {
424 DEBUG(1, ("Got a message without having a msg ctx, "
425 "dropping msg %llu\n",
426 (long long unsigned)msg->srvid));
427 goto next_pkt;
430 if ((conn->release_ip_handler != NULL)
431 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
432 /* must be dispatched immediately */
433 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
434 conn->release_ip_handler((const char *)msg->data,
435 conn->release_ip_priv);
436 TALLOC_FREE(hdr);
437 goto next_pkt;
440 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
441 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
443 DEBUG(1, ("ctdb_read_req: Got %s message\n",
444 (msg->srvid == CTDB_SRVID_RECONFIGURE)
445 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
447 messaging_send(conn->msg_ctx,
448 messaging_server_id(conn->msg_ctx),
449 MSG_SMB_BRL_VALIDATE, &data_blob_null);
450 messaging_send(conn->msg_ctx,
451 messaging_server_id(conn->msg_ctx),
452 MSG_DBWRAP_G_LOCK_RETRY,
453 &data_blob_null);
454 TALLOC_FREE(hdr);
455 goto next_pkt;
458 msg_state = talloc(NULL, struct deferred_msg_state);
459 if (msg_state == NULL) {
460 DEBUG(0, ("talloc failed\n"));
461 TALLOC_FREE(hdr);
462 goto next_pkt;
465 if (!(msg_state->rec = ctdb_pull_messaging_rec(
466 msg_state, state.req.length, msg))) {
467 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
468 TALLOC_FREE(msg_state);
469 TALLOC_FREE(hdr);
470 goto next_pkt;
473 TALLOC_FREE(hdr);
475 msg_state->msg_ctx = conn->msg_ctx;
478 * We're waiting for a call reply, but an async message has
479 * crossed. Defer dispatching to the toplevel event loop.
481 evt = event_add_timed(conn->msg_ctx->event_ctx,
482 conn->msg_ctx->event_ctx,
483 timeval_zero(),
484 deferred_message_dispatch,
485 msg_state);
486 if (evt == NULL) {
487 DEBUG(0, ("event_add_timed failed\n"));
488 TALLOC_FREE(msg_state);
489 TALLOC_FREE(hdr);
490 goto next_pkt;
493 goto next_pkt;
496 if ((reqid != 0) && (hdr->reqid != reqid)) {
497 /* we got the wrong reply */
498 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
499 "been %u\n", hdr->reqid, reqid));
500 TALLOC_FREE(hdr);
501 goto next_pkt;
504 *((void **)result) = talloc_move(mem_ctx, &hdr);
506 return NT_STATUS_OK;
510 * Get us a ctdbd connection
513 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
514 struct ctdbd_connection **pconn)
516 struct ctdbd_connection *conn;
517 NTSTATUS status;
519 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
520 DEBUG(0, ("talloc failed\n"));
521 return NT_STATUS_NO_MEMORY;
524 status = ctdbd_connect(conn, &conn->pkt);
526 if (!NT_STATUS_IS_OK(status)) {
527 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
528 goto fail;
531 status = get_cluster_vnn(conn, &conn->our_vnn);
533 if (!NT_STATUS_IS_OK(status)) {
534 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
535 goto fail;
538 if (!ctdbd_working(conn, conn->our_vnn)) {
539 DEBUG(2, ("Node is not working, can not connect\n"));
540 status = NT_STATUS_INTERNAL_DB_ERROR;
541 goto fail;
544 generate_random_buffer((unsigned char *)&conn->rand_srvid,
545 sizeof(conn->rand_srvid));
547 status = register_with_ctdbd(conn, conn->rand_srvid);
549 if (!NT_STATUS_IS_OK(status)) {
550 DEBUG(5, ("Could not register random srvid: %s\n",
551 nt_errstr(status)));
552 goto fail;
555 *pconn = conn;
556 return NT_STATUS_OK;
558 fail:
559 TALLOC_FREE(conn);
560 return status;
564 * Get us a ctdbd connection and register us as a process
567 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
568 struct ctdbd_connection **pconn)
570 struct ctdbd_connection *conn;
571 NTSTATUS status;
573 status = ctdbd_init_connection(mem_ctx, &conn);
575 if (!NT_STATUS_IS_OK(status)) {
576 return status;
579 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
580 if (!NT_STATUS_IS_OK(status)) {
581 goto fail;
584 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
585 if (!NT_STATUS_IS_OK(status)) {
586 goto fail;
589 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
590 if (!NT_STATUS_IS_OK(status)) {
591 goto fail;
594 *pconn = conn;
595 return NT_STATUS_OK;
597 fail:
598 TALLOC_FREE(conn);
599 return status;
602 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
604 return conn->msg_ctx;
607 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
609 return ctdb_packet_get_fd(conn->pkt);
613 * Packet handler to receive and handle a ctdb message
615 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
616 void *private_data)
618 struct ctdbd_connection *conn = talloc_get_type_abort(
619 private_data, struct ctdbd_connection);
620 struct ctdb_req_message *msg;
621 struct messaging_rec *msg_rec;
623 msg = (struct ctdb_req_message *)buf;
625 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
626 DEBUG(0, ("Received async msg of type %u, discarding\n",
627 msg->hdr.operation));
628 TALLOC_FREE(buf);
629 return NT_STATUS_INVALID_PARAMETER;
632 if ((conn->release_ip_handler != NULL)
633 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
634 /* must be dispatched immediately */
635 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
636 conn->release_ip_handler((const char *)msg->data,
637 conn->release_ip_priv);
638 TALLOC_FREE(buf);
639 return NT_STATUS_OK;
642 SMB_ASSERT(conn->msg_ctx != NULL);
644 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
645 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
646 DEBUG(0,("Got cluster reconfigure message\n"));
648 * when the cluster is reconfigured or someone of the
649 * family has passed away (SAMBA_NOTIFY), we need to
650 * clean the brl database
652 messaging_send(conn->msg_ctx,
653 messaging_server_id(conn->msg_ctx),
654 MSG_SMB_BRL_VALIDATE, &data_blob_null);
656 messaging_send(conn->msg_ctx,
657 messaging_server_id(conn->msg_ctx),
658 MSG_DBWRAP_G_LOCK_RETRY,
659 &data_blob_null);
661 TALLOC_FREE(buf);
662 return NT_STATUS_OK;
665 /* only messages to our pid or the broadcast are valid here */
666 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
667 DEBUG(0,("Got unexpected message with srvid=%llu\n",
668 (unsigned long long)msg->srvid));
669 TALLOC_FREE(buf);
670 return NT_STATUS_OK;
673 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
674 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
675 TALLOC_FREE(buf);
676 return NT_STATUS_NO_MEMORY;
679 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
681 TALLOC_FREE(msg_rec);
682 TALLOC_FREE(buf);
683 return NT_STATUS_OK;
687 * The ctdbd socket is readable asynchronuously
690 static void ctdbd_socket_handler(struct event_context *event_ctx,
691 struct fd_event *event,
692 uint16 flags,
693 void *private_data)
695 struct ctdbd_connection *conn = talloc_get_type_abort(
696 private_data, struct ctdbd_connection);
698 NTSTATUS status;
700 status = ctdb_packet_fd_read(conn->pkt);
702 if (!NT_STATUS_IS_OK(status)) {
703 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
704 cluster_fatal("ctdbd died\n");
707 while (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
708 ctdb_handle_message, conn, &status)) {
709 if (!NT_STATUS_IS_OK(status)) {
710 DEBUG(10, ("could not handle incoming message: %s\n",
711 nt_errstr(status)));
717 * Prepare a ctdbd connection to receive messages
720 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
721 struct messaging_context *msg_ctx)
723 SMB_ASSERT(conn->msg_ctx == NULL);
724 SMB_ASSERT(conn->fde == NULL);
726 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
727 ctdb_packet_get_fd(conn->pkt),
728 EVENT_FD_READ,
729 ctdbd_socket_handler,
730 conn))) {
731 DEBUG(0, ("event_add_fd failed\n"));
732 return NT_STATUS_NO_MEMORY;
735 conn->msg_ctx = msg_ctx;
737 return NT_STATUS_OK;
741 * Send a messaging message across a ctdbd
744 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
745 uint32 dst_vnn, uint64 dst_srvid,
746 struct messaging_rec *msg)
748 struct ctdb_req_message r;
749 TALLOC_CTX *mem_ctx;
750 DATA_BLOB blob;
751 NTSTATUS status;
752 enum ndr_err_code ndr_err;
754 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
755 DEBUG(0, ("talloc failed\n"));
756 return NT_STATUS_NO_MEMORY;
759 ndr_err = ndr_push_struct_blob(
760 &blob, mem_ctx, msg,
761 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
763 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
764 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
765 ndr_errstr(ndr_err)));
766 status = ndr_map_error2ntstatus(ndr_err);
767 goto fail;
770 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
771 r.hdr.ctdb_magic = CTDB_MAGIC;
772 r.hdr.ctdb_version = CTDB_VERSION;
773 r.hdr.generation = 1;
774 r.hdr.operation = CTDB_REQ_MESSAGE;
775 r.hdr.destnode = dst_vnn;
776 r.hdr.srcnode = conn->our_vnn;
777 r.hdr.reqid = 0;
778 r.srvid = dst_srvid;
779 r.datalen = blob.length;
781 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
782 ctdb_packet_dump(&r.hdr);
784 status = ctdb_packet_send(
785 conn->pkt, 2,
786 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
787 blob);
789 if (!NT_STATUS_IS_OK(status)) {
790 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
791 goto fail;
794 status = ctdb_packet_flush(conn->pkt);
796 if (!NT_STATUS_IS_OK(status)) {
797 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
798 cluster_fatal("cluster dispatch daemon msg write error\n");
801 status = NT_STATUS_OK;
802 fail:
803 TALLOC_FREE(mem_ctx);
804 return status;
808 * send/recv a generic ctdb control message
810 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
811 uint32_t vnn, uint32 opcode,
812 uint64_t srvid, uint32_t flags,
813 TDB_DATA data,
814 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
815 int *cstatus)
817 struct ctdb_req_control req;
818 struct ctdb_reply_control *reply = NULL;
819 struct ctdbd_connection *new_conn = NULL;
820 NTSTATUS status;
822 if (conn == NULL) {
823 status = ctdbd_init_connection(NULL, &new_conn);
825 if (!NT_STATUS_IS_OK(status)) {
826 DEBUG(10, ("Could not init temp connection: %s\n",
827 nt_errstr(status)));
828 goto fail;
831 conn = new_conn;
834 ZERO_STRUCT(req);
835 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
836 req.hdr.ctdb_magic = CTDB_MAGIC;
837 req.hdr.ctdb_version = CTDB_VERSION;
838 req.hdr.operation = CTDB_REQ_CONTROL;
839 req.hdr.reqid = ctdbd_next_reqid(conn);
840 req.hdr.destnode = vnn;
841 req.opcode = opcode;
842 req.srvid = srvid;
843 req.datalen = data.dsize;
844 req.flags = flags;
846 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
847 ctdb_packet_dump(&req.hdr);
849 status = ctdb_packet_send(
850 conn->pkt, 2,
851 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
852 data_blob_const(data.dptr, data.dsize));
854 if (!NT_STATUS_IS_OK(status)) {
855 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
856 goto fail;
859 status = ctdb_packet_flush(conn->pkt);
861 if (!NT_STATUS_IS_OK(status)) {
862 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
863 cluster_fatal("cluster dispatch daemon control write error\n");
866 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
867 TALLOC_FREE(new_conn);
868 if (cstatus) {
869 *cstatus = 0;
871 return NT_STATUS_OK;
874 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
876 if (!NT_STATUS_IS_OK(status)) {
877 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
878 goto fail;
881 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
882 DEBUG(0, ("received invalid reply\n"));
883 goto fail;
886 if (outdata) {
887 if (!(outdata->dptr = (uint8 *)talloc_memdup(
888 mem_ctx, reply->data, reply->datalen))) {
889 TALLOC_FREE(reply);
890 return NT_STATUS_NO_MEMORY;
892 outdata->dsize = reply->datalen;
894 if (cstatus) {
895 (*cstatus) = reply->status;
898 status = NT_STATUS_OK;
900 fail:
901 TALLOC_FREE(new_conn);
902 TALLOC_FREE(reply);
903 return status;
907 * see if a remote process exists
909 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
911 struct server_id id;
912 bool result;
914 id.pid = pid;
915 id.vnn = vnn;
917 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
918 DEBUG(10, ("ctdb_processes_exist failed\n"));
919 return false;
921 return result;
924 bool ctdb_processes_exist(struct ctdbd_connection *conn,
925 const struct server_id *pids, int num_pids,
926 bool *results)
928 TALLOC_CTX *frame = talloc_stackframe();
929 int i, num_received;
930 NTSTATUS status;
931 uint32_t *reqids;
932 bool result = false;
934 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
935 if (reqids == NULL) {
936 goto fail;
939 for (i=0; i<num_pids; i++) {
940 struct ctdb_req_control req;
941 pid_t pid;
943 results[i] = false;
944 reqids[i] = ctdbd_next_reqid(conn);
946 ZERO_STRUCT(req);
949 * pids[i].pid is uint64_t, scale down to pid_t which
950 * is the wire protocol towards ctdb.
952 pid = pids[i].pid;
954 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
955 (int)pids[i].vnn, (int)pid,
956 (int)reqids[i]));
958 req.hdr.length = offsetof(struct ctdb_req_control, data);
959 req.hdr.length += sizeof(pid);
960 req.hdr.ctdb_magic = CTDB_MAGIC;
961 req.hdr.ctdb_version = CTDB_VERSION;
962 req.hdr.operation = CTDB_REQ_CONTROL;
963 req.hdr.reqid = reqids[i];
964 req.hdr.destnode = pids[i].vnn;
965 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
966 req.srvid = 0;
967 req.datalen = sizeof(pid);
968 req.flags = 0;
970 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
971 ctdb_packet_dump(&req.hdr);
973 status = ctdb_packet_send(
974 conn->pkt, 2,
975 data_blob_const(
976 &req, offsetof(struct ctdb_req_control, data)),
977 data_blob_const(&pid, sizeof(pid)));
978 if (!NT_STATUS_IS_OK(status)) {
979 DEBUG(10, ("ctdb_packet_send failed: %s\n",
980 nt_errstr(status)));
981 goto fail;
985 status = ctdb_packet_flush(conn->pkt);
986 if (!NT_STATUS_IS_OK(status)) {
987 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
988 nt_errstr(status)));
989 goto fail;
992 num_received = 0;
994 while (num_received < num_pids) {
995 struct ctdb_reply_control *reply = NULL;
996 uint32_t reqid;
998 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
999 if (!NT_STATUS_IS_OK(status)) {
1000 DEBUG(10, ("ctdb_read_req failed: %s\n",
1001 nt_errstr(status)));
1002 goto fail;
1005 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1006 DEBUG(10, ("Received invalid reply\n"));
1007 goto fail;
1010 reqid = reply->hdr.reqid;
1012 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1014 for (i=0; i<num_pids; i++) {
1015 if (reqid == reqids[i]) {
1016 break;
1019 if (i == num_pids) {
1020 DEBUG(10, ("Received unknown record number %u\n",
1021 (unsigned)reqid));
1022 goto fail;
1024 results[i] = ((reply->status) == 0);
1025 TALLOC_FREE(reply);
1026 num_received += 1;
1029 result = true;
1030 fail:
1031 TALLOC_FREE(frame);
1032 return result;
1035 struct ctdb_vnn_list {
1036 uint32_t vnn;
1037 uint32_t reqid;
1038 unsigned num_srvids;
1039 unsigned num_filled;
1040 uint64_t *srvids;
1041 unsigned *pid_indexes;
1045 * Get a list of all vnns mentioned in a list of
1046 * server_ids. vnn_indexes tells where in the vnns array we have to
1047 * place the pids.
1049 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1050 const struct server_id *pids, unsigned num_pids,
1051 struct ctdb_vnn_list **pvnns,
1052 unsigned *pnum_vnns)
1054 struct ctdb_vnn_list *vnns = NULL;
1055 unsigned *vnn_indexes = NULL;
1056 unsigned i, num_vnns = 0;
1058 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1059 if (vnn_indexes == NULL) {
1060 goto fail;
1063 for (i=0; i<num_pids; i++) {
1064 unsigned j;
1065 uint32_t vnn = pids[i].vnn;
1067 for (j=0; j<num_vnns; j++) {
1068 if (vnn == vnns[j].vnn) {
1069 break;
1072 vnn_indexes[i] = j;
1074 if (j < num_vnns) {
1076 * Already in the array
1078 vnns[j].num_srvids += 1;
1079 continue;
1081 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1082 num_vnns+1);
1083 if (vnns == NULL) {
1084 goto fail;
1086 vnns[num_vnns].vnn = vnn;
1087 vnns[num_vnns].num_srvids = 1;
1088 vnns[num_vnns].num_filled = 0;
1089 num_vnns += 1;
1091 for (i=0; i<num_vnns; i++) {
1092 struct ctdb_vnn_list *vnn = &vnns[i];
1094 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1095 if (vnn->srvids == NULL) {
1096 goto fail;
1098 vnn->pid_indexes = talloc_array(vnns, unsigned,
1099 vnn->num_srvids);
1100 if (vnn->pid_indexes == NULL) {
1101 goto fail;
1104 for (i=0; i<num_pids; i++) {
1105 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1106 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1107 vnn->pid_indexes[vnn->num_filled] = i;
1108 vnn->num_filled += 1;
1111 TALLOC_FREE(vnn_indexes);
1112 *pvnns = vnns;
1113 *pnum_vnns = num_vnns;
1114 return true;
1115 fail:
1116 TALLOC_FREE(vnns);
1117 TALLOC_FREE(vnn_indexes);
1118 return false;
1121 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1123 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1124 const struct server_id *pids, unsigned num_pids,
1125 bool *results)
1127 unsigned i, num_received;
1128 NTSTATUS status;
1129 struct ctdb_vnn_list *vnns = NULL;
1130 unsigned num_vnns;
1131 bool result = false;
1133 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1134 &vnns, &num_vnns)) {
1135 goto fail;
1138 for (i=0; i<num_vnns; i++) {
1139 struct ctdb_vnn_list *vnn = &vnns[i];
1140 struct ctdb_req_control req;
1142 vnn->reqid = ctdbd_next_reqid(conn);
1144 ZERO_STRUCT(req);
1146 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1147 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1149 req.hdr.length = offsetof(struct ctdb_req_control, data);
1150 req.hdr.ctdb_magic = CTDB_MAGIC;
1151 req.hdr.ctdb_version = CTDB_VERSION;
1152 req.hdr.operation = CTDB_REQ_CONTROL;
1153 req.hdr.reqid = vnn->reqid;
1154 req.hdr.destnode = vnn->vnn;
1155 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1156 req.srvid = 0;
1157 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1158 req.hdr.length += req.datalen;
1159 req.flags = 0;
1161 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1162 ctdb_packet_dump(&req.hdr);
1164 status = ctdb_packet_send(
1165 conn->pkt, 2,
1166 data_blob_const(
1167 &req, offsetof(struct ctdb_req_control,
1168 data)),
1169 data_blob_const(vnn->srvids, req.datalen));
1170 if (!NT_STATUS_IS_OK(status)) {
1171 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1172 nt_errstr(status)));
1173 goto fail;
1177 status = ctdb_packet_flush(conn->pkt);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1180 nt_errstr(status)));
1181 goto fail;
1184 num_received = 0;
1186 while (num_received < num_vnns) {
1187 struct ctdb_reply_control *reply = NULL;
1188 struct ctdb_vnn_list *vnn;
1189 uint32_t reqid;
1191 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1192 if (!NT_STATUS_IS_OK(status)) {
1193 DEBUG(10, ("ctdb_read_req failed: %s\n",
1194 nt_errstr(status)));
1195 goto fail;
1198 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1199 DEBUG(10, ("Received invalid reply\n"));
1200 goto fail;
1203 reqid = reply->hdr.reqid;
1205 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1207 for (i=0; i<num_vnns; i++) {
1208 if (reqid == vnns[i].reqid) {
1209 break;
1212 if (i == num_vnns) {
1213 DEBUG(10, ("Received unknown reqid number %u\n",
1214 (unsigned)reqid));
1215 goto fail;
1218 DEBUG(10, ("Found index %u\n", i));
1220 vnn = &vnns[i];
1222 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1223 (unsigned)vnn->vnn, vnn->num_srvids,
1224 (unsigned)reply->datalen));
1226 if (reply->datalen < ((vnn->num_srvids+7)/8)) {
1227 DEBUG(10, ("Received short reply\n"));
1228 goto fail;
1231 for (i=0; i<vnn->num_srvids; i++) {
1232 int idx = vnn->pid_indexes[i];
1234 if (pids[i].unique_id ==
1235 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1236 results[idx] = true;
1237 continue;
1239 results[idx] = ((reply->data[i/8] & (1<<(i%8))) != 0);
1242 TALLOC_FREE(reply);
1243 num_received += 1;
1246 result = true;
1247 fail:
1248 TALLOC_FREE(vnns);
1249 return result;
1252 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1255 * Get a db path
1257 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1258 TALLOC_CTX *mem_ctx, uint32_t db_id)
1260 NTSTATUS status;
1261 TDB_DATA data;
1262 int32_t cstatus;
1264 data.dptr = (uint8_t*)&db_id;
1265 data.dsize = sizeof(db_id);
1267 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1268 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1269 mem_ctx, &data, &cstatus);
1270 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1271 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1272 return NULL;
1275 return (char *)data.dptr;
1279 * attach to a ctdb database
1281 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1282 const char *name, uint32_t *db_id, int tdb_flags)
1284 NTSTATUS status;
1285 TDB_DATA data;
1286 int32_t cstatus;
1287 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1289 data.dptr = (uint8_t*)name;
1290 data.dsize = strlen(name)+1;
1292 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1293 persistent
1294 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1295 : CTDB_CONTROL_DB_ATTACH,
1296 tdb_flags, 0, data, NULL, &data, &cstatus);
1297 if (!NT_STATUS_IS_OK(status)) {
1298 DEBUG(0, (__location__ " ctdb_control for db_attach "
1299 "failed: %s\n", nt_errstr(status)));
1300 return status;
1303 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1304 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1305 return NT_STATUS_INTERNAL_ERROR;
1308 *db_id = *(uint32_t *)data.dptr;
1309 talloc_free(data.dptr);
1311 if (!(tdb_flags & TDB_SEQNUM)) {
1312 return NT_STATUS_OK;
1315 data.dptr = (uint8_t *)db_id;
1316 data.dsize = sizeof(*db_id);
1318 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1319 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1320 NULL, NULL, &cstatus);
1321 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1322 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1323 "failed\n"));
1324 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1325 status;
1328 return NT_STATUS_OK;
1332 * force the migration of a record to this node
1334 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
1335 TDB_DATA key)
1337 struct ctdb_req_call req;
1338 struct ctdb_reply_call *reply;
1339 NTSTATUS status;
1341 ZERO_STRUCT(req);
1343 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1344 req.hdr.ctdb_magic = CTDB_MAGIC;
1345 req.hdr.ctdb_version = CTDB_VERSION;
1346 req.hdr.operation = CTDB_REQ_CALL;
1347 req.hdr.reqid = ctdbd_next_reqid(conn);
1348 req.flags = CTDB_IMMEDIATE_MIGRATION;
1349 req.callid = CTDB_NULL_FUNC;
1350 req.db_id = db_id;
1351 req.keylen = key.dsize;
1353 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1354 ctdb_packet_dump(&req.hdr);
1356 status = ctdb_packet_send(
1357 conn->pkt, 2,
1358 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1359 data_blob_const(key.dptr, key.dsize));
1361 if (!NT_STATUS_IS_OK(status)) {
1362 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1363 return status;
1366 status = ctdb_packet_flush(conn->pkt);
1368 if (!NT_STATUS_IS_OK(status)) {
1369 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1370 cluster_fatal("cluster dispatch daemon control write error\n");
1373 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1375 if (!NT_STATUS_IS_OK(status)) {
1376 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1377 goto fail;
1380 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1381 DEBUG(0, ("received invalid reply\n"));
1382 status = NT_STATUS_INTERNAL_ERROR;
1383 goto fail;
1386 status = NT_STATUS_OK;
1387 fail:
1389 TALLOC_FREE(reply);
1390 return status;
1394 * remotely fetch a record without locking it or forcing a migration
1396 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
1397 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
1399 struct ctdb_req_call req;
1400 struct ctdb_reply_call *reply;
1401 NTSTATUS status;
1403 ZERO_STRUCT(req);
1405 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1406 req.hdr.ctdb_magic = CTDB_MAGIC;
1407 req.hdr.ctdb_version = CTDB_VERSION;
1408 req.hdr.operation = CTDB_REQ_CALL;
1409 req.hdr.reqid = ctdbd_next_reqid(conn);
1410 req.flags = 0;
1411 req.callid = CTDB_FETCH_FUNC;
1412 req.db_id = db_id;
1413 req.keylen = key.dsize;
1415 status = ctdb_packet_send(
1416 conn->pkt, 2,
1417 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1418 data_blob_const(key.dptr, key.dsize));
1420 if (!NT_STATUS_IS_OK(status)) {
1421 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1422 return status;
1425 status = ctdb_packet_flush(conn->pkt);
1427 if (!NT_STATUS_IS_OK(status)) {
1428 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1429 cluster_fatal("cluster dispatch daemon control write error\n");
1432 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1434 if (!NT_STATUS_IS_OK(status)) {
1435 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1436 goto fail;
1439 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1440 DEBUG(0, ("received invalid reply\n"));
1441 status = NT_STATUS_INTERNAL_ERROR;
1442 goto fail;
1445 data->dsize = reply->datalen;
1446 if (data->dsize == 0) {
1447 data->dptr = NULL;
1448 goto done;
1451 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1452 reply->datalen);
1453 if (data->dptr == NULL) {
1454 DEBUG(0, ("talloc failed\n"));
1455 status = NT_STATUS_NO_MEMORY;
1456 goto fail;
1459 done:
1460 status = NT_STATUS_OK;
1461 fail:
1462 TALLOC_FREE(reply);
1463 return status;
1466 struct ctdbd_traverse_state {
1467 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1468 void *private_data;
1472 * Handle a traverse record coming in on the ctdbd connection
1475 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1476 void *private_data)
1478 struct ctdbd_traverse_state *state =
1479 (struct ctdbd_traverse_state *)private_data;
1481 struct ctdb_req_message *m;
1482 struct ctdb_rec_data *d;
1483 TDB_DATA key, data;
1485 m = (struct ctdb_req_message *)buf;
1487 if (length < sizeof(*m) || m->hdr.length != length) {
1488 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1489 TALLOC_FREE(buf);
1490 return NT_STATUS_UNEXPECTED_IO_ERROR;
1493 d = (struct ctdb_rec_data *)&m->data[0];
1494 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1495 DEBUG(0, ("Got invalid traverse data of length %d\n",
1496 (int)m->datalen));
1497 TALLOC_FREE(buf);
1498 return NT_STATUS_UNEXPECTED_IO_ERROR;
1501 key.dsize = d->keylen;
1502 key.dptr = &d->data[0];
1503 data.dsize = d->datalen;
1504 data.dptr = &d->data[d->keylen];
1506 if (key.dsize == 0 && data.dsize == 0) {
1507 /* end of traverse */
1508 return NT_STATUS_END_OF_FILE;
1511 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1512 DEBUG(0, ("Got invalid ltdb header length %d\n",
1513 (int)data.dsize));
1514 TALLOC_FREE(buf);
1515 return NT_STATUS_UNEXPECTED_IO_ERROR;
1517 data.dsize -= sizeof(struct ctdb_ltdb_header);
1518 data.dptr += sizeof(struct ctdb_ltdb_header);
1520 if (state->fn) {
1521 state->fn(key, data, state->private_data);
1524 TALLOC_FREE(buf);
1525 return NT_STATUS_OK;
1529 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1530 connection to ctdbd to avoid the hairy recursive and async problems with
1531 everything in-line.
1534 NTSTATUS ctdbd_traverse(uint32 db_id,
1535 void (*fn)(TDB_DATA key, TDB_DATA data,
1536 void *private_data),
1537 void *private_data)
1539 struct ctdbd_connection *conn;
1540 NTSTATUS status;
1542 TDB_DATA data;
1543 struct ctdb_traverse_start t;
1544 int cstatus;
1545 struct ctdbd_traverse_state state;
1547 become_root();
1548 status = ctdbd_init_connection(NULL, &conn);
1549 unbecome_root();
1550 if (!NT_STATUS_IS_OK(status)) {
1551 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1552 nt_errstr(status)));
1553 return status;
1556 t.db_id = db_id;
1557 t.srvid = conn->rand_srvid;
1558 t.reqid = ctdbd_next_reqid(conn);
1560 data.dptr = (uint8_t *)&t;
1561 data.dsize = sizeof(t);
1563 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1564 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1565 data, NULL, NULL, &cstatus);
1567 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1569 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1570 cstatus));
1572 if (NT_STATUS_IS_OK(status)) {
1574 * We need a mapping here
1576 status = NT_STATUS_UNSUCCESSFUL;
1578 goto done;
1581 state.fn = fn;
1582 state.private_data = private_data;
1584 while (True) {
1586 status = NT_STATUS_OK;
1588 if (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
1589 ctdb_traverse_handler, &state, &status)) {
1591 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1592 status = NT_STATUS_OK;
1593 break;
1597 * There might be more in the queue
1599 continue;
1602 if (!NT_STATUS_IS_OK(status)) {
1603 break;
1606 status = ctdb_packet_fd_read_sync(conn->pkt);
1608 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1610 * There might be more in the queue
1612 continue;
1615 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1616 status = NT_STATUS_OK;
1617 break;
1620 if (!NT_STATUS_IS_OK(status)) {
1621 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1622 cluster_fatal("ctdbd died\n");
1626 done:
1627 TALLOC_FREE(conn);
1628 return status;
1632 This is used to canonicalize a ctdb_sock_addr structure.
1634 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1635 struct sockaddr_storage *out)
1637 memcpy(out, in, sizeof (*out));
1639 #ifdef HAVE_IPV6
1640 if (in->ss_family == AF_INET6) {
1641 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1642 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1643 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1644 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1645 memset(out, 0, sizeof(*out));
1646 #ifdef HAVE_SOCK_SIN_LEN
1647 out4->sin_len = sizeof(*out);
1648 #endif
1649 out4->sin_family = AF_INET;
1650 out4->sin_port = in6->sin6_port;
1651 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1654 #endif
1658 * Register us as a server for a particular tcp connection
1661 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1662 const struct sockaddr_storage *_server,
1663 const struct sockaddr_storage *_client,
1664 void (*release_ip_handler)(const char *ip_addr,
1665 void *private_data),
1666 void *private_data)
1669 * we still use ctdb_control_tcp for ipv4
1670 * because we want to work against older ctdb
1671 * versions at runtime
1673 struct ctdb_control_tcp p4;
1674 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1675 struct ctdb_control_tcp_addr p;
1676 #endif
1677 TDB_DATA data;
1678 NTSTATUS status;
1679 struct sockaddr_storage client;
1680 struct sockaddr_storage server;
1683 * Only one connection so far
1685 SMB_ASSERT(conn->release_ip_handler == NULL);
1687 smbd_ctdb_canonicalize_ip(_client, &client);
1688 smbd_ctdb_canonicalize_ip(_server, &server);
1690 switch (client.ss_family) {
1691 case AF_INET:
1692 memcpy(&p4.dest, &server, sizeof(p4.dest));
1693 memcpy(&p4.src, &client, sizeof(p4.src));
1694 data.dptr = (uint8_t *)&p4;
1695 data.dsize = sizeof(p4);
1696 break;
1697 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1698 case AF_INET6:
1699 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1700 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1701 data.dptr = (uint8_t *)&p;
1702 data.dsize = sizeof(p);
1703 break;
1704 #endif
1705 default:
1706 return NT_STATUS_INTERNAL_ERROR;
1709 conn->release_ip_handler = release_ip_handler;
1710 conn->release_ip_priv = private_data;
1713 * We want to be told about IP releases
1716 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 return status;
1722 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1723 * can send an extra ack to trigger a reset for our client, so it
1724 * immediately reconnects
1726 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1727 CTDB_CONTROL_TCP_CLIENT, 0,
1728 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1732 * We want to handle reconfigure events
1734 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1736 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1740 call a control on the local node
1742 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1743 uint64_t srvid, uint32_t flags, TDB_DATA data,
1744 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1745 int *cstatus)
1747 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1750 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1752 struct ctdb_client_notify_register reg_data;
1753 size_t struct_len;
1754 NTSTATUS status;
1755 int cstatus;
1757 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1758 reg_data.len = 1;
1759 reg_data.notify_data[0] = 0;
1761 struct_len = offsetof(struct ctdb_client_notify_register,
1762 notify_data) + reg_data.len;
1764 status = ctdbd_control_local(
1765 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1766 make_tdb_data((uint8_t *)&reg_data, struct_len),
1767 NULL, NULL, &cstatus);
1768 if (!NT_STATUS_IS_OK(status)) {
1769 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1770 nt_errstr(status)));
1772 return status;
1775 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1777 struct ctdb_client_notify_deregister dereg_data;
1778 NTSTATUS status;
1779 int cstatus;
1781 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1783 status = ctdbd_control_local(
1784 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1785 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1786 NULL, NULL, &cstatus);
1787 if (!NT_STATUS_IS_OK(status)) {
1788 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1789 nt_errstr(status)));
1791 return status;
1794 #endif