ctdbd_conn: use the right error code from ctdbd_connect for debug and return
[Samba.git] / source3 / lib / ctdbd_conn.c
blobfae086a2be6b6cc78a5fad1cd57a83e83462218a
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 void (*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;
54 bool (*release_ip_handler)(const char *ip_addr, void *private_data);
55 void *release_ip_priv;
58 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
60 conn->reqid += 1;
61 if (conn->reqid == 0) {
62 conn->reqid += 1;
64 return conn->reqid;
67 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
68 uint32_t vnn, uint32_t opcode,
69 uint64_t srvid, uint32_t flags, 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 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid,
104 void (*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 NTSTATUS status;
112 int cstatus;
113 size_t num_callbacks;
114 struct ctdbd_srvid_cb *tmp;
116 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
117 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
118 tdb_null, NULL, NULL, &cstatus);
119 if (!NT_STATUS_IS_OK(status)) {
120 return status;
123 num_callbacks = talloc_array_length(conn->callbacks);
125 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
126 num_callbacks + 1);
127 if (tmp == NULL) {
128 return NT_STATUS_NO_MEMORY;
130 conn->callbacks = tmp;
132 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
133 .srvid = srvid, .cb = cb, .private_data = private_data
136 return NT_STATUS_OK;
139 static void ctdbd_msg_call_back(struct ctdbd_connection *conn,
140 struct ctdb_req_message *msg)
142 size_t msg_len;
143 size_t i, num_callbacks;
145 msg_len = msg->hdr.length;
146 if (msg_len < offsetof(struct ctdb_req_message, data)) {
147 DEBUG(10, ("%s: len %u too small\n", __func__,
148 (unsigned)msg_len));
149 return;
151 msg_len -= offsetof(struct ctdb_req_message, data);
153 if (msg_len < msg->datalen) {
154 DEBUG(10, ("%s: msg_len=%u < msg->datalen=%u\n", __func__,
155 (unsigned)msg_len, (unsigned)msg->datalen));
156 return;
159 num_callbacks = talloc_array_length(conn->callbacks);
161 for (i=0; i<num_callbacks; i++) {
162 struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
164 if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
165 cb->cb(msg->hdr.srcnode, msg->hdr.destnode,
166 msg->srvid, msg->data, msg->datalen,
167 cb->private_data);
173 * get our vnn from the cluster
175 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
177 int32_t cstatus=-1;
178 NTSTATUS status;
179 status = ctdbd_control(conn,
180 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
181 tdb_null, NULL, NULL, &cstatus);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
184 return status;
186 *vnn = (uint32_t)cstatus;
187 return status;
191 * Are we active (i.e. not banned or stopped?)
193 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
195 int32_t cstatus=-1;
196 NTSTATUS status;
197 TDB_DATA outdata;
198 struct ctdb_node_map *m;
199 uint32_t failure_flags;
200 bool ret = false;
201 int i;
203 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
204 CTDB_CONTROL_GET_NODEMAP, 0, 0,
205 tdb_null, talloc_tos(), &outdata, &cstatus);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
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 *)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 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
230 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
232 if ((m->nodes[i].flags & failure_flags) != 0) {
233 DEBUG(2, ("Node has status %x, not active\n",
234 (int)m->nodes[i].flags));
235 goto fail;
238 ret = true;
239 fail:
240 TALLOC_FREE(outdata.dptr);
241 return ret;
244 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
246 return conn->our_vnn;
249 const char *lp_ctdbd_socket(void)
251 const char *ret;
253 ret = lp__ctdbd_socket();
254 if (ret != NULL && strlen(ret) > 0) {
255 return ret;
258 return CTDB_SOCKET;
262 * Get us a ctdb connection
265 static int ctdbd_connect(int *pfd)
267 const char *sockname = lp_ctdbd_socket();
268 struct sockaddr_un addr = { 0, };
269 int fd;
270 socklen_t salen;
271 size_t namelen;
273 fd = socket(AF_UNIX, SOCK_STREAM, 0);
274 if (fd == -1) {
275 int err = errno;
276 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
277 return err;
280 addr.sun_family = AF_UNIX;
282 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
283 if (namelen >= sizeof(addr.sun_path)) {
284 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
285 sockname));
286 close(fd);
287 return ENAMETOOLONG;
290 salen = sizeof(struct sockaddr_un);
292 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
293 int err = errno;
294 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
295 strerror(err)));
296 close(fd);
297 return err;
300 *pfd = fd;
301 return 0;
304 static NTSTATUS ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
305 struct ctdb_req_header **result)
307 int timeout = lp_ctdb_timeout();
308 struct ctdb_req_header *req;
309 int ret, revents;
310 uint32_t msglen;
311 ssize_t nread;
313 if (timeout == 0) {
314 timeout = -1;
317 if (timeout != -1) {
318 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
319 if (ret == -1) {
320 return map_nt_error_from_unix(errno);
322 if (ret == 0) {
323 return NT_STATUS_IO_TIMEOUT;
325 if (ret != 1) {
326 return NT_STATUS_UNEXPECTED_IO_ERROR;
330 nread = read_data(fd, &msglen, sizeof(msglen));
331 if (nread == -1) {
332 return map_nt_error_from_unix(errno);
334 if (nread == 0) {
335 return NT_STATUS_UNEXPECTED_IO_ERROR;
338 if (msglen < sizeof(struct ctdb_req_header)) {
339 return NT_STATUS_UNEXPECTED_IO_ERROR;
342 req = talloc_size(mem_ctx, msglen);
343 if (req == NULL) {
344 return NT_STATUS_NO_MEMORY;
346 talloc_set_name_const(req, "struct ctdb_req_header");
348 req->length = msglen;
350 nread = read_data(fd, ((char *)req) + sizeof(msglen),
351 msglen - sizeof(msglen));
352 if (nread == -1) {
353 return map_nt_error_from_unix(errno);
355 if (nread == 0) {
356 return NT_STATUS_UNEXPECTED_IO_ERROR;
359 *result = req;
360 return NT_STATUS_OK;
364 * Read a full ctdbd request. If we have a messaging context, defer incoming
365 * messages that might come in between.
368 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
369 TALLOC_CTX *mem_ctx,
370 struct ctdb_req_header **result)
372 struct ctdb_req_header *hdr;
373 NTSTATUS status;
375 next_pkt:
377 status = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
378 if (!NT_STATUS_IS_OK(status)) {
379 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
380 cluster_fatal("ctdbd died\n");
383 DEBUG(11, ("Received ctdb packet\n"));
384 ctdb_packet_dump(hdr);
386 if (hdr->operation == CTDB_REQ_MESSAGE) {
387 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
389 if (conn->msg_ctx == NULL) {
390 DEBUG(1, ("Got a message without having a msg ctx, "
391 "dropping msg %llu\n",
392 (long long unsigned)msg->srvid));
393 goto next_pkt;
396 if ((conn->release_ip_handler != NULL)
397 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
398 bool ret;
400 /* must be dispatched immediately */
401 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
402 ret = conn->release_ip_handler((const char *)msg->data,
403 conn->release_ip_priv);
404 TALLOC_FREE(hdr);
406 if (ret) {
408 * We need to release the ip,
409 * so return an error to the upper layers.
411 * We make sure we don't trigger this again.
413 conn->release_ip_handler = NULL;
414 conn->release_ip_priv = NULL;
415 return NT_STATUS_ADDRESS_CLOSED;
417 goto next_pkt;
420 ctdbd_msg_call_back(conn, msg);
421 TALLOC_FREE(hdr);
422 goto next_pkt;
425 if ((reqid != 0) && (hdr->reqid != reqid)) {
426 /* we got the wrong reply */
427 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
428 "been %u\n", hdr->reqid, reqid));
429 TALLOC_FREE(hdr);
430 goto next_pkt;
433 *result = talloc_move(mem_ctx, &hdr);
435 return NT_STATUS_OK;
438 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
440 close(c->fd);
441 return 0;
444 * Get us a ctdbd connection
447 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
448 struct ctdbd_connection **pconn)
450 struct ctdbd_connection *conn;
451 int ret;
452 NTSTATUS status;
454 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
455 DEBUG(0, ("talloc failed\n"));
456 return NT_STATUS_NO_MEMORY;
459 ret = ctdbd_connect(&conn->fd);
460 if (ret != 0) {
461 status = map_nt_error_from_unix(ret);
462 DEBUG(1, ("ctdbd_connect failed: %s\n", strerror(ret)));
463 goto fail;
465 talloc_set_destructor(conn, ctdbd_connection_destructor);
467 status = get_cluster_vnn(conn, &conn->our_vnn);
469 if (!NT_STATUS_IS_OK(status)) {
470 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
471 goto fail;
474 if (!ctdbd_working(conn, conn->our_vnn)) {
475 DEBUG(2, ("Node is not working, can not connect\n"));
476 status = NT_STATUS_INTERNAL_DB_ERROR;
477 goto fail;
480 generate_random_buffer((unsigned char *)&conn->rand_srvid,
481 sizeof(conn->rand_srvid));
483 status = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
485 if (!NT_STATUS_IS_OK(status)) {
486 DEBUG(5, ("Could not register random srvid: %s\n",
487 nt_errstr(status)));
488 goto fail;
491 *pconn = conn;
492 return NT_STATUS_OK;
494 fail:
495 TALLOC_FREE(conn);
496 return status;
500 * Get us a ctdbd connection and register us as a process
503 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
504 struct ctdbd_connection **pconn)
506 struct ctdbd_connection *conn;
507 NTSTATUS status;
509 status = ctdbd_init_connection(mem_ctx, &conn);
511 if (!NT_STATUS_IS_OK(status)) {
512 return status;
515 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA, NULL, NULL);
516 if (!NT_STATUS_IS_OK(status)) {
517 goto fail;
520 *pconn = conn;
521 return NT_STATUS_OK;
523 fail:
524 TALLOC_FREE(conn);
525 return status;
528 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
530 return conn->msg_ctx;
533 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
535 return conn->fd;
539 * Packet handler to receive and handle a ctdb message
541 static NTSTATUS ctdb_handle_message(struct ctdbd_connection *conn,
542 struct ctdb_req_header *hdr)
544 struct ctdb_req_message *msg;
546 if (hdr->operation != CTDB_REQ_MESSAGE) {
547 DEBUG(0, ("Received async msg of type %u, discarding\n",
548 hdr->operation));
549 return NT_STATUS_INVALID_PARAMETER;
552 msg = (struct ctdb_req_message *)hdr;
554 if ((conn->release_ip_handler != NULL)
555 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
556 bool ret;
558 /* must be dispatched immediately */
559 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
560 ret = conn->release_ip_handler((const char *)msg->data,
561 conn->release_ip_priv);
562 if (ret) {
564 * We need to release the ip.
566 * We make sure we don't trigger this again.
568 conn->release_ip_handler = NULL;
569 conn->release_ip_priv = NULL;
571 return NT_STATUS_OK;
574 ctdbd_msg_call_back(conn, msg);
576 return NT_STATUS_OK;
580 * The ctdbd socket is readable asynchronuously
583 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
584 struct tevent_fd *event,
585 uint16_t flags,
586 void *private_data)
588 struct ctdbd_connection *conn = talloc_get_type_abort(
589 private_data, struct ctdbd_connection);
590 struct ctdb_req_header *hdr = NULL;
591 NTSTATUS status;
593 status = ctdb_read_packet(conn->fd, talloc_tos(), &hdr);
594 if (!NT_STATUS_IS_OK(status)) {
595 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
596 cluster_fatal("ctdbd died\n");
599 status = ctdb_handle_message(conn, hdr);
601 TALLOC_FREE(hdr);
603 if (!NT_STATUS_IS_OK(status)) {
604 DEBUG(10, ("could not handle incoming message: %s\n",
605 nt_errstr(status)));
610 * Prepare a ctdbd connection to receive messages
613 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
614 struct messaging_context *msg_ctx)
616 SMB_ASSERT(conn->msg_ctx == NULL);
617 SMB_ASSERT(conn->fde == NULL);
619 if (!(conn->fde = tevent_add_fd(messaging_tevent_context(msg_ctx),
620 conn,
621 conn->fd,
622 TEVENT_FD_READ,
623 ctdbd_socket_handler,
624 conn))) {
625 DEBUG(0, ("event_add_fd failed\n"));
626 return NT_STATUS_NO_MEMORY;
629 conn->msg_ctx = msg_ctx;
631 return NT_STATUS_OK;
634 NTSTATUS ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
635 uint32_t dst_vnn, uint64_t dst_srvid,
636 const struct iovec *iov, int iovlen)
638 struct ctdb_req_message r;
639 struct iovec iov2[iovlen+1];
640 size_t buflen = iov_buflen(iov, iovlen);
641 ssize_t nwritten;
643 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
644 r.hdr.ctdb_magic = CTDB_MAGIC;
645 r.hdr.ctdb_version = CTDB_PROTOCOL;
646 r.hdr.generation = 1;
647 r.hdr.operation = CTDB_REQ_MESSAGE;
648 r.hdr.destnode = dst_vnn;
649 r.hdr.srcnode = conn->our_vnn;
650 r.hdr.reqid = 0;
651 r.srvid = dst_srvid;
652 r.datalen = buflen;
654 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
655 ctdb_packet_dump(&r.hdr);
657 iov2[0].iov_base = &r;
658 iov2[0].iov_len = offsetof(struct ctdb_req_message, data);
659 memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
661 nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
662 if (nwritten == -1) {
663 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
664 cluster_fatal("cluster dispatch daemon msg write error\n");
667 return NT_STATUS_OK;
671 * send/recv a generic ctdb control message
673 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
674 uint32_t vnn, uint32_t opcode,
675 uint64_t srvid, uint32_t flags,
676 TDB_DATA data,
677 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
678 int *cstatus)
680 struct ctdb_req_control req;
681 struct ctdb_req_header *hdr;
682 struct ctdb_reply_control *reply = NULL;
683 struct ctdbd_connection *new_conn = NULL;
684 struct iovec iov[2];
685 ssize_t nwritten;
686 NTSTATUS status;
688 if (conn == NULL) {
689 status = ctdbd_init_connection(NULL, &new_conn);
691 if (!NT_STATUS_IS_OK(status)) {
692 DEBUG(10, ("Could not init temp connection: %s\n",
693 nt_errstr(status)));
694 goto fail;
697 conn = new_conn;
700 ZERO_STRUCT(req);
701 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
702 req.hdr.ctdb_magic = CTDB_MAGIC;
703 req.hdr.ctdb_version = CTDB_PROTOCOL;
704 req.hdr.operation = CTDB_REQ_CONTROL;
705 req.hdr.reqid = ctdbd_next_reqid(conn);
706 req.hdr.destnode = vnn;
707 req.opcode = opcode;
708 req.srvid = srvid;
709 req.datalen = data.dsize;
710 req.flags = flags;
712 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
713 ctdb_packet_dump(&req.hdr);
715 iov[0].iov_base = &req;
716 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
717 iov[1].iov_base = data.dptr;
718 iov[1].iov_len = data.dsize;
720 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
721 if (nwritten == -1) {
722 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
723 cluster_fatal("cluster dispatch daemon msg write error\n");
726 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
727 TALLOC_FREE(new_conn);
728 if (cstatus) {
729 *cstatus = 0;
731 return NT_STATUS_OK;
734 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
736 if (!NT_STATUS_IS_OK(status)) {
737 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
738 goto fail;
741 if (hdr->operation != CTDB_REPLY_CONTROL) {
742 DEBUG(0, ("received invalid reply\n"));
743 goto fail;
745 reply = (struct ctdb_reply_control *)hdr;
747 if (outdata) {
748 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
749 mem_ctx, reply->data, reply->datalen))) {
750 TALLOC_FREE(reply);
751 return NT_STATUS_NO_MEMORY;
753 outdata->dsize = reply->datalen;
755 if (cstatus) {
756 (*cstatus) = reply->status;
759 status = NT_STATUS_OK;
761 fail:
762 TALLOC_FREE(new_conn);
763 TALLOC_FREE(reply);
764 return status;
768 * see if a remote process exists
770 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
772 struct server_id id;
773 bool result;
775 id.pid = pid;
776 id.vnn = vnn;
778 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
779 DEBUG(10, ("ctdb_processes_exist failed\n"));
780 return false;
782 return result;
785 bool ctdb_processes_exist(struct ctdbd_connection *conn,
786 const struct server_id *pids, int num_pids,
787 bool *results)
789 TALLOC_CTX *frame = talloc_stackframe();
790 int i, num_received;
791 NTSTATUS status;
792 uint32_t *reqids;
793 bool result = false;
795 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
796 if (reqids == NULL) {
797 goto fail;
800 for (i=0; i<num_pids; i++) {
801 struct ctdb_req_control req;
802 pid_t pid;
803 struct iovec iov[2];
804 ssize_t nwritten;
806 results[i] = false;
807 reqids[i] = ctdbd_next_reqid(conn);
809 ZERO_STRUCT(req);
812 * pids[i].pid is uint64_t, scale down to pid_t which
813 * is the wire protocol towards ctdb.
815 pid = pids[i].pid;
817 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
818 (int)pids[i].vnn, (int)pid,
819 (int)reqids[i]));
821 req.hdr.length = offsetof(struct ctdb_req_control, data);
822 req.hdr.length += sizeof(pid);
823 req.hdr.ctdb_magic = CTDB_MAGIC;
824 req.hdr.ctdb_version = CTDB_PROTOCOL;
825 req.hdr.operation = CTDB_REQ_CONTROL;
826 req.hdr.reqid = reqids[i];
827 req.hdr.destnode = pids[i].vnn;
828 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
829 req.srvid = 0;
830 req.datalen = sizeof(pid);
831 req.flags = 0;
833 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
834 ctdb_packet_dump(&req.hdr);
836 iov[0].iov_base = &req;
837 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
838 iov[1].iov_base = &pid;
839 iov[1].iov_len = sizeof(pid);
841 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
842 if (nwritten == -1) {
843 status = map_nt_error_from_unix(errno);
844 DEBUG(10, ("write_data_iov failed: %s\n",
845 strerror(errno)));
846 goto fail;
850 num_received = 0;
852 while (num_received < num_pids) {
853 struct ctdb_req_header *hdr;
854 struct ctdb_reply_control *reply;
855 uint32_t reqid;
857 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
858 if (!NT_STATUS_IS_OK(status)) {
859 DEBUG(10, ("ctdb_read_req failed: %s\n",
860 nt_errstr(status)));
861 goto fail;
864 if (hdr->operation != CTDB_REPLY_CONTROL) {
865 DEBUG(10, ("Received invalid reply\n"));
866 goto fail;
868 reply = (struct ctdb_reply_control *)hdr;
870 reqid = reply->hdr.reqid;
872 DEBUG(10, ("Received reqid %d\n", (int)reqid));
874 for (i=0; i<num_pids; i++) {
875 if (reqid == reqids[i]) {
876 break;
879 if (i == num_pids) {
880 DEBUG(10, ("Received unknown record number %u\n",
881 (unsigned)reqid));
882 goto fail;
884 results[i] = ((reply->status) == 0);
885 TALLOC_FREE(reply);
886 num_received += 1;
889 result = true;
890 fail:
891 TALLOC_FREE(frame);
892 return result;
895 struct ctdb_vnn_list {
896 uint32_t vnn;
897 uint32_t reqid;
898 unsigned num_srvids;
899 unsigned num_filled;
900 uint64_t *srvids;
901 unsigned *pid_indexes;
905 * Get a list of all vnns mentioned in a list of
906 * server_ids. vnn_indexes tells where in the vnns array we have to
907 * place the pids.
909 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
910 const struct server_id *pids, unsigned num_pids,
911 struct ctdb_vnn_list **pvnns,
912 unsigned *pnum_vnns)
914 struct ctdb_vnn_list *vnns = NULL;
915 unsigned *vnn_indexes = NULL;
916 unsigned i, num_vnns = 0;
918 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
919 if (vnn_indexes == NULL) {
920 DEBUG(1, ("talloc_array failed\n"));
921 goto fail;
924 for (i=0; i<num_pids; i++) {
925 unsigned j;
926 uint32_t vnn = pids[i].vnn;
928 for (j=0; j<num_vnns; j++) {
929 if (vnn == vnns[j].vnn) {
930 break;
933 vnn_indexes[i] = j;
935 if (j < num_vnns) {
937 * Already in the array
939 vnns[j].num_srvids += 1;
940 continue;
942 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
943 num_vnns+1);
944 if (vnns == NULL) {
945 DEBUG(1, ("talloc_realloc failed\n"));
946 goto fail;
948 vnns[num_vnns].vnn = vnn;
949 vnns[num_vnns].num_srvids = 1;
950 vnns[num_vnns].num_filled = 0;
951 num_vnns += 1;
953 for (i=0; i<num_vnns; i++) {
954 struct ctdb_vnn_list *vnn = &vnns[i];
956 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
957 if (vnn->srvids == NULL) {
958 DEBUG(1, ("talloc_array failed\n"));
959 goto fail;
961 vnn->pid_indexes = talloc_array(vnns, unsigned,
962 vnn->num_srvids);
963 if (vnn->pid_indexes == NULL) {
964 DEBUG(1, ("talloc_array failed\n"));
965 goto fail;
968 for (i=0; i<num_pids; i++) {
969 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
970 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
971 vnn->pid_indexes[vnn->num_filled] = i;
972 vnn->num_filled += 1;
975 TALLOC_FREE(vnn_indexes);
976 *pvnns = vnns;
977 *pnum_vnns = num_vnns;
978 return true;
979 fail:
980 TALLOC_FREE(vnns);
981 TALLOC_FREE(vnn_indexes);
982 return false;
985 bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
987 return true;
990 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
991 const struct server_id *pids, unsigned num_pids,
992 bool *results)
994 unsigned i, num_received;
995 NTSTATUS status;
996 struct ctdb_vnn_list *vnns = NULL;
997 unsigned num_vnns;
999 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1000 &vnns, &num_vnns)) {
1001 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1002 goto fail;
1005 for (i=0; i<num_vnns; i++) {
1006 struct ctdb_vnn_list *vnn = &vnns[i];
1007 struct ctdb_req_control req;
1008 struct iovec iov[2];
1009 ssize_t nwritten;
1011 vnn->reqid = ctdbd_next_reqid(conn);
1013 ZERO_STRUCT(req);
1015 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1016 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1018 req.hdr.length = offsetof(struct ctdb_req_control, data);
1019 req.hdr.ctdb_magic = CTDB_MAGIC;
1020 req.hdr.ctdb_version = CTDB_PROTOCOL;
1021 req.hdr.operation = CTDB_REQ_CONTROL;
1022 req.hdr.reqid = vnn->reqid;
1023 req.hdr.destnode = vnn->vnn;
1024 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1025 req.srvid = 0;
1026 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1027 req.hdr.length += req.datalen;
1028 req.flags = 0;
1030 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1031 ctdb_packet_dump(&req.hdr);
1033 iov[0].iov_base = &req;
1034 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
1035 iov[1].iov_base = vnn->srvids;
1036 iov[1].iov_len = req.datalen;
1038 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1039 if (nwritten == -1) {
1040 status = map_nt_error_from_unix(errno);
1041 DEBUG(10, ("write_data_iov failed: %s\n",
1042 strerror(errno)));
1043 goto fail;
1047 num_received = 0;
1049 while (num_received < num_vnns) {
1050 struct ctdb_req_header *hdr;
1051 struct ctdb_reply_control *reply;
1052 struct ctdb_vnn_list *vnn;
1053 uint32_t reqid;
1054 uint8_t *reply_data;
1056 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
1057 if (!NT_STATUS_IS_OK(status)) {
1058 DEBUG(1, ("ctdb_read_req failed: %s\n",
1059 nt_errstr(status)));
1060 goto fail;
1063 if (hdr->operation != CTDB_REPLY_CONTROL) {
1064 DEBUG(1, ("Received invalid reply %u\n",
1065 (unsigned)hdr->operation));
1066 goto fail;
1068 reply = (struct ctdb_reply_control *)hdr;
1070 reqid = reply->hdr.reqid;
1072 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1074 for (i=0; i<num_vnns; i++) {
1075 if (reqid == vnns[i].reqid) {
1076 break;
1079 if (i == num_vnns) {
1080 DEBUG(1, ("Received unknown reqid number %u\n",
1081 (unsigned)reqid));
1082 goto fail;
1085 DEBUG(10, ("Found index %u\n", i));
1087 vnn = &vnns[i];
1089 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1090 (unsigned)vnn->vnn, vnn->num_srvids,
1091 (unsigned)reply->datalen));
1093 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1095 * Got a real reply
1097 reply_data = reply->data;
1098 } else {
1100 * Got an error reply
1102 DEBUG(5, ("Received short reply len %d, status %u, "
1103 "errorlen %u\n",
1104 (unsigned)reply->datalen,
1105 (unsigned)reply->status,
1106 (unsigned)reply->errorlen));
1107 dump_data(5, reply->data, reply->errorlen);
1110 * This will trigger everything set to false
1112 reply_data = NULL;
1115 for (i=0; i<vnn->num_srvids; i++) {
1116 int idx = vnn->pid_indexes[i];
1118 if (pids[i].unique_id ==
1119 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1120 results[idx] = true;
1121 continue;
1123 results[idx] =
1124 (reply_data != NULL) &&
1125 ((reply_data[i/8] & (1<<(i%8))) != 0);
1128 TALLOC_FREE(reply);
1129 num_received += 1;
1132 TALLOC_FREE(vnns);
1133 return true;
1134 fail:
1135 cluster_fatal("serverids_exist failed");
1136 return false;
1140 * Get a db path
1142 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1143 TALLOC_CTX *mem_ctx, uint32_t db_id)
1145 NTSTATUS status;
1146 TDB_DATA data;
1147 TDB_DATA rdata = {0};
1148 int32_t cstatus = 0;
1150 data.dptr = (uint8_t*)&db_id;
1151 data.dsize = sizeof(db_id);
1153 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1154 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1155 mem_ctx, &rdata, &cstatus);
1156 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1157 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1158 return NULL;
1161 return (char *)rdata.dptr;
1165 * attach to a ctdb database
1167 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1168 const char *name, uint32_t *db_id, int tdb_flags)
1170 NTSTATUS status;
1171 TDB_DATA data;
1172 int32_t cstatus;
1173 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1175 data = string_term_tdb_data(name);
1177 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1178 persistent
1179 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1180 : CTDB_CONTROL_DB_ATTACH,
1181 tdb_flags, 0, data, NULL, &data, &cstatus);
1182 if (!NT_STATUS_IS_OK(status)) {
1183 DEBUG(0, (__location__ " ctdb_control for db_attach "
1184 "failed: %s\n", nt_errstr(status)));
1185 return status;
1188 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1189 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1190 return NT_STATUS_INTERNAL_ERROR;
1193 *db_id = *(uint32_t *)data.dptr;
1194 talloc_free(data.dptr);
1196 if (!(tdb_flags & TDB_SEQNUM)) {
1197 return NT_STATUS_OK;
1200 data.dptr = (uint8_t *)db_id;
1201 data.dsize = sizeof(*db_id);
1203 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1204 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1205 NULL, NULL, &cstatus);
1206 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1207 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1208 "failed\n"));
1209 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1210 status;
1213 return NT_STATUS_OK;
1217 * force the migration of a record to this node
1219 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1220 TDB_DATA key)
1222 struct ctdb_req_call req;
1223 struct ctdb_req_header *hdr;
1224 struct iovec iov[2];
1225 ssize_t nwritten;
1226 NTSTATUS status;
1228 ZERO_STRUCT(req);
1230 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1231 req.hdr.ctdb_magic = CTDB_MAGIC;
1232 req.hdr.ctdb_version = CTDB_PROTOCOL;
1233 req.hdr.operation = CTDB_REQ_CALL;
1234 req.hdr.reqid = ctdbd_next_reqid(conn);
1235 req.flags = CTDB_IMMEDIATE_MIGRATION;
1236 req.callid = CTDB_NULL_FUNC;
1237 req.db_id = db_id;
1238 req.keylen = key.dsize;
1240 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1241 ctdb_packet_dump(&req.hdr);
1243 iov[0].iov_base = &req;
1244 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1245 iov[1].iov_base = key.dptr;
1246 iov[1].iov_len = key.dsize;
1248 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1249 if (nwritten == -1) {
1250 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1251 cluster_fatal("cluster dispatch daemon msg write error\n");
1254 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1256 if (!NT_STATUS_IS_OK(status)) {
1257 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1258 goto fail;
1261 if (hdr->operation != CTDB_REPLY_CALL) {
1262 DEBUG(0, ("received invalid reply\n"));
1263 status = NT_STATUS_INTERNAL_ERROR;
1264 goto fail;
1267 status = NT_STATUS_OK;
1268 fail:
1270 TALLOC_FREE(hdr);
1271 return status;
1275 * Fetch a record and parse it
1277 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1278 TDB_DATA key, bool local_copy,
1279 void (*parser)(TDB_DATA key, TDB_DATA data,
1280 void *private_data),
1281 void *private_data)
1283 struct ctdb_req_call req;
1284 struct ctdb_req_header *hdr = NULL;
1285 struct ctdb_reply_call *reply;
1286 struct iovec iov[2];
1287 ssize_t nwritten;
1288 NTSTATUS status;
1289 uint32_t flags;
1291 flags = local_copy ? CTDB_WANT_READONLY : 0;
1293 ZERO_STRUCT(req);
1295 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1296 req.hdr.ctdb_magic = CTDB_MAGIC;
1297 req.hdr.ctdb_version = CTDB_PROTOCOL;
1298 req.hdr.operation = CTDB_REQ_CALL;
1299 req.hdr.reqid = ctdbd_next_reqid(conn);
1300 req.flags = flags;
1301 req.callid = CTDB_FETCH_FUNC;
1302 req.db_id = db_id;
1303 req.keylen = key.dsize;
1305 iov[0].iov_base = &req;
1306 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1307 iov[1].iov_base = key.dptr;
1308 iov[1].iov_len = key.dsize;
1310 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1311 if (nwritten == -1) {
1312 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1313 cluster_fatal("cluster dispatch daemon msg write error\n");
1316 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1318 if (!NT_STATUS_IS_OK(status)) {
1319 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1320 goto fail;
1323 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
1324 DEBUG(0, ("received invalid reply\n"));
1325 status = NT_STATUS_INTERNAL_ERROR;
1326 goto fail;
1328 reply = (struct ctdb_reply_call *)hdr;
1330 if (reply->datalen == 0) {
1332 * Treat an empty record as non-existing
1334 status = NT_STATUS_NOT_FOUND;
1335 goto fail;
1338 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1339 private_data);
1341 status = NT_STATUS_OK;
1342 fail:
1343 TALLOC_FREE(hdr);
1344 return status;
1348 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1349 connection to ctdbd to avoid the hairy recursive and async problems with
1350 everything in-line.
1353 NTSTATUS ctdbd_traverse(uint32_t db_id,
1354 void (*fn)(TDB_DATA key, TDB_DATA data,
1355 void *private_data),
1356 void *private_data)
1358 struct ctdbd_connection *conn;
1359 NTSTATUS status;
1361 TDB_DATA key, data;
1362 struct ctdb_traverse_start t;
1363 int cstatus;
1365 become_root();
1366 status = ctdbd_init_connection(NULL, &conn);
1367 unbecome_root();
1368 if (!NT_STATUS_IS_OK(status)) {
1369 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1370 nt_errstr(status)));
1371 return status;
1374 t.db_id = db_id;
1375 t.srvid = conn->rand_srvid;
1376 t.reqid = ctdbd_next_reqid(conn);
1378 data.dptr = (uint8_t *)&t;
1379 data.dsize = sizeof(t);
1381 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1382 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1383 data, NULL, NULL, &cstatus);
1385 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1387 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1388 cstatus));
1390 if (NT_STATUS_IS_OK(status)) {
1392 * We need a mapping here
1394 status = NT_STATUS_UNSUCCESSFUL;
1396 TALLOC_FREE(conn);
1397 return status;
1400 while (True) {
1401 struct ctdb_req_header *hdr = NULL;
1402 struct ctdb_req_message *m;
1403 struct ctdb_rec_data *d;
1405 status = ctdb_read_packet(conn->fd, conn, &hdr);
1406 if (!NT_STATUS_IS_OK(status)) {
1407 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1408 nt_errstr(status)));
1409 cluster_fatal("ctdbd died\n");
1412 if (hdr->operation != CTDB_REQ_MESSAGE) {
1413 DEBUG(0, ("Got operation %u, expected a message\n",
1414 (unsigned)hdr->operation));
1415 TALLOC_FREE(conn);
1416 return NT_STATUS_UNEXPECTED_IO_ERROR;
1419 m = (struct ctdb_req_message *)hdr;
1420 d = (struct ctdb_rec_data *)&m->data[0];
1421 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1422 DEBUG(0, ("Got invalid traverse data of length %d\n",
1423 (int)m->datalen));
1424 TALLOC_FREE(conn);
1425 return NT_STATUS_UNEXPECTED_IO_ERROR;
1428 key.dsize = d->keylen;
1429 key.dptr = &d->data[0];
1430 data.dsize = d->datalen;
1431 data.dptr = &d->data[d->keylen];
1433 if (key.dsize == 0 && data.dsize == 0) {
1434 /* end of traverse */
1435 TALLOC_FREE(conn);
1436 return NT_STATUS_OK;
1439 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1440 DEBUG(0, ("Got invalid ltdb header length %d\n",
1441 (int)data.dsize));
1442 TALLOC_FREE(conn);
1443 return NT_STATUS_UNEXPECTED_IO_ERROR;
1445 data.dsize -= sizeof(struct ctdb_ltdb_header);
1446 data.dptr += sizeof(struct ctdb_ltdb_header);
1448 if (fn != NULL) {
1449 fn(key, data, private_data);
1452 return NT_STATUS_OK;
1456 This is used to canonicalize a ctdb_sock_addr structure.
1458 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1459 struct sockaddr_storage *out)
1461 memcpy(out, in, sizeof (*out));
1463 #ifdef HAVE_IPV6
1464 if (in->ss_family == AF_INET6) {
1465 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1466 const struct sockaddr_in6 *in6 =
1467 (const struct sockaddr_in6 *)in;
1468 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1469 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1470 memset(out, 0, sizeof(*out));
1471 #ifdef HAVE_SOCK_SIN_LEN
1472 out4->sin_len = sizeof(*out);
1473 #endif
1474 out4->sin_family = AF_INET;
1475 out4->sin_port = in6->sin6_port;
1476 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1479 #endif
1483 * Register us as a server for a particular tcp connection
1486 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1487 const struct sockaddr_storage *_server,
1488 const struct sockaddr_storage *_client,
1489 bool (*release_ip_handler)(const char *ip_addr,
1490 void *private_data),
1491 void *private_data)
1493 struct ctdb_control_tcp_addr p;
1494 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1495 NTSTATUS status;
1496 struct sockaddr_storage client;
1497 struct sockaddr_storage server;
1500 * Only one connection so far
1502 SMB_ASSERT(conn->release_ip_handler == NULL);
1504 smbd_ctdb_canonicalize_ip(_client, &client);
1505 smbd_ctdb_canonicalize_ip(_server, &server);
1507 switch (client.ss_family) {
1508 case AF_INET:
1509 memcpy(&p.dest.ip, &server, sizeof(p.dest.ip));
1510 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1511 break;
1512 case AF_INET6:
1513 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1514 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1515 break;
1516 default:
1517 return NT_STATUS_INTERNAL_ERROR;
1520 conn->release_ip_handler = release_ip_handler;
1521 conn->release_ip_priv = private_data;
1524 * We want to be told about IP releases
1527 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP, NULL, NULL);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 return status;
1533 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1534 * can send an extra ack to trigger a reset for our client, so it
1535 * immediately reconnects
1537 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1538 CTDB_CONTROL_TCP_CLIENT, 0,
1539 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1543 call a control on the local node
1545 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1546 uint64_t srvid, uint32_t flags, TDB_DATA data,
1547 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1548 int *cstatus)
1550 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1553 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1555 struct ctdb_client_notify_register reg_data;
1556 size_t struct_len;
1557 NTSTATUS status;
1558 int cstatus;
1560 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1561 reg_data.len = 1;
1562 reg_data.notify_data[0] = 0;
1564 struct_len = offsetof(struct ctdb_client_notify_register,
1565 notify_data) + reg_data.len;
1567 status = ctdbd_control_local(
1568 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1569 make_tdb_data((uint8_t *)&reg_data, struct_len),
1570 NULL, NULL, &cstatus);
1571 if (!NT_STATUS_IS_OK(status)) {
1572 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1573 nt_errstr(status)));
1575 return status;
1578 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1580 struct ctdb_client_notify_deregister dereg_data;
1581 NTSTATUS status;
1582 int cstatus;
1584 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1586 status = ctdbd_control_local(
1587 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1588 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1589 NULL, NULL, &cstatus);
1590 if (!NT_STATUS_IS_OK(status)) {
1591 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1592 nt_errstr(status)));
1594 return status;
1597 NTSTATUS ctdbd_probe(void)
1600 * Do a very early check if ctdbd is around to avoid an abort and core
1601 * later
1603 struct ctdbd_connection *conn = NULL;
1604 NTSTATUS status;
1606 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1609 * We only care if we can connect.
1611 TALLOC_FREE(conn);
1613 return status;