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;
53 struct tevent_fd *fde;
57 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
60 if (conn->reqid == 0) {
66 static int ctdbd_control(struct ctdbd_connection *conn,
67 uint32_t vnn, uint32_t opcode,
68 uint64_t srvid, uint32_t flags,
70 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
74 * exit on fatal communications errors with the ctdbd daemon
76 static void cluster_fatal(const char *why)
78 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
79 /* we don't use smb_panic() as we don't want to delay to write
80 a core file. We need to release this process id immediately
81 so that someone else can take over without getting sharing
89 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
91 if (DEBUGLEVEL < 11) {
94 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
95 (int)hdr->length, (int)hdr->ctdb_magic,
96 (int)hdr->ctdb_version, (int)hdr->generation,
97 (int)hdr->operation, (int)hdr->reqid));
101 * Register a srvid with ctdbd
103 int register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid,
104 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
106 const uint8_t *msg, size_t msglen,
113 size_t num_callbacks;
114 struct ctdbd_srvid_cb *tmp;
116 ret = ctdbd_control_local(conn, CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
117 tdb_null, NULL, NULL, &cstatus);
122 num_callbacks = talloc_array_length(conn->callbacks);
124 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
129 conn->callbacks = tmp;
131 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
132 .srvid = srvid, .cb = cb, .private_data = private_data
138 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
139 struct ctdb_req_message_old *msg)
142 size_t i, num_callbacks;
144 msg_len = msg->hdr.length;
145 if (msg_len < offsetof(struct ctdb_req_message_old, data)) {
146 DBG_DEBUG("len %"PRIu32" too small\n", msg_len);
149 msg_len -= offsetof(struct ctdb_req_message_old, data);
151 if (msg_len < msg->datalen) {
152 DBG_DEBUG("msg_len=%"PRIu32" < msg->datalen=%"PRIu32"\n",
153 msg_len, msg->datalen);
157 num_callbacks = talloc_array_length(conn->callbacks);
159 for (i=0; i<num_callbacks; i++) {
160 struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
162 if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
165 ret = cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
166 msg->srvid, msg->data, msg->datalen,
177 * get our vnn from the cluster
179 static int get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
183 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_PNN, 0, 0,
184 tdb_null, NULL, NULL, &cstatus);
186 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
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)
200 struct ctdb_node_map_old *m;
201 uint32_t failure_flags;
206 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_NODEMAP, 0, 0,
207 tdb_null, talloc_tos(), &outdata, &cstatus);
209 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
212 if ((cstatus != 0) || (outdata.dptr == NULL)) {
213 DEBUG(2, ("Received invalid ctdb data\n"));
217 m = (struct ctdb_node_map_old *)outdata.dptr;
219 for (i=0; i<m->num; i++) {
220 if (vnn == m->nodes[i].pnn) {
226 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
231 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
232 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
234 if ((m->nodes[i].flags & failure_flags) != 0) {
235 DEBUG(2, ("Node has status %x, not active\n",
236 (int)m->nodes[i].flags));
242 TALLOC_FREE(outdata.dptr);
246 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
248 return conn->our_vnn;
252 * Get us a ctdb connection
255 static int ctdbd_connect(const char *sockname, int *pfd)
257 struct sockaddr_un addr = { 0, };
262 fd = socket(AF_UNIX, SOCK_STREAM, 0);
265 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
269 addr.sun_family = AF_UNIX;
271 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
272 if (namelen >= sizeof(addr.sun_path)) {
273 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
279 salen = sizeof(struct sockaddr_un);
281 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
283 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
293 static int ctdb_read_packet(int fd, int timeout, TALLOC_CTX *mem_ctx,
294 struct ctdb_req_header **result)
296 struct ctdb_req_header *req;
301 struct pollfd pfd = { .fd = fd, .events = POLLIN };
304 ret = sys_poll_intr(&pfd, 1, timeout);
316 nread = read_data(fd, &msglen, sizeof(msglen));
324 if (msglen < sizeof(struct ctdb_req_header)) {
328 req = talloc_size(mem_ctx, msglen);
332 talloc_set_name_const(req, "struct ctdb_req_header");
334 req->length = msglen;
336 nread = read_data(fd, ((char *)req) + sizeof(msglen),
337 msglen - sizeof(msglen));
352 * Read a full ctdbd request. If we have a messaging context, defer incoming
353 * messages that might come in between.
356 static int ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
357 TALLOC_CTX *mem_ctx, struct ctdb_req_header **result)
359 struct ctdb_req_header *hdr;
364 ret = ctdb_read_packet(conn->fd, conn->timeout, mem_ctx, &hdr);
366 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
367 cluster_fatal("ctdbd died\n");
370 DEBUG(11, ("Received ctdb packet\n"));
371 ctdb_packet_dump(hdr);
373 if (hdr->operation == CTDB_REQ_MESSAGE) {
374 struct ctdb_req_message_old *msg = (struct ctdb_req_message_old *)hdr;
376 ret = ctdbd_msg_call_back(conn, msg);
386 if ((reqid != 0) && (hdr->reqid != reqid)) {
387 /* we got the wrong reply */
388 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
389 "been %u\n", hdr->reqid, reqid));
394 *result = talloc_move(mem_ctx, &hdr);
399 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
409 * Get us a ctdbd connection
412 int ctdbd_init_connection(TALLOC_CTX *mem_ctx,
413 const char *sockname, int timeout,
414 struct ctdbd_connection **pconn)
416 struct ctdbd_connection *conn;
419 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
420 DEBUG(0, ("talloc failed\n"));
424 conn->timeout = timeout;
426 if (conn->timeout == 0) {
430 ret = ctdbd_connect(sockname, &conn->fd);
432 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
435 talloc_set_destructor(conn, ctdbd_connection_destructor);
437 ret = get_cluster_vnn(conn, &conn->our_vnn);
440 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret)));
444 if (!ctdbd_working(conn, conn->our_vnn)) {
445 DEBUG(2, ("Node is not working, can not connect\n"));
450 generate_random_buffer((unsigned char *)&conn->rand_srvid,
451 sizeof(conn->rand_srvid));
453 ret = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
456 DEBUG(5, ("Could not register random srvid: %s\n",
469 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
475 * Packet handler to receive and handle a ctdb message
477 static int ctdb_handle_message(struct ctdbd_connection *conn,
478 struct ctdb_req_header *hdr)
480 struct ctdb_req_message_old *msg;
482 if (hdr->operation != CTDB_REQ_MESSAGE) {
483 DEBUG(0, ("Received async msg of type %u, discarding\n",
488 msg = (struct ctdb_req_message_old *)hdr;
490 ctdbd_msg_call_back(conn, msg);
495 void ctdbd_socket_readable(struct ctdbd_connection *conn)
497 struct ctdb_req_header *hdr = NULL;
500 ret = ctdb_read_packet(conn->fd, conn->timeout, talloc_tos(), &hdr);
502 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
503 cluster_fatal("ctdbd died\n");
506 ret = ctdb_handle_message(conn, hdr);
511 DEBUG(10, ("could not handle incoming message: %s\n",
516 int ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
517 uint32_t dst_vnn, uint64_t dst_srvid,
518 const struct iovec *iov, int iovlen)
520 struct ctdb_req_message_old r;
521 struct iovec iov2[iovlen+1];
522 size_t buflen = iov_buflen(iov, iovlen);
525 r.hdr.length = offsetof(struct ctdb_req_message_old, data) + buflen;
526 r.hdr.ctdb_magic = CTDB_MAGIC;
527 r.hdr.ctdb_version = CTDB_PROTOCOL;
528 r.hdr.generation = 1;
529 r.hdr.operation = CTDB_REQ_MESSAGE;
530 r.hdr.destnode = dst_vnn;
531 r.hdr.srcnode = conn->our_vnn;
536 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
537 ctdb_packet_dump(&r.hdr);
539 iov2[0].iov_base = &r;
540 iov2[0].iov_len = offsetof(struct ctdb_req_message_old, data);
541 memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
543 nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
544 if (nwritten == -1) {
545 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
546 cluster_fatal("cluster dispatch daemon msg write error\n");
553 * send/recv a generic ctdb control message
555 static int ctdbd_control(struct ctdbd_connection *conn,
556 uint32_t vnn, uint32_t opcode,
557 uint64_t srvid, uint32_t flags,
559 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
562 struct ctdb_req_control_old req;
563 struct ctdb_req_header *hdr;
564 struct ctdb_reply_control_old *reply = NULL;
570 req.hdr.length = offsetof(struct ctdb_req_control_old, data) + data.dsize;
571 req.hdr.ctdb_magic = CTDB_MAGIC;
572 req.hdr.ctdb_version = CTDB_PROTOCOL;
573 req.hdr.operation = CTDB_REQ_CONTROL;
574 req.hdr.reqid = ctdbd_next_reqid(conn);
575 req.hdr.destnode = vnn;
578 req.datalen = data.dsize;
581 DBG_DEBUG("Sending ctdb packet reqid=%"PRIu32", vnn=%"PRIu32", "
582 "opcode=%"PRIu32", srvid=%"PRIu64"\n", req.hdr.reqid,
583 req.hdr.destnode, req.opcode, req.srvid);
584 ctdb_packet_dump(&req.hdr);
586 iov[0].iov_base = &req;
587 iov[0].iov_len = offsetof(struct ctdb_req_control_old, data);
588 iov[1].iov_base = data.dptr;
589 iov[1].iov_len = data.dsize;
591 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
592 if (nwritten == -1) {
593 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
594 cluster_fatal("cluster dispatch daemon msg write error\n");
597 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
604 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
606 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
610 if (hdr->operation != CTDB_REPLY_CONTROL) {
611 DEBUG(0, ("received invalid reply\n"));
615 reply = (struct ctdb_reply_control_old *)hdr;
618 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
619 mem_ctx, reply->data, reply->datalen))) {
623 outdata->dsize = reply->datalen;
626 (*cstatus) = reply->status;
634 * see if a remote process exists
636 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
641 ret = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
642 (TDB_DATA) { .dptr = (uint8_t *)&pid,
643 .dsize = sizeof(pid) },
644 NULL, NULL, &cstatus);
648 return (cstatus == 0);
654 char *ctdbd_dbpath(struct ctdbd_connection *conn,
655 TALLOC_CTX *mem_ctx, uint32_t db_id)
659 TDB_DATA rdata = {0};
662 data.dptr = (uint8_t*)&db_id;
663 data.dsize = sizeof(db_id);
665 ret = ctdbd_control_local(conn, CTDB_CONTROL_GETDBPATH, 0, 0, data,
666 mem_ctx, &rdata, &cstatus);
667 if ((ret != 0) || cstatus != 0) {
668 DEBUG(0, (__location__ " ctdb_control for getdbpath failed: %s\n",
673 return (char *)rdata.dptr;
677 * attach to a ctdb database
679 int ctdbd_db_attach(struct ctdbd_connection *conn,
680 const char *name, uint32_t *db_id, int tdb_flags)
685 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
687 data = string_term_tdb_data(name);
689 ret = ctdbd_control_local(conn,
691 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
692 : CTDB_CONTROL_DB_ATTACH,
693 tdb_flags, 0, data, NULL, &data, &cstatus);
695 DEBUG(0, (__location__ " ctdb_control for db_attach "
696 "failed: %s\n", strerror(ret)));
700 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
701 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
705 *db_id = *(uint32_t *)data.dptr;
706 talloc_free(data.dptr);
708 if (!(tdb_flags & TDB_SEQNUM)) {
712 data.dptr = (uint8_t *)db_id;
713 data.dsize = sizeof(*db_id);
715 ret = ctdbd_control_local(conn, CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
716 NULL, NULL, &cstatus);
717 if ((ret != 0) || cstatus != 0) {
718 DEBUG(0, (__location__ " ctdb_control for enable seqnum "
719 "failed: %s\n", strerror(ret)));
720 return (ret == 0) ? EIO : ret;
727 * force the migration of a record to this node
729 int ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key)
731 struct ctdb_req_call_old req;
732 struct ctdb_req_header *hdr;
739 req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
740 req.hdr.ctdb_magic = CTDB_MAGIC;
741 req.hdr.ctdb_version = CTDB_PROTOCOL;
742 req.hdr.operation = CTDB_REQ_CALL;
743 req.hdr.reqid = ctdbd_next_reqid(conn);
744 req.flags = CTDB_IMMEDIATE_MIGRATION;
745 req.callid = CTDB_NULL_FUNC;
747 req.keylen = key.dsize;
749 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
750 ctdb_packet_dump(&req.hdr);
752 iov[0].iov_base = &req;
753 iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
754 iov[1].iov_base = key.dptr;
755 iov[1].iov_len = key.dsize;
757 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
758 if (nwritten == -1) {
759 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
760 cluster_fatal("cluster dispatch daemon msg write error\n");
763 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
765 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
769 if (hdr->operation != CTDB_REPLY_CALL) {
770 DEBUG(0, ("received invalid reply\n"));
781 * Fetch a record and parse it
783 int ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
784 TDB_DATA key, bool local_copy,
785 void (*parser)(TDB_DATA key, TDB_DATA data,
789 struct ctdb_req_call_old req;
790 struct ctdb_req_header *hdr = NULL;
791 struct ctdb_reply_call_old *reply;
797 flags = local_copy ? CTDB_WANT_READONLY : 0;
801 req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
802 req.hdr.ctdb_magic = CTDB_MAGIC;
803 req.hdr.ctdb_version = CTDB_PROTOCOL;
804 req.hdr.operation = CTDB_REQ_CALL;
805 req.hdr.reqid = ctdbd_next_reqid(conn);
807 req.callid = CTDB_FETCH_FUNC;
809 req.keylen = key.dsize;
811 iov[0].iov_base = &req;
812 iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
813 iov[1].iov_base = key.dptr;
814 iov[1].iov_len = key.dsize;
816 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
817 if (nwritten == -1) {
818 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
819 cluster_fatal("cluster dispatch daemon msg write error\n");
822 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
824 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
828 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
829 DEBUG(0, ("received invalid reply\n"));
833 reply = (struct ctdb_reply_call_old *)hdr;
835 if (reply->datalen == 0) {
837 * Treat an empty record as non-existing
843 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
853 Traverse a ctdb database. "conn" must be an otherwise unused
854 ctdb_connection where no other messages but the traverse ones are
858 int ctdbd_traverse(struct ctdbd_connection *conn, uint32_t db_id,
859 void (*fn)(TDB_DATA key, TDB_DATA data,
865 struct ctdb_traverse_start t;
869 t.srvid = conn->rand_srvid;
870 t.reqid = ctdbd_next_reqid(conn);
872 data.dptr = (uint8_t *)&t;
873 data.dsize = sizeof(t);
875 ret = ctdbd_control_local(conn, CTDB_CONTROL_TRAVERSE_START,
877 0, data, NULL, NULL, &cstatus);
879 if ((ret != 0) || (cstatus != 0)) {
880 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
885 * We need a mapping here
893 struct ctdb_req_header *hdr = NULL;
894 struct ctdb_req_message_old *m;
895 struct ctdb_rec_data_old *d;
897 ret = ctdb_read_packet(conn->fd, conn->timeout, conn, &hdr);
899 DEBUG(0, ("ctdb_read_packet failed: %s\n",
901 cluster_fatal("ctdbd died\n");
904 if (hdr->operation != CTDB_REQ_MESSAGE) {
905 DEBUG(0, ("Got operation %u, expected a message\n",
906 (unsigned)hdr->operation));
910 m = (struct ctdb_req_message_old *)hdr;
911 d = (struct ctdb_rec_data_old *)&m->data[0];
912 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
913 DEBUG(0, ("Got invalid traverse data of length %d\n",
918 key.dsize = d->keylen;
919 key.dptr = &d->data[0];
920 data.dsize = d->datalen;
921 data.dptr = &d->data[d->keylen];
923 if (key.dsize == 0 && data.dsize == 0) {
924 /* end of traverse */
928 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
929 DEBUG(0, ("Got invalid ltdb header length %d\n",
933 data.dsize -= sizeof(struct ctdb_ltdb_header);
934 data.dptr += sizeof(struct ctdb_ltdb_header);
937 fn(key, data, private_data);
944 This is used to canonicalize a ctdb_sock_addr structure.
946 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
947 struct sockaddr_storage *out)
949 memcpy(out, in, sizeof (*out));
952 if (in->ss_family == AF_INET6) {
953 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
954 const struct sockaddr_in6 *in6 =
955 (const struct sockaddr_in6 *)in;
956 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
957 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
958 memset(out, 0, sizeof(*out));
959 #ifdef HAVE_SOCK_SIN_LEN
960 out4->sin_len = sizeof(*out);
962 out4->sin_family = AF_INET;
963 out4->sin_port = in6->sin6_port;
964 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
971 * Register us as a server for a particular tcp connection
974 int ctdbd_register_ips(struct ctdbd_connection *conn,
975 const struct sockaddr_storage *_server,
976 const struct sockaddr_storage *_client,
977 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
979 const uint8_t *msg, size_t msglen,
983 struct ctdb_connection p;
984 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
986 struct sockaddr_storage client;
987 struct sockaddr_storage server;
990 * Only one connection so far
993 smbd_ctdb_canonicalize_ip(_client, &client);
994 smbd_ctdb_canonicalize_ip(_server, &server);
996 switch (client.ss_family) {
998 memcpy(&p.dst.ip, &server, sizeof(p.dst.ip));
999 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1002 memcpy(&p.dst.ip6, &server, sizeof(p.dst.ip6));
1003 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1010 * We want to be told about IP releases
1013 ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1020 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1021 * can send an extra ack to trigger a reset for our client, so it
1022 * immediately reconnects
1024 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
1025 CTDB_CONTROL_TCP_CLIENT, 0,
1026 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL,
1035 call a control on the local node
1037 int ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1038 uint64_t srvid, uint32_t flags, TDB_DATA data,
1039 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1042 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data,
1043 mem_ctx, outdata, cstatus);
1046 int ctdb_watch_us(struct ctdbd_connection *conn)
1048 struct ctdb_notify_data_old reg_data;
1053 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1055 reg_data.notify_data[0] = 0;
1057 struct_len = offsetof(struct ctdb_notify_data_old,
1058 notify_data) + reg_data.len;
1060 ret = ctdbd_control_local(
1061 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1062 make_tdb_data((uint8_t *)®_data, struct_len),
1063 NULL, NULL, &cstatus);
1065 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1071 int ctdb_unwatch(struct ctdbd_connection *conn)
1073 uint64_t srvid = CTDB_SRVID_SAMBA_NOTIFY;
1077 ret = ctdbd_control_local(
1078 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1079 make_tdb_data((uint8_t *)&srvid, sizeof(srvid)),
1080 NULL, NULL, &cstatus);
1082 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1088 int ctdbd_probe(const char *sockname, int timeout)
1091 * Do a very early check if ctdbd is around to avoid an abort and core
1094 struct ctdbd_connection *conn = NULL;
1097 ret = ctdbd_init_connection(talloc_tos(), sockname, timeout,
1101 * We only care if we can connect.