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
;
197 const char *lp_ctdbd_socket(void)
201 ret
= lp__ctdbd_socket();
202 if (ret
!= NULL
&& strlen(ret
) > 0) {
210 * Get us a ctdb connection
213 static NTSTATUS
ctdbd_connect(TALLOC_CTX
*mem_ctx
,
214 struct ctdb_packet_context
**presult
)
216 struct ctdb_packet_context
*result
;
217 const char *sockname
= lp_ctdbd_socket();
218 struct sockaddr_un addr
= { 0, };
222 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
224 DEBUG(3, ("Could not create socket: %s\n", strerror(errno
)));
225 return map_nt_error_from_unix(errno
);
228 addr
.sun_family
= AF_UNIX
;
229 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", sockname
);
231 salen
= sizeof(struct sockaddr_un
);
232 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
233 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
236 return map_nt_error_from_unix(errno
);
239 if (!(result
= ctdb_packet_init(mem_ctx
, fd
))) {
241 return NT_STATUS_NO_MEMORY
;
249 * Do we have a complete ctdb packet in the queue?
252 static bool ctdb_req_complete(const uint8_t *buf
, size_t available
,
258 if (available
< sizeof(msglen
)) {
262 msglen
= *((const uint32_t *)buf
);
264 DEBUG(11, ("msglen = %d\n", msglen
));
266 if (msglen
< sizeof(struct ctdb_req_header
)) {
267 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
268 "the req_header\n", (int)msglen
,
269 (int)sizeof(struct ctdb_req_header
)));
270 cluster_fatal("ctdbd protocol error\n");
273 if (available
< msglen
) {
282 * State necessary to defer an incoming message while we are waiting for a
286 struct deferred_msg_state
{
287 struct messaging_context
*msg_ctx
;
288 struct messaging_rec
*rec
;
292 * Timed event handler for the deferred message
295 static void deferred_message_dispatch(struct tevent_context
*event_ctx
,
296 struct tevent_timer
*te
,
300 struct deferred_msg_state
*state
= talloc_get_type_abort(
301 private_data
, struct deferred_msg_state
);
303 messaging_dispatch_rec(state
->msg_ctx
, state
->rec
);
308 struct req_pull_state
{
314 * Pull a ctdb request out of the incoming ctdb_packet queue
317 static NTSTATUS
ctdb_req_pull(uint8_t *buf
, size_t length
,
320 struct req_pull_state
*state
= (struct req_pull_state
*)private_data
;
322 state
->req
.data
= talloc_move(state
->mem_ctx
, &buf
);
323 state
->req
.length
= length
;
328 * Fetch a messaging_rec from an incoming ctdb style message
331 static struct messaging_rec
*ctdb_pull_messaging_rec(TALLOC_CTX
*mem_ctx
,
332 size_t overall_length
,
333 struct ctdb_req_message
*msg
)
335 struct messaging_rec
*result
;
337 enum ndr_err_code ndr_err
;
339 if ((overall_length
< offsetof(struct ctdb_req_message
, data
))
341 < offsetof(struct ctdb_req_message
, data
) + msg
->datalen
)) {
343 cluster_fatal("got invalid msg length");
346 if (!(result
= talloc(mem_ctx
, struct messaging_rec
))) {
347 DEBUG(0, ("talloc failed\n"));
351 blob
= data_blob_const(msg
->data
, msg
->datalen
);
353 ndr_err
= ndr_pull_struct_blob(
354 &blob
, result
, result
,
355 (ndr_pull_flags_fn_t
)ndr_pull_messaging_rec
);
357 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
358 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
359 ndr_errstr(ndr_err
)));
364 if (DEBUGLEVEL
>= 11) {
365 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
366 NDR_PRINT_DEBUG(messaging_rec
, result
);
372 static NTSTATUS
ctdb_packet_fd_read_sync(struct ctdb_packet_context
*ctx
)
374 int timeout
= lp_ctdb_timeout();
379 return ctdb_packet_fd_read_sync_timeout(ctx
, timeout
);
383 * Read a full ctdbd request. If we have a messaging context, defer incoming
384 * messages that might come in between.
387 static NTSTATUS
ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
388 TALLOC_CTX
*mem_ctx
, void *result
)
390 struct ctdb_req_header
*hdr
;
391 struct req_pull_state state
;
396 state
.mem_ctx
= mem_ctx
;
398 while (!ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
399 ctdb_req_pull
, &state
, &status
)) {
403 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
405 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_BUSY
)) {
408 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
413 if (!NT_STATUS_IS_OK(status
)) {
414 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
415 cluster_fatal("ctdbd died\n");
419 if (!NT_STATUS_IS_OK(status
)) {
420 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status
)));
421 cluster_fatal("ctdbd died\n");
424 hdr
= (struct ctdb_req_header
*)state
.req
.data
;
426 DEBUG(11, ("Received ctdb packet\n"));
427 ctdb_packet_dump(hdr
);
429 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
430 struct tevent_timer
*evt
;
431 struct deferred_msg_state
*msg_state
;
432 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
434 if (conn
->msg_ctx
== NULL
) {
435 DEBUG(1, ("Got a message without having a msg ctx, "
436 "dropping msg %llu\n",
437 (long long unsigned)msg
->srvid
));
441 if ((conn
->release_ip_handler
!= NULL
)
442 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
445 /* must be dispatched immediately */
446 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
447 ret
= conn
->release_ip_handler((const char *)msg
->data
,
448 conn
->release_ip_priv
);
451 * We need to release the ip,
452 * so return an error to the upper layers.
454 * We make sure we don't trigger this again.
456 conn
->release_ip_handler
= NULL
;
457 conn
->release_ip_priv
= NULL
;
458 return NT_STATUS_ADDRESS_CLOSED
;
464 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
465 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)) {
467 DEBUG(1, ("ctdb_read_req: Got %s message\n",
468 (msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
469 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
471 messaging_send(conn
->msg_ctx
,
472 messaging_server_id(conn
->msg_ctx
),
473 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
474 messaging_send(conn
->msg_ctx
,
475 messaging_server_id(conn
->msg_ctx
),
476 MSG_DBWRAP_G_LOCK_RETRY
,
482 msg_state
= talloc(NULL
, struct deferred_msg_state
);
483 if (msg_state
== NULL
) {
484 DEBUG(0, ("talloc failed\n"));
489 if (!(msg_state
->rec
= ctdb_pull_messaging_rec(
490 msg_state
, state
.req
.length
, msg
))) {
491 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
492 TALLOC_FREE(msg_state
);
499 msg_state
->msg_ctx
= conn
->msg_ctx
;
502 * We're waiting for a call reply, but an async message has
503 * crossed. Defer dispatching to the toplevel event loop.
505 evt
= tevent_add_timer(conn
->msg_ctx
->event_ctx
,
506 conn
->msg_ctx
->event_ctx
,
508 deferred_message_dispatch
,
511 DEBUG(0, ("event_add_timed failed\n"));
512 TALLOC_FREE(msg_state
);
520 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
521 /* we got the wrong reply */
522 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
523 "been %u\n", hdr
->reqid
, reqid
));
528 *((void **)result
) = talloc_move(mem_ctx
, &hdr
);
534 * Get us a ctdbd connection
537 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
538 struct ctdbd_connection
**pconn
)
540 struct ctdbd_connection
*conn
;
543 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
544 DEBUG(0, ("talloc failed\n"));
545 return NT_STATUS_NO_MEMORY
;
548 status
= ctdbd_connect(conn
, &conn
->pkt
);
550 if (!NT_STATUS_IS_OK(status
)) {
551 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status
)));
555 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
557 if (!NT_STATUS_IS_OK(status
)) {
558 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
562 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
563 DEBUG(2, ("Node is not working, can not connect\n"));
564 status
= NT_STATUS_INTERNAL_DB_ERROR
;
568 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
569 sizeof(conn
->rand_srvid
));
571 status
= register_with_ctdbd(conn
, conn
->rand_srvid
);
573 if (!NT_STATUS_IS_OK(status
)) {
574 DEBUG(5, ("Could not register random srvid: %s\n",
588 * Get us a ctdbd connection and register us as a process
591 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
592 struct ctdbd_connection
**pconn
)
594 struct ctdbd_connection
*conn
;
597 status
= ctdbd_init_connection(mem_ctx
, &conn
);
599 if (!NT_STATUS_IS_OK(status
)) {
603 status
= register_with_ctdbd(conn
, (uint64_t)getpid());
604 if (!NT_STATUS_IS_OK(status
)) {
608 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
);
609 if (!NT_STATUS_IS_OK(status
)) {
613 status
= register_with_ctdbd(conn
, CTDB_SRVID_SAMBA_NOTIFY
);
614 if (!NT_STATUS_IS_OK(status
)) {
626 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
628 return conn
->msg_ctx
;
631 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
633 return ctdb_packet_get_fd(conn
->pkt
);
637 * Packet handler to receive and handle a ctdb message
639 static NTSTATUS
ctdb_handle_message(uint8_t *buf
, size_t length
,
642 struct ctdbd_connection
*conn
= talloc_get_type_abort(
643 private_data
, struct ctdbd_connection
);
644 struct ctdb_req_message
*msg
;
645 struct messaging_rec
*msg_rec
;
647 msg
= (struct ctdb_req_message
*)buf
;
649 if (msg
->hdr
.operation
!= CTDB_REQ_MESSAGE
) {
650 DEBUG(0, ("Received async msg of type %u, discarding\n",
651 msg
->hdr
.operation
));
653 return NT_STATUS_INVALID_PARAMETER
;
656 if ((conn
->release_ip_handler
!= NULL
)
657 && (msg
->srvid
== CTDB_SRVID_RELEASE_IP
)) {
660 /* must be dispatched immediately */
661 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
662 ret
= conn
->release_ip_handler((const char *)msg
->data
,
663 conn
->release_ip_priv
);
666 * We need to release the ip.
668 * We make sure we don't trigger this again.
670 conn
->release_ip_handler
= NULL
;
671 conn
->release_ip_priv
= NULL
;
677 SMB_ASSERT(conn
->msg_ctx
!= NULL
);
679 if ((msg
->srvid
== CTDB_SRVID_RECONFIGURE
)
680 || (msg
->srvid
== CTDB_SRVID_SAMBA_NOTIFY
)){
681 DEBUG(0,("Got cluster reconfigure message\n"));
683 * when the cluster is reconfigured or someone of the
684 * family has passed away (SAMBA_NOTIFY), we need to
685 * clean the brl database
687 messaging_send(conn
->msg_ctx
,
688 messaging_server_id(conn
->msg_ctx
),
689 MSG_SMB_BRL_VALIDATE
, &data_blob_null
);
691 messaging_send(conn
->msg_ctx
,
692 messaging_server_id(conn
->msg_ctx
),
693 MSG_DBWRAP_G_LOCK_RETRY
,
700 /* only messages to our pid or the broadcast are valid here */
701 if (msg
->srvid
!= getpid() && msg
->srvid
!= MSG_SRVID_SAMBA
) {
702 DEBUG(0,("Got unexpected message with srvid=%llu\n",
703 (unsigned long long)msg
->srvid
));
708 if (!(msg_rec
= ctdb_pull_messaging_rec(NULL
, length
, msg
))) {
709 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
711 return NT_STATUS_NO_MEMORY
;
714 messaging_dispatch_rec(conn
->msg_ctx
, msg_rec
);
716 TALLOC_FREE(msg_rec
);
722 * The ctdbd socket is readable asynchronuously
725 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
726 struct tevent_fd
*event
,
730 struct ctdbd_connection
*conn
= talloc_get_type_abort(
731 private_data
, struct ctdbd_connection
);
735 status
= ctdb_packet_fd_read(conn
->pkt
);
737 if (!NT_STATUS_IS_OK(status
)) {
738 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status
)));
739 cluster_fatal("ctdbd died\n");
742 while (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
743 ctdb_handle_message
, conn
, &status
)) {
744 if (!NT_STATUS_IS_OK(status
)) {
745 DEBUG(10, ("could not handle incoming message: %s\n",
752 * Prepare a ctdbd connection to receive messages
755 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
756 struct messaging_context
*msg_ctx
)
758 SMB_ASSERT(conn
->msg_ctx
== NULL
);
759 SMB_ASSERT(conn
->fde
== NULL
);
761 if (!(conn
->fde
= tevent_add_fd(msg_ctx
->event_ctx
, conn
,
762 ctdb_packet_get_fd(conn
->pkt
),
764 ctdbd_socket_handler
,
766 DEBUG(0, ("event_add_fd failed\n"));
767 return NT_STATUS_NO_MEMORY
;
770 conn
->msg_ctx
= msg_ctx
;
776 * Send a messaging message across a ctdbd
779 NTSTATUS
ctdbd_messaging_send(struct ctdbd_connection
*conn
,
780 uint32_t dst_vnn
, uint64_t dst_srvid
,
781 struct messaging_rec
*msg
)
785 enum ndr_err_code ndr_err
;
787 ndr_err
= ndr_push_struct_blob(
788 &blob
, talloc_tos(), msg
,
789 (ndr_push_flags_fn_t
)ndr_push_messaging_rec
);
791 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
792 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
793 ndr_errstr(ndr_err
)));
794 return ndr_map_error2ntstatus(ndr_err
);
797 status
= ctdbd_messaging_send_blob(conn
, dst_vnn
, dst_srvid
,
798 blob
.data
, blob
.length
);
799 TALLOC_FREE(blob
.data
);
803 NTSTATUS
ctdbd_messaging_send_blob(struct ctdbd_connection
*conn
,
804 uint32_t dst_vnn
, uint64_t dst_srvid
,
805 const uint8_t *buf
, size_t buflen
)
807 struct ctdb_req_message r
;
810 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
811 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
812 r
.hdr
.ctdb_version
= CTDB_VERSION
;
813 r
.hdr
.generation
= 1;
814 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
815 r
.hdr
.destnode
= dst_vnn
;
816 r
.hdr
.srcnode
= conn
->our_vnn
;
821 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
822 ctdb_packet_dump(&r
.hdr
);
824 status
= ctdb_packet_send(
826 data_blob_const(&r
, offsetof(struct ctdb_req_message
, data
)),
827 data_blob_const(buf
, buflen
));
829 if (!NT_STATUS_IS_OK(status
)) {
830 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
834 status
= ctdb_packet_flush(conn
->pkt
);
835 if (!NT_STATUS_IS_OK(status
)) {
836 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
837 cluster_fatal("cluster dispatch daemon msg write error\n");
843 * send/recv a generic ctdb control message
845 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
846 uint32_t vnn
, uint32_t opcode
,
847 uint64_t srvid
, uint32_t flags
,
849 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
852 struct ctdb_req_control req
;
853 struct ctdb_reply_control
*reply
= NULL
;
854 struct ctdbd_connection
*new_conn
= NULL
;
858 status
= ctdbd_init_connection(NULL
, &new_conn
);
860 if (!NT_STATUS_IS_OK(status
)) {
861 DEBUG(10, ("Could not init temp connection: %s\n",
870 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
871 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
872 req
.hdr
.ctdb_version
= CTDB_VERSION
;
873 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
874 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
875 req
.hdr
.destnode
= vnn
;
878 req
.datalen
= data
.dsize
;
881 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
882 ctdb_packet_dump(&req
.hdr
);
884 status
= ctdb_packet_send(
886 data_blob_const(&req
, offsetof(struct ctdb_req_control
, data
)),
887 data_blob_const(data
.dptr
, data
.dsize
));
889 if (!NT_STATUS_IS_OK(status
)) {
890 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
894 status
= ctdb_packet_flush(conn
->pkt
);
896 if (!NT_STATUS_IS_OK(status
)) {
897 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
898 cluster_fatal("cluster dispatch daemon control write error\n");
901 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
902 TALLOC_FREE(new_conn
);
909 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
911 if (!NT_STATUS_IS_OK(status
)) {
912 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
916 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
917 DEBUG(0, ("received invalid reply\n"));
922 if (!(outdata
->dptr
= (uint8
*)talloc_memdup(
923 mem_ctx
, reply
->data
, reply
->datalen
))) {
925 return NT_STATUS_NO_MEMORY
;
927 outdata
->dsize
= reply
->datalen
;
930 (*cstatus
) = reply
->status
;
933 status
= NT_STATUS_OK
;
936 TALLOC_FREE(new_conn
);
942 * see if a remote process exists
944 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
952 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
953 DEBUG(10, ("ctdb_processes_exist failed\n"));
959 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
960 const struct server_id
*pids
, int num_pids
,
963 TALLOC_CTX
*frame
= talloc_stackframe();
969 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
970 if (reqids
== NULL
) {
974 for (i
=0; i
<num_pids
; i
++) {
975 struct ctdb_req_control req
;
979 reqids
[i
] = ctdbd_next_reqid(conn
);
984 * pids[i].pid is uint64_t, scale down to pid_t which
985 * is the wire protocol towards ctdb.
989 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
990 (int)pids
[i
].vnn
, (int)pid
,
993 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
994 req
.hdr
.length
+= sizeof(pid
);
995 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
996 req
.hdr
.ctdb_version
= CTDB_VERSION
;
997 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
998 req
.hdr
.reqid
= reqids
[i
];
999 req
.hdr
.destnode
= pids
[i
].vnn
;
1000 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
1002 req
.datalen
= sizeof(pid
);
1005 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1006 ctdb_packet_dump(&req
.hdr
);
1008 status
= ctdb_packet_send(
1011 &req
, offsetof(struct ctdb_req_control
, data
)),
1012 data_blob_const(&pid
, sizeof(pid
)));
1013 if (!NT_STATUS_IS_OK(status
)) {
1014 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1015 nt_errstr(status
)));
1020 status
= ctdb_packet_flush(conn
->pkt
);
1021 if (!NT_STATUS_IS_OK(status
)) {
1022 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1023 nt_errstr(status
)));
1029 while (num_received
< num_pids
) {
1030 struct ctdb_reply_control
*reply
= NULL
;
1033 status
= ctdb_read_req(conn
, 0, talloc_tos(), (void *)&reply
);
1034 if (!NT_STATUS_IS_OK(status
)) {
1035 DEBUG(10, ("ctdb_read_req failed: %s\n",
1036 nt_errstr(status
)));
1040 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
1041 DEBUG(10, ("Received invalid reply\n"));
1045 reqid
= reply
->hdr
.reqid
;
1047 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1049 for (i
=0; i
<num_pids
; i
++) {
1050 if (reqid
== reqids
[i
]) {
1054 if (i
== num_pids
) {
1055 DEBUG(10, ("Received unknown record number %u\n",
1059 results
[i
] = ((reply
->status
) == 0);
1070 struct ctdb_vnn_list
{
1073 unsigned num_srvids
;
1074 unsigned num_filled
;
1076 unsigned *pid_indexes
;
1080 * Get a list of all vnns mentioned in a list of
1081 * server_ids. vnn_indexes tells where in the vnns array we have to
1084 static bool ctdb_collect_vnns(TALLOC_CTX
*mem_ctx
,
1085 const struct server_id
*pids
, unsigned num_pids
,
1086 struct ctdb_vnn_list
**pvnns
,
1087 unsigned *pnum_vnns
)
1089 struct ctdb_vnn_list
*vnns
= NULL
;
1090 unsigned *vnn_indexes
= NULL
;
1091 unsigned i
, num_vnns
= 0;
1093 vnn_indexes
= talloc_array(mem_ctx
, unsigned, num_pids
);
1094 if (vnn_indexes
== NULL
) {
1095 DEBUG(1, ("talloc_array failed\n"));
1099 for (i
=0; i
<num_pids
; i
++) {
1101 uint32_t vnn
= pids
[i
].vnn
;
1103 for (j
=0; j
<num_vnns
; j
++) {
1104 if (vnn
== vnns
[j
].vnn
) {
1112 * Already in the array
1114 vnns
[j
].num_srvids
+= 1;
1117 vnns
= talloc_realloc(mem_ctx
, vnns
, struct ctdb_vnn_list
,
1120 DEBUG(1, ("talloc_realloc failed\n"));
1123 vnns
[num_vnns
].vnn
= vnn
;
1124 vnns
[num_vnns
].num_srvids
= 1;
1125 vnns
[num_vnns
].num_filled
= 0;
1128 for (i
=0; i
<num_vnns
; i
++) {
1129 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1131 vnn
->srvids
= talloc_array(vnns
, uint64_t, vnn
->num_srvids
);
1132 if (vnn
->srvids
== NULL
) {
1133 DEBUG(1, ("talloc_array failed\n"));
1136 vnn
->pid_indexes
= talloc_array(vnns
, unsigned,
1138 if (vnn
->pid_indexes
== NULL
) {
1139 DEBUG(1, ("talloc_array failed\n"));
1143 for (i
=0; i
<num_pids
; i
++) {
1144 struct ctdb_vnn_list
*vnn
= &vnns
[vnn_indexes
[i
]];
1145 vnn
->srvids
[vnn
->num_filled
] = pids
[i
].unique_id
;
1146 vnn
->pid_indexes
[vnn
->num_filled
] = i
;
1147 vnn
->num_filled
+= 1;
1150 TALLOC_FREE(vnn_indexes
);
1152 *pnum_vnns
= num_vnns
;
1156 TALLOC_FREE(vnn_indexes
);
1160 bool ctdb_serverids_exist_supported(struct ctdbd_connection
*conn
)
1162 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1164 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1166 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1169 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
1170 const struct server_id
*pids
, unsigned num_pids
,
1173 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1175 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1176 unsigned i
, num_received
;
1178 struct ctdb_vnn_list
*vnns
= NULL
;
1180 bool result
= false;
1182 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
1183 &vnns
, &num_vnns
)) {
1184 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1188 for (i
=0; i
<num_vnns
; i
++) {
1189 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
1190 struct ctdb_req_control req
;
1192 vnn
->reqid
= ctdbd_next_reqid(conn
);
1196 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1197 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
1199 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
1200 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1201 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1202 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
1203 req
.hdr
.reqid
= vnn
->reqid
;
1204 req
.hdr
.destnode
= vnn
->vnn
;
1205 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
1207 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
1208 req
.hdr
.length
+= req
.datalen
;
1211 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1212 ctdb_packet_dump(&req
.hdr
);
1214 status
= ctdb_packet_send(
1217 &req
, offsetof(struct ctdb_req_control
,
1219 data_blob_const(vnn
->srvids
, req
.datalen
));
1220 if (!NT_STATUS_IS_OK(status
)) {
1221 DEBUG(1, ("ctdb_packet_send failed: %s\n",
1222 nt_errstr(status
)));
1227 status
= ctdb_packet_flush(conn
->pkt
);
1228 if (!NT_STATUS_IS_OK(status
)) {
1229 DEBUG(1, ("ctdb_packet_flush failed: %s\n",
1230 nt_errstr(status
)));
1236 while (num_received
< num_vnns
) {
1237 struct ctdb_reply_control
*reply
= NULL
;
1238 struct ctdb_vnn_list
*vnn
;
1240 uint8_t *reply_data
;
1242 status
= ctdb_read_req(conn
, 0, talloc_tos(), (void *)&reply
);
1243 if (!NT_STATUS_IS_OK(status
)) {
1244 DEBUG(1, ("ctdb_read_req failed: %s\n",
1245 nt_errstr(status
)));
1249 if (reply
->hdr
.operation
!= CTDB_REPLY_CONTROL
) {
1250 DEBUG(1, ("Received invalid reply %u\n",
1251 (unsigned)reply
->hdr
.operation
));
1255 reqid
= reply
->hdr
.reqid
;
1257 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1259 for (i
=0; i
<num_vnns
; i
++) {
1260 if (reqid
== vnns
[i
].reqid
) {
1264 if (i
== num_vnns
) {
1265 DEBUG(1, ("Received unknown reqid number %u\n",
1270 DEBUG(10, ("Found index %u\n", i
));
1274 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1275 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1276 (unsigned)reply
->datalen
));
1278 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1282 reply_data
= reply
->data
;
1285 * Got an error reply
1287 DEBUG(5, ("Received short reply len %d, status %u, "
1289 (unsigned)reply
->datalen
,
1290 (unsigned)reply
->status
,
1291 (unsigned)reply
->errorlen
));
1292 dump_data(5, reply
->data
, reply
->errorlen
);
1295 * This will trigger everything set to false
1300 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1301 int idx
= vnn
->pid_indexes
[i
];
1303 if (pids
[i
].unique_id
==
1304 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1305 results
[idx
] = true;
1309 (reply_data
!= NULL
) &&
1310 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1321 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1327 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1328 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1334 data
.dptr
= (uint8_t*)&db_id
;
1335 data
.dsize
= sizeof(db_id
);
1337 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1338 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1339 mem_ctx
, &data
, &cstatus
);
1340 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1341 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1345 return (char *)data
.dptr
;
1349 * attach to a ctdb database
1351 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1352 const char *name
, uint32_t *db_id
, int tdb_flags
)
1357 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1359 data
= string_term_tdb_data(name
);
1361 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1363 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1364 : CTDB_CONTROL_DB_ATTACH
,
1365 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1366 if (!NT_STATUS_IS_OK(status
)) {
1367 DEBUG(0, (__location__
" ctdb_control for db_attach "
1368 "failed: %s\n", nt_errstr(status
)));
1372 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1373 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1374 return NT_STATUS_INTERNAL_ERROR
;
1377 *db_id
= *(uint32_t *)data
.dptr
;
1378 talloc_free(data
.dptr
);
1380 if (!(tdb_flags
& TDB_SEQNUM
)) {
1381 return NT_STATUS_OK
;
1384 data
.dptr
= (uint8_t *)db_id
;
1385 data
.dsize
= sizeof(*db_id
);
1387 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1388 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1389 NULL
, NULL
, &cstatus
);
1390 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1391 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1393 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1397 return NT_STATUS_OK
;
1401 * force the migration of a record to this node
1403 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1406 struct ctdb_req_call req
;
1407 struct ctdb_reply_call
*reply
;
1412 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1413 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1414 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1415 req
.hdr
.operation
= CTDB_REQ_CALL
;
1416 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1417 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1418 req
.callid
= CTDB_NULL_FUNC
;
1420 req
.keylen
= key
.dsize
;
1422 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1423 ctdb_packet_dump(&req
.hdr
);
1425 status
= ctdb_packet_send(
1427 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1428 data_blob_const(key
.dptr
, key
.dsize
));
1430 if (!NT_STATUS_IS_OK(status
)) {
1431 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1435 status
= ctdb_packet_flush(conn
->pkt
);
1437 if (!NT_STATUS_IS_OK(status
)) {
1438 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1439 cluster_fatal("cluster dispatch daemon control write error\n");
1442 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1444 if (!NT_STATUS_IS_OK(status
)) {
1445 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1449 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1450 DEBUG(0, ("received invalid reply\n"));
1451 status
= NT_STATUS_INTERNAL_ERROR
;
1455 status
= NT_STATUS_OK
;
1463 * Fetch a record and parse it
1465 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1466 TDB_DATA key
, bool local_copy
,
1467 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1468 void *private_data
),
1471 struct ctdb_req_call req
;
1472 struct ctdb_reply_call
*reply
;
1476 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1477 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1484 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1485 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1486 req
.hdr
.ctdb_version
= CTDB_VERSION
;
1487 req
.hdr
.operation
= CTDB_REQ_CALL
;
1488 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1490 req
.callid
= CTDB_FETCH_FUNC
;
1492 req
.keylen
= key
.dsize
;
1494 status
= ctdb_packet_send(
1496 data_blob_const(&req
, offsetof(struct ctdb_req_call
, data
)),
1497 data_blob_const(key
.dptr
, key
.dsize
));
1499 if (!NT_STATUS_IS_OK(status
)) {
1500 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status
)));
1504 status
= ctdb_packet_flush(conn
->pkt
);
1506 if (!NT_STATUS_IS_OK(status
)) {
1507 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status
)));
1508 cluster_fatal("cluster dispatch daemon control write error\n");
1511 status
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, (void *)&reply
);
1513 if (!NT_STATUS_IS_OK(status
)) {
1514 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status
)));
1518 if (reply
->hdr
.operation
!= CTDB_REPLY_CALL
) {
1519 DEBUG(0, ("received invalid reply\n"));
1520 status
= NT_STATUS_INTERNAL_ERROR
;
1524 if (reply
->datalen
== 0) {
1526 * Treat an empty record as non-existing
1528 status
= NT_STATUS_NOT_FOUND
;
1532 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1535 status
= NT_STATUS_OK
;
1541 struct ctdbd_traverse_state
{
1542 void (*fn
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1547 * Handle a traverse record coming in on the ctdbd connection
1550 static NTSTATUS
ctdb_traverse_handler(uint8_t *buf
, size_t length
,
1553 struct ctdbd_traverse_state
*state
=
1554 (struct ctdbd_traverse_state
*)private_data
;
1556 struct ctdb_req_message
*m
;
1557 struct ctdb_rec_data
*d
;
1560 m
= (struct ctdb_req_message
*)buf
;
1562 if (length
< sizeof(*m
) || m
->hdr
.length
!= length
) {
1563 DEBUG(0, ("Got invalid message of length %d\n", (int)length
));
1565 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1568 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1569 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1570 DEBUG(0, ("Got invalid traverse data of length %d\n",
1573 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1576 key
.dsize
= d
->keylen
;
1577 key
.dptr
= &d
->data
[0];
1578 data
.dsize
= d
->datalen
;
1579 data
.dptr
= &d
->data
[d
->keylen
];
1581 if (key
.dsize
== 0 && data
.dsize
== 0) {
1582 /* end of traverse */
1583 return NT_STATUS_END_OF_FILE
;
1586 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1587 DEBUG(0, ("Got invalid ltdb header length %d\n",
1590 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1592 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1593 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1596 state
->fn(key
, data
, state
->private_data
);
1600 return NT_STATUS_OK
;
1604 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1605 connection to ctdbd to avoid the hairy recursive and async problems with
1609 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1610 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1611 void *private_data
),
1614 struct ctdbd_connection
*conn
;
1618 struct ctdb_traverse_start t
;
1620 struct ctdbd_traverse_state state
;
1623 status
= ctdbd_init_connection(NULL
, &conn
);
1625 if (!NT_STATUS_IS_OK(status
)) {
1626 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1627 nt_errstr(status
)));
1632 t
.srvid
= conn
->rand_srvid
;
1633 t
.reqid
= ctdbd_next_reqid(conn
);
1635 data
.dptr
= (uint8_t *)&t
;
1636 data
.dsize
= sizeof(t
);
1638 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1639 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1640 data
, NULL
, NULL
, &cstatus
);
1642 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1644 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1647 if (NT_STATUS_IS_OK(status
)) {
1649 * We need a mapping here
1651 status
= NT_STATUS_UNSUCCESSFUL
;
1657 state
.private_data
= private_data
;
1661 status
= NT_STATUS_OK
;
1663 if (ctdb_packet_handler(conn
->pkt
, ctdb_req_complete
,
1664 ctdb_traverse_handler
, &state
, &status
)) {
1666 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1667 status
= NT_STATUS_OK
;
1672 * There might be more in the queue
1677 if (!NT_STATUS_IS_OK(status
)) {
1681 status
= ctdb_packet_fd_read_sync(conn
->pkt
);
1683 if (NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1685 * There might be more in the queue
1690 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
1691 status
= NT_STATUS_OK
;
1695 if (!NT_STATUS_IS_OK(status
)) {
1696 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status
)));
1697 cluster_fatal("ctdbd died\n");
1707 This is used to canonicalize a ctdb_sock_addr structure.
1709 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1710 struct sockaddr_storage
*out
)
1712 memcpy(out
, in
, sizeof (*out
));
1715 if (in
->ss_family
== AF_INET6
) {
1716 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1717 const struct sockaddr_in6
*in6
=
1718 (const struct sockaddr_in6
*)in
;
1719 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1720 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1721 memset(out
, 0, sizeof(*out
));
1722 #ifdef HAVE_SOCK_SIN_LEN
1723 out4
->sin_len
= sizeof(*out
);
1725 out4
->sin_family
= AF_INET
;
1726 out4
->sin_port
= in6
->sin6_port
;
1727 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1734 * Register us as a server for a particular tcp connection
1737 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1738 const struct sockaddr_storage
*_server
,
1739 const struct sockaddr_storage
*_client
,
1740 bool (*release_ip_handler
)(const char *ip_addr
,
1741 void *private_data
),
1745 * we still use ctdb_control_tcp for ipv4
1746 * because we want to work against older ctdb
1747 * versions at runtime
1749 struct ctdb_control_tcp p4
;
1750 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1751 struct ctdb_control_tcp_addr p
;
1755 struct sockaddr_storage client
;
1756 struct sockaddr_storage server
;
1759 * Only one connection so far
1761 SMB_ASSERT(conn
->release_ip_handler
== NULL
);
1763 smbd_ctdb_canonicalize_ip(_client
, &client
);
1764 smbd_ctdb_canonicalize_ip(_server
, &server
);
1766 switch (client
.ss_family
) {
1768 memcpy(&p4
.dest
, &server
, sizeof(p4
.dest
));
1769 memcpy(&p4
.src
, &client
, sizeof(p4
.src
));
1770 data
.dptr
= (uint8_t *)&p4
;
1771 data
.dsize
= sizeof(p4
);
1773 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1775 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1776 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1777 data
.dptr
= (uint8_t *)&p
;
1778 data
.dsize
= sizeof(p
);
1782 return NT_STATUS_INTERNAL_ERROR
;
1785 conn
->release_ip_handler
= release_ip_handler
;
1786 conn
->release_ip_priv
= private_data
;
1789 * We want to be told about IP releases
1792 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
);
1793 if (!NT_STATUS_IS_OK(status
)) {
1798 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1799 * can send an extra ack to trigger a reset for our client, so it
1800 * immediately reconnects
1802 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1803 CTDB_CONTROL_TCP_CLIENT
, 0,
1804 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1808 * We want to handle reconfigure events
1810 NTSTATUS
ctdbd_register_reconfigure(struct ctdbd_connection
*conn
)
1812 return register_with_ctdbd(conn
, CTDB_SRVID_RECONFIGURE
);
1816 call a control on the local node
1818 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1819 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1820 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1823 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1826 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1828 struct ctdb_client_notify_register reg_data
;
1833 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1835 reg_data
.notify_data
[0] = 0;
1837 struct_len
= offsetof(struct ctdb_client_notify_register
,
1838 notify_data
) + reg_data
.len
;
1840 status
= ctdbd_control_local(
1841 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1842 make_tdb_data((uint8_t *)®_data
, struct_len
),
1843 NULL
, NULL
, &cstatus
);
1844 if (!NT_STATUS_IS_OK(status
)) {
1845 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1846 nt_errstr(status
)));
1851 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1853 struct ctdb_client_notify_deregister dereg_data
;
1857 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1859 status
= ctdbd_control_local(
1860 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1861 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1862 NULL
, NULL
, &cstatus
);
1863 if (!NT_STATUS_IS_OK(status
)) {
1864 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1865 nt_errstr(status
)));
1870 NTSTATUS
ctdbd_probe(void)
1873 * Do a very early check if ctdbd is around to avoid an abort and core
1876 struct ctdbd_connection
*conn
= NULL
;
1879 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1882 * We only care if we can connect.