gssapi: check for gss_acquire_cred_from
[Samba.git] / source3 / lib / ctdbd_conn.c
blobd16796f4038279780836d3879f09e4dfba2dd998
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 /* paths to these include files come from --with-ctdb= in configure */
36 #include "ctdb_private.h"
38 struct ctdbd_srvid_cb {
39 uint64_t srvid;
40 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
41 uint64_t dst_srvid,
42 const uint8_t *msg, size_t msglen,
43 void *private_data);
44 void *private_data;
47 struct ctdbd_connection {
48 uint32_t reqid;
49 uint32_t our_vnn;
50 uint64_t rand_srvid;
51 struct ctdbd_srvid_cb *callbacks;
52 int fd;
53 int timeout;
56 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
58 conn->reqid += 1;
59 if (conn->reqid == 0) {
60 conn->reqid += 1;
62 return conn->reqid;
65 static int ctdbd_control(struct ctdbd_connection *conn,
66 uint32_t vnn, uint32_t opcode,
67 uint64_t srvid, uint32_t flags,
68 TDB_DATA data,
69 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
70 int32_t *cstatus);
73 * exit on fatal communications errors with the ctdbd daemon
75 static void cluster_fatal(const char *why)
77 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
78 /* we don't use smb_panic() as we don't want to delay to write
79 a core file. We need to release this process id immediately
80 so that someone else can take over without getting sharing
81 violations */
82 _exit(1);
88 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
90 if (DEBUGLEVEL < 11) {
91 return;
93 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
94 (int)hdr->length, (int)hdr->ctdb_magic,
95 (int)hdr->ctdb_version, (int)hdr->generation,
96 (int)hdr->operation, (int)hdr->reqid));
100 * Register a srvid with ctdbd
102 int register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid,
103 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
104 uint64_t dst_srvid,
105 const uint8_t *msg, size_t msglen,
106 void *private_data),
107 void *private_data)
110 int ret;
111 int32_t cstatus;
112 size_t num_callbacks;
113 struct ctdbd_srvid_cb *tmp;
115 ret = ctdbd_control_local(conn, CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
116 tdb_null, NULL, NULL, &cstatus);
117 if (ret != 0) {
118 return ret;
121 num_callbacks = talloc_array_length(conn->callbacks);
123 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
124 num_callbacks + 1);
125 if (tmp == NULL) {
126 return ENOMEM;
128 conn->callbacks = tmp;
130 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
131 .srvid = srvid, .cb = cb, .private_data = private_data
134 return 0;
137 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
138 struct ctdb_req_message_old *msg)
140 uint32_t msg_len;
141 size_t i, num_callbacks;
143 msg_len = msg->hdr.length;
144 if (msg_len < offsetof(struct ctdb_req_message_old, data)) {
145 DBG_DEBUG("len %"PRIu32" too small\n", msg_len);
146 return 0;
148 msg_len -= offsetof(struct ctdb_req_message_old, data);
150 if (msg_len < msg->datalen) {
151 DBG_DEBUG("msg_len=%"PRIu32" < msg->datalen=%"PRIu32"\n",
152 msg_len, msg->datalen);
153 return 0;
156 num_callbacks = talloc_array_length(conn->callbacks);
158 for (i=0; i<num_callbacks; i++) {
159 struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
161 if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
162 int ret;
164 ret = cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
165 msg->srvid, msg->data, msg->datalen,
166 cb->private_data);
167 if (ret != 0) {
168 return ret;
172 return 0;
176 * get our vnn from the cluster
178 static int get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
180 int32_t cstatus=-1;
181 int ret;
182 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_PNN, 0, 0,
183 tdb_null, NULL, NULL, &cstatus);
184 if (ret != 0) {
185 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
186 return ret;
188 *vnn = (uint32_t)cstatus;
189 return ret;
193 * Are we active (i.e. not banned or stopped?)
195 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
197 int32_t cstatus=-1;
198 TDB_DATA outdata;
199 struct ctdb_node_map_old *m;
200 bool ok = false;
201 uint32_t i;
202 int ret;
204 ret = ctdbd_control_local(conn, CTDB_CONTROL_GET_NODEMAP, 0, 0,
205 tdb_null, talloc_tos(), &outdata, &cstatus);
206 if (ret != 0) {
207 DEBUG(1, ("ctdbd_control failed: %s\n", strerror(ret)));
208 return false;
210 if ((cstatus != 0) || (outdata.dptr == NULL)) {
211 DEBUG(2, ("Received invalid ctdb data\n"));
212 return false;
215 m = (struct ctdb_node_map_old *)outdata.dptr;
217 for (i=0; i<m->num; i++) {
218 if (vnn == m->nodes[i].pnn) {
219 break;
223 if (i == m->num) {
224 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
225 (int)vnn));
226 goto fail;
229 if ((m->nodes[i].flags & NODE_FLAGS_INACTIVE) != 0) {
230 DEBUG(2, ("Node has status %x, not active\n",
231 (int)m->nodes[i].flags));
232 goto fail;
235 ok = true;
236 fail:
237 TALLOC_FREE(outdata.dptr);
238 return ok;
241 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
243 return conn->our_vnn;
247 * Get us a ctdb connection
250 static int ctdbd_connect(const char *sockname, int *pfd)
252 struct sockaddr_un addr = { 0, };
253 int fd;
254 socklen_t salen;
255 size_t namelen;
257 fd = socket(AF_UNIX, SOCK_STREAM, 0);
258 if (fd == -1) {
259 int err = errno;
260 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
261 return err;
264 addr.sun_family = AF_UNIX;
266 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
267 if (namelen >= sizeof(addr.sun_path)) {
268 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
269 sockname));
270 close(fd);
271 return ENAMETOOLONG;
274 salen = sizeof(struct sockaddr_un);
276 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
277 int err = errno;
278 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
279 strerror(err)));
280 close(fd);
281 return err;
284 *pfd = fd;
285 return 0;
288 static int ctdb_read_packet(int fd, int timeout, TALLOC_CTX *mem_ctx,
289 struct ctdb_req_header **result)
291 struct ctdb_req_header *req;
292 uint32_t msglen;
293 ssize_t nread;
295 if (timeout != -1) {
296 struct pollfd pfd = { .fd = fd, .events = POLLIN };
297 int ret;
299 ret = sys_poll_intr(&pfd, 1, timeout);
300 if (ret == -1) {
301 return errno;
303 if (ret == 0) {
304 return ETIMEDOUT;
306 if (ret != 1) {
307 return EIO;
311 nread = read_data(fd, &msglen, sizeof(msglen));
312 if (nread == -1) {
313 return errno;
315 if (nread == 0) {
316 return EIO;
319 if (msglen < sizeof(struct ctdb_req_header)) {
320 return EIO;
323 req = talloc_size(mem_ctx, msglen);
324 if (req == NULL) {
325 return ENOMEM;
327 talloc_set_name_const(req, "struct ctdb_req_header");
329 req->length = msglen;
331 nread = read_data(fd, ((char *)req) + sizeof(msglen),
332 msglen - sizeof(msglen));
333 if (nread == -1) {
334 TALLOC_FREE(req);
335 return errno;
337 if (nread == 0) {
338 TALLOC_FREE(req);
339 return EIO;
342 *result = req;
343 return 0;
347 * Read a full ctdbd request. If we have a messaging context, defer incoming
348 * messages that might come in between.
351 static int ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
352 TALLOC_CTX *mem_ctx, struct ctdb_req_header **result)
354 struct ctdb_req_header *hdr;
355 int ret;
357 next_pkt:
359 ret = ctdb_read_packet(conn->fd, conn->timeout, mem_ctx, &hdr);
360 if (ret != 0) {
361 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
362 cluster_fatal("ctdbd died\n");
365 DEBUG(11, ("Received ctdb packet\n"));
366 ctdb_packet_dump(hdr);
368 if (hdr->operation == CTDB_REQ_MESSAGE) {
369 struct ctdb_req_message_old *msg = (struct ctdb_req_message_old *)hdr;
371 ret = ctdbd_msg_call_back(conn, msg);
372 if (ret != 0) {
373 TALLOC_FREE(hdr);
374 return ret;
377 TALLOC_FREE(hdr);
378 goto next_pkt;
381 if ((reqid != 0) && (hdr->reqid != reqid)) {
382 /* we got the wrong reply */
383 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
384 "been %u\n", hdr->reqid, reqid));
385 TALLOC_FREE(hdr);
386 goto next_pkt;
389 *result = talloc_move(mem_ctx, &hdr);
391 return 0;
394 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
396 if (c->fd != -1) {
397 close(c->fd);
398 c->fd = -1;
400 return 0;
403 * Get us a ctdbd connection
406 static int ctdbd_init_connection_internal(TALLOC_CTX *mem_ctx,
407 const char *sockname, int timeout,
408 struct ctdbd_connection *conn)
410 int ret;
412 conn->timeout = timeout;
413 if (conn->timeout == 0) {
414 conn->timeout = -1;
417 ret = ctdbd_connect(sockname, &conn->fd);
418 if (ret != 0) {
419 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
420 return ret;
422 talloc_set_destructor(conn, ctdbd_connection_destructor);
424 ret = get_cluster_vnn(conn, &conn->our_vnn);
425 if (ret != 0) {
426 DEBUG(10, ("get_cluster_vnn failed: %s\n", strerror(ret)));
427 return ret;
430 if (!ctdbd_working(conn, conn->our_vnn)) {
431 DEBUG(2, ("Node is not working, can not connect\n"));
432 return EIO;
435 generate_random_buffer((unsigned char *)&conn->rand_srvid,
436 sizeof(conn->rand_srvid));
438 ret = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
439 if (ret != 0) {
440 DEBUG(5, ("Could not register random srvid: %s\n",
441 strerror(ret)));
442 return ret;
445 return 0;
448 int ctdbd_init_connection(TALLOC_CTX *mem_ctx,
449 const char *sockname, int timeout,
450 struct ctdbd_connection **pconn)
452 struct ctdbd_connection *conn;
453 int ret;
455 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
456 DEBUG(0, ("talloc failed\n"));
457 return ENOMEM;
460 ret = ctdbd_init_connection_internal(mem_ctx,
461 sockname,
462 timeout,
463 conn);
464 if (ret != 0) {
465 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
466 strerror(ret));
467 goto fail;
470 *pconn = conn;
471 return 0;
473 fail:
474 TALLOC_FREE(conn);
475 return ret;
478 int ctdbd_reinit_connection(TALLOC_CTX *mem_ctx,
479 const char *sockname, int timeout,
480 struct ctdbd_connection *conn)
482 int ret;
484 ret = ctdbd_connection_destructor(conn);
485 if (ret != 0) {
486 DBG_ERR("ctdbd_connection_destructor failed\n");
487 return ret;
490 ret = ctdbd_init_connection_internal(mem_ctx,
491 sockname,
492 timeout,
493 conn);
494 if (ret != 0) {
495 DBG_ERR("ctdbd_init_connection_internal failed (%s)\n",
496 strerror(ret));
497 return ret;
500 return 0;
503 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
505 return conn->fd;
509 * Packet handler to receive and handle a ctdb message
511 static int ctdb_handle_message(struct ctdbd_connection *conn,
512 struct ctdb_req_header *hdr)
514 struct ctdb_req_message_old *msg;
516 if (hdr->operation != CTDB_REQ_MESSAGE) {
517 DEBUG(0, ("Received async msg of type %u, discarding\n",
518 hdr->operation));
519 return EINVAL;
522 msg = (struct ctdb_req_message_old *)hdr;
524 ctdbd_msg_call_back(conn, msg);
526 return 0;
529 void ctdbd_socket_readable(struct ctdbd_connection *conn)
531 struct ctdb_req_header *hdr = NULL;
532 int ret;
534 ret = ctdb_read_packet(conn->fd, conn->timeout, talloc_tos(), &hdr);
535 if (ret != 0) {
536 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
537 cluster_fatal("ctdbd died\n");
540 ret = ctdb_handle_message(conn, hdr);
542 TALLOC_FREE(hdr);
544 if (ret != 0) {
545 DEBUG(10, ("could not handle incoming message: %s\n",
546 strerror(ret)));
550 int ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
551 uint32_t dst_vnn, uint64_t dst_srvid,
552 const struct iovec *iov, int iovlen)
554 struct ctdb_req_message_old r;
555 struct iovec iov2[iovlen+1];
556 size_t buflen = iov_buflen(iov, iovlen);
557 ssize_t nwritten;
559 r.hdr.length = offsetof(struct ctdb_req_message_old, data) + buflen;
560 r.hdr.ctdb_magic = CTDB_MAGIC;
561 r.hdr.ctdb_version = CTDB_PROTOCOL;
562 r.hdr.generation = 1;
563 r.hdr.operation = CTDB_REQ_MESSAGE;
564 r.hdr.destnode = dst_vnn;
565 r.hdr.srcnode = conn->our_vnn;
566 r.hdr.reqid = 0;
567 r.srvid = dst_srvid;
568 r.datalen = buflen;
570 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
571 ctdb_packet_dump(&r.hdr);
573 iov2[0].iov_base = &r;
574 iov2[0].iov_len = offsetof(struct ctdb_req_message_old, data);
575 memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
577 nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
578 if (nwritten == -1) {
579 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
580 cluster_fatal("cluster dispatch daemon msg write error\n");
583 return 0;
587 * send/recv a generic ctdb control message
589 static int ctdbd_control(struct ctdbd_connection *conn,
590 uint32_t vnn, uint32_t opcode,
591 uint64_t srvid, uint32_t flags,
592 TDB_DATA data,
593 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
594 int32_t *cstatus)
596 struct ctdb_req_control_old req;
597 struct ctdb_req_header *hdr;
598 struct ctdb_reply_control_old *reply = NULL;
599 struct iovec iov[2];
600 ssize_t nwritten;
601 int ret;
603 ZERO_STRUCT(req);
604 req.hdr.length = offsetof(struct ctdb_req_control_old, data) + data.dsize;
605 req.hdr.ctdb_magic = CTDB_MAGIC;
606 req.hdr.ctdb_version = CTDB_PROTOCOL;
607 req.hdr.operation = CTDB_REQ_CONTROL;
608 req.hdr.reqid = ctdbd_next_reqid(conn);
609 req.hdr.destnode = vnn;
610 req.opcode = opcode;
611 req.srvid = srvid;
612 req.datalen = data.dsize;
613 req.flags = flags;
615 DBG_DEBUG("Sending ctdb packet reqid=%"PRIu32", vnn=%"PRIu32", "
616 "opcode=%"PRIu32", srvid=%"PRIu64"\n", req.hdr.reqid,
617 req.hdr.destnode, req.opcode, req.srvid);
618 ctdb_packet_dump(&req.hdr);
620 iov[0].iov_base = &req;
621 iov[0].iov_len = offsetof(struct ctdb_req_control_old, data);
622 iov[1].iov_base = data.dptr;
623 iov[1].iov_len = data.dsize;
625 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
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 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
632 if (cstatus) {
633 *cstatus = 0;
635 return 0;
638 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
639 if (ret != 0) {
640 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
641 return ret;
644 if (hdr->operation != CTDB_REPLY_CONTROL) {
645 DEBUG(0, ("received invalid reply\n"));
646 TALLOC_FREE(hdr);
647 return EIO;
649 reply = (struct ctdb_reply_control_old *)hdr;
651 if (outdata) {
652 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
653 mem_ctx, reply->data, reply->datalen))) {
654 TALLOC_FREE(reply);
655 return ENOMEM;
657 outdata->dsize = reply->datalen;
659 if (cstatus) {
660 (*cstatus) = reply->status;
663 TALLOC_FREE(reply);
664 return ret;
668 * see if a remote process exists
670 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
672 int32_t cstatus = 0;
673 int ret;
675 ret = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
676 (TDB_DATA) { .dptr = (uint8_t *)&pid,
677 .dsize = sizeof(pid) },
678 NULL, NULL, &cstatus);
679 if (ret != 0) {
680 return false;
682 return (cstatus == 0);
686 * Get a db path
688 char *ctdbd_dbpath(struct ctdbd_connection *conn,
689 TALLOC_CTX *mem_ctx, uint32_t db_id)
691 int ret;
692 TDB_DATA data;
693 TDB_DATA rdata = {0};
694 int32_t cstatus = 0;
696 data.dptr = (uint8_t*)&db_id;
697 data.dsize = sizeof(db_id);
699 ret = ctdbd_control_local(conn, CTDB_CONTROL_GETDBPATH, 0, 0, data,
700 mem_ctx, &rdata, &cstatus);
701 if ((ret != 0) || cstatus != 0) {
702 DEBUG(0, (__location__ " ctdb_control for getdbpath failed: %s\n",
703 strerror(ret)));
704 return NULL;
707 return (char *)rdata.dptr;
711 * attach to a ctdb database
713 int ctdbd_db_attach(struct ctdbd_connection *conn,
714 const char *name, uint32_t *db_id, int tdb_flags)
716 int ret;
717 TDB_DATA data;
718 int32_t cstatus;
719 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
721 data = string_term_tdb_data(name);
723 ret = ctdbd_control_local(conn,
724 persistent
725 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
726 : CTDB_CONTROL_DB_ATTACH,
727 tdb_flags, 0, data, NULL, &data, &cstatus);
728 if (ret != 0) {
729 DEBUG(0, (__location__ " ctdb_control for db_attach "
730 "failed: %s\n", strerror(ret)));
731 return ret;
734 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
735 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
736 return EIO;
739 *db_id = *(uint32_t *)data.dptr;
740 talloc_free(data.dptr);
742 if (!(tdb_flags & TDB_SEQNUM)) {
743 return 0;
746 data.dptr = (uint8_t *)db_id;
747 data.dsize = sizeof(*db_id);
749 ret = ctdbd_control_local(conn, CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
750 NULL, NULL, &cstatus);
751 if ((ret != 0) || cstatus != 0) {
752 DEBUG(0, (__location__ " ctdb_control for enable seqnum "
753 "failed: %s\n", strerror(ret)));
754 return (ret == 0) ? EIO : ret;
757 return 0;
761 * force the migration of a record to this node
763 int ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id, TDB_DATA key)
765 struct ctdb_req_call_old req;
766 struct ctdb_req_header *hdr = NULL;
767 struct iovec iov[2];
768 ssize_t nwritten;
769 int ret;
771 ZERO_STRUCT(req);
773 req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
774 req.hdr.ctdb_magic = CTDB_MAGIC;
775 req.hdr.ctdb_version = CTDB_PROTOCOL;
776 req.hdr.operation = CTDB_REQ_CALL;
777 req.hdr.reqid = ctdbd_next_reqid(conn);
778 req.flags = CTDB_IMMEDIATE_MIGRATION;
779 req.callid = CTDB_NULL_FUNC;
780 req.db_id = db_id;
781 req.keylen = key.dsize;
783 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
784 ctdb_packet_dump(&req.hdr);
786 iov[0].iov_base = &req;
787 iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
788 iov[1].iov_base = key.dptr;
789 iov[1].iov_len = key.dsize;
791 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
792 if (nwritten == -1) {
793 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
794 cluster_fatal("cluster dispatch daemon msg write error\n");
797 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
798 if (ret != 0) {
799 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
800 goto fail;
803 if (hdr->operation != CTDB_REPLY_CALL) {
804 if (hdr->operation == CTDB_REPLY_ERROR) {
805 DBG_ERR("received error from ctdb\n");
806 } else {
807 DBG_ERR("received invalid reply\n");
809 ret = EIO;
810 goto fail;
813 fail:
815 TALLOC_FREE(hdr);
816 return ret;
820 * Fetch a record and parse it
822 int ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
823 TDB_DATA key, bool local_copy,
824 void (*parser)(TDB_DATA key, TDB_DATA data,
825 void *private_data),
826 void *private_data)
828 struct ctdb_req_call_old req;
829 struct ctdb_req_header *hdr = NULL;
830 struct ctdb_reply_call_old *reply;
831 struct iovec iov[2];
832 ssize_t nwritten;
833 uint32_t flags;
834 int ret;
836 flags = local_copy ? CTDB_WANT_READONLY : 0;
838 ZERO_STRUCT(req);
840 req.hdr.length = offsetof(struct ctdb_req_call_old, data) + key.dsize;
841 req.hdr.ctdb_magic = CTDB_MAGIC;
842 req.hdr.ctdb_version = CTDB_PROTOCOL;
843 req.hdr.operation = CTDB_REQ_CALL;
844 req.hdr.reqid = ctdbd_next_reqid(conn);
845 req.flags = flags;
846 req.callid = CTDB_FETCH_FUNC;
847 req.db_id = db_id;
848 req.keylen = key.dsize;
850 iov[0].iov_base = &req;
851 iov[0].iov_len = offsetof(struct ctdb_req_call_old, data);
852 iov[1].iov_base = key.dptr;
853 iov[1].iov_len = key.dsize;
855 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
856 if (nwritten == -1) {
857 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
858 cluster_fatal("cluster dispatch daemon msg write error\n");
861 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
862 if (ret != 0) {
863 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
864 goto fail;
867 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
868 DEBUG(0, ("received invalid reply\n"));
869 ret = EIO;
870 goto fail;
872 reply = (struct ctdb_reply_call_old *)hdr;
874 if (reply->datalen == 0) {
876 * Treat an empty record as non-existing
878 ret = ENOENT;
879 goto fail;
882 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
883 private_data);
885 ret = 0;
886 fail:
887 TALLOC_FREE(hdr);
888 return ret;
892 Traverse a ctdb database. "conn" must be an otherwise unused
893 ctdb_connection where no other messages but the traverse ones are
894 expected.
897 int ctdbd_traverse(struct ctdbd_connection *conn, uint32_t db_id,
898 void (*fn)(TDB_DATA key, TDB_DATA data,
899 void *private_data),
900 void *private_data)
902 int ret;
903 TDB_DATA key, data;
904 struct ctdb_traverse_start t;
905 int32_t cstatus;
907 t.db_id = db_id;
908 t.srvid = conn->rand_srvid;
909 t.reqid = ctdbd_next_reqid(conn);
911 data.dptr = (uint8_t *)&t;
912 data.dsize = sizeof(t);
914 ret = ctdbd_control_local(conn, CTDB_CONTROL_TRAVERSE_START,
915 conn->rand_srvid,
916 0, data, NULL, NULL, &cstatus);
918 if ((ret != 0) || (cstatus != 0)) {
919 DEBUG(0,("ctdbd_control failed: %s, %d\n", strerror(ret),
920 cstatus));
922 if (ret == 0) {
924 * We need a mapping here
926 ret = EIO;
928 return ret;
931 while (true) {
932 struct ctdb_req_header *hdr = NULL;
933 struct ctdb_req_message_old *m;
934 struct ctdb_rec_data_old *d;
936 ret = ctdb_read_packet(conn->fd, conn->timeout, conn, &hdr);
937 if (ret != 0) {
938 DEBUG(0, ("ctdb_read_packet failed: %s\n",
939 strerror(ret)));
940 cluster_fatal("ctdbd died\n");
943 if (hdr->operation != CTDB_REQ_MESSAGE) {
944 DEBUG(0, ("Got operation %u, expected a message\n",
945 (unsigned)hdr->operation));
946 return EIO;
949 m = (struct ctdb_req_message_old *)hdr;
950 d = (struct ctdb_rec_data_old *)&m->data[0];
951 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
952 DEBUG(0, ("Got invalid traverse data of length %d\n",
953 (int)m->datalen));
954 return EIO;
957 key.dsize = d->keylen;
958 key.dptr = &d->data[0];
959 data.dsize = d->datalen;
960 data.dptr = &d->data[d->keylen];
962 if (key.dsize == 0 && data.dsize == 0) {
963 /* end of traverse */
964 return 0;
967 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
968 DEBUG(0, ("Got invalid ltdb header length %d\n",
969 (int)data.dsize));
970 return EIO;
972 data.dsize -= sizeof(struct ctdb_ltdb_header);
973 data.dptr += sizeof(struct ctdb_ltdb_header);
975 if (fn != NULL) {
976 fn(key, data, private_data);
979 return 0;
983 This is used to canonicalize a ctdb_sock_addr structure.
985 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
986 struct sockaddr_storage *out)
988 memcpy(out, in, sizeof (*out));
990 #ifdef HAVE_IPV6
991 if (in->ss_family == AF_INET6) {
992 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
993 const struct sockaddr_in6 *in6 =
994 (const struct sockaddr_in6 *)in;
995 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
996 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
997 memset(out, 0, sizeof(*out));
998 #ifdef HAVE_SOCK_SIN_LEN
999 out4->sin_len = sizeof(*out);
1000 #endif
1001 out4->sin_family = AF_INET;
1002 out4->sin_port = in6->sin6_port;
1003 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1006 #endif
1010 * Register us as a server for a particular tcp connection
1013 int ctdbd_register_ips(struct ctdbd_connection *conn,
1014 const struct sockaddr_storage *_server,
1015 const struct sockaddr_storage *_client,
1016 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
1017 uint64_t dst_srvid,
1018 const uint8_t *msg, size_t msglen,
1019 void *private_data),
1020 void *private_data)
1022 struct ctdb_connection p;
1023 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1024 int ret;
1025 struct sockaddr_storage client;
1026 struct sockaddr_storage server;
1029 * Only one connection so far
1032 smbd_ctdb_canonicalize_ip(_client, &client);
1033 smbd_ctdb_canonicalize_ip(_server, &server);
1035 switch (client.ss_family) {
1036 case AF_INET:
1037 memcpy(&p.dst.ip, &server, sizeof(p.dst.ip));
1038 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1039 break;
1040 case AF_INET6:
1041 memcpy(&p.dst.ip6, &server, sizeof(p.dst.ip6));
1042 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1043 break;
1044 default:
1045 return EIO;
1049 * We want to be told about IP releases
1052 ret = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1053 cb, private_data);
1054 if (ret != 0) {
1055 return ret;
1059 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1060 * can send an extra ack to trigger a reset for our client, so it
1061 * immediately reconnects
1063 ret = ctdbd_control(conn, CTDB_CURRENT_NODE,
1064 CTDB_CONTROL_TCP_CLIENT, 0,
1065 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL,
1066 NULL);
1067 if (ret != 0) {
1068 return ret;
1070 return 0;
1074 call a control on the local node
1076 int ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1077 uint64_t srvid, uint32_t flags, TDB_DATA data,
1078 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1079 int32_t *cstatus)
1081 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data,
1082 mem_ctx, outdata, cstatus);
1085 int ctdb_watch_us(struct ctdbd_connection *conn)
1087 struct ctdb_notify_data_old reg_data;
1088 size_t struct_len;
1089 int ret;
1090 int32_t cstatus;
1092 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1093 reg_data.len = 1;
1094 reg_data.notify_data[0] = 0;
1096 struct_len = offsetof(struct ctdb_notify_data_old,
1097 notify_data) + reg_data.len;
1099 ret = ctdbd_control_local(
1100 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1101 make_tdb_data((uint8_t *)&reg_data, struct_len),
1102 NULL, NULL, &cstatus);
1103 if (ret != 0) {
1104 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1105 strerror(ret)));
1107 return ret;
1110 int ctdb_unwatch(struct ctdbd_connection *conn)
1112 uint64_t srvid = CTDB_SRVID_SAMBA_NOTIFY;
1113 int ret;
1114 int32_t cstatus;
1116 ret = ctdbd_control_local(
1117 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1118 make_tdb_data((uint8_t *)&srvid, sizeof(srvid)),
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 ctdbd_probe(const char *sockname, int timeout)
1130 * Do a very early check if ctdbd is around to avoid an abort and core
1131 * later
1133 struct ctdbd_connection *conn = NULL;
1134 int ret;
1136 ret = ctdbd_init_connection(talloc_tos(), sockname, timeout,
1137 &conn);
1140 * We only care if we can connect.
1142 TALLOC_FREE(conn);
1144 return ret;