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"
34 /* paths to these include files come from --with-ctdb= in configure */
36 #include "ctdb_private.h"
38 struct ctdbd_srvid_cb
{
40 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
42 const uint8_t *msg
, size_t msglen
,
47 struct ctdbd_connection
{
51 struct ctdbd_srvid_cb
*callbacks
;
56 static uint32_t ctdbd_next_reqid(struct ctdbd_connection
*conn
)
59 if (conn
->reqid
== 0) {
65 static int ctdbd_control(struct ctdbd_connection
*conn
,
66 uint32_t vnn
, uint32_t opcode
,
67 uint64_t srvid
, uint32_t flags
,
69 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
73 * exit on fatal communications errors with the ctdbd daemon
75 static void cluster_fatal(const char *why
)
77 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why
));
78 /* we don't use smb_panic() as we don't want to delay to write
79 a core file. We need to release this process id immediately
80 so that someone else can take over without getting sharing
88 static void ctdb_packet_dump(struct ctdb_req_header
*hdr
)
90 if (DEBUGLEVEL
< 11) {
93 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
94 (int)hdr
->length
, (int)hdr
->ctdb_magic
,
95 (int)hdr
->ctdb_version
, (int)hdr
->generation
,
96 (int)hdr
->operation
, (int)hdr
->reqid
));
100 * Register a srvid with ctdbd
102 int register_with_ctdbd(struct ctdbd_connection
*conn
, uint64_t srvid
,
103 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
105 const uint8_t *msg
, size_t msglen
,
112 size_t num_callbacks
;
113 struct ctdbd_srvid_cb
*tmp
;
115 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
116 tdb_null
, NULL
, NULL
, &cstatus
);
121 num_callbacks
= talloc_array_length(conn
->callbacks
);
123 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
128 conn
->callbacks
= tmp
;
130 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
131 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
137 static int ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
138 struct ctdb_req_message_old
*msg
)
141 size_t i
, num_callbacks
;
143 msg_len
= msg
->hdr
.length
;
144 if (msg_len
< offsetof(struct ctdb_req_message_old
, data
)) {
145 DBG_DEBUG("len %"PRIu32
" too small\n", msg_len
);
148 msg_len
-= offsetof(struct ctdb_req_message_old
, data
);
150 if (msg_len
< msg
->datalen
) {
151 DBG_DEBUG("msg_len=%"PRIu32
" < msg->datalen=%"PRIu32
"\n",
152 msg_len
, 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 int get_cluster_vnn(struct ctdbd_connection
*conn
, uint32_t *vnn
)
182 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GET_PNN
, 0, 0,
183 tdb_null
, NULL
, NULL
, &cstatus
);
185 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
188 *vnn
= (uint32_t)cstatus
;
193 * Are we active (i.e. not banned or stopped?)
195 static bool ctdbd_working(struct ctdbd_connection
*conn
, uint32_t vnn
)
199 struct ctdb_node_map_old
*m
;
204 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GET_NODEMAP
, 0, 0,
205 tdb_null
, talloc_tos(), &outdata
, &cstatus
);
207 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret
)));
210 if ((cstatus
!= 0) || (outdata
.dptr
== NULL
)) {
211 DEBUG(2, ("Received invalid ctdb data\n"));
215 m
= (struct ctdb_node_map_old
*)outdata
.dptr
;
217 for (i
=0; i
<m
->num
; i
++) {
218 if (vnn
== m
->nodes
[i
].pnn
) {
224 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
229 if ((m
->nodes
[i
].flags
& NODE_FLAGS_INACTIVE
) != 0) {
230 DEBUG(2, ("Node has status %x, not active\n",
231 (int)m
->nodes
[i
].flags
));
237 TALLOC_FREE(outdata
.dptr
);
241 uint32_t ctdbd_vnn(const struct ctdbd_connection
*conn
)
243 return conn
->our_vnn
;
247 * Get us a ctdb connection
250 static int ctdbd_connect(const char *sockname
, int *pfd
)
252 struct sockaddr_un addr
= { 0, };
257 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
260 DEBUG(3, ("Could not create socket: %s\n", strerror(err
)));
264 addr
.sun_family
= AF_UNIX
;
266 namelen
= strlcpy(addr
.sun_path
, sockname
, sizeof(addr
.sun_path
));
267 if (namelen
>= sizeof(addr
.sun_path
)) {
268 DEBUG(3, ("%s: Socket name too long: %s\n", __func__
,
274 salen
= sizeof(struct sockaddr_un
);
276 if (connect(fd
, (struct sockaddr
*)(void *)&addr
, salen
) == -1) {
278 DEBUG(1, ("connect(%s) failed: %s\n", sockname
,
288 static int ctdb_read_packet(int fd
, int timeout
, TALLOC_CTX
*mem_ctx
,
289 struct ctdb_req_header
**result
)
291 struct ctdb_req_header
*req
;
296 struct pollfd pfd
= { .fd
= fd
, .events
= POLLIN
};
299 ret
= sys_poll_intr(&pfd
, 1, timeout
);
311 nread
= read_data(fd
, &msglen
, sizeof(msglen
));
319 if (msglen
< sizeof(struct ctdb_req_header
)) {
323 req
= talloc_size(mem_ctx
, msglen
);
327 talloc_set_name_const(req
, "struct ctdb_req_header");
329 req
->length
= msglen
;
331 nread
= read_data(fd
, ((char *)req
) + sizeof(msglen
),
332 msglen
- sizeof(msglen
));
347 * Read a full ctdbd request. If we have a messaging context, defer incoming
348 * messages that might come in between.
351 static int ctdb_read_req(struct ctdbd_connection
*conn
, uint32_t reqid
,
352 TALLOC_CTX
*mem_ctx
, struct ctdb_req_header
**result
)
354 struct ctdb_req_header
*hdr
;
359 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, mem_ctx
, &hdr
);
361 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
362 cluster_fatal("ctdbd died\n");
365 DEBUG(11, ("Received ctdb packet\n"));
366 ctdb_packet_dump(hdr
);
368 if (hdr
->operation
== CTDB_REQ_MESSAGE
) {
369 struct ctdb_req_message_old
*msg
= (struct ctdb_req_message_old
*)hdr
;
371 ret
= ctdbd_msg_call_back(conn
, msg
);
381 if ((reqid
!= 0) && (hdr
->reqid
!= reqid
)) {
382 /* we got the wrong reply */
383 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
384 "been %u\n", hdr
->reqid
, reqid
));
389 *result
= talloc_move(mem_ctx
, &hdr
);
394 static int ctdbd_connection_destructor(struct ctdbd_connection
*c
)
403 * Get us a ctdbd connection
406 static int ctdbd_init_connection_internal(TALLOC_CTX
*mem_ctx
,
407 const char *sockname
, int timeout
,
408 struct ctdbd_connection
*conn
)
412 conn
->timeout
= timeout
;
413 if (conn
->timeout
== 0) {
417 ret
= ctdbd_connect(sockname
, &conn
->fd
);
419 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret
)));
422 talloc_set_destructor(conn
, ctdbd_connection_destructor
);
424 ret
= get_cluster_vnn(conn
, &conn
->our_vnn
);
426 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret
)));
430 if (!ctdbd_working(conn
, conn
->our_vnn
)) {
431 DEBUG(2, ("Node is not working, can not connect\n"));
435 generate_random_buffer((unsigned char *)&conn
->rand_srvid
,
436 sizeof(conn
->rand_srvid
));
438 ret
= register_with_ctdbd(conn
, conn
->rand_srvid
, NULL
, NULL
);
440 DEBUG(5, ("Could not register random srvid: %s\n",
448 int ctdbd_init_connection(TALLOC_CTX
*mem_ctx
,
449 const char *sockname
, int timeout
,
450 struct ctdbd_connection
**pconn
)
452 struct ctdbd_connection
*conn
;
455 if (!(conn
= talloc_zero(mem_ctx
, struct ctdbd_connection
))) {
456 DEBUG(0, ("talloc failed\n"));
460 ret
= ctdbd_init_connection_internal(mem_ctx
,
465 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
478 int ctdbd_reinit_connection(TALLOC_CTX
*mem_ctx
,
479 const char *sockname
, int timeout
,
480 struct ctdbd_connection
*conn
)
484 ret
= ctdbd_connection_destructor(conn
);
486 DBG_ERR("ctdbd_connection_destructor failed\n");
490 ret
= ctdbd_init_connection_internal(mem_ctx
,
495 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
503 int ctdbd_conn_get_fd(struct ctdbd_connection
*conn
)
509 * Packet handler to receive and handle a ctdb message
511 static int ctdb_handle_message(struct ctdbd_connection
*conn
,
512 struct ctdb_req_header
*hdr
)
514 struct ctdb_req_message_old
*msg
;
516 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
517 DEBUG(0, ("Received async msg of type %u, discarding\n",
522 msg
= (struct ctdb_req_message_old
*)hdr
;
524 ctdbd_msg_call_back(conn
, msg
);
529 void ctdbd_socket_readable(struct ctdbd_connection
*conn
)
531 struct ctdb_req_header
*hdr
= NULL
;
534 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, talloc_tos(), &hdr
);
536 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret
)));
537 cluster_fatal("ctdbd died\n");
540 ret
= ctdb_handle_message(conn
, hdr
);
545 DEBUG(10, ("could not handle incoming message: %s\n",
550 int ctdbd_messaging_send_iov(struct ctdbd_connection
*conn
,
551 uint32_t dst_vnn
, uint64_t dst_srvid
,
552 const struct iovec
*iov
, int iovlen
)
554 struct ctdb_req_message_old r
;
555 struct iovec iov2
[iovlen
+1];
556 size_t buflen
= iov_buflen(iov
, iovlen
);
559 r
.hdr
.length
= offsetof(struct ctdb_req_message_old
, data
) + buflen
;
560 r
.hdr
.ctdb_magic
= CTDB_MAGIC
;
561 r
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
562 r
.hdr
.generation
= 1;
563 r
.hdr
.operation
= CTDB_REQ_MESSAGE
;
564 r
.hdr
.destnode
= dst_vnn
;
565 r
.hdr
.srcnode
= conn
->our_vnn
;
570 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
571 ctdb_packet_dump(&r
.hdr
);
573 iov2
[0].iov_base
= &r
;
574 iov2
[0].iov_len
= offsetof(struct ctdb_req_message_old
, data
);
575 memcpy(&iov2
[1], iov
, iovlen
* sizeof(struct iovec
));
577 nwritten
= write_data_iov(conn
->fd
, iov2
, iovlen
+1);
578 if (nwritten
== -1) {
579 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
580 cluster_fatal("cluster dispatch daemon msg write error\n");
587 * send/recv a generic ctdb control message
589 static int ctdbd_control(struct ctdbd_connection
*conn
,
590 uint32_t vnn
, uint32_t opcode
,
591 uint64_t srvid
, uint32_t flags
,
593 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
596 struct ctdb_req_control_old req
;
597 struct ctdb_req_header
*hdr
;
598 struct ctdb_reply_control_old
*reply
= NULL
;
604 req
.hdr
.length
= offsetof(struct ctdb_req_control_old
, data
) + data
.dsize
;
605 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
606 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
607 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
608 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
609 req
.hdr
.destnode
= vnn
;
612 req
.datalen
= data
.dsize
;
615 DBG_DEBUG("Sending ctdb packet reqid=%"PRIu32
", vnn=%"PRIu32
", "
616 "opcode=%"PRIu32
", srvid=%"PRIu64
"\n", req
.hdr
.reqid
,
617 req
.hdr
.destnode
, req
.opcode
, req
.srvid
);
618 ctdb_packet_dump(&req
.hdr
);
620 iov
[0].iov_base
= &req
;
621 iov
[0].iov_len
= offsetof(struct ctdb_req_control_old
, data
);
622 iov
[1].iov_base
= data
.dptr
;
623 iov
[1].iov_len
= data
.dsize
;
625 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
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");
631 if (flags
& CTDB_CTRL_FLAG_NOREPLY
) {
638 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
640 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
644 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
645 DEBUG(0, ("received invalid reply\n"));
649 reply
= (struct ctdb_reply_control_old
*)hdr
;
652 if (!(outdata
->dptr
= (uint8_t *)talloc_memdup(
653 mem_ctx
, reply
->data
, reply
->datalen
))) {
657 outdata
->dsize
= reply
->datalen
;
660 (*cstatus
) = reply
->status
;
668 * see if a remote process exists
670 bool ctdbd_process_exists(struct ctdbd_connection
*conn
, uint32_t vnn
, pid_t pid
)
675 ret
= ctdbd_control(conn
, vnn
, CTDB_CONTROL_PROCESS_EXISTS
, 0, 0,
676 (TDB_DATA
) { .dptr
= (uint8_t *)&pid
,
677 .dsize
= sizeof(pid
) },
678 NULL
, NULL
, &cstatus
);
682 return (cstatus
== 0);
688 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
689 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
693 TDB_DATA rdata
= {0};
696 data
.dptr
= (uint8_t*)&db_id
;
697 data
.dsize
= sizeof(db_id
);
699 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
700 mem_ctx
, &rdata
, &cstatus
);
701 if ((ret
!= 0) || cstatus
!= 0) {
702 DEBUG(0, (__location__
" ctdb_control for getdbpath failed: %s\n",
707 return (char *)rdata
.dptr
;
711 * attach to a ctdb database
713 int ctdbd_db_attach(struct ctdbd_connection
*conn
,
714 const char *name
, uint32_t *db_id
, int tdb_flags
)
719 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
721 data
= string_term_tdb_data(name
);
723 ret
= ctdbd_control_local(conn
,
725 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
726 : CTDB_CONTROL_DB_ATTACH
,
727 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
729 DEBUG(0, (__location__
" ctdb_control for db_attach "
730 "failed: %s\n", strerror(ret
)));
734 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
735 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
739 *db_id
= *(uint32_t *)data
.dptr
;
740 talloc_free(data
.dptr
);
742 if (!(tdb_flags
& TDB_SEQNUM
)) {
746 data
.dptr
= (uint8_t *)db_id
;
747 data
.dsize
= sizeof(*db_id
);
749 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
750 NULL
, NULL
, &cstatus
);
751 if ((ret
!= 0) || cstatus
!= 0) {
752 DEBUG(0, (__location__
" ctdb_control for enable seqnum "
753 "failed: %s\n", strerror(ret
)));
754 return (ret
== 0) ? EIO
: ret
;
761 * force the migration of a record to this node
763 int ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
, TDB_DATA key
)
765 struct ctdb_req_call_old req
;
766 struct ctdb_req_header
*hdr
= NULL
;
773 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
774 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
775 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
776 req
.hdr
.operation
= CTDB_REQ_CALL
;
777 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
778 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
779 req
.callid
= CTDB_NULL_FUNC
;
781 req
.keylen
= key
.dsize
;
783 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
784 ctdb_packet_dump(&req
.hdr
);
786 iov
[0].iov_base
= &req
;
787 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
788 iov
[1].iov_base
= key
.dptr
;
789 iov
[1].iov_len
= key
.dsize
;
791 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
792 if (nwritten
== -1) {
793 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
794 cluster_fatal("cluster dispatch daemon msg write error\n");
797 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
799 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
803 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
804 if (hdr
->operation
== CTDB_REPLY_ERROR
) {
805 DBG_ERR("received error from ctdb\n");
807 DBG_ERR("received invalid reply\n");
820 * Fetch a record and parse it
822 int ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
823 TDB_DATA key
, bool local_copy
,
824 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
828 struct ctdb_req_call_old req
;
829 struct ctdb_req_header
*hdr
= NULL
;
830 struct ctdb_reply_call_old
*reply
;
836 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
840 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
841 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
842 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
843 req
.hdr
.operation
= CTDB_REQ_CALL
;
844 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
846 req
.callid
= CTDB_FETCH_FUNC
;
848 req
.keylen
= key
.dsize
;
850 iov
[0].iov_base
= &req
;
851 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
852 iov
[1].iov_base
= key
.dptr
;
853 iov
[1].iov_len
= key
.dsize
;
855 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
856 if (nwritten
== -1) {
857 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
858 cluster_fatal("cluster dispatch daemon msg write error\n");
861 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
863 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
867 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
868 DEBUG(0, ("received invalid reply\n"));
872 reply
= (struct ctdb_reply_call_old
*)hdr
;
874 if (reply
->datalen
== 0) {
876 * Treat an empty record as non-existing
882 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
892 Traverse a ctdb database. "conn" must be an otherwise unused
893 ctdb_connection where no other messages but the traverse ones are
897 int ctdbd_traverse(struct ctdbd_connection
*conn
, uint32_t db_id
,
898 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
904 struct ctdb_traverse_start t
;
908 t
.srvid
= conn
->rand_srvid
;
909 t
.reqid
= ctdbd_next_reqid(conn
);
911 data
.dptr
= (uint8_t *)&t
;
912 data
.dsize
= sizeof(t
);
914 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_TRAVERSE_START
,
916 0, data
, NULL
, NULL
, &cstatus
);
918 if ((ret
!= 0) || (cstatus
!= 0)) {
919 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret
),
924 * We need a mapping here
932 struct ctdb_req_header
*hdr
= NULL
;
933 struct ctdb_req_message_old
*m
;
934 struct ctdb_rec_data_old
*d
;
936 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, conn
, &hdr
);
938 DEBUG(0, ("ctdb_read_packet failed: %s\n",
940 cluster_fatal("ctdbd died\n");
943 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
944 DEBUG(0, ("Got operation %u, expected a message\n",
945 (unsigned)hdr
->operation
));
949 m
= (struct ctdb_req_message_old
*)hdr
;
950 d
= (struct ctdb_rec_data_old
*)&m
->data
[0];
951 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
952 DEBUG(0, ("Got invalid traverse data of length %d\n",
957 key
.dsize
= d
->keylen
;
958 key
.dptr
= &d
->data
[0];
959 data
.dsize
= d
->datalen
;
960 data
.dptr
= &d
->data
[d
->keylen
];
962 if (key
.dsize
== 0 && data
.dsize
== 0) {
963 /* end of traverse */
967 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
968 DEBUG(0, ("Got invalid ltdb header length %d\n",
972 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
973 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
976 fn(key
, data
, private_data
);
983 This is used to canonicalize a ctdb_sock_addr structure.
985 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
986 struct sockaddr_storage
*out
)
988 memcpy(out
, in
, sizeof (*out
));
991 if (in
->ss_family
== AF_INET6
) {
992 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
993 const struct sockaddr_in6
*in6
=
994 (const struct sockaddr_in6
*)in
;
995 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
996 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
997 memset(out
, 0, sizeof(*out
));
998 #ifdef HAVE_SOCK_SIN_LEN
999 out4
->sin_len
= sizeof(*out
);
1001 out4
->sin_family
= AF_INET
;
1002 out4
->sin_port
= in6
->sin6_port
;
1003 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1010 * Register us as a server for a particular tcp connection
1013 int ctdbd_register_ips(struct ctdbd_connection
*conn
,
1014 const struct sockaddr_storage
*_server
,
1015 const struct sockaddr_storage
*_client
,
1016 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
1018 const uint8_t *msg
, size_t msglen
,
1019 void *private_data
),
1022 struct ctdb_connection p
;
1023 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1025 struct sockaddr_storage client
;
1026 struct sockaddr_storage server
;
1029 * Only one connection so far
1032 smbd_ctdb_canonicalize_ip(_client
, &client
);
1033 smbd_ctdb_canonicalize_ip(_server
, &server
);
1035 switch (client
.ss_family
) {
1037 memcpy(&p
.dst
.ip
, &server
, sizeof(p
.dst
.ip
));
1038 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1041 memcpy(&p
.dst
.ip6
, &server
, sizeof(p
.dst
.ip6
));
1042 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1049 * We want to be told about IP releases
1052 ret
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1059 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1060 * can send an extra ack to trigger a reset for our client, so it
1061 * immediately reconnects
1063 ret
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1064 CTDB_CONTROL_TCP_CLIENT
, 0,
1065 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
,
1074 call a control on the local node
1076 int ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1077 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1078 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1081 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
,
1082 mem_ctx
, outdata
, cstatus
);
1085 int ctdb_watch_us(struct ctdbd_connection
*conn
)
1087 struct ctdb_notify_data_old reg_data
;
1092 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1094 reg_data
.notify_data
[0] = 0;
1096 struct_len
= offsetof(struct ctdb_notify_data_old
,
1097 notify_data
) + reg_data
.len
;
1099 ret
= ctdbd_control_local(
1100 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1101 make_tdb_data((uint8_t *)®_data
, struct_len
),
1102 NULL
, NULL
, &cstatus
);
1104 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1110 int ctdb_unwatch(struct ctdbd_connection
*conn
)
1112 uint64_t srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1116 ret
= ctdbd_control_local(
1117 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1118 make_tdb_data((uint8_t *)&srvid
, sizeof(srvid
)),
1119 NULL
, NULL
, &cstatus
);
1121 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1127 int ctdbd_probe(const char *sockname
, int timeout
)
1130 * Do a very early check if ctdbd is around to avoid an abort and core
1133 struct ctdbd_connection
*conn
= NULL
;
1136 ret
= ctdbd_init_connection(talloc_tos(), sockname
, timeout
,
1140 * We only care if we can connect.