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"
25 #include "system/select.h"
26 #include "lib/sys_rw_data.h"
27 #include "lib/util/iov_buf.h"
31 /* paths to these include files come from --with-ctdb= in configure */
34 #include "ctdb_private.h"
36 struct ctdbd_srvid_cb
{
38 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
40 const uint8_t *msg
, size_t msglen
,
45 struct ctdbd_connection
{
46 struct messaging_context
*msg_ctx
;
50 struct ctdbd_srvid_cb
*callbacks
;
52 struct tevent_fd
*fde
;
55 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
58 if (conn
->reqid
== 0) {
64 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
65 uint32_t vnn
, uint32_t opcode
,
66 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
67 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
71 * exit on fatal communications errors with the ctdbd daemon
73 static void cluster_fatal(const char *why
)
75 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
76 /* we don't use smb_panic() as we don't want to delay to write
77 a core file. We need to release this process id immediately
78 so that someone else can take over without getting sharing
86 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
88 if (DEBUGLEVEL
< 11) {
91 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
92 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
93 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
94 (int)hdr
->operation
, (int)hdr
->reqid
));
98 * Register a srvid with ctdbd
100 NTSTATUS
register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
101 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
103 const uint8_t *msg
, size_t msglen
,
110 size_t num_callbacks
;
111 struct ctdbd_srvid_cb
*tmp
;
113 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
114 CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
115 tdb_null
, NULL
, NULL
, &cstatus
);
116 if (!NT_STATUS_IS_OK(status
)) {
120 num_callbacks
= talloc_array_length(conn
->callbacks
);
122 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
125 return NT_STATUS_NO_MEMORY
;
127 conn
->callbacks
= tmp
;
129 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
130 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
136 static int ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
137 struct ctdb_req_message
*msg
)
140 size_t i
, num_callbacks
;
142 msg_len
= msg
->hdr
.length
;
143 if (msg_len
< offsetof(struct ctdb_req_message
, data
)) {
144 DEBUG(10, ("%s: len %u too small\n", __func__
,
148 msg_len
-= offsetof(struct ctdb_req_message
, data
);
150 if (msg_len
< msg
->datalen
) {
151 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__
,
152 (unsigned)msg_len
, (unsigned)msg
->datalen
));
156 num_callbacks
= talloc_array_length(conn
->callbacks
);
158 for (i
=0; i
<num_callbacks
; i
++) {
159 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
161 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
164 ret
= cb
->cb(msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
165 msg
->srvid
, msg
->data
, msg
->datalen
,
176 * get our vnn from the cluster
178 static NTSTATUS
get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
182 status
= ctdbd_control(conn
,
183 CTDB_CURRENT_NODE
, CTDB_CONTROL_GET_PNN
, 0, 0,
184 tdb_null
, NULL
, NULL
, &cstatus
);
185 if (!NT_STATUS_IS_OK(status
)) {
186 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
189 *vnn
= (uint32_t)cstatus
;
194 * Are we active (i.e. not banned or stopped?)
196 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
201 struct ctdb_node_map
*m
;
202 uint32_t failure_flags
;
206 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
207 CTDB_CONTROL_GET_NODEMAP
, 0, 0,
208 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
209 if (!NT_STATUS_IS_OK(status
)) {
210 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status
)));
213 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
214 DEBUG(2, ("Received invalid ctdb data\n"));
218 m
= (struct ctdb_node_map
*)outdata
.dptr
;
220 for (i
=0; i
<m
->num
; i
++) {
221 if (vnn
== m
->nodes
[i
].pnn
) {
227 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
232 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
233 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
235 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
236 DEBUG(2, ("Node has status %x, not active\n",
237 (int)m
->nodes
[i
].flags
));
243 TALLOC_FREE(outdata
.dptr
);
247 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
249 return conn
->our_vnn
;
252 const char *lp_ctdbd_socket(void)
256 ret
= lp__ctdbd_socket();
257 if (ret
!= NULL
&& strlen(ret
) > 0) {
265 * Get us a ctdb connection
268 static int ctdbd_connect(int *pfd
)
270 const char *sockname
= lp_ctdbd_socket();
271 struct sockaddr_un addr
= { 0, };
276 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
279 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
283 addr
.sun_family
= AF_UNIX
;
285 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
286 if (namelen
>= sizeof(addr
.sun_path
)) {
287 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
293 salen
= sizeof(struct sockaddr_un
);
295 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
297 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
307 static int ctdb_read_packet(int fd
, TALLOC_CTX
*mem_ctx
,
308 struct ctdb_req_header
**result
)
310 int timeout
= lp_ctdb_timeout();
311 struct ctdb_req_header
*req
;
321 ret
= poll_one_fd(fd
, POLLIN
, timeout
, &revents
);
333 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
341 if (msglen
< sizeof(struct ctdb_req_header
)) {
345 req
= talloc_size(mem_ctx
, msglen
);
349 talloc_set_name_const(req
, "struct ctdb_req_header");
351 req
->length
= msglen
;
353 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
354 msglen
- sizeof(msglen
));
367 * Read a full ctdbd request. If we have a messaging context, defer incoming
368 * messages that might come in between.
371 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
372 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
374 struct ctdb_req_header
*hdr
;
379 ret
= ctdb_read_packet(conn
->fd
, mem_ctx
, &hdr
);
381 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
382 cluster_fatal("ctdbd died\n");
385 DEBUG(11, ("Received ctdb packet\n"));
386 ctdb_packet_dump(hdr
);
388 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
389 struct ctdb_req_message
*msg
= (struct ctdb_req_message
*)hdr
;
391 if (conn
->msg_ctx
== NULL
) {
392 DEBUG(1, ("Got a message without having a msg ctx, "
393 "dropping msg %llu\n",
394 (long long unsigned)msg
->srvid
));
399 ret
= ctdbd_msg_call_back(conn
, msg
);
409 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
410 /* we got the wrong reply */
411 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
412 "been %u\n", hdr
->reqid
, reqid
));
417 *result
= talloc_move(mem_ctx
, &hdr
);
422 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
428 * Get us a ctdbd connection
431 static NTSTATUS
ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
432 struct ctdbd_connection
**pconn
)
434 struct ctdbd_connection
*conn
;
438 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
439 DEBUG(0, ("talloc failed\n"));
440 return NT_STATUS_NO_MEMORY
;
443 ret
= ctdbd_connect(&conn
->fd
);
445 status
= map_nt_error_from_unix(ret
);
446 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
449 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
451 status
= get_cluster_vnn(conn
, &conn
->our_vnn
);
453 if (!NT_STATUS_IS_OK(status
)) {
454 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status
)));
458 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
459 DEBUG(2, ("Node is not working, can not connect\n"));
460 status
= NT_STATUS_INTERNAL_DB_ERROR
;
464 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
465 sizeof(conn
->rand_srvid
));
467 status
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
469 if (!NT_STATUS_IS_OK(status
)) {
470 DEBUG(5, ("Could not register random srvid: %s\n",
484 * Get us a ctdbd connection and register us as a process
487 NTSTATUS
ctdbd_messaging_connection(TALLOC_CTX
*mem_ctx
,
488 struct ctdbd_connection
**pconn
)
490 struct ctdbd_connection
*conn
;
493 status
= ctdbd_init_connection(mem_ctx
, &conn
);
495 if (!NT_STATUS_IS_OK(status
)) {
499 status
= register_with_ctdbd(conn
, MSG_SRVID_SAMBA
, NULL
, NULL
);
500 if (!NT_STATUS_IS_OK(status
)) {
512 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
514 return conn
->msg_ctx
;
517 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
523 * Packet handler to receive and handle a ctdb message
525 static int ctdb_handle_message(struct ctdbd_connection
*conn
,
526 struct ctdb_req_header
*hdr
)
528 struct ctdb_req_message
*msg
;
530 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
531 DEBUG(0, ("Received async msg of type %u, discarding\n",
536 msg
= (struct ctdb_req_message
*)hdr
;
538 ctdbd_msg_call_back(conn
, msg
);
544 * The ctdbd socket is readable asynchronuously
547 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
548 struct tevent_fd
*event
,
552 struct ctdbd_connection
*conn
= talloc_get_type_abort(
553 private_data
, struct ctdbd_connection
);
554 struct ctdb_req_header
*hdr
= NULL
;
557 ret
= ctdb_read_packet(conn
->fd
, talloc_tos(), &hdr
);
559 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
560 cluster_fatal("ctdbd died\n");
563 ret
= ctdb_handle_message(conn
, hdr
);
568 DEBUG(10, ("could not handle incoming message: %s\n",
574 * Prepare a ctdbd connection to receive messages
577 NTSTATUS
ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
578 struct messaging_context
*msg_ctx
)
580 SMB_ASSERT(conn
->msg_ctx
== NULL
);
581 SMB_ASSERT(conn
->fde
== NULL
);
583 if (!(conn
->fde
= tevent_add_fd(messaging_tevent_context(msg_ctx
),
587 ctdbd_socket_handler
,
589 DEBUG(0, ("event_add_fd failed\n"));
590 return NT_STATUS_NO_MEMORY
;
593 conn
->msg_ctx
= msg_ctx
;
598 NTSTATUS
ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
599 uint32_t dst_vnn
, uint64_t dst_srvid
,
600 const struct iovec
*iov
, int iovlen
)
602 struct ctdb_req_message r
;
603 struct iovec iov2
[iovlen
+1];
604 size_t buflen
= iov_buflen(iov
, iovlen
);
607 r
.hdr
.length
= offsetof(struct ctdb_req_message
, data
) + buflen
;
608 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
609 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
610 r
.hdr
.generation
= 1;
611 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
612 r
.hdr
.destnode
= dst_vnn
;
613 r
.hdr
.srcnode
= conn
->our_vnn
;
618 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
619 ctdb_packet_dump(&r
.hdr
);
621 iov2
[0].iov_base
= &r
;
622 iov2
[0].iov_len
= offsetof(struct ctdb_req_message
, data
);
623 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
625 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
626 if (nwritten
== -1) {
627 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
628 cluster_fatal("cluster dispatch daemon msg write error\n");
635 * send/recv a generic ctdb control message
637 static NTSTATUS
ctdbd_control(struct ctdbd_connection
*conn
,
638 uint32_t vnn
, uint32_t opcode
,
639 uint64_t srvid
, uint32_t flags
,
641 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
644 struct ctdb_req_control req
;
645 struct ctdb_req_header
*hdr
;
646 struct ctdb_reply_control
*reply
= NULL
;
647 struct ctdbd_connection
*new_conn
= NULL
;
654 status
= ctdbd_init_connection(NULL
, &new_conn
);
656 if (!NT_STATUS_IS_OK(status
)) {
657 DEBUG(10, ("Could not init temp connection: %s\n",
666 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
) + data
.dsize
;
667 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
668 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
669 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
670 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
671 req
.hdr
.destnode
= vnn
;
674 req
.datalen
= data
.dsize
;
677 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
678 ctdb_packet_dump(&req
.hdr
);
680 iov
[0].iov_base
= &req
;
681 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
682 iov
[1].iov_base
= data
.dptr
;
683 iov
[1].iov_len
= data
.dsize
;
685 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
686 if (nwritten
== -1) {
687 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
688 cluster_fatal("cluster dispatch daemon msg write error\n");
691 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
692 TALLOC_FREE(new_conn
);
699 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
701 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
702 status
= map_nt_error_from_unix(ret
);
706 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
707 DEBUG(0, ("received invalid reply\n"));
710 reply
= (struct ctdb_reply_control
*)hdr
;
713 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
714 mem_ctx
, reply
->data
, reply
->datalen
))) {
716 return NT_STATUS_NO_MEMORY
;
718 outdata
->dsize
= reply
->datalen
;
721 (*cstatus
) = reply
->status
;
724 status
= NT_STATUS_OK
;
727 TALLOC_FREE(new_conn
);
733 * see if a remote process exists
735 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
743 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
744 DEBUG(10, ("ctdb_processes_exist failed\n"));
750 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
751 const struct server_id
*pids
, int num_pids
,
754 TALLOC_CTX
*frame
= talloc_stackframe();
759 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
760 if (reqids
== NULL
) {
764 for (i
=0; i
<num_pids
; i
++) {
765 struct ctdb_req_control req
;
771 reqids
[i
] = ctdbd_next_reqid(conn
);
776 * pids[i].pid is uint64_t, scale down to pid_t which
777 * is the wire protocol towards ctdb.
781 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
782 (int)pids
[i
].vnn
, (int)pid
,
785 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
786 req
.hdr
.length
+= sizeof(pid
);
787 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
788 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
789 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
790 req
.hdr
.reqid
= reqids
[i
];
791 req
.hdr
.destnode
= pids
[i
].vnn
;
792 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
794 req
.datalen
= sizeof(pid
);
797 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
798 ctdb_packet_dump(&req
.hdr
);
800 iov
[0].iov_base
= &req
;
801 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
802 iov
[1].iov_base
= &pid
;
803 iov
[1].iov_len
= sizeof(pid
);
805 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
806 if (nwritten
== -1) {
807 DEBUG(10, ("write_data_iov failed: %s\n",
815 while (num_received
< num_pids
) {
816 struct ctdb_req_header
*hdr
;
817 struct ctdb_reply_control
*reply
;
821 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
823 DEBUG(10, ("ctdb_read_req failed: %s\n",
828 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
829 DEBUG(10, ("Received invalid reply\n"));
832 reply
= (struct ctdb_reply_control
*)hdr
;
834 reqid
= reply
->hdr
.reqid
;
836 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
838 for (i
=0; i
<num_pids
; i
++) {
839 if (reqid
== reqids
[i
]) {
844 DEBUG(10, ("Received unknown record number %u\n",
848 results
[i
] = ((reply
->status
) == 0);
859 struct ctdb_vnn_list
{
865 unsigned *pid_indexes
;
869 * Get a list of all vnns mentioned in a list of
870 * server_ids. vnn_indexes tells where in the vnns array we have to
873 static bool ctdb_collect_vnns(TALLOC_CTX
*mem_ctx
,
874 const struct server_id
*pids
, unsigned num_pids
,
875 struct ctdb_vnn_list
**pvnns
,
878 struct ctdb_vnn_list
*vnns
= NULL
;
879 unsigned *vnn_indexes
= NULL
;
880 unsigned i
, num_vnns
= 0;
882 vnn_indexes
= talloc_array(mem_ctx
, unsigned, num_pids
);
883 if (vnn_indexes
== NULL
) {
884 DEBUG(1, ("talloc_array failed\n"));
888 for (i
=0; i
<num_pids
; i
++) {
890 uint32_t vnn
= pids
[i
].vnn
;
892 for (j
=0; j
<num_vnns
; j
++) {
893 if (vnn
== vnns
[j
].vnn
) {
901 * Already in the array
903 vnns
[j
].num_srvids
+= 1;
906 vnns
= talloc_realloc(mem_ctx
, vnns
, struct ctdb_vnn_list
,
909 DEBUG(1, ("talloc_realloc failed\n"));
912 vnns
[num_vnns
].vnn
= vnn
;
913 vnns
[num_vnns
].num_srvids
= 1;
914 vnns
[num_vnns
].num_filled
= 0;
917 for (i
=0; i
<num_vnns
; i
++) {
918 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
920 vnn
->srvids
= talloc_array(vnns
, uint64_t, vnn
->num_srvids
);
921 if (vnn
->srvids
== NULL
) {
922 DEBUG(1, ("talloc_array failed\n"));
925 vnn
->pid_indexes
= talloc_array(vnns
, unsigned,
927 if (vnn
->pid_indexes
== NULL
) {
928 DEBUG(1, ("talloc_array failed\n"));
932 for (i
=0; i
<num_pids
; i
++) {
933 struct ctdb_vnn_list
*vnn
= &vnns
[vnn_indexes
[i
]];
934 vnn
->srvids
[vnn
->num_filled
] = pids
[i
].unique_id
;
935 vnn
->pid_indexes
[vnn
->num_filled
] = i
;
936 vnn
->num_filled
+= 1;
939 TALLOC_FREE(vnn_indexes
);
941 *pnum_vnns
= num_vnns
;
945 TALLOC_FREE(vnn_indexes
);
949 bool ctdb_serverids_exist_supported(struct ctdbd_connection
*conn
)
954 bool ctdb_serverids_exist(struct ctdbd_connection
*conn
,
955 const struct server_id
*pids
, unsigned num_pids
,
958 unsigned i
, num_received
;
959 struct ctdb_vnn_list
*vnns
= NULL
;
962 if (!ctdb_collect_vnns(talloc_tos(), pids
, num_pids
,
964 DEBUG(1, ("ctdb_collect_vnns failed\n"));
968 for (i
=0; i
<num_vnns
; i
++) {
969 struct ctdb_vnn_list
*vnn
= &vnns
[i
];
970 struct ctdb_req_control req
;
974 vnn
->reqid
= ctdbd_next_reqid(conn
);
978 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
979 (int)vnn
->vnn
, (int)vnn
->reqid
, vnn
->num_srvids
));
981 req
.hdr
.length
= offsetof(struct ctdb_req_control
, data
);
982 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
983 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
984 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
985 req
.hdr
.reqid
= vnn
->reqid
;
986 req
.hdr
.destnode
= vnn
->vnn
;
987 req
.opcode
= CTDB_CONTROL_CHECK_SRVIDS
;
989 req
.datalen
= sizeof(uint64_t) * vnn
->num_srvids
;
990 req
.hdr
.length
+= req
.datalen
;
993 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
994 ctdb_packet_dump(&req
.hdr
);
996 iov
[0].iov_base
= &req
;
997 iov
[0].iov_len
= offsetof(struct ctdb_req_control
, data
);
998 iov
[1].iov_base
= vnn
->srvids
;
999 iov
[1].iov_len
= req
.datalen
;
1001 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1002 if (nwritten
== -1) {
1003 DEBUG(10, ("write_data_iov failed: %s\n",
1011 while (num_received
< num_vnns
) {
1012 struct ctdb_req_header
*hdr
;
1013 struct ctdb_reply_control
*reply
;
1014 struct ctdb_vnn_list
*vnn
;
1016 uint8_t *reply_data
;
1019 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
1021 DEBUG(10, ("ctdb_read_req failed: %s\n",
1026 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
1027 DEBUG(1, ("Received invalid reply %u\n",
1028 (unsigned)hdr
->operation
));
1031 reply
= (struct ctdb_reply_control
*)hdr
;
1033 reqid
= reply
->hdr
.reqid
;
1035 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
1037 for (i
=0; i
<num_vnns
; i
++) {
1038 if (reqid
== vnns
[i
].reqid
) {
1042 if (i
== num_vnns
) {
1043 DEBUG(1, ("Received unknown reqid number %u\n",
1048 DEBUG(10, ("Found index %u\n", i
));
1052 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1053 (unsigned)vnn
->vnn
, vnn
->num_srvids
,
1054 (unsigned)reply
->datalen
));
1056 if (reply
->datalen
>= ((vnn
->num_srvids
+7)/8)) {
1060 reply_data
= reply
->data
;
1063 * Got an error reply
1065 DEBUG(5, ("Received short reply len %d, status %u, "
1067 (unsigned)reply
->datalen
,
1068 (unsigned)reply
->status
,
1069 (unsigned)reply
->errorlen
));
1070 dump_data(5, reply
->data
, reply
->errorlen
);
1073 * This will trigger everything set to false
1078 for (i
=0; i
<vnn
->num_srvids
; i
++) {
1079 int idx
= vnn
->pid_indexes
[i
];
1081 if (pids
[i
].unique_id
==
1082 SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
1083 results
[idx
] = true;
1087 (reply_data
!= NULL
) &&
1088 ((reply_data
[i
/8] & (1<<(i
%8))) != 0);
1098 cluster_fatal("serverids_exist failed");
1105 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
1106 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
1110 TDB_DATA rdata
= {0};
1111 int32_t cstatus
= 0;
1113 data
.dptr
= (uint8_t*)&db_id
;
1114 data
.dsize
= sizeof(db_id
);
1116 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1117 CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
1118 mem_ctx
, &rdata
, &cstatus
);
1119 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1120 DEBUG(0,(__location__
" ctdb_control for getdbpath failed\n"));
1124 return (char *)rdata
.dptr
;
1128 * attach to a ctdb database
1130 NTSTATUS
ctdbd_db_attach(struct ctdbd_connection
*conn
,
1131 const char *name
, uint32_t *db_id
, int tdb_flags
)
1136 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1138 data
= string_term_tdb_data(name
);
1140 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1142 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1143 : CTDB_CONTROL_DB_ATTACH
,
1144 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
1145 if (!NT_STATUS_IS_OK(status
)) {
1146 DEBUG(0, (__location__
" ctdb_control for db_attach "
1147 "failed: %s\n", nt_errstr(status
)));
1151 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
1152 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
1153 return NT_STATUS_INTERNAL_ERROR
;
1156 *db_id
= *(uint32_t *)data
.dptr
;
1157 talloc_free(data
.dptr
);
1159 if (!(tdb_flags
& TDB_SEQNUM
)) {
1160 return NT_STATUS_OK
;
1163 data
.dptr
= (uint8_t *)db_id
;
1164 data
.dsize
= sizeof(*db_id
);
1166 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1167 CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
1168 NULL
, NULL
, &cstatus
);
1169 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
1170 DEBUG(0,(__location__
" ctdb_control for enable seqnum "
1172 return NT_STATUS_IS_OK(status
) ? NT_STATUS_INTERNAL_ERROR
:
1176 return NT_STATUS_OK
;
1180 * force the migration of a record to this node
1182 NTSTATUS
ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
,
1185 struct ctdb_req_call req
;
1186 struct ctdb_req_header
*hdr
;
1187 struct iovec iov
[2];
1194 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1195 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1196 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1197 req
.hdr
.operation
= CTDB_REQ_CALL
;
1198 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1199 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
1200 req
.callid
= CTDB_NULL_FUNC
;
1202 req
.keylen
= key
.dsize
;
1204 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1205 ctdb_packet_dump(&req
.hdr
);
1207 iov
[0].iov_base
= &req
;
1208 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1209 iov
[1].iov_base
= key
.dptr
;
1210 iov
[1].iov_len
= key
.dsize
;
1212 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1213 if (nwritten
== -1) {
1214 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1215 cluster_fatal("cluster dispatch daemon msg write error\n");
1218 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1220 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1221 status
= map_nt_error_from_unix(ret
);
1225 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
1226 DEBUG(0, ("received invalid reply\n"));
1227 status
= NT_STATUS_INTERNAL_ERROR
;
1231 status
= NT_STATUS_OK
;
1239 * Fetch a record and parse it
1241 NTSTATUS
ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1242 TDB_DATA key
, bool local_copy
,
1243 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
1244 void *private_data
),
1247 struct ctdb_req_call req
;
1248 struct ctdb_req_header
*hdr
= NULL
;
1249 struct ctdb_reply_call
*reply
;
1250 struct iovec iov
[2];
1256 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
1260 req
.hdr
.length
= offsetof(struct ctdb_req_call
, data
) + key
.dsize
;
1261 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
1262 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
1263 req
.hdr
.operation
= CTDB_REQ_CALL
;
1264 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
1266 req
.callid
= CTDB_FETCH_FUNC
;
1268 req
.keylen
= key
.dsize
;
1270 iov
[0].iov_base
= &req
;
1271 iov
[0].iov_len
= offsetof(struct ctdb_req_call
, data
);
1272 iov
[1].iov_base
= key
.dptr
;
1273 iov
[1].iov_len
= key
.dsize
;
1275 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
1276 if (nwritten
== -1) {
1277 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
1278 cluster_fatal("cluster dispatch daemon msg write error\n");
1281 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
1283 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
1284 status
= map_nt_error_from_unix(ret
);
1288 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
1289 DEBUG(0, ("received invalid reply\n"));
1290 status
= NT_STATUS_INTERNAL_ERROR
;
1293 reply
= (struct ctdb_reply_call
*)hdr
;
1295 if (reply
->datalen
== 0) {
1297 * Treat an empty record as non-existing
1299 status
= NT_STATUS_NOT_FOUND
;
1303 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1306 status
= NT_STATUS_OK
;
1313 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1314 connection to ctdbd to avoid the hairy recursive and async problems with
1318 NTSTATUS
ctdbd_traverse(uint32_t db_id
,
1319 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1320 void *private_data
),
1323 struct ctdbd_connection
*conn
;
1327 struct ctdb_traverse_start t
;
1331 status
= ctdbd_init_connection(NULL
, &conn
);
1333 if (!NT_STATUS_IS_OK(status
)) {
1334 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1335 nt_errstr(status
)));
1340 t
.srvid
= conn
->rand_srvid
;
1341 t
.reqid
= ctdbd_next_reqid(conn
);
1343 data
.dptr
= (uint8_t *)&t
;
1344 data
.dsize
= sizeof(t
);
1346 status
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1347 CTDB_CONTROL_TRAVERSE_START
, conn
->rand_srvid
, 0,
1348 data
, NULL
, NULL
, &cstatus
);
1350 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1352 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status
),
1355 if (NT_STATUS_IS_OK(status
)) {
1357 * We need a mapping here
1359 status
= NT_STATUS_UNSUCCESSFUL
;
1366 struct ctdb_req_header
*hdr
= NULL
;
1367 struct ctdb_req_message
*m
;
1368 struct ctdb_rec_data
*d
;
1371 ret
= ctdb_read_packet(conn
->fd
, conn
, &hdr
);
1373 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1375 cluster_fatal("ctdbd died\n");
1378 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1379 DEBUG(0, ("Got operation %u, expected a message\n",
1380 (unsigned)hdr
->operation
));
1382 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1385 m
= (struct ctdb_req_message
*)hdr
;
1386 d
= (struct ctdb_rec_data
*)&m
->data
[0];
1387 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1388 DEBUG(0, ("Got invalid traverse data of length %d\n",
1391 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1394 key
.dsize
= d
->keylen
;
1395 key
.dptr
= &d
->data
[0];
1396 data
.dsize
= d
->datalen
;
1397 data
.dptr
= &d
->data
[d
->keylen
];
1399 if (key
.dsize
== 0 && data
.dsize
== 0) {
1400 /* end of traverse */
1402 return NT_STATUS_OK
;
1405 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1406 DEBUG(0, ("Got invalid ltdb header length %d\n",
1409 return NT_STATUS_UNEXPECTED_IO_ERROR
;
1411 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1412 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1415 fn(key
, data
, private_data
);
1418 return NT_STATUS_OK
;
1422 This is used to canonicalize a ctdb_sock_addr structure.
1424 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1425 struct sockaddr_storage
*out
)
1427 memcpy(out
, in
, sizeof (*out
));
1430 if (in
->ss_family
== AF_INET6
) {
1431 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1432 const struct sockaddr_in6
*in6
=
1433 (const struct sockaddr_in6
*)in
;
1434 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1435 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1436 memset(out
, 0, sizeof(*out
));
1437 #ifdef HAVE_SOCK_SIN_LEN
1438 out4
->sin_len
= sizeof(*out
);
1440 out4
->sin_family
= AF_INET
;
1441 out4
->sin_port
= in6
->sin6_port
;
1442 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1449 * Register us as a server for a particular tcp connection
1452 NTSTATUS
ctdbd_register_ips(struct ctdbd_connection
*conn
,
1453 const struct sockaddr_storage
*_server
,
1454 const struct sockaddr_storage
*_client
,
1455 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
1457 const uint8_t *msg
, size_t msglen
,
1458 void *private_data
),
1461 struct ctdb_control_tcp_addr p
;
1462 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1464 struct sockaddr_storage client
;
1465 struct sockaddr_storage server
;
1468 * Only one connection so far
1471 smbd_ctdb_canonicalize_ip(_client
, &client
);
1472 smbd_ctdb_canonicalize_ip(_server
, &server
);
1474 switch (client
.ss_family
) {
1476 memcpy(&p
.dest
.ip
, &server
, sizeof(p
.dest
.ip
));
1477 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1480 memcpy(&p
.dest
.ip6
, &server
, sizeof(p
.dest
.ip6
));
1481 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1484 return NT_STATUS_INTERNAL_ERROR
;
1488 * We want to be told about IP releases
1491 status
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1493 if (!NT_STATUS_IS_OK(status
)) {
1498 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1499 * can send an extra ack to trigger a reset for our client, so it
1500 * immediately reconnects
1502 return ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1503 CTDB_CONTROL_TCP_CLIENT
, 0,
1504 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
, NULL
);
1508 call a control on the local node
1510 NTSTATUS
ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1511 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1512 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1515 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
, mem_ctx
, outdata
, cstatus
);
1518 NTSTATUS
ctdb_watch_us(struct ctdbd_connection
*conn
)
1520 struct ctdb_client_notify_register reg_data
;
1525 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1527 reg_data
.notify_data
[0] = 0;
1529 struct_len
= offsetof(struct ctdb_client_notify_register
,
1530 notify_data
) + reg_data
.len
;
1532 status
= ctdbd_control_local(
1533 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1534 make_tdb_data((uint8_t *)®_data
, struct_len
),
1535 NULL
, NULL
, &cstatus
);
1536 if (!NT_STATUS_IS_OK(status
)) {
1537 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1538 nt_errstr(status
)));
1543 NTSTATUS
ctdb_unwatch(struct ctdbd_connection
*conn
)
1545 struct ctdb_client_notify_deregister dereg_data
;
1549 dereg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1551 status
= ctdbd_control_local(
1552 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1553 make_tdb_data((uint8_t *)&dereg_data
, sizeof(dereg_data
)),
1554 NULL
, NULL
, &cstatus
);
1555 if (!NT_STATUS_IS_OK(status
)) {
1556 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1557 nt_errstr(status
)));
1562 NTSTATUS
ctdbd_probe(void)
1565 * Do a very early check if ctdbd is around to avoid an abort and core
1568 struct ctdbd_connection
*conn
= NULL
;
1571 status
= ctdbd_messaging_connection(talloc_tos(), &conn
);
1574 * We only care if we can connect.