ctdbd_conn: Fix CID 1301580 Explicit null dereferenced
[Samba.git] / source3 / lib / ctdbd_conn.c
blob3aa5ced5fb013ab9a6e967faf84cbab962a18827
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)(struct ctdb_req_message *msg, void *private_data);
39 void *private_data;
42 struct ctdbd_connection {
43 struct messaging_context *msg_ctx;
44 uint32_t reqid;
45 uint32_t our_vnn;
46 uint64_t rand_srvid;
47 struct ctdbd_srvid_cb *callbacks;
48 int fd;
49 struct tevent_fd *fde;
51 bool (*release_ip_handler)(const char *ip_addr, void *private_data);
52 void *release_ip_priv;
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 void (*cb)(struct ctdb_req_message *msg,
102 void *private_data),
103 void *private_data)
106 NTSTATUS status;
107 int cstatus;
108 size_t num_callbacks;
109 struct ctdbd_srvid_cb *tmp;
111 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
112 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
113 tdb_null, NULL, NULL, &cstatus);
114 if (!NT_STATUS_IS_OK(status)) {
115 return status;
118 num_callbacks = talloc_array_length(conn->callbacks);
120 tmp = talloc_realloc(conn, conn->callbacks, struct ctdbd_srvid_cb,
121 num_callbacks + 1);
122 if (tmp == NULL) {
123 return NT_STATUS_NO_MEMORY;
125 conn->callbacks = tmp;
127 conn->callbacks[num_callbacks] = (struct ctdbd_srvid_cb) {
128 .srvid = srvid, .cb = cb, .private_data = private_data
131 return NT_STATUS_OK;
134 static void ctdbd_msg_call_back(struct ctdbd_connection *conn,
135 struct ctdb_req_message *msg)
137 size_t i, num_callbacks;
139 num_callbacks = talloc_array_length(conn->callbacks);
141 for (i=0; i<num_callbacks; i++) {
142 struct ctdbd_srvid_cb *cb = &conn->callbacks[i];
144 if ((cb->srvid == msg->srvid) && (cb->cb != NULL)) {
145 cb->cb(msg, cb->private_data);
151 * get our vnn from the cluster
153 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
155 int32_t cstatus=-1;
156 NTSTATUS status;
157 status = ctdbd_control(conn,
158 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
159 tdb_null, NULL, NULL, &cstatus);
160 if (!NT_STATUS_IS_OK(status)) {
161 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
162 return status;
164 *vnn = (uint32_t)cstatus;
165 return status;
169 * Are we active (i.e. not banned or stopped?)
171 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
173 int32_t cstatus=-1;
174 NTSTATUS status;
175 TDB_DATA outdata;
176 struct ctdb_node_map *m;
177 uint32_t failure_flags;
178 bool ret = false;
179 int i;
181 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
182 CTDB_CONTROL_GET_NODEMAP, 0, 0,
183 tdb_null, talloc_tos(), &outdata, &cstatus);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
186 return false;
188 if ((cstatus != 0) || (outdata.dptr == NULL)) {
189 DEBUG(2, ("Received invalid ctdb data\n"));
190 return false;
193 m = (struct ctdb_node_map *)outdata.dptr;
195 for (i=0; i<m->num; i++) {
196 if (vnn == m->nodes[i].pnn) {
197 break;
201 if (i == m->num) {
202 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
203 (int)vnn));
204 goto fail;
207 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
208 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
210 if ((m->nodes[i].flags & failure_flags) != 0) {
211 DEBUG(2, ("Node has status %x, not active\n",
212 (int)m->nodes[i].flags));
213 goto fail;
216 ret = true;
217 fail:
218 TALLOC_FREE(outdata.dptr);
219 return ret;
222 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
224 return conn->our_vnn;
227 const char *lp_ctdbd_socket(void)
229 const char *ret;
231 ret = lp__ctdbd_socket();
232 if (ret != NULL && strlen(ret) > 0) {
233 return ret;
236 return CTDB_SOCKET;
240 * Get us a ctdb connection
243 static int ctdbd_connect(int *pfd)
245 const char *sockname = lp_ctdbd_socket();
246 struct sockaddr_un addr = { 0, };
247 int fd;
248 socklen_t salen;
249 size_t namelen;
251 fd = socket(AF_UNIX, SOCK_STREAM, 0);
252 if (fd == -1) {
253 int err = errno;
254 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
255 return err;
258 addr.sun_family = AF_UNIX;
260 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
261 if (namelen >= sizeof(addr.sun_path)) {
262 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
263 sockname));
264 close(fd);
265 return ENAMETOOLONG;
268 salen = sizeof(struct sockaddr_un);
270 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
271 int err = errno;
272 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
273 strerror(err)));
274 close(fd);
275 return err;
278 *pfd = fd;
279 return 0;
282 static NTSTATUS ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
283 struct ctdb_req_header **result)
285 int timeout = lp_ctdb_timeout();
286 struct ctdb_req_header *req;
287 int ret, revents;
288 uint32_t msglen;
289 ssize_t nread;
291 if (timeout == 0) {
292 timeout = -1;
295 if (timeout != -1) {
296 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
297 if (ret == -1) {
298 return map_nt_error_from_unix(errno);
300 if (ret == 0) {
301 return NT_STATUS_IO_TIMEOUT;
303 if (ret != 1) {
304 return NT_STATUS_UNEXPECTED_IO_ERROR;
308 nread = read_data(fd, &msglen, sizeof(msglen));
309 if (nread == -1) {
310 return map_nt_error_from_unix(errno);
312 if (nread == 0) {
313 return NT_STATUS_UNEXPECTED_IO_ERROR;
316 if (msglen < sizeof(struct ctdb_req_header)) {
317 return NT_STATUS_UNEXPECTED_IO_ERROR;
320 req = talloc_size(mem_ctx, msglen);
321 if (req == NULL) {
322 return NT_STATUS_NO_MEMORY;
324 talloc_set_name_const(req, "struct ctdb_req_header");
326 req->length = msglen;
328 nread = read_data(fd, ((char *)req) + sizeof(msglen),
329 msglen - sizeof(msglen));
330 if (nread == -1) {
331 return map_nt_error_from_unix(errno);
333 if (nread == 0) {
334 return NT_STATUS_UNEXPECTED_IO_ERROR;
337 *result = req;
338 return NT_STATUS_OK;
342 * Read a full ctdbd request. If we have a messaging context, defer incoming
343 * messages that might come in between.
346 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
347 TALLOC_CTX *mem_ctx,
348 struct ctdb_req_header **result)
350 struct ctdb_req_header *hdr;
351 NTSTATUS status;
353 next_pkt:
355 status = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
356 if (!NT_STATUS_IS_OK(status)) {
357 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
358 cluster_fatal("ctdbd died\n");
361 DEBUG(11, ("Received ctdb packet\n"));
362 ctdb_packet_dump(hdr);
364 if (hdr->operation == CTDB_REQ_MESSAGE) {
365 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
367 if (conn->msg_ctx == NULL) {
368 DEBUG(1, ("Got a message without having a msg ctx, "
369 "dropping msg %llu\n",
370 (long long unsigned)msg->srvid));
371 goto next_pkt;
374 if ((conn->release_ip_handler != NULL)
375 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
376 bool ret;
378 /* must be dispatched immediately */
379 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
380 ret = conn->release_ip_handler((const char *)msg->data,
381 conn->release_ip_priv);
382 TALLOC_FREE(hdr);
384 if (ret) {
386 * We need to release the ip,
387 * so return an error to the upper layers.
389 * We make sure we don't trigger this again.
391 conn->release_ip_handler = NULL;
392 conn->release_ip_priv = NULL;
393 return NT_STATUS_ADDRESS_CLOSED;
395 goto next_pkt;
398 ctdbd_msg_call_back(conn, msg);
399 TALLOC_FREE(hdr);
400 goto next_pkt;
403 if ((reqid != 0) && (hdr->reqid != reqid)) {
404 /* we got the wrong reply */
405 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
406 "been %u\n", hdr->reqid, reqid));
407 TALLOC_FREE(hdr);
408 goto next_pkt;
411 *result = talloc_move(mem_ctx, &hdr);
413 return NT_STATUS_OK;
416 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
418 close(c->fd);
419 return 0;
422 * Get us a ctdbd connection
425 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
426 struct ctdbd_connection **pconn)
428 struct ctdbd_connection *conn;
429 int ret;
430 NTSTATUS status;
432 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
433 DEBUG(0, ("talloc failed\n"));
434 return NT_STATUS_NO_MEMORY;
437 ret = ctdbd_connect(&conn->fd);
438 if (ret != 0) {
439 status = map_nt_error_from_unix(errno);
440 DEBUG(10, ("ctdbd_connect failed: %s\n", strerror(errno)));
441 goto fail;
443 talloc_set_destructor(conn, ctdbd_connection_destructor);
445 status = get_cluster_vnn(conn, &conn->our_vnn);
447 if (!NT_STATUS_IS_OK(status)) {
448 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
449 goto fail;
452 if (!ctdbd_working(conn, conn->our_vnn)) {
453 DEBUG(2, ("Node is not working, can not connect\n"));
454 status = NT_STATUS_INTERNAL_DB_ERROR;
455 goto fail;
458 generate_random_buffer((unsigned char *)&conn->rand_srvid,
459 sizeof(conn->rand_srvid));
461 status = register_with_ctdbd(conn, conn->rand_srvid, NULL, NULL);
463 if (!NT_STATUS_IS_OK(status)) {
464 DEBUG(5, ("Could not register random srvid: %s\n",
465 nt_errstr(status)));
466 goto fail;
469 *pconn = conn;
470 return NT_STATUS_OK;
472 fail:
473 TALLOC_FREE(conn);
474 return status;
478 * Get us a ctdbd connection and register us as a process
481 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
482 struct ctdbd_connection **pconn)
484 struct ctdbd_connection *conn;
485 NTSTATUS status;
487 status = ctdbd_init_connection(mem_ctx, &conn);
489 if (!NT_STATUS_IS_OK(status)) {
490 return status;
493 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA, NULL, NULL);
494 if (!NT_STATUS_IS_OK(status)) {
495 goto fail;
498 *pconn = conn;
499 return NT_STATUS_OK;
501 fail:
502 TALLOC_FREE(conn);
503 return status;
506 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
508 return conn->msg_ctx;
511 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
513 return conn->fd;
517 * Packet handler to receive and handle a ctdb message
519 static NTSTATUS ctdb_handle_message(struct ctdbd_connection *conn,
520 struct ctdb_req_header *hdr)
522 struct ctdb_req_message *msg;
524 if (hdr->operation != CTDB_REQ_MESSAGE) {
525 DEBUG(0, ("Received async msg of type %u, discarding\n",
526 hdr->operation));
527 return NT_STATUS_INVALID_PARAMETER;
530 msg = (struct ctdb_req_message *)hdr;
532 if ((conn->release_ip_handler != NULL)
533 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
534 bool ret;
536 /* must be dispatched immediately */
537 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
538 ret = conn->release_ip_handler((const char *)msg->data,
539 conn->release_ip_priv);
540 if (ret) {
542 * We need to release the ip.
544 * We make sure we don't trigger this again.
546 conn->release_ip_handler = NULL;
547 conn->release_ip_priv = NULL;
549 return NT_STATUS_OK;
552 ctdbd_msg_call_back(conn, msg);
554 return NT_STATUS_OK;
558 * The ctdbd socket is readable asynchronuously
561 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
562 struct tevent_fd *event,
563 uint16_t flags,
564 void *private_data)
566 struct ctdbd_connection *conn = talloc_get_type_abort(
567 private_data, struct ctdbd_connection);
568 struct ctdb_req_header *hdr = NULL;
569 NTSTATUS status;
571 status = ctdb_read_packet(conn->fd, talloc_tos(), &hdr);
572 if (!NT_STATUS_IS_OK(status)) {
573 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
574 cluster_fatal("ctdbd died\n");
577 status = ctdb_handle_message(conn, hdr);
578 if (!NT_STATUS_IS_OK(status)) {
579 DEBUG(10, ("could not handle incoming message: %s\n",
580 nt_errstr(status)));
585 * Prepare a ctdbd connection to receive messages
588 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
589 struct messaging_context *msg_ctx)
591 SMB_ASSERT(conn->msg_ctx == NULL);
592 SMB_ASSERT(conn->fde == NULL);
594 if (!(conn->fde = tevent_add_fd(messaging_tevent_context(msg_ctx),
595 conn,
596 conn->fd,
597 TEVENT_FD_READ,
598 ctdbd_socket_handler,
599 conn))) {
600 DEBUG(0, ("event_add_fd failed\n"));
601 return NT_STATUS_NO_MEMORY;
604 conn->msg_ctx = msg_ctx;
606 return NT_STATUS_OK;
609 NTSTATUS ctdbd_messaging_send_iov(struct ctdbd_connection *conn,
610 uint32_t dst_vnn, uint64_t dst_srvid,
611 const struct iovec *iov, int iovlen)
613 struct ctdb_req_message r;
614 struct iovec iov2[iovlen+1];
615 size_t buflen = iov_buflen(iov, iovlen);
616 ssize_t nwritten;
618 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
619 r.hdr.ctdb_magic = CTDB_MAGIC;
620 r.hdr.ctdb_version = CTDB_PROTOCOL;
621 r.hdr.generation = 1;
622 r.hdr.operation = CTDB_REQ_MESSAGE;
623 r.hdr.destnode = dst_vnn;
624 r.hdr.srcnode = conn->our_vnn;
625 r.hdr.reqid = 0;
626 r.srvid = dst_srvid;
627 r.datalen = buflen;
629 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
630 ctdb_packet_dump(&r.hdr);
632 iov2[0].iov_base = &r;
633 iov2[0].iov_len = offsetof(struct ctdb_req_message, data);
634 memcpy(&iov2[1], iov, iovlen * sizeof(struct iovec));
636 nwritten = write_data_iov(conn->fd, iov2, iovlen+1);
637 if (nwritten == -1) {
638 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
639 cluster_fatal("cluster dispatch daemon msg write error\n");
642 return NT_STATUS_OK;
646 * send/recv a generic ctdb control message
648 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
649 uint32_t vnn, uint32_t opcode,
650 uint64_t srvid, uint32_t flags,
651 TDB_DATA data,
652 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
653 int *cstatus)
655 struct ctdb_req_control req;
656 struct ctdb_req_header *hdr;
657 struct ctdb_reply_control *reply = NULL;
658 struct ctdbd_connection *new_conn = NULL;
659 struct iovec iov[2];
660 ssize_t nwritten;
661 NTSTATUS status;
663 if (conn == NULL) {
664 status = ctdbd_init_connection(NULL, &new_conn);
666 if (!NT_STATUS_IS_OK(status)) {
667 DEBUG(10, ("Could not init temp connection: %s\n",
668 nt_errstr(status)));
669 goto fail;
672 conn = new_conn;
675 ZERO_STRUCT(req);
676 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
677 req.hdr.ctdb_magic = CTDB_MAGIC;
678 req.hdr.ctdb_version = CTDB_PROTOCOL;
679 req.hdr.operation = CTDB_REQ_CONTROL;
680 req.hdr.reqid = ctdbd_next_reqid(conn);
681 req.hdr.destnode = vnn;
682 req.opcode = opcode;
683 req.srvid = srvid;
684 req.datalen = data.dsize;
685 req.flags = flags;
687 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
688 ctdb_packet_dump(&req.hdr);
690 iov[0].iov_base = &req;
691 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
692 iov[1].iov_base = data.dptr;
693 iov[1].iov_len = data.dsize;
695 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
696 if (nwritten == -1) {
697 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
698 cluster_fatal("cluster dispatch daemon msg write error\n");
701 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
702 TALLOC_FREE(new_conn);
703 if (cstatus) {
704 *cstatus = 0;
706 return NT_STATUS_OK;
709 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
711 if (!NT_STATUS_IS_OK(status)) {
712 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
713 goto fail;
716 if (hdr->operation != CTDB_REPLY_CONTROL) {
717 DEBUG(0, ("received invalid reply\n"));
718 goto fail;
720 reply = (struct ctdb_reply_control *)hdr;
722 if (outdata) {
723 if (!(outdata->dptr = (uint8_t *)talloc_memdup(
724 mem_ctx, reply->data, reply->datalen))) {
725 TALLOC_FREE(reply);
726 return NT_STATUS_NO_MEMORY;
728 outdata->dsize = reply->datalen;
730 if (cstatus) {
731 (*cstatus) = reply->status;
734 status = NT_STATUS_OK;
736 fail:
737 TALLOC_FREE(new_conn);
738 TALLOC_FREE(reply);
739 return status;
743 * see if a remote process exists
745 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
747 struct server_id id;
748 bool result;
750 id.pid = pid;
751 id.vnn = vnn;
753 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
754 DEBUG(10, ("ctdb_processes_exist failed\n"));
755 return false;
757 return result;
760 bool ctdb_processes_exist(struct ctdbd_connection *conn,
761 const struct server_id *pids, int num_pids,
762 bool *results)
764 TALLOC_CTX *frame = talloc_stackframe();
765 int i, num_received;
766 NTSTATUS status;
767 uint32_t *reqids;
768 bool result = false;
770 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
771 if (reqids == NULL) {
772 goto fail;
775 for (i=0; i<num_pids; i++) {
776 struct ctdb_req_control req;
777 pid_t pid;
778 struct iovec iov[2];
779 ssize_t nwritten;
781 results[i] = false;
782 reqids[i] = ctdbd_next_reqid(conn);
784 ZERO_STRUCT(req);
787 * pids[i].pid is uint64_t, scale down to pid_t which
788 * is the wire protocol towards ctdb.
790 pid = pids[i].pid;
792 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
793 (int)pids[i].vnn, (int)pid,
794 (int)reqids[i]));
796 req.hdr.length = offsetof(struct ctdb_req_control, data);
797 req.hdr.length += sizeof(pid);
798 req.hdr.ctdb_magic = CTDB_MAGIC;
799 req.hdr.ctdb_version = CTDB_PROTOCOL;
800 req.hdr.operation = CTDB_REQ_CONTROL;
801 req.hdr.reqid = reqids[i];
802 req.hdr.destnode = pids[i].vnn;
803 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
804 req.srvid = 0;
805 req.datalen = sizeof(pid);
806 req.flags = 0;
808 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
809 ctdb_packet_dump(&req.hdr);
811 iov[0].iov_base = &req;
812 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
813 iov[1].iov_base = &pid;
814 iov[1].iov_len = sizeof(pid);
816 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
817 if (nwritten == -1) {
818 status = map_nt_error_from_unix(errno);
819 DEBUG(10, ("write_data_iov failed: %s\n",
820 strerror(errno)));
821 goto fail;
825 num_received = 0;
827 while (num_received < num_pids) {
828 struct ctdb_req_header *hdr;
829 struct ctdb_reply_control *reply;
830 uint32_t reqid;
832 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
833 if (!NT_STATUS_IS_OK(status)) {
834 DEBUG(10, ("ctdb_read_req failed: %s\n",
835 nt_errstr(status)));
836 goto fail;
839 if (hdr->operation != CTDB_REPLY_CONTROL) {
840 DEBUG(10, ("Received invalid reply\n"));
841 goto fail;
843 reply = (struct ctdb_reply_control *)hdr;
845 reqid = reply->hdr.reqid;
847 DEBUG(10, ("Received reqid %d\n", (int)reqid));
849 for (i=0; i<num_pids; i++) {
850 if (reqid == reqids[i]) {
851 break;
854 if (i == num_pids) {
855 DEBUG(10, ("Received unknown record number %u\n",
856 (unsigned)reqid));
857 goto fail;
859 results[i] = ((reply->status) == 0);
860 TALLOC_FREE(reply);
861 num_received += 1;
864 result = true;
865 fail:
866 TALLOC_FREE(frame);
867 return result;
870 struct ctdb_vnn_list {
871 uint32_t vnn;
872 uint32_t reqid;
873 unsigned num_srvids;
874 unsigned num_filled;
875 uint64_t *srvids;
876 unsigned *pid_indexes;
880 * Get a list of all vnns mentioned in a list of
881 * server_ids. vnn_indexes tells where in the vnns array we have to
882 * place the pids.
884 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
885 const struct server_id *pids, unsigned num_pids,
886 struct ctdb_vnn_list **pvnns,
887 unsigned *pnum_vnns)
889 struct ctdb_vnn_list *vnns = NULL;
890 unsigned *vnn_indexes = NULL;
891 unsigned i, num_vnns = 0;
893 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
894 if (vnn_indexes == NULL) {
895 DEBUG(1, ("talloc_array failed\n"));
896 goto fail;
899 for (i=0; i<num_pids; i++) {
900 unsigned j;
901 uint32_t vnn = pids[i].vnn;
903 for (j=0; j<num_vnns; j++) {
904 if (vnn == vnns[j].vnn) {
905 break;
908 vnn_indexes[i] = j;
910 if (j < num_vnns) {
912 * Already in the array
914 vnns[j].num_srvids += 1;
915 continue;
917 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
918 num_vnns+1);
919 if (vnns == NULL) {
920 DEBUG(1, ("talloc_realloc failed\n"));
921 goto fail;
923 vnns[num_vnns].vnn = vnn;
924 vnns[num_vnns].num_srvids = 1;
925 vnns[num_vnns].num_filled = 0;
926 num_vnns += 1;
928 for (i=0; i<num_vnns; i++) {
929 struct ctdb_vnn_list *vnn = &vnns[i];
931 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
932 if (vnn->srvids == NULL) {
933 DEBUG(1, ("talloc_array failed\n"));
934 goto fail;
936 vnn->pid_indexes = talloc_array(vnns, unsigned,
937 vnn->num_srvids);
938 if (vnn->pid_indexes == NULL) {
939 DEBUG(1, ("talloc_array failed\n"));
940 goto fail;
943 for (i=0; i<num_pids; i++) {
944 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
945 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
946 vnn->pid_indexes[vnn->num_filled] = i;
947 vnn->num_filled += 1;
950 TALLOC_FREE(vnn_indexes);
951 *pvnns = vnns;
952 *pnum_vnns = num_vnns;
953 return true;
954 fail:
955 TALLOC_FREE(vnns);
956 TALLOC_FREE(vnn_indexes);
957 return false;
960 bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
962 return true;
965 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
966 const struct server_id *pids, unsigned num_pids,
967 bool *results)
969 unsigned i, num_received;
970 NTSTATUS status;
971 struct ctdb_vnn_list *vnns = NULL;
972 unsigned num_vnns;
974 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
975 &vnns, &num_vnns)) {
976 DEBUG(1, ("ctdb_collect_vnns failed\n"));
977 goto fail;
980 for (i=0; i<num_vnns; i++) {
981 struct ctdb_vnn_list *vnn = &vnns[i];
982 struct ctdb_req_control req;
983 struct iovec iov[2];
984 ssize_t nwritten;
986 vnn->reqid = ctdbd_next_reqid(conn);
988 ZERO_STRUCT(req);
990 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
991 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
993 req.hdr.length = offsetof(struct ctdb_req_control, data);
994 req.hdr.ctdb_magic = CTDB_MAGIC;
995 req.hdr.ctdb_version = CTDB_PROTOCOL;
996 req.hdr.operation = CTDB_REQ_CONTROL;
997 req.hdr.reqid = vnn->reqid;
998 req.hdr.destnode = vnn->vnn;
999 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1000 req.srvid = 0;
1001 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1002 req.hdr.length += req.datalen;
1003 req.flags = 0;
1005 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1006 ctdb_packet_dump(&req.hdr);
1008 iov[0].iov_base = &req;
1009 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
1010 iov[1].iov_base = vnn->srvids;
1011 iov[1].iov_len = req.datalen;
1013 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1014 if (nwritten == -1) {
1015 status = map_nt_error_from_unix(errno);
1016 DEBUG(10, ("write_data_iov failed: %s\n",
1017 strerror(errno)));
1018 goto fail;
1022 num_received = 0;
1024 while (num_received < num_vnns) {
1025 struct ctdb_req_header *hdr;
1026 struct ctdb_reply_control *reply;
1027 struct ctdb_vnn_list *vnn;
1028 uint32_t reqid;
1029 uint8_t *reply_data;
1031 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 DEBUG(1, ("ctdb_read_req failed: %s\n",
1034 nt_errstr(status)));
1035 goto fail;
1038 if (hdr->operation != CTDB_REPLY_CONTROL) {
1039 DEBUG(1, ("Received invalid reply %u\n",
1040 (unsigned)hdr->operation));
1041 goto fail;
1043 reply = (struct ctdb_reply_control *)hdr;
1045 reqid = reply->hdr.reqid;
1047 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1049 for (i=0; i<num_vnns; i++) {
1050 if (reqid == vnns[i].reqid) {
1051 break;
1054 if (i == num_vnns) {
1055 DEBUG(1, ("Received unknown reqid number %u\n",
1056 (unsigned)reqid));
1057 goto fail;
1060 DEBUG(10, ("Found index %u\n", i));
1062 vnn = &vnns[i];
1064 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1065 (unsigned)vnn->vnn, vnn->num_srvids,
1066 (unsigned)reply->datalen));
1068 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1070 * Got a real reply
1072 reply_data = reply->data;
1073 } else {
1075 * Got an error reply
1077 DEBUG(5, ("Received short reply len %d, status %u, "
1078 "errorlen %u\n",
1079 (unsigned)reply->datalen,
1080 (unsigned)reply->status,
1081 (unsigned)reply->errorlen));
1082 dump_data(5, reply->data, reply->errorlen);
1085 * This will trigger everything set to false
1087 reply_data = NULL;
1090 for (i=0; i<vnn->num_srvids; i++) {
1091 int idx = vnn->pid_indexes[i];
1093 if (pids[i].unique_id ==
1094 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1095 results[idx] = true;
1096 continue;
1098 results[idx] =
1099 (reply_data != NULL) &&
1100 ((reply_data[i/8] & (1<<(i%8))) != 0);
1103 TALLOC_FREE(reply);
1104 num_received += 1;
1107 TALLOC_FREE(vnns);
1108 return true;
1109 fail:
1110 cluster_fatal("serverids_exist failed");
1111 return false;
1115 * Get a db path
1117 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1118 TALLOC_CTX *mem_ctx, uint32_t db_id)
1120 NTSTATUS status;
1121 TDB_DATA data;
1122 TDB_DATA rdata = {0};
1123 int32_t cstatus = 0;
1125 data.dptr = (uint8_t*)&db_id;
1126 data.dsize = sizeof(db_id);
1128 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1129 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1130 mem_ctx, &rdata, &cstatus);
1131 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1132 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1133 return NULL;
1136 return (char *)rdata.dptr;
1140 * attach to a ctdb database
1142 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1143 const char *name, uint32_t *db_id, int tdb_flags)
1145 NTSTATUS status;
1146 TDB_DATA data;
1147 int32_t cstatus;
1148 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1150 data = string_term_tdb_data(name);
1152 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1153 persistent
1154 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1155 : CTDB_CONTROL_DB_ATTACH,
1156 tdb_flags, 0, data, NULL, &data, &cstatus);
1157 if (!NT_STATUS_IS_OK(status)) {
1158 DEBUG(0, (__location__ " ctdb_control for db_attach "
1159 "failed: %s\n", nt_errstr(status)));
1160 return status;
1163 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1164 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1165 return NT_STATUS_INTERNAL_ERROR;
1168 *db_id = *(uint32_t *)data.dptr;
1169 talloc_free(data.dptr);
1171 if (!(tdb_flags & TDB_SEQNUM)) {
1172 return NT_STATUS_OK;
1175 data.dptr = (uint8_t *)db_id;
1176 data.dsize = sizeof(*db_id);
1178 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1179 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1180 NULL, NULL, &cstatus);
1181 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1182 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1183 "failed\n"));
1184 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1185 status;
1188 return NT_STATUS_OK;
1192 * force the migration of a record to this node
1194 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1195 TDB_DATA key)
1197 struct ctdb_req_call req;
1198 struct ctdb_req_header *hdr;
1199 struct iovec iov[2];
1200 ssize_t nwritten;
1201 NTSTATUS status;
1203 ZERO_STRUCT(req);
1205 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1206 req.hdr.ctdb_magic = CTDB_MAGIC;
1207 req.hdr.ctdb_version = CTDB_PROTOCOL;
1208 req.hdr.operation = CTDB_REQ_CALL;
1209 req.hdr.reqid = ctdbd_next_reqid(conn);
1210 req.flags = CTDB_IMMEDIATE_MIGRATION;
1211 req.callid = CTDB_NULL_FUNC;
1212 req.db_id = db_id;
1213 req.keylen = key.dsize;
1215 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1216 ctdb_packet_dump(&req.hdr);
1218 iov[0].iov_base = &req;
1219 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1220 iov[1].iov_base = key.dptr;
1221 iov[1].iov_len = key.dsize;
1223 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1224 if (nwritten == -1) {
1225 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1226 cluster_fatal("cluster dispatch daemon msg write error\n");
1229 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1231 if (!NT_STATUS_IS_OK(status)) {
1232 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1233 goto fail;
1236 if (hdr->operation != CTDB_REPLY_CALL) {
1237 DEBUG(0, ("received invalid reply\n"));
1238 status = NT_STATUS_INTERNAL_ERROR;
1239 goto fail;
1242 status = NT_STATUS_OK;
1243 fail:
1245 TALLOC_FREE(hdr);
1246 return status;
1250 * Fetch a record and parse it
1252 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1253 TDB_DATA key, bool local_copy,
1254 void (*parser)(TDB_DATA key, TDB_DATA data,
1255 void *private_data),
1256 void *private_data)
1258 struct ctdb_req_call req;
1259 struct ctdb_req_header *hdr = NULL;
1260 struct ctdb_reply_call *reply;
1261 struct iovec iov[2];
1262 ssize_t nwritten;
1263 NTSTATUS status;
1264 uint32_t flags;
1266 flags = local_copy ? CTDB_WANT_READONLY : 0;
1268 ZERO_STRUCT(req);
1270 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1271 req.hdr.ctdb_magic = CTDB_MAGIC;
1272 req.hdr.ctdb_version = CTDB_PROTOCOL;
1273 req.hdr.operation = CTDB_REQ_CALL;
1274 req.hdr.reqid = ctdbd_next_reqid(conn);
1275 req.flags = flags;
1276 req.callid = CTDB_FETCH_FUNC;
1277 req.db_id = db_id;
1278 req.keylen = key.dsize;
1280 iov[0].iov_base = &req;
1281 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1282 iov[1].iov_base = key.dptr;
1283 iov[1].iov_len = key.dsize;
1285 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1286 if (nwritten == -1) {
1287 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1288 cluster_fatal("cluster dispatch daemon msg write error\n");
1291 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1293 if (!NT_STATUS_IS_OK(status)) {
1294 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1295 goto fail;
1298 if ((hdr == NULL) || (hdr->operation != CTDB_REPLY_CALL)) {
1299 DEBUG(0, ("received invalid reply\n"));
1300 status = NT_STATUS_INTERNAL_ERROR;
1301 goto fail;
1303 reply = (struct ctdb_reply_call *)hdr;
1305 if (reply->datalen == 0) {
1307 * Treat an empty record as non-existing
1309 status = NT_STATUS_NOT_FOUND;
1310 goto fail;
1313 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1314 private_data);
1316 status = NT_STATUS_OK;
1317 fail:
1318 TALLOC_FREE(hdr);
1319 return status;
1323 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1324 connection to ctdbd to avoid the hairy recursive and async problems with
1325 everything in-line.
1328 NTSTATUS ctdbd_traverse(uint32_t db_id,
1329 void (*fn)(TDB_DATA key, TDB_DATA data,
1330 void *private_data),
1331 void *private_data)
1333 struct ctdbd_connection *conn;
1334 NTSTATUS status;
1336 TDB_DATA key, data;
1337 struct ctdb_traverse_start t;
1338 int cstatus;
1340 become_root();
1341 status = ctdbd_init_connection(NULL, &conn);
1342 unbecome_root();
1343 if (!NT_STATUS_IS_OK(status)) {
1344 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1345 nt_errstr(status)));
1346 return status;
1349 t.db_id = db_id;
1350 t.srvid = conn->rand_srvid;
1351 t.reqid = ctdbd_next_reqid(conn);
1353 data.dptr = (uint8_t *)&t;
1354 data.dsize = sizeof(t);
1356 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1357 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1358 data, NULL, NULL, &cstatus);
1360 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1362 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1363 cstatus));
1365 if (NT_STATUS_IS_OK(status)) {
1367 * We need a mapping here
1369 status = NT_STATUS_UNSUCCESSFUL;
1371 TALLOC_FREE(conn);
1372 return status;
1375 while (True) {
1376 struct ctdb_req_header *hdr = NULL;
1377 struct ctdb_req_message *m;
1378 struct ctdb_rec_data *d;
1380 status = ctdb_read_packet(conn->fd, conn, &hdr);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1383 nt_errstr(status)));
1384 cluster_fatal("ctdbd died\n");
1387 if (hdr->operation != CTDB_REQ_MESSAGE) {
1388 DEBUG(0, ("Got operation %u, expected a message\n",
1389 (unsigned)hdr->operation));
1390 TALLOC_FREE(conn);
1391 return NT_STATUS_UNEXPECTED_IO_ERROR;
1394 m = (struct ctdb_req_message *)hdr;
1395 d = (struct ctdb_rec_data *)&m->data[0];
1396 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1397 DEBUG(0, ("Got invalid traverse data of length %d\n",
1398 (int)m->datalen));
1399 TALLOC_FREE(conn);
1400 return NT_STATUS_UNEXPECTED_IO_ERROR;
1403 key.dsize = d->keylen;
1404 key.dptr = &d->data[0];
1405 data.dsize = d->datalen;
1406 data.dptr = &d->data[d->keylen];
1408 if (key.dsize == 0 && data.dsize == 0) {
1409 /* end of traverse */
1410 TALLOC_FREE(conn);
1411 return NT_STATUS_OK;
1414 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1415 DEBUG(0, ("Got invalid ltdb header length %d\n",
1416 (int)data.dsize));
1417 TALLOC_FREE(conn);
1418 return NT_STATUS_UNEXPECTED_IO_ERROR;
1420 data.dsize -= sizeof(struct ctdb_ltdb_header);
1421 data.dptr += sizeof(struct ctdb_ltdb_header);
1423 if (fn != NULL) {
1424 fn(key, data, private_data);
1427 return NT_STATUS_OK;
1431 This is used to canonicalize a ctdb_sock_addr structure.
1433 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1434 struct sockaddr_storage *out)
1436 memcpy(out, in, sizeof (*out));
1438 #ifdef HAVE_IPV6
1439 if (in->ss_family == AF_INET6) {
1440 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1441 const struct sockaddr_in6 *in6 =
1442 (const struct sockaddr_in6 *)in;
1443 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1444 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1445 memset(out, 0, sizeof(*out));
1446 #ifdef HAVE_SOCK_SIN_LEN
1447 out4->sin_len = sizeof(*out);
1448 #endif
1449 out4->sin_family = AF_INET;
1450 out4->sin_port = in6->sin6_port;
1451 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1454 #endif
1458 * Register us as a server for a particular tcp connection
1461 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1462 const struct sockaddr_storage *_server,
1463 const struct sockaddr_storage *_client,
1464 bool (*release_ip_handler)(const char *ip_addr,
1465 void *private_data),
1466 void *private_data)
1468 struct ctdb_control_tcp_addr p;
1469 TDB_DATA data = { .dptr = (uint8_t *)&p, .dsize = sizeof(p) };
1470 NTSTATUS status;
1471 struct sockaddr_storage client;
1472 struct sockaddr_storage server;
1475 * Only one connection so far
1477 SMB_ASSERT(conn->release_ip_handler == NULL);
1479 smbd_ctdb_canonicalize_ip(_client, &client);
1480 smbd_ctdb_canonicalize_ip(_server, &server);
1482 switch (client.ss_family) {
1483 case AF_INET:
1484 memcpy(&p.dest.ip, &server, sizeof(p.dest.ip));
1485 memcpy(&p.src.ip, &client, sizeof(p.src.ip));
1486 break;
1487 case AF_INET6:
1488 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1489 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1490 break;
1491 default:
1492 return NT_STATUS_INTERNAL_ERROR;
1495 conn->release_ip_handler = release_ip_handler;
1496 conn->release_ip_priv = private_data;
1499 * We want to be told about IP releases
1502 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP, NULL, NULL);
1503 if (!NT_STATUS_IS_OK(status)) {
1504 return status;
1508 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1509 * can send an extra ack to trigger a reset for our client, so it
1510 * immediately reconnects
1512 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1513 CTDB_CONTROL_TCP_CLIENT, 0,
1514 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1518 call a control on the local node
1520 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1521 uint64_t srvid, uint32_t flags, TDB_DATA data,
1522 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1523 int *cstatus)
1525 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1528 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1530 struct ctdb_client_notify_register reg_data;
1531 size_t struct_len;
1532 NTSTATUS status;
1533 int cstatus;
1535 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1536 reg_data.len = 1;
1537 reg_data.notify_data[0] = 0;
1539 struct_len = offsetof(struct ctdb_client_notify_register,
1540 notify_data) + reg_data.len;
1542 status = ctdbd_control_local(
1543 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1544 make_tdb_data((uint8_t *)&reg_data, struct_len),
1545 NULL, NULL, &cstatus);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1548 nt_errstr(status)));
1550 return status;
1553 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1555 struct ctdb_client_notify_deregister dereg_data;
1556 NTSTATUS status;
1557 int cstatus;
1559 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1561 status = ctdbd_control_local(
1562 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1563 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1564 NULL, NULL, &cstatus);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1567 nt_errstr(status)));
1569 return status;
1572 NTSTATUS ctdbd_probe(void)
1575 * Do a very early check if ctdbd is around to avoid an abort and core
1576 * later
1578 struct ctdbd_connection *conn = NULL;
1579 NTSTATUS status;
1581 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1584 * We only care if we can connect.
1586 TALLOC_FREE(conn);
1588 return status;