smbd: fix creation of BUILTIN\{Administrators,Users} when "tdbsam:map builtin = false"
[Samba/wip.git] / source3 / lib / ctdbd_conn.c
blobfae3f9077195c3f5a1f1d9d74e6bac41cc11e462
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"
26 #include "ctdb_packet.h"
27 #include "messages.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
32 * warnings.
35 #ifdef typesafe_cb
36 #undef typesafe_cb
37 #endif
39 #ifdef typesafe_cb_preargs
40 #undef typesafe_cb_preargs
41 #endif
43 #ifdef typesafe_cb_postargs
44 #undef typesafe_cb_postargs
45 #endif
47 /* paths to these include files come from --with-ctdb= in configure */
49 #include "ctdb.h"
50 #include "ctdb_private.h"
52 struct ctdbd_connection {
53 struct messaging_context *msg_ctx;
54 uint32_t reqid;
55 uint32_t our_vnn;
56 uint64_t rand_srvid;
57 struct ctdb_packet_context *pkt;
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)
66 conn->reqid += 1;
67 if (conn->reqid == 0) {
68 conn->reqid += 1;
70 return conn->reqid;
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,
77 int *cstatus);
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
88 violations */
89 _exit(1);
95 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
97 if (DEBUGLEVEL < 11) {
98 return;
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)
112 int cstatus;
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)
123 int32_t cstatus=-1;
124 NTSTATUS status;
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)));
130 return status;
132 *vnn = (uint32_t)cstatus;
133 return status;
137 * Are we active (i.e. not banned or stopped?)
139 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
141 int32_t cstatus=-1;
142 NTSTATUS status;
143 TDB_DATA outdata;
144 struct ctdb_node_map *m;
145 uint32_t failure_flags;
146 bool ret = false;
147 int i;
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)));
154 return false;
156 if ((cstatus != 0) || (outdata.dptr == NULL)) {
157 DEBUG(2, ("Received invalid ctdb data\n"));
158 return false;
161 m = (struct ctdb_node_map *)outdata.dptr;
163 for (i=0; i<m->num; i++) {
164 if (vnn == m->nodes[i].pnn) {
165 break;
169 if (i == m->num) {
170 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
171 (int)vnn));
172 goto fail;
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));
181 goto fail;
184 ret = true;
185 fail:
186 TALLOC_FREE(outdata.dptr);
187 return ret;
190 uint32_t ctdbd_vnn(const struct ctdbd_connection *conn)
192 return conn->our_vnn;
195 const char *lp_ctdbd_socket(void)
197 const char *ret;
199 ret = lp__ctdbd_socket();
200 if (ret != NULL && strlen(ret) > 0) {
201 return ret;
204 return CTDB_PATH;
208 * Get us a ctdb connection
211 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
212 struct ctdb_packet_context **presult)
214 struct ctdb_packet_context *result;
215 const char *sockname = lp_ctdbd_socket();
216 struct sockaddr_un addr = { 0, };
217 int fd;
218 socklen_t salen;
220 fd = socket(AF_UNIX, SOCK_STREAM, 0);
221 if (fd == -1) {
222 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
223 return map_nt_error_from_unix(errno);
226 addr.sun_family = AF_UNIX;
227 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sockname);
229 salen = sizeof(struct sockaddr_un);
230 if (connect(fd, (struct sockaddr *)(void *)&addr, salen) == -1) {
231 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
232 strerror(errno)));
233 close(fd);
234 return map_nt_error_from_unix(errno);
237 if (!(result = ctdb_packet_init(mem_ctx, fd))) {
238 close(fd);
239 return NT_STATUS_NO_MEMORY;
242 *presult = result;
243 return NT_STATUS_OK;
247 * Do we have a complete ctdb packet in the queue?
250 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
251 size_t *length,
252 void *private_data)
254 uint32_t msglen;
256 if (available < sizeof(msglen)) {
257 return False;
260 msglen = *((const uint32_t *)buf);
262 DEBUG(11, ("msglen = %d\n", msglen));
264 if (msglen < sizeof(struct ctdb_req_header)) {
265 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
266 "the req_header\n", (int)msglen,
267 (int)sizeof(struct ctdb_req_header)));
268 cluster_fatal("ctdbd protocol error\n");
271 if (available < msglen) {
272 return false;
275 *length = msglen;
276 return true;
280 * State necessary to defer an incoming message while we are waiting for a
281 * ctdb reply.
284 struct deferred_msg_state {
285 struct messaging_context *msg_ctx;
286 struct messaging_rec *rec;
290 * Timed event handler for the deferred message
293 static void deferred_message_dispatch(struct tevent_context *event_ctx,
294 struct tevent_timer *te,
295 struct timeval now,
296 void *private_data)
298 struct deferred_msg_state *state = talloc_get_type_abort(
299 private_data, struct deferred_msg_state);
301 messaging_dispatch_rec(state->msg_ctx, state->rec);
302 TALLOC_FREE(state);
303 TALLOC_FREE(te);
306 struct req_pull_state {
307 TALLOC_CTX *mem_ctx;
308 DATA_BLOB req;
312 * Pull a ctdb request out of the incoming ctdb_packet queue
315 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
316 void *private_data)
318 struct req_pull_state *state = (struct req_pull_state *)private_data;
320 state->req.data = talloc_move(state->mem_ctx, &buf);
321 state->req.length = length;
322 return NT_STATUS_OK;
326 * Fetch a messaging_rec from an incoming ctdb style message
329 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
330 size_t overall_length,
331 struct ctdb_req_message *msg)
333 struct messaging_rec *result;
334 DATA_BLOB blob;
335 enum ndr_err_code ndr_err;
337 if ((overall_length < offsetof(struct ctdb_req_message, data))
338 || (overall_length
339 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
341 cluster_fatal("got invalid msg length");
344 if (!(result = talloc(mem_ctx, struct messaging_rec))) {
345 DEBUG(0, ("talloc failed\n"));
346 return NULL;
349 blob = data_blob_const(msg->data, msg->datalen);
351 ndr_err = ndr_pull_struct_blob(
352 &blob, result, result,
353 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
355 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
356 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
357 ndr_errstr(ndr_err)));
358 TALLOC_FREE(result);
359 return NULL;
362 if (DEBUGLEVEL >= 11) {
363 DEBUG(11, ("ctdb_pull_messaging_rec:\n"));
364 NDR_PRINT_DEBUG(messaging_rec, result);
367 return result;
370 static NTSTATUS ctdb_packet_fd_read_sync(struct ctdb_packet_context *ctx)
372 int timeout = lp_ctdb_timeout();
374 if (timeout == 0) {
375 timeout = -1;
377 return ctdb_packet_fd_read_sync_timeout(ctx, timeout);
381 * Read a full ctdbd request. If we have a messaging context, defer incoming
382 * messages that might come in between.
385 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32_t reqid,
386 TALLOC_CTX *mem_ctx, void *result)
388 struct ctdb_req_header *hdr;
389 struct req_pull_state state;
390 NTSTATUS status;
392 next_pkt:
393 ZERO_STRUCT(state);
394 state.mem_ctx = mem_ctx;
396 while (!ctdb_packet_handler(conn->pkt, ctdb_req_complete,
397 ctdb_req_pull, &state, &status)) {
399 * Not enough data
401 status = ctdb_packet_fd_read_sync(conn->pkt);
403 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
404 /* EAGAIN */
405 continue;
406 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
407 /* EAGAIN */
408 continue;
411 if (!NT_STATUS_IS_OK(status)) {
412 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
413 cluster_fatal("ctdbd died\n");
417 if (!NT_STATUS_IS_OK(status)) {
418 DEBUG(0, ("Could not read ctdb_packet: %s\n", nt_errstr(status)));
419 cluster_fatal("ctdbd died\n");
422 hdr = (struct ctdb_req_header *)state.req.data;
424 DEBUG(11, ("Received ctdb packet\n"));
425 ctdb_packet_dump(hdr);
427 if (hdr->operation == CTDB_REQ_MESSAGE) {
428 struct tevent_timer *evt;
429 struct deferred_msg_state *msg_state;
430 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
432 if (conn->msg_ctx == NULL) {
433 DEBUG(1, ("Got a message without having a msg ctx, "
434 "dropping msg %llu\n",
435 (long long unsigned)msg->srvid));
436 goto next_pkt;
439 if ((conn->release_ip_handler != NULL)
440 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
441 bool ret;
443 /* must be dispatched immediately */
444 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
445 ret = conn->release_ip_handler((const char *)msg->data,
446 conn->release_ip_priv);
447 if (ret) {
449 * We need to release the ip,
450 * so return an error to the upper layers.
452 * We make sure we don't trigger this again.
454 conn->release_ip_handler = NULL;
455 conn->release_ip_priv = NULL;
456 return NT_STATUS_ADDRESS_CLOSED;
458 TALLOC_FREE(hdr);
459 goto next_pkt;
462 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
463 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
465 DEBUG(1, ("ctdb_read_req: Got %s message\n",
466 (msg->srvid == CTDB_SRVID_RECONFIGURE)
467 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
469 messaging_send(conn->msg_ctx,
470 messaging_server_id(conn->msg_ctx),
471 MSG_SMB_BRL_VALIDATE, &data_blob_null);
472 TALLOC_FREE(hdr);
473 goto next_pkt;
476 msg_state = talloc(NULL, struct deferred_msg_state);
477 if (msg_state == NULL) {
478 DEBUG(0, ("talloc failed\n"));
479 TALLOC_FREE(hdr);
480 goto next_pkt;
483 if (!(msg_state->rec = ctdb_pull_messaging_rec(
484 msg_state, state.req.length, msg))) {
485 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
486 TALLOC_FREE(msg_state);
487 TALLOC_FREE(hdr);
488 goto next_pkt;
491 TALLOC_FREE(hdr);
493 msg_state->msg_ctx = conn->msg_ctx;
496 * We're waiting for a call reply, but an async message has
497 * crossed. Defer dispatching to the toplevel event loop.
499 evt = tevent_add_timer(conn->msg_ctx->event_ctx,
500 conn->msg_ctx->event_ctx,
501 timeval_zero(),
502 deferred_message_dispatch,
503 msg_state);
504 if (evt == NULL) {
505 DEBUG(0, ("event_add_timed failed\n"));
506 TALLOC_FREE(msg_state);
507 TALLOC_FREE(hdr);
508 goto next_pkt;
511 goto next_pkt;
514 if ((reqid != 0) && (hdr->reqid != reqid)) {
515 /* we got the wrong reply */
516 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
517 "been %u\n", hdr->reqid, reqid));
518 TALLOC_FREE(hdr);
519 goto next_pkt;
522 *((void **)result) = talloc_move(mem_ctx, &hdr);
524 return NT_STATUS_OK;
528 * Get us a ctdbd connection
531 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
532 struct ctdbd_connection **pconn)
534 struct ctdbd_connection *conn;
535 NTSTATUS status;
537 if (!(conn = talloc_zero(mem_ctx, struct ctdbd_connection))) {
538 DEBUG(0, ("talloc failed\n"));
539 return NT_STATUS_NO_MEMORY;
542 status = ctdbd_connect(conn, &conn->pkt);
544 if (!NT_STATUS_IS_OK(status)) {
545 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
546 goto fail;
549 status = get_cluster_vnn(conn, &conn->our_vnn);
551 if (!NT_STATUS_IS_OK(status)) {
552 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
553 goto fail;
556 if (!ctdbd_working(conn, conn->our_vnn)) {
557 DEBUG(2, ("Node is not working, can not connect\n"));
558 status = NT_STATUS_INTERNAL_DB_ERROR;
559 goto fail;
562 generate_random_buffer((unsigned char *)&conn->rand_srvid,
563 sizeof(conn->rand_srvid));
565 status = register_with_ctdbd(conn, conn->rand_srvid);
567 if (!NT_STATUS_IS_OK(status)) {
568 DEBUG(5, ("Could not register random srvid: %s\n",
569 nt_errstr(status)));
570 goto fail;
573 *pconn = conn;
574 return NT_STATUS_OK;
576 fail:
577 TALLOC_FREE(conn);
578 return status;
582 * Get us a ctdbd connection and register us as a process
585 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
586 struct ctdbd_connection **pconn)
588 struct ctdbd_connection *conn;
589 NTSTATUS status;
591 status = ctdbd_init_connection(mem_ctx, &conn);
593 if (!NT_STATUS_IS_OK(status)) {
594 return status;
597 status = register_with_ctdbd(conn, (uint64_t)getpid());
598 if (!NT_STATUS_IS_OK(status)) {
599 goto fail;
602 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
603 if (!NT_STATUS_IS_OK(status)) {
604 goto fail;
607 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
608 if (!NT_STATUS_IS_OK(status)) {
609 goto fail;
612 *pconn = conn;
613 return NT_STATUS_OK;
615 fail:
616 TALLOC_FREE(conn);
617 return status;
620 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
622 return conn->msg_ctx;
625 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
627 return ctdb_packet_get_fd(conn->pkt);
631 * Packet handler to receive and handle a ctdb message
633 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
634 void *private_data)
636 struct ctdbd_connection *conn = talloc_get_type_abort(
637 private_data, struct ctdbd_connection);
638 struct ctdb_req_message *msg;
639 struct messaging_rec *msg_rec;
641 msg = (struct ctdb_req_message *)buf;
643 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
644 DEBUG(0, ("Received async msg of type %u, discarding\n",
645 msg->hdr.operation));
646 TALLOC_FREE(buf);
647 return NT_STATUS_INVALID_PARAMETER;
650 if ((conn->release_ip_handler != NULL)
651 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
652 bool ret;
654 /* must be dispatched immediately */
655 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
656 ret = conn->release_ip_handler((const char *)msg->data,
657 conn->release_ip_priv);
658 if (ret) {
660 * We need to release the ip.
662 * We make sure we don't trigger this again.
664 conn->release_ip_handler = NULL;
665 conn->release_ip_priv = NULL;
667 TALLOC_FREE(buf);
668 return NT_STATUS_OK;
671 SMB_ASSERT(conn->msg_ctx != NULL);
673 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
674 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
675 DEBUG(0,("Got cluster reconfigure message\n"));
677 * when the cluster is reconfigured or someone of the
678 * family has passed away (SAMBA_NOTIFY), we need to
679 * clean the brl database
681 messaging_send(conn->msg_ctx,
682 messaging_server_id(conn->msg_ctx),
683 MSG_SMB_BRL_VALIDATE, &data_blob_null);
685 TALLOC_FREE(buf);
686 return NT_STATUS_OK;
689 /* only messages to our pid or the broadcast are valid here */
690 if (msg->srvid != getpid() && msg->srvid != MSG_SRVID_SAMBA) {
691 DEBUG(0,("Got unexpected message with srvid=%llu\n",
692 (unsigned long long)msg->srvid));
693 TALLOC_FREE(buf);
694 return NT_STATUS_OK;
697 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
698 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
699 TALLOC_FREE(buf);
700 return NT_STATUS_NO_MEMORY;
703 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
705 TALLOC_FREE(msg_rec);
706 TALLOC_FREE(buf);
707 return NT_STATUS_OK;
711 * The ctdbd socket is readable asynchronuously
714 static void ctdbd_socket_handler(struct tevent_context *event_ctx,
715 struct tevent_fd *event,
716 uint16 flags,
717 void *private_data)
719 struct ctdbd_connection *conn = talloc_get_type_abort(
720 private_data, struct ctdbd_connection);
722 NTSTATUS status;
724 status = ctdb_packet_fd_read(conn->pkt);
726 if (!NT_STATUS_IS_OK(status)) {
727 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
728 cluster_fatal("ctdbd died\n");
731 while (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
732 ctdb_handle_message, conn, &status)) {
733 if (!NT_STATUS_IS_OK(status)) {
734 DEBUG(10, ("could not handle incoming message: %s\n",
735 nt_errstr(status)));
741 * Prepare a ctdbd connection to receive messages
744 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
745 struct messaging_context *msg_ctx)
747 SMB_ASSERT(conn->msg_ctx == NULL);
748 SMB_ASSERT(conn->fde == NULL);
750 if (!(conn->fde = tevent_add_fd(msg_ctx->event_ctx, conn,
751 ctdb_packet_get_fd(conn->pkt),
752 TEVENT_FD_READ,
753 ctdbd_socket_handler,
754 conn))) {
755 DEBUG(0, ("event_add_fd failed\n"));
756 return NT_STATUS_NO_MEMORY;
759 conn->msg_ctx = msg_ctx;
761 return NT_STATUS_OK;
765 * Send a messaging message across a ctdbd
768 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
769 uint32_t dst_vnn, uint64_t dst_srvid,
770 struct messaging_rec *msg)
772 DATA_BLOB blob;
773 NTSTATUS status;
774 enum ndr_err_code ndr_err;
776 ndr_err = ndr_push_struct_blob(
777 &blob, talloc_tos(), msg,
778 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
780 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
781 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
782 ndr_errstr(ndr_err)));
783 return ndr_map_error2ntstatus(ndr_err);
786 status = ctdbd_messaging_send_blob(conn, dst_vnn, dst_srvid,
787 blob.data, blob.length);
788 TALLOC_FREE(blob.data);
789 return status;
792 NTSTATUS ctdbd_messaging_send_blob(struct ctdbd_connection *conn,
793 uint32_t dst_vnn, uint64_t dst_srvid,
794 const uint8_t *buf, size_t buflen)
796 struct ctdb_req_message r;
797 NTSTATUS status;
799 r.hdr.length = offsetof(struct ctdb_req_message, data) + buflen;
800 r.hdr.ctdb_magic = CTDB_MAGIC;
801 r.hdr.ctdb_version = CTDB_VERSION;
802 r.hdr.generation = 1;
803 r.hdr.operation = CTDB_REQ_MESSAGE;
804 r.hdr.destnode = dst_vnn;
805 r.hdr.srcnode = conn->our_vnn;
806 r.hdr.reqid = 0;
807 r.srvid = dst_srvid;
808 r.datalen = buflen;
810 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
811 ctdb_packet_dump(&r.hdr);
813 status = ctdb_packet_send(
814 conn->pkt, 2,
815 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
816 data_blob_const(buf, buflen));
818 if (!NT_STATUS_IS_OK(status)) {
819 DEBUG(0, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
820 return status;
823 status = ctdb_packet_flush(conn->pkt);
824 if (!NT_STATUS_IS_OK(status)) {
825 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
826 cluster_fatal("cluster dispatch daemon msg write error\n");
828 return NT_STATUS_OK;
832 * send/recv a generic ctdb control message
834 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
835 uint32_t vnn, uint32_t opcode,
836 uint64_t srvid, uint32_t flags,
837 TDB_DATA data,
838 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
839 int *cstatus)
841 struct ctdb_req_control req;
842 struct ctdb_reply_control *reply = NULL;
843 struct ctdbd_connection *new_conn = NULL;
844 NTSTATUS status;
846 if (conn == NULL) {
847 status = ctdbd_init_connection(NULL, &new_conn);
849 if (!NT_STATUS_IS_OK(status)) {
850 DEBUG(10, ("Could not init temp connection: %s\n",
851 nt_errstr(status)));
852 goto fail;
855 conn = new_conn;
858 ZERO_STRUCT(req);
859 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
860 req.hdr.ctdb_magic = CTDB_MAGIC;
861 req.hdr.ctdb_version = CTDB_VERSION;
862 req.hdr.operation = CTDB_REQ_CONTROL;
863 req.hdr.reqid = ctdbd_next_reqid(conn);
864 req.hdr.destnode = vnn;
865 req.opcode = opcode;
866 req.srvid = srvid;
867 req.datalen = data.dsize;
868 req.flags = flags;
870 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
871 ctdb_packet_dump(&req.hdr);
873 status = ctdb_packet_send(
874 conn->pkt, 2,
875 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
876 data_blob_const(data.dptr, data.dsize));
878 if (!NT_STATUS_IS_OK(status)) {
879 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
880 goto fail;
883 status = ctdb_packet_flush(conn->pkt);
885 if (!NT_STATUS_IS_OK(status)) {
886 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
887 cluster_fatal("cluster dispatch daemon control write error\n");
890 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
891 TALLOC_FREE(new_conn);
892 if (cstatus) {
893 *cstatus = 0;
895 return NT_STATUS_OK;
898 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
900 if (!NT_STATUS_IS_OK(status)) {
901 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
902 goto fail;
905 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
906 DEBUG(0, ("received invalid reply\n"));
907 goto fail;
910 if (outdata) {
911 if (!(outdata->dptr = (uint8 *)talloc_memdup(
912 mem_ctx, reply->data, reply->datalen))) {
913 TALLOC_FREE(reply);
914 return NT_STATUS_NO_MEMORY;
916 outdata->dsize = reply->datalen;
918 if (cstatus) {
919 (*cstatus) = reply->status;
922 status = NT_STATUS_OK;
924 fail:
925 TALLOC_FREE(new_conn);
926 TALLOC_FREE(reply);
927 return status;
931 * see if a remote process exists
933 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32_t vnn, pid_t pid)
935 struct server_id id;
936 bool result;
938 id.pid = pid;
939 id.vnn = vnn;
941 if (!ctdb_processes_exist(conn, &id, 1, &result)) {
942 DEBUG(10, ("ctdb_processes_exist failed\n"));
943 return false;
945 return result;
948 bool ctdb_processes_exist(struct ctdbd_connection *conn,
949 const struct server_id *pids, int num_pids,
950 bool *results)
952 TALLOC_CTX *frame = talloc_stackframe();
953 int i, num_received;
954 NTSTATUS status;
955 uint32_t *reqids;
956 bool result = false;
958 reqids = talloc_array(talloc_tos(), uint32_t, num_pids);
959 if (reqids == NULL) {
960 goto fail;
963 for (i=0; i<num_pids; i++) {
964 struct ctdb_req_control req;
965 pid_t pid;
967 results[i] = false;
968 reqids[i] = ctdbd_next_reqid(conn);
970 ZERO_STRUCT(req);
973 * pids[i].pid is uint64_t, scale down to pid_t which
974 * is the wire protocol towards ctdb.
976 pid = pids[i].pid;
978 DEBUG(10, ("Requesting PID %d/%d, reqid=%d\n",
979 (int)pids[i].vnn, (int)pid,
980 (int)reqids[i]));
982 req.hdr.length = offsetof(struct ctdb_req_control, data);
983 req.hdr.length += sizeof(pid);
984 req.hdr.ctdb_magic = CTDB_MAGIC;
985 req.hdr.ctdb_version = CTDB_VERSION;
986 req.hdr.operation = CTDB_REQ_CONTROL;
987 req.hdr.reqid = reqids[i];
988 req.hdr.destnode = pids[i].vnn;
989 req.opcode = CTDB_CONTROL_PROCESS_EXISTS;
990 req.srvid = 0;
991 req.datalen = sizeof(pid);
992 req.flags = 0;
994 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
995 ctdb_packet_dump(&req.hdr);
997 status = ctdb_packet_send(
998 conn->pkt, 2,
999 data_blob_const(
1000 &req, offsetof(struct ctdb_req_control, data)),
1001 data_blob_const(&pid, sizeof(pid)));
1002 if (!NT_STATUS_IS_OK(status)) {
1003 DEBUG(10, ("ctdb_packet_send failed: %s\n",
1004 nt_errstr(status)));
1005 goto fail;
1009 status = ctdb_packet_flush(conn->pkt);
1010 if (!NT_STATUS_IS_OK(status)) {
1011 DEBUG(10, ("ctdb_packet_flush failed: %s\n",
1012 nt_errstr(status)));
1013 goto fail;
1016 num_received = 0;
1018 while (num_received < num_pids) {
1019 struct ctdb_reply_control *reply = NULL;
1020 uint32_t reqid;
1022 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1023 if (!NT_STATUS_IS_OK(status)) {
1024 DEBUG(10, ("ctdb_read_req failed: %s\n",
1025 nt_errstr(status)));
1026 goto fail;
1029 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1030 DEBUG(10, ("Received invalid reply\n"));
1031 goto fail;
1034 reqid = reply->hdr.reqid;
1036 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1038 for (i=0; i<num_pids; i++) {
1039 if (reqid == reqids[i]) {
1040 break;
1043 if (i == num_pids) {
1044 DEBUG(10, ("Received unknown record number %u\n",
1045 (unsigned)reqid));
1046 goto fail;
1048 results[i] = ((reply->status) == 0);
1049 TALLOC_FREE(reply);
1050 num_received += 1;
1053 result = true;
1054 fail:
1055 TALLOC_FREE(frame);
1056 return result;
1059 struct ctdb_vnn_list {
1060 uint32_t vnn;
1061 uint32_t reqid;
1062 unsigned num_srvids;
1063 unsigned num_filled;
1064 uint64_t *srvids;
1065 unsigned *pid_indexes;
1069 * Get a list of all vnns mentioned in a list of
1070 * server_ids. vnn_indexes tells where in the vnns array we have to
1071 * place the pids.
1073 static bool ctdb_collect_vnns(TALLOC_CTX *mem_ctx,
1074 const struct server_id *pids, unsigned num_pids,
1075 struct ctdb_vnn_list **pvnns,
1076 unsigned *pnum_vnns)
1078 struct ctdb_vnn_list *vnns = NULL;
1079 unsigned *vnn_indexes = NULL;
1080 unsigned i, num_vnns = 0;
1082 vnn_indexes = talloc_array(mem_ctx, unsigned, num_pids);
1083 if (vnn_indexes == NULL) {
1084 DEBUG(1, ("talloc_array failed\n"));
1085 goto fail;
1088 for (i=0; i<num_pids; i++) {
1089 unsigned j;
1090 uint32_t vnn = pids[i].vnn;
1092 for (j=0; j<num_vnns; j++) {
1093 if (vnn == vnns[j].vnn) {
1094 break;
1097 vnn_indexes[i] = j;
1099 if (j < num_vnns) {
1101 * Already in the array
1103 vnns[j].num_srvids += 1;
1104 continue;
1106 vnns = talloc_realloc(mem_ctx, vnns, struct ctdb_vnn_list,
1107 num_vnns+1);
1108 if (vnns == NULL) {
1109 DEBUG(1, ("talloc_realloc failed\n"));
1110 goto fail;
1112 vnns[num_vnns].vnn = vnn;
1113 vnns[num_vnns].num_srvids = 1;
1114 vnns[num_vnns].num_filled = 0;
1115 num_vnns += 1;
1117 for (i=0; i<num_vnns; i++) {
1118 struct ctdb_vnn_list *vnn = &vnns[i];
1120 vnn->srvids = talloc_array(vnns, uint64_t, vnn->num_srvids);
1121 if (vnn->srvids == NULL) {
1122 DEBUG(1, ("talloc_array failed\n"));
1123 goto fail;
1125 vnn->pid_indexes = talloc_array(vnns, unsigned,
1126 vnn->num_srvids);
1127 if (vnn->pid_indexes == NULL) {
1128 DEBUG(1, ("talloc_array failed\n"));
1129 goto fail;
1132 for (i=0; i<num_pids; i++) {
1133 struct ctdb_vnn_list *vnn = &vnns[vnn_indexes[i]];
1134 vnn->srvids[vnn->num_filled] = pids[i].unique_id;
1135 vnn->pid_indexes[vnn->num_filled] = i;
1136 vnn->num_filled += 1;
1139 TALLOC_FREE(vnn_indexes);
1140 *pvnns = vnns;
1141 *pnum_vnns = num_vnns;
1142 return true;
1143 fail:
1144 TALLOC_FREE(vnns);
1145 TALLOC_FREE(vnn_indexes);
1146 return false;
1149 bool ctdb_serverids_exist_supported(struct ctdbd_connection *conn)
1151 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1152 return false;
1153 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1154 return true;
1155 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1158 bool ctdb_serverids_exist(struct ctdbd_connection *conn,
1159 const struct server_id *pids, unsigned num_pids,
1160 bool *results)
1162 #ifndef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
1163 return false;
1164 #else /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1165 unsigned i, num_received;
1166 NTSTATUS status;
1167 struct ctdb_vnn_list *vnns = NULL;
1168 unsigned num_vnns;
1169 bool result = false;
1171 if (!ctdb_collect_vnns(talloc_tos(), pids, num_pids,
1172 &vnns, &num_vnns)) {
1173 DEBUG(1, ("ctdb_collect_vnns failed\n"));
1174 goto fail;
1177 for (i=0; i<num_vnns; i++) {
1178 struct ctdb_vnn_list *vnn = &vnns[i];
1179 struct ctdb_req_control req;
1181 vnn->reqid = ctdbd_next_reqid(conn);
1183 ZERO_STRUCT(req);
1185 DEBUG(10, ("Requesting VNN %d, reqid=%d, num_srvids=%u\n",
1186 (int)vnn->vnn, (int)vnn->reqid, vnn->num_srvids));
1188 req.hdr.length = offsetof(struct ctdb_req_control, data);
1189 req.hdr.ctdb_magic = CTDB_MAGIC;
1190 req.hdr.ctdb_version = CTDB_VERSION;
1191 req.hdr.operation = CTDB_REQ_CONTROL;
1192 req.hdr.reqid = vnn->reqid;
1193 req.hdr.destnode = vnn->vnn;
1194 req.opcode = CTDB_CONTROL_CHECK_SRVIDS;
1195 req.srvid = 0;
1196 req.datalen = sizeof(uint64_t) * vnn->num_srvids;
1197 req.hdr.length += req.datalen;
1198 req.flags = 0;
1200 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
1201 ctdb_packet_dump(&req.hdr);
1203 status = ctdb_packet_send(
1204 conn->pkt, 2,
1205 data_blob_const(
1206 &req, offsetof(struct ctdb_req_control,
1207 data)),
1208 data_blob_const(vnn->srvids, req.datalen));
1209 if (!NT_STATUS_IS_OK(status)) {
1210 DEBUG(1, ("ctdb_packet_send failed: %s\n",
1211 nt_errstr(status)));
1212 goto fail;
1216 status = ctdb_packet_flush(conn->pkt);
1217 if (!NT_STATUS_IS_OK(status)) {
1218 DEBUG(1, ("ctdb_packet_flush failed: %s\n",
1219 nt_errstr(status)));
1220 goto fail;
1223 num_received = 0;
1225 while (num_received < num_vnns) {
1226 struct ctdb_reply_control *reply = NULL;
1227 struct ctdb_vnn_list *vnn;
1228 uint32_t reqid;
1229 uint8_t *reply_data;
1231 status = ctdb_read_req(conn, 0, talloc_tos(), (void *)&reply);
1232 if (!NT_STATUS_IS_OK(status)) {
1233 DEBUG(1, ("ctdb_read_req failed: %s\n",
1234 nt_errstr(status)));
1235 goto fail;
1238 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
1239 DEBUG(1, ("Received invalid reply %u\n",
1240 (unsigned)reply->hdr.operation));
1241 goto fail;
1244 reqid = reply->hdr.reqid;
1246 DEBUG(10, ("Received reqid %d\n", (int)reqid));
1248 for (i=0; i<num_vnns; i++) {
1249 if (reqid == vnns[i].reqid) {
1250 break;
1253 if (i == num_vnns) {
1254 DEBUG(1, ("Received unknown reqid number %u\n",
1255 (unsigned)reqid));
1256 goto fail;
1259 DEBUG(10, ("Found index %u\n", i));
1261 vnn = &vnns[i];
1263 DEBUG(10, ("Received vnn %u, vnn->num_srvids %u, datalen %u\n",
1264 (unsigned)vnn->vnn, vnn->num_srvids,
1265 (unsigned)reply->datalen));
1267 if (reply->datalen >= ((vnn->num_srvids+7)/8)) {
1269 * Got a real reply
1271 reply_data = reply->data;
1272 } else {
1274 * Got an error reply
1276 DEBUG(5, ("Received short reply len %d, status %u, "
1277 "errorlen %u\n",
1278 (unsigned)reply->datalen,
1279 (unsigned)reply->status,
1280 (unsigned)reply->errorlen));
1281 dump_data(5, reply->data, reply->errorlen);
1284 * This will trigger everything set to false
1286 reply_data = NULL;
1289 for (i=0; i<vnn->num_srvids; i++) {
1290 int idx = vnn->pid_indexes[i];
1292 if (pids[i].unique_id ==
1293 SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
1294 results[idx] = true;
1295 continue;
1297 results[idx] =
1298 (reply_data != NULL) &&
1299 ((reply_data[i/8] & (1<<(i%8))) != 0);
1302 TALLOC_FREE(reply);
1303 num_received += 1;
1306 result = true;
1307 fail:
1308 TALLOC_FREE(vnns);
1309 return result;
1310 #endif /* HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL */
1314 * Get a db path
1316 char *ctdbd_dbpath(struct ctdbd_connection *conn,
1317 TALLOC_CTX *mem_ctx, uint32_t db_id)
1319 NTSTATUS status;
1320 TDB_DATA data;
1321 int32_t cstatus;
1323 data.dptr = (uint8_t*)&db_id;
1324 data.dsize = sizeof(db_id);
1326 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1327 CTDB_CONTROL_GETDBPATH, 0, 0, data,
1328 mem_ctx, &data, &cstatus);
1329 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1330 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
1331 return NULL;
1334 return (char *)data.dptr;
1338 * attach to a ctdb database
1340 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
1341 const char *name, uint32_t *db_id, int tdb_flags)
1343 NTSTATUS status;
1344 TDB_DATA data;
1345 int32_t cstatus;
1346 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
1348 data = string_term_tdb_data(name);
1350 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1351 persistent
1352 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
1353 : CTDB_CONTROL_DB_ATTACH,
1354 tdb_flags, 0, data, NULL, &data, &cstatus);
1355 if (!NT_STATUS_IS_OK(status)) {
1356 DEBUG(0, (__location__ " ctdb_control for db_attach "
1357 "failed: %s\n", nt_errstr(status)));
1358 return status;
1361 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
1362 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
1363 return NT_STATUS_INTERNAL_ERROR;
1366 *db_id = *(uint32_t *)data.dptr;
1367 talloc_free(data.dptr);
1369 if (!(tdb_flags & TDB_SEQNUM)) {
1370 return NT_STATUS_OK;
1373 data.dptr = (uint8_t *)db_id;
1374 data.dsize = sizeof(*db_id);
1376 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1377 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
1378 NULL, NULL, &cstatus);
1379 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
1380 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
1381 "failed\n"));
1382 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
1383 status;
1386 return NT_STATUS_OK;
1390 * force the migration of a record to this node
1392 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32_t db_id,
1393 TDB_DATA key)
1395 struct ctdb_req_call req;
1396 struct ctdb_reply_call *reply;
1397 NTSTATUS status;
1399 ZERO_STRUCT(req);
1401 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1402 req.hdr.ctdb_magic = CTDB_MAGIC;
1403 req.hdr.ctdb_version = CTDB_VERSION;
1404 req.hdr.operation = CTDB_REQ_CALL;
1405 req.hdr.reqid = ctdbd_next_reqid(conn);
1406 req.flags = CTDB_IMMEDIATE_MIGRATION;
1407 req.callid = CTDB_NULL_FUNC;
1408 req.db_id = db_id;
1409 req.keylen = key.dsize;
1411 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1412 ctdb_packet_dump(&req.hdr);
1414 status = ctdb_packet_send(
1415 conn->pkt, 2,
1416 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1417 data_blob_const(key.dptr, key.dsize));
1419 if (!NT_STATUS_IS_OK(status)) {
1420 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1421 return status;
1424 status = ctdb_packet_flush(conn->pkt);
1426 if (!NT_STATUS_IS_OK(status)) {
1427 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1428 cluster_fatal("cluster dispatch daemon control write error\n");
1431 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1435 goto fail;
1438 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1439 DEBUG(0, ("received invalid reply\n"));
1440 status = NT_STATUS_INTERNAL_ERROR;
1441 goto fail;
1444 status = NT_STATUS_OK;
1445 fail:
1447 TALLOC_FREE(reply);
1448 return status;
1452 * Fetch a record and parse it
1454 NTSTATUS ctdbd_parse(struct ctdbd_connection *conn, uint32_t db_id,
1455 TDB_DATA key, bool local_copy,
1456 void (*parser)(TDB_DATA key, TDB_DATA data,
1457 void *private_data),
1458 void *private_data)
1460 struct ctdb_req_call req;
1461 struct ctdb_reply_call *reply;
1462 NTSTATUS status;
1463 uint32_t flags;
1465 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1466 flags = local_copy ? CTDB_WANT_READONLY : 0;
1467 #else
1468 flags = 0;
1469 #endif
1471 ZERO_STRUCT(req);
1473 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1474 req.hdr.ctdb_magic = CTDB_MAGIC;
1475 req.hdr.ctdb_version = CTDB_VERSION;
1476 req.hdr.operation = CTDB_REQ_CALL;
1477 req.hdr.reqid = ctdbd_next_reqid(conn);
1478 req.flags = flags;
1479 req.callid = CTDB_FETCH_FUNC;
1480 req.db_id = db_id;
1481 req.keylen = key.dsize;
1483 status = ctdb_packet_send(
1484 conn->pkt, 2,
1485 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1486 data_blob_const(key.dptr, key.dsize));
1488 if (!NT_STATUS_IS_OK(status)) {
1489 DEBUG(3, ("ctdb_packet_send failed: %s\n", nt_errstr(status)));
1490 return status;
1493 status = ctdb_packet_flush(conn->pkt);
1495 if (!NT_STATUS_IS_OK(status)) {
1496 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1497 cluster_fatal("cluster dispatch daemon control write error\n");
1500 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1504 goto fail;
1507 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1508 DEBUG(0, ("received invalid reply\n"));
1509 status = NT_STATUS_INTERNAL_ERROR;
1510 goto fail;
1513 if (reply->datalen == 0) {
1515 * Treat an empty record as non-existing
1517 status = NT_STATUS_NOT_FOUND;
1518 goto fail;
1521 parser(key, make_tdb_data(&reply->data[0], reply->datalen),
1522 private_data);
1524 status = NT_STATUS_OK;
1525 fail:
1526 TALLOC_FREE(reply);
1527 return status;
1530 struct ctdbd_traverse_state {
1531 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1532 void *private_data;
1536 * Handle a traverse record coming in on the ctdbd connection
1539 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1540 void *private_data)
1542 struct ctdbd_traverse_state *state =
1543 (struct ctdbd_traverse_state *)private_data;
1545 struct ctdb_req_message *m;
1546 struct ctdb_rec_data *d;
1547 TDB_DATA key, data;
1549 m = (struct ctdb_req_message *)buf;
1551 if (length < sizeof(*m) || m->hdr.length != length) {
1552 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1553 TALLOC_FREE(buf);
1554 return NT_STATUS_UNEXPECTED_IO_ERROR;
1557 d = (struct ctdb_rec_data *)&m->data[0];
1558 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1559 DEBUG(0, ("Got invalid traverse data of length %d\n",
1560 (int)m->datalen));
1561 TALLOC_FREE(buf);
1562 return NT_STATUS_UNEXPECTED_IO_ERROR;
1565 key.dsize = d->keylen;
1566 key.dptr = &d->data[0];
1567 data.dsize = d->datalen;
1568 data.dptr = &d->data[d->keylen];
1570 if (key.dsize == 0 && data.dsize == 0) {
1571 /* end of traverse */
1572 return NT_STATUS_END_OF_FILE;
1575 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1576 DEBUG(0, ("Got invalid ltdb header length %d\n",
1577 (int)data.dsize));
1578 TALLOC_FREE(buf);
1579 return NT_STATUS_UNEXPECTED_IO_ERROR;
1581 data.dsize -= sizeof(struct ctdb_ltdb_header);
1582 data.dptr += sizeof(struct ctdb_ltdb_header);
1584 if (state->fn) {
1585 state->fn(key, data, state->private_data);
1588 TALLOC_FREE(buf);
1589 return NT_STATUS_OK;
1593 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1594 connection to ctdbd to avoid the hairy recursive and async problems with
1595 everything in-line.
1598 NTSTATUS ctdbd_traverse(uint32_t db_id,
1599 void (*fn)(TDB_DATA key, TDB_DATA data,
1600 void *private_data),
1601 void *private_data)
1603 struct ctdbd_connection *conn;
1604 NTSTATUS status;
1606 TDB_DATA data;
1607 struct ctdb_traverse_start t;
1608 int cstatus;
1609 struct ctdbd_traverse_state state;
1611 become_root();
1612 status = ctdbd_init_connection(NULL, &conn);
1613 unbecome_root();
1614 if (!NT_STATUS_IS_OK(status)) {
1615 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1616 nt_errstr(status)));
1617 return status;
1620 t.db_id = db_id;
1621 t.srvid = conn->rand_srvid;
1622 t.reqid = ctdbd_next_reqid(conn);
1624 data.dptr = (uint8_t *)&t;
1625 data.dsize = sizeof(t);
1627 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1628 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1629 data, NULL, NULL, &cstatus);
1631 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1633 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1634 cstatus));
1636 if (NT_STATUS_IS_OK(status)) {
1638 * We need a mapping here
1640 status = NT_STATUS_UNSUCCESSFUL;
1642 goto done;
1645 state.fn = fn;
1646 state.private_data = private_data;
1648 while (True) {
1650 status = NT_STATUS_OK;
1652 if (ctdb_packet_handler(conn->pkt, ctdb_req_complete,
1653 ctdb_traverse_handler, &state, &status)) {
1655 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1656 status = NT_STATUS_OK;
1657 break;
1661 * There might be more in the queue
1663 continue;
1666 if (!NT_STATUS_IS_OK(status)) {
1667 break;
1670 status = ctdb_packet_fd_read_sync(conn->pkt);
1672 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1674 * There might be more in the queue
1676 continue;
1679 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1680 status = NT_STATUS_OK;
1681 break;
1684 if (!NT_STATUS_IS_OK(status)) {
1685 DEBUG(0, ("ctdb_packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1686 cluster_fatal("ctdbd died\n");
1690 done:
1691 TALLOC_FREE(conn);
1692 return status;
1696 This is used to canonicalize a ctdb_sock_addr structure.
1698 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1699 struct sockaddr_storage *out)
1701 memcpy(out, in, sizeof (*out));
1703 #ifdef HAVE_IPV6
1704 if (in->ss_family == AF_INET6) {
1705 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1706 const struct sockaddr_in6 *in6 =
1707 (const struct sockaddr_in6 *)in;
1708 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1709 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1710 memset(out, 0, sizeof(*out));
1711 #ifdef HAVE_SOCK_SIN_LEN
1712 out4->sin_len = sizeof(*out);
1713 #endif
1714 out4->sin_family = AF_INET;
1715 out4->sin_port = in6->sin6_port;
1716 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr[12], 4);
1719 #endif
1723 * Register us as a server for a particular tcp connection
1726 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1727 const struct sockaddr_storage *_server,
1728 const struct sockaddr_storage *_client,
1729 bool (*release_ip_handler)(const char *ip_addr,
1730 void *private_data),
1731 void *private_data)
1734 * we still use ctdb_control_tcp for ipv4
1735 * because we want to work against older ctdb
1736 * versions at runtime
1738 struct ctdb_control_tcp p4;
1739 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1740 struct ctdb_control_tcp_addr p;
1741 #endif
1742 TDB_DATA data;
1743 NTSTATUS status;
1744 struct sockaddr_storage client;
1745 struct sockaddr_storage server;
1748 * Only one connection so far
1750 SMB_ASSERT(conn->release_ip_handler == NULL);
1752 smbd_ctdb_canonicalize_ip(_client, &client);
1753 smbd_ctdb_canonicalize_ip(_server, &server);
1755 switch (client.ss_family) {
1756 case AF_INET:
1757 memcpy(&p4.dest, &server, sizeof(p4.dest));
1758 memcpy(&p4.src, &client, sizeof(p4.src));
1759 data.dptr = (uint8_t *)&p4;
1760 data.dsize = sizeof(p4);
1761 break;
1762 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1763 case AF_INET6:
1764 memcpy(&p.dest.ip6, &server, sizeof(p.dest.ip6));
1765 memcpy(&p.src.ip6, &client, sizeof(p.src.ip6));
1766 data.dptr = (uint8_t *)&p;
1767 data.dsize = sizeof(p);
1768 break;
1769 #endif
1770 default:
1771 return NT_STATUS_INTERNAL_ERROR;
1774 conn->release_ip_handler = release_ip_handler;
1775 conn->release_ip_priv = private_data;
1778 * We want to be told about IP releases
1781 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1782 if (!NT_STATUS_IS_OK(status)) {
1783 return status;
1787 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1788 * can send an extra ack to trigger a reset for our client, so it
1789 * immediately reconnects
1791 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1792 CTDB_CONTROL_TCP_CLIENT, 0,
1793 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1797 * We want to handle reconfigure events
1799 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1801 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1805 call a control on the local node
1807 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32_t opcode,
1808 uint64_t srvid, uint32_t flags, TDB_DATA data,
1809 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1810 int *cstatus)
1812 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1815 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1817 struct ctdb_client_notify_register reg_data;
1818 size_t struct_len;
1819 NTSTATUS status;
1820 int cstatus;
1822 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1823 reg_data.len = 1;
1824 reg_data.notify_data[0] = 0;
1826 struct_len = offsetof(struct ctdb_client_notify_register,
1827 notify_data) + reg_data.len;
1829 status = ctdbd_control_local(
1830 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1831 make_tdb_data((uint8_t *)&reg_data, struct_len),
1832 NULL, NULL, &cstatus);
1833 if (!NT_STATUS_IS_OK(status)) {
1834 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1835 nt_errstr(status)));
1837 return status;
1840 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1842 struct ctdb_client_notify_deregister dereg_data;
1843 NTSTATUS status;
1844 int cstatus;
1846 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1848 status = ctdbd_control_local(
1849 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1850 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1851 NULL, NULL, &cstatus);
1852 if (!NT_STATUS_IS_OK(status)) {
1853 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1854 nt_errstr(status)));
1856 return status;
1859 NTSTATUS ctdbd_probe(void)
1862 * Do a very early check if ctdbd is around to avoid an abort and core
1863 * later
1865 struct ctdbd_connection *conn = NULL;
1866 NTSTATUS status;
1868 status = ctdbd_messaging_connection(talloc_tos(), &conn);
1871 * We only care if we can connect.
1873 TALLOC_FREE(conn);
1875 return status;