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 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1150 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
1151 const struct server_id
*pids
, unsigned num_pids
,
1154 unsigned i
, num_received
;
1156 struct ctdb_vnn_list
*vnns
= NULL
;
1158 bool result
= false;
1160 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
1161 &vnns
, &num_vnns
)) {
1162 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1166 for (i
=0; i
<num_vnns
; i
++) {
1167 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1168 struct ctdb_req_control req
;
1170 vnn
->reqid
= ctdbd_next_reqid(conn
);
1174 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1175 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
1177 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
1178 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1179 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1180 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
1181 req
.hdr
.reqid
= vnn
->reqid
;
1182 req
.hdr
.destnode
= vnn
->vnn
;
1183 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
1185 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
1186 req
.hdr
.length
+= req
.datalen
;
1189 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1190 ctdb_packet_dump(&req
.hdr
);
1192 status
= ctdb_packet_send(
1195 &req
, offsetof(struct ctdb_req_control
,
1197 data_blob_const(vnn
->srvids
, req
.datalen
));
1198 if (!NT_STATUS_IS_OK(status
)) {
1199 DEBUG(1, ("ctdb_packet_send failed: %s\n",
1200 nt_errstr(status
)));
1205 status
= ctdb_packet_flush(conn
->pkt
);
1206 if (!NT_STATUS_IS_OK(status
)) {
1207 DEBUG(1, ("ctdb_packet_flush failed: %s\n",
1208 nt_errstr(status
)));
1214 while (num_received
< num_vnns
) {
1215 struct ctdb_reply_control
*reply
= NULL
;
1216 struct ctdb_vnn_list
*vnn
;
1218 uint8_t *reply_data
;
1220 status
= ctdb_read_req(conn
, 0, talloc_tos(), (void *)&reply
);
1221 if (!NT_STATUS_IS_OK(status
)) {
1222 DEBUG(1, ("ctdb_read_req failed: %s\n",
1223 nt_errstr(status
)));
1227 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
1228 DEBUG(1, ("Received invalid reply %u\n",
1229 (unsigned)reply
->hdr
.operation
));
1233 reqid
= reply
->hdr
.reqid
;
1235 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1237 for (i
=0; i
<num_vnns
; i
++) {
1238 if (reqid
== vnns
[i
].reqid
) {
1242 if (i
== num_vnns
) {
1243 DEBUG(1, ("Received unknown reqid number %u\n",
1248 DEBUG(10, ("Found index %u\n", i
));
1252 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1253 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1254 (unsigned)reply
->datalen
));
1256 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1260 reply_data
= reply
->data
;
1263 * Got an error reply
1265 DEBUG(5, ("Received short reply len %d, status %u, "
1267 (unsigned)reply
->datalen
,
1268 (unsigned)reply
->status
,
1269 (unsigned)reply
->errorlen
));
1270 dump_data(5, reply
->data
, reply
->errorlen
);
1273 * This will trigger everything set to false
1278 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1279 int idx
= vnn
->pid_indexes
[i
];
1281 if (pids
[i
].unique_id
==
1282 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1283 results
[idx
] = true;
1287 (reply_data
!= NULL
) &&
1288 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1301 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1306 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1307 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1313 data
.dptr
= (uint8_t*)&db_id
;
1314 data
.dsize
= sizeof(db_id
);
1316 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1317 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1318 mem_ctx
, &data
, &cstatus
);
1319 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1320 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1324 return (char *)data
.dptr
;
1328 * attach to a ctdb database
1330 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1331 const char *name
, uint32_t *db_id
, int tdb_flags
)
1336 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1338 data
= string_term_tdb_data(name
);
1340 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1342 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1343 : CTDB_CONTROL_DB_ATTACH
,
1344 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1345 if (!NT_STATUS_IS_OK(status
)) {
1346 DEBUG(0, (__location__
" ctdb_control for db_attach "
1347 "failed: %s\n", nt_errstr(status
)));
1351 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1352 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1353 return NT_STATUS_INTERNAL_ERROR
;
1356 *db_id
= *(uint32_t *)data
.dptr
;
1357 talloc_free(data
.dptr
);
1359 if (!(tdb_flags
& TDB_SEQNUM
)) {
1360 return NT_STATUS_OK
;
1363 data
.dptr
= (uint8_t *)db_id
;
1364 data
.dsize
= sizeof(*db_id
);
1366 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1367 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1368 NULL
, NULL
, &cstatus
);
1369 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1370 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1372 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1376 return NT_STATUS_OK
;
1380 * force the migration of a record to this node
1382 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1385 struct ctdb_req_call req
;
1386 struct ctdb_reply_call
*reply
;
1391 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1392 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1393 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1394 req
.hdr
.operation
= CTDB_REQ_CALL
;
1395 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1396 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1397 req
.callid
= CTDB_NULL_FUNC
;
1399 req
.keylen
= key
.dsize
;
1401 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1402 ctdb_packet_dump(&req
.hdr
);
1404 status
= ctdb_packet_send(
1406 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1407 data_blob_const(key
.dptr
, key
.dsize
));
1409 if (!NT_STATUS_IS_OK(status
)) {
1410 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1414 status
= ctdb_packet_flush(conn
->pkt
);
1416 if (!NT_STATUS_IS_OK(status
)) {
1417 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1418 cluster_fatal("cluster dispatch daemon control write error\n");
1421 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1423 if (!NT_STATUS_IS_OK(status
)) {
1424 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1428 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1429 DEBUG(0, ("received invalid reply\n"));
1430 status
= NT_STATUS_INTERNAL_ERROR
;
1434 status
= NT_STATUS_OK
;
1442 * Fetch a record and parse it
1444 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1445 TDB_DATA key
, bool local_copy
,
1446 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1447 void *private_data
),
1450 struct ctdb_req_call req
;
1451 struct ctdb_reply_call
*reply
;
1455 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1456 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1463 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1464 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1465 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1466 req
.hdr
.operation
= CTDB_REQ_CALL
;
1467 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1469 req
.callid
= CTDB_FETCH_FUNC
;
1471 req
.keylen
= key
.dsize
;
1473 status
= ctdb_packet_send(
1475 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1476 data_blob_const(key
.dptr
, key
.dsize
));
1478 if (!NT_STATUS_IS_OK(status
)) {
1479 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1483 status
= ctdb_packet_flush(conn
->pkt
);
1485 if (!NT_STATUS_IS_OK(status
)) {
1486 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1487 cluster_fatal("cluster dispatch daemon control write error\n");
1490 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1492 if (!NT_STATUS_IS_OK(status
)) {
1493 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1497 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1498 DEBUG(0, ("received invalid reply\n"));
1499 status
= NT_STATUS_INTERNAL_ERROR
;
1503 if (reply
->datalen
== 0) {
1505 * Treat an empty record as non-existing
1507 status
= NT_STATUS_NOT_FOUND
;
1511 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1514 status
= NT_STATUS_OK
;
1520 struct ctdbd_traverse_state
{
1521 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1526 * Handle a traverse record coming in on the ctdbd connection
1529 static NTSTATUS
ctdb_traverse_handler(uint8_t *buf
, size_t length
,
1532 struct ctdbd_traverse_state
*state
=
1533 (struct ctdbd_traverse_state
*)private_data
;
1535 struct ctdb_req_message
*m
;
1536 struct ctdb_rec_data
*d
;
1539 m
= (struct ctdb_req_message
*)buf
;
1541 if (length
< sizeof(*m
) || m
->hdr
.length
!= length
) {
1542 DEBUG(0, ("Got invalid message of length %d\n", (int)length
));
1544 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1547 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1548 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1549 DEBUG(0, ("Got invalid traverse data of length %d\n",
1552 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1555 key
.dsize
= d
->keylen
;
1556 key
.dptr
= &d
->data
[0];
1557 data
.dsize
= d
->datalen
;
1558 data
.dptr
= &d
->data
[d
->keylen
];
1560 if (key
.dsize
== 0 && data
.dsize
== 0) {
1561 /* end of traverse */
1562 return NT_STATUS_END_OF_FILE
;
1565 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1566 DEBUG(0, ("Got invalid ltdb header length %d\n",
1569 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1571 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1572 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1575 state
->fn(key
, data
, state
->private_data
);
1579 return NT_STATUS_OK
;
1583 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1584 connection to ctdbd to avoid the hairy recursive and async problems with
1588 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1589 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1590 void *private_data
),
1593 struct ctdbd_connection
*conn
;
1597 struct ctdb_traverse_start t
;
1599 struct ctdbd_traverse_state state
;
1602 status
= ctdbd_init_connection(NULL
, &conn
);
1604 if (!NT_STATUS_IS_OK(status
)) {
1605 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1606 nt_errstr(status
)));
1611 t
.srvid
= conn
->rand_srvid
;
1612 t
.reqid
= ctdbd_next_reqid(conn
);
1614 data
.dptr
= (uint8_t *)&t
;
1615 data
.dsize
= sizeof(t
);
1617 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1618 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1619 data
, NULL
, NULL
, &cstatus
);
1621 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1623 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1626 if (NT_STATUS_IS_OK(status
)) {
1628 * We need a mapping here
1630 status
= NT_STATUS_UNSUCCESSFUL
;
1636 state
.private_data
= private_data
;
1640 status
= NT_STATUS_OK
;
1642 if (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
1643 ctdb_traverse_handler
, &state
, &status
)) {
1645 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1646 status
= NT_STATUS_OK
;
1651 * There might be more in the queue
1656 if (!NT_STATUS_IS_OK(status
)) {
1660 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
1662 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1664 * There might be more in the queue
1669 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1670 status
= NT_STATUS_OK
;
1674 if (!NT_STATUS_IS_OK(status
)) {
1675 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status
)));
1676 cluster_fatal("ctdbd died\n");
1686 This is used to canonicalize a ctdb_sock_addr structure.
1688 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1689 struct sockaddr_storage
*out
)
1691 memcpy(out
, in
, sizeof (*out
));
1694 if (in
->ss_family
== AF_INET6
) {
1695 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1696 const struct sockaddr_in6
*in6
=
1697 (const struct sockaddr_in6
*)in
;
1698 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1699 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1700 memset(out
, 0, sizeof(*out
));
1701 #ifdef HAVE_SOCK_SIN_LEN
1702 out4
->sin_len
= sizeof(*out
);
1704 out4
->sin_family
= AF_INET
;
1705 out4
->sin_port
= in6
->sin6_port
;
1706 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1713 * Register us as a server for a particular tcp connection
1716 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1717 const struct sockaddr_storage
*_server
,
1718 const struct sockaddr_storage
*_client
,
1719 bool (*release_ip_handler
)(const char *ip_addr
,
1720 void *private_data
),
1724 * we still use ctdb_control_tcp for ipv4
1725 * because we want to work against older ctdb
1726 * versions at runtime
1728 struct ctdb_control_tcp p4
;
1729 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1730 struct ctdb_control_tcp_addr p
;
1734 struct sockaddr_storage client
;
1735 struct sockaddr_storage server
;
1738 * Only one connection so far
1740 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1742 smbd_ctdb_canonicalize_ip(_client
, &client
);
1743 smbd_ctdb_canonicalize_ip(_server
, &server
);
1745 switch (client
.ss_family
) {
1747 memcpy(&p4
.dest
, &server
, sizeof(p4
.dest
));
1748 memcpy(&p4
.src
, &client
, sizeof(p4
.src
));
1749 data
.dptr
= (uint8_t *)&p4
;
1750 data
.dsize
= sizeof(p4
);
1752 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1754 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1755 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1756 data
.dptr
= (uint8_t *)&p
;
1757 data
.dsize
= sizeof(p
);
1761 return NT_STATUS_INTERNAL_ERROR
;
1764 conn
->release_ip_handler
= release_ip_handler
;
1765 conn
->release_ip_priv
= private_data
;
1768 * We want to be told about IP releases
1771 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1772 if (!NT_STATUS_IS_OK(status
)) {
1777 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1778 * can send an extra ack to trigger a reset for our client, so it
1779 * immediately reconnects
1781 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1782 CTDB_CONTROL_TCP_CLIENT
, 0,
1783 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1787 * We want to handle reconfigure events
1789 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1791 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1795 call a control on the local node
1797 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1798 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1799 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1802 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1805 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1807 struct ctdb_client_notify_register reg_data
;
1812 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1814 reg_data
.notify_data
[0] = 0;
1816 struct_len
= offsetof(struct ctdb_client_notify_register
,
1817 notify_data
) + reg_data
.len
;
1819 status
= ctdbd_control_local(
1820 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1821 make_tdb_data((uint8_t *)®_data
, struct_len
),
1822 NULL
, NULL
, &cstatus
);
1823 if (!NT_STATUS_IS_OK(status
)) {
1824 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1825 nt_errstr(status
)));
1830 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1832 struct ctdb_client_notify_deregister dereg_data
;
1836 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1838 status
= ctdbd_control_local(
1839 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1840 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1841 NULL
, NULL
, &cstatus
);
1842 if (!NT_STATUS_IS_OK(status
)) {
1843 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1844 nt_errstr(status
)));
1849 NTSTATUS
ctdbd_probe(void)
1852 * Do a very early check if ctdbd is around to avoid an abort and core
1855 struct ctdbd_connection
*conn
= NULL
;
1858 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1861 * We only care if we can connect.
1870 NTSTATUS
ctdbd_probe(void)
1872 return NT_STATUS_OK
;
1875 NTSTATUS
ctdbd_messaging_send_blob(struct ctdbd_connection
*conn
,
1876 uint32_t dst_vnn
, uint64_t dst_srvid
,
1877 const uint8_t *buf
, size_t buflen
)
1879 return NT_STATUS_NOT_IMPLEMENTED
;