s3: Make ctdbd_init_connection static
[Samba/ita.git] / source3 / lib / ctdbd_conn.c
blobafc13f20c5cbe87f4abd2e17ee1676d8308687d6
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 *)(void *)&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 uint8_t *buf, size_t available,
159 size_t *length,
160 void *private_data)
162 uint32 msglen;
164 if (available < sizeof(msglen)) {
165 return False;
168 msglen = *((uint32 *)buf);
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 (available < msglen) {
180 return false;
183 *length = msglen;
184 return true;
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(uint8_t *buf, size_t length,
224 void *private_data)
226 struct req_pull_state *state = (struct req_pull_state *)private_data;
228 state->req.data = talloc_move(state->mem_ctx, &buf);
229 state->req.length = length;
230 return NT_STATUS_OK;
234 * Fetch a messaging_rec from an incoming ctdb style message
237 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
238 size_t overall_length,
239 struct ctdb_req_message *msg)
241 struct messaging_rec *result;
242 DATA_BLOB blob;
243 enum ndr_err_code ndr_err;
245 if ((overall_length < offsetof(struct ctdb_req_message, data))
246 || (overall_length
247 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
249 cluster_fatal("got invalid msg length");
252 if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
253 DEBUG(0, ("talloc failed\n"));
254 return NULL;
257 blob = data_blob_const(msg->data, msg->datalen);
259 ndr_err = ndr_pull_struct_blob(
260 &blob, result, result,
261 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
263 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
264 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
265 ndr_errstr(ndr_err)));
266 TALLOC_FREE(result);
267 return NULL;
270 if (DEBUGLEVEL >= 10) {
271 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
272 NDR_PRINT_DEBUG(messaging_rec, result);
275 return result;
278 static NTSTATUS ctdb_packet_fd_read_sync(struct packet_context *ctx)
280 struct timeval timeout;
281 struct timeval *ptimeout;
283 timeout = timeval_set(lp_ctdb_timeout(), 0);
284 ptimeout = (timeout.tv_sec != 0) ? &timeout : NULL;
286 return packet_fd_read_sync(ctx, ptimeout);
290 * Read a full ctdbd request. If we have a messaging context, defer incoming
291 * messages that might come in between.
294 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
295 TALLOC_CTX *mem_ctx, void *result)
297 struct ctdb_req_header *hdr;
298 struct req_pull_state state;
299 NTSTATUS status;
301 again:
303 status = ctdb_packet_fd_read_sync(conn->pkt);
305 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
306 /* EAGAIN */
307 goto again;
308 } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
309 /* EAGAIN */
310 goto again;
313 if (!NT_STATUS_IS_OK(status)) {
314 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
315 cluster_fatal("ctdbd died\n");
318 next_pkt:
320 ZERO_STRUCT(state);
321 state.mem_ctx = mem_ctx;
323 if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
324 &state, &status)) {
326 * Not enough data
328 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
329 goto again;
332 if (!NT_STATUS_IS_OK(status)) {
333 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
334 cluster_fatal("ctdbd died\n");
337 hdr = (struct ctdb_req_header *)state.req.data;
339 DEBUG(10, ("Received ctdb packet\n"));
340 ctdb_packet_dump(hdr);
342 if (hdr->operation == CTDB_REQ_MESSAGE) {
343 struct timed_event *evt;
344 struct deferred_msg_state *msg_state;
345 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
347 if (conn->msg_ctx == NULL) {
348 DEBUG(1, ("Got a message without having a msg ctx, "
349 "dropping msg %llu\n",
350 (long long unsigned)msg->srvid));
351 goto next_pkt;
354 if ((conn->release_ip_handler != NULL)
355 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
356 /* must be dispatched immediately */
357 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
358 conn->release_ip_handler((const char *)msg->data,
359 conn->release_ip_priv);
360 TALLOC_FREE(hdr);
361 goto next_pkt;
364 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
365 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
367 DEBUG(1, ("ctdb_read_req: Got %s message\n",
368 (msg->srvid == CTDB_SRVID_RECONFIGURE)
369 ? "cluster reconfigure" : "SAMBA_NOTIFY"));
371 messaging_send(conn->msg_ctx,
372 messaging_server_id(conn->msg_ctx),
373 MSG_SMB_BRL_VALIDATE, &data_blob_null);
374 messaging_send(conn->msg_ctx,
375 messaging_server_id(conn->msg_ctx),
376 MSG_DBWRAP_G_LOCK_RETRY,
377 &data_blob_null);
378 TALLOC_FREE(hdr);
379 goto next_pkt;
382 if (!(msg_state = TALLOC_P(talloc_autofree_context(), struct deferred_msg_state))) {
383 DEBUG(0, ("talloc failed\n"));
384 TALLOC_FREE(hdr);
385 goto next_pkt;
388 if (!(msg_state->rec = ctdb_pull_messaging_rec(
389 msg_state, state.req.length, msg))) {
390 DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
391 TALLOC_FREE(msg_state);
392 TALLOC_FREE(hdr);
393 goto next_pkt;
396 TALLOC_FREE(hdr);
398 msg_state->msg_ctx = conn->msg_ctx;
401 * We're waiting for a call reply, but an async message has
402 * crossed. Defer dispatching to the toplevel event loop.
404 evt = event_add_timed(conn->msg_ctx->event_ctx,
405 conn->msg_ctx->event_ctx,
406 timeval_zero(),
407 deferred_message_dispatch,
408 msg_state);
409 if (evt == NULL) {
410 DEBUG(0, ("event_add_timed failed\n"));
411 TALLOC_FREE(msg_state);
412 TALLOC_FREE(hdr);
413 goto next_pkt;
416 goto next_pkt;
419 if (hdr->reqid != reqid) {
420 /* we got the wrong reply */
421 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
422 "been %u\n", hdr->reqid, reqid));
423 TALLOC_FREE(hdr);
424 goto again;
427 *((void **)result) = talloc_move(mem_ctx, &hdr);
429 return NT_STATUS_OK;
433 * Get us a ctdbd connection
436 static NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
437 struct ctdbd_connection **pconn)
439 struct ctdbd_connection *conn;
440 NTSTATUS status;
442 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
443 DEBUG(0, ("talloc failed\n"));
444 return NT_STATUS_NO_MEMORY;
447 status = ctdbd_connect(conn, &conn->pkt);
449 if (!NT_STATUS_IS_OK(status)) {
450 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
451 goto fail;
454 status = get_cluster_vnn(conn, &conn->our_vnn);
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
458 goto fail;
461 generate_random_buffer((unsigned char *)&conn->rand_srvid,
462 sizeof(conn->rand_srvid));
464 status = register_with_ctdbd(conn, conn->rand_srvid);
466 if (!NT_STATUS_IS_OK(status)) {
467 DEBUG(5, ("Could not register random srvid: %s\n",
468 nt_errstr(status)));
469 goto fail;
472 *pconn = conn;
473 return NT_STATUS_OK;
475 fail:
476 TALLOC_FREE(conn);
477 return status;
481 * Get us a ctdbd connection and register us as a process
484 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
485 struct ctdbd_connection **pconn)
487 struct ctdbd_connection *conn;
488 NTSTATUS status;
490 status = ctdbd_init_connection(mem_ctx, &conn);
492 if (!NT_STATUS_IS_OK(status)) {
493 return status;
496 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
497 if (!NT_STATUS_IS_OK(status)) {
498 goto fail;
501 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
502 if (!NT_STATUS_IS_OK(status)) {
503 goto fail;
506 status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
507 if (!NT_STATUS_IS_OK(status)) {
508 goto fail;
511 *pconn = conn;
512 return NT_STATUS_OK;
514 fail:
515 TALLOC_FREE(conn);
516 return status;
519 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
521 return conn->msg_ctx;
524 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
526 return packet_get_fd(conn->pkt);
530 * Packet handler to receive and handle a ctdb message
532 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
533 void *private_data)
535 struct ctdbd_connection *conn = talloc_get_type_abort(
536 private_data, struct ctdbd_connection);
537 struct ctdb_req_message *msg;
538 struct messaging_rec *msg_rec;
540 msg = (struct ctdb_req_message *)buf;
542 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
543 DEBUG(0, ("Received async msg of type %u, discarding\n",
544 msg->hdr.operation));
545 TALLOC_FREE(buf);
546 return NT_STATUS_INVALID_PARAMETER;
549 if ((conn->release_ip_handler != NULL)
550 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
551 /* must be dispatched immediately */
552 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
553 conn->release_ip_handler((const char *)msg->data,
554 conn->release_ip_priv);
555 TALLOC_FREE(buf);
556 return NT_STATUS_OK;
559 SMB_ASSERT(conn->msg_ctx != NULL);
561 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
562 || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
563 DEBUG(0,("Got cluster reconfigure message\n"));
565 * when the cluster is reconfigured or someone of the
566 * family has passed away (SAMBA_NOTIFY), we need to
567 * clean the brl database
569 messaging_send(conn->msg_ctx,
570 messaging_server_id(conn->msg_ctx),
571 MSG_SMB_BRL_VALIDATE, &data_blob_null);
573 messaging_send(conn->msg_ctx,
574 messaging_server_id(conn->msg_ctx),
575 MSG_DBWRAP_G_LOCK_RETRY,
576 &data_blob_null);
578 TALLOC_FREE(buf);
579 return NT_STATUS_OK;
582 /* only messages to our pid or the broadcast are valid here */
583 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
584 DEBUG(0,("Got unexpected message with srvid=%llu\n",
585 (unsigned long long)msg->srvid));
586 TALLOC_FREE(buf);
587 return NT_STATUS_OK;
590 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
591 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
592 TALLOC_FREE(buf);
593 return NT_STATUS_NO_MEMORY;
596 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
598 TALLOC_FREE(msg_rec);
599 TALLOC_FREE(buf);
600 return NT_STATUS_OK;
604 * The ctdbd socket is readable asynchronuously
607 static void ctdbd_socket_handler(struct event_context *event_ctx,
608 struct fd_event *event,
609 uint16 flags,
610 void *private_data)
612 struct ctdbd_connection *conn = talloc_get_type_abort(
613 private_data, struct ctdbd_connection);
615 NTSTATUS status;
617 status = packet_fd_read(conn->pkt);
619 if (!NT_STATUS_IS_OK(status)) {
620 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
621 cluster_fatal("ctdbd died\n");
624 while (packet_handler(conn->pkt, ctdb_req_complete,
625 ctdb_handle_message, conn, &status)) {
626 if (!NT_STATUS_IS_OK(status)) {
627 DEBUG(10, ("could not handle incoming message: %s\n",
628 nt_errstr(status)));
634 * Prepare a ctdbd connection to receive messages
637 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
638 struct messaging_context *msg_ctx)
640 SMB_ASSERT(conn->msg_ctx == NULL);
641 SMB_ASSERT(conn->fde == NULL);
643 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
644 packet_get_fd(conn->pkt),
645 EVENT_FD_READ,
646 ctdbd_socket_handler,
647 conn))) {
648 DEBUG(0, ("event_add_fd failed\n"));
649 return NT_STATUS_NO_MEMORY;
652 conn->msg_ctx = msg_ctx;
654 return NT_STATUS_OK;
658 * Send a messaging message across a ctdbd
661 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
662 uint32 dst_vnn, uint64 dst_srvid,
663 struct messaging_rec *msg)
665 struct ctdb_req_message r;
666 TALLOC_CTX *mem_ctx;
667 DATA_BLOB blob;
668 NTSTATUS status;
669 enum ndr_err_code ndr_err;
671 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
672 DEBUG(0, ("talloc failed\n"));
673 return NT_STATUS_NO_MEMORY;
676 ndr_err = ndr_push_struct_blob(
677 &blob, mem_ctx, msg,
678 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
680 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
681 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
682 ndr_errstr(ndr_err)));
683 status = ndr_map_error2ntstatus(ndr_err);
684 goto fail;
687 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
688 r.hdr.ctdb_magic = CTDB_MAGIC;
689 r.hdr.ctdb_version = CTDB_VERSION;
690 r.hdr.generation = 1;
691 r.hdr.operation = CTDB_REQ_MESSAGE;
692 r.hdr.destnode = dst_vnn;
693 r.hdr.srcnode = conn->our_vnn;
694 r.hdr.reqid = 0;
695 r.srvid = dst_srvid;
696 r.datalen = blob.length;
698 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
699 ctdb_packet_dump(&r.hdr);
701 status = packet_send(
702 conn->pkt, 2,
703 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
704 blob);
706 if (!NT_STATUS_IS_OK(status)) {
707 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
708 goto fail;
711 status = packet_flush(conn->pkt);
713 if (!NT_STATUS_IS_OK(status)) {
714 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
715 cluster_fatal("cluster dispatch daemon msg write error\n");
718 status = NT_STATUS_OK;
719 fail:
720 TALLOC_FREE(mem_ctx);
721 return status;
725 * send/recv a generic ctdb control message
727 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
728 uint32_t vnn, uint32 opcode,
729 uint64_t srvid, uint32_t flags,
730 TDB_DATA data,
731 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
732 int *cstatus)
734 struct ctdb_req_control req;
735 struct ctdb_reply_control *reply = NULL;
736 struct ctdbd_connection *new_conn = NULL;
737 NTSTATUS status;
739 /* the samba3 ctdb code can't handle NOREPLY yet */
740 flags &= ~CTDB_CTRL_FLAG_NOREPLY;
742 if (conn == NULL) {
743 status = ctdbd_init_connection(NULL, &new_conn);
745 if (!NT_STATUS_IS_OK(status)) {
746 DEBUG(10, ("Could not init temp connection: %s\n",
747 nt_errstr(status)));
748 goto fail;
751 conn = new_conn;
754 ZERO_STRUCT(req);
755 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
756 req.hdr.ctdb_magic = CTDB_MAGIC;
757 req.hdr.ctdb_version = CTDB_VERSION;
758 req.hdr.operation = CTDB_REQ_CONTROL;
759 req.hdr.reqid = ++conn->reqid;
760 req.hdr.destnode = vnn;
761 req.opcode = opcode;
762 req.srvid = srvid;
763 req.datalen = data.dsize;
765 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
766 ctdb_packet_dump(&req.hdr);
768 status = packet_send(
769 conn->pkt, 2,
770 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
771 data_blob_const(data.dptr, data.dsize));
773 if (!NT_STATUS_IS_OK(status)) {
774 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
775 goto fail;
778 status = packet_flush(conn->pkt);
780 if (!NT_STATUS_IS_OK(status)) {
781 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
782 cluster_fatal("cluster dispatch daemon control write error\n");
785 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
786 TALLOC_FREE(new_conn);
787 return NT_STATUS_OK;
790 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
792 if (!NT_STATUS_IS_OK(status)) {
793 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
794 goto fail;
797 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
798 DEBUG(0, ("received invalid reply\n"));
799 goto fail;
802 if (outdata) {
803 if (!(outdata->dptr = (uint8 *)talloc_memdup(
804 mem_ctx, reply->data, reply->datalen))) {
805 TALLOC_FREE(reply);
806 return NT_STATUS_NO_MEMORY;
808 outdata->dsize = reply->datalen;
810 if (cstatus) {
811 (*cstatus) = reply->status;
814 status = NT_STATUS_OK;
816 fail:
817 TALLOC_FREE(new_conn);
818 TALLOC_FREE(reply);
819 return status;
823 * see if a remote process exists
825 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
827 NTSTATUS status;
828 TDB_DATA data;
829 int32_t cstatus;
831 data.dptr = (uint8_t*)&pid;
832 data.dsize = sizeof(pid);
834 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
835 data, NULL, NULL, &cstatus);
836 if (!NT_STATUS_IS_OK(status)) {
837 DEBUG(0, (__location__ " ctdb_control for process_exists "
838 "failed\n"));
839 return False;
842 return cstatus == 0;
846 * Get a db path
848 char *ctdbd_dbpath(struct ctdbd_connection *conn,
849 TALLOC_CTX *mem_ctx, uint32_t db_id)
851 NTSTATUS status;
852 TDB_DATA data;
853 int32_t cstatus;
855 data.dptr = (uint8_t*)&db_id;
856 data.dsize = sizeof(db_id);
858 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
859 CTDB_CONTROL_GETDBPATH, 0, 0, data,
860 mem_ctx, &data, &cstatus);
861 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
862 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
863 return NULL;
866 return (char *)data.dptr;
870 * attach to a ctdb database
872 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
873 const char *name, uint32_t *db_id, int tdb_flags)
875 NTSTATUS status;
876 TDB_DATA data;
877 int32_t cstatus;
878 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
880 data.dptr = (uint8_t*)name;
881 data.dsize = strlen(name)+1;
883 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
884 persistent
885 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
886 : CTDB_CONTROL_DB_ATTACH,
887 0, 0, data, NULL, &data, &cstatus);
888 if (!NT_STATUS_IS_OK(status)) {
889 DEBUG(0, (__location__ " ctdb_control for db_attach "
890 "failed: %s\n", nt_errstr(status)));
891 return status;
894 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
895 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
896 return NT_STATUS_INTERNAL_ERROR;
899 *db_id = *(uint32_t *)data.dptr;
900 talloc_free(data.dptr);
902 if (!(tdb_flags & TDB_SEQNUM)) {
903 return NT_STATUS_OK;
906 data.dptr = (uint8_t *)db_id;
907 data.dsize = sizeof(*db_id);
909 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
910 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
911 NULL, NULL, &cstatus);
912 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
913 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
914 "failed\n"));
915 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
916 status;
919 return NT_STATUS_OK;
923 * force the migration of a record to this node
925 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
926 TDB_DATA key)
928 struct ctdb_req_call req;
929 struct ctdb_reply_call *reply;
930 NTSTATUS status;
932 ZERO_STRUCT(req);
934 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
935 req.hdr.ctdb_magic = CTDB_MAGIC;
936 req.hdr.ctdb_version = CTDB_VERSION;
937 req.hdr.operation = CTDB_REQ_CALL;
938 req.hdr.reqid = ++conn->reqid;
939 req.flags = CTDB_IMMEDIATE_MIGRATION;
940 req.callid = CTDB_NULL_FUNC;
941 req.db_id = db_id;
942 req.keylen = key.dsize;
944 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
945 ctdb_packet_dump(&req.hdr);
947 status = packet_send(
948 conn->pkt, 2,
949 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
950 data_blob_const(key.dptr, key.dsize));
952 if (!NT_STATUS_IS_OK(status)) {
953 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
954 return status;
957 status = packet_flush(conn->pkt);
959 if (!NT_STATUS_IS_OK(status)) {
960 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
961 cluster_fatal("cluster dispatch daemon control write error\n");
964 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
966 if (!NT_STATUS_IS_OK(status)) {
967 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
968 goto fail;
971 if (reply->hdr.operation != CTDB_REPLY_CALL) {
972 DEBUG(0, ("received invalid reply\n"));
973 status = NT_STATUS_INTERNAL_ERROR;
974 goto fail;
977 status = NT_STATUS_OK;
978 fail:
980 TALLOC_FREE(reply);
981 return status;
985 * remotely fetch a record without locking it or forcing a migration
987 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
988 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
990 struct ctdb_req_call req;
991 struct ctdb_reply_call *reply;
992 NTSTATUS status;
994 ZERO_STRUCT(req);
996 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
997 req.hdr.ctdb_magic = CTDB_MAGIC;
998 req.hdr.ctdb_version = CTDB_VERSION;
999 req.hdr.operation = CTDB_REQ_CALL;
1000 req.hdr.reqid = ++conn->reqid;
1001 req.flags = 0;
1002 req.callid = CTDB_FETCH_FUNC;
1003 req.db_id = db_id;
1004 req.keylen = key.dsize;
1006 status = packet_send(
1007 conn->pkt, 2,
1008 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1009 data_blob_const(key.dptr, key.dsize));
1011 if (!NT_STATUS_IS_OK(status)) {
1012 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
1013 return status;
1016 status = packet_flush(conn->pkt);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1020 cluster_fatal("cluster dispatch daemon control write error\n");
1023 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1025 if (!NT_STATUS_IS_OK(status)) {
1026 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1027 goto fail;
1030 if (reply->hdr.operation != CTDB_REPLY_CALL) {
1031 DEBUG(0, ("received invalid reply\n"));
1032 status = NT_STATUS_INTERNAL_ERROR;
1033 goto fail;
1036 data->dsize = reply->datalen;
1037 if (data->dsize == 0) {
1038 data->dptr = NULL;
1039 goto done;
1042 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1043 reply->datalen);
1044 if (data->dptr == NULL) {
1045 DEBUG(0, ("talloc failed\n"));
1046 status = NT_STATUS_NO_MEMORY;
1047 goto fail;
1050 done:
1051 status = NT_STATUS_OK;
1052 fail:
1053 TALLOC_FREE(reply);
1054 return status;
1057 struct ctdbd_traverse_state {
1058 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1059 void *private_data;
1063 * Handle a traverse record coming in on the ctdbd connection
1066 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1067 void *private_data)
1069 struct ctdbd_traverse_state *state =
1070 (struct ctdbd_traverse_state *)private_data;
1072 struct ctdb_req_message *m;
1073 struct ctdb_rec_data *d;
1074 TDB_DATA key, data;
1076 m = (struct ctdb_req_message *)buf;
1078 if (length < sizeof(*m) || m->hdr.length != length) {
1079 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1080 TALLOC_FREE(buf);
1081 return NT_STATUS_UNEXPECTED_IO_ERROR;
1084 d = (struct ctdb_rec_data *)&m->data[0];
1085 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1086 DEBUG(0, ("Got invalid traverse data of length %d\n",
1087 (int)m->datalen));
1088 TALLOC_FREE(buf);
1089 return NT_STATUS_UNEXPECTED_IO_ERROR;
1092 key.dsize = d->keylen;
1093 key.dptr = &d->data[0];
1094 data.dsize = d->datalen;
1095 data.dptr = &d->data[d->keylen];
1097 if (key.dsize == 0 && data.dsize == 0) {
1098 /* end of traverse */
1099 return NT_STATUS_END_OF_FILE;
1102 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1103 DEBUG(0, ("Got invalid ltdb header length %d\n",
1104 (int)data.dsize));
1105 TALLOC_FREE(buf);
1106 return NT_STATUS_UNEXPECTED_IO_ERROR;
1108 data.dsize -= sizeof(struct ctdb_ltdb_header);
1109 data.dptr += sizeof(struct ctdb_ltdb_header);
1111 if (state->fn) {
1112 state->fn(key, data, state->private_data);
1115 TALLOC_FREE(buf);
1116 return NT_STATUS_OK;
1120 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1121 connection to ctdbd to avoid the hairy recursive and async problems with
1122 everything in-line.
1125 NTSTATUS ctdbd_traverse(uint32 db_id,
1126 void (*fn)(TDB_DATA key, TDB_DATA data,
1127 void *private_data),
1128 void *private_data)
1130 struct ctdbd_connection *conn;
1131 NTSTATUS status;
1133 TDB_DATA data;
1134 struct ctdb_traverse_start t;
1135 int cstatus;
1136 struct ctdbd_traverse_state state;
1138 status = ctdbd_init_connection(NULL, &conn);
1139 if (!NT_STATUS_IS_OK(status)) {
1140 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1141 nt_errstr(status)));
1142 return status;
1145 t.db_id = db_id;
1146 t.srvid = conn->rand_srvid;
1147 t.reqid = ++conn->reqid;
1149 data.dptr = (uint8_t *)&t;
1150 data.dsize = sizeof(t);
1152 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1153 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1154 data, NULL, NULL, &cstatus);
1156 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1158 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1159 cstatus));
1161 if (NT_STATUS_IS_OK(status)) {
1163 * We need a mapping here
1165 status = NT_STATUS_UNSUCCESSFUL;
1167 goto done;
1170 state.fn = fn;
1171 state.private_data = private_data;
1173 while (True) {
1175 status = NT_STATUS_OK;
1177 if (packet_handler(conn->pkt, ctdb_req_complete,
1178 ctdb_traverse_handler, &state, &status)) {
1180 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1181 status = NT_STATUS_OK;
1182 break;
1186 * There might be more in the queue
1188 continue;
1191 if (!NT_STATUS_IS_OK(status)) {
1192 break;
1195 status = ctdb_packet_fd_read_sync(conn->pkt);
1197 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1199 * There might be more in the queue
1201 continue;
1204 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1205 status = NT_STATUS_OK;
1206 break;
1209 if (!NT_STATUS_IS_OK(status)) {
1210 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1211 cluster_fatal("ctdbd died\n");
1215 done:
1216 TALLOC_FREE(conn);
1217 return status;
1221 This is used to canonicalize a ctdb_sock_addr structure.
1223 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1224 struct sockaddr_storage *out)
1226 memcpy(out, in, sizeof (*out));
1228 #ifdef HAVE_IPV6
1229 if (in->ss_family == AF_INET6) {
1230 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1231 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1232 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1233 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1234 memset(out, 0, sizeof(*out));
1235 #ifdef HAVE_SOCK_SIN_LEN
1236 out4->sin_len = sizeof(*out);
1237 #endif
1238 out4->sin_family = AF_INET;
1239 out4->sin_port = in6->sin6_port;
1240 memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1243 #endif
1247 * Register us as a server for a particular tcp connection
1250 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1251 const struct sockaddr_storage *_server,
1252 const struct sockaddr_storage *_client,
1253 void (*release_ip_handler)(const char *ip_addr,
1254 void *private_data),
1255 void *private_data)
1258 * we still use ctdb_control_tcp for ipv4
1259 * because we want to work against older ctdb
1260 * versions at runtime
1262 struct ctdb_control_tcp p4;
1263 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1264 struct ctdb_control_tcp_addr p;
1265 #endif
1266 TDB_DATA data;
1267 NTSTATUS status;
1268 struct sockaddr_storage client;
1269 struct sockaddr_storage server;
1272 * Only one connection so far
1274 SMB_ASSERT(conn->release_ip_handler == NULL);
1276 smbd_ctdb_canonicalize_ip(_client, &client);
1277 smbd_ctdb_canonicalize_ip(_server, &server);
1279 switch (client.ss_family) {
1280 case AF_INET:
1281 p4.dest = *(struct sockaddr_in *)(void *)&server;
1282 p4.src = *(struct sockaddr_in *)(void *)&client;
1283 data.dptr = (uint8_t *)&p4;
1284 data.dsize = sizeof(p4);
1285 break;
1286 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1287 case AF_INET6:
1288 p.dest.ip6 = *(struct sockaddr_in6 *)(void *)&server;
1289 p.src.ip6 = *(struct sockaddr_in6 *)(void *)&client;
1290 data.dptr = (uint8_t *)&p;
1291 data.dsize = sizeof(p);
1292 break;
1293 #endif
1294 default:
1295 return NT_STATUS_INTERNAL_ERROR;
1298 conn->release_ip_handler = release_ip_handler;
1301 * We want to be told about IP releases
1304 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1305 if (!NT_STATUS_IS_OK(status)) {
1306 return status;
1310 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1311 * can send an extra ack to trigger a reset for our client, so it
1312 * immediately reconnects
1314 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1315 CTDB_CONTROL_TCP_CLIENT, 0,
1316 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1320 * We want to handle reconfigure events
1322 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1324 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1328 call a control on the local node
1330 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1331 uint64_t srvid, uint32_t flags, TDB_DATA data,
1332 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1333 int *cstatus)
1335 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1338 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1340 struct ctdb_client_notify_register reg_data;
1341 size_t struct_len;
1342 NTSTATUS status;
1343 int cstatus;
1345 reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1346 reg_data.len = 1;
1347 reg_data.notify_data[0] = 0;
1349 struct_len = offsetof(struct ctdb_client_notify_register,
1350 notify_data) + reg_data.len;
1352 status = ctdbd_control_local(
1353 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1354 make_tdb_data((uint8_t *)&reg_data, struct_len),
1355 NULL, NULL, &cstatus);
1356 if (!NT_STATUS_IS_OK(status)) {
1357 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1358 nt_errstr(status)));
1360 return status;
1363 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1365 struct ctdb_client_notify_deregister dereg_data;
1366 NTSTATUS status;
1367 int cstatus;
1369 dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1371 status = ctdbd_control_local(
1372 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1373 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1374 NULL, NULL, &cstatus);
1375 if (!NT_STATUS_IS_OK(status)) {
1376 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1377 nt_errstr(status)));
1379 return status;
1382 #else
1384 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1385 struct ctdbd_connection **pconn)
1387 return NT_STATUS_NOT_IMPLEMENTED;
1390 #endif