lib: Remove messaging_tevent_context() dependency from ctdbd_conn.c
[Samba.git] / source3 / lib / ctdbd_conn.c
blob990819e089aa9cd36b9c72a166e3b2e378b13297
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 "includes.h"
22 #include "util_tdb.h"
23 #include "serverid.h"
24 #include "ctdbd_conn.h"
25 #include "system/select.h"
26 #include "lib/sys_rw_data.h"
27 #include "lib/util/iov_buf.h"
29 #include "messages.h"
31 /* paths to these include files come from --with-ctdb= in configure */
33 #include "ctdb.h"
34 #include "ctdb_private.h"
36 struct ctdbd_srvid_cb {
37 uint64_t srvid;
38 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
39 uint64_t dst_srvid,
40 const uint8_t *msg, size_t msglen,
41 void *private_data);
42 void *private_data;
45 struct ctdbd_connection {
46 const char *sockname; /* Needed in ctdbd_traverse */
47 struct messaging_context *msg_ctx;
48 uint32_t reqid;
49 uint32_t our_vnn;
50 uint64_t rand_srvid;
51 struct ctdbd_srvid_cb *callbacks;
52 int fd;
53 struct tevent_fd *fde;
54 int timeout;
57 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
59 conn->reqid += 1;
60 if (conn->reqid == 0) {
61 conn->reqid += 1;
63 return conn->reqid;
66 static int ctdbd_control(struct ctdbd_connection *conn,
67 uint32_t vnn, uint32_t opcode,
68 uint64_t srvid, uint32_t flags,
69 TDB_DATA data,
70 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
71 int *cstatus);
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
82 violations */
83 _exit(1);
89 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
91 if (DEBUGLEVEL < 11) {
92 return;
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,
105 uint64_t dst_srvid,
106 const uint8_t *msg, size_t msglen,
107 void *private_data),
108 void *private_data)
111 int ret, cstatus;
112 size_t num_callbacks;
113 struct ctdbd_srvid_cb *tmp;
115 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
116 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
117 tdb_null, NULL, NULL, &cstatus);
118 if (ret != 0) {
119 return ret;
122 num_callbacks = talloc_array_length(conn->callbacks);
124 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
125 num_callbacks + 1);
126 if (tmp == NULL) {
127 return ENOMEM;
129 conn->callbacks = tmp;
131 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
132 .srvid = srvid, .cb = cb, .private_data = private_data
135 return 0;
138 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
139 struct ctdb_req_message *msg)
141 size_t msg_len;
142 size_t i, num_callbacks;
144 msg_len = msg->hdr.length;
145 if (msg_len < offsetof(struct ctdb_req_message, data)) {
146 DEBUG(10, ("%s: len %u too small\n", __func__,
147 (unsigned)msg_len));
148 return 0;
150 msg_len -= offsetof(struct ctdb_req_message, data);
152 if (msg_len < msg->datalen) {
153 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__,
154 (unsigned)msg_len, (unsigned)msg->datalen));
155 return 0;
158 num_callbacks = talloc_array_length(conn->callbacks);
160 for (i=0; i<num_callbacks; i++) {
161 struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
163 if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
164 int ret;
166 ret = cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
167 msg->srvid, msg->data, msg->datalen,
168 cb->private_data);
169 if (ret != 0) {
170 return ret;
174 return 0;
178 * get our vnn from the cluster
180 static int get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
182 int32_t cstatus=-1;
183 int ret;
184 ret = ctdbd_control(conn,
185 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
186 tdb_null, NULL, NULL, &cstatus);
187 if (ret != 0) {
188 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
189 return ret;
191 *vnn = (uint32_t)cstatus;
192 return ret;
196 * Are we active (i.e. not banned or stopped?)
198 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
200 int32_t cstatus=-1;
201 TDB_DATA outdata;
202 struct ctdb_node_map *m;
203 uint32_t failure_flags;
204 bool ok = false;
205 int i, ret;
207 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
208 CTDB_CONTROL_GET_NODEMAP, 0, 0,
209 tdb_null, talloc_tos(), &outdata, &cstatus);
210 if (ret != 0) {
211 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
212 return false;
214 if ((cstatus != 0) || (outdata.dptr == NULL)) {
215 DEBUG(2, ("Received invalid ctdb data\n"));
216 return false;
219 m = (struct ctdb_node_map *)outdata.dptr;
221 for (i=0; i<m->num; i++) {
222 if (vnn == m->nodes[i].pnn) {
223 break;
227 if (i == m->num) {
228 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
229 (int)vnn));
230 goto fail;
233 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
234 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
236 if ((m->nodes[i].flags & failure_flags) != 0) {
237 DEBUG(2, ("Node has status %x, not active\n",
238 (int)m->nodes[i].flags));
239 goto fail;
242 ok = true;
243 fail:
244 TALLOC_FREE(outdata.dptr);
245 return ok;
248 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
250 return conn->our_vnn;
254 * Get us a ctdb connection
257 static int ctdbd_connect(const char *sockname, int *pfd)
259 struct sockaddr_un addr = { 0, };
260 int fd;
261 socklen_t salen;
262 size_t namelen;
264 fd = socket(AF_UNIX, SOCK_STREAM, 0);
265 if (fd == -1) {
266 int err = errno;
267 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
268 return err;
271 addr.sun_family = AF_UNIX;
273 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
274 if (namelen >= sizeof(addr.sun_path)) {
275 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
276 sockname));
277 close(fd);
278 return ENAMETOOLONG;
281 salen = sizeof(struct sockaddr_un);
283 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
284 int err = errno;
285 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
286 strerror(err)));
287 close(fd);
288 return err;
291 *pfd = fd;
292 return 0;
295 static int ctdb_read_packet(int fd, int timeout, TALLOC_CTX *mem_ctx,
296 struct ctdb_req_header **result)
298 struct ctdb_req_header *req;
299 int ret, revents;
300 uint32_t msglen;
301 ssize_t nread;
303 if (timeout != -1) {
304 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
305 if (ret == -1) {
306 return errno;
308 if (ret == 0) {
309 return ETIMEDOUT;
311 if (ret != 1) {
312 return EIO;
316 nread = read_data(fd, &msglen, sizeof(msglen));
317 if (nread == -1) {
318 return errno;
320 if (nread == 0) {
321 return EIO;
324 if (msglen < sizeof(struct ctdb_req_header)) {
325 return EIO;
328 req = talloc_size(mem_ctx, msglen);
329 if (req == NULL) {
330 return ENOMEM;
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));
338 if (nread == -1) {
339 TALLOC_FREE(req);
340 return errno;
342 if (nread == 0) {
343 TALLOC_FREE(req);
344 return EIO;
347 *result = req;
348 return 0;
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;
360 int ret;
362 next_pkt:
364 ret = ctdb_read_packet(conn->fd, conn->timeout, mem_ctx, &hdr);
365 if (ret != 0) {
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 *msg = (struct ctdb_req_message *)hdr;
376 if (conn->msg_ctx == NULL) {
377 DEBUG(1, ("Got a message without having a msg ctx, "
378 "dropping msg %llu\n",
379 (long long unsigned)msg->srvid));
380 TALLOC_FREE(hdr);
381 goto next_pkt;
384 ret = ctdbd_msg_call_back(conn, msg);
385 if (ret != 0) {
386 TALLOC_FREE(hdr);
387 return ret;
390 TALLOC_FREE(hdr);
391 goto next_pkt;
394 if ((reqid != 0) && (hdr->reqid != reqid)) {
395 /* we got the wrong reply */
396 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
397 "been %u\n", hdr->reqid, reqid));
398 TALLOC_FREE(hdr);
399 goto next_pkt;
402 *result = talloc_move(mem_ctx, &hdr);
404 return 0;
407 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
409 TALLOC_FREE(c->fde);
410 if (c->fd != -1) {
411 close(c->fd);
412 c->fd = -1;
414 return 0;
417 * Get us a ctdbd connection
420 static int ctdbd_init_connection(TALLOC_CTX *mem_ctx,
421 const char *sockname, int timeout,
422 struct ctdbd_connection **pconn)
424 struct ctdbd_connection *conn;
425 int ret;
427 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
428 DEBUG(0, ("talloc failed\n"));
429 return ENOMEM;
432 conn->sockname = talloc_strdup(conn, sockname);
433 if (conn->sockname == NULL) {
434 DBG_ERR("%s: talloc failed\n", __func__);
435 ret = ENOMEM;
436 goto fail;
439 conn->timeout = timeout;
441 if (conn->timeout == 0) {
442 conn->timeout = -1;
445 ret = ctdbd_connect(conn->sockname, &conn->fd);
446 if (ret != 0) {
447 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
448 goto fail;
450 talloc_set_destructor(conn, ctdbd_connection_destructor);
452 ret = get_cluster_vnn(conn, &conn->our_vnn);
454 if (ret != 0) {
455 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret)));
456 goto fail;
459 if (!ctdbd_working(conn, conn->our_vnn)) {
460 DEBUG(2, ("Node is not working, can not connect\n"));
461 ret = EIO;
462 goto fail;
465 generate_random_buffer((unsigned char *)&conn->rand_srvid,
466 sizeof(conn->rand_srvid));
468 ret = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
470 if (ret != 0) {
471 DEBUG(5, ("Could not register random srvid: %s\n",
472 strerror(ret)));
473 goto fail;
476 *pconn = conn;
477 return 0;
479 fail:
480 TALLOC_FREE(conn);
481 return ret;
485 * Get us a ctdbd connection and register us as a process
488 int ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
489 const char *sockname, int timeout,
490 struct ctdbd_connection **pconn)
492 struct ctdbd_connection *conn;
493 int ret;
495 ret = ctdbd_init_connection(mem_ctx, sockname, timeout, &conn);
497 if (ret != 0) {
498 return ret;
501 ret = register_with_ctdbd(conn, MSG_SRVID_SAMBA, NULL, NULL);
502 if (ret != 0) {
503 goto fail;
506 *pconn = conn;
507 return 0;
509 fail:
510 TALLOC_FREE(conn);
511 return ret;
514 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
516 return conn->msg_ctx;
519 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
521 return conn->fd;
525 * Packet handler to receive and handle a ctdb message
527 static int ctdb_handle_message(struct ctdbd_connection *conn,
528 struct ctdb_req_header *hdr)
530 struct ctdb_req_message *msg;
532 if (hdr->operation != CTDB_REQ_MESSAGE) {
533 DEBUG(0, ("Received async msg of type %u, discarding\n",
534 hdr->operation));
535 return EINVAL;
538 msg = (struct ctdb_req_message *)hdr;
540 ctdbd_msg_call_back(conn, msg);
542 return 0;
546 * The ctdbd socket is readable asynchronuously
549 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
550 struct tevent_fd *event,
551 uint16_t flags,
552 void *private_data)
554 struct ctdbd_connection *conn = talloc_get_type_abort(
555 private_data, struct ctdbd_connection);
556 struct ctdb_req_header *hdr = NULL;
557 int ret;
559 ret = ctdb_read_packet(conn->fd, conn->timeout, talloc_tos(), &hdr);
560 if (ret != 0) {
561 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
562 cluster_fatal("ctdbd died\n");
565 ret = ctdb_handle_message(conn, hdr);
567 TALLOC_FREE(hdr);
569 if (ret != 0) {
570 DEBUG(10, ("could not handle incoming message: %s\n",
571 strerror(ret)));
576 * Prepare a ctdbd connection to receive messages
579 int ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
580 struct messaging_context *msg_ctx,
581 struct tevent_context *ev)
583 SMB_ASSERT(conn->msg_ctx == NULL);
584 SMB_ASSERT(conn->fde == NULL);
586 conn->fde = tevent_add_fd(ev, conn, conn->fd, TEVENT_FD_READ,
587 ctdbd_socket_handler, conn);
588 if (conn->fde == NULL) {
589 DEBUG(0, ("event_add_fd failed\n"));
590 return ENOMEM;
593 conn->msg_ctx = msg_ctx;
595 return 0;
598 int ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
599 uint32_t dst_vnn, uint64_t dst_srvid,
600 const struct iovec *iov, int iovlen)
602 struct ctdb_req_message r;
603 struct iovec iov2[iovlen+1];
604 size_t buflen = iov_buflen(iov, iovlen);
605 ssize_t nwritten;
607 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
608 r.hdr.ctdb_magic = CTDB_MAGIC;
609 r.hdr.ctdb_version = CTDB_PROTOCOL;
610 r.hdr.generation = 1;
611 r.hdr.operation = CTDB_REQ_MESSAGE;
612 r.hdr.destnode = dst_vnn;
613 r.hdr.srcnode = conn->our_vnn;
614 r.hdr.reqid = 0;
615 r.srvid = dst_srvid;
616 r.datalen = buflen;
618 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
619 ctdb_packet_dump(&r.hdr);
621 iov2[0].iov_base = &r;
622 iov2[0].iov_len = offsetof(struct ctdb_req_message, data);
623 memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
625 nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
626 if (nwritten == -1) {
627 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
628 cluster_fatal("cluster dispatch daemon msg write error\n");
631 return 0;
635 * send/recv a generic ctdb control message
637 static int ctdbd_control(struct ctdbd_connection *conn,
638 uint32_t vnn, uint32_t opcode,
639 uint64_t srvid, uint32_t flags,
640 TDB_DATA data,
641 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
642 int *cstatus)
644 struct ctdb_req_control req;
645 struct ctdb_req_header *hdr;
646 struct ctdb_reply_control *reply = NULL;
647 struct iovec iov[2];
648 ssize_t nwritten;
649 int ret;
651 ZERO_STRUCT(req);
652 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
653 req.hdr.ctdb_magic = CTDB_MAGIC;
654 req.hdr.ctdb_version = CTDB_PROTOCOL;
655 req.hdr.operation = CTDB_REQ_CONTROL;
656 req.hdr.reqid = ctdbd_next_reqid(conn);
657 req.hdr.destnode = vnn;
658 req.opcode = opcode;
659 req.srvid = srvid;
660 req.datalen = data.dsize;
661 req.flags = flags;
663 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
664 ctdb_packet_dump(&req.hdr);
666 iov[0].iov_base = &req;
667 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
668 iov[1].iov_base = data.dptr;
669 iov[1].iov_len = data.dsize;
671 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
672 if (nwritten == -1) {
673 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
674 cluster_fatal("cluster dispatch daemon msg write error\n");
677 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
678 if (cstatus) {
679 *cstatus = 0;
681 return 0;
684 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
685 if (ret != 0) {
686 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
687 return ret;
690 if (hdr->operation != CTDB_REPLY_CONTROL) {
691 DEBUG(0, ("received invalid reply\n"));
692 TALLOC_FREE(hdr);
693 return EIO;
695 reply = (struct ctdb_reply_control *)hdr;
697 if (outdata) {
698 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
699 mem_ctx, reply->data, reply->datalen))) {
700 TALLOC_FREE(reply);
701 return ENOMEM;
703 outdata->dsize = reply->datalen;
705 if (cstatus) {
706 (*cstatus) = reply->status;
709 TALLOC_FREE(reply);
710 return ret;
714 * see if a remote process exists
716 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
718 struct server_id id;
719 bool result;
721 id.pid = pid;
722 id.vnn = vnn;
724 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
725 DEBUG(10, ("ctdb_processes_exist failed\n"));
726 return false;
728 return result;
731 bool ctdb_processes_exist(struct ctdbd_connection *conn,
732 const struct server_id *pids, int num_pids,
733 bool *results)
735 TALLOC_CTX *frame = talloc_stackframe();
736 int i, num_received;
737 uint32_t *reqids;
738 bool result = false;
740 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
741 if (reqids == NULL) {
742 goto fail;
745 for (i=0; i<num_pids; i++) {
746 struct ctdb_req_control req;
747 pid_t pid;
748 struct iovec iov[2];
749 ssize_t nwritten;
751 results[i] = false;
752 reqids[i] = ctdbd_next_reqid(conn);
754 ZERO_STRUCT(req);
757 * pids[i].pid is uint64_t, scale down to pid_t which
758 * is the wire protocol towards ctdb.
760 pid = pids[i].pid;
762 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
763 (int)pids[i].vnn, (int)pid,
764 (int)reqids[i]));
766 req.hdr.length = offsetof(struct ctdb_req_control, data);
767 req.hdr.length += sizeof(pid);
768 req.hdr.ctdb_magic = CTDB_MAGIC;
769 req.hdr.ctdb_version = CTDB_PROTOCOL;
770 req.hdr.operation = CTDB_REQ_CONTROL;
771 req.hdr.reqid = reqids[i];
772 req.hdr.destnode = pids[i].vnn;
773 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
774 req.srvid = 0;
775 req.datalen = sizeof(pid);
776 req.flags = 0;
778 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
779 ctdb_packet_dump(&req.hdr);
781 iov[0].iov_base = &req;
782 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
783 iov[1].iov_base = &pid;
784 iov[1].iov_len = sizeof(pid);
786 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
787 if (nwritten == -1) {
788 DEBUG(10, ("write_data_iov failed: %s\n",
789 strerror(errno)));
790 goto fail;
794 num_received = 0;
796 while (num_received < num_pids) {
797 struct ctdb_req_header *hdr;
798 struct ctdb_reply_control *reply;
799 uint32_t reqid;
800 int ret;
802 ret = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
803 if (ret != 0) {
804 DEBUG(10, ("ctdb_read_req failed: %s\n",
805 strerror(ret)));
806 goto fail;
809 if (hdr->operation != CTDB_REPLY_CONTROL) {
810 DEBUG(10, ("Received invalid reply\n"));
811 goto fail;
813 reply = (struct ctdb_reply_control *)hdr;
815 reqid = reply->hdr.reqid;
817 DEBUG(10, ("Received reqid %d\n", (int)reqid));
819 for (i=0; i<num_pids; i++) {
820 if (reqid == reqids[i]) {
821 break;
824 if (i == num_pids) {
825 DEBUG(10, ("Received unknown record number %u\n",
826 (unsigned)reqid));
827 goto fail;
829 results[i] = ((reply->status) == 0);
830 TALLOC_FREE(reply);
831 num_received += 1;
834 result = true;
835 fail:
836 TALLOC_FREE(frame);
837 return result;
841 * Get a db path
843 char *ctdbd_dbpath(struct ctdbd_connection *conn,
844 TALLOC_CTX *mem_ctx, uint32_t db_id)
846 int ret;
847 TDB_DATA data;
848 TDB_DATA rdata = {0};
849 int32_t cstatus = 0;
851 data.dptr = (uint8_t*)&db_id;
852 data.dsize = sizeof(db_id);
854 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
855 CTDB_CONTROL_GETDBPATH, 0, 0, data,
856 mem_ctx, &rdata, &cstatus);
857 if ((ret != 0) || cstatus != 0) {
858 DEBUG(0, (__location__ " ctdb_control for getdbpath failed: %s\n",
859 strerror(ret)));
860 return NULL;
863 return (char *)rdata.dptr;
867 * attach to a ctdb database
869 int ctdbd_db_attach(struct ctdbd_connection *conn,
870 const char *name, uint32_t *db_id, int tdb_flags)
872 int ret;
873 TDB_DATA data;
874 int32_t cstatus;
875 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
877 data = string_term_tdb_data(name);
879 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
880 persistent
881 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
882 : CTDB_CONTROL_DB_ATTACH,
883 tdb_flags, 0, data, NULL, &data, &cstatus);
884 if (ret != 0) {
885 DEBUG(0, (__location__ " ctdb_control for db_attach "
886 "failed: %s\n", strerror(ret)));
887 return ret;
890 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
891 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
892 return EIO;
895 *db_id = *(uint32_t *)data.dptr;
896 talloc_free(data.dptr);
898 if (!(tdb_flags & TDB_SEQNUM)) {
899 return 0;
902 data.dptr = (uint8_t *)db_id;
903 data.dsize = sizeof(*db_id);
905 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
906 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
907 NULL, NULL, &cstatus);
908 if ((ret != 0) || cstatus != 0) {
909 DEBUG(0, (__location__ " ctdb_control for enable seqnum "
910 "failed: %s\n", strerror(ret)));
911 return (ret == 0) ? EIO : ret;
914 return 0;
918 * force the migration of a record to this node
920 int ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key)
922 struct ctdb_req_call req;
923 struct ctdb_req_header *hdr;
924 struct iovec iov[2];
925 ssize_t nwritten;
926 int ret;
928 ZERO_STRUCT(req);
930 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
931 req.hdr.ctdb_magic = CTDB_MAGIC;
932 req.hdr.ctdb_version = CTDB_PROTOCOL;
933 req.hdr.operation = CTDB_REQ_CALL;
934 req.hdr.reqid = ctdbd_next_reqid(conn);
935 req.flags = CTDB_IMMEDIATE_MIGRATION;
936 req.callid = CTDB_NULL_FUNC;
937 req.db_id = db_id;
938 req.keylen = key.dsize;
940 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
941 ctdb_packet_dump(&req.hdr);
943 iov[0].iov_base = &req;
944 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
945 iov[1].iov_base = key.dptr;
946 iov[1].iov_len = key.dsize;
948 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
949 if (nwritten == -1) {
950 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
951 cluster_fatal("cluster dispatch daemon msg write error\n");
954 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
955 if (ret != 0) {
956 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
957 goto fail;
960 if (hdr->operation != CTDB_REPLY_CALL) {
961 DEBUG(0, ("received invalid reply\n"));
962 goto fail;
965 fail:
967 TALLOC_FREE(hdr);
968 return ret;
972 * Fetch a record and parse it
974 int ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
975 TDB_DATA key, bool local_copy,
976 void (*parser)(TDB_DATA key, TDB_DATA data,
977 void *private_data),
978 void *private_data)
980 struct ctdb_req_call req;
981 struct ctdb_req_header *hdr = NULL;
982 struct ctdb_reply_call *reply;
983 struct iovec iov[2];
984 ssize_t nwritten;
985 uint32_t flags;
986 int ret;
988 flags = local_copy ? CTDB_WANT_READONLY : 0;
990 ZERO_STRUCT(req);
992 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
993 req.hdr.ctdb_magic = CTDB_MAGIC;
994 req.hdr.ctdb_version = CTDB_PROTOCOL;
995 req.hdr.operation = CTDB_REQ_CALL;
996 req.hdr.reqid = ctdbd_next_reqid(conn);
997 req.flags = flags;
998 req.callid = CTDB_FETCH_FUNC;
999 req.db_id = db_id;
1000 req.keylen = key.dsize;
1002 iov[0].iov_base = &req;
1003 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1004 iov[1].iov_base = key.dptr;
1005 iov[1].iov_len = key.dsize;
1007 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1008 if (nwritten == -1) {
1009 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1010 cluster_fatal("cluster dispatch daemon msg write error\n");
1013 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1014 if (ret != 0) {
1015 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
1016 goto fail;
1019 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
1020 DEBUG(0, ("received invalid reply\n"));
1021 ret = EIO;
1022 goto fail;
1024 reply = (struct ctdb_reply_call *)hdr;
1026 if (reply->datalen == 0) {
1028 * Treat an empty record as non-existing
1030 ret = ENOENT;
1031 goto fail;
1034 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1035 private_data);
1037 ret = 0;
1038 fail:
1039 TALLOC_FREE(hdr);
1040 return ret;
1044 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1045 connection to ctdbd to avoid the hairy recursive and async problems with
1046 everything in-line.
1049 int ctdbd_traverse(struct ctdbd_connection *master, uint32_t db_id,
1050 void (*fn)(TDB_DATA key, TDB_DATA data,
1051 void *private_data),
1052 void *private_data)
1054 struct ctdbd_connection *conn;
1055 int ret;
1056 TDB_DATA key, data;
1057 struct ctdb_traverse_start t;
1058 int cstatus;
1060 become_root();
1061 ret = ctdbd_init_connection(NULL, master->sockname, master->timeout,
1062 &conn);
1063 unbecome_root();
1064 if (ret != 0) {
1065 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1066 strerror(ret)));
1067 return ret;
1070 t.db_id = db_id;
1071 t.srvid = conn->rand_srvid;
1072 t.reqid = ctdbd_next_reqid(conn);
1074 data.dptr = (uint8_t *)&t;
1075 data.dsize = sizeof(t);
1077 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
1078 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid,
1079 0, data, NULL, NULL, &cstatus);
1081 if ((ret != 0) || (cstatus != 0)) {
1082 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
1083 cstatus));
1085 if (ret == 0) {
1087 * We need a mapping here
1089 ret = EIO;
1091 TALLOC_FREE(conn);
1092 return ret;
1095 while (True) {
1096 struct ctdb_req_header *hdr = NULL;
1097 struct ctdb_req_message *m;
1098 struct ctdb_rec_data *d;
1100 ret = ctdb_read_packet(conn->fd, conn->timeout, conn, &hdr);
1101 if (ret != 0) {
1102 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1103 strerror(ret)));
1104 cluster_fatal("ctdbd died\n");
1107 if (hdr->operation != CTDB_REQ_MESSAGE) {
1108 DEBUG(0, ("Got operation %u, expected a message\n",
1109 (unsigned)hdr->operation));
1110 TALLOC_FREE(conn);
1111 return EIO;
1114 m = (struct ctdb_req_message *)hdr;
1115 d = (struct ctdb_rec_data *)&m->data[0];
1116 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1117 DEBUG(0, ("Got invalid traverse data of length %d\n",
1118 (int)m->datalen));
1119 TALLOC_FREE(conn);
1120 return EIO;
1123 key.dsize = d->keylen;
1124 key.dptr = &d->data[0];
1125 data.dsize = d->datalen;
1126 data.dptr = &d->data[d->keylen];
1128 if (key.dsize == 0 && data.dsize == 0) {
1129 /* end of traverse */
1130 TALLOC_FREE(conn);
1131 return 0;
1134 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1135 DEBUG(0, ("Got invalid ltdb header length %d\n",
1136 (int)data.dsize));
1137 TALLOC_FREE(conn);
1138 return EIO;
1140 data.dsize -= sizeof(struct ctdb_ltdb_header);
1141 data.dptr += sizeof(struct ctdb_ltdb_header);
1143 if (fn != NULL) {
1144 fn(key, data, private_data);
1147 return 0;
1151 This is used to canonicalize a ctdb_sock_addr structure.
1153 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1154 struct sockaddr_storage *out)
1156 memcpy(out, in, sizeof (*out));
1158 #ifdef HAVE_IPV6
1159 if (in->ss_family == AF_INET6) {
1160 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1161 const struct sockaddr_in6 *in6 =
1162 (const struct sockaddr_in6 *)in;
1163 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1164 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1165 memset(out, 0, sizeof(*out));
1166 #ifdef HAVE_SOCK_SIN_LEN
1167 out4->sin_len = sizeof(*out);
1168 #endif
1169 out4->sin_family = AF_INET;
1170 out4->sin_port = in6->sin6_port;
1171 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1174 #endif
1178 * Register us as a server for a particular tcp connection
1181 int ctdbd_register_ips(struct ctdbd_connection *conn,
1182 const struct sockaddr_storage *_server,
1183 const struct sockaddr_storage *_client,
1184 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
1185 uint64_t dst_srvid,
1186 const uint8_t *msg, size_t msglen,
1187 void *private_data),
1188 void *private_data)
1190 struct ctdb_control_tcp_addr p;
1191 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1192 int ret;
1193 struct sockaddr_storage client;
1194 struct sockaddr_storage server;
1197 * Only one connection so far
1200 smbd_ctdb_canonicalize_ip(_client, &client);
1201 smbd_ctdb_canonicalize_ip(_server, &server);
1203 switch (client.ss_family) {
1204 case AF_INET:
1205 memcpy(&p.dest.ip, &server, sizeof(p.dest.ip));
1206 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1207 break;
1208 case AF_INET6:
1209 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1210 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1211 break;
1212 default:
1213 return EIO;
1217 * We want to be told about IP releases
1220 ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1221 cb, private_data);
1222 if (ret != 0) {
1223 return ret;
1227 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1228 * can send an extra ack to trigger a reset for our client, so it
1229 * immediately reconnects
1231 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
1232 CTDB_CONTROL_TCP_CLIENT, 0,
1233 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL,
1234 NULL);
1235 if (ret != 0) {
1236 return ret;
1238 return 0;
1242 call a control on the local node
1244 int ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1245 uint64_t srvid, uint32_t flags, TDB_DATA data,
1246 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1247 int *cstatus)
1249 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data,
1250 mem_ctx, outdata, cstatus);
1253 int ctdb_watch_us(struct ctdbd_connection *conn)
1255 struct ctdb_client_notify_register reg_data;
1256 size_t struct_len;
1257 int ret;
1258 int cstatus;
1260 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1261 reg_data.len = 1;
1262 reg_data.notify_data[0] = 0;
1264 struct_len = offsetof(struct ctdb_client_notify_register,
1265 notify_data) + reg_data.len;
1267 ret = ctdbd_control_local(
1268 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1269 make_tdb_data((uint8_t *)&reg_data, struct_len),
1270 NULL, NULL, &cstatus);
1271 if (ret != 0) {
1272 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1273 strerror(ret)));
1275 return ret;
1278 int ctdb_unwatch(struct ctdbd_connection *conn)
1280 struct ctdb_client_notify_deregister dereg_data;
1281 int ret;
1282 int cstatus;
1284 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1286 ret = ctdbd_control_local(
1287 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1288 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1289 NULL, NULL, &cstatus);
1290 if (ret != 0) {
1291 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1292 strerror(ret)));
1294 return ret;
1297 int ctdbd_probe(const char *sockname, int timeout)
1300 * Do a very early check if ctdbd is around to avoid an abort and core
1301 * later
1303 struct ctdbd_connection *conn = NULL;
1304 int ret;
1306 ret = ctdbd_messaging_connection(talloc_tos(), sockname, timeout,
1307 &conn);
1310 * We only care if we can connect.
1312 TALLOC_FREE(conn);
1314 return ret;