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
,
116 size_t num_callbacks
;
117 struct ctdbd_srvid_cb
*tmp
;
119 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_REGISTER_SRVID
, srvid
, 0,
120 tdb_null
, NULL
, NULL
, &cstatus
);
125 num_callbacks
= talloc_array_length(conn
->callbacks
);
127 tmp
= talloc_realloc(conn
, conn
->callbacks
, struct ctdbd_srvid_cb
,
132 conn
->callbacks
= tmp
;
134 conn
->callbacks
[num_callbacks
] = (struct ctdbd_srvid_cb
) {
135 .srvid
= srvid
, .cb
= cb
, .private_data
= private_data
141 static int ctdbd_msg_call_back(struct ctdbd_connection
*conn
,
142 struct ctdb_req_message_old
*msg
)
145 size_t i
, num_callbacks
;
147 msg_len
= msg
->hdr
.length
;
148 if (msg_len
< offsetof(struct ctdb_req_message_old
, data
)) {
149 DEBUG(10, ("%s: len %u too small\n", __func__
,
153 msg_len
-= offsetof(struct ctdb_req_message_old
, data
);
155 if (msg_len
< msg
->datalen
) {
156 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__
,
157 (unsigned)msg_len
, (unsigned)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
)
700 if (!ctdb_processes_exist(conn
, &id
, 1, &result
)) {
701 DEBUG(10, ("ctdb_processes_exist failed\n"));
707 bool ctdb_processes_exist(struct ctdbd_connection
*conn
,
708 const struct server_id
*pids
, int num_pids
,
711 TALLOC_CTX
*frame
= talloc_stackframe();
716 reqids
= talloc_array(talloc_tos(), uint32_t, num_pids
);
717 if (reqids
== NULL
) {
721 for (i
=0; i
<num_pids
; i
++) {
722 struct ctdb_req_control_old req
;
728 reqids
[i
] = ctdbd_next_reqid(conn
);
733 * pids[i].pid is uint64_t, scale down to pid_t which
734 * is the wire protocol towards ctdb.
738 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
739 (int)pids
[i
].vnn
, (int)pid
,
742 req
.hdr
.length
= offsetof(struct ctdb_req_control_old
, data
);
743 req
.hdr
.length
+= sizeof(pid
);
744 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
745 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
746 req
.hdr
.operation
= CTDB_REQ_CONTROL
;
747 req
.hdr
.reqid
= reqids
[i
];
748 req
.hdr
.destnode
= pids
[i
].vnn
;
749 req
.opcode
= CTDB_CONTROL_PROCESS_EXISTS
;
751 req
.datalen
= sizeof(pid
);
754 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
755 ctdb_packet_dump(&req
.hdr
);
757 iov
[0].iov_base
= &req
;
758 iov
[0].iov_len
= offsetof(struct ctdb_req_control_old
, data
);
759 iov
[1].iov_base
= &pid
;
760 iov
[1].iov_len
= sizeof(pid
);
762 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
763 if (nwritten
== -1) {
764 DEBUG(10, ("write_data_iov failed: %s\n",
772 while (num_received
< num_pids
) {
773 struct ctdb_req_header
*hdr
;
774 struct ctdb_reply_control_old
*reply
;
778 ret
= ctdb_read_req(conn
, 0, talloc_tos(), &hdr
);
780 DEBUG(10, ("ctdb_read_req failed: %s\n",
785 if (hdr
->operation
!= CTDB_REPLY_CONTROL
) {
786 DEBUG(10, ("Received invalid reply\n"));
789 reply
= (struct ctdb_reply_control_old
*)hdr
;
791 reqid
= reply
->hdr
.reqid
;
793 DEBUG(10, ("Received reqid %d\n", (int)reqid
));
795 for (i
=0; i
<num_pids
; i
++) {
796 if (reqid
== reqids
[i
]) {
801 DEBUG(10, ("Received unknown record number %u\n",
805 results
[i
] = ((reply
->status
) == 0);
819 char *ctdbd_dbpath(struct ctdbd_connection
*conn
,
820 TALLOC_CTX
*mem_ctx
, uint32_t db_id
)
824 TDB_DATA rdata
= {0};
827 data
.dptr
= (uint8_t*)&db_id
;
828 data
.dsize
= sizeof(db_id
);
830 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_GETDBPATH
, 0, 0, data
,
831 mem_ctx
, &rdata
, &cstatus
);
832 if ((ret
!= 0) || cstatus
!= 0) {
833 DEBUG(0, (__location__
" ctdb_control for getdbpath failed: %s\n",
838 return (char *)rdata
.dptr
;
842 * attach to a ctdb database
844 int ctdbd_db_attach(struct ctdbd_connection
*conn
,
845 const char *name
, uint32_t *db_id
, int tdb_flags
)
850 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
852 data
= string_term_tdb_data(name
);
854 ret
= ctdbd_control_local(conn
,
856 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
857 : CTDB_CONTROL_DB_ATTACH
,
858 tdb_flags
, 0, data
, NULL
, &data
, &cstatus
);
860 DEBUG(0, (__location__
" ctdb_control for db_attach "
861 "failed: %s\n", strerror(ret
)));
865 if (cstatus
!= 0 || data
.dsize
!= sizeof(uint32_t)) {
866 DEBUG(0,(__location__
" ctdb_control for db_attach failed\n"));
870 *db_id
= *(uint32_t *)data
.dptr
;
871 talloc_free(data
.dptr
);
873 if (!(tdb_flags
& TDB_SEQNUM
)) {
877 data
.dptr
= (uint8_t *)db_id
;
878 data
.dsize
= sizeof(*db_id
);
880 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_ENABLE_SEQNUM
, 0, 0, data
,
881 NULL
, NULL
, &cstatus
);
882 if ((ret
!= 0) || cstatus
!= 0) {
883 DEBUG(0, (__location__
" ctdb_control for enable seqnum "
884 "failed: %s\n", strerror(ret
)));
885 return (ret
== 0) ? EIO
: ret
;
892 * force the migration of a record to this node
894 int ctdbd_migrate(struct ctdbd_connection
*conn
, uint32_t db_id
, TDB_DATA key
)
896 struct ctdb_req_call_old req
;
897 struct ctdb_req_header
*hdr
;
904 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
905 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
906 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
907 req
.hdr
.operation
= CTDB_REQ_CALL
;
908 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
909 req
.flags
= CTDB_IMMEDIATE_MIGRATION
;
910 req
.callid
= CTDB_NULL_FUNC
;
912 req
.keylen
= key
.dsize
;
914 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
915 ctdb_packet_dump(&req
.hdr
);
917 iov
[0].iov_base
= &req
;
918 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
919 iov
[1].iov_base
= key
.dptr
;
920 iov
[1].iov_len
= key
.dsize
;
922 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
923 if (nwritten
== -1) {
924 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
925 cluster_fatal("cluster dispatch daemon msg write error\n");
928 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
930 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
934 if (hdr
->operation
!= CTDB_REPLY_CALL
) {
935 DEBUG(0, ("received invalid reply\n"));
946 * Fetch a record and parse it
948 int ctdbd_parse(struct ctdbd_connection
*conn
, uint32_t db_id
,
949 TDB_DATA key
, bool local_copy
,
950 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
954 struct ctdb_req_call_old req
;
955 struct ctdb_req_header
*hdr
= NULL
;
956 struct ctdb_reply_call_old
*reply
;
962 flags
= local_copy
? CTDB_WANT_READONLY
: 0;
966 req
.hdr
.length
= offsetof(struct ctdb_req_call_old
, data
) + key
.dsize
;
967 req
.hdr
.ctdb_magic
= CTDB_MAGIC
;
968 req
.hdr
.ctdb_version
= CTDB_PROTOCOL
;
969 req
.hdr
.operation
= CTDB_REQ_CALL
;
970 req
.hdr
.reqid
= ctdbd_next_reqid(conn
);
972 req
.callid
= CTDB_FETCH_FUNC
;
974 req
.keylen
= key
.dsize
;
976 iov
[0].iov_base
= &req
;
977 iov
[0].iov_len
= offsetof(struct ctdb_req_call_old
, data
);
978 iov
[1].iov_base
= key
.dptr
;
979 iov
[1].iov_len
= key
.dsize
;
981 nwritten
= write_data_iov(conn
->fd
, iov
, ARRAY_SIZE(iov
));
982 if (nwritten
== -1) {
983 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno
)));
984 cluster_fatal("cluster dispatch daemon msg write error\n");
987 ret
= ctdb_read_req(conn
, req
.hdr
.reqid
, NULL
, &hdr
);
989 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret
)));
993 if ((hdr
== NULL
) || (hdr
->operation
!= CTDB_REPLY_CALL
)) {
994 DEBUG(0, ("received invalid reply\n"));
998 reply
= (struct ctdb_reply_call_old
*)hdr
;
1000 if (reply
->datalen
== 0) {
1002 * Treat an empty record as non-existing
1008 parser(key
, make_tdb_data(&reply
->data
[0], reply
->datalen
),
1018 Traverse a ctdb database. "conn" must be an otherwise unused
1019 ctdb_connection where no other messages but the traverse ones are
1023 int ctdbd_traverse(struct ctdbd_connection
*conn
, uint32_t db_id
,
1024 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1025 void *private_data
),
1030 struct ctdb_traverse_start t
;
1034 t
.srvid
= conn
->rand_srvid
;
1035 t
.reqid
= ctdbd_next_reqid(conn
);
1037 data
.dptr
= (uint8_t *)&t
;
1038 data
.dsize
= sizeof(t
);
1040 ret
= ctdbd_control_local(conn
, CTDB_CONTROL_TRAVERSE_START
,
1042 0, data
, NULL
, NULL
, &cstatus
);
1044 if ((ret
!= 0) || (cstatus
!= 0)) {
1045 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret
),
1050 * We need a mapping here
1058 struct ctdb_req_header
*hdr
= NULL
;
1059 struct ctdb_req_message_old
*m
;
1060 struct ctdb_rec_data_old
*d
;
1062 ret
= ctdb_read_packet(conn
->fd
, conn
->timeout
, conn
, &hdr
);
1064 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1066 cluster_fatal("ctdbd died\n");
1069 if (hdr
->operation
!= CTDB_REQ_MESSAGE
) {
1070 DEBUG(0, ("Got operation %u, expected a message\n",
1071 (unsigned)hdr
->operation
));
1075 m
= (struct ctdb_req_message_old
*)hdr
;
1076 d
= (struct ctdb_rec_data_old
*)&m
->data
[0];
1077 if (m
->datalen
< sizeof(uint32_t) || m
->datalen
!= d
->length
) {
1078 DEBUG(0, ("Got invalid traverse data of length %d\n",
1083 key
.dsize
= d
->keylen
;
1084 key
.dptr
= &d
->data
[0];
1085 data
.dsize
= d
->datalen
;
1086 data
.dptr
= &d
->data
[d
->keylen
];
1088 if (key
.dsize
== 0 && data
.dsize
== 0) {
1089 /* end of traverse */
1093 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1094 DEBUG(0, ("Got invalid ltdb header length %d\n",
1098 data
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1099 data
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1102 fn(key
, data
, private_data
);
1109 This is used to canonicalize a ctdb_sock_addr structure.
1111 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage
*in
,
1112 struct sockaddr_storage
*out
)
1114 memcpy(out
, in
, sizeof (*out
));
1117 if (in
->ss_family
== AF_INET6
) {
1118 const char prefix
[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1119 const struct sockaddr_in6
*in6
=
1120 (const struct sockaddr_in6
*)in
;
1121 struct sockaddr_in
*out4
= (struct sockaddr_in
*)out
;
1122 if (memcmp(&in6
->sin6_addr
, prefix
, 12) == 0) {
1123 memset(out
, 0, sizeof(*out
));
1124 #ifdef HAVE_SOCK_SIN_LEN
1125 out4
->sin_len
= sizeof(*out
);
1127 out4
->sin_family
= AF_INET
;
1128 out4
->sin_port
= in6
->sin6_port
;
1129 memcpy(&out4
->sin_addr
, &in6
->sin6_addr
.s6_addr
[12], 4);
1136 * Register us as a server for a particular tcp connection
1139 int ctdbd_register_ips(struct ctdbd_connection
*conn
,
1140 const struct sockaddr_storage
*_server
,
1141 const struct sockaddr_storage
*_client
,
1142 int (*cb
)(uint32_t src_vnn
, uint32_t dst_vnn
,
1144 const uint8_t *msg
, size_t msglen
,
1145 void *private_data
),
1148 struct ctdb_connection p
;
1149 TDB_DATA data
= { .dptr
= (uint8_t *)&p
, .dsize
= sizeof(p
) };
1151 struct sockaddr_storage client
;
1152 struct sockaddr_storage server
;
1155 * Only one connection so far
1158 smbd_ctdb_canonicalize_ip(_client
, &client
);
1159 smbd_ctdb_canonicalize_ip(_server
, &server
);
1161 switch (client
.ss_family
) {
1163 memcpy(&p
.dst
.ip
, &server
, sizeof(p
.dst
.ip
));
1164 memcpy(&p
.src
.ip
, &client
, sizeof(p
.src
.ip
));
1167 memcpy(&p
.dst
.ip6
, &server
, sizeof(p
.dst
.ip6
));
1168 memcpy(&p
.src
.ip6
, &client
, sizeof(p
.src
.ip6
));
1175 * We want to be told about IP releases
1178 ret
= register_with_ctdbd(conn
, CTDB_SRVID_RELEASE_IP
,
1185 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1186 * can send an extra ack to trigger a reset for our client, so it
1187 * immediately reconnects
1189 ret
= ctdbd_control(conn
, CTDB_CURRENT_NODE
,
1190 CTDB_CONTROL_TCP_CLIENT
, 0,
1191 CTDB_CTRL_FLAG_NOREPLY
, data
, NULL
, NULL
,
1200 call a control on the local node
1202 int ctdbd_control_local(struct ctdbd_connection
*conn
, uint32_t opcode
,
1203 uint64_t srvid
, uint32_t flags
, TDB_DATA data
,
1204 TALLOC_CTX
*mem_ctx
, TDB_DATA
*outdata
,
1207 return ctdbd_control(conn
, CTDB_CURRENT_NODE
, opcode
, srvid
, flags
, data
,
1208 mem_ctx
, outdata
, cstatus
);
1211 int ctdb_watch_us(struct ctdbd_connection
*conn
)
1213 struct ctdb_notify_data_old reg_data
;
1218 reg_data
.srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1220 reg_data
.notify_data
[0] = 0;
1222 struct_len
= offsetof(struct ctdb_notify_data_old
,
1223 notify_data
) + reg_data
.len
;
1225 ret
= ctdbd_control_local(
1226 conn
, CTDB_CONTROL_REGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1227 make_tdb_data((uint8_t *)®_data
, struct_len
),
1228 NULL
, NULL
, &cstatus
);
1230 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1236 int ctdb_unwatch(struct ctdbd_connection
*conn
)
1238 uint64_t srvid
= CTDB_SRVID_SAMBA_NOTIFY
;
1242 ret
= ctdbd_control_local(
1243 conn
, CTDB_CONTROL_DEREGISTER_NOTIFY
, conn
->rand_srvid
, 0,
1244 make_tdb_data((uint8_t *)&srvid
, sizeof(srvid
)),
1245 NULL
, NULL
, &cstatus
);
1247 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1253 int ctdbd_probe(const char *sockname
, int timeout
)
1256 * Do a very early check if ctdbd is around to avoid an abort and core
1259 struct ctdbd_connection
*conn
= NULL
;
1262 ret
= ctdbd_init_connection(talloc_tos(), sockname
, timeout
,
1266 * We only care if we can connect.