s3:lib/ctdbd_conn: let release_ip_handler return bool
[Samba.git] / source3 / lib / ctdbd_conn.c
blob1c70b3bec728ac0fef2559c25c62607bc679ee6f
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"
24 #include "ctdbd_conn.h"
26 #ifdef CLUSTER_SUPPORT
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_t reqid;
57 uint32_t our_vnn;
58 uint64_t rand_srvid;
59 struct ctdb_packet_context *pkt;
60 struct tevent_fd *fde;
62 bool (*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_t 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_t *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_t 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;
206 socklen_t salen;
208 fd = socket(AF_UNIX, SOCK_STREAM, 0);
209 if (fd == -1) {
210 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
211 return map_nt_error_from_unix(errno);
214 ZERO_STRUCT(addr);
215 addr.sun_family = AF_UNIX;
216 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sockname);
218 salen = sizeof(struct sockaddr_un);
219 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
220 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
221 strerror(errno)));
222 close(fd);
223 return map_nt_error_from_unix(errno);
226 if (!(result = ctdb_packet_init(mem_ctx, fd))) {
227 close(fd);
228 return NT_STATUS_NO_MEMORY;
231 *presult = result;
232 return NT_STATUS_OK;
236 * Do we have a complete ctdb packet in the queue?
239 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
240 size_t *length,
241 void *private_data)
243 uint32_t msglen;
245 if (available < sizeof(msglen)) {
246 return False;
249 msglen = *((const uint32_t *)buf);
251 DEBUG(11, ("msglen = %d\n", msglen));
253 if (msglen < sizeof(struct ctdb_req_header)) {
254 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
255 "the req_header\n", (int)msglen,
256 (int)sizeof(struct ctdb_req_header)));
257 cluster_fatal("ctdbd protocol error\n");
260 if (available < msglen) {
261 return false;
264 *length = msglen;
265 return true;
269 * State necessary to defer an incoming message while we are waiting for a
270 * ctdb reply.
273 struct deferred_msg_state {
274 struct messaging_context *msg_ctx;
275 struct messaging_rec *rec;
279 * Timed event handler for the deferred message
282 static void deferred_message_dispatch(struct tevent_context *event_ctx,
283 struct tevent_timer *te,
284 struct timeval now,
285 void *private_data)
287 struct deferred_msg_state *state = talloc_get_type_abort(
288 private_data, struct deferred_msg_state);
290 messaging_dispatch_rec(state->msg_ctx, state->rec);
291 TALLOC_FREE(state);
292 TALLOC_FREE(te);
295 struct req_pull_state {
296 TALLOC_CTX *mem_ctx;
297 DATA_BLOB req;
301 * Pull a ctdb request out of the incoming ctdb_packet queue
304 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
305 void *private_data)
307 struct req_pull_state *state = (struct req_pull_state *)private_data;
309 state->req.data = talloc_move(state->mem_ctx, &buf);
310 state->req.length = length;
311 return NT_STATUS_OK;
315 * Fetch a messaging_rec from an incoming ctdb style message
318 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
319 size_t overall_length,
320 struct ctdb_req_message *msg)
322 struct messaging_rec *result;
323 DATA_BLOB blob;
324 enum ndr_err_code ndr_err;
326 if ((overall_length < offsetof(struct ctdb_req_message, data))
327 || (overall_length
328 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
330 cluster_fatal("got invalid msg length");
333 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
334 DEBUG(0, ("talloc failed\n"));
335 return NULL;
338 blob = data_blob_const(msg->data, msg->datalen);
340 ndr_err = ndr_pull_struct_blob(
341 &blob, result, result,
342 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
344 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
345 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
346 ndr_errstr(ndr_err)));
347 TALLOC_FREE(result);
348 return NULL;
351 if (DEBUGLEVEL >= 11) {
352 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
353 NDR_PRINT_DEBUG(messaging_rec, result);
356 return result;
359 static NTSTATUS ctdb_packet_fd_read_sync(struct ctdb_packet_context *ctx)
361 int timeout = lp_ctdb_timeout();
363 if (timeout == 0) {
364 timeout = -1;
366 return ctdb_packet_fd_read_sync_timeout(ctx, timeout);
370 * Read a full ctdbd request. If we have a messaging context, defer incoming
371 * messages that might come in between.
374 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
375 TALLOC_CTX *mem_ctx, void *result)
377 struct ctdb_req_header *hdr;
378 struct req_pull_state state;
379 NTSTATUS status;
381 next_pkt:
382 ZERO_STRUCT(state);
383 state.mem_ctx = mem_ctx;
385 while (!ctdb_packet_handler(conn->pkt, ctdb_req_complete,
386 ctdb_req_pull, &state, &status)) {
388 * Not enough data
390 status = ctdb_packet_fd_read_sync(conn->pkt);
392 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
393 /* EAGAIN */
394 continue;
395 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
396 /* EAGAIN */
397 continue;
400 if (!NT_STATUS_IS_OK(status)) {
401 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
402 cluster_fatal("ctdbd died\n");
406 if (!NT_STATUS_IS_OK(status)) {
407 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status)));
408 cluster_fatal("ctdbd died\n");
411 hdr = (struct ctdb_req_header *)state.req.data;
413 DEBUG(11, ("Received ctdb packet\n"));
414 ctdb_packet_dump(hdr);
416 if (hdr->operation == CTDB_REQ_MESSAGE) {
417 struct tevent_timer *evt;
418 struct deferred_msg_state *msg_state;
419 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
421 if (conn->msg_ctx == NULL) {
422 DEBUG(1, ("Got a message without having a msg ctx, "
423 "dropping msg %llu\n",
424 (long long unsigned)msg->srvid));
425 goto next_pkt;
428 if ((conn->release_ip_handler != NULL)
429 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
430 bool ret;
432 /* must be dispatched immediately */
433 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
434 ret = conn->release_ip_handler((const char *)msg->data,
435 conn->release_ip_priv);
436 if (ret) {
438 * We need to release the ip,
439 * so return an error to the upper layers.
441 * We make sure we don't trigger this again.
443 conn->release_ip_handler = NULL;
444 conn->release_ip_priv = NULL;
445 return NT_STATUS_ADDRESS_CLOSED;
447 TALLOC_FREE(hdr);
448 goto next_pkt;
451 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
452 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
454 DEBUG(1, ("ctdb_read_req: Got %s message\n",
455 (msg->srvid == CTDB_SRVID_RECONFIGURE)
456 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
458 messaging_send(conn->msg_ctx,
459 messaging_server_id(conn->msg_ctx),
460 MSG_SMB_BRL_VALIDATE, &data_blob_null);
461 messaging_send(conn->msg_ctx,
462 messaging_server_id(conn->msg_ctx),
463 MSG_DBWRAP_G_LOCK_RETRY,
464 &data_blob_null);
465 TALLOC_FREE(hdr);
466 goto next_pkt;
469 msg_state = talloc(NULL, struct deferred_msg_state);
470 if (msg_state == NULL) {
471 DEBUG(0, ("talloc failed\n"));
472 TALLOC_FREE(hdr);
473 goto next_pkt;
476 if (!(msg_state->rec = ctdb_pull_messaging_rec(
477 msg_state, state.req.length, msg))) {
478 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
479 TALLOC_FREE(msg_state);
480 TALLOC_FREE(hdr);
481 goto next_pkt;
484 TALLOC_FREE(hdr);
486 msg_state->msg_ctx = conn->msg_ctx;
489 * We're waiting for a call reply, but an async message has
490 * crossed. Defer dispatching to the toplevel event loop.
492 evt = tevent_add_timer(conn->msg_ctx->event_ctx,
493 conn->msg_ctx->event_ctx,
494 timeval_zero(),
495 deferred_message_dispatch,
496 msg_state);
497 if (evt == NULL) {
498 DEBUG(0, ("event_add_timed failed\n"));
499 TALLOC_FREE(msg_state);
500 TALLOC_FREE(hdr);
501 goto next_pkt;
504 goto next_pkt;
507 if ((reqid != 0) && (hdr->reqid != reqid)) {
508 /* we got the wrong reply */
509 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
510 "been %u\n", hdr->reqid, reqid));
511 TALLOC_FREE(hdr);
512 goto next_pkt;
515 *((void **)result) = talloc_move(mem_ctx, &hdr);
517 return NT_STATUS_OK;
521 * Get us a ctdbd connection
524 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
525 struct ctdbd_connection **pconn)
527 struct ctdbd_connection *conn;
528 NTSTATUS status;
530 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
531 DEBUG(0, ("talloc failed\n"));
532 return NT_STATUS_NO_MEMORY;
535 status = ctdbd_connect(conn, &conn->pkt);
537 if (!NT_STATUS_IS_OK(status)) {
538 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
539 goto fail;
542 status = get_cluster_vnn(conn, &conn->our_vnn);
544 if (!NT_STATUS_IS_OK(status)) {
545 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
546 goto fail;
549 if (!ctdbd_working(conn, conn->our_vnn)) {
550 DEBUG(2, ("Node is not working, can not connect\n"));
551 status = NT_STATUS_INTERNAL_DB_ERROR;
552 goto fail;
555 generate_random_buffer((unsigned char *)&conn->rand_srvid,
556 sizeof(conn->rand_srvid));
558 status = register_with_ctdbd(conn, conn->rand_srvid);
560 if (!NT_STATUS_IS_OK(status)) {
561 DEBUG(5, ("Could not register random srvid: %s\n",
562 nt_errstr(status)));
563 goto fail;
566 *pconn = conn;
567 return NT_STATUS_OK;
569 fail:
570 TALLOC_FREE(conn);
571 return status;
575 * Get us a ctdbd connection and register us as a process
578 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
579 struct ctdbd_connection **pconn)
581 struct ctdbd_connection *conn;
582 NTSTATUS status;
584 status = ctdbd_init_connection(mem_ctx, &conn);
586 if (!NT_STATUS_IS_OK(status)) {
587 return status;
590 status = register_with_ctdbd(conn, (uint64_t)getpid());
591 if (!NT_STATUS_IS_OK(status)) {
592 goto fail;
595 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
596 if (!NT_STATUS_IS_OK(status)) {
597 goto fail;
600 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
601 if (!NT_STATUS_IS_OK(status)) {
602 goto fail;
605 *pconn = conn;
606 return NT_STATUS_OK;
608 fail:
609 TALLOC_FREE(conn);
610 return status;
613 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
615 return conn->msg_ctx;
618 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
620 return ctdb_packet_get_fd(conn->pkt);
624 * Packet handler to receive and handle a ctdb message
626 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
627 void *private_data)
629 struct ctdbd_connection *conn = talloc_get_type_abort(
630 private_data, struct ctdbd_connection);
631 struct ctdb_req_message *msg;
632 struct messaging_rec *msg_rec;
634 msg = (struct ctdb_req_message *)buf;
636 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
637 DEBUG(0, ("Received async msg of type %u, discarding\n",
638 msg->hdr.operation));
639 TALLOC_FREE(buf);
640 return NT_STATUS_INVALID_PARAMETER;
643 if ((conn->release_ip_handler != NULL)
644 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
645 bool ret;
647 /* must be dispatched immediately */
648 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
649 ret = conn->release_ip_handler((const char *)msg->data,
650 conn->release_ip_priv);
651 if (ret) {
653 * We need to release the ip.
655 * We make sure we don't trigger this again.
657 conn->release_ip_handler = NULL;
658 conn->release_ip_priv = NULL;
660 TALLOC_FREE(buf);
661 return NT_STATUS_OK;
664 SMB_ASSERT(conn->msg_ctx != NULL);
666 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
667 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
668 DEBUG(0,("Got cluster reconfigure message\n"));
670 * when the cluster is reconfigured or someone of the
671 * family has passed away (SAMBA_NOTIFY), we need to
672 * clean the brl database
674 messaging_send(conn->msg_ctx,
675 messaging_server_id(conn->msg_ctx),
676 MSG_SMB_BRL_VALIDATE, &data_blob_null);
678 messaging_send(conn->msg_ctx,
679 messaging_server_id(conn->msg_ctx),
680 MSG_DBWRAP_G_LOCK_RETRY,
681 &data_blob_null);
683 TALLOC_FREE(buf);
684 return NT_STATUS_OK;
687 /* only messages to our pid or the broadcast are valid here */
688 if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
689 DEBUG(0,("Got unexpected message with srvid=%llu\n",
690 (unsigned long long)msg->srvid));
691 TALLOC_FREE(buf);
692 return NT_STATUS_OK;
695 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
696 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
697 TALLOC_FREE(buf);
698 return NT_STATUS_NO_MEMORY;
701 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
703 TALLOC_FREE(msg_rec);
704 TALLOC_FREE(buf);
705 return NT_STATUS_OK;
709 * The ctdbd socket is readable asynchronuously
712 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
713 struct tevent_fd *event,
714 uint16 flags,
715 void *private_data)
717 struct ctdbd_connection *conn = talloc_get_type_abort(
718 private_data, struct ctdbd_connection);
720 NTSTATUS status;
722 status = ctdb_packet_fd_read(conn->pkt);
724 if (!NT_STATUS_IS_OK(status)) {
725 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
726 cluster_fatal("ctdbd died\n");
729 while (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
730 ctdb_handle_message, conn, &status)) {
731 if (!NT_STATUS_IS_OK(status)) {
732 DEBUG(10, ("could not handle incoming message: %s\n",
733 nt_errstr(status)));
739 * Prepare a ctdbd connection to receive messages
742 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
743 struct messaging_context *msg_ctx)
745 SMB_ASSERT(conn->msg_ctx == NULL);
746 SMB_ASSERT(conn->fde == NULL);
748 if (!(conn->fde = tevent_add_fd(msg_ctx->event_ctx, conn,
749 ctdb_packet_get_fd(conn->pkt),
750 TEVENT_FD_READ,
751 ctdbd_socket_handler,
752 conn))) {
753 DEBUG(0, ("event_add_fd failed\n"));
754 return NT_STATUS_NO_MEMORY;
757 conn->msg_ctx = msg_ctx;
759 return NT_STATUS_OK;
763 * Send a messaging message across a ctdbd
766 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
767 uint32_t dst_vnn, uint64_t dst_srvid,
768 struct messaging_rec *msg)
770 DATA_BLOB blob;
771 NTSTATUS status;
772 enum ndr_err_code ndr_err;
774 ndr_err = ndr_push_struct_blob(
775 &blob, talloc_tos(), msg,
776 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
778 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
779 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
780 ndr_errstr(ndr_err)));
781 return ndr_map_error2ntstatus(ndr_err);
784 status = ctdbd_messaging_send_blob(conn, dst_vnn, dst_srvid,
785 blob.data, blob.length);
786 TALLOC_FREE(blob.data);
787 return status;
790 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
791 uint32_t dst_vnn, uint64_t dst_srvid,
792 const uint8_t *buf, size_t buflen)
794 struct ctdb_req_message r;
795 NTSTATUS status;
797 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
798 r.hdr.ctdb_magic = CTDB_MAGIC;
799 r.hdr.ctdb_version = CTDB_VERSION;
800 r.hdr.generation = 1;
801 r.hdr.operation = CTDB_REQ_MESSAGE;
802 r.hdr.destnode = dst_vnn;
803 r.hdr.srcnode = conn->our_vnn;
804 r.hdr.reqid = 0;
805 r.srvid = dst_srvid;
806 r.datalen = buflen;
808 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
809 ctdb_packet_dump(&r.hdr);
811 status = ctdb_packet_send(
812 conn->pkt, 2,
813 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
814 data_blob_const(buf, buflen));
816 if (!NT_STATUS_IS_OK(status)) {
817 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
818 return status;
821 status = ctdb_packet_flush(conn->pkt);
822 if (!NT_STATUS_IS_OK(status)) {
823 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
824 cluster_fatal("cluster dispatch daemon msg write error\n");
826 return NT_STATUS_OK;
830 * send/recv a generic ctdb control message
832 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
833 uint32_t vnn, uint32_t opcode,
834 uint64_t srvid, uint32_t flags,
835 TDB_DATA data,
836 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
837 int *cstatus)
839 struct ctdb_req_control req;
840 struct ctdb_reply_control *reply = NULL;
841 struct ctdbd_connection *new_conn = NULL;
842 NTSTATUS status;
844 if (conn == NULL) {
845 status = ctdbd_init_connection(NULL, &new_conn);
847 if (!NT_STATUS_IS_OK(status)) {
848 DEBUG(10, ("Could not init temp connection: %s\n",
849 nt_errstr(status)));
850 goto fail;
853 conn = new_conn;
856 ZERO_STRUCT(req);
857 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
858 req.hdr.ctdb_magic = CTDB_MAGIC;
859 req.hdr.ctdb_version = CTDB_VERSION;
860 req.hdr.operation = CTDB_REQ_CONTROL;
861 req.hdr.reqid = ctdbd_next_reqid(conn);
862 req.hdr.destnode = vnn;
863 req.opcode = opcode;
864 req.srvid = srvid;
865 req.datalen = data.dsize;
866 req.flags = flags;
868 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
869 ctdb_packet_dump(&req.hdr);
871 status = ctdb_packet_send(
872 conn->pkt, 2,
873 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
874 data_blob_const(data.dptr, data.dsize));
876 if (!NT_STATUS_IS_OK(status)) {
877 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
878 goto fail;
881 status = ctdb_packet_flush(conn->pkt);
883 if (!NT_STATUS_IS_OK(status)) {
884 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
885 cluster_fatal("cluster dispatch daemon control write error\n");
888 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
889 TALLOC_FREE(new_conn);
890 if (cstatus) {
891 *cstatus = 0;
893 return NT_STATUS_OK;
896 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
898 if (!NT_STATUS_IS_OK(status)) {
899 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
900 goto fail;
903 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
904 DEBUG(0, ("received invalid reply\n"));
905 goto fail;
908 if (outdata) {
909 if (!(outdata->dptr = (uint8 *)talloc_memdup(
910 mem_ctx, reply->data, reply->datalen))) {
911 TALLOC_FREE(reply);
912 return NT_STATUS_NO_MEMORY;
914 outdata->dsize = reply->datalen;
916 if (cstatus) {
917 (*cstatus) = reply->status;
920 status = NT_STATUS_OK;
922 fail:
923 TALLOC_FREE(new_conn);
924 TALLOC_FREE(reply);
925 return status;
929 * see if a remote process exists
931 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
933 struct server_id id;
934 bool result;
936 id.pid = pid;
937 id.vnn = vnn;
939 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
940 DEBUG(10, ("ctdb_processes_exist failed\n"));
941 return false;
943 return result;
946 bool ctdb_processes_exist(struct ctdbd_connection *conn,
947 const struct server_id *pids, int num_pids,
948 bool *results)
950 TALLOC_CTX *frame = talloc_stackframe();
951 int i, num_received;
952 NTSTATUS status;
953 uint32_t *reqids;
954 bool result = false;
956 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
957 if (reqids == NULL) {
958 goto fail;
961 for (i=0; i<num_pids; i++) {
962 struct ctdb_req_control req;
963 pid_t pid;
965 results[i] = false;
966 reqids[i] = ctdbd_next_reqid(conn);
968 ZERO_STRUCT(req);
971 * pids[i].pid is uint64_t, scale down to pid_t which
972 * is the wire protocol towards ctdb.
974 pid = pids[i].pid;
976 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
977 (int)pids[i].vnn, (int)pid,
978 (int)reqids[i]));
980 req.hdr.length = offsetof(struct ctdb_req_control, data);
981 req.hdr.length += sizeof(pid);
982 req.hdr.ctdb_magic = CTDB_MAGIC;
983 req.hdr.ctdb_version = CTDB_VERSION;
984 req.hdr.operation = CTDB_REQ_CONTROL;
985 req.hdr.reqid = reqids[i];
986 req.hdr.destnode = pids[i].vnn;
987 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
988 req.srvid = 0;
989 req.datalen = sizeof(pid);
990 req.flags = 0;
992 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
993 ctdb_packet_dump(&req.hdr);
995 status = ctdb_packet_send(
996 conn->pkt, 2,
997 data_blob_const(
998 &req, offsetof(struct ctdb_req_control, data)),
999 data_blob_const(&pid, sizeof(pid)));
1000 if (!NT_STATUS_IS_OK(status)) {
1001 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1002 nt_errstr(status)));
1003 goto fail;
1007 status = ctdb_packet_flush(conn->pkt);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1010 nt_errstr(status)));
1011 goto fail;
1014 num_received = 0;
1016 while (num_received < num_pids) {
1017 struct ctdb_reply_control *reply = NULL;
1018 uint32_t reqid;
1020 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1021 if (!NT_STATUS_IS_OK(status)) {
1022 DEBUG(10, ("ctdb_read_req failed: %s\n",
1023 nt_errstr(status)));
1024 goto fail;
1027 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1028 DEBUG(10, ("Received invalid reply\n"));
1029 goto fail;
1032 reqid = reply->hdr.reqid;
1034 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1036 for (i=0; i<num_pids; i++) {
1037 if (reqid == reqids[i]) {
1038 break;
1041 if (i == num_pids) {
1042 DEBUG(10, ("Received unknown record number %u\n",
1043 (unsigned)reqid));
1044 goto fail;
1046 results[i] = ((reply->status) == 0);
1047 TALLOC_FREE(reply);
1048 num_received += 1;
1051 result = true;
1052 fail:
1053 TALLOC_FREE(frame);
1054 return result;
1057 struct ctdb_vnn_list {
1058 uint32_t vnn;
1059 uint32_t reqid;
1060 unsigned num_srvids;
1061 unsigned num_filled;
1062 uint64_t *srvids;
1063 unsigned *pid_indexes;
1067 * Get a list of all vnns mentioned in a list of
1068 * server_ids. vnn_indexes tells where in the vnns array we have to
1069 * place the pids.
1071 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1072 const struct server_id *pids, unsigned num_pids,
1073 struct ctdb_vnn_list **pvnns,
1074 unsigned *pnum_vnns)
1076 struct ctdb_vnn_list *vnns = NULL;
1077 unsigned *vnn_indexes = NULL;
1078 unsigned i, num_vnns = 0;
1080 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1081 if (vnn_indexes == NULL) {
1082 DEBUG(1, ("talloc_array failed\n"));
1083 goto fail;
1086 for (i=0; i<num_pids; i++) {
1087 unsigned j;
1088 uint32_t vnn = pids[i].vnn;
1090 for (j=0; j<num_vnns; j++) {
1091 if (vnn == vnns[j].vnn) {
1092 break;
1095 vnn_indexes[i] = j;
1097 if (j < num_vnns) {
1099 * Already in the array
1101 vnns[j].num_srvids += 1;
1102 continue;
1104 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1105 num_vnns+1);
1106 if (vnns == NULL) {
1107 DEBUG(1, ("talloc_realloc failed\n"));
1108 goto fail;
1110 vnns[num_vnns].vnn = vnn;
1111 vnns[num_vnns].num_srvids = 1;
1112 vnns[num_vnns].num_filled = 0;
1113 num_vnns += 1;
1115 for (i=0; i<num_vnns; i++) {
1116 struct ctdb_vnn_list *vnn = &vnns[i];
1118 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1119 if (vnn->srvids == NULL) {
1120 DEBUG(1, ("talloc_array failed\n"));
1121 goto fail;
1123 vnn->pid_indexes = talloc_array(vnns, unsigned,
1124 vnn->num_srvids);
1125 if (vnn->pid_indexes == NULL) {
1126 DEBUG(1, ("talloc_array failed\n"));
1127 goto fail;
1130 for (i=0; i<num_pids; i++) {
1131 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1132 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1133 vnn->pid_indexes[vnn->num_filled] = i;
1134 vnn->num_filled += 1;
1137 TALLOC_FREE(vnn_indexes);
1138 *pvnns = vnns;
1139 *pnum_vnns = num_vnns;
1140 return true;
1141 fail:
1142 TALLOC_FREE(vnns);
1143 TALLOC_FREE(vnn_indexes);
1144 return false;
1147 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1149 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1150 const struct server_id *pids, unsigned num_pids,
1151 bool *results)
1153 unsigned i, num_received;
1154 NTSTATUS status;
1155 struct ctdb_vnn_list *vnns = NULL;
1156 unsigned num_vnns;
1157 bool result = false;
1159 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1160 &vnns, &num_vnns)) {
1161 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1162 goto fail;
1165 for (i=0; i<num_vnns; i++) {
1166 struct ctdb_vnn_list *vnn = &vnns[i];
1167 struct ctdb_req_control req;
1169 vnn->reqid = ctdbd_next_reqid(conn);
1171 ZERO_STRUCT(req);
1173 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1174 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1176 req.hdr.length = offsetof(struct ctdb_req_control, data);
1177 req.hdr.ctdb_magic = CTDB_MAGIC;
1178 req.hdr.ctdb_version = CTDB_VERSION;
1179 req.hdr.operation = CTDB_REQ_CONTROL;
1180 req.hdr.reqid = vnn->reqid;
1181 req.hdr.destnode = vnn->vnn;
1182 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1183 req.srvid = 0;
1184 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1185 req.hdr.length += req.datalen;
1186 req.flags = 0;
1188 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1189 ctdb_packet_dump(&req.hdr);
1191 status = ctdb_packet_send(
1192 conn->pkt, 2,
1193 data_blob_const(
1194 &req, offsetof(struct ctdb_req_control,
1195 data)),
1196 data_blob_const(vnn->srvids, req.datalen));
1197 if (!NT_STATUS_IS_OK(status)) {
1198 DEBUG(1, ("ctdb_packet_send failed: %s\n",
1199 nt_errstr(status)));
1200 goto fail;
1204 status = ctdb_packet_flush(conn->pkt);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 DEBUG(1, ("ctdb_packet_flush failed: %s\n",
1207 nt_errstr(status)));
1208 goto fail;
1211 num_received = 0;
1213 while (num_received < num_vnns) {
1214 struct ctdb_reply_control *reply = NULL;
1215 struct ctdb_vnn_list *vnn;
1216 uint32_t reqid;
1217 uint8_t *reply_data;
1219 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 DEBUG(1, ("ctdb_read_req failed: %s\n",
1222 nt_errstr(status)));
1223 goto fail;
1226 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1227 DEBUG(1, ("Received invalid reply %u\n",
1228 (unsigned)reply->hdr.operation));
1229 goto fail;
1232 reqid = reply->hdr.reqid;
1234 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1236 for (i=0; i<num_vnns; i++) {
1237 if (reqid == vnns[i].reqid) {
1238 break;
1241 if (i == num_vnns) {
1242 DEBUG(1, ("Received unknown reqid number %u\n",
1243 (unsigned)reqid));
1244 goto fail;
1247 DEBUG(10, ("Found index %u\n", i));
1249 vnn = &vnns[i];
1251 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1252 (unsigned)vnn->vnn, vnn->num_srvids,
1253 (unsigned)reply->datalen));
1255 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1257 * Got a real reply
1259 reply_data = reply->data;
1260 } else {
1262 * Got an error reply
1264 DEBUG(5, ("Received short reply len %d, status %u, "
1265 "errorlen %u\n",
1266 (unsigned)reply->datalen,
1267 (unsigned)reply->status,
1268 (unsigned)reply->errorlen));
1269 dump_data(5, reply->data, reply->errorlen);
1272 * This will trigger everything set to false
1274 reply_data = NULL;
1277 for (i=0; i<vnn->num_srvids; i++) {
1278 int idx = vnn->pid_indexes[i];
1280 if (pids[i].unique_id ==
1281 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1282 results[idx] = true;
1283 continue;
1285 results[idx] =
1286 (reply_data != NULL) &&
1287 ((reply_data[i/8] & (1<<(i%8))) != 0);
1290 TALLOC_FREE(reply);
1291 num_received += 1;
1294 result = true;
1295 fail:
1296 TALLOC_FREE(vnns);
1297 return result;
1300 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1303 * Get a db path
1305 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1306 TALLOC_CTX *mem_ctx, uint32_t db_id)
1308 NTSTATUS status;
1309 TDB_DATA data;
1310 int32_t cstatus;
1312 data.dptr = (uint8_t*)&db_id;
1313 data.dsize = sizeof(db_id);
1315 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1316 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1317 mem_ctx, &data, &cstatus);
1318 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1319 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1320 return NULL;
1323 return (char *)data.dptr;
1327 * attach to a ctdb database
1329 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1330 const char *name, uint32_t *db_id, int tdb_flags)
1332 NTSTATUS status;
1333 TDB_DATA data;
1334 int32_t cstatus;
1335 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1337 data = string_term_tdb_data(name);
1339 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1340 persistent
1341 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1342 : CTDB_CONTROL_DB_ATTACH,
1343 tdb_flags, 0, data, NULL, &data, &cstatus);
1344 if (!NT_STATUS_IS_OK(status)) {
1345 DEBUG(0, (__location__ " ctdb_control for db_attach "
1346 "failed: %s\n", nt_errstr(status)));
1347 return status;
1350 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1351 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1352 return NT_STATUS_INTERNAL_ERROR;
1355 *db_id = *(uint32_t *)data.dptr;
1356 talloc_free(data.dptr);
1358 if (!(tdb_flags & TDB_SEQNUM)) {
1359 return NT_STATUS_OK;
1362 data.dptr = (uint8_t *)db_id;
1363 data.dsize = sizeof(*db_id);
1365 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1366 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1367 NULL, NULL, &cstatus);
1368 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1369 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1370 "failed\n"));
1371 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1372 status;
1375 return NT_STATUS_OK;
1379 * force the migration of a record to this node
1381 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1382 TDB_DATA key)
1384 struct ctdb_req_call req;
1385 struct ctdb_reply_call *reply;
1386 NTSTATUS status;
1388 ZERO_STRUCT(req);
1390 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1391 req.hdr.ctdb_magic = CTDB_MAGIC;
1392 req.hdr.ctdb_version = CTDB_VERSION;
1393 req.hdr.operation = CTDB_REQ_CALL;
1394 req.hdr.reqid = ctdbd_next_reqid(conn);
1395 req.flags = CTDB_IMMEDIATE_MIGRATION;
1396 req.callid = CTDB_NULL_FUNC;
1397 req.db_id = db_id;
1398 req.keylen = key.dsize;
1400 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1401 ctdb_packet_dump(&req.hdr);
1403 status = ctdb_packet_send(
1404 conn->pkt, 2,
1405 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1406 data_blob_const(key.dptr, key.dsize));
1408 if (!NT_STATUS_IS_OK(status)) {
1409 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1410 return status;
1413 status = ctdb_packet_flush(conn->pkt);
1415 if (!NT_STATUS_IS_OK(status)) {
1416 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1417 cluster_fatal("cluster dispatch daemon control write error\n");
1420 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1422 if (!NT_STATUS_IS_OK(status)) {
1423 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1424 goto fail;
1427 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1428 DEBUG(0, ("received invalid reply\n"));
1429 status = NT_STATUS_INTERNAL_ERROR;
1430 goto fail;
1433 status = NT_STATUS_OK;
1434 fail:
1436 TALLOC_FREE(reply);
1437 return status;
1441 * Fetch a record and parse it
1443 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1444 TDB_DATA key, bool local_copy,
1445 void (*parser)(TDB_DATA key, TDB_DATA data,
1446 void *private_data),
1447 void *private_data)
1449 struct ctdb_req_call req;
1450 struct ctdb_reply_call *reply;
1451 NTSTATUS status;
1452 uint32_t flags;
1454 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1455 flags = local_copy ? CTDB_WANT_READONLY : 0;
1456 #else
1457 flags = 0;
1458 #endif
1460 ZERO_STRUCT(req);
1462 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1463 req.hdr.ctdb_magic = CTDB_MAGIC;
1464 req.hdr.ctdb_version = CTDB_VERSION;
1465 req.hdr.operation = CTDB_REQ_CALL;
1466 req.hdr.reqid = ctdbd_next_reqid(conn);
1467 req.flags = flags;
1468 req.callid = CTDB_FETCH_FUNC;
1469 req.db_id = db_id;
1470 req.keylen = key.dsize;
1472 status = ctdb_packet_send(
1473 conn->pkt, 2,
1474 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1475 data_blob_const(key.dptr, key.dsize));
1477 if (!NT_STATUS_IS_OK(status)) {
1478 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1479 return status;
1482 status = ctdb_packet_flush(conn->pkt);
1484 if (!NT_STATUS_IS_OK(status)) {
1485 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1486 cluster_fatal("cluster dispatch daemon control write error\n");
1489 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1491 if (!NT_STATUS_IS_OK(status)) {
1492 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1493 goto fail;
1496 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1497 DEBUG(0, ("received invalid reply\n"));
1498 status = NT_STATUS_INTERNAL_ERROR;
1499 goto fail;
1502 if (reply->datalen == 0) {
1504 * Treat an empty record as non-existing
1506 status = NT_STATUS_NOT_FOUND;
1507 goto fail;
1510 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1511 private_data);
1513 status = NT_STATUS_OK;
1514 fail:
1515 TALLOC_FREE(reply);
1516 return status;
1519 struct ctdbd_traverse_state {
1520 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1521 void *private_data;
1525 * Handle a traverse record coming in on the ctdbd connection
1528 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1529 void *private_data)
1531 struct ctdbd_traverse_state *state =
1532 (struct ctdbd_traverse_state *)private_data;
1534 struct ctdb_req_message *m;
1535 struct ctdb_rec_data *d;
1536 TDB_DATA key, data;
1538 m = (struct ctdb_req_message *)buf;
1540 if (length < sizeof(*m) || m->hdr.length != length) {
1541 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1542 TALLOC_FREE(buf);
1543 return NT_STATUS_UNEXPECTED_IO_ERROR;
1546 d = (struct ctdb_rec_data *)&m->data[0];
1547 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1548 DEBUG(0, ("Got invalid traverse data of length %d\n",
1549 (int)m->datalen));
1550 TALLOC_FREE(buf);
1551 return NT_STATUS_UNEXPECTED_IO_ERROR;
1554 key.dsize = d->keylen;
1555 key.dptr = &d->data[0];
1556 data.dsize = d->datalen;
1557 data.dptr = &d->data[d->keylen];
1559 if (key.dsize == 0 && data.dsize == 0) {
1560 /* end of traverse */
1561 return NT_STATUS_END_OF_FILE;
1564 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1565 DEBUG(0, ("Got invalid ltdb header length %d\n",
1566 (int)data.dsize));
1567 TALLOC_FREE(buf);
1568 return NT_STATUS_UNEXPECTED_IO_ERROR;
1570 data.dsize -= sizeof(struct ctdb_ltdb_header);
1571 data.dptr += sizeof(struct ctdb_ltdb_header);
1573 if (state->fn) {
1574 state->fn(key, data, state->private_data);
1577 TALLOC_FREE(buf);
1578 return NT_STATUS_OK;
1582 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1583 connection to ctdbd to avoid the hairy recursive and async problems with
1584 everything in-line.
1587 NTSTATUS ctdbd_traverse(uint32_t db_id,
1588 void (*fn)(TDB_DATA key, TDB_DATA data,
1589 void *private_data),
1590 void *private_data)
1592 struct ctdbd_connection *conn;
1593 NTSTATUS status;
1595 TDB_DATA data;
1596 struct ctdb_traverse_start t;
1597 int cstatus;
1598 struct ctdbd_traverse_state state;
1600 become_root();
1601 status = ctdbd_init_connection(NULL, &conn);
1602 unbecome_root();
1603 if (!NT_STATUS_IS_OK(status)) {
1604 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1605 nt_errstr(status)));
1606 return status;
1609 t.db_id = db_id;
1610 t.srvid = conn->rand_srvid;
1611 t.reqid = ctdbd_next_reqid(conn);
1613 data.dptr = (uint8_t *)&t;
1614 data.dsize = sizeof(t);
1616 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1617 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1618 data, NULL, NULL, &cstatus);
1620 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1622 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1623 cstatus));
1625 if (NT_STATUS_IS_OK(status)) {
1627 * We need a mapping here
1629 status = NT_STATUS_UNSUCCESSFUL;
1631 goto done;
1634 state.fn = fn;
1635 state.private_data = private_data;
1637 while (True) {
1639 status = NT_STATUS_OK;
1641 if (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
1642 ctdb_traverse_handler, &state, &status)) {
1644 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1645 status = NT_STATUS_OK;
1646 break;
1650 * There might be more in the queue
1652 continue;
1655 if (!NT_STATUS_IS_OK(status)) {
1656 break;
1659 status = ctdb_packet_fd_read_sync(conn->pkt);
1661 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1663 * There might be more in the queue
1665 continue;
1668 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1669 status = NT_STATUS_OK;
1670 break;
1673 if (!NT_STATUS_IS_OK(status)) {
1674 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1675 cluster_fatal("ctdbd died\n");
1679 done:
1680 TALLOC_FREE(conn);
1681 return status;
1685 This is used to canonicalize a ctdb_sock_addr structure.
1687 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1688 struct sockaddr_storage *out)
1690 memcpy(out, in, sizeof (*out));
1692 #ifdef HAVE_IPV6
1693 if (in->ss_family == AF_INET6) {
1694 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1695 const struct sockaddr_in6 *in6 =
1696 (const struct sockaddr_in6 *)in;
1697 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1698 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1699 memset(out, 0, sizeof(*out));
1700 #ifdef HAVE_SOCK_SIN_LEN
1701 out4->sin_len = sizeof(*out);
1702 #endif
1703 out4->sin_family = AF_INET;
1704 out4->sin_port = in6->sin6_port;
1705 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1708 #endif
1712 * Register us as a server for a particular tcp connection
1715 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1716 const struct sockaddr_storage *_server,
1717 const struct sockaddr_storage *_client,
1718 bool (*release_ip_handler)(const char *ip_addr,
1719 void *private_data),
1720 void *private_data)
1723 * we still use ctdb_control_tcp for ipv4
1724 * because we want to work against older ctdb
1725 * versions at runtime
1727 struct ctdb_control_tcp p4;
1728 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1729 struct ctdb_control_tcp_addr p;
1730 #endif
1731 TDB_DATA data;
1732 NTSTATUS status;
1733 struct sockaddr_storage client;
1734 struct sockaddr_storage server;
1737 * Only one connection so far
1739 SMB_ASSERT(conn->release_ip_handler == NULL);
1741 smbd_ctdb_canonicalize_ip(_client, &client);
1742 smbd_ctdb_canonicalize_ip(_server, &server);
1744 switch (client.ss_family) {
1745 case AF_INET:
1746 memcpy(&p4.dest, &server, sizeof(p4.dest));
1747 memcpy(&p4.src, &client, sizeof(p4.src));
1748 data.dptr = (uint8_t *)&p4;
1749 data.dsize = sizeof(p4);
1750 break;
1751 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1752 case AF_INET6:
1753 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1754 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1755 data.dptr = (uint8_t *)&p;
1756 data.dsize = sizeof(p);
1757 break;
1758 #endif
1759 default:
1760 return NT_STATUS_INTERNAL_ERROR;
1763 conn->release_ip_handler = release_ip_handler;
1764 conn->release_ip_priv = private_data;
1767 * We want to be told about IP releases
1770 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1771 if (!NT_STATUS_IS_OK(status)) {
1772 return status;
1776 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1777 * can send an extra ack to trigger a reset for our client, so it
1778 * immediately reconnects
1780 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1781 CTDB_CONTROL_TCP_CLIENT, 0,
1782 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1786 * We want to handle reconfigure events
1788 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1790 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1794 call a control on the local node
1796 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1797 uint64_t srvid, uint32_t flags, TDB_DATA data,
1798 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1799 int *cstatus)
1801 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1804 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1806 struct ctdb_client_notify_register reg_data;
1807 size_t struct_len;
1808 NTSTATUS status;
1809 int cstatus;
1811 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1812 reg_data.len = 1;
1813 reg_data.notify_data[0] = 0;
1815 struct_len = offsetof(struct ctdb_client_notify_register,
1816 notify_data) + reg_data.len;
1818 status = ctdbd_control_local(
1819 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1820 make_tdb_data((uint8_t *)&reg_data, struct_len),
1821 NULL, NULL, &cstatus);
1822 if (!NT_STATUS_IS_OK(status)) {
1823 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1824 nt_errstr(status)));
1826 return status;
1829 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1831 struct ctdb_client_notify_deregister dereg_data;
1832 NTSTATUS status;
1833 int cstatus;
1835 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1837 status = ctdbd_control_local(
1838 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1839 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1840 NULL, NULL, &cstatus);
1841 if (!NT_STATUS_IS_OK(status)) {
1842 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1843 nt_errstr(status)));
1845 return status;
1848 #else
1850 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
1851 uint32_t dst_vnn, uint64_t dst_srvid,
1852 const uint8_t *buf, size_t buflen)
1854 return NT_STATUS_NOT_IMPLEMENTED;
1857 #endif