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/>.
24 #include "ctdbd_conn.h"
25 #include "system/select.h"
30 * It is not possible to include ctdb.h and tdb_compat.h (included via
31 * some other include above) without warnings. This fixes those
39 #ifdef typesafe_cb_preargs
40 #undef typesafe_cb_preargs
43 #ifdef typesafe_cb_postargs
44 #undef typesafe_cb_postargs
47 /* paths to these include files come from --with-ctdb= in configure */
50 #include "ctdb_private.h"
52 struct ctdbd_connection {
53 struct messaging_context *msg_ctx;
58 struct tevent_fd *fde;
60 bool (*release_ip_handler)(const char *ip_addr, void *private_data);
61 void *release_ip_priv;
64 static uint32_t ctdbd_next_reqid(struct ctdbd_connection *conn)
67 if (conn->reqid == 0) {
73 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
74 uint32_t vnn, uint32_t opcode,
75 uint64_t srvid, uint32_t flags, TDB_DATA data,
76 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
80 * exit on fatal communications errors with the ctdbd daemon
82 static void cluster_fatal(const char *why)
84 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
85 /* we don't use smb_panic() as we don't want to delay to write
86 a core file. We need to release this process id immediately
87 so that someone else can take over without getting sharing
95 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
97 if (DEBUGLEVEL < 11) {
100 DEBUGADD(11, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
101 (int)hdr->length, (int)hdr->ctdb_magic,
102 (int)hdr->ctdb_version, (int)hdr->generation,
103 (int)hdr->operation, (int)hdr->reqid));
107 * Register a srvid with ctdbd
109 NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn, uint64_t srvid)
113 return ctdbd_control(conn, CTDB_CURRENT_NODE,
114 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
115 tdb_null, NULL, NULL, &cstatus);
119 * get our vnn from the cluster
121 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32_t *vnn)
125 status = ctdbd_control(conn,
126 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
127 tdb_null, NULL, NULL, &cstatus);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
132 *vnn = (uint32_t)cstatus;
137 * Are we active (i.e. not banned or stopped?)
139 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
144 struct ctdb_node_map *m;
145 uint32_t failure_flags;
149 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
150 CTDB_CONTROL_GET_NODEMAP, 0, 0,
151 tdb_null, talloc_tos(), &outdata, &cstatus);
152 if (!NT_STATUS_IS_OK(status)) {
153 DEBUG(1, ("ctdbd_control failed: %s\n", nt_errstr(status)));
156 if ((cstatus != 0) || (outdata.dptr == NULL)) {
157 DEBUG(2, ("Received invalid ctdb data\n"));
161 m = (struct ctdb_node_map *)outdata.dptr;
163 for (i=0; i<m->num; i++) {
164 if (vnn == m->nodes[i].pnn) {
170 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
175 failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
176 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
178 if ((m->nodes[i].flags & failure_flags) != 0) {
179 DEBUG(2, ("Node has status %x, not active\n",
180 (int)m->nodes[i].flags));
186 TALLOC_FREE(outdata.dptr);
190 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
192 return conn->our_vnn;
195 const char *lp_ctdbd_socket(void)
199 ret = lp__ctdbd_socket();
200 if (ret != NULL && strlen(ret) > 0) {
208 * Get us a ctdb connection
211 static int ctdbd_connect(int *pfd)
213 const char *sockname = lp_ctdbd_socket();
214 struct sockaddr_un addr = { 0, };
219 fd = socket(AF_UNIX, SOCK_STREAM, 0);
222 DEBUG(3, ("Could not create socket: %s\n", strerror(err)));
226 addr.sun_family = AF_UNIX;
228 namelen = strlcpy(addr.sun_path, sockname, sizeof(addr.sun_path));
229 if (namelen >= sizeof(addr.sun_path)) {
230 DEBUG(3, ("%s: Socket name too long: %s\n", __func__,
236 salen = sizeof(struct sockaddr_un);
238 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
240 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
251 * State necessary to defer an incoming message while we are waiting for a
255 struct deferred_msg_state {
256 struct messaging_context *msg_ctx;
257 struct messaging_rec *rec;
261 * Timed event handler for the deferred message
264 static void deferred_message_dispatch(struct tevent_context *event_ctx,
265 struct tevent_timer *te,
269 struct deferred_msg_state *state = talloc_get_type_abort(
270 private_data, struct deferred_msg_state);
272 messaging_dispatch_rec(state->msg_ctx, state->rec);
278 * Fetch a messaging_rec from an incoming ctdb style message
281 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
282 size_t overall_length,
283 struct ctdb_req_message *msg)
285 struct messaging_rec *result;
287 enum ndr_err_code ndr_err;
289 if ((overall_length < offsetof(struct ctdb_req_message, data))
291 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
293 cluster_fatal("got invalid msg length");
296 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
297 DEBUG(0, ("talloc failed\n"));
301 blob = data_blob_const(msg->data, msg->datalen);
303 ndr_err = ndr_pull_struct_blob(
304 &blob, result, result,
305 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
307 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
308 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
309 ndr_errstr(ndr_err)));
314 if (DEBUGLEVEL >= 11) {
315 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
316 NDR_PRINT_DEBUG(messaging_rec, result);
322 static NTSTATUS ctdb_read_packet(int fd, TALLOC_CTX *mem_ctx,
323 struct ctdb_req_header **result)
325 int timeout = lp_ctdb_timeout();
326 struct ctdb_req_header *req;
336 ret = poll_one_fd(fd, POLLIN, timeout, &revents);
338 return map_nt_error_from_unix(errno);
341 return NT_STATUS_IO_TIMEOUT;
344 return NT_STATUS_UNEXPECTED_IO_ERROR;
348 status = read_data(fd, (char *)&msglen, sizeof(msglen));
349 if (!NT_STATUS_IS_OK(status)) {
353 if (msglen < sizeof(struct ctdb_req_header)) {
354 return NT_STATUS_UNEXPECTED_IO_ERROR;
357 req = talloc_size(mem_ctx, msglen);
359 return NT_STATUS_NO_MEMORY;
361 talloc_set_name_const(req, "struct ctdb_req_header");
363 req->length = msglen;
365 status = read_data(fd, ((char *)req) + sizeof(msglen),
366 msglen - sizeof(msglen));
367 if (!NT_STATUS_IS_OK(status)) {
376 * Read a full ctdbd request. If we have a messaging context, defer incoming
377 * messages that might come in between.
380 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
382 struct ctdb_req_header **result)
384 struct ctdb_req_header *hdr;
389 status = ctdb_read_packet(conn->fd, mem_ctx, &hdr);
390 if (!NT_STATUS_IS_OK(status)) {
391 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
392 cluster_fatal("ctdbd died\n");
395 DEBUG(11, ("Received ctdb packet\n"));
396 ctdb_packet_dump(hdr);
398 if (hdr->operation == CTDB_REQ_MESSAGE) {
399 struct tevent_timer *evt;
400 struct deferred_msg_state *msg_state;
401 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
403 if (conn->msg_ctx == NULL) {
404 DEBUG(1, ("Got a message without having a msg ctx, "
405 "dropping msg %llu\n",
406 (long long unsigned)msg->srvid));
410 if ((conn->release_ip_handler != NULL)
411 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
414 /* must be dispatched immediately */
415 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
416 ret = conn->release_ip_handler((const char *)msg->data,
417 conn->release_ip_priv);
422 * We need to release the ip,
423 * so return an error to the upper layers.
425 * We make sure we don't trigger this again.
427 conn->release_ip_handler = NULL;
428 conn->release_ip_priv = NULL;
429 return NT_STATUS_ADDRESS_CLOSED;
434 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
435 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
437 DEBUG(1, ("ctdb_read_req: Got %s message\n",
438 (msg->srvid == CTDB_SRVID_RECONFIGURE)
439 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
441 messaging_send(conn->msg_ctx,
442 messaging_server_id(conn->msg_ctx),
443 MSG_SMB_BRL_VALIDATE, &data_blob_null);
448 msg_state = talloc(NULL, struct deferred_msg_state);
449 if (msg_state == NULL) {
450 DEBUG(0, ("talloc failed\n"));
455 if (!(msg_state->rec = ctdb_pull_messaging_rec(
456 msg_state, msg->hdr.length, msg))) {
457 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
458 TALLOC_FREE(msg_state);
465 msg_state->msg_ctx = conn->msg_ctx;
468 * We're waiting for a call reply, but an async message has
469 * crossed. Defer dispatching to the toplevel event loop.
471 evt = tevent_add_timer(messaging_tevent_context(conn->msg_ctx),
472 messaging_tevent_context(conn->msg_ctx),
474 deferred_message_dispatch,
477 DEBUG(0, ("event_add_timed failed\n"));
478 TALLOC_FREE(msg_state);
486 if ((reqid != 0) && (hdr->reqid != reqid)) {
487 /* we got the wrong reply */
488 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
489 "been %u\n", hdr->reqid, reqid));
494 *result = talloc_move(mem_ctx, &hdr);
499 static int ctdbd_connection_destructor(struct ctdbd_connection *c)
505 * Get us a ctdbd connection
508 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
509 struct ctdbd_connection **pconn)
511 struct ctdbd_connection *conn;
515 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
516 DEBUG(0, ("talloc failed\n"));
517 return NT_STATUS_NO_MEMORY;
520 ret = ctdbd_connect(&conn->fd);
522 status = map_nt_error_from_unix(errno);
523 DEBUG(10, ("ctdbd_connect failed: %s\n", strerror(errno)));
526 talloc_set_destructor(conn, ctdbd_connection_destructor);
528 status = get_cluster_vnn(conn, &conn->our_vnn);
530 if (!NT_STATUS_IS_OK(status)) {
531 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
535 if (!ctdbd_working(conn, conn->our_vnn)) {
536 DEBUG(2, ("Node is not working, can not connect\n"));
537 status = NT_STATUS_INTERNAL_DB_ERROR;
541 generate_random_buffer((unsigned char *)&conn->rand_srvid,
542 sizeof(conn->rand_srvid));
544 status = register_with_ctdbd(conn, conn->rand_srvid);
546 if (!NT_STATUS_IS_OK(status)) {
547 DEBUG(5, ("Could not register random srvid: %s\n",
561 * Get us a ctdbd connection and register us as a process
564 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
565 struct ctdbd_connection **pconn)
567 struct ctdbd_connection *conn;
570 status = ctdbd_init_connection(mem_ctx, &conn);
572 if (!NT_STATUS_IS_OK(status)) {
576 status = register_with_ctdbd(conn, (uint64_t)getpid());
577 if (!NT_STATUS_IS_OK(status)) {
581 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
582 if (!NT_STATUS_IS_OK(status)) {
586 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
587 if (!NT_STATUS_IS_OK(status)) {
599 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
601 return conn->msg_ctx;
604 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
610 * Packet handler to receive and handle a ctdb message
612 static NTSTATUS ctdb_handle_message(struct messaging_context *msg_ctx,
613 struct ctdbd_connection *conn,
614 struct ctdb_req_header *hdr)
616 struct ctdb_req_message *msg;
617 struct messaging_rec *msg_rec;
619 if (hdr->operation != CTDB_REQ_MESSAGE) {
620 DEBUG(0, ("Received async msg of type %u, discarding\n",
622 return NT_STATUS_INVALID_PARAMETER;
625 msg = (struct ctdb_req_message *)hdr;
627 if ((conn->release_ip_handler != NULL)
628 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
631 /* must be dispatched immediately */
632 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
633 ret = conn->release_ip_handler((const char *)msg->data,
634 conn->release_ip_priv);
637 * We need to release the ip.
639 * We make sure we don't trigger this again.
641 conn->release_ip_handler = NULL;
642 conn->release_ip_priv = NULL;
647 SMB_ASSERT(conn->msg_ctx != NULL);
649 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
650 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
651 DEBUG(0,("Got cluster reconfigure message\n"));
653 * when the cluster is reconfigured or someone of the
654 * family has passed away (SAMBA_NOTIFY), we need to
655 * clean the brl database
657 messaging_send(conn->msg_ctx,
658 messaging_server_id(conn->msg_ctx),
659 MSG_SMB_BRL_VALIDATE, &data_blob_null);
664 /* only messages to our pid or the broadcast are valid here */
665 if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
666 DEBUG(0,("Got unexpected message with srvid=%llu\n",
667 (unsigned long long)msg->srvid));
671 msg_rec = ctdb_pull_messaging_rec(talloc_tos(), msg->hdr.length, msg);
672 if (msg_rec == NULL) {
673 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
674 return NT_STATUS_NO_MEMORY;
676 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
681 * The ctdbd socket is readable asynchronuously
684 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
685 struct tevent_fd *event,
689 struct ctdbd_connection *conn = talloc_get_type_abort(
690 private_data, struct ctdbd_connection);
691 struct ctdb_req_header *hdr;
694 status = ctdb_read_packet(conn->fd, talloc_tos(), &hdr);
695 if (!NT_STATUS_IS_OK(status)) {
696 DEBUG(0, ("ctdb_read_packet failed: %s\n", nt_errstr(status)));
697 cluster_fatal("ctdbd died\n");
700 status = ctdb_handle_message(conn->msg_ctx, conn, hdr);
701 if (!NT_STATUS_IS_OK(status)) {
702 DEBUG(10, ("could not handle incoming message: %s\n",
708 * Prepare a ctdbd connection to receive messages
711 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
712 struct messaging_context *msg_ctx)
714 SMB_ASSERT(conn->msg_ctx == NULL);
715 SMB_ASSERT(conn->fde == NULL);
717 if (!(conn->fde = tevent_add_fd(messaging_tevent_context(msg_ctx),
721 ctdbd_socket_handler,
723 DEBUG(0, ("event_add_fd failed\n"));
724 return NT_STATUS_NO_MEMORY;
727 conn->msg_ctx = msg_ctx;
733 * Send a messaging message across a ctdbd
736 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
737 uint32_t dst_vnn, uint64_t dst_srvid,
738 struct messaging_rec *msg)
742 enum ndr_err_code ndr_err;
744 ndr_err = ndr_push_struct_blob(
745 &blob, talloc_tos(), msg,
746 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
748 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
749 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
750 ndr_errstr(ndr_err)));
751 return ndr_map_error2ntstatus(ndr_err);
754 status = ctdbd_messaging_send_blob(conn, dst_vnn, dst_srvid,
755 blob.data, blob.length);
756 TALLOC_FREE(blob.data);
760 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
761 uint32_t dst_vnn, uint64_t dst_srvid,
762 const uint8_t *buf, size_t buflen)
764 struct ctdb_req_message r;
768 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
769 r.hdr.ctdb_magic = CTDB_MAGIC;
770 r.hdr.ctdb_version = CTDB_VERSION;
771 r.hdr.generation = 1;
772 r.hdr.operation = CTDB_REQ_MESSAGE;
773 r.hdr.destnode = dst_vnn;
774 r.hdr.srcnode = conn->our_vnn;
779 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
780 ctdb_packet_dump(&r.hdr);
782 iov[0].iov_base = &r;
783 iov[0].iov_len = offsetof(struct ctdb_req_message, data);
784 iov[1].iov_base = discard_const_p(uint8_t, buf);
785 iov[1].iov_len = buflen;
787 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
788 if (nwritten == -1) {
789 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
790 cluster_fatal("cluster dispatch daemon msg write error\n");
797 * send/recv a generic ctdb control message
799 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
800 uint32_t vnn, uint32_t opcode,
801 uint64_t srvid, uint32_t flags,
803 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
806 struct ctdb_req_control req;
807 struct ctdb_req_header *hdr;
808 struct ctdb_reply_control *reply = NULL;
809 struct ctdbd_connection *new_conn = NULL;
815 status = ctdbd_init_connection(NULL, &new_conn);
817 if (!NT_STATUS_IS_OK(status)) {
818 DEBUG(10, ("Could not init temp connection: %s\n",
827 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
828 req.hdr.ctdb_magic = CTDB_MAGIC;
829 req.hdr.ctdb_version = CTDB_VERSION;
830 req.hdr.operation = CTDB_REQ_CONTROL;
831 req.hdr.reqid = ctdbd_next_reqid(conn);
832 req.hdr.destnode = vnn;
835 req.datalen = data.dsize;
838 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
839 ctdb_packet_dump(&req.hdr);
841 iov[0].iov_base = &req;
842 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
843 iov[1].iov_base = data.dptr;
844 iov[1].iov_len = data.dsize;
846 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
847 if (nwritten == -1) {
848 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
849 cluster_fatal("cluster dispatch daemon msg write error\n");
852 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
853 TALLOC_FREE(new_conn);
860 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
862 if (!NT_STATUS_IS_OK(status)) {
863 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
867 if (hdr->operation != CTDB_REPLY_CONTROL) {
868 DEBUG(0, ("received invalid reply\n"));
871 reply = (struct ctdb_reply_control *)hdr;
874 if (!(outdata->dptr = (uint8 *)talloc_memdup(
875 mem_ctx, reply->data, reply->datalen))) {
877 return NT_STATUS_NO_MEMORY;
879 outdata->dsize = reply->datalen;
882 (*cstatus) = reply->status;
885 status = NT_STATUS_OK;
888 TALLOC_FREE(new_conn);
894 * see if a remote process exists
896 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
904 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
905 DEBUG(10, ("ctdb_processes_exist failed\n"));
911 bool ctdb_processes_exist(struct ctdbd_connection *conn,
912 const struct server_id *pids, int num_pids,
915 TALLOC_CTX *frame = talloc_stackframe();
921 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
922 if (reqids == NULL) {
926 for (i=0; i<num_pids; i++) {
927 struct ctdb_req_control req;
933 reqids[i] = ctdbd_next_reqid(conn);
938 * pids[i].pid is uint64_t, scale down to pid_t which
939 * is the wire protocol towards ctdb.
943 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
944 (int)pids[i].vnn, (int)pid,
947 req.hdr.length = offsetof(struct ctdb_req_control, data);
948 req.hdr.length += sizeof(pid);
949 req.hdr.ctdb_magic = CTDB_MAGIC;
950 req.hdr.ctdb_version = CTDB_VERSION;
951 req.hdr.operation = CTDB_REQ_CONTROL;
952 req.hdr.reqid = reqids[i];
953 req.hdr.destnode = pids[i].vnn;
954 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
956 req.datalen = sizeof(pid);
959 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
960 ctdb_packet_dump(&req.hdr);
962 iov[0].iov_base = &req;
963 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
964 iov[1].iov_base = &pid;
965 iov[1].iov_len = sizeof(pid);
967 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
968 if (nwritten == -1) {
969 status = map_nt_error_from_unix(errno);
970 DEBUG(10, ("write_data_iov failed: %s\n",
978 while (num_received < num_pids) {
979 struct ctdb_req_header *hdr;
980 struct ctdb_reply_control *reply;
983 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
984 if (!NT_STATUS_IS_OK(status)) {
985 DEBUG(10, ("ctdb_read_req failed: %s\n",
990 if (hdr->operation != CTDB_REPLY_CONTROL) {
991 DEBUG(10, ("Received invalid reply\n"));
994 reply = (struct ctdb_reply_control *)hdr;
996 reqid = reply->hdr.reqid;
998 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1000 for (i=0; i<num_pids; i++) {
1001 if (reqid == reqids[i]) {
1005 if (i == num_pids) {
1006 DEBUG(10, ("Received unknown record number %u\n",
1010 results[i] = ((reply->status) == 0);
1021 struct ctdb_vnn_list {
1024 unsigned num_srvids;
1025 unsigned num_filled;
1027 unsigned *pid_indexes;
1031 * Get a list of all vnns mentioned in a list of
1032 * server_ids. vnn_indexes tells where in the vnns array we have to
1035 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1036 const struct server_id *pids, unsigned num_pids,
1037 struct ctdb_vnn_list **pvnns,
1038 unsigned *pnum_vnns)
1040 struct ctdb_vnn_list *vnns = NULL;
1041 unsigned *vnn_indexes = NULL;
1042 unsigned i, num_vnns = 0;
1044 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1045 if (vnn_indexes == NULL) {
1046 DEBUG(1, ("talloc_array failed\n"));
1050 for (i=0; i<num_pids; i++) {
1052 uint32_t vnn = pids[i].vnn;
1054 for (j=0; j<num_vnns; j++) {
1055 if (vnn == vnns[j].vnn) {
1063 * Already in the array
1065 vnns[j].num_srvids += 1;
1068 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1071 DEBUG(1, ("talloc_realloc failed\n"));
1074 vnns[num_vnns].vnn = vnn;
1075 vnns[num_vnns].num_srvids = 1;
1076 vnns[num_vnns].num_filled = 0;
1079 for (i=0; i<num_vnns; i++) {
1080 struct ctdb_vnn_list *vnn = &vnns[i];
1082 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1083 if (vnn->srvids == NULL) {
1084 DEBUG(1, ("talloc_array failed\n"));
1087 vnn->pid_indexes = talloc_array(vnns, unsigned,
1089 if (vnn->pid_indexes == NULL) {
1090 DEBUG(1, ("talloc_array failed\n"));
1094 for (i=0; i<num_pids; i++) {
1095 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1096 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1097 vnn->pid_indexes[vnn->num_filled] = i;
1098 vnn->num_filled += 1;
1101 TALLOC_FREE(vnn_indexes);
1103 *pnum_vnns = num_vnns;
1107 TALLOC_FREE(vnn_indexes);
1111 bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
1116 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1117 const struct server_id *pids, unsigned num_pids,
1120 unsigned i, num_received;
1122 struct ctdb_vnn_list *vnns = NULL;
1125 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1126 &vnns, &num_vnns)) {
1127 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1131 for (i=0; i<num_vnns; i++) {
1132 struct ctdb_vnn_list *vnn = &vnns[i];
1133 struct ctdb_req_control req;
1134 struct iovec iov[2];
1137 vnn->reqid = ctdbd_next_reqid(conn);
1141 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1142 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1144 req.hdr.length = offsetof(struct ctdb_req_control, data);
1145 req.hdr.ctdb_magic = CTDB_MAGIC;
1146 req.hdr.ctdb_version = CTDB_VERSION;
1147 req.hdr.operation = CTDB_REQ_CONTROL;
1148 req.hdr.reqid = vnn->reqid;
1149 req.hdr.destnode = vnn->vnn;
1150 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1152 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1153 req.hdr.length += req.datalen;
1156 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1157 ctdb_packet_dump(&req.hdr);
1159 iov[0].iov_base = &req;
1160 iov[0].iov_len = offsetof(struct ctdb_req_control, data);
1161 iov[1].iov_base = vnn->srvids;
1162 iov[1].iov_len = req.datalen;
1164 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1165 if (nwritten == -1) {
1166 status = map_nt_error_from_unix(errno);
1167 DEBUG(10, ("write_data_iov failed: %s\n",
1175 while (num_received < num_vnns) {
1176 struct ctdb_req_header *hdr;
1177 struct ctdb_reply_control *reply;
1178 struct ctdb_vnn_list *vnn;
1180 uint8_t *reply_data;
1182 status = ctdb_read_req(conn, 0, talloc_tos(), &hdr);
1183 if (!NT_STATUS_IS_OK(status)) {
1184 DEBUG(1, ("ctdb_read_req failed: %s\n",
1185 nt_errstr(status)));
1189 if (hdr->operation != CTDB_REPLY_CONTROL) {
1190 DEBUG(1, ("Received invalid reply %u\n",
1191 (unsigned)reply->hdr.operation));
1194 reply = (struct ctdb_reply_control *)hdr;
1196 reqid = reply->hdr.reqid;
1198 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1200 for (i=0; i<num_vnns; i++) {
1201 if (reqid == vnns[i].reqid) {
1205 if (i == num_vnns) {
1206 DEBUG(1, ("Received unknown reqid number %u\n",
1211 DEBUG(10, ("Found index %u\n", i));
1215 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1216 (unsigned)vnn->vnn, vnn->num_srvids,
1217 (unsigned)reply->datalen));
1219 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1223 reply_data = reply->data;
1226 * Got an error reply
1228 DEBUG(5, ("Received short reply len %d, status %u, "
1230 (unsigned)reply->datalen,
1231 (unsigned)reply->status,
1232 (unsigned)reply->errorlen));
1233 dump_data(5, reply->data, reply->errorlen);
1236 * This will trigger everything set to false
1241 for (i=0; i<vnn->num_srvids; i++) {
1242 int idx = vnn->pid_indexes[i];
1244 if (pids[i].unique_id ==
1245 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1246 results[idx] = true;
1250 (reply_data != NULL) &&
1251 ((reply_data[i/8] & (1<<(i%8))) != 0);
1261 cluster_fatal("serverids_exist failed");
1268 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1269 TALLOC_CTX *mem_ctx, uint32_t db_id)
1275 data.dptr = (uint8_t*)&db_id;
1276 data.dsize = sizeof(db_id);
1278 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1279 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1280 mem_ctx, &data, &cstatus);
1281 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1282 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1286 return (char *)data.dptr;
1290 * attach to a ctdb database
1292 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1293 const char *name, uint32_t *db_id, int tdb_flags)
1298 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1300 data = string_term_tdb_data(name);
1302 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1304 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1305 : CTDB_CONTROL_DB_ATTACH,
1306 tdb_flags, 0, data, NULL, &data, &cstatus);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 DEBUG(0, (__location__ " ctdb_control for db_attach "
1309 "failed: %s\n", nt_errstr(status)));
1313 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1314 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1315 return NT_STATUS_INTERNAL_ERROR;
1318 *db_id = *(uint32_t *)data.dptr;
1319 talloc_free(data.dptr);
1321 if (!(tdb_flags & TDB_SEQNUM)) {
1322 return NT_STATUS_OK;
1325 data.dptr = (uint8_t *)db_id;
1326 data.dsize = sizeof(*db_id);
1328 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1329 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1330 NULL, NULL, &cstatus);
1331 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1332 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1334 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1338 return NT_STATUS_OK;
1342 * force the migration of a record to this node
1344 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1347 struct ctdb_req_call req;
1348 struct ctdb_req_header *hdr;
1349 struct iovec iov[2];
1355 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1356 req.hdr.ctdb_magic = CTDB_MAGIC;
1357 req.hdr.ctdb_version = CTDB_VERSION;
1358 req.hdr.operation = CTDB_REQ_CALL;
1359 req.hdr.reqid = ctdbd_next_reqid(conn);
1360 req.flags = CTDB_IMMEDIATE_MIGRATION;
1361 req.callid = CTDB_NULL_FUNC;
1363 req.keylen = key.dsize;
1365 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1366 ctdb_packet_dump(&req.hdr);
1368 iov[0].iov_base = &req;
1369 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1370 iov[1].iov_base = key.dptr;
1371 iov[1].iov_len = key.dsize;
1373 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1374 if (nwritten == -1) {
1375 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1376 cluster_fatal("cluster dispatch daemon msg write error\n");
1379 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1386 if (hdr->operation != CTDB_REPLY_CALL) {
1387 DEBUG(0, ("received invalid reply\n"));
1388 status = NT_STATUS_INTERNAL_ERROR;
1392 status = NT_STATUS_OK;
1400 * Fetch a record and parse it
1402 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1403 TDB_DATA key, bool local_copy,
1404 void (*parser)(TDB_DATA key, TDB_DATA data,
1405 void *private_data),
1408 struct ctdb_req_call req;
1409 struct ctdb_req_header *hdr = NULL;
1410 struct ctdb_reply_call *reply;
1411 struct iovec iov[2];
1416 flags = local_copy ? CTDB_WANT_READONLY : 0;
1420 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1421 req.hdr.ctdb_magic = CTDB_MAGIC;
1422 req.hdr.ctdb_version = CTDB_VERSION;
1423 req.hdr.operation = CTDB_REQ_CALL;
1424 req.hdr.reqid = ctdbd_next_reqid(conn);
1426 req.callid = CTDB_FETCH_FUNC;
1428 req.keylen = key.dsize;
1430 iov[0].iov_base = &req;
1431 iov[0].iov_len = offsetof(struct ctdb_req_call, data);
1432 iov[1].iov_base = key.dptr;
1433 iov[1].iov_len = key.dsize;
1435 nwritten = write_data_iov(conn->fd, iov, ARRAY_SIZE(iov));
1436 if (nwritten == -1) {
1437 DEBUG(3, ("write_data_iov failed: %s\n", strerror(errno)));
1438 cluster_fatal("cluster dispatch daemon msg write error\n");
1441 status = ctdb_read_req(conn, req.hdr.reqid, NULL, &hdr);
1443 if (!NT_STATUS_IS_OK(status)) {
1444 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1448 if (hdr->operation != CTDB_REPLY_CALL) {
1449 DEBUG(0, ("received invalid reply\n"));
1450 status = NT_STATUS_INTERNAL_ERROR;
1453 reply = (struct ctdb_reply_call *)hdr;
1455 if (reply->datalen == 0) {
1457 * Treat an empty record as non-existing
1459 status = NT_STATUS_NOT_FOUND;
1463 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1466 status = NT_STATUS_OK;
1473 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1474 connection to ctdbd to avoid the hairy recursive and async problems with
1478 NTSTATUS ctdbd_traverse(uint32_t db_id,
1479 void (*fn)(TDB_DATA key, TDB_DATA data,
1480 void *private_data),
1483 struct ctdbd_connection *conn;
1487 struct ctdb_traverse_start t;
1491 status = ctdbd_init_connection(NULL, &conn);
1493 if (!NT_STATUS_IS_OK(status)) {
1494 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1495 nt_errstr(status)));
1500 t.srvid = conn->rand_srvid;
1501 t.reqid = ctdbd_next_reqid(conn);
1503 data.dptr = (uint8_t *)&t;
1504 data.dsize = sizeof(t);
1506 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1507 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1508 data, NULL, NULL, &cstatus);
1510 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1512 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1515 if (NT_STATUS_IS_OK(status)) {
1517 * We need a mapping here
1519 status = NT_STATUS_UNSUCCESSFUL;
1526 struct ctdb_req_header *hdr;
1527 struct ctdb_req_message *m;
1528 struct ctdb_rec_data *d;
1530 status = ctdb_read_packet(conn->fd, conn, &hdr);
1531 if (!NT_STATUS_IS_OK(status)) {
1532 DEBUG(0, ("ctdb_read_packet failed: %s\n",
1533 nt_errstr(status)));
1534 cluster_fatal("ctdbd died\n");
1537 if (hdr->operation != CTDB_REQ_MESSAGE) {
1538 DEBUG(0, ("Got operation %u, expected a message\n",
1539 (unsigned)hdr->operation));
1541 return NT_STATUS_UNEXPECTED_IO_ERROR;
1544 m = (struct ctdb_req_message *)hdr;
1545 d = (struct ctdb_rec_data *)&m->data[0];
1546 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1547 DEBUG(0, ("Got invalid traverse data of length %d\n",
1550 return NT_STATUS_UNEXPECTED_IO_ERROR;
1553 key.dsize = d->keylen;
1554 key.dptr = &d->data[0];
1555 data.dsize = d->datalen;
1556 data.dptr = &d->data[d->keylen];
1558 if (key.dsize == 0 && data.dsize == 0) {
1559 /* end of traverse */
1561 return NT_STATUS_OK;
1564 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1565 DEBUG(0, ("Got invalid ltdb header length %d\n",
1568 return NT_STATUS_UNEXPECTED_IO_ERROR;
1570 data.dsize -= sizeof(struct ctdb_ltdb_header);
1571 data.dptr += sizeof(struct ctdb_ltdb_header);
1574 fn(key, data, private_data);
1577 return NT_STATUS_OK;
1581 This is used to canonicalize a ctdb_sock_addr structure.
1583 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1584 struct sockaddr_storage *out)
1586 memcpy(out, in, sizeof (*out));
1589 if (in->ss_family == AF_INET6) {
1590 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1591 const struct sockaddr_in6 *in6 =
1592 (const struct sockaddr_in6 *)in;
1593 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1594 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1595 memset(out, 0, sizeof(*out));
1596 #ifdef HAVE_SOCK_SIN_LEN
1597 out4->sin_len = sizeof(*out);
1599 out4->sin_family = AF_INET;
1600 out4->sin_port = in6->sin6_port;
1601 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1608 * Register us as a server for a particular tcp connection
1611 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1612 const struct sockaddr_storage *_server,
1613 const struct sockaddr_storage *_client,
1614 bool (*release_ip_handler)(const char *ip_addr,
1615 void *private_data),
1619 * we still use ctdb_control_tcp for ipv4
1620 * because we want to work against older ctdb
1621 * versions at runtime
1623 struct ctdb_control_tcp p4;
1624 struct ctdb_control_tcp_addr p;
1627 struct sockaddr_storage client;
1628 struct sockaddr_storage server;
1631 * Only one connection so far
1633 SMB_ASSERT(conn->release_ip_handler == NULL);
1635 smbd_ctdb_canonicalize_ip(_client, &client);
1636 smbd_ctdb_canonicalize_ip(_server, &server);
1638 switch (client.ss_family) {
1640 memcpy(&p4.dest, &server, sizeof(p4.dest));
1641 memcpy(&p4.src, &client, sizeof(p4.src));
1642 data.dptr = (uint8_t *)&p4;
1643 data.dsize = sizeof(p4);
1646 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1647 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1648 data.dptr = (uint8_t *)&p;
1649 data.dsize = sizeof(p);
1652 return NT_STATUS_INTERNAL_ERROR;
1655 conn->release_ip_handler = release_ip_handler;
1656 conn->release_ip_priv = private_data;
1659 * We want to be told about IP releases
1662 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1663 if (!NT_STATUS_IS_OK(status)) {
1668 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1669 * can send an extra ack to trigger a reset for our client, so it
1670 * immediately reconnects
1672 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1673 CTDB_CONTROL_TCP_CLIENT, 0,
1674 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1678 * We want to handle reconfigure events
1680 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1682 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1686 call a control on the local node
1688 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1689 uint64_t srvid, uint32_t flags, TDB_DATA data,
1690 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1693 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1696 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1698 struct ctdb_client_notify_register reg_data;
1703 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1705 reg_data.notify_data[0] = 0;
1707 struct_len = offsetof(struct ctdb_client_notify_register,
1708 notify_data) + reg_data.len;
1710 status = ctdbd_control_local(
1711 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1712 make_tdb_data((uint8_t *)®_data, struct_len),
1713 NULL, NULL, &cstatus);
1714 if (!NT_STATUS_IS_OK(status)) {
1715 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1716 nt_errstr(status)));
1721 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1723 struct ctdb_client_notify_deregister dereg_data;
1727 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1729 status = ctdbd_control_local(
1730 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1731 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1732 NULL, NULL, &cstatus);
1733 if (!NT_STATUS_IS_OK(status)) {
1734 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1735 nt_errstr(status)));
1740 NTSTATUS ctdbd_probe(void)
1743 * Do a very early check if ctdbd is around to avoid an abort and core
1746 struct ctdbd_connection *conn = NULL;
1749 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1752 * We only care if we can connect.