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/>.
24 #include "ctdbd_conn.h"
26 #ifdef CLUSTER_SUPPORT
28 #include "ctdb_packet.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
41 #ifdef typesafe_cb_preargs
42 #undef typesafe_cb_preargs
45 #ifdef typesafe_cb_postargs
46 #undef typesafe_cb_postargs
49 /* paths to these include files come from --with-ctdb= in configure */
52 #include "ctdb_private.h"
54 struct ctdbd_connection
{
55 struct messaging_context
*msg_ctx
;
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
)
69 if (conn
->reqid
== 0) {
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
,
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
97 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
99 if (DEBUGLEVEL
< 11) {
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
)
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
)
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 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
134 *vnn
= (uint32_t)cstatus
;
139 * Are we active (i.e. not banned or stopped?)
141 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
146 struct ctdb_node_map
*m
;
147 uint32_t failure_flags
;
151 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
152 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
153 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
154 if (!NT_STATUS_IS_OK(status
)) {
155 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
158 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
159 DEBUG(2, ("Received invalid ctdb data\n"));
163 m
= (struct ctdb_node_map
*)outdata
.dptr
;
165 for (i
=0; i
<m
->num
; i
++) {
166 if (vnn
== m
->nodes
[i
].pnn
) {
172 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
177 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
178 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
180 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
181 DEBUG(2, ("Node has status %x, not active\n",
182 (int)m
->nodes
[i
].flags
));
188 TALLOC_FREE(outdata
.dptr
);
192 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
194 return conn
->our_vnn
;
198 * Get us a ctdb connection
201 static NTSTATUS
ctdbd_connect(TALLOC_CTX
*mem_ctx
,
202 struct ctdb_packet_context
**presult
)
204 struct ctdb_packet_context
*result
;
205 const char *sockname
= lp_ctdbd_socket();
206 struct sockaddr_un addr
= { 0, };
210 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
212 DEBUG(3, ("Could not create socket: %s\n", strerror(errno
)));
213 return map_nt_error_from_unix(errno
);
216 addr
.sun_family
= AF_UNIX
;
217 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", sockname
);
219 salen
= sizeof(struct sockaddr_un
);
220 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
221 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
224 return map_nt_error_from_unix(errno
);
227 if (!(result
= ctdb_packet_init(mem_ctx
, fd
))) {
229 return NT_STATUS_NO_MEMORY
;
237 * Do we have a complete ctdb packet in the queue?
240 static bool ctdb_req_complete(const uint8_t *buf
, size_t available
,
246 if (available
< sizeof(msglen
)) {
250 msglen
= *((const uint32_t *)buf
);
252 DEBUG(11, ("msglen = %d\n", msglen
));
254 if (msglen
< sizeof(struct ctdb_req_header
)) {
255 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
256 "the req_header\n", (int)msglen
,
257 (int)sizeof(struct ctdb_req_header
)));
258 cluster_fatal("ctdbd protocol error\n");
261 if (available
< msglen
) {
270 * State necessary to defer an incoming message while we are waiting for a
274 struct deferred_msg_state
{
275 struct messaging_context
*msg_ctx
;
276 struct messaging_rec
*rec
;
280 * Timed event handler for the deferred message
283 static void deferred_message_dispatch(struct tevent_context
*event_ctx
,
284 struct tevent_timer
*te
,
288 struct deferred_msg_state
*state
= talloc_get_type_abort(
289 private_data
, struct deferred_msg_state
);
291 messaging_dispatch_rec(state
->msg_ctx
, state
->rec
);
296 struct req_pull_state
{
302 * Pull a ctdb request out of the incoming ctdb_packet queue
305 static NTSTATUS
ctdb_req_pull(uint8_t *buf
, size_t length
,
308 struct req_pull_state
*state
= (struct req_pull_state
*)private_data
;
310 state
->req
.data
= talloc_move(state
->mem_ctx
, &buf
);
311 state
->req
.length
= length
;
316 * Fetch a messaging_rec from an incoming ctdb style message
319 static struct messaging_rec
*ctdb_pull_messaging_rec(TALLOC_CTX
*mem_ctx
,
320 size_t overall_length
,
321 struct ctdb_req_message
*msg
)
323 struct messaging_rec
*result
;
325 enum ndr_err_code ndr_err
;
327 if ((overall_length
< offsetof(struct ctdb_req_message
, data
))
329 < offsetof(struct ctdb_req_message
, data
) + msg
->datalen
)) {
331 cluster_fatal("got invalid msg length");
334 if (!(result
= talloc(mem_ctx
, struct messaging_rec
))) {
335 DEBUG(0, ("talloc failed\n"));
339 blob
= data_blob_const(msg
->data
, msg
->datalen
);
341 ndr_err
= ndr_pull_struct_blob(
342 &blob
, result
, result
,
343 (ndr_pull_flags_fn_t
)ndr_pull_messaging_rec
);
345 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
346 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
347 ndr_errstr(ndr_err
)));
352 if (DEBUGLEVEL
>= 11) {
353 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
354 NDR_PRINT_DEBUG(messaging_rec
, result
);
360 static NTSTATUS
ctdb_packet_fd_read_sync(struct ctdb_packet_context
*ctx
)
362 int timeout
= lp_ctdb_timeout();
367 return ctdb_packet_fd_read_sync_timeout(ctx
, timeout
);
371 * Read a full ctdbd request. If we have a messaging context, defer incoming
372 * messages that might come in between.
375 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
376 TALLOC_CTX
*mem_ctx
, void *result
)
378 struct ctdb_req_header
*hdr
;
379 struct req_pull_state state
;
384 state
.mem_ctx
= mem_ctx
;
386 while (!ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
387 ctdb_req_pull
, &state
, &status
)) {
391 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
393 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_BUSY
)) {
396 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
401 if (!NT_STATUS_IS_OK(status
)) {
402 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
403 cluster_fatal("ctdbd died\n");
407 if (!NT_STATUS_IS_OK(status
)) {
408 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status
)));
409 cluster_fatal("ctdbd died\n");
412 hdr
= (struct ctdb_req_header
*)state
.req
.data
;
414 DEBUG(11, ("Received ctdb packet\n"));
415 ctdb_packet_dump(hdr
);
417 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
418 struct tevent_timer
*evt
;
419 struct deferred_msg_state
*msg_state
;
420 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
422 if (conn
->msg_ctx
== NULL
) {
423 DEBUG(1, ("Got a message without having a msg ctx, "
424 "dropping msg %llu\n",
425 (long long unsigned)msg
->srvid
));
429 if ((conn
->release_ip_handler
!= NULL
)
430 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
433 /* must be dispatched immediately */
434 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
435 ret
= conn
->release_ip_handler((const char *)msg
->data
,
436 conn
->release_ip_priv
);
439 * We need to release the ip,
440 * so return an error to the upper layers.
442 * We make sure we don't trigger this again.
444 conn
->release_ip_handler
= NULL
;
445 conn
->release_ip_priv
= NULL
;
446 return NT_STATUS_ADDRESS_CLOSED
;
452 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
453 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)) {
455 DEBUG(1, ("ctdb_read_req: Got %s message\n",
456 (msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
457 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
459 messaging_send(conn
->msg_ctx
,
460 messaging_server_id(conn
->msg_ctx
),
461 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
462 messaging_send(conn
->msg_ctx
,
463 messaging_server_id(conn
->msg_ctx
),
464 MSG_DBWRAP_G_LOCK_RETRY
,
470 msg_state
= talloc(NULL
, struct deferred_msg_state
);
471 if (msg_state
== NULL
) {
472 DEBUG(0, ("talloc failed\n"));
477 if (!(msg_state
->rec
= ctdb_pull_messaging_rec(
478 msg_state
, state
.req
.length
, msg
))) {
479 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
480 TALLOC_FREE(msg_state
);
487 msg_state
->msg_ctx
= conn
->msg_ctx
;
490 * We're waiting for a call reply, but an async message has
491 * crossed. Defer dispatching to the toplevel event loop.
493 evt
= tevent_add_timer(conn
->msg_ctx
->event_ctx
,
494 conn
->msg_ctx
->event_ctx
,
496 deferred_message_dispatch
,
499 DEBUG(0, ("event_add_timed failed\n"));
500 TALLOC_FREE(msg_state
);
508 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
509 /* we got the wrong reply */
510 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
511 "been %u\n", hdr
->reqid
, reqid
));
516 *((void **)result
) = talloc_move(mem_ctx
, &hdr
);
522 * Get us a ctdbd connection
525 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
526 struct ctdbd_connection
**pconn
)
528 struct ctdbd_connection
*conn
;
531 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
532 DEBUG(0, ("talloc failed\n"));
533 return NT_STATUS_NO_MEMORY
;
536 status
= ctdbd_connect(conn
, &conn
->pkt
);
538 if (!NT_STATUS_IS_OK(status
)) {
539 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status
)));
543 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
545 if (!NT_STATUS_IS_OK(status
)) {
546 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
550 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
551 DEBUG(2, ("Node is not working, can not connect\n"));
552 status
= NT_STATUS_INTERNAL_DB_ERROR
;
556 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
557 sizeof(conn
->rand_srvid
));
559 status
= register_with_ctdbd(conn
, conn
->rand_srvid
);
561 if (!NT_STATUS_IS_OK(status
)) {
562 DEBUG(5, ("Could not register random srvid: %s\n",
576 * Get us a ctdbd connection and register us as a process
579 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
580 struct ctdbd_connection
**pconn
)
582 struct ctdbd_connection
*conn
;
585 status
= ctdbd_init_connection(mem_ctx
, &conn
);
587 if (!NT_STATUS_IS_OK(status
)) {
591 status
= register_with_ctdbd(conn
, (uint64_t)getpid());
592 if (!NT_STATUS_IS_OK(status
)) {
596 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
);
597 if (!NT_STATUS_IS_OK(status
)) {
601 status
= register_with_ctdbd(conn
, CTDB_SRVID_SAMBA_NOTIFY
);
602 if (!NT_STATUS_IS_OK(status
)) {
614 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
616 return conn
->msg_ctx
;
619 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
621 return ctdb_packet_get_fd(conn
->pkt
);
625 * Packet handler to receive and handle a ctdb message
627 static NTSTATUS
ctdb_handle_message(uint8_t *buf
, size_t length
,
630 struct ctdbd_connection
*conn
= talloc_get_type_abort(
631 private_data
, struct ctdbd_connection
);
632 struct ctdb_req_message
*msg
;
633 struct messaging_rec
*msg_rec
;
635 msg
= (struct ctdb_req_message
*)buf
;
637 if (msg
->hdr
.operation
!= CTDB_REQ_MESSAGE
) {
638 DEBUG(0, ("Received async msg of type %u, discarding\n",
639 msg
->hdr
.operation
));
641 return NT_STATUS_INVALID_PARAMETER
;
644 if ((conn
->release_ip_handler
!= NULL
)
645 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
648 /* must be dispatched immediately */
649 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
650 ret
= conn
->release_ip_handler((const char *)msg
->data
,
651 conn
->release_ip_priv
);
654 * We need to release the ip.
656 * We make sure we don't trigger this again.
658 conn
->release_ip_handler
= NULL
;
659 conn
->release_ip_priv
= NULL
;
665 SMB_ASSERT(conn
->msg_ctx
!= NULL
);
667 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
668 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)){
669 DEBUG(0,("Got cluster reconfigure message\n"));
671 * when the cluster is reconfigured or someone of the
672 * family has passed away (SAMBA_NOTIFY), we need to
673 * clean the brl database
675 messaging_send(conn
->msg_ctx
,
676 messaging_server_id(conn
->msg_ctx
),
677 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
679 messaging_send(conn
->msg_ctx
,
680 messaging_server_id(conn
->msg_ctx
),
681 MSG_DBWRAP_G_LOCK_RETRY
,
688 /* only messages to our pid or the broadcast are valid here */
689 if (msg
->srvid
!= getpid() && msg
->srvid
!= MSG_SRVID_SAMBA
) {
690 DEBUG(0,("Got unexpected message with srvid=%llu\n",
691 (unsigned long long)msg
->srvid
));
696 if (!(msg_rec
= ctdb_pull_messaging_rec(NULL
, length
, msg
))) {
697 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
699 return NT_STATUS_NO_MEMORY
;
702 messaging_dispatch_rec(conn
->msg_ctx
, msg_rec
);
704 TALLOC_FREE(msg_rec
);
710 * The ctdbd socket is readable asynchronuously
713 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
714 struct tevent_fd
*event
,
718 struct ctdbd_connection
*conn
= talloc_get_type_abort(
719 private_data
, struct ctdbd_connection
);
723 status
= ctdb_packet_fd_read(conn
->pkt
);
725 if (!NT_STATUS_IS_OK(status
)) {
726 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
727 cluster_fatal("ctdbd died\n");
730 while (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
731 ctdb_handle_message
, conn
, &status
)) {
732 if (!NT_STATUS_IS_OK(status
)) {
733 DEBUG(10, ("could not handle incoming message: %s\n",
740 * Prepare a ctdbd connection to receive messages
743 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
744 struct messaging_context
*msg_ctx
)
746 SMB_ASSERT(conn
->msg_ctx
== NULL
);
747 SMB_ASSERT(conn
->fde
== NULL
);
749 if (!(conn
->fde
= tevent_add_fd(msg_ctx
->event_ctx
, conn
,
750 ctdb_packet_get_fd(conn
->pkt
),
752 ctdbd_socket_handler
,
754 DEBUG(0, ("event_add_fd failed\n"));
755 return NT_STATUS_NO_MEMORY
;
758 conn
->msg_ctx
= msg_ctx
;
764 * Send a messaging message across a ctdbd
767 NTSTATUS
ctdbd_messaging_send(struct ctdbd_connection
*conn
,
768 uint32_t dst_vnn
, uint64_t dst_srvid
,
769 struct messaging_rec
*msg
)
773 enum ndr_err_code ndr_err
;
775 ndr_err
= ndr_push_struct_blob(
776 &blob
, talloc_tos(), msg
,
777 (ndr_push_flags_fn_t
)ndr_push_messaging_rec
);
779 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
780 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
781 ndr_errstr(ndr_err
)));
782 return ndr_map_error2ntstatus(ndr_err
);
785 status
= ctdbd_messaging_send_blob(conn
, dst_vnn
, dst_srvid
,
786 blob
.data
, blob
.length
);
787 TALLOC_FREE(blob
.data
);
791 NTSTATUS
ctdbd_messaging_send_blob(struct ctdbd_connection
*conn
,
792 uint32_t dst_vnn
, uint64_t dst_srvid
,
793 const uint8_t *buf
, size_t buflen
)
795 struct ctdb_req_message r
;
798 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
799 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
800 r
.hdr
.ctdb_version
= CTDB_VERSION
;
801 r
.hdr
.generation
= 1;
802 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
803 r
.hdr
.destnode
= dst_vnn
;
804 r
.hdr
.srcnode
= conn
->our_vnn
;
809 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
810 ctdb_packet_dump(&r
.hdr
);
812 status
= ctdb_packet_send(
814 data_blob_const(&r
, offsetof(struct ctdb_req_message
, data
)),
815 data_blob_const(buf
, buflen
));
817 if (!NT_STATUS_IS_OK(status
)) {
818 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
822 status
= ctdb_packet_flush(conn
->pkt
);
823 if (!NT_STATUS_IS_OK(status
)) {
824 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
825 cluster_fatal("cluster dispatch daemon msg write error\n");
831 * send/recv a generic ctdb control message
833 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
834 uint32_t vnn
, uint32_t opcode
,
835 uint64_t srvid
, uint32_t flags
,
837 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
840 struct ctdb_req_control req
;
841 struct ctdb_reply_control
*reply
= NULL
;
842 struct ctdbd_connection
*new_conn
= NULL
;
846 status
= ctdbd_init_connection(NULL
, &new_conn
);
848 if (!NT_STATUS_IS_OK(status
)) {
849 DEBUG(10, ("Could not init temp connection: %s\n",
858 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
859 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
860 req
.hdr
.ctdb_version
= CTDB_VERSION
;
861 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
862 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
863 req
.hdr
.destnode
= vnn
;
866 req
.datalen
= data
.dsize
;
869 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
870 ctdb_packet_dump(&req
.hdr
);
872 status
= ctdb_packet_send(
874 data_blob_const(&req
, offsetof(struct ctdb_req_control
, data
)),
875 data_blob_const(data
.dptr
, data
.dsize
));
877 if (!NT_STATUS_IS_OK(status
)) {
878 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
882 status
= ctdb_packet_flush(conn
->pkt
);
884 if (!NT_STATUS_IS_OK(status
)) {
885 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
886 cluster_fatal("cluster dispatch daemon control write error\n");
889 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
890 TALLOC_FREE(new_conn
);
897 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
899 if (!NT_STATUS_IS_OK(status
)) {
900 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
904 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
905 DEBUG(0, ("received invalid reply\n"));
910 if (!(outdata
->dptr
= (uint8
*)talloc_memdup(
911 mem_ctx
, reply
->data
, reply
->datalen
))) {
913 return NT_STATUS_NO_MEMORY
;
915 outdata
->dsize
= reply
->datalen
;
918 (*cstatus
) = reply
->status
;
921 status
= NT_STATUS_OK
;
924 TALLOC_FREE(new_conn
);
930 * see if a remote process exists
932 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
940 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
941 DEBUG(10, ("ctdb_processes_exist failed\n"));
947 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
948 const struct server_id
*pids
, int num_pids
,
951 TALLOC_CTX
*frame
= talloc_stackframe();
957 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
958 if (reqids
== NULL
) {
962 for (i
=0; i
<num_pids
; i
++) {
963 struct ctdb_req_control req
;
967 reqids
[i
] = ctdbd_next_reqid(conn
);
972 * pids[i].pid is uint64_t, scale down to pid_t which
973 * is the wire protocol towards ctdb.
977 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
978 (int)pids
[i
].vnn
, (int)pid
,
981 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
982 req
.hdr
.length
+= sizeof(pid
);
983 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
984 req
.hdr
.ctdb_version
= CTDB_VERSION
;
985 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
986 req
.hdr
.reqid
= reqids
[i
];
987 req
.hdr
.destnode
= pids
[i
].vnn
;
988 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
990 req
.datalen
= sizeof(pid
);
993 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
994 ctdb_packet_dump(&req
.hdr
);
996 status
= ctdb_packet_send(
999 &req
, offsetof(struct ctdb_req_control
, data
)),
1000 data_blob_const(&pid
, sizeof(pid
)));
1001 if (!NT_STATUS_IS_OK(status
)) {
1002 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1003 nt_errstr(status
)));
1008 status
= ctdb_packet_flush(conn
->pkt
);
1009 if (!NT_STATUS_IS_OK(status
)) {
1010 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1011 nt_errstr(status
)));
1017 while (num_received
< num_pids
) {
1018 struct ctdb_reply_control
*reply
= NULL
;
1021 status
= ctdb_read_req(conn
, 0, talloc_tos(), (void *)&reply
);
1022 if (!NT_STATUS_IS_OK(status
)) {
1023 DEBUG(10, ("ctdb_read_req failed: %s\n",
1024 nt_errstr(status
)));
1028 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
1029 DEBUG(10, ("Received invalid reply\n"));
1033 reqid
= reply
->hdr
.reqid
;
1035 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1037 for (i
=0; i
<num_pids
; i
++) {
1038 if (reqid
== reqids
[i
]) {
1042 if (i
== num_pids
) {
1043 DEBUG(10, ("Received unknown record number %u\n",
1047 results
[i
] = ((reply
->status
) == 0);
1058 struct ctdb_vnn_list
{
1061 unsigned num_srvids
;
1062 unsigned num_filled
;
1064 unsigned *pid_indexes
;
1068 * Get a list of all vnns mentioned in a list of
1069 * server_ids. vnn_indexes tells where in the vnns array we have to
1072 static bool ctdb_collect_vnns(TALLOC_CTX
*mem_ctx
,
1073 const struct server_id
*pids
, unsigned num_pids
,
1074 struct ctdb_vnn_list
**pvnns
,
1075 unsigned *pnum_vnns
)
1077 struct ctdb_vnn_list
*vnns
= NULL
;
1078 unsigned *vnn_indexes
= NULL
;
1079 unsigned i
, num_vnns
= 0;
1081 vnn_indexes
= talloc_array(mem_ctx
, unsigned, num_pids
);
1082 if (vnn_indexes
== NULL
) {
1083 DEBUG(1, ("talloc_array failed\n"));
1087 for (i
=0; i
<num_pids
; i
++) {
1089 uint32_t vnn
= pids
[i
].vnn
;
1091 for (j
=0; j
<num_vnns
; j
++) {
1092 if (vnn
== vnns
[j
].vnn
) {
1100 * Already in the array
1102 vnns
[j
].num_srvids
+= 1;
1105 vnns
= talloc_realloc(mem_ctx
, vnns
, struct ctdb_vnn_list
,
1108 DEBUG(1, ("talloc_realloc failed\n"));
1111 vnns
[num_vnns
].vnn
= vnn
;
1112 vnns
[num_vnns
].num_srvids
= 1;
1113 vnns
[num_vnns
].num_filled
= 0;
1116 for (i
=0; i
<num_vnns
; i
++) {
1117 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1119 vnn
->srvids
= talloc_array(vnns
, uint64_t, vnn
->num_srvids
);
1120 if (vnn
->srvids
== NULL
) {
1121 DEBUG(1, ("talloc_array failed\n"));
1124 vnn
->pid_indexes
= talloc_array(vnns
, unsigned,
1126 if (vnn
->pid_indexes
== NULL
) {
1127 DEBUG(1, ("talloc_array failed\n"));
1131 for (i
=0; i
<num_pids
; i
++) {
1132 struct ctdb_vnn_list
*vnn
= &vnns
[vnn_indexes
[i
]];
1133 vnn
->srvids
[vnn
->num_filled
] = pids
[i
].unique_id
;
1134 vnn
->pid_indexes
[vnn
->num_filled
] = i
;
1135 vnn
->num_filled
+= 1;
1138 TALLOC_FREE(vnn_indexes
);
1140 *pnum_vnns
= num_vnns
;
1144 TALLOC_FREE(vnn_indexes
);
1148 bool ctdb_serverids_exist_supported(struct ctdbd_connection
*conn
)
1150 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1152 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1154 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1157 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1159 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
1160 const struct server_id
*pids
, unsigned num_pids
,
1163 unsigned i
, num_received
;
1165 struct ctdb_vnn_list
*vnns
= NULL
;
1167 bool result
= false;
1169 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
1170 &vnns
, &num_vnns
)) {
1171 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1175 for (i
=0; i
<num_vnns
; i
++) {
1176 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1177 struct ctdb_req_control req
;
1179 vnn
->reqid
= ctdbd_next_reqid(conn
);
1183 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1184 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
1186 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
1187 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1188 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1189 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
1190 req
.hdr
.reqid
= vnn
->reqid
;
1191 req
.hdr
.destnode
= vnn
->vnn
;
1192 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
1194 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
1195 req
.hdr
.length
+= req
.datalen
;
1198 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1199 ctdb_packet_dump(&req
.hdr
);
1201 status
= ctdb_packet_send(
1204 &req
, offsetof(struct ctdb_req_control
,
1206 data_blob_const(vnn
->srvids
, req
.datalen
));
1207 if (!NT_STATUS_IS_OK(status
)) {
1208 DEBUG(1, ("ctdb_packet_send failed: %s\n",
1209 nt_errstr(status
)));
1214 status
= ctdb_packet_flush(conn
->pkt
);
1215 if (!NT_STATUS_IS_OK(status
)) {
1216 DEBUG(1, ("ctdb_packet_flush failed: %s\n",
1217 nt_errstr(status
)));
1223 while (num_received
< num_vnns
) {
1224 struct ctdb_reply_control
*reply
= NULL
;
1225 struct ctdb_vnn_list
*vnn
;
1227 uint8_t *reply_data
;
1229 status
= ctdb_read_req(conn
, 0, talloc_tos(), (void *)&reply
);
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 DEBUG(1, ("ctdb_read_req failed: %s\n",
1232 nt_errstr(status
)));
1236 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
1237 DEBUG(1, ("Received invalid reply %u\n",
1238 (unsigned)reply
->hdr
.operation
));
1242 reqid
= reply
->hdr
.reqid
;
1244 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1246 for (i
=0; i
<num_vnns
; i
++) {
1247 if (reqid
== vnns
[i
].reqid
) {
1251 if (i
== num_vnns
) {
1252 DEBUG(1, ("Received unknown reqid number %u\n",
1257 DEBUG(10, ("Found index %u\n", i
));
1261 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1262 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1263 (unsigned)reply
->datalen
));
1265 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1269 reply_data
= reply
->data
;
1272 * Got an error reply
1274 DEBUG(5, ("Received short reply len %d, status %u, "
1276 (unsigned)reply
->datalen
,
1277 (unsigned)reply
->status
,
1278 (unsigned)reply
->errorlen
));
1279 dump_data(5, reply
->data
, reply
->errorlen
);
1282 * This will trigger everything set to false
1287 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1288 int idx
= vnn
->pid_indexes
[i
];
1290 if (pids
[i
].unique_id
==
1291 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1292 results
[idx
] = true;
1296 (reply_data
!= NULL
) &&
1297 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1310 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1315 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1316 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1322 data
.dptr
= (uint8_t*)&db_id
;
1323 data
.dsize
= sizeof(db_id
);
1325 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1326 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1327 mem_ctx
, &data
, &cstatus
);
1328 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1329 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1333 return (char *)data
.dptr
;
1337 * attach to a ctdb database
1339 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1340 const char *name
, uint32_t *db_id
, int tdb_flags
)
1345 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1347 data
= string_term_tdb_data(name
);
1349 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1351 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1352 : CTDB_CONTROL_DB_ATTACH
,
1353 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1354 if (!NT_STATUS_IS_OK(status
)) {
1355 DEBUG(0, (__location__
" ctdb_control for db_attach "
1356 "failed: %s\n", nt_errstr(status
)));
1360 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1361 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1362 return NT_STATUS_INTERNAL_ERROR
;
1365 *db_id
= *(uint32_t *)data
.dptr
;
1366 talloc_free(data
.dptr
);
1368 if (!(tdb_flags
& TDB_SEQNUM
)) {
1369 return NT_STATUS_OK
;
1372 data
.dptr
= (uint8_t *)db_id
;
1373 data
.dsize
= sizeof(*db_id
);
1375 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1376 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1377 NULL
, NULL
, &cstatus
);
1378 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1379 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1381 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1385 return NT_STATUS_OK
;
1389 * force the migration of a record to this node
1391 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1394 struct ctdb_req_call req
;
1395 struct ctdb_reply_call
*reply
;
1400 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1401 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1402 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1403 req
.hdr
.operation
= CTDB_REQ_CALL
;
1404 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1405 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1406 req
.callid
= CTDB_NULL_FUNC
;
1408 req
.keylen
= key
.dsize
;
1410 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1411 ctdb_packet_dump(&req
.hdr
);
1413 status
= ctdb_packet_send(
1415 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1416 data_blob_const(key
.dptr
, key
.dsize
));
1418 if (!NT_STATUS_IS_OK(status
)) {
1419 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1423 status
= ctdb_packet_flush(conn
->pkt
);
1425 if (!NT_STATUS_IS_OK(status
)) {
1426 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1427 cluster_fatal("cluster dispatch daemon control write error\n");
1430 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1432 if (!NT_STATUS_IS_OK(status
)) {
1433 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1437 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1438 DEBUG(0, ("received invalid reply\n"));
1439 status
= NT_STATUS_INTERNAL_ERROR
;
1443 status
= NT_STATUS_OK
;
1451 * Fetch a record and parse it
1453 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1454 TDB_DATA key
, bool local_copy
,
1455 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1456 void *private_data
),
1459 struct ctdb_req_call req
;
1460 struct ctdb_reply_call
*reply
;
1464 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1465 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1472 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1473 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1474 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1475 req
.hdr
.operation
= CTDB_REQ_CALL
;
1476 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1478 req
.callid
= CTDB_FETCH_FUNC
;
1480 req
.keylen
= key
.dsize
;
1482 status
= ctdb_packet_send(
1484 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1485 data_blob_const(key
.dptr
, key
.dsize
));
1487 if (!NT_STATUS_IS_OK(status
)) {
1488 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1492 status
= ctdb_packet_flush(conn
->pkt
);
1494 if (!NT_STATUS_IS_OK(status
)) {
1495 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1496 cluster_fatal("cluster dispatch daemon control write error\n");
1499 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1501 if (!NT_STATUS_IS_OK(status
)) {
1502 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1506 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1507 DEBUG(0, ("received invalid reply\n"));
1508 status
= NT_STATUS_INTERNAL_ERROR
;
1512 if (reply
->datalen
== 0) {
1514 * Treat an empty record as non-existing
1516 status
= NT_STATUS_NOT_FOUND
;
1520 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1523 status
= NT_STATUS_OK
;
1529 struct ctdbd_traverse_state
{
1530 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1535 * Handle a traverse record coming in on the ctdbd connection
1538 static NTSTATUS
ctdb_traverse_handler(uint8_t *buf
, size_t length
,
1541 struct ctdbd_traverse_state
*state
=
1542 (struct ctdbd_traverse_state
*)private_data
;
1544 struct ctdb_req_message
*m
;
1545 struct ctdb_rec_data
*d
;
1548 m
= (struct ctdb_req_message
*)buf
;
1550 if (length
< sizeof(*m
) || m
->hdr
.length
!= length
) {
1551 DEBUG(0, ("Got invalid message of length %d\n", (int)length
));
1553 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1556 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1557 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1558 DEBUG(0, ("Got invalid traverse data of length %d\n",
1561 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1564 key
.dsize
= d
->keylen
;
1565 key
.dptr
= &d
->data
[0];
1566 data
.dsize
= d
->datalen
;
1567 data
.dptr
= &d
->data
[d
->keylen
];
1569 if (key
.dsize
== 0 && data
.dsize
== 0) {
1570 /* end of traverse */
1571 return NT_STATUS_END_OF_FILE
;
1574 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1575 DEBUG(0, ("Got invalid ltdb header length %d\n",
1578 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1580 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1581 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1584 state
->fn(key
, data
, state
->private_data
);
1588 return NT_STATUS_OK
;
1592 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1593 connection to ctdbd to avoid the hairy recursive and async problems with
1597 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1598 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1599 void *private_data
),
1602 struct ctdbd_connection
*conn
;
1606 struct ctdb_traverse_start t
;
1608 struct ctdbd_traverse_state state
;
1611 status
= ctdbd_init_connection(NULL
, &conn
);
1613 if (!NT_STATUS_IS_OK(status
)) {
1614 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1615 nt_errstr(status
)));
1620 t
.srvid
= conn
->rand_srvid
;
1621 t
.reqid
= ctdbd_next_reqid(conn
);
1623 data
.dptr
= (uint8_t *)&t
;
1624 data
.dsize
= sizeof(t
);
1626 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1627 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1628 data
, NULL
, NULL
, &cstatus
);
1630 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1632 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1635 if (NT_STATUS_IS_OK(status
)) {
1637 * We need a mapping here
1639 status
= NT_STATUS_UNSUCCESSFUL
;
1645 state
.private_data
= private_data
;
1649 status
= NT_STATUS_OK
;
1651 if (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
1652 ctdb_traverse_handler
, &state
, &status
)) {
1654 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1655 status
= NT_STATUS_OK
;
1660 * There might be more in the queue
1665 if (!NT_STATUS_IS_OK(status
)) {
1669 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
1671 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1673 * There might be more in the queue
1678 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1679 status
= NT_STATUS_OK
;
1683 if (!NT_STATUS_IS_OK(status
)) {
1684 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status
)));
1685 cluster_fatal("ctdbd died\n");
1695 This is used to canonicalize a ctdb_sock_addr structure.
1697 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1698 struct sockaddr_storage
*out
)
1700 memcpy(out
, in
, sizeof (*out
));
1703 if (in
->ss_family
== AF_INET6
) {
1704 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1705 const struct sockaddr_in6
*in6
=
1706 (const struct sockaddr_in6
*)in
;
1707 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1708 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1709 memset(out
, 0, sizeof(*out
));
1710 #ifdef HAVE_SOCK_SIN_LEN
1711 out4
->sin_len
= sizeof(*out
);
1713 out4
->sin_family
= AF_INET
;
1714 out4
->sin_port
= in6
->sin6_port
;
1715 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1722 * Register us as a server for a particular tcp connection
1725 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1726 const struct sockaddr_storage
*_server
,
1727 const struct sockaddr_storage
*_client
,
1728 bool (*release_ip_handler
)(const char *ip_addr
,
1729 void *private_data
),
1733 * we still use ctdb_control_tcp for ipv4
1734 * because we want to work against older ctdb
1735 * versions at runtime
1737 struct ctdb_control_tcp p4
;
1738 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1739 struct ctdb_control_tcp_addr p
;
1743 struct sockaddr_storage client
;
1744 struct sockaddr_storage server
;
1747 * Only one connection so far
1749 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1751 smbd_ctdb_canonicalize_ip(_client
, &client
);
1752 smbd_ctdb_canonicalize_ip(_server
, &server
);
1754 switch (client
.ss_family
) {
1756 memcpy(&p4
.dest
, &server
, sizeof(p4
.dest
));
1757 memcpy(&p4
.src
, &client
, sizeof(p4
.src
));
1758 data
.dptr
= (uint8_t *)&p4
;
1759 data
.dsize
= sizeof(p4
);
1761 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1763 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1764 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1765 data
.dptr
= (uint8_t *)&p
;
1766 data
.dsize
= sizeof(p
);
1770 return NT_STATUS_INTERNAL_ERROR
;
1773 conn
->release_ip_handler
= release_ip_handler
;
1774 conn
->release_ip_priv
= private_data
;
1777 * We want to be told about IP releases
1780 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1781 if (!NT_STATUS_IS_OK(status
)) {
1786 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1787 * can send an extra ack to trigger a reset for our client, so it
1788 * immediately reconnects
1790 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1791 CTDB_CONTROL_TCP_CLIENT
, 0,
1792 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1796 * We want to handle reconfigure events
1798 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1800 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1804 call a control on the local node
1806 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1807 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1808 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1811 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1814 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1816 struct ctdb_client_notify_register reg_data
;
1821 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1823 reg_data
.notify_data
[0] = 0;
1825 struct_len
= offsetof(struct ctdb_client_notify_register
,
1826 notify_data
) + reg_data
.len
;
1828 status
= ctdbd_control_local(
1829 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1830 make_tdb_data((uint8_t *)®_data
, struct_len
),
1831 NULL
, NULL
, &cstatus
);
1832 if (!NT_STATUS_IS_OK(status
)) {
1833 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1834 nt_errstr(status
)));
1839 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1841 struct ctdb_client_notify_deregister dereg_data
;
1845 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1847 status
= ctdbd_control_local(
1848 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1849 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1850 NULL
, NULL
, &cstatus
);
1851 if (!NT_STATUS_IS_OK(status
)) {
1852 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1853 nt_errstr(status
)));
1858 NTSTATUS
ctdbd_probe(void)
1861 * Do a very early check if ctdbd is around to avoid an abort and core
1864 struct ctdbd_connection
*conn
= NULL
;
1867 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1870 * We only care if we can connect.
1879 NTSTATUS
ctdbd_probe(void)
1881 return NT_STATUS_OK
;
1884 NTSTATUS
ctdbd_messaging_send_blob(struct ctdbd_connection
*conn
,
1885 uint32_t dst_vnn
, uint64_t dst_srvid
,
1886 const uint8_t *buf
, size_t buflen
)
1888 return NT_STATUS_NOT_IMPLEMENTED
;