s4-auth: Use kerberos util functions in srv_keytab
[Samba.git] / source3 / lib / ctdbd_conn.c
blob1acce1284422acfb27df354543d27c34ab59a532
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(int *pfd)
270 const char *sockname = lp_ctdbd_socket();
271 struct sockaddr_un addr = { 0, };
272 int fd;
273 socklen_t salen;
274 size_t namelen;
276 fd = socket(AF_UNIX, SOCK_STREAM, 0);
277 if (fd == -1) {
278 int err = errno;
279 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
280 return err;
283 addr.sun_family = AF_UNIX;
285 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
286 if (namelen >= sizeof(addr.sun_path)) {
287 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
288 sockname));
289 close(fd);
290 return ENAMETOOLONG;
293 salen = sizeof(struct sockaddr_un);
295 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
296 int err = errno;
297 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
298 strerror(err)));
299 close(fd);
300 return err;
303 *pfd = fd;
304 return 0;
307 static int ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
308 struct ctdb_req_header **result)
310 int timeout = lp_ctdb_timeout();
311 struct ctdb_req_header *req;
312 int ret, revents;
313 uint32_t msglen;
314 ssize_t nread;
316 if (timeout == 0) {
317 timeout = -1;
320 if (timeout != -1) {
321 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
322 if (ret == -1) {
323 return errno;
325 if (ret == 0) {
326 return ETIMEDOUT;
328 if (ret != 1) {
329 return EIO;
333 nread = read_data(fd, &msglen, sizeof(msglen));
334 if (nread == -1) {
335 return errno;
337 if (nread == 0) {
338 return EIO;
341 if (msglen < sizeof(struct ctdb_req_header)) {
342 return EIO;
345 req = talloc_size(mem_ctx, msglen);
346 if (req == NULL) {
347 return ENOMEM;
349 talloc_set_name_const(req, "struct ctdb_req_header");
351 req->length = msglen;
353 nread = read_data(fd, ((char *)req) + sizeof(msglen),
354 msglen - sizeof(msglen));
355 if (nread == -1) {
356 return errno;
358 if (nread == 0) {
359 return EIO;
362 *result = req;
363 return 0;
367 * Read a full ctdbd request. If we have a messaging context, defer incoming
368 * messages that might come in between.
371 static int ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
372 TALLOC_CTX *mem_ctx, struct ctdb_req_header **result)
374 struct ctdb_req_header *hdr;
375 int ret;
377 next_pkt:
379 ret = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
380 if (ret != 0) {
381 DEBUG(0, ("ctdb_read_packet failed: %s\n", strerror(ret)));
382 cluster_fatal("ctdbd died\n");
385 DEBUG(11, ("Received ctdb packet\n"));
386 ctdb_packet_dump(hdr);
388 if (hdr->operation == CTDB_REQ_MESSAGE) {
389 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
391 if (conn->msg_ctx == NULL) {
392 DEBUG(1, ("Got a message without having a msg ctx, "
393 "dropping msg %llu\n",
394 (long long unsigned)msg->srvid));
395 TALLOC_FREE(hdr);
396 goto next_pkt;
399 ret = ctdbd_msg_call_back(conn, msg);
400 if (ret != 0) {
401 TALLOC_FREE(hdr);
402 return ret;
405 TALLOC_FREE(hdr);
406 goto next_pkt;
409 if ((reqid != 0) && (hdr->reqid != reqid)) {
410 /* we got the wrong reply */
411 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
412 "been %u\n", hdr->reqid, reqid));
413 TALLOC_FREE(hdr);
414 goto next_pkt;
417 *result = talloc_move(mem_ctx, &hdr);
419 return 0;
422 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
424 close(c->fd);
425 return 0;
428 * Get us a ctdbd connection
431 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
432 struct ctdbd_connection **pconn)
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(&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;
859 struct ctdb_vnn_list {
860 uint32_t vnn;
861 uint32_t reqid;
862 unsigned num_srvids;
863 unsigned num_filled;
864 uint64_t *srvids;
865 unsigned *pid_indexes;
869 * Get a list of all vnns mentioned in a list of
870 * server_ids. vnn_indexes tells where in the vnns array we have to
871 * place the pids.
873 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
874 const struct server_id *pids, unsigned num_pids,
875 struct ctdb_vnn_list **pvnns,
876 unsigned *pnum_vnns)
878 struct ctdb_vnn_list *vnns = NULL;
879 unsigned *vnn_indexes = NULL;
880 unsigned i, num_vnns = 0;
882 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
883 if (vnn_indexes == NULL) {
884 DEBUG(1, ("talloc_array failed\n"));
885 goto fail;
888 for (i=0; i<num_pids; i++) {
889 unsigned j;
890 uint32_t vnn = pids[i].vnn;
892 for (j=0; j<num_vnns; j++) {
893 if (vnn == vnns[j].vnn) {
894 break;
897 vnn_indexes[i] = j;
899 if (j < num_vnns) {
901 * Already in the array
903 vnns[j].num_srvids += 1;
904 continue;
906 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
907 num_vnns+1);
908 if (vnns == NULL) {
909 DEBUG(1, ("talloc_realloc failed\n"));
910 goto fail;
912 vnns[num_vnns].vnn = vnn;
913 vnns[num_vnns].num_srvids = 1;
914 vnns[num_vnns].num_filled = 0;
915 num_vnns += 1;
917 for (i=0; i<num_vnns; i++) {
918 struct ctdb_vnn_list *vnn = &vnns[i];
920 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
921 if (vnn->srvids == NULL) {
922 DEBUG(1, ("talloc_array failed\n"));
923 goto fail;
925 vnn->pid_indexes = talloc_array(vnns, unsigned,
926 vnn->num_srvids);
927 if (vnn->pid_indexes == NULL) {
928 DEBUG(1, ("talloc_array failed\n"));
929 goto fail;
932 for (i=0; i<num_pids; i++) {
933 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
934 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
935 vnn->pid_indexes[vnn->num_filled] = i;
936 vnn->num_filled += 1;
939 TALLOC_FREE(vnn_indexes);
940 *pvnns = vnns;
941 *pnum_vnns = num_vnns;
942 return true;
943 fail:
944 TALLOC_FREE(vnns);
945 TALLOC_FREE(vnn_indexes);
946 return false;
949 bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
951 return true;
954 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
955 const struct server_id *pids, unsigned num_pids,
956 bool *results)
958 unsigned i, num_received;
959 struct ctdb_vnn_list *vnns = NULL;
960 unsigned num_vnns;
962 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
963 &vnns, &num_vnns)) {
964 DEBUG(1, ("ctdb_collect_vnns failed\n"));
965 goto fail;
968 for (i=0; i<num_vnns; i++) {
969 struct ctdb_vnn_list *vnn = &vnns[i];
970 struct ctdb_req_control req;
971 struct iovec iov[2];
972 ssize_t nwritten;
974 vnn->reqid = ctdbd_next_reqid(conn);
976 ZERO_STRUCT(req);
978 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
979 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
981 req.hdr.length = offsetof(struct ctdb_req_control, data);
982 req.hdr.ctdb_magic = CTDB_MAGIC;
983 req.hdr.ctdb_version = CTDB_PROTOCOL;
984 req.hdr.operation = CTDB_REQ_CONTROL;
985 req.hdr.reqid = vnn->reqid;
986 req.hdr.destnode = vnn->vnn;
987 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
988 req.srvid = 0;
989 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
990 req.hdr.length += req.datalen;
991 req.flags = 0;
993 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
994 ctdb_packet_dump(&req.hdr);
996 iov[0].iov_base = &req;
997 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
998 iov[1].iov_base = vnn->srvids;
999 iov[1].iov_len = req.datalen;
1001 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1002 if (nwritten == -1) {
1003 DEBUG(10, ("write_data_iov failed: %s\n",
1004 strerror(errno)));
1005 goto fail;
1009 num_received = 0;
1011 while (num_received < num_vnns) {
1012 struct ctdb_req_header *hdr;
1013 struct ctdb_reply_control *reply;
1014 struct ctdb_vnn_list *vnn;
1015 uint32_t reqid;
1016 uint8_t *reply_data;
1017 int ret;
1019 ret = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
1020 if (ret != 0) {
1021 DEBUG(10, ("ctdb_read_req failed: %s\n",
1022 strerror(ret)));
1023 goto fail;
1026 if (hdr->operation != CTDB_REPLY_CONTROL) {
1027 DEBUG(1, ("Received invalid reply %u\n",
1028 (unsigned)hdr->operation));
1029 goto fail;
1031 reply = (struct ctdb_reply_control *)hdr;
1033 reqid = reply->hdr.reqid;
1035 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1037 for (i=0; i<num_vnns; i++) {
1038 if (reqid == vnns[i].reqid) {
1039 break;
1042 if (i == num_vnns) {
1043 DEBUG(1, ("Received unknown reqid number %u\n",
1044 (unsigned)reqid));
1045 goto fail;
1048 DEBUG(10, ("Found index %u\n", i));
1050 vnn = &vnns[i];
1052 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1053 (unsigned)vnn->vnn, vnn->num_srvids,
1054 (unsigned)reply->datalen));
1056 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1058 * Got a real reply
1060 reply_data = reply->data;
1061 } else {
1063 * Got an error reply
1065 DEBUG(5, ("Received short reply len %d, status %u, "
1066 "errorlen %u\n",
1067 (unsigned)reply->datalen,
1068 (unsigned)reply->status,
1069 (unsigned)reply->errorlen));
1070 dump_data(5, reply->data, reply->errorlen);
1073 * This will trigger everything set to false
1075 reply_data = NULL;
1078 for (i=0; i<vnn->num_srvids; i++) {
1079 int idx = vnn->pid_indexes[i];
1081 if (pids[i].unique_id ==
1082 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1083 results[idx] = true;
1084 continue;
1086 results[idx] =
1087 (reply_data != NULL) &&
1088 ((reply_data[i/8] & (1<<(i%8))) != 0);
1091 TALLOC_FREE(reply);
1092 num_received += 1;
1095 TALLOC_FREE(vnns);
1096 return true;
1097 fail:
1098 cluster_fatal("serverids_exist failed");
1099 return false;
1103 * Get a db path
1105 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1106 TALLOC_CTX *mem_ctx, uint32_t db_id)
1108 NTSTATUS status;
1109 TDB_DATA data;
1110 TDB_DATA rdata = {0};
1111 int32_t cstatus = 0;
1113 data.dptr = (uint8_t*)&db_id;
1114 data.dsize = sizeof(db_id);
1116 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1117 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1118 mem_ctx, &rdata, &cstatus);
1119 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1120 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1121 return NULL;
1124 return (char *)rdata.dptr;
1128 * attach to a ctdb database
1130 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1131 const char *name, uint32_t *db_id, int tdb_flags)
1133 NTSTATUS status;
1134 TDB_DATA data;
1135 int32_t cstatus;
1136 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1138 data = string_term_tdb_data(name);
1140 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1141 persistent
1142 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1143 : CTDB_CONTROL_DB_ATTACH,
1144 tdb_flags, 0, data, NULL, &data, &cstatus);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 DEBUG(0, (__location__ " ctdb_control for db_attach "
1147 "failed: %s\n", nt_errstr(status)));
1148 return status;
1151 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1152 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1153 return NT_STATUS_INTERNAL_ERROR;
1156 *db_id = *(uint32_t *)data.dptr;
1157 talloc_free(data.dptr);
1159 if (!(tdb_flags & TDB_SEQNUM)) {
1160 return NT_STATUS_OK;
1163 data.dptr = (uint8_t *)db_id;
1164 data.dsize = sizeof(*db_id);
1166 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1167 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1168 NULL, NULL, &cstatus);
1169 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1170 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1171 "failed\n"));
1172 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1173 status;
1176 return NT_STATUS_OK;
1180 * force the migration of a record to this node
1182 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1183 TDB_DATA key)
1185 struct ctdb_req_call req;
1186 struct ctdb_req_header *hdr;
1187 struct iovec iov[2];
1188 ssize_t nwritten;
1189 NTSTATUS status;
1190 int ret;
1192 ZERO_STRUCT(req);
1194 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1195 req.hdr.ctdb_magic = CTDB_MAGIC;
1196 req.hdr.ctdb_version = CTDB_PROTOCOL;
1197 req.hdr.operation = CTDB_REQ_CALL;
1198 req.hdr.reqid = ctdbd_next_reqid(conn);
1199 req.flags = CTDB_IMMEDIATE_MIGRATION;
1200 req.callid = CTDB_NULL_FUNC;
1201 req.db_id = db_id;
1202 req.keylen = key.dsize;
1204 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1205 ctdb_packet_dump(&req.hdr);
1207 iov[0].iov_base = &req;
1208 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1209 iov[1].iov_base = key.dptr;
1210 iov[1].iov_len = key.dsize;
1212 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1213 if (nwritten == -1) {
1214 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1215 cluster_fatal("cluster dispatch daemon msg write error\n");
1218 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1219 if (ret != 0) {
1220 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
1221 status = map_nt_error_from_unix(ret);
1222 goto fail;
1225 if (hdr->operation != CTDB_REPLY_CALL) {
1226 DEBUG(0, ("received invalid reply\n"));
1227 status = NT_STATUS_INTERNAL_ERROR;
1228 goto fail;
1231 status = NT_STATUS_OK;
1232 fail:
1234 TALLOC_FREE(hdr);
1235 return status;
1239 * Fetch a record and parse it
1241 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1242 TDB_DATA key, bool local_copy,
1243 void (*parser)(TDB_DATA key, TDB_DATA data,
1244 void *private_data),
1245 void *private_data)
1247 struct ctdb_req_call req;
1248 struct ctdb_req_header *hdr = NULL;
1249 struct ctdb_reply_call *reply;
1250 struct iovec iov[2];
1251 ssize_t nwritten;
1252 NTSTATUS status;
1253 uint32_t flags;
1254 int ret;
1256 flags = local_copy ? CTDB_WANT_READONLY : 0;
1258 ZERO_STRUCT(req);
1260 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1261 req.hdr.ctdb_magic = CTDB_MAGIC;
1262 req.hdr.ctdb_version = CTDB_PROTOCOL;
1263 req.hdr.operation = CTDB_REQ_CALL;
1264 req.hdr.reqid = ctdbd_next_reqid(conn);
1265 req.flags = flags;
1266 req.callid = CTDB_FETCH_FUNC;
1267 req.db_id = db_id;
1268 req.keylen = key.dsize;
1270 iov[0].iov_base = &req;
1271 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1272 iov[1].iov_base = key.dptr;
1273 iov[1].iov_len = key.dsize;
1275 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1276 if (nwritten == -1) {
1277 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1278 cluster_fatal("cluster dispatch daemon msg write error\n");
1281 ret = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1282 if (ret != 0) {
1283 DEBUG(10, ("ctdb_read_req failed: %s\n", strerror(ret)));
1284 status = map_nt_error_from_unix(ret);
1285 goto fail;
1288 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
1289 DEBUG(0, ("received invalid reply\n"));
1290 status = NT_STATUS_INTERNAL_ERROR;
1291 goto fail;
1293 reply = (struct ctdb_reply_call *)hdr;
1295 if (reply->datalen == 0) {
1297 * Treat an empty record as non-existing
1299 status = NT_STATUS_NOT_FOUND;
1300 goto fail;
1303 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1304 private_data);
1306 status = NT_STATUS_OK;
1307 fail:
1308 TALLOC_FREE(hdr);
1309 return status;
1313 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1314 connection to ctdbd to avoid the hairy recursive and async problems with
1315 everything in-line.
1318 NTSTATUS ctdbd_traverse(uint32_t db_id,
1319 void (*fn)(TDB_DATA key, TDB_DATA data,
1320 void *private_data),
1321 void *private_data)
1323 struct ctdbd_connection *conn;
1324 NTSTATUS status;
1326 TDB_DATA key, data;
1327 struct ctdb_traverse_start t;
1328 int cstatus;
1330 become_root();
1331 status = ctdbd_init_connection(NULL, &conn);
1332 unbecome_root();
1333 if (!NT_STATUS_IS_OK(status)) {
1334 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1335 nt_errstr(status)));
1336 return status;
1339 t.db_id = db_id;
1340 t.srvid = conn->rand_srvid;
1341 t.reqid = ctdbd_next_reqid(conn);
1343 data.dptr = (uint8_t *)&t;
1344 data.dsize = sizeof(t);
1346 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1347 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1348 data, NULL, NULL, &cstatus);
1350 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1352 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1353 cstatus));
1355 if (NT_STATUS_IS_OK(status)) {
1357 * We need a mapping here
1359 status = NT_STATUS_UNSUCCESSFUL;
1361 TALLOC_FREE(conn);
1362 return status;
1365 while (True) {
1366 struct ctdb_req_header *hdr = NULL;
1367 struct ctdb_req_message *m;
1368 struct ctdb_rec_data *d;
1369 int ret;
1371 ret = ctdb_read_packet(conn->fd, conn, &hdr);
1372 if (ret != 0) {
1373 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1374 strerror(ret)));
1375 cluster_fatal("ctdbd died\n");
1378 if (hdr->operation != CTDB_REQ_MESSAGE) {
1379 DEBUG(0, ("Got operation %u, expected a message\n",
1380 (unsigned)hdr->operation));
1381 TALLOC_FREE(conn);
1382 return NT_STATUS_UNEXPECTED_IO_ERROR;
1385 m = (struct ctdb_req_message *)hdr;
1386 d = (struct ctdb_rec_data *)&m->data[0];
1387 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1388 DEBUG(0, ("Got invalid traverse data of length %d\n",
1389 (int)m->datalen));
1390 TALLOC_FREE(conn);
1391 return NT_STATUS_UNEXPECTED_IO_ERROR;
1394 key.dsize = d->keylen;
1395 key.dptr = &d->data[0];
1396 data.dsize = d->datalen;
1397 data.dptr = &d->data[d->keylen];
1399 if (key.dsize == 0 && data.dsize == 0) {
1400 /* end of traverse */
1401 TALLOC_FREE(conn);
1402 return NT_STATUS_OK;
1405 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1406 DEBUG(0, ("Got invalid ltdb header length %d\n",
1407 (int)data.dsize));
1408 TALLOC_FREE(conn);
1409 return NT_STATUS_UNEXPECTED_IO_ERROR;
1411 data.dsize -= sizeof(struct ctdb_ltdb_header);
1412 data.dptr += sizeof(struct ctdb_ltdb_header);
1414 if (fn != NULL) {
1415 fn(key, data, private_data);
1418 return NT_STATUS_OK;
1422 This is used to canonicalize a ctdb_sock_addr structure.
1424 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1425 struct sockaddr_storage *out)
1427 memcpy(out, in, sizeof (*out));
1429 #ifdef HAVE_IPV6
1430 if (in->ss_family == AF_INET6) {
1431 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1432 const struct sockaddr_in6 *in6 =
1433 (const struct sockaddr_in6 *)in;
1434 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1435 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1436 memset(out, 0, sizeof(*out));
1437 #ifdef HAVE_SOCK_SIN_LEN
1438 out4->sin_len = sizeof(*out);
1439 #endif
1440 out4->sin_family = AF_INET;
1441 out4->sin_port = in6->sin6_port;
1442 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1445 #endif
1449 * Register us as a server for a particular tcp connection
1452 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1453 const struct sockaddr_storage *_server,
1454 const struct sockaddr_storage *_client,
1455 int (*cb)(uint32_t src_vnn, uint32_t dst_vnn,
1456 uint64_t dst_srvid,
1457 const uint8_t *msg, size_t msglen,
1458 void *private_data),
1459 void *private_data)
1461 struct ctdb_control_tcp_addr p;
1462 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1463 NTSTATUS status;
1464 struct sockaddr_storage client;
1465 struct sockaddr_storage server;
1468 * Only one connection so far
1471 smbd_ctdb_canonicalize_ip(_client, &client);
1472 smbd_ctdb_canonicalize_ip(_server, &server);
1474 switch (client.ss_family) {
1475 case AF_INET:
1476 memcpy(&p.dest.ip, &server, sizeof(p.dest.ip));
1477 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1478 break;
1479 case AF_INET6:
1480 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1481 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1482 break;
1483 default:
1484 return NT_STATUS_INTERNAL_ERROR;
1488 * We want to be told about IP releases
1491 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP,
1492 cb, private_data);
1493 if (!NT_STATUS_IS_OK(status)) {
1494 return status;
1498 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1499 * can send an extra ack to trigger a reset for our client, so it
1500 * immediately reconnects
1502 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1503 CTDB_CONTROL_TCP_CLIENT, 0,
1504 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1508 call a control on the local node
1510 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1511 uint64_t srvid, uint32_t flags, TDB_DATA data,
1512 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1513 int *cstatus)
1515 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1518 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1520 struct ctdb_client_notify_register reg_data;
1521 size_t struct_len;
1522 NTSTATUS status;
1523 int cstatus;
1525 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1526 reg_data.len = 1;
1527 reg_data.notify_data[0] = 0;
1529 struct_len = offsetof(struct ctdb_client_notify_register,
1530 notify_data) + reg_data.len;
1532 status = ctdbd_control_local(
1533 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1534 make_tdb_data((uint8_t *)&reg_data, struct_len),
1535 NULL, NULL, &cstatus);
1536 if (!NT_STATUS_IS_OK(status)) {
1537 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1538 nt_errstr(status)));
1540 return status;
1543 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1545 struct ctdb_client_notify_deregister dereg_data;
1546 NTSTATUS status;
1547 int cstatus;
1549 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1551 status = ctdbd_control_local(
1552 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1553 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1554 NULL, NULL, &cstatus);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1557 nt_errstr(status)));
1559 return status;
1562 NTSTATUS ctdbd_probe(void)
1565 * Do a very early check if ctdbd is around to avoid an abort and core
1566 * later
1568 struct ctdbd_connection *conn = NULL;
1569 NTSTATUS status;
1571 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1574 * We only care if we can connect.
1576 TALLOC_FREE(conn);
1578 return status;