ctdbd_conn: Simplify two DEBUGs
[Samba.git] / source3 / lib / ctdbd_conn.c
blob82abdf959373892752b0cc4e7fea1f6e4d05dc92
1 /*
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/>.
21 #include "replace.h"
22 #include "util_tdb.h"
23 #include "serverid.h"
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 #include "messages.h"
36 /* paths to these include files come from --with-ctdb= in configure */
38 #include "ctdb_private.h"
40 struct ctdbd_srvid_cb {
41 uint64_t srvid;
42 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
43 uint64_t dst_srvid,
44 const uint8_t *msg, size_t msglen,
45 void *private_data);
46 void *private_data;
49 struct ctdbd_connection {
50 const char *sockname; /* Needed in ctdbd_traverse */
51 struct messaging_context *msg_ctx;
52 uint32_t reqid;
53 uint32_t our_vnn;
54 uint64_t rand_srvid;
55 struct ctdbd_srvid_cb *callbacks;
56 int fd;
57 struct tevent_fd *fde;
58 int timeout;
61 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
63 conn->reqid += 1;
64 if (conn->reqid == 0) {
65 conn->reqid += 1;
67 return conn->reqid;
70 static int ctdbd_control(struct ctdbd_connection *conn,
71 uint32_t vnn, uint32_t opcode,
72 uint64_t srvid, uint32_t flags,
73 TDB_DATA data,
74 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
75 int32_t *cstatus);
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
86 violations */
87 _exit(1);
93 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
95 if (DEBUGLEVEL < 11) {
96 return;
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,
109 uint64_t dst_srvid,
110 const uint8_t *msg, size_t msglen,
111 void *private_data),
112 void *private_data)
115 int ret;
116 int32_t cstatus;
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);
122 if (ret != 0) {
123 return ret;
126 num_callbacks = talloc_array_length(conn->callbacks);
128 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
129 num_callbacks + 1);
130 if (tmp == NULL) {
131 return ENOMEM;
133 conn->callbacks = tmp;
135 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
136 .srvid = srvid, .cb = cb, .private_data = private_data
139 return 0;
142 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
143 struct ctdb_req_message_old *msg)
145 uint32_t msg_len;
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);
151 return 0;
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);
158 return 0;
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)) {
167 int ret;
169 ret = cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
170 msg->srvid, msg->data, msg->datalen,
171 cb->private_data);
172 if (ret != 0) {
173 return ret;
177 return 0;
181 * get our vnn from the cluster
183 static int get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
185 int32_t cstatus=-1;
186 int ret;
187 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_PNN, 0, 0,
188 tdb_null, NULL, NULL, &cstatus);
189 if (ret != 0) {
190 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
191 return ret;
193 *vnn = (uint32_t)cstatus;
194 return ret;
198 * Are we active (i.e. not banned or stopped?)
200 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
202 int32_t cstatus=-1;
203 TDB_DATA outdata;
204 struct ctdb_node_map_old *m;
205 uint32_t failure_flags;
206 bool ok = false;
207 uint32_t i;
208 int ret;
210 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_NODEMAP, 0, 0,
211 tdb_null, talloc_tos(), &outdata, &cstatus);
212 if (ret != 0) {
213 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
214 return false;
216 if ((cstatus != 0) || (outdata.dptr == NULL)) {
217 DEBUG(2, ("Received invalid ctdb data\n"));
218 return false;
221 m = (struct ctdb_node_map_old *)outdata.dptr;
223 for (i=0; i<m->num; i++) {
224 if (vnn == m->nodes[i].pnn) {
225 break;
229 if (i == m->num) {
230 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
231 (int)vnn));
232 goto fail;
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));
241 goto fail;
244 ok = true;
245 fail:
246 TALLOC_FREE(outdata.dptr);
247 return ok;
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, };
262 int fd;
263 socklen_t salen;
264 size_t namelen;
266 fd = socket(AF_UNIX, SOCK_STREAM, 0);
267 if (fd == -1) {
268 int err = errno;
269 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
270 return 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__,
278 sockname));
279 close(fd);
280 return ENAMETOOLONG;
283 salen = sizeof(struct sockaddr_un);
285 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
286 int err = errno;
287 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
288 strerror(err)));
289 close(fd);
290 return err;
293 *pfd = fd;
294 return 0;
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;
301 uint32_t msglen;
302 ssize_t nread;
304 if (timeout != -1) {
305 struct pollfd pfd = { .fd = fd, .events = POLLIN };
306 int ret;
308 ret = sys_poll_intr(&pfd, 1, timeout);
309 if (ret == -1) {
310 return errno;
312 if (ret == 0) {
313 return ETIMEDOUT;
315 if (ret != 1) {
316 return EIO;
320 nread = read_data(fd, &msglen, sizeof(msglen));
321 if (nread == -1) {
322 return errno;
324 if (nread == 0) {
325 return EIO;
328 if (msglen < sizeof(struct ctdb_req_header)) {
329 return EIO;
332 req = talloc_size(mem_ctx, msglen);
333 if (req == NULL) {
334 return ENOMEM;
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));
342 if (nread == -1) {
343 TALLOC_FREE(req);
344 return errno;
346 if (nread == 0) {
347 TALLOC_FREE(req);
348 return EIO;
351 *result = req;
352 return 0;
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;
364 int ret;
366 next_pkt:
368 ret = ctdb_read_packet(conn->fd, conn->timeout, mem_ctx, &hdr);
369 if (ret != 0) {
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));
384 TALLOC_FREE(hdr);
385 goto next_pkt;
388 ret = ctdbd_msg_call_back(conn, msg);
389 if (ret != 0) {
390 TALLOC_FREE(hdr);
391 return ret;
394 TALLOC_FREE(hdr);
395 goto next_pkt;
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));
402 TALLOC_FREE(hdr);
403 goto next_pkt;
406 *result = talloc_move(mem_ctx, &hdr);
408 return 0;
411 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
413 TALLOC_FREE(c->fde);
414 if (c->fd != -1) {
415 close(c->fd);
416 c->fd = -1;
418 return 0;
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;
429 int ret;
431 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
432 DEBUG(0, ("talloc failed\n"));
433 return ENOMEM;
436 conn->sockname = talloc_strdup(conn, sockname);
437 if (conn->sockname == NULL) {
438 DBG_ERR("talloc failed\n");
439 ret = ENOMEM;
440 goto fail;
443 conn->timeout = timeout;
445 if (conn->timeout == 0) {
446 conn->timeout = -1;
449 ret = ctdbd_connect(conn->sockname, &conn->fd);
450 if (ret != 0) {
451 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
452 goto fail;
454 talloc_set_destructor(conn, ctdbd_connection_destructor);
456 ret = get_cluster_vnn(conn, &conn->our_vnn);
458 if (ret != 0) {
459 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret)));
460 goto fail;
463 if (!ctdbd_working(conn, conn->our_vnn)) {
464 DEBUG(2, ("Node is not working, can not connect\n"));
465 ret = EIO;
466 goto fail;
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);
474 if (ret != 0) {
475 DEBUG(5, ("Could not register random srvid: %s\n",
476 strerror(ret)));
477 goto fail;
480 *pconn = conn;
481 return 0;
483 fail:
484 TALLOC_FREE(conn);
485 return ret;
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)
495 return conn->fd;
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",
508 hdr->operation));
509 return EINVAL;
512 msg = (struct ctdb_req_message_old *)hdr;
514 ctdbd_msg_call_back(conn, msg);
516 return 0;
520 * The ctdbd socket is readable asynchronuously
523 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
524 struct tevent_fd *event,
525 uint16_t flags,
526 void *private_data)
528 struct ctdbd_connection *conn = talloc_get_type_abort(
529 private_data, struct ctdbd_connection);
530 struct ctdb_req_header *hdr = NULL;
531 int ret;
533 ret = ctdb_read_packet(conn->fd, conn->timeout, talloc_tos(), &hdr);
534 if (ret != 0) {
535 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
536 cluster_fatal("ctdbd died\n");
539 ret = ctdb_handle_message(conn, hdr);
541 TALLOC_FREE(hdr);
543 if (ret != 0) {
544 DEBUG(10, ("could not handle incoming message: %s\n",
545 strerror(ret)));
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"));
564 return ENOMEM;
567 conn->msg_ctx = msg_ctx;
569 return 0;
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);
579 ssize_t nwritten;
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;
588 r.hdr.reqid = 0;
589 r.srvid = dst_srvid;
590 r.datalen = buflen;
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");
605 return 0;
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,
614 TDB_DATA data,
615 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
616 int32_t *cstatus)
618 struct ctdb_req_control_old req;
619 struct ctdb_req_header *hdr;
620 struct ctdb_reply_control_old *reply = NULL;
621 struct iovec iov[2];
622 ssize_t nwritten;
623 int ret;
625 ZERO_STRUCT(req);
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;
632 req.opcode = opcode;
633 req.srvid = srvid;
634 req.datalen = data.dsize;
635 req.flags = flags;
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) {
654 if (cstatus) {
655 *cstatus = 0;
657 return 0;
660 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
661 if (ret != 0) {
662 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
663 return ret;
666 if (hdr->operation != CTDB_REPLY_CONTROL) {
667 DEBUG(0, ("received invalid reply\n"));
668 TALLOC_FREE(hdr);
669 return EIO;
671 reply = (struct ctdb_reply_control_old *)hdr;
673 if (outdata) {
674 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
675 mem_ctx, reply->data, reply->datalen))) {
676 TALLOC_FREE(reply);
677 return ENOMEM;
679 outdata->dsize = reply->datalen;
681 if (cstatus) {
682 (*cstatus) = reply->status;
685 TALLOC_FREE(reply);
686 return ret;
690 * see if a remote process exists
692 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
694 int32_t cstatus = 0;
695 int ret;
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);
701 if (ret != 0) {
702 return false;
704 return (cstatus == 0);
708 * Get a db path
710 char *ctdbd_dbpath(struct ctdbd_connection *conn,
711 TALLOC_CTX *mem_ctx, uint32_t db_id)
713 int ret;
714 TDB_DATA data;
715 TDB_DATA rdata = {0};
716 int32_t cstatus = 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",
725 strerror(ret)));
726 return NULL;
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)
738 int ret;
739 TDB_DATA data;
740 int32_t cstatus;
741 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
743 data = string_term_tdb_data(name);
745 ret = ctdbd_control_local(conn,
746 persistent
747 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
748 : CTDB_CONTROL_DB_ATTACH,
749 tdb_flags, 0, data, NULL, &data, &cstatus);
750 if (ret != 0) {
751 DEBUG(0, (__location__ " ctdb_control for db_attach "
752 "failed: %s\n", strerror(ret)));
753 return ret;
756 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
757 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
758 return EIO;
761 *db_id = *(uint32_t *)data.dptr;
762 talloc_free(data.dptr);
764 if (!(tdb_flags & TDB_SEQNUM)) {
765 return 0;
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;
779 return 0;
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;
789 struct iovec iov[2];
790 ssize_t nwritten;
791 int ret;
793 ZERO_STRUCT(req);
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;
802 req.db_id = db_id;
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);
820 if (ret != 0) {
821 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
822 goto fail;
825 if (hdr->operation != CTDB_REPLY_CALL) {
826 DEBUG(0, ("received invalid reply\n"));
827 goto fail;
830 fail:
832 TALLOC_FREE(hdr);
833 return ret;
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,
842 void *private_data),
843 void *private_data)
845 struct ctdb_req_call_old req;
846 struct ctdb_req_header *hdr = NULL;
847 struct ctdb_reply_call_old *reply;
848 struct iovec iov[2];
849 ssize_t nwritten;
850 uint32_t flags;
851 int ret;
853 flags = local_copy ? CTDB_WANT_READONLY : 0;
855 ZERO_STRUCT(req);
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);
862 req.flags = flags;
863 req.callid = CTDB_FETCH_FUNC;
864 req.db_id = db_id;
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);
879 if (ret != 0) {
880 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
881 goto fail;
884 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
885 DEBUG(0, ("received invalid reply\n"));
886 ret = EIO;
887 goto fail;
889 reply = (struct ctdb_reply_call_old *)hdr;
891 if (reply->datalen == 0) {
893 * Treat an empty record as non-existing
895 ret = ENOENT;
896 goto fail;
899 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
900 private_data);
902 ret = 0;
903 fail:
904 TALLOC_FREE(hdr);
905 return ret;
909 Traverse a ctdb database. "conn" must be an otherwise unused
910 ctdb_connection where no other messages but the traverse ones are
911 expected.
914 int ctdbd_traverse(struct ctdbd_connection *conn, uint32_t db_id,
915 void (*fn)(TDB_DATA key, TDB_DATA data,
916 void *private_data),
917 void *private_data)
919 int ret;
920 TDB_DATA key, data;
921 struct ctdb_traverse_start t;
922 int32_t cstatus;
924 t.db_id = db_id;
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,
932 conn->rand_srvid,
933 0, data, NULL, NULL, &cstatus);
935 if ((ret != 0) || (cstatus != 0)) {
936 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
937 cstatus));
939 if (ret == 0) {
941 * We need a mapping here
943 ret = EIO;
945 return ret;
948 while (true) {
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);
954 if (ret != 0) {
955 DEBUG(0, ("ctdb_read_packet failed: %s\n",
956 strerror(ret)));
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));
963 return EIO;
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",
970 (int)m->datalen));
971 return EIO;
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 */
981 return 0;
984 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
985 DEBUG(0, ("Got invalid ltdb header length %d\n",
986 (int)data.dsize));
987 return EIO;
989 data.dsize -= sizeof(struct ctdb_ltdb_header);
990 data.dptr += sizeof(struct ctdb_ltdb_header);
992 if (fn != NULL) {
993 fn(key, data, private_data);
996 return 0;
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));
1007 #ifdef HAVE_IPV6
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);
1017 #endif
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);
1023 #endif
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,
1034 uint64_t dst_srvid,
1035 const uint8_t *msg, size_t msglen,
1036 void *private_data),
1037 void *private_data)
1039 struct ctdb_connection p;
1040 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1041 int ret;
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) {
1053 case AF_INET:
1054 memcpy(&p.dst.ip, &server, sizeof(p.dst.ip));
1055 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1056 break;
1057 case AF_INET6:
1058 memcpy(&p.dst.ip6, &server, sizeof(p.dst.ip6));
1059 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1060 break;
1061 default:
1062 return EIO;
1066 * We want to be told about IP releases
1069 ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1070 cb, private_data);
1071 if (ret != 0) {
1072 return ret;
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,
1083 NULL);
1084 if (ret != 0) {
1085 return ret;
1087 return 0;
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,
1096 int32_t *cstatus)
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;
1105 size_t struct_len;
1106 int ret;
1107 int32_t cstatus;
1109 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1110 reg_data.len = 1;
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 *)&reg_data, struct_len),
1119 NULL, NULL, &cstatus);
1120 if (ret != 0) {
1121 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1122 strerror(ret)));
1124 return ret;
1127 int ctdb_unwatch(struct ctdbd_connection *conn)
1129 uint64_t srvid = CTDB_SRVID_SAMBA_NOTIFY;
1130 int ret;
1131 int32_t cstatus;
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);
1137 if (ret != 0) {
1138 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1139 strerror(ret)));
1141 return ret;
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
1148 * later
1150 struct ctdbd_connection *conn = NULL;
1151 int ret;
1153 ret = ctdbd_init_connection(talloc_tos(), sockname, timeout,
1154 &conn);
1157 * We only care if we can connect.
1159 TALLOC_FREE(conn);
1161 return ret;