ctdbd_conn: Add some more debug info
[Samba.git] / source3 / lib / ctdbd_conn.c
blobc95af83ed3834846061f2a2dad84fe3bcae4e3a9
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 int *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, cstatus;
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);
121 if (ret != 0) {
122 return ret;
125 num_callbacks = talloc_array_length(conn->callbacks);
127 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
128 num_callbacks + 1);
129 if (tmp == NULL) {
130 return ENOMEM;
132 conn->callbacks = tmp;
134 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
135 .srvid = srvid, .cb = cb, .private_data = private_data
138 return 0;
141 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
142 struct ctdb_req_message_old *msg)
144 size_t msg_len;
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__,
150 (unsigned)msg_len));
151 return 0;
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));
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 int *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 struct server_id id;
695 bool result;
697 id.pid = pid;
698 id.vnn = vnn;
700 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
701 DEBUG(10, ("ctdb_processes_exist failed\n"));
702 return false;
704 return result;
707 bool ctdb_processes_exist(struct ctdbd_connection *conn,
708 const struct server_id *pids, int num_pids,
709 bool *results)
711 TALLOC_CTX *frame = talloc_stackframe();
712 int i, num_received;
713 uint32_t *reqids;
714 bool result = false;
716 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
717 if (reqids == NULL) {
718 goto fail;
721 for (i=0; i<num_pids; i++) {
722 struct ctdb_req_control_old req;
723 pid_t pid;
724 struct iovec iov[2];
725 ssize_t nwritten;
727 results[i] = false;
728 reqids[i] = ctdbd_next_reqid(conn);
730 ZERO_STRUCT(req);
733 * pids[i].pid is uint64_t, scale down to pid_t which
734 * is the wire protocol towards ctdb.
736 pid = pids[i].pid;
738 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
739 (int)pids[i].vnn, (int)pid,
740 (int)reqids[i]));
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;
750 req.srvid = 0;
751 req.datalen = sizeof(pid);
752 req.flags = 0;
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",
765 strerror(errno)));
766 goto fail;
770 num_received = 0;
772 while (num_received < num_pids) {
773 struct ctdb_req_header *hdr;
774 struct ctdb_reply_control_old *reply;
775 uint32_t reqid;
776 int ret;
778 ret = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
779 if (ret != 0) {
780 DEBUG(10, ("ctdb_read_req failed: %s\n",
781 strerror(ret)));
782 goto fail;
785 if (hdr->operation != CTDB_REPLY_CONTROL) {
786 DEBUG(10, ("Received invalid reply\n"));
787 goto fail;
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]) {
797 break;
800 if (i == num_pids) {
801 DEBUG(10, ("Received unknown record number %u\n",
802 (unsigned)reqid));
803 goto fail;
805 results[i] = ((reply->status) == 0);
806 TALLOC_FREE(reply);
807 num_received += 1;
810 result = true;
811 fail:
812 TALLOC_FREE(frame);
813 return result;
817 * Get a db path
819 char *ctdbd_dbpath(struct ctdbd_connection *conn,
820 TALLOC_CTX *mem_ctx, uint32_t db_id)
822 int ret;
823 TDB_DATA data;
824 TDB_DATA rdata = {0};
825 int32_t cstatus = 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",
834 strerror(ret)));
835 return NULL;
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)
847 int ret;
848 TDB_DATA data;
849 int32_t cstatus;
850 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
852 data = string_term_tdb_data(name);
854 ret = ctdbd_control_local(conn,
855 persistent
856 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
857 : CTDB_CONTROL_DB_ATTACH,
858 tdb_flags, 0, data, NULL, &data, &cstatus);
859 if (ret != 0) {
860 DEBUG(0, (__location__ " ctdb_control for db_attach "
861 "failed: %s\n", strerror(ret)));
862 return ret;
865 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
866 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
867 return EIO;
870 *db_id = *(uint32_t *)data.dptr;
871 talloc_free(data.dptr);
873 if (!(tdb_flags & TDB_SEQNUM)) {
874 return 0;
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;
888 return 0;
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;
898 struct iovec iov[2];
899 ssize_t nwritten;
900 int ret;
902 ZERO_STRUCT(req);
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;
911 req.db_id = db_id;
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);
929 if (ret != 0) {
930 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
931 goto fail;
934 if (hdr->operation != CTDB_REPLY_CALL) {
935 DEBUG(0, ("received invalid reply\n"));
936 goto fail;
939 fail:
941 TALLOC_FREE(hdr);
942 return ret;
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,
951 void *private_data),
952 void *private_data)
954 struct ctdb_req_call_old req;
955 struct ctdb_req_header *hdr = NULL;
956 struct ctdb_reply_call_old *reply;
957 struct iovec iov[2];
958 ssize_t nwritten;
959 uint32_t flags;
960 int ret;
962 flags = local_copy ? CTDB_WANT_READONLY : 0;
964 ZERO_STRUCT(req);
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);
971 req.flags = flags;
972 req.callid = CTDB_FETCH_FUNC;
973 req.db_id = db_id;
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);
988 if (ret != 0) {
989 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
990 goto fail;
993 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
994 DEBUG(0, ("received invalid reply\n"));
995 ret = EIO;
996 goto fail;
998 reply = (struct ctdb_reply_call_old *)hdr;
1000 if (reply->datalen == 0) {
1002 * Treat an empty record as non-existing
1004 ret = ENOENT;
1005 goto fail;
1008 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1009 private_data);
1011 ret = 0;
1012 fail:
1013 TALLOC_FREE(hdr);
1014 return ret;
1018 Traverse a ctdb database. "conn" must be an otherwise unused
1019 ctdb_connection where no other messages but the traverse ones are
1020 expected.
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),
1026 void *private_data)
1028 int ret;
1029 TDB_DATA key, data;
1030 struct ctdb_traverse_start t;
1031 int cstatus;
1033 t.db_id = db_id;
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,
1041 conn->rand_srvid,
1042 0, data, NULL, NULL, &cstatus);
1044 if ((ret != 0) || (cstatus != 0)) {
1045 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
1046 cstatus));
1048 if (ret == 0) {
1050 * We need a mapping here
1052 ret = EIO;
1054 return ret;
1057 while (true) {
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);
1063 if (ret != 0) {
1064 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1065 strerror(ret)));
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));
1072 return EIO;
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",
1079 (int)m->datalen));
1080 return EIO;
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 */
1090 return 0;
1093 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1094 DEBUG(0, ("Got invalid ltdb header length %d\n",
1095 (int)data.dsize));
1096 return EIO;
1098 data.dsize -= sizeof(struct ctdb_ltdb_header);
1099 data.dptr += sizeof(struct ctdb_ltdb_header);
1101 if (fn != NULL) {
1102 fn(key, data, private_data);
1105 return 0;
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));
1116 #ifdef HAVE_IPV6
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);
1126 #endif
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);
1132 #endif
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,
1143 uint64_t dst_srvid,
1144 const uint8_t *msg, size_t msglen,
1145 void *private_data),
1146 void *private_data)
1148 struct ctdb_connection p;
1149 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1150 int ret;
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) {
1162 case AF_INET:
1163 memcpy(&p.dst.ip, &server, sizeof(p.dst.ip));
1164 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1165 break;
1166 case AF_INET6:
1167 memcpy(&p.dst.ip6, &server, sizeof(p.dst.ip6));
1168 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1169 break;
1170 default:
1171 return EIO;
1175 * We want to be told about IP releases
1178 ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1179 cb, private_data);
1180 if (ret != 0) {
1181 return ret;
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,
1192 NULL);
1193 if (ret != 0) {
1194 return ret;
1196 return 0;
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,
1205 int *cstatus)
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;
1214 size_t struct_len;
1215 int ret;
1216 int cstatus;
1218 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1219 reg_data.len = 1;
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 *)&reg_data, struct_len),
1228 NULL, NULL, &cstatus);
1229 if (ret != 0) {
1230 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1231 strerror(ret)));
1233 return ret;
1236 int ctdb_unwatch(struct ctdbd_connection *conn)
1238 uint64_t srvid = CTDB_SRVID_SAMBA_NOTIFY;
1239 int ret;
1240 int cstatus;
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);
1246 if (ret != 0) {
1247 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1248 strerror(ret)));
1250 return ret;
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
1257 * later
1259 struct ctdbd_connection *conn = NULL;
1260 int ret;
1262 ret = ctdbd_init_connection(talloc_tos(), sockname, timeout,
1263 &conn);
1266 * We only care if we can connect.
1268 TALLOC_FREE(conn);
1270 return ret;