Do not crash in ctdbd_traverse if ctdbd is not around
[Samba/bb.git] / source / lib / ctdbd_conn.c
blob465c09a32dbbd7d51a5b794fc29081f69852e0c4
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"
23 #ifdef CLUSTER_SUPPORT
25 #include "librpc/gen_ndr/messaging.h"
26 #include "librpc/gen_ndr/ndr_messaging.h"
28 /* paths to these include files come from --with-ctdb= in configure */
29 #include "ctdb.h"
30 #include "ctdb_private.h"
32 struct ctdbd_connection {
33 struct messaging_context *msg_ctx;
34 uint32 reqid;
35 uint32 our_vnn;
36 uint64 rand_srvid;
37 struct packet_context *pkt;
38 struct fd_event *fde;
40 void (*release_ip_handler)(const char *ip_addr, void *private_data);
41 void *release_ip_priv;
44 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
45 uint32_t vnn, uint32 opcode,
46 uint64_t srvid, uint32_t flags, TDB_DATA data,
47 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
48 int *cstatus);
51 * exit on fatal communications errors with the ctdbd daemon
53 static void cluster_fatal(const char *why)
55 DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
56 /* we don't use smb_panic() as we don't want to delay to write
57 a core file. We need to release this process id immediately
58 so that someone else can take over without getting sharing
59 violations */
60 _exit(0);
66 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
68 if (DEBUGLEVEL < 10) {
69 return;
71 DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
72 (int)hdr->length, (int)hdr->ctdb_magic,
73 (int)hdr->ctdb_version, (int)hdr->generation,
74 (int)hdr->operation, (int)hdr->reqid));
78 * Register a srvid with ctdbd
80 static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
81 uint64_t srvid)
84 int cstatus;
85 return ctdbd_control(conn, CTDB_CURRENT_NODE,
86 CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
87 tdb_null, NULL, NULL, &cstatus);
91 * get our vnn from the cluster
93 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
95 int32_t cstatus=-1;
96 NTSTATUS status;
97 status = ctdbd_control(conn,
98 CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
99 tdb_null, NULL, NULL, &cstatus);
100 if (!NT_STATUS_IS_OK(status)) {
101 cluster_fatal("ctdbd_control failed\n");
103 *vnn = (uint32_t)cstatus;
104 return status;
107 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
109 return conn->our_vnn;
113 * Get us a ctdb connection
116 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
117 struct packet_context **presult)
119 struct packet_context *result;
120 const char *sockname = lp_ctdbd_socket();
121 struct sockaddr_un addr;
122 int fd;
124 if (!sockname || !*sockname) {
125 sockname = CTDB_PATH;
128 fd = socket(AF_UNIX, SOCK_STREAM, 0);
129 if (fd == -1) {
130 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
131 return map_nt_error_from_unix(errno);
134 ZERO_STRUCT(addr);
135 addr.sun_family = AF_UNIX;
136 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
138 if (sys_connect(fd, (struct sockaddr *)&addr) == -1) {
139 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
140 strerror(errno)));
141 close(fd);
142 return map_nt_error_from_unix(errno);
145 if (!(result = packet_init(mem_ctx, fd))) {
146 close(fd);
147 return NT_STATUS_NO_MEMORY;
150 *presult = result;
151 return NT_STATUS_OK;
155 * Do we have a complete ctdb packet in the queue?
158 static bool ctdb_req_complete(const struct data_blob *data,
159 size_t *length,
160 void *private_data)
162 uint32 msglen;
164 if (data->length < sizeof(msglen)) {
165 return False;
168 msglen = *((uint32 *)data->data);
170 DEBUG(10, ("msglen = %d\n", msglen));
172 if (msglen < sizeof(struct ctdb_req_header)) {
173 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
174 "the req_header\n", (int)msglen,
175 (int)sizeof(struct ctdb_req_header)));
176 cluster_fatal("ctdbd protocol error\n");
179 if (data->length >= msglen) {
180 *length = msglen;
181 return True;
184 return False;
188 * State necessary to defer an incoming message while we are waiting for a
189 * ctdb reply.
192 struct deferred_msg_state {
193 struct messaging_context *msg_ctx;
194 struct messaging_rec *rec;
198 * Timed event handler for the deferred message
201 static void deferred_message_dispatch(struct event_context *event_ctx,
202 struct timed_event *te,
203 struct timeval now,
204 void *private_data)
206 struct deferred_msg_state *state = talloc_get_type_abort(
207 private_data, struct deferred_msg_state);
209 messaging_dispatch_rec(state->msg_ctx, state->rec);
210 TALLOC_FREE(state);
211 TALLOC_FREE(te);
214 struct req_pull_state {
215 TALLOC_CTX *mem_ctx;
216 DATA_BLOB req;
220 * Pull a ctdb request out of the incoming packet queue
223 static NTSTATUS ctdb_req_pull(const struct data_blob *data,
224 void *private_data)
226 struct req_pull_state *state = (struct req_pull_state *)private_data;
228 state->req = data_blob_talloc(state->mem_ctx, data->data,
229 data->length);
230 if (state->req.data == NULL) {
231 return NT_STATUS_NO_MEMORY;
233 return NT_STATUS_OK;
237 * Fetch a messaging_rec from an incoming ctdb style message
240 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
241 size_t overall_length,
242 struct ctdb_req_message *msg)
244 struct messaging_rec *result;
245 DATA_BLOB blob;
246 enum ndr_err_code ndr_err;
248 if ((overall_length < offsetof(struct ctdb_req_message, data))
249 || (overall_length
250 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
252 cluster_fatal("got invalid msg length");
255 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
256 DEBUG(0, ("talloc failed\n"));
257 return NULL;
260 blob = data_blob_const(msg->data, msg->datalen);
262 ndr_err = ndr_pull_struct_blob(
263 &blob, result, result,
264 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
266 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
267 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
268 ndr_errstr(ndr_err)));
269 TALLOC_FREE(result);
270 return NULL;
273 if (DEBUGLEVEL >= 10) {
274 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
275 NDR_PRINT_DEBUG(messaging_rec, result);
278 return result;
282 * Read a full ctdbd request. If we have a messaging context, defer incoming
283 * messages that might come in between.
286 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
287 TALLOC_CTX *mem_ctx, void *result)
289 struct ctdb_req_header *hdr;
290 struct req_pull_state state;
291 NTSTATUS status;
293 again:
295 status = packet_fd_read_sync(conn->pkt);
297 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
298 /* EAGAIN */
299 goto again;
300 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
301 /* EAGAIN */
302 goto again;
305 if (!NT_STATUS_IS_OK(status)) {
306 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
307 cluster_fatal("ctdbd died\n");
310 next_pkt:
312 ZERO_STRUCT(state);
313 state.mem_ctx = mem_ctx;
315 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
316 &state, &status)) {
318 * Not enough data
320 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
321 goto again;
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
326 cluster_fatal("ctdbd died\n");
329 hdr = (struct ctdb_req_header *)state.req.data;
331 DEBUG(10, ("Received ctdb packet\n"));
332 ctdb_packet_dump(hdr);
334 if (hdr->operation == CTDB_REQ_MESSAGE) {
335 struct timed_event *evt;
336 struct deferred_msg_state *msg_state;
337 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
339 if (conn->msg_ctx == NULL) {
340 DEBUG(1, ("Got a message without having a msg ctx, "
341 "dropping msg %llu\n",
342 (long long unsigned)msg->srvid));
343 goto next_pkt;
346 if ((conn->release_ip_handler != NULL)
347 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
348 /* must be dispatched immediately */
349 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
350 conn->release_ip_handler((const char *)msg->data,
351 conn->release_ip_priv);
352 TALLOC_FREE(hdr);
353 goto next_pkt;
356 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
357 DEBUG(0,("Got cluster reconfigure message in ctdb_read_req\n"));
358 messaging_send(conn->msg_ctx, procid_self(),
359 MSG_SMB_BRL_VALIDATE, &data_blob_null);
360 TALLOC_FREE(hdr);
361 goto next_pkt;
364 if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
365 DEBUG(0, ("talloc failed\n"));
366 TALLOC_FREE(hdr);
367 goto next_pkt;
370 if (!(msg_state->rec = ctdb_pull_messaging_rec(
371 msg_state, state.req.length, msg))) {
372 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
373 TALLOC_FREE(msg_state);
374 TALLOC_FREE(hdr);
375 goto next_pkt;
378 TALLOC_FREE(hdr);
380 msg_state->msg_ctx = conn->msg_ctx;
383 * We're waiting for a call reply, but an async message has
384 * crossed. Defer dispatching to the toplevel event loop.
386 evt = event_add_timed(conn->msg_ctx->event_ctx,
387 conn->msg_ctx->event_ctx,
388 timeval_zero(),
389 deferred_message_dispatch,
390 msg_state);
391 if (evt == NULL) {
392 DEBUG(0, ("event_add_timed failed\n"));
393 TALLOC_FREE(msg_state);
394 TALLOC_FREE(hdr);
395 goto next_pkt;
398 goto next_pkt;
401 if (hdr->reqid != reqid) {
402 /* we got the wrong reply */
403 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
404 "been %u\n", hdr->reqid, reqid));
405 TALLOC_FREE(hdr);
406 goto again;
409 *((void **)result) = talloc_move(mem_ctx, &hdr);
411 return NT_STATUS_OK;
415 * Get us a ctdbd connection
418 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
419 struct ctdbd_connection **pconn)
421 struct ctdbd_connection *conn;
422 NTSTATUS status;
424 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
425 DEBUG(0, ("talloc failed\n"));
426 return NT_STATUS_NO_MEMORY;
429 status = ctdbd_connect(conn, &conn->pkt);
431 if (!NT_STATUS_IS_OK(status)) {
432 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
433 goto fail;
436 status = get_cluster_vnn(conn, &conn->our_vnn);
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
440 goto fail;
443 generate_random_buffer((unsigned char *)&conn->rand_srvid,
444 sizeof(conn->rand_srvid));
446 status = register_with_ctdbd(conn, conn->rand_srvid);
448 if (!NT_STATUS_IS_OK(status)) {
449 DEBUG(5, ("Could not register random srvid: %s\n",
450 nt_errstr(status)));
451 goto fail;
454 *pconn = conn;
455 return NT_STATUS_OK;
457 fail:
458 TALLOC_FREE(conn);
459 return status;
463 * Get us a ctdbd connection and register us as a process
466 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
467 struct ctdbd_connection **pconn)
469 struct ctdbd_connection *conn;
470 NTSTATUS status;
472 status = ctdbd_init_connection(mem_ctx, &conn);
474 if (!NT_STATUS_IS_OK(status)) {
475 return status;
478 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
479 if (!NT_STATUS_IS_OK(status)) {
480 goto fail;
483 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
484 if (!NT_STATUS_IS_OK(status)) {
485 goto fail;
488 *pconn = conn;
489 return NT_STATUS_OK;
491 fail:
492 TALLOC_FREE(conn);
493 return status;
497 * Packet handler to receive and handle a ctdb message
499 static NTSTATUS ctdb_handle_message(const struct data_blob *data,
500 void *private_data)
502 struct ctdbd_connection *conn = talloc_get_type_abort(
503 private_data, struct ctdbd_connection);
504 struct ctdb_req_message *msg;
505 struct messaging_rec *msg_rec;
507 msg = (struct ctdb_req_message *)data->data;
509 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
510 DEBUG(0, ("Received async msg of type %u, discarding\n",
511 msg->hdr.operation));
512 return NT_STATUS_INVALID_PARAMETER;
515 if ((conn->release_ip_handler != NULL)
516 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
517 /* must be dispatched immediately */
518 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
519 conn->release_ip_handler((const char *)msg->data,
520 conn->release_ip_priv);
521 return NT_STATUS_OK;
524 SMB_ASSERT(conn->msg_ctx != NULL);
526 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
527 DEBUG(0,("Got cluster reconfigure message\n"));
529 * when the cluster is reconfigured, we need to clean the brl
530 * database
532 messaging_send(conn->msg_ctx, procid_self(),
533 MSG_SMB_BRL_VALIDATE, &data_blob_null);
536 * it's possible that we have just rejoined the cluster after
537 * an outage. In that case our pending locks could have been
538 * removed from the lockdb, so retry them once more
540 message_send_all(conn->msg_ctx, MSG_SMB_UNLOCK, NULL, 0, NULL);
542 return NT_STATUS_OK;
546 /* only messages to our pid or the broadcast are valid here */
547 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
548 DEBUG(0,("Got unexpected message with srvid=%llu\n",
549 (unsigned long long)msg->srvid));
550 return NT_STATUS_OK;
553 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, data->length, msg))) {
554 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
555 return NT_STATUS_NO_MEMORY;
558 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
560 TALLOC_FREE(msg_rec);
561 return NT_STATUS_OK;
565 * The ctdbd socket is readable asynchronuously
568 static void ctdbd_socket_handler(struct event_context *event_ctx,
569 struct fd_event *event,
570 uint16 flags,
571 void *private_data)
573 struct ctdbd_connection *conn = talloc_get_type_abort(
574 private_data, struct ctdbd_connection);
576 NTSTATUS status;
578 status = packet_fd_read(conn->pkt);
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
582 cluster_fatal("ctdbd died\n");
585 while (packet_handler(conn->pkt, ctdb_req_complete,
586 ctdb_handle_message, conn, &status)) {
587 if (!NT_STATUS_IS_OK(status)) {
588 DEBUG(10, ("could not handle incoming message: %s\n",
589 nt_errstr(status)));
595 * Prepare a ctdbd connection to receive messages
598 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
599 struct messaging_context *msg_ctx)
601 SMB_ASSERT(conn->msg_ctx == NULL);
602 SMB_ASSERT(conn->fde == NULL);
604 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
605 packet_get_fd(conn->pkt),
606 EVENT_FD_READ,
607 ctdbd_socket_handler,
608 conn))) {
609 DEBUG(0, ("event_add_fd failed\n"));
610 return NT_STATUS_NO_MEMORY;
613 conn->msg_ctx = msg_ctx;
615 return NT_STATUS_OK;
619 * Send a messaging message across a ctdbd
622 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
623 uint32 dst_vnn, uint64 dst_srvid,
624 struct messaging_rec *msg)
626 struct ctdb_req_message r;
627 TALLOC_CTX *mem_ctx;
628 DATA_BLOB blob;
629 NTSTATUS status;
630 enum ndr_err_code ndr_err;
632 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
633 DEBUG(0, ("talloc failed\n"));
634 return NT_STATUS_NO_MEMORY;
637 ndr_err = ndr_push_struct_blob(
638 &blob, mem_ctx, msg,
639 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
641 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
642 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
643 ndr_errstr(ndr_err)));
644 status = ndr_map_error2ntstatus(ndr_err);
645 goto fail;
648 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
649 r.hdr.ctdb_magic = CTDB_MAGIC;
650 r.hdr.ctdb_version = CTDB_VERSION;
651 r.hdr.generation = 1;
652 r.hdr.operation = CTDB_REQ_MESSAGE;
653 r.hdr.destnode = dst_vnn;
654 r.hdr.srcnode = conn->our_vnn;
655 r.hdr.reqid = 0;
656 r.srvid = dst_srvid;
657 r.datalen = blob.length;
659 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
660 ctdb_packet_dump(&r.hdr);
662 status = packet_send(
663 conn->pkt, 2,
664 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
665 blob);
667 if (!NT_STATUS_IS_OK(status)) {
668 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
669 goto fail;
672 status = packet_flush(conn->pkt);
674 if (!NT_STATUS_IS_OK(status)) {
675 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
676 cluster_fatal("cluster dispatch daemon msg write error\n");
679 status = NT_STATUS_OK;
680 fail:
681 TALLOC_FREE(mem_ctx);
682 return status;
686 * send/recv a generic ctdb control message
688 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
689 uint32_t vnn, uint32 opcode,
690 uint64_t srvid, uint32_t flags,
691 TDB_DATA data,
692 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
693 int *cstatus)
695 struct ctdb_req_control req;
696 struct ctdb_reply_control *reply = NULL;
697 struct ctdbd_connection *new_conn = NULL;
698 NTSTATUS status;
700 /* the samba3 ctdb code can't handle NOREPLY yet */
701 flags &= ~CTDB_CTRL_FLAG_NOREPLY;
703 if (conn == NULL) {
704 status = ctdbd_init_connection(NULL, &new_conn);
706 if (!NT_STATUS_IS_OK(status)) {
707 DEBUG(10, ("Could not init temp connection: %s\n",
708 nt_errstr(status)));
709 goto fail;
712 conn = new_conn;
715 ZERO_STRUCT(req);
716 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
717 req.hdr.ctdb_magic = CTDB_MAGIC;
718 req.hdr.ctdb_version = CTDB_VERSION;
719 req.hdr.operation = CTDB_REQ_CONTROL;
720 req.hdr.reqid = ++conn->reqid;
721 req.hdr.destnode = vnn;
722 req.opcode = opcode;
723 req.srvid = srvid;
724 req.datalen = data.dsize;
726 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
727 ctdb_packet_dump(&req.hdr);
729 status = packet_send(
730 conn->pkt, 2,
731 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
732 data_blob_const(data.dptr, data.dsize));
734 if (!NT_STATUS_IS_OK(status)) {
735 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
736 goto fail;
739 status = packet_flush(conn->pkt);
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
743 cluster_fatal("cluster dispatch daemon control write error\n");
746 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
747 TALLOC_FREE(new_conn);
748 return NT_STATUS_OK;
751 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
753 if (!NT_STATUS_IS_OK(status)) {
754 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
755 goto fail;
758 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
759 DEBUG(0, ("received invalid reply\n"));
760 goto fail;
763 if (outdata) {
764 if (!(outdata->dptr = (uint8 *)talloc_memdup(
765 mem_ctx, reply->data, reply->datalen))) {
766 TALLOC_FREE(reply);
767 return NT_STATUS_NO_MEMORY;
769 outdata->dsize = reply->datalen;
771 if (cstatus) {
772 (*cstatus) = reply->status;
775 status = NT_STATUS_OK;
777 fail:
778 TALLOC_FREE(new_conn);
779 TALLOC_FREE(reply);
780 return status;
784 * see if a remote process exists
786 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
788 NTSTATUS status;
789 TDB_DATA data;
790 int32_t cstatus;
792 data.dptr = (uint8_t*)&pid;
793 data.dsize = sizeof(pid);
795 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
796 data, NULL, NULL, &cstatus);
797 if (!NT_STATUS_IS_OK(status)) {
798 DEBUG(0, (__location__ " ctdb_control for process_exists "
799 "failed\n"));
800 return False;
803 return cstatus == 0;
807 * Get a db path
809 char *ctdbd_dbpath(struct ctdbd_connection *conn,
810 TALLOC_CTX *mem_ctx, uint32_t db_id)
812 NTSTATUS status;
813 TDB_DATA data;
814 int32_t cstatus;
816 data.dptr = (uint8_t*)&db_id;
817 data.dsize = sizeof(db_id);
819 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
820 CTDB_CONTROL_GETDBPATH, 0, 0, data,
821 mem_ctx, &data, &cstatus);
822 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
823 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
824 return NULL;
827 return (char *)data.dptr;
831 * attach to a ctdb database
833 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
834 const char *name, uint32_t *db_id, int tdb_flags)
836 NTSTATUS status;
837 TDB_DATA data;
838 int32_t cstatus;
839 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
841 data.dptr = (uint8_t*)name;
842 data.dsize = strlen(name)+1;
844 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
845 persistent
846 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
847 : CTDB_CONTROL_DB_ATTACH,
848 0, 0, data, NULL, &data, &cstatus);
849 if (!NT_STATUS_IS_OK(status)) {
850 DEBUG(0, (__location__ " ctdb_control for db_attach "
851 "failed: %s\n", nt_errstr(status)));
852 return status;
855 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
856 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
857 return NT_STATUS_INTERNAL_ERROR;
860 *db_id = *(uint32_t *)data.dptr;
861 talloc_free(data.dptr);
863 if (!(tdb_flags & TDB_SEQNUM)) {
864 return NT_STATUS_OK;
867 data.dptr = (uint8_t *)db_id;
868 data.dsize = sizeof(*db_id);
870 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
871 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
872 NULL, NULL, &cstatus);
873 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
874 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
875 "failed\n"));
876 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
877 status;
880 return NT_STATUS_OK;
884 * force the migration of a record to this node
886 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
887 TDB_DATA key)
889 struct ctdb_req_call req;
890 struct ctdb_reply_call *reply;
891 NTSTATUS status;
893 ZERO_STRUCT(req);
895 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
896 req.hdr.ctdb_magic = CTDB_MAGIC;
897 req.hdr.ctdb_version = CTDB_VERSION;
898 req.hdr.operation = CTDB_REQ_CALL;
899 req.hdr.reqid = ++conn->reqid;
900 req.flags = CTDB_IMMEDIATE_MIGRATION;
901 req.callid = CTDB_NULL_FUNC;
902 req.db_id = db_id;
903 req.keylen = key.dsize;
905 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
906 ctdb_packet_dump(&req.hdr);
908 status = packet_send(
909 conn->pkt, 2,
910 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
911 data_blob_const(key.dptr, key.dsize));
913 if (!NT_STATUS_IS_OK(status)) {
914 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
915 return status;
918 status = packet_flush(conn->pkt);
920 if (!NT_STATUS_IS_OK(status)) {
921 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
922 cluster_fatal("cluster dispatch daemon control write error\n");
925 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
927 if (!NT_STATUS_IS_OK(status)) {
928 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
929 goto fail;
932 if (reply->hdr.operation != CTDB_REPLY_CALL) {
933 DEBUG(0, ("received invalid reply\n"));
934 status = NT_STATUS_INTERNAL_ERROR;
935 goto fail;
938 status = NT_STATUS_OK;
939 fail:
941 TALLOC_FREE(reply);
942 return status;
946 * remotely fetch a record without locking it or forcing a migration
948 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
949 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
951 struct ctdb_req_call req;
952 struct ctdb_reply_call *reply;
953 NTSTATUS status;
955 ZERO_STRUCT(req);
957 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
958 req.hdr.ctdb_magic = CTDB_MAGIC;
959 req.hdr.ctdb_version = CTDB_VERSION;
960 req.hdr.operation = CTDB_REQ_CALL;
961 req.hdr.reqid = ++conn->reqid;
962 req.flags = 0;
963 req.callid = CTDB_FETCH_FUNC;
964 req.db_id = db_id;
965 req.keylen = key.dsize;
967 status = packet_send(
968 conn->pkt, 2,
969 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
970 data_blob_const(key.dptr, key.dsize));
972 if (!NT_STATUS_IS_OK(status)) {
973 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
974 return status;
977 status = packet_flush(conn->pkt);
979 if (!NT_STATUS_IS_OK(status)) {
980 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
981 cluster_fatal("cluster dispatch daemon control write error\n");
984 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
986 if (!NT_STATUS_IS_OK(status)) {
987 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
988 goto fail;
991 if (reply->hdr.operation != CTDB_REPLY_CALL) {
992 DEBUG(0, ("received invalid reply\n"));
993 status = NT_STATUS_INTERNAL_ERROR;
994 goto fail;
997 data->dsize = reply->datalen;
998 if (data->dsize == 0) {
999 data->dptr = NULL;
1000 goto done;
1003 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1004 reply->datalen);
1005 if (data->dptr == NULL) {
1006 DEBUG(0, ("talloc failed\n"));
1007 status = NT_STATUS_NO_MEMORY;
1008 goto fail;
1011 done:
1012 status = NT_STATUS_OK;
1013 fail:
1014 TALLOC_FREE(reply);
1015 return status;
1018 struct ctdbd_traverse_state {
1019 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1020 void *private_data;
1024 * Handle a traverse record coming in on the ctdbd connection
1027 static NTSTATUS ctdb_traverse_handler(const struct data_blob *blob,
1028 void *private_data)
1030 struct ctdbd_traverse_state *state =
1031 (struct ctdbd_traverse_state *)private_data;
1033 struct ctdb_req_message *m;
1034 struct ctdb_rec_data *d;
1035 TDB_DATA key, data;
1037 m = (struct ctdb_req_message *)blob->data;
1039 if (blob->length < sizeof(*m) || m->hdr.length != blob->length) {
1040 DEBUG(0, ("Got invalid message of length %d\n",
1041 (int)blob->length));
1042 return NT_STATUS_UNEXPECTED_IO_ERROR;
1045 d = (struct ctdb_rec_data *)&m->data[0];
1046 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1047 DEBUG(0, ("Got invalid traverse data of length %d\n",
1048 (int)m->datalen));
1049 return NT_STATUS_UNEXPECTED_IO_ERROR;
1052 key.dsize = d->keylen;
1053 key.dptr = &d->data[0];
1054 data.dsize = d->datalen;
1055 data.dptr = &d->data[d->keylen];
1057 if (key.dsize == 0 && data.dsize == 0) {
1058 /* end of traverse */
1059 return NT_STATUS_END_OF_FILE;
1062 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1063 DEBUG(0, ("Got invalid ltdb header length %d\n",
1064 (int)data.dsize));
1065 return NT_STATUS_UNEXPECTED_IO_ERROR;
1067 data.dsize -= sizeof(struct ctdb_ltdb_header);
1068 data.dptr += sizeof(struct ctdb_ltdb_header);
1070 if (state->fn) {
1071 state->fn(key, data, state->private_data);
1074 return NT_STATUS_OK;
1078 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1079 connection to ctdbd to avoid the hairy recursive and async problems with
1080 everything in-line.
1083 NTSTATUS ctdbd_traverse(uint32 db_id,
1084 void (*fn)(TDB_DATA key, TDB_DATA data,
1085 void *private_data),
1086 void *private_data)
1088 struct ctdbd_connection *conn;
1089 NTSTATUS status;
1091 TDB_DATA data;
1092 struct ctdb_traverse_start t;
1093 int cstatus;
1094 struct ctdbd_traverse_state state;
1096 status = ctdbd_init_connection(NULL, &conn);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1099 nt_errstr(status)));
1100 return status;
1103 t.db_id = db_id;
1104 t.srvid = conn->rand_srvid;
1105 t.reqid = ++conn->reqid;
1107 data.dptr = (uint8_t *)&t;
1108 data.dsize = sizeof(t);
1110 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1111 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1112 data, NULL, NULL, &cstatus);
1114 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1116 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1117 cstatus));
1119 if (NT_STATUS_IS_OK(status)) {
1121 * We need a mapping here
1123 status = NT_STATUS_UNSUCCESSFUL;
1125 goto done;
1128 state.fn = fn;
1129 state.private_data = private_data;
1131 while (True) {
1133 status = NT_STATUS_OK;
1135 if (packet_handler(conn->pkt, ctdb_req_complete,
1136 ctdb_traverse_handler, &state, &status)) {
1138 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1139 status = NT_STATUS_OK;
1140 break;
1144 * There might be more in the queue
1146 continue;
1149 if (!NT_STATUS_IS_OK(status)) {
1150 break;
1153 status = packet_fd_read_sync(conn->pkt);
1155 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1157 * There might be more in the queue
1159 continue;
1162 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1163 status = NT_STATUS_OK;
1166 if (!NT_STATUS_IS_OK(status)) {
1167 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1168 cluster_fatal("ctdbd died\n");
1172 done:
1173 TALLOC_FREE(conn);
1174 return status;
1178 This is used to canonicalize a ctdb_sock_addr structure.
1180 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1181 struct sockaddr_storage *out)
1183 memcpy(out, in, sizeof (*out));
1185 #ifdef HAVE_IPV6
1186 if (in->ss_family == AF_INET6) {
1187 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1188 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1189 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1190 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1191 memset(out, 0, sizeof(*out));
1192 #ifdef HAVE_SOCK_SIN_LEN
1193 out4->sin_len = sizeof(*out);
1194 #endif
1195 out4->sin_family = AF_INET;
1196 out4->sin_port = in6->sin6_port;
1197 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1200 #endif
1204 * Register us as a server for a particular tcp connection
1207 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1208 const struct sockaddr_storage *_server,
1209 const struct sockaddr_storage *_client,
1210 void (*release_ip_handler)(const char *ip_addr,
1211 void *private_data),
1212 void *private_data)
1215 * we still use ctdb_control_tcp for ipv4
1216 * because we want to work against older ctdb
1217 * versions at runtime
1219 struct ctdb_control_tcp p4;
1220 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1221 struct ctdb_control_tcp_addr p;
1222 #endif
1223 TDB_DATA data;
1224 NTSTATUS status;
1225 struct sockaddr_storage client;
1226 struct sockaddr_storage server;
1229 * Only one connection so far
1231 SMB_ASSERT(conn->release_ip_handler == NULL);
1233 smbd_ctdb_canonicalize_ip(_client, &client);
1234 smbd_ctdb_canonicalize_ip(_server, &server);
1236 switch (client.ss_family) {
1237 case AF_INET:
1238 p4.dest = *(struct sockaddr_in *)&server;
1239 p4.src = *(struct sockaddr_in *)&client;
1240 data.dptr = (uint8_t *)&p4;
1241 data.dsize = sizeof(p4);
1242 break;
1243 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1244 case AF_INET6:
1245 p.dest.ip6 = *(struct sockaddr_in6 *)&server;
1246 p.src.ip6 = *(struct sockaddr_in6 *)&client;
1247 data.dptr = (uint8_t *)&p;
1248 data.dsize = sizeof(p);
1249 break;
1250 #endif
1251 default:
1252 return NT_STATUS_INTERNAL_ERROR;
1255 conn->release_ip_handler = release_ip_handler;
1258 * We want to be told about IP releases
1261 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1262 if (!NT_STATUS_IS_OK(status)) {
1263 return status;
1267 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1268 * can send an extra ack to trigger a reset for our client, so it
1269 * immediately reconnects
1271 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1272 CTDB_CONTROL_TCP_CLIENT, 0,
1273 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1277 * We want to handle reconfigure events
1279 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1281 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1285 call a control on the local node
1287 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1288 uint64_t srvid, uint32_t flags, TDB_DATA data,
1289 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1290 int *cstatus)
1292 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1295 #else
1297 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1298 struct ctdbd_connection **pconn)
1300 return NT_STATUS_NOT_IMPLEMENTED;
1303 #endif