lib: Lift lp_ctdbd_socket() call up one level
[Samba.git] / source3 / lib / ctdbd_conn.c
blobc966c4f4ab3d2c67ab7e39ee81c17b4f3934555e
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 struct messaging_context *msg_ctx;
47 uint32_t reqid;
48 uint32_t our_vnn;
49 uint64_t rand_srvid;
50 struct ctdbd_srvid_cb *callbacks;
51 int fd;
52 struct tevent_fd *fde;
55 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
57 conn->reqid += 1;
58 if (conn->reqid == 0) {
59 conn->reqid += 1;
61 return conn->reqid;
64 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
65 uint32_t vnn, uint32_t opcode,
66 uint64_t srvid, uint32_t flags, TDB_DATA data,
67 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
68 int *cstatus);
71 * exit on fatal communications errors with the ctdbd daemon
73 static void cluster_fatal(const char *why)
75 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
76 /* we don't use smb_panic() as we don't want to delay to write
77 a core file. We need to release this process id immediately
78 so that someone else can take over without getting sharing
79 violations */
80 _exit(1);
86 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
88 if (DEBUGLEVEL < 11) {
89 return;
91 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
92 (int)hdr->length, (int)hdr->ctdb_magic,
93 (int)hdr->ctdb_version, (int)hdr->generation,
94 (int)hdr->operation, (int)hdr->reqid));
98 * Register a srvid with ctdbd
100 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid,
101 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
102 uint64_t dst_srvid,
103 const uint8_t *msg, size_t msglen,
104 void *private_data),
105 void *private_data)
108 NTSTATUS status;
109 int cstatus;
110 size_t num_callbacks;
111 struct ctdbd_srvid_cb *tmp;
113 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
114 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
115 tdb_null, NULL, NULL, &cstatus);
116 if (!NT_STATUS_IS_OK(status)) {
117 return status;
120 num_callbacks = talloc_array_length(conn->callbacks);
122 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
123 num_callbacks + 1);
124 if (tmp == NULL) {
125 return NT_STATUS_NO_MEMORY;
127 conn->callbacks = tmp;
129 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
130 .srvid = srvid, .cb = cb, .private_data = private_data
133 return NT_STATUS_OK;
136 static int ctdbd_msg_call_back(struct ctdbd_connection *conn,
137 struct ctdb_req_message *msg)
139 size_t msg_len;
140 size_t i, num_callbacks;
142 msg_len = msg->hdr.length;
143 if (msg_len < offsetof(struct ctdb_req_message, data)) {
144 DEBUG(10, ("%s: len %u too small\n", __func__,
145 (unsigned)msg_len));
146 return 0;
148 msg_len -= offsetof(struct ctdb_req_message, data);
150 if (msg_len < msg->datalen) {
151 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__,
152 (unsigned)msg_len, (unsigned)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 NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
180 int32_t cstatus=-1;
181 NTSTATUS status;
182 status = ctdbd_control(conn,
183 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
184 tdb_null, NULL, NULL, &cstatus);
185 if (!NT_STATUS_IS_OK(status)) {
186 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
187 return status;
189 *vnn = (uint32_t)cstatus;
190 return status;
194 * Are we active (i.e. not banned or stopped?)
196 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
198 int32_t cstatus=-1;
199 NTSTATUS status;
200 TDB_DATA outdata;
201 struct ctdb_node_map *m;
202 uint32_t failure_flags;
203 bool ret = false;
204 int i;
206 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
207 CTDB_CONTROL_GET_NODEMAP, 0, 0,
208 tdb_null, talloc_tos(), &outdata, &cstatus);
209 if (!NT_STATUS_IS_OK(status)) {
210 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
211 return false;
213 if ((cstatus != 0) || (outdata.dptr == NULL)) {
214 DEBUG(2, ("Received invalid ctdb data\n"));
215 return false;
218 m = (struct ctdb_node_map *)outdata.dptr;
220 for (i=0; i<m->num; i++) {
221 if (vnn == m->nodes[i].pnn) {
222 break;
226 if (i == m->num) {
227 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
228 (int)vnn));
229 goto fail;
232 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
233 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
235 if ((m->nodes[i].flags & failure_flags) != 0) {
236 DEBUG(2, ("Node has status %x, not active\n",
237 (int)m->nodes[i].flags));
238 goto fail;
241 ret = true;
242 fail:
243 TALLOC_FREE(outdata.dptr);
244 return ret;
247 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
249 return conn->our_vnn;
252 const char *lp_ctdbd_socket(void)
254 const char *ret;
256 ret = lp__ctdbd_socket();
257 if (ret != NULL && strlen(ret) > 0) {
258 return ret;
261 return CTDB_SOCKET;
265 * Get us a ctdb connection
268 static int ctdbd_connect(const char *sockname, int *pfd)
270 struct sockaddr_un addr = { 0, };
271 int fd;
272 socklen_t salen;
273 size_t namelen;
275 fd = socket(AF_UNIX, SOCK_STREAM, 0);
276 if (fd == -1) {
277 int err = errno;
278 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
279 return err;
282 addr.sun_family = AF_UNIX;
284 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
285 if (namelen >= sizeof(addr.sun_path)) {
286 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
287 sockname));
288 close(fd);
289 return ENAMETOOLONG;
292 salen = sizeof(struct sockaddr_un);
294 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
295 int err = errno;
296 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
297 strerror(err)));
298 close(fd);
299 return err;
302 *pfd = fd;
303 return 0;
306 static int ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
307 struct ctdb_req_header **result)
309 int timeout = lp_ctdb_timeout();
310 struct ctdb_req_header *req;
311 int ret, revents;
312 uint32_t msglen;
313 ssize_t nread;
315 if (timeout == 0) {
316 timeout = -1;
319 if (timeout != -1) {
320 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
321 if (ret == -1) {
322 return errno;
324 if (ret == 0) {
325 return ETIMEDOUT;
327 if (ret != 1) {
328 return EIO;
332 nread = read_data(fd, &msglen, sizeof(msglen));
333 if (nread == -1) {
334 return errno;
336 if (nread == 0) {
337 return EIO;
340 if (msglen < sizeof(struct ctdb_req_header)) {
341 return EIO;
344 req = talloc_size(mem_ctx, msglen);
345 if (req == NULL) {
346 return ENOMEM;
348 talloc_set_name_const(req, "struct ctdb_req_header");
350 req->length = msglen;
352 nread = read_data(fd, ((char *)req) + sizeof(msglen),
353 msglen - sizeof(msglen));
354 if (nread == -1) {
355 return errno;
357 if (nread == 0) {
358 return EIO;
361 *result = req;
362 return 0;
366 * Read a full ctdbd request. If we have a messaging context, defer incoming
367 * messages that might come in between.
370 static int ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
371 TALLOC_CTX *mem_ctx, struct ctdb_req_header **result)
373 struct ctdb_req_header *hdr;
374 int ret;
376 next_pkt:
378 ret = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
379 if (ret != 0) {
380 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
381 cluster_fatal("ctdbd died\n");
384 DEBUG(11, ("Received ctdb packet\n"));
385 ctdb_packet_dump(hdr);
387 if (hdr->operation == CTDB_REQ_MESSAGE) {
388 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
390 if (conn->msg_ctx == NULL) {
391 DEBUG(1, ("Got a message without having a msg ctx, "
392 "dropping msg %llu\n",
393 (long long unsigned)msg->srvid));
394 TALLOC_FREE(hdr);
395 goto next_pkt;
398 ret = ctdbd_msg_call_back(conn, msg);
399 if (ret != 0) {
400 TALLOC_FREE(hdr);
401 return ret;
404 TALLOC_FREE(hdr);
405 goto next_pkt;
408 if ((reqid != 0) && (hdr->reqid != reqid)) {
409 /* we got the wrong reply */
410 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
411 "been %u\n", hdr->reqid, reqid));
412 TALLOC_FREE(hdr);
413 goto next_pkt;
416 *result = talloc_move(mem_ctx, &hdr);
418 return 0;
421 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
423 close(c->fd);
424 return 0;
427 * Get us a ctdbd connection
430 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
431 struct ctdbd_connection **pconn)
433 const char *sockname = lp_ctdbd_socket();
434 struct ctdbd_connection *conn;
435 int ret;
436 NTSTATUS status;
438 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
439 DEBUG(0, ("talloc failed\n"));
440 return NT_STATUS_NO_MEMORY;
443 ret = ctdbd_connect(sockname, &conn->fd);
444 if (ret != 0) {
445 status = map_nt_error_from_unix(ret);
446 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
447 goto fail;
449 talloc_set_destructor(conn, ctdbd_connection_destructor);
451 status = get_cluster_vnn(conn, &conn->our_vnn);
453 if (!NT_STATUS_IS_OK(status)) {
454 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
455 goto fail;
458 if (!ctdbd_working(conn, conn->our_vnn)) {
459 DEBUG(2, ("Node is not working, can not connect\n"));
460 status = NT_STATUS_INTERNAL_DB_ERROR;
461 goto fail;
464 generate_random_buffer((unsigned char *)&conn->rand_srvid,
465 sizeof(conn->rand_srvid));
467 status = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
469 if (!NT_STATUS_IS_OK(status)) {
470 DEBUG(5, ("Could not register random srvid: %s\n",
471 nt_errstr(status)));
472 goto fail;
475 *pconn = conn;
476 return NT_STATUS_OK;
478 fail:
479 TALLOC_FREE(conn);
480 return status;
484 * Get us a ctdbd connection and register us as a process
487 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
488 struct ctdbd_connection **pconn)
490 struct ctdbd_connection *conn;
491 NTSTATUS status;
493 status = ctdbd_init_connection(mem_ctx, &conn);
495 if (!NT_STATUS_IS_OK(status)) {
496 return status;
499 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA, NULL, NULL);
500 if (!NT_STATUS_IS_OK(status)) {
501 goto fail;
504 *pconn = conn;
505 return NT_STATUS_OK;
507 fail:
508 TALLOC_FREE(conn);
509 return status;
512 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
514 return conn->msg_ctx;
517 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
519 return conn->fd;
523 * Packet handler to receive and handle a ctdb message
525 static int ctdb_handle_message(struct ctdbd_connection *conn,
526 struct ctdb_req_header *hdr)
528 struct ctdb_req_message *msg;
530 if (hdr->operation != CTDB_REQ_MESSAGE) {
531 DEBUG(0, ("Received async msg of type %u, discarding\n",
532 hdr->operation));
533 return EINVAL;
536 msg = (struct ctdb_req_message *)hdr;
538 ctdbd_msg_call_back(conn, msg);
540 return 0;
544 * The ctdbd socket is readable asynchronuously
547 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
548 struct tevent_fd *event,
549 uint16_t flags,
550 void *private_data)
552 struct ctdbd_connection *conn = talloc_get_type_abort(
553 private_data, struct ctdbd_connection);
554 struct ctdb_req_header *hdr = NULL;
555 int ret;
557 ret = ctdb_read_packet(conn->fd, talloc_tos(), &hdr);
558 if (ret != 0) {
559 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
560 cluster_fatal("ctdbd died\n");
563 ret = ctdb_handle_message(conn, hdr);
565 TALLOC_FREE(hdr);
567 if (ret != 0) {
568 DEBUG(10, ("could not handle incoming message: %s\n",
569 strerror(ret)));
574 * Prepare a ctdbd connection to receive messages
577 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
578 struct messaging_context *msg_ctx)
580 SMB_ASSERT(conn->msg_ctx == NULL);
581 SMB_ASSERT(conn->fde == NULL);
583 if (!(conn->fde = tevent_add_fd(messaging_tevent_context(msg_ctx),
584 conn,
585 conn->fd,
586 TEVENT_FD_READ,
587 ctdbd_socket_handler,
588 conn))) {
589 DEBUG(0, ("event_add_fd failed\n"));
590 return NT_STATUS_NO_MEMORY;
593 conn->msg_ctx = msg_ctx;
595 return NT_STATUS_OK;
598 NTSTATUS 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 NT_STATUS_OK;
635 * send/recv a generic ctdb control message
637 static NTSTATUS 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 ctdbd_connection *new_conn = NULL;
648 struct iovec iov[2];
649 ssize_t nwritten;
650 NTSTATUS status;
651 int ret;
653 if (conn == NULL) {
654 status = ctdbd_init_connection(NULL, &new_conn);
656 if (!NT_STATUS_IS_OK(status)) {
657 DEBUG(10, ("Could not init temp connection: %s\n",
658 nt_errstr(status)));
659 goto fail;
662 conn = new_conn;
665 ZERO_STRUCT(req);
666 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
667 req.hdr.ctdb_magic = CTDB_MAGIC;
668 req.hdr.ctdb_version = CTDB_PROTOCOL;
669 req.hdr.operation = CTDB_REQ_CONTROL;
670 req.hdr.reqid = ctdbd_next_reqid(conn);
671 req.hdr.destnode = vnn;
672 req.opcode = opcode;
673 req.srvid = srvid;
674 req.datalen = data.dsize;
675 req.flags = flags;
677 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
678 ctdb_packet_dump(&req.hdr);
680 iov[0].iov_base = &req;
681 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
682 iov[1].iov_base = data.dptr;
683 iov[1].iov_len = data.dsize;
685 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
686 if (nwritten == -1) {
687 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
688 cluster_fatal("cluster dispatch daemon msg write error\n");
691 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
692 TALLOC_FREE(new_conn);
693 if (cstatus) {
694 *cstatus = 0;
696 return NT_STATUS_OK;
699 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
700 if (ret != 0) {
701 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
702 status = map_nt_error_from_unix(ret);
703 goto fail;
706 if (hdr->operation != CTDB_REPLY_CONTROL) {
707 DEBUG(0, ("received invalid reply\n"));
708 goto fail;
710 reply = (struct ctdb_reply_control *)hdr;
712 if (outdata) {
713 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
714 mem_ctx, reply->data, reply->datalen))) {
715 TALLOC_FREE(reply);
716 return NT_STATUS_NO_MEMORY;
718 outdata->dsize = reply->datalen;
720 if (cstatus) {
721 (*cstatus) = reply->status;
724 status = NT_STATUS_OK;
726 fail:
727 TALLOC_FREE(new_conn);
728 TALLOC_FREE(reply);
729 return status;
733 * see if a remote process exists
735 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
737 struct server_id id;
738 bool result;
740 id.pid = pid;
741 id.vnn = vnn;
743 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
744 DEBUG(10, ("ctdb_processes_exist failed\n"));
745 return false;
747 return result;
750 bool ctdb_processes_exist(struct ctdbd_connection *conn,
751 const struct server_id *pids, int num_pids,
752 bool *results)
754 TALLOC_CTX *frame = talloc_stackframe();
755 int i, num_received;
756 uint32_t *reqids;
757 bool result = false;
759 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
760 if (reqids == NULL) {
761 goto fail;
764 for (i=0; i<num_pids; i++) {
765 struct ctdb_req_control req;
766 pid_t pid;
767 struct iovec iov[2];
768 ssize_t nwritten;
770 results[i] = false;
771 reqids[i] = ctdbd_next_reqid(conn);
773 ZERO_STRUCT(req);
776 * pids[i].pid is uint64_t, scale down to pid_t which
777 * is the wire protocol towards ctdb.
779 pid = pids[i].pid;
781 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
782 (int)pids[i].vnn, (int)pid,
783 (int)reqids[i]));
785 req.hdr.length = offsetof(struct ctdb_req_control, data);
786 req.hdr.length += sizeof(pid);
787 req.hdr.ctdb_magic = CTDB_MAGIC;
788 req.hdr.ctdb_version = CTDB_PROTOCOL;
789 req.hdr.operation = CTDB_REQ_CONTROL;
790 req.hdr.reqid = reqids[i];
791 req.hdr.destnode = pids[i].vnn;
792 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
793 req.srvid = 0;
794 req.datalen = sizeof(pid);
795 req.flags = 0;
797 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
798 ctdb_packet_dump(&req.hdr);
800 iov[0].iov_base = &req;
801 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
802 iov[1].iov_base = &pid;
803 iov[1].iov_len = sizeof(pid);
805 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
806 if (nwritten == -1) {
807 DEBUG(10, ("write_data_iov failed: %s\n",
808 strerror(errno)));
809 goto fail;
813 num_received = 0;
815 while (num_received < num_pids) {
816 struct ctdb_req_header *hdr;
817 struct ctdb_reply_control *reply;
818 uint32_t reqid;
819 int ret;
821 ret = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
822 if (ret != 0) {
823 DEBUG(10, ("ctdb_read_req failed: %s\n",
824 strerror(ret)));
825 goto fail;
828 if (hdr->operation != CTDB_REPLY_CONTROL) {
829 DEBUG(10, ("Received invalid reply\n"));
830 goto fail;
832 reply = (struct ctdb_reply_control *)hdr;
834 reqid = reply->hdr.reqid;
836 DEBUG(10, ("Received reqid %d\n", (int)reqid));
838 for (i=0; i<num_pids; i++) {
839 if (reqid == reqids[i]) {
840 break;
843 if (i == num_pids) {
844 DEBUG(10, ("Received unknown record number %u\n",
845 (unsigned)reqid));
846 goto fail;
848 results[i] = ((reply->status) == 0);
849 TALLOC_FREE(reply);
850 num_received += 1;
853 result = true;
854 fail:
855 TALLOC_FREE(frame);
856 return result;
860 * Get a db path
862 char *ctdbd_dbpath(struct ctdbd_connection *conn,
863 TALLOC_CTX *mem_ctx, uint32_t db_id)
865 NTSTATUS status;
866 TDB_DATA data;
867 TDB_DATA rdata = {0};
868 int32_t cstatus = 0;
870 data.dptr = (uint8_t*)&db_id;
871 data.dsize = sizeof(db_id);
873 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
874 CTDB_CONTROL_GETDBPATH, 0, 0, data,
875 mem_ctx, &rdata, &cstatus);
876 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
877 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
878 return NULL;
881 return (char *)rdata.dptr;
885 * attach to a ctdb database
887 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
888 const char *name, uint32_t *db_id, int tdb_flags)
890 NTSTATUS status;
891 TDB_DATA data;
892 int32_t cstatus;
893 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
895 data = string_term_tdb_data(name);
897 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
898 persistent
899 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
900 : CTDB_CONTROL_DB_ATTACH,
901 tdb_flags, 0, data, NULL, &data, &cstatus);
902 if (!NT_STATUS_IS_OK(status)) {
903 DEBUG(0, (__location__ " ctdb_control for db_attach "
904 "failed: %s\n", nt_errstr(status)));
905 return status;
908 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
909 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
910 return NT_STATUS_INTERNAL_ERROR;
913 *db_id = *(uint32_t *)data.dptr;
914 talloc_free(data.dptr);
916 if (!(tdb_flags & TDB_SEQNUM)) {
917 return NT_STATUS_OK;
920 data.dptr = (uint8_t *)db_id;
921 data.dsize = sizeof(*db_id);
923 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
924 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
925 NULL, NULL, &cstatus);
926 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
927 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
928 "failed\n"));
929 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
930 status;
933 return NT_STATUS_OK;
937 * force the migration of a record to this node
939 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
940 TDB_DATA key)
942 struct ctdb_req_call req;
943 struct ctdb_req_header *hdr;
944 struct iovec iov[2];
945 ssize_t nwritten;
946 NTSTATUS status;
947 int ret;
949 ZERO_STRUCT(req);
951 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
952 req.hdr.ctdb_magic = CTDB_MAGIC;
953 req.hdr.ctdb_version = CTDB_PROTOCOL;
954 req.hdr.operation = CTDB_REQ_CALL;
955 req.hdr.reqid = ctdbd_next_reqid(conn);
956 req.flags = CTDB_IMMEDIATE_MIGRATION;
957 req.callid = CTDB_NULL_FUNC;
958 req.db_id = db_id;
959 req.keylen = key.dsize;
961 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
962 ctdb_packet_dump(&req.hdr);
964 iov[0].iov_base = &req;
965 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
966 iov[1].iov_base = key.dptr;
967 iov[1].iov_len = key.dsize;
969 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
970 if (nwritten == -1) {
971 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
972 cluster_fatal("cluster dispatch daemon msg write error\n");
975 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
976 if (ret != 0) {
977 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
978 status = map_nt_error_from_unix(ret);
979 goto fail;
982 if (hdr->operation != CTDB_REPLY_CALL) {
983 DEBUG(0, ("received invalid reply\n"));
984 status = NT_STATUS_INTERNAL_ERROR;
985 goto fail;
988 status = NT_STATUS_OK;
989 fail:
991 TALLOC_FREE(hdr);
992 return status;
996 * Fetch a record and parse it
998 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
999 TDB_DATA key, bool local_copy,
1000 void (*parser)(TDB_DATA key, TDB_DATA data,
1001 void *private_data),
1002 void *private_data)
1004 struct ctdb_req_call req;
1005 struct ctdb_req_header *hdr = NULL;
1006 struct ctdb_reply_call *reply;
1007 struct iovec iov[2];
1008 ssize_t nwritten;
1009 NTSTATUS status;
1010 uint32_t flags;
1011 int ret;
1013 flags = local_copy ? CTDB_WANT_READONLY : 0;
1015 ZERO_STRUCT(req);
1017 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1018 req.hdr.ctdb_magic = CTDB_MAGIC;
1019 req.hdr.ctdb_version = CTDB_PROTOCOL;
1020 req.hdr.operation = CTDB_REQ_CALL;
1021 req.hdr.reqid = ctdbd_next_reqid(conn);
1022 req.flags = flags;
1023 req.callid = CTDB_FETCH_FUNC;
1024 req.db_id = db_id;
1025 req.keylen = key.dsize;
1027 iov[0].iov_base = &req;
1028 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1029 iov[1].iov_base = key.dptr;
1030 iov[1].iov_len = key.dsize;
1032 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1033 if (nwritten == -1) {
1034 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1035 cluster_fatal("cluster dispatch daemon msg write error\n");
1038 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1039 if (ret != 0) {
1040 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
1041 status = map_nt_error_from_unix(ret);
1042 goto fail;
1045 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
1046 DEBUG(0, ("received invalid reply\n"));
1047 status = NT_STATUS_INTERNAL_ERROR;
1048 goto fail;
1050 reply = (struct ctdb_reply_call *)hdr;
1052 if (reply->datalen == 0) {
1054 * Treat an empty record as non-existing
1056 status = NT_STATUS_NOT_FOUND;
1057 goto fail;
1060 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1061 private_data);
1063 status = NT_STATUS_OK;
1064 fail:
1065 TALLOC_FREE(hdr);
1066 return status;
1070 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1071 connection to ctdbd to avoid the hairy recursive and async problems with
1072 everything in-line.
1075 NTSTATUS ctdbd_traverse(uint32_t db_id,
1076 void (*fn)(TDB_DATA key, TDB_DATA data,
1077 void *private_data),
1078 void *private_data)
1080 struct ctdbd_connection *conn;
1081 NTSTATUS status;
1083 TDB_DATA key, data;
1084 struct ctdb_traverse_start t;
1085 int cstatus;
1087 become_root();
1088 status = ctdbd_init_connection(NULL, &conn);
1089 unbecome_root();
1090 if (!NT_STATUS_IS_OK(status)) {
1091 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1092 nt_errstr(status)));
1093 return status;
1096 t.db_id = db_id;
1097 t.srvid = conn->rand_srvid;
1098 t.reqid = ctdbd_next_reqid(conn);
1100 data.dptr = (uint8_t *)&t;
1101 data.dsize = sizeof(t);
1103 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1104 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1105 data, NULL, NULL, &cstatus);
1107 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1109 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1110 cstatus));
1112 if (NT_STATUS_IS_OK(status)) {
1114 * We need a mapping here
1116 status = NT_STATUS_UNSUCCESSFUL;
1118 TALLOC_FREE(conn);
1119 return status;
1122 while (True) {
1123 struct ctdb_req_header *hdr = NULL;
1124 struct ctdb_req_message *m;
1125 struct ctdb_rec_data *d;
1126 int ret;
1128 ret = ctdb_read_packet(conn->fd, conn, &hdr);
1129 if (ret != 0) {
1130 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1131 strerror(ret)));
1132 cluster_fatal("ctdbd died\n");
1135 if (hdr->operation != CTDB_REQ_MESSAGE) {
1136 DEBUG(0, ("Got operation %u, expected a message\n",
1137 (unsigned)hdr->operation));
1138 TALLOC_FREE(conn);
1139 return NT_STATUS_UNEXPECTED_IO_ERROR;
1142 m = (struct ctdb_req_message *)hdr;
1143 d = (struct ctdb_rec_data *)&m->data[0];
1144 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1145 DEBUG(0, ("Got invalid traverse data of length %d\n",
1146 (int)m->datalen));
1147 TALLOC_FREE(conn);
1148 return NT_STATUS_UNEXPECTED_IO_ERROR;
1151 key.dsize = d->keylen;
1152 key.dptr = &d->data[0];
1153 data.dsize = d->datalen;
1154 data.dptr = &d->data[d->keylen];
1156 if (key.dsize == 0 && data.dsize == 0) {
1157 /* end of traverse */
1158 TALLOC_FREE(conn);
1159 return NT_STATUS_OK;
1162 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1163 DEBUG(0, ("Got invalid ltdb header length %d\n",
1164 (int)data.dsize));
1165 TALLOC_FREE(conn);
1166 return NT_STATUS_UNEXPECTED_IO_ERROR;
1168 data.dsize -= sizeof(struct ctdb_ltdb_header);
1169 data.dptr += sizeof(struct ctdb_ltdb_header);
1171 if (fn != NULL) {
1172 fn(key, data, private_data);
1175 return NT_STATUS_OK;
1179 This is used to canonicalize a ctdb_sock_addr structure.
1181 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1182 struct sockaddr_storage *out)
1184 memcpy(out, in, sizeof (*out));
1186 #ifdef HAVE_IPV6
1187 if (in->ss_family == AF_INET6) {
1188 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1189 const struct sockaddr_in6 *in6 =
1190 (const struct sockaddr_in6 *)in;
1191 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1192 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1193 memset(out, 0, sizeof(*out));
1194 #ifdef HAVE_SOCK_SIN_LEN
1195 out4->sin_len = sizeof(*out);
1196 #endif
1197 out4->sin_family = AF_INET;
1198 out4->sin_port = in6->sin6_port;
1199 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1202 #endif
1206 * Register us as a server for a particular tcp connection
1209 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1210 const struct sockaddr_storage *_server,
1211 const struct sockaddr_storage *_client,
1212 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
1213 uint64_t dst_srvid,
1214 const uint8_t *msg, size_t msglen,
1215 void *private_data),
1216 void *private_data)
1218 struct ctdb_control_tcp_addr p;
1219 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1220 NTSTATUS status;
1221 struct sockaddr_storage client;
1222 struct sockaddr_storage server;
1225 * Only one connection so far
1228 smbd_ctdb_canonicalize_ip(_client, &client);
1229 smbd_ctdb_canonicalize_ip(_server, &server);
1231 switch (client.ss_family) {
1232 case AF_INET:
1233 memcpy(&p.dest.ip, &server, sizeof(p.dest.ip));
1234 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1235 break;
1236 case AF_INET6:
1237 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1238 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1239 break;
1240 default:
1241 return NT_STATUS_INTERNAL_ERROR;
1245 * We want to be told about IP releases
1248 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1249 cb, private_data);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 return status;
1255 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1256 * can send an extra ack to trigger a reset for our client, so it
1257 * immediately reconnects
1259 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1260 CTDB_CONTROL_TCP_CLIENT, 0,
1261 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1265 call a control on the local node
1267 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1268 uint64_t srvid, uint32_t flags, TDB_DATA data,
1269 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1270 int *cstatus)
1272 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1275 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1277 struct ctdb_client_notify_register reg_data;
1278 size_t struct_len;
1279 NTSTATUS status;
1280 int cstatus;
1282 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1283 reg_data.len = 1;
1284 reg_data.notify_data[0] = 0;
1286 struct_len = offsetof(struct ctdb_client_notify_register,
1287 notify_data) + reg_data.len;
1289 status = ctdbd_control_local(
1290 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1291 make_tdb_data((uint8_t *)&reg_data, struct_len),
1292 NULL, NULL, &cstatus);
1293 if (!NT_STATUS_IS_OK(status)) {
1294 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1295 nt_errstr(status)));
1297 return status;
1300 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1302 struct ctdb_client_notify_deregister dereg_data;
1303 NTSTATUS status;
1304 int cstatus;
1306 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1308 status = ctdbd_control_local(
1309 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1310 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1311 NULL, NULL, &cstatus);
1312 if (!NT_STATUS_IS_OK(status)) {
1313 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1314 nt_errstr(status)));
1316 return status;
1319 NTSTATUS ctdbd_probe(void)
1322 * Do a very early check if ctdbd is around to avoid an abort and core
1323 * later
1325 struct ctdbd_connection *conn = NULL;
1326 NTSTATUS status;
1328 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1331 * We only care if we can connect.
1333 TALLOC_FREE(conn);
1335 return status;