lib/talloc: sync build system changes from samba4
[Samba/bb.git] / source / lib / ctdbd_conn.c
blob1ae23bcf8270f482d65ccdb5698b3cfc8a70a5dc
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 const 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 deferred_message_dispatch,
391 msg_state);
392 if (evt == NULL) {
393 DEBUG(0, ("event_add_timed failed\n"));
394 TALLOC_FREE(msg_state);
395 TALLOC_FREE(hdr);
396 goto next_pkt;
399 goto next_pkt;
402 if (hdr->reqid != reqid) {
403 /* we got the wrong reply */
404 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
405 "been %u\n", hdr->reqid, reqid));
406 TALLOC_FREE(hdr);
407 goto again;
410 *((void **)result) = talloc_move(mem_ctx, &hdr);
412 return NT_STATUS_OK;
416 * Get us a ctdbd connection
419 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
420 struct ctdbd_connection **pconn)
422 struct ctdbd_connection *conn;
423 NTSTATUS status;
425 if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
426 DEBUG(0, ("talloc failed\n"));
427 return NT_STATUS_NO_MEMORY;
430 status = ctdbd_connect(conn, &conn->pkt);
432 if (!NT_STATUS_IS_OK(status)) {
433 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
434 goto fail;
437 status = get_cluster_vnn(conn, &conn->our_vnn);
439 if (!NT_STATUS_IS_OK(status)) {
440 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
441 goto fail;
444 generate_random_buffer((unsigned char *)&conn->rand_srvid,
445 sizeof(conn->rand_srvid));
447 status = register_with_ctdbd(conn, conn->rand_srvid);
449 if (!NT_STATUS_IS_OK(status)) {
450 DEBUG(5, ("Could not register random srvid: %s\n",
451 nt_errstr(status)));
452 goto fail;
455 *pconn = conn;
456 return NT_STATUS_OK;
458 fail:
459 TALLOC_FREE(conn);
460 return status;
464 * Get us a ctdbd connection and register us as a process
467 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
468 struct ctdbd_connection **pconn)
470 struct ctdbd_connection *conn;
471 NTSTATUS status;
473 status = ctdbd_init_connection(mem_ctx, &conn);
475 if (!NT_STATUS_IS_OK(status)) {
476 return status;
479 status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
480 if (!NT_STATUS_IS_OK(status)) {
481 goto fail;
484 status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
485 if (!NT_STATUS_IS_OK(status)) {
486 goto fail;
489 *pconn = conn;
490 return NT_STATUS_OK;
492 fail:
493 TALLOC_FREE(conn);
494 return status;
498 * Packet handler to receive and handle a ctdb message
500 static NTSTATUS ctdb_handle_message(const struct data_blob *data,
501 void *private_data)
503 struct ctdbd_connection *conn = talloc_get_type_abort(
504 private_data, struct ctdbd_connection);
505 struct ctdb_req_message *msg;
506 struct messaging_rec *msg_rec;
508 msg = (struct ctdb_req_message *)data->data;
510 if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
511 DEBUG(0, ("Received async msg of type %u, discarding\n",
512 msg->hdr.operation));
513 return NT_STATUS_INVALID_PARAMETER;
516 if ((conn->release_ip_handler != NULL)
517 && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
518 /* must be dispatched immediately */
519 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
520 conn->release_ip_handler((const char *)msg->data,
521 conn->release_ip_priv);
522 return NT_STATUS_OK;
525 SMB_ASSERT(conn->msg_ctx != NULL);
527 if (msg->srvid == CTDB_SRVID_RECONFIGURE) {
528 DEBUG(0,("Got cluster reconfigure message\n"));
530 * when the cluster is reconfigured, we need to clean the brl
531 * database
533 messaging_send(conn->msg_ctx, procid_self(),
534 MSG_SMB_BRL_VALIDATE, &data_blob_null);
537 * it's possible that we have just rejoined the cluster after
538 * an outage. In that case our pending locks could have been
539 * removed from the lockdb, so retry them once more
541 message_send_all(conn->msg_ctx, MSG_SMB_UNLOCK, NULL, 0, NULL);
543 return NT_STATUS_OK;
547 /* only messages to our pid or the broadcast are valid here */
548 if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
549 DEBUG(0,("Got unexpected message with srvid=%llu\n",
550 (unsigned long long)msg->srvid));
551 return NT_STATUS_OK;
554 if (!(msg_rec = ctdb_pull_messaging_rec(NULL, data->length, msg))) {
555 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
556 return NT_STATUS_NO_MEMORY;
559 messaging_dispatch_rec(conn->msg_ctx, msg_rec);
561 TALLOC_FREE(msg_rec);
562 return NT_STATUS_OK;
566 * The ctdbd socket is readable asynchronuously
569 static void ctdbd_socket_handler(struct event_context *event_ctx,
570 struct fd_event *event,
571 uint16 flags,
572 void *private_data)
574 struct ctdbd_connection *conn = talloc_get_type_abort(
575 private_data, struct ctdbd_connection);
577 NTSTATUS status;
579 status = packet_fd_read(conn->pkt);
581 if (!NT_STATUS_IS_OK(status)) {
582 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
583 cluster_fatal("ctdbd died\n");
586 while (packet_handler(conn->pkt, ctdb_req_complete,
587 ctdb_handle_message, conn, &status)) {
588 if (!NT_STATUS_IS_OK(status)) {
589 DEBUG(10, ("could not handle incoming message: %s\n",
590 nt_errstr(status)));
596 * Prepare a ctdbd connection to receive messages
599 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
600 struct messaging_context *msg_ctx)
602 SMB_ASSERT(conn->msg_ctx == NULL);
603 SMB_ASSERT(conn->fde == NULL);
605 if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
606 packet_get_fd(conn->pkt),
607 EVENT_FD_READ,
608 ctdbd_socket_handler,
609 conn))) {
610 DEBUG(0, ("event_add_fd failed\n"));
611 return NT_STATUS_NO_MEMORY;
614 conn->msg_ctx = msg_ctx;
616 return NT_STATUS_OK;
620 * Send a messaging message across a ctdbd
623 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
624 uint32 dst_vnn, uint64 dst_srvid,
625 struct messaging_rec *msg)
627 struct ctdb_req_message r;
628 TALLOC_CTX *mem_ctx;
629 DATA_BLOB blob;
630 NTSTATUS status;
631 enum ndr_err_code ndr_err;
633 if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
634 DEBUG(0, ("talloc failed\n"));
635 return NT_STATUS_NO_MEMORY;
638 ndr_err = ndr_push_struct_blob(
639 &blob, mem_ctx, msg,
640 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
642 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
643 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
644 ndr_errstr(ndr_err)));
645 status = ndr_map_error2ntstatus(ndr_err);
646 goto fail;
649 r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
650 r.hdr.ctdb_magic = CTDB_MAGIC;
651 r.hdr.ctdb_version = CTDB_VERSION;
652 r.hdr.generation = 1;
653 r.hdr.operation = CTDB_REQ_MESSAGE;
654 r.hdr.destnode = dst_vnn;
655 r.hdr.srcnode = conn->our_vnn;
656 r.hdr.reqid = 0;
657 r.srvid = dst_srvid;
658 r.datalen = blob.length;
660 DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
661 ctdb_packet_dump(&r.hdr);
663 status = packet_send(
664 conn->pkt, 2,
665 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
666 blob);
668 if (!NT_STATUS_IS_OK(status)) {
669 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
670 goto fail;
673 status = packet_flush(conn->pkt);
675 if (!NT_STATUS_IS_OK(status)) {
676 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
677 cluster_fatal("cluster dispatch daemon msg write error\n");
680 status = NT_STATUS_OK;
681 fail:
682 TALLOC_FREE(mem_ctx);
683 return status;
687 * send/recv a generic ctdb control message
689 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
690 uint32_t vnn, uint32 opcode,
691 uint64_t srvid, uint32_t flags,
692 TDB_DATA data,
693 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
694 int *cstatus)
696 struct ctdb_req_control req;
697 struct ctdb_reply_control *reply = NULL;
698 struct ctdbd_connection *new_conn = NULL;
699 NTSTATUS status;
701 /* the samba3 ctdb code can't handle NOREPLY yet */
702 flags &= ~CTDB_CTRL_FLAG_NOREPLY;
704 if (conn == NULL) {
705 status = ctdbd_init_connection(NULL, &new_conn);
707 if (!NT_STATUS_IS_OK(status)) {
708 DEBUG(10, ("Could not init temp connection: %s\n",
709 nt_errstr(status)));
710 goto fail;
713 conn = new_conn;
716 ZERO_STRUCT(req);
717 req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
718 req.hdr.ctdb_magic = CTDB_MAGIC;
719 req.hdr.ctdb_version = CTDB_VERSION;
720 req.hdr.operation = CTDB_REQ_CONTROL;
721 req.hdr.reqid = ++conn->reqid;
722 req.hdr.destnode = vnn;
723 req.opcode = opcode;
724 req.srvid = srvid;
725 req.datalen = data.dsize;
727 DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
728 ctdb_packet_dump(&req.hdr);
730 status = packet_send(
731 conn->pkt, 2,
732 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
733 data_blob_const(data.dptr, data.dsize));
735 if (!NT_STATUS_IS_OK(status)) {
736 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
737 goto fail;
740 status = packet_flush(conn->pkt);
742 if (!NT_STATUS_IS_OK(status)) {
743 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
744 cluster_fatal("cluster dispatch daemon control write error\n");
747 if (flags & CTDB_CTRL_FLAG_NOREPLY) {
748 TALLOC_FREE(new_conn);
749 return NT_STATUS_OK;
752 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
754 if (!NT_STATUS_IS_OK(status)) {
755 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
756 goto fail;
759 if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
760 DEBUG(0, ("received invalid reply\n"));
761 goto fail;
764 if (outdata) {
765 if (!(outdata->dptr = (uint8 *)talloc_memdup(
766 mem_ctx, reply->data, reply->datalen))) {
767 TALLOC_FREE(reply);
768 return NT_STATUS_NO_MEMORY;
770 outdata->dsize = reply->datalen;
772 if (cstatus) {
773 (*cstatus) = reply->status;
776 status = NT_STATUS_OK;
778 fail:
779 TALLOC_FREE(new_conn);
780 TALLOC_FREE(reply);
781 return status;
785 * see if a remote process exists
787 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
789 NTSTATUS status;
790 TDB_DATA data;
791 int32_t cstatus;
793 data.dptr = (uint8_t*)&pid;
794 data.dsize = sizeof(pid);
796 status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
797 data, NULL, NULL, &cstatus);
798 if (!NT_STATUS_IS_OK(status)) {
799 DEBUG(0, (__location__ " ctdb_control for process_exists "
800 "failed\n"));
801 return False;
804 return cstatus == 0;
808 * Get a db path
810 char *ctdbd_dbpath(struct ctdbd_connection *conn,
811 TALLOC_CTX *mem_ctx, uint32_t db_id)
813 NTSTATUS status;
814 TDB_DATA data;
815 int32_t cstatus;
817 data.dptr = (uint8_t*)&db_id;
818 data.dsize = sizeof(db_id);
820 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
821 CTDB_CONTROL_GETDBPATH, 0, 0, data,
822 mem_ctx, &data, &cstatus);
823 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
824 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
825 return NULL;
828 return (char *)data.dptr;
832 * attach to a ctdb database
834 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
835 const char *name, uint32_t *db_id, int tdb_flags)
837 NTSTATUS status;
838 TDB_DATA data;
839 int32_t cstatus;
840 bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
842 data.dptr = (uint8_t*)name;
843 data.dsize = strlen(name)+1;
845 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
846 persistent
847 ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
848 : CTDB_CONTROL_DB_ATTACH,
849 0, 0, data, NULL, &data, &cstatus);
850 if (!NT_STATUS_IS_OK(status)) {
851 DEBUG(0, (__location__ " ctdb_control for db_attach "
852 "failed: %s\n", nt_errstr(status)));
853 return status;
856 if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
857 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
858 return NT_STATUS_INTERNAL_ERROR;
861 *db_id = *(uint32_t *)data.dptr;
862 talloc_free(data.dptr);
864 if (!(tdb_flags & TDB_SEQNUM)) {
865 return NT_STATUS_OK;
868 data.dptr = (uint8_t *)db_id;
869 data.dsize = sizeof(*db_id);
871 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
872 CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data,
873 NULL, NULL, &cstatus);
874 if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
875 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
876 "failed\n"));
877 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
878 status;
881 return NT_STATUS_OK;
885 * force the migration of a record to this node
887 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
888 TDB_DATA key)
890 struct ctdb_req_call req;
891 struct ctdb_reply_call *reply;
892 NTSTATUS status;
894 ZERO_STRUCT(req);
896 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
897 req.hdr.ctdb_magic = CTDB_MAGIC;
898 req.hdr.ctdb_version = CTDB_VERSION;
899 req.hdr.operation = CTDB_REQ_CALL;
900 req.hdr.reqid = ++conn->reqid;
901 req.flags = CTDB_IMMEDIATE_MIGRATION;
902 req.callid = CTDB_NULL_FUNC;
903 req.db_id = db_id;
904 req.keylen = key.dsize;
906 DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
907 ctdb_packet_dump(&req.hdr);
909 status = packet_send(
910 conn->pkt, 2,
911 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
912 data_blob_const(key.dptr, key.dsize));
914 if (!NT_STATUS_IS_OK(status)) {
915 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
916 return status;
919 status = packet_flush(conn->pkt);
921 if (!NT_STATUS_IS_OK(status)) {
922 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
923 cluster_fatal("cluster dispatch daemon control write error\n");
926 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
928 if (!NT_STATUS_IS_OK(status)) {
929 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
930 goto fail;
933 if (reply->hdr.operation != CTDB_REPLY_CALL) {
934 DEBUG(0, ("received invalid reply\n"));
935 status = NT_STATUS_INTERNAL_ERROR;
936 goto fail;
939 status = NT_STATUS_OK;
940 fail:
942 TALLOC_FREE(reply);
943 return status;
947 * remotely fetch a record without locking it or forcing a migration
949 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
950 TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
952 struct ctdb_req_call req;
953 struct ctdb_reply_call *reply;
954 NTSTATUS status;
956 ZERO_STRUCT(req);
958 req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
959 req.hdr.ctdb_magic = CTDB_MAGIC;
960 req.hdr.ctdb_version = CTDB_VERSION;
961 req.hdr.operation = CTDB_REQ_CALL;
962 req.hdr.reqid = ++conn->reqid;
963 req.flags = 0;
964 req.callid = CTDB_FETCH_FUNC;
965 req.db_id = db_id;
966 req.keylen = key.dsize;
968 status = packet_send(
969 conn->pkt, 2,
970 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
971 data_blob_const(key.dptr, key.dsize));
973 if (!NT_STATUS_IS_OK(status)) {
974 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
975 return status;
978 status = packet_flush(conn->pkt);
980 if (!NT_STATUS_IS_OK(status)) {
981 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
982 cluster_fatal("cluster dispatch daemon control write error\n");
985 status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
987 if (!NT_STATUS_IS_OK(status)) {
988 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
989 goto fail;
992 if (reply->hdr.operation != CTDB_REPLY_CALL) {
993 DEBUG(0, ("received invalid reply\n"));
994 status = NT_STATUS_INTERNAL_ERROR;
995 goto fail;
998 data->dsize = reply->datalen;
999 if (data->dsize == 0) {
1000 data->dptr = NULL;
1001 goto done;
1004 data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1005 reply->datalen);
1006 if (data->dptr == NULL) {
1007 DEBUG(0, ("talloc failed\n"));
1008 status = NT_STATUS_NO_MEMORY;
1009 goto fail;
1012 done:
1013 status = NT_STATUS_OK;
1014 fail:
1015 TALLOC_FREE(reply);
1016 return status;
1019 struct ctdbd_traverse_state {
1020 void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1021 void *private_data;
1025 * Handle a traverse record coming in on the ctdbd connection
1028 static NTSTATUS ctdb_traverse_handler(const struct data_blob *blob,
1029 void *private_data)
1031 struct ctdbd_traverse_state *state =
1032 (struct ctdbd_traverse_state *)private_data;
1034 struct ctdb_req_message *m;
1035 struct ctdb_rec_data *d;
1036 TDB_DATA key, data;
1038 m = (struct ctdb_req_message *)blob->data;
1040 if (blob->length < sizeof(*m) || m->hdr.length != blob->length) {
1041 DEBUG(0, ("Got invalid message of length %d\n",
1042 (int)blob->length));
1043 return NT_STATUS_UNEXPECTED_IO_ERROR;
1046 d = (struct ctdb_rec_data *)&m->data[0];
1047 if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1048 DEBUG(0, ("Got invalid traverse data of length %d\n",
1049 (int)m->datalen));
1050 return NT_STATUS_UNEXPECTED_IO_ERROR;
1053 key.dsize = d->keylen;
1054 key.dptr = &d->data[0];
1055 data.dsize = d->datalen;
1056 data.dptr = &d->data[d->keylen];
1058 if (key.dsize == 0 && data.dsize == 0) {
1059 /* end of traverse */
1060 return NT_STATUS_END_OF_FILE;
1063 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1064 DEBUG(0, ("Got invalid ltdb header length %d\n",
1065 (int)data.dsize));
1066 return NT_STATUS_UNEXPECTED_IO_ERROR;
1068 data.dsize -= sizeof(struct ctdb_ltdb_header);
1069 data.dptr += sizeof(struct ctdb_ltdb_header);
1071 if (state->fn) {
1072 state->fn(key, data, state->private_data);
1075 return NT_STATUS_OK;
1079 Traverse a ctdb database. This uses a kind-of hackish way to open a second
1080 connection to ctdbd to avoid the hairy recursive and async problems with
1081 everything in-line.
1084 NTSTATUS ctdbd_traverse(uint32 db_id,
1085 void (*fn)(TDB_DATA key, TDB_DATA data,
1086 void *private_data),
1087 void *private_data)
1089 struct ctdbd_connection *conn;
1090 NTSTATUS status;
1092 TDB_DATA data;
1093 struct ctdb_traverse_start t;
1094 int cstatus;
1095 struct ctdbd_traverse_state state;
1097 status = ctdbd_init_connection(NULL, &conn);
1099 t.db_id = db_id;
1100 t.srvid = conn->rand_srvid;
1101 t.reqid = ++conn->reqid;
1103 data.dptr = (uint8_t *)&t;
1104 data.dsize = sizeof(t);
1106 status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1107 CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1108 data, NULL, NULL, &cstatus);
1110 if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1112 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1113 cstatus));
1115 if (NT_STATUS_IS_OK(status)) {
1117 * We need a mapping here
1119 status = NT_STATUS_UNSUCCESSFUL;
1121 goto done;
1124 state.fn = fn;
1125 state.private_data = private_data;
1127 while (True) {
1129 status = NT_STATUS_OK;
1131 if (packet_handler(conn->pkt, ctdb_req_complete,
1132 ctdb_traverse_handler, &state, &status)) {
1134 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1135 status = NT_STATUS_OK;
1136 break;
1140 * There might be more in the queue
1142 continue;
1145 if (!NT_STATUS_IS_OK(status)) {
1146 break;
1149 status = packet_fd_read_sync(conn->pkt);
1151 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1153 * There might be more in the queue
1155 continue;
1158 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1159 status = NT_STATUS_OK;
1162 if (!NT_STATUS_IS_OK(status)) {
1163 DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1164 cluster_fatal("ctdbd died\n");
1168 done:
1169 TALLOC_FREE(conn);
1170 return status;
1174 * Register us as a server for a particular tcp connection
1177 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1178 const struct sockaddr_in *server,
1179 const struct sockaddr_in *client,
1180 void (*release_ip_handler)(const char *ip_addr,
1181 void *private_data),
1182 void *private_data)
1184 struct ctdb_control_tcp p;
1185 TDB_DATA data;
1186 NTSTATUS status;
1189 * Only one connection so far
1191 SMB_ASSERT(conn->release_ip_handler == NULL);
1193 conn->release_ip_handler = release_ip_handler;
1196 * We want to be told about IP releases
1199 status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1200 if (!NT_STATUS_IS_OK(status)) {
1201 return status;
1204 p.dest = *server;
1205 p.src = *client;
1208 * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1209 * can send an extra ack to trigger a reset for our client, so it
1210 * immediately reconnects
1212 data.dptr = (uint8_t *)&p;
1213 data.dsize = sizeof(p);
1215 return ctdbd_control(conn, CTDB_CURRENT_NODE,
1216 CTDB_CONTROL_TCP_CLIENT, 0,
1217 CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1221 * We want to handle reconfigure events
1223 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1225 return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1229 call a control on the local node
1231 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode,
1232 uint64_t srvid, uint32_t flags, TDB_DATA data,
1233 TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1234 int *cstatus)
1236 return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1239 #else
1241 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1242 struct ctdbd_connection **pconn)
1244 return NT_STATUS_NOT_IMPLEMENTED;
1247 #endif