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/util/sys_rw_data.h"
27 #include "lib/util/iov_buf.h"
28 #include "lib/util/select.h"
29 #include "lib/util/debug.h"
30 #include "lib/util/talloc_stack.h"
31 #include "lib/util/genrand.h"
32 #include "lib/util/fault.h"
36 /* paths to these include files come from --with-ctdb= in configure */
38 #include "ctdb_private.h"
40 struct ctdbd_srvid_cb
{
42 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
44 const uint8_t *msg
, size_t msglen
,
49 struct ctdbd_connection
{
50 const char *sockname
; /* Needed in ctdbd_traverse */
51 struct messaging_context
*msg_ctx
;
55 struct ctdbd_srvid_cb
*callbacks
;
57 struct tevent_fd
*fde
;
61 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
64 if (conn
->reqid
== 0) {
70 static int ctdbd_control(struct ctdbd_connection
*conn
,
71 uint32_t vnn
, uint32_t opcode
,
72 uint64_t srvid
, uint32_t flags
,
74 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
78 * exit on fatal communications errors with the ctdbd daemon
80 static void cluster_fatal(const char *why
)
82 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
83 /* we don't use smb_panic() as we don't want to delay to write
84 a core file. We need to release this process id immediately
85 so that someone else can take over without getting sharing
93 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
95 if (DEBUGLEVEL
< 11) {
98 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
99 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
100 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
101 (int)hdr
->operation
, (int)hdr
->reqid
));
105 * Register a srvid with ctdbd
107 int register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
108 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
110 const uint8_t *msg
, size_t msglen
,
117 size_t num_callbacks
;
118 struct ctdbd_srvid_cb
*tmp
;
120 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
121 tdb_null
, NULL
, NULL
, &cstatus
);
126 num_callbacks
= talloc_array_length(conn
->callbacks
);
128 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
133 conn
->callbacks
= tmp
;
135 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
136 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
142 static int ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
143 struct ctdb_req_message_old
*msg
)
146 size_t i
, num_callbacks
;
148 msg_len
= msg
->hdr
.length
;
149 if (msg_len
< offsetof(struct ctdb_req_message_old
, data
)) {
150 DBG_DEBUG("len %"PRIu32
" too small\n", msg_len
);
153 msg_len
-= offsetof(struct ctdb_req_message_old
, data
);
155 if (msg_len
< msg
->datalen
) {
156 DBG_DEBUG("msg_len=%"PRIu32
" < msg->datalen=%"PRIu32
"\n",
157 msg_len
, msg
->datalen
);
161 num_callbacks
= talloc_array_length(conn
->callbacks
);
163 for (i
=0; i
<num_callbacks
; i
++) {
164 struct ctdbd_srvid_cb
*cb
= &conn
->callbacks
[i
];
166 if ((cb
->srvid
== msg
->srvid
) && (cb
->cb
!= NULL
)) {
169 ret
= cb
->cb(msg
->hdr
.srcnode
, msg
->hdr
.destnode
,
170 msg
->srvid
, msg
->data
, msg
->datalen
,
181 * get our vnn from the cluster
183 static int get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
187 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GET_PNN
, 0, 0,
188 tdb_null
, NULL
, NULL
, &cstatus
);
190 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
193 *vnn
= (uint32_t)cstatus
;
198 * Are we active (i.e. not banned or stopped?)
200 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
204 struct ctdb_node_map_old
*m
;
205 uint32_t failure_flags
;
210 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GET_NODEMAP
, 0, 0,
211 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
213 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
216 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
217 DEBUG(2, ("Received invalid ctdb data\n"));
221 m
= (struct ctdb_node_map_old
*)outdata
.dptr
;
223 for (i
=0; i
<m
->num
; i
++) {
224 if (vnn
== m
->nodes
[i
].pnn
) {
230 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
235 failure_flags
= NODE_FLAGS_BANNED
| NODE_FLAGS_DISCONNECTED
236 | NODE_FLAGS_PERMANENTLY_DISABLED
| NODE_FLAGS_STOPPED
;
238 if ((m
->nodes
[i
].flags
& failure_flags
) != 0) {
239 DEBUG(2, ("Node has status %x, not active\n",
240 (int)m
->nodes
[i
].flags
));
246 TALLOC_FREE(outdata
.dptr
);
250 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
252 return conn
->our_vnn
;
256 * Get us a ctdb connection
259 static int ctdbd_connect(const char *sockname
, int *pfd
)
261 struct sockaddr_un addr
= { 0, };
266 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
269 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
273 addr
.sun_family
= AF_UNIX
;
275 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
276 if (namelen
>= sizeof(addr
.sun_path
)) {
277 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
283 salen
= sizeof(struct sockaddr_un
);
285 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
287 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
297 static int ctdb_read_packet(int fd
, int timeout
, TALLOC_CTX
*mem_ctx
,
298 struct ctdb_req_header
**result
)
300 struct ctdb_req_header
*req
;
305 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
308 ret
= sys_poll_intr(&pfd
, 1, timeout
);
320 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
328 if (msglen
< sizeof(struct ctdb_req_header
)) {
332 req
= talloc_size(mem_ctx
, msglen
);
336 talloc_set_name_const(req
, "struct ctdb_req_header");
338 req
->length
= msglen
;
340 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
341 msglen
- sizeof(msglen
));
356 * Read a full ctdbd request. If we have a messaging context, defer incoming
357 * messages that might come in between.
360 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
361 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
363 struct ctdb_req_header
*hdr
;
368 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, mem_ctx
, &hdr
);
370 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
371 cluster_fatal("ctdbd died\n");
374 DEBUG(11, ("Received ctdb packet\n"));
375 ctdb_packet_dump(hdr
);
377 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
378 struct ctdb_req_message_old
*msg
= (struct ctdb_req_message_old
*)hdr
;
380 if (conn
->msg_ctx
== NULL
) {
381 DEBUG(1, ("Got a message without having a msg ctx, "
382 "dropping msg %llu\n",
383 (long long unsigned)msg
->srvid
));
388 ret
= ctdbd_msg_call_back(conn
, msg
);
398 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
399 /* we got the wrong reply */
400 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
401 "been %u\n", hdr
->reqid
, reqid
));
406 *result
= talloc_move(mem_ctx
, &hdr
);
411 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
421 * Get us a ctdbd connection
424 int ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
425 const char *sockname
, int timeout
,
426 struct ctdbd_connection
**pconn
)
428 struct ctdbd_connection
*conn
;
431 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
432 DEBUG(0, ("talloc failed\n"));
436 conn
->sockname
= talloc_strdup(conn
, sockname
);
437 if (conn
->sockname
== NULL
) {
438 DBG_ERR("talloc failed\n");
443 conn
->timeout
= timeout
;
445 if (conn
->timeout
== 0) {
449 ret
= ctdbd_connect(conn
->sockname
, &conn
->fd
);
451 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
454 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
456 ret
= get_cluster_vnn(conn
, &conn
->our_vnn
);
459 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret
)));
463 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
464 DEBUG(2, ("Node is not working, can not connect\n"));
469 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
470 sizeof(conn
->rand_srvid
));
472 ret
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
475 DEBUG(5, ("Could not register random srvid: %s\n",
488 struct messaging_context
*ctdb_conn_msg_ctx(struct ctdbd_connection
*conn
)
490 return conn
->msg_ctx
;
493 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
499 * Packet handler to receive and handle a ctdb message
501 static int ctdb_handle_message(struct ctdbd_connection
*conn
,
502 struct ctdb_req_header
*hdr
)
504 struct ctdb_req_message_old
*msg
;
506 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
507 DEBUG(0, ("Received async msg of type %u, discarding\n",
512 msg
= (struct ctdb_req_message_old
*)hdr
;
514 ctdbd_msg_call_back(conn
, msg
);
520 * The ctdbd socket is readable asynchronuously
523 static void ctdbd_socket_handler(struct tevent_context
*event_ctx
,
524 struct tevent_fd
*event
,
528 struct ctdbd_connection
*conn
= talloc_get_type_abort(
529 private_data
, struct ctdbd_connection
);
530 struct ctdb_req_header
*hdr
= NULL
;
533 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, talloc_tos(), &hdr
);
535 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
536 cluster_fatal("ctdbd died\n");
539 ret
= ctdb_handle_message(conn
, hdr
);
544 DEBUG(10, ("could not handle incoming message: %s\n",
550 * Prepare a ctdbd connection to receive messages
553 int ctdbd_register_msg_ctx(struct ctdbd_connection
*conn
,
554 struct messaging_context
*msg_ctx
,
555 struct tevent_context
*ev
)
557 SMB_ASSERT(conn
->msg_ctx
== NULL
);
558 SMB_ASSERT(conn
->fde
== NULL
);
560 conn
->fde
= tevent_add_fd(ev
, conn
, conn
->fd
, TEVENT_FD_READ
,
561 ctdbd_socket_handler
, conn
);
562 if (conn
->fde
== NULL
) {
563 DEBUG(0, ("event_add_fd failed\n"));
567 conn
->msg_ctx
= msg_ctx
;
572 int ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
573 uint32_t dst_vnn
, uint64_t dst_srvid
,
574 const struct iovec
*iov
, int iovlen
)
576 struct ctdb_req_message_old r
;
577 struct iovec iov2
[iovlen
+1];
578 size_t buflen
= iov_buflen(iov
, iovlen
);
581 r
.hdr
.length
= offsetof(struct ctdb_req_message_old
, data
) + buflen
;
582 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
583 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
584 r
.hdr
.generation
= 1;
585 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
586 r
.hdr
.destnode
= dst_vnn
;
587 r
.hdr
.srcnode
= conn
->our_vnn
;
592 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
593 ctdb_packet_dump(&r
.hdr
);
595 iov2
[0].iov_base
= &r
;
596 iov2
[0].iov_len
= offsetof(struct ctdb_req_message_old
, data
);
597 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
599 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
600 if (nwritten
== -1) {
601 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
602 cluster_fatal("cluster dispatch daemon msg write error\n");
609 * send/recv a generic ctdb control message
611 static int ctdbd_control(struct ctdbd_connection
*conn
,
612 uint32_t vnn
, uint32_t opcode
,
613 uint64_t srvid
, uint32_t flags
,
615 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
618 struct ctdb_req_control_old req
;
619 struct ctdb_req_header
*hdr
;
620 struct ctdb_reply_control_old
*reply
= NULL
;
626 req
.hdr
.length
= offsetof(struct ctdb_req_control_old
, data
) + data
.dsize
;
627 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
628 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
629 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
630 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
631 req
.hdr
.destnode
= vnn
;
634 req
.datalen
= data
.dsize
;
637 DBG_DEBUG("Sending ctdb packet reqid=%"PRIu32
", vnn=%"PRIu32
", "
638 "opcode=%"PRIu32
", srvid=%"PRIu64
"\n", req
.hdr
.reqid
,
639 req
.hdr
.destnode
, req
.opcode
, req
.srvid
);
640 ctdb_packet_dump(&req
.hdr
);
642 iov
[0].iov_base
= &req
;
643 iov
[0].iov_len
= offsetof(struct ctdb_req_control_old
, data
);
644 iov
[1].iov_base
= data
.dptr
;
645 iov
[1].iov_len
= data
.dsize
;
647 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
648 if (nwritten
== -1) {
649 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
650 cluster_fatal("cluster dispatch daemon msg write error\n");
653 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
660 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
662 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
666 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
667 DEBUG(0, ("received invalid reply\n"));
671 reply
= (struct ctdb_reply_control_old
*)hdr
;
674 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
675 mem_ctx
, reply
->data
, reply
->datalen
))) {
679 outdata
->dsize
= reply
->datalen
;
682 (*cstatus
) = reply
->status
;
690 * see if a remote process exists
692 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
697 ret
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
, 0, 0,
698 (TDB_DATA
) { .dptr
= (uint8_t *)&pid
,
699 .dsize
= sizeof(pid
) },
700 NULL
, NULL
, &cstatus
);
704 return (cstatus
== 0);
710 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
711 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
715 TDB_DATA rdata
= {0};
718 data
.dptr
= (uint8_t*)&db_id
;
719 data
.dsize
= sizeof(db_id
);
721 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
722 mem_ctx
, &rdata
, &cstatus
);
723 if ((ret
!= 0) || cstatus
!= 0) {
724 DEBUG(0, (__location__
" ctdb_control for getdbpath failed: %s\n",
729 return (char *)rdata
.dptr
;
733 * attach to a ctdb database
735 int ctdbd_db_attach(struct ctdbd_connection
*conn
,
736 const char *name
, uint32_t *db_id
, int tdb_flags
)
741 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
743 data
= string_term_tdb_data(name
);
745 ret
= ctdbd_control_local(conn
,
747 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
748 : CTDB_CONTROL_DB_ATTACH
,
749 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
751 DEBUG(0, (__location__
" ctdb_control for db_attach "
752 "failed: %s\n", strerror(ret
)));
756 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
757 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
761 *db_id
= *(uint32_t *)data
.dptr
;
762 talloc_free(data
.dptr
);
764 if (!(tdb_flags
& TDB_SEQNUM
)) {
768 data
.dptr
= (uint8_t *)db_id
;
769 data
.dsize
= sizeof(*db_id
);
771 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
772 NULL
, NULL
, &cstatus
);
773 if ((ret
!= 0) || cstatus
!= 0) {
774 DEBUG(0, (__location__
" ctdb_control for enable seqnum "
775 "failed: %s\n", strerror(ret
)));
776 return (ret
== 0) ? EIO
: ret
;
783 * force the migration of a record to this node
785 int ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
, TDB_DATA key
)
787 struct ctdb_req_call_old req
;
788 struct ctdb_req_header
*hdr
;
795 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
796 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
797 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
798 req
.hdr
.operation
= CTDB_REQ_CALL
;
799 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
800 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
801 req
.callid
= CTDB_NULL_FUNC
;
803 req
.keylen
= key
.dsize
;
805 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
806 ctdb_packet_dump(&req
.hdr
);
808 iov
[0].iov_base
= &req
;
809 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
810 iov
[1].iov_base
= key
.dptr
;
811 iov
[1].iov_len
= key
.dsize
;
813 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
814 if (nwritten
== -1) {
815 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
816 cluster_fatal("cluster dispatch daemon msg write error\n");
819 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
821 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
825 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
826 DEBUG(0, ("received invalid reply\n"));
837 * Fetch a record and parse it
839 int ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
840 TDB_DATA key
, bool local_copy
,
841 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
845 struct ctdb_req_call_old req
;
846 struct ctdb_req_header
*hdr
= NULL
;
847 struct ctdb_reply_call_old
*reply
;
853 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
857 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
858 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
859 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
860 req
.hdr
.operation
= CTDB_REQ_CALL
;
861 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
863 req
.callid
= CTDB_FETCH_FUNC
;
865 req
.keylen
= key
.dsize
;
867 iov
[0].iov_base
= &req
;
868 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
869 iov
[1].iov_base
= key
.dptr
;
870 iov
[1].iov_len
= key
.dsize
;
872 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
873 if (nwritten
== -1) {
874 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
875 cluster_fatal("cluster dispatch daemon msg write error\n");
878 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
880 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
884 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
885 DEBUG(0, ("received invalid reply\n"));
889 reply
= (struct ctdb_reply_call_old
*)hdr
;
891 if (reply
->datalen
== 0) {
893 * Treat an empty record as non-existing
899 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
909 Traverse a ctdb database. "conn" must be an otherwise unused
910 ctdb_connection where no other messages but the traverse ones are
914 int ctdbd_traverse(struct ctdbd_connection
*conn
, uint32_t db_id
,
915 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
921 struct ctdb_traverse_start t
;
925 t
.srvid
= conn
->rand_srvid
;
926 t
.reqid
= ctdbd_next_reqid(conn
);
928 data
.dptr
= (uint8_t *)&t
;
929 data
.dsize
= sizeof(t
);
931 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_TRAVERSE_START
,
933 0, data
, NULL
, NULL
, &cstatus
);
935 if ((ret
!= 0) || (cstatus
!= 0)) {
936 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret
),
941 * We need a mapping here
949 struct ctdb_req_header
*hdr
= NULL
;
950 struct ctdb_req_message_old
*m
;
951 struct ctdb_rec_data_old
*d
;
953 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, conn
, &hdr
);
955 DEBUG(0, ("ctdb_read_packet failed: %s\n",
957 cluster_fatal("ctdbd died\n");
960 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
961 DEBUG(0, ("Got operation %u, expected a message\n",
962 (unsigned)hdr
->operation
));
966 m
= (struct ctdb_req_message_old
*)hdr
;
967 d
= (struct ctdb_rec_data_old
*)&m
->data
[0];
968 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
969 DEBUG(0, ("Got invalid traverse data of length %d\n",
974 key
.dsize
= d
->keylen
;
975 key
.dptr
= &d
->data
[0];
976 data
.dsize
= d
->datalen
;
977 data
.dptr
= &d
->data
[d
->keylen
];
979 if (key
.dsize
== 0 && data
.dsize
== 0) {
980 /* end of traverse */
984 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
985 DEBUG(0, ("Got invalid ltdb header length %d\n",
989 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
990 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
993 fn(key
, data
, private_data
);
1000 This is used to canonicalize a ctdb_sock_addr structure.
1002 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1003 struct sockaddr_storage
*out
)
1005 memcpy(out
, in
, sizeof (*out
));
1008 if (in
->ss_family
== AF_INET6
) {
1009 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1010 const struct sockaddr_in6
*in6
=
1011 (const struct sockaddr_in6
*)in
;
1012 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1013 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1014 memset(out
, 0, sizeof(*out
));
1015 #ifdef HAVE_SOCK_SIN_LEN
1016 out4
->sin_len
= sizeof(*out
);
1018 out4
->sin_family
= AF_INET
;
1019 out4
->sin_port
= in6
->sin6_port
;
1020 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1027 * Register us as a server for a particular tcp connection
1030 int ctdbd_register_ips(struct ctdbd_connection
*conn
,
1031 const struct sockaddr_storage
*_server
,
1032 const struct sockaddr_storage
*_client
,
1033 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
1035 const uint8_t *msg
, size_t msglen
,
1036 void *private_data
),
1039 struct ctdb_connection p
;
1040 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1042 struct sockaddr_storage client
;
1043 struct sockaddr_storage server
;
1046 * Only one connection so far
1049 smbd_ctdb_canonicalize_ip(_client
, &client
);
1050 smbd_ctdb_canonicalize_ip(_server
, &server
);
1052 switch (client
.ss_family
) {
1054 memcpy(&p
.dst
.ip
, &server
, sizeof(p
.dst
.ip
));
1055 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1058 memcpy(&p
.dst
.ip6
, &server
, sizeof(p
.dst
.ip6
));
1059 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1066 * We want to be told about IP releases
1069 ret
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1076 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1077 * can send an extra ack to trigger a reset for our client, so it
1078 * immediately reconnects
1080 ret
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1081 CTDB_CONTROL_TCP_CLIENT
, 0,
1082 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
,
1091 call a control on the local node
1093 int ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1094 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1095 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1098 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
,
1099 mem_ctx
, outdata
, cstatus
);
1102 int ctdb_watch_us(struct ctdbd_connection
*conn
)
1104 struct ctdb_notify_data_old reg_data
;
1109 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1111 reg_data
.notify_data
[0] = 0;
1113 struct_len
= offsetof(struct ctdb_notify_data_old
,
1114 notify_data
) + reg_data
.len
;
1116 ret
= ctdbd_control_local(
1117 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1118 make_tdb_data((uint8_t *)®_data
, struct_len
),
1119 NULL
, NULL
, &cstatus
);
1121 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1127 int ctdb_unwatch(struct ctdbd_connection
*conn
)
1129 uint64_t srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1133 ret
= ctdbd_control_local(
1134 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1135 make_tdb_data((uint8_t *)&srvid
, sizeof(srvid
)),
1136 NULL
, NULL
, &cstatus
);
1138 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1144 int ctdbd_probe(const char *sockname
, int timeout
)
1147 * Do a very early check if ctdbd is around to avoid an abort and core
1150 struct ctdbd_connection
*conn
= NULL
;
1153 ret
= ctdbd_init_connection(talloc_tos(), sockname
, timeout
,
1157 * We only care if we can connect.