4 Copyright (C) Andrew Tridgell 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/tdb/include/tdb.h"
23 #include "lib/events/events.h"
24 #include "lib/util/dlinklist.h"
25 #include "system/network.h"
26 #include "system/filesys.h"
27 #include "system/wait.h"
28 #include "../include/ctdb.h"
29 #include "../include/ctdb_private.h"
31 static void daemon_incoming_packet(void *, struct ctdb_req_header *);
34 handler for when a node changes its flags
36 static void flag_change_handler(struct ctdb_context *ctdb, uint64_t srvid,
37 TDB_DATA data, void *private_data)
39 struct ctdb_node_flag_change *c = (struct ctdb_node_flag_change *)data.dptr;
41 if (data.dsize != sizeof(*c) || !ctdb_validate_vnn(ctdb, c->vnn)) {
42 DEBUG(0,(__location__ "Invalid data in ctdb_node_flag_change\n"));
46 if (!ctdb_validate_vnn(ctdb, c->vnn)) {
47 DEBUG(0,("Bad vnn %u in flag_change_handler\n", c->vnn));
51 /* don't get the disconnected flag from the other node */
52 ctdb->nodes[c->vnn]->flags =
53 (ctdb->nodes[c->vnn]->flags&NODE_FLAGS_DISCONNECTED)
54 | (c->flags & ~NODE_FLAGS_DISCONNECTED);
55 DEBUG(2,("Node flags for node %u are now 0x%x\n", c->vnn, ctdb->nodes[c->vnn]->flags));
57 /* make sure we don't hold any IPs when we shouldn't */
58 if (c->vnn == ctdb->vnn &&
59 (ctdb->nodes[c->vnn]->flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_BANNED))) {
60 ctdb_release_all_ips(ctdb);
64 /* called when the "startup" event script has finished */
65 static void ctdb_start_transport(struct ctdb_context *ctdb, int status, void *p)
68 DEBUG(0,("startup event failed!\n"));
69 ctdb_fatal(ctdb, "startup event script failed");
72 /* start the transport running */
73 if (ctdb->methods->start(ctdb) != 0) {
74 DEBUG(0,("transport failed to start!\n"));
75 ctdb_fatal(ctdb, "transport failed to start");
78 /* start the recovery daemon process */
79 if (ctdb_start_recoverd(ctdb) != 0) {
80 DEBUG(0,("Failed to start recovery daemon\n"));
84 /* a handler for when nodes are disabled/enabled */
85 ctdb_register_message_handler(ctdb, ctdb, CTDB_SRVID_NODE_FLAGS_CHANGED,
86 flag_change_handler, NULL);
88 /* start monitoring for dead nodes */
89 ctdb_start_monitoring(ctdb);
92 /* go into main ctdb loop */
93 static void ctdb_main_loop(struct ctdb_context *ctdb)
97 if (strcmp(ctdb->transport, "tcp") == 0) {
98 int ctdb_tcp_init(struct ctdb_context *);
99 ret = ctdb_tcp_init(ctdb);
101 #ifdef USE_INFINIBAND
102 if (strcmp(ctdb->transport, "ib") == 0) {
103 int ctdb_ibw_init(struct ctdb_context *);
104 ret = ctdb_ibw_init(ctdb);
108 DEBUG(0,("Failed to initialise transport '%s'\n", ctdb->transport));
112 /* initialise the transport */
113 if (ctdb->methods->initialise(ctdb) != 0) {
114 DEBUG(0,("transport failed to initialise!\n"));
115 ctdb_fatal(ctdb, "transport failed to initialise");
118 /* tell all other nodes we've just started up */
119 ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL,
120 0, CTDB_CONTROL_STARTUP, 0,
121 CTDB_CTRL_FLAG_NOREPLY,
122 tdb_null, NULL, NULL);
124 /* release any IPs we hold from previous runs of the daemon */
125 ctdb_release_all_ips(ctdb);
127 ret = ctdb_event_script_callback(ctdb, timeval_zero(), ctdb,
128 ctdb_start_transport, NULL, "startup");
130 DEBUG(0,("Failed startup event script\n"));
134 /* go into a wait loop to allow other nodes to complete */
135 event_loop_wait(ctdb->ev);
137 DEBUG(0,("event_loop_wait() returned. this should not happen\n"));
142 static void block_signal(int signum)
144 struct sigaction act;
146 memset(&act, 0, sizeof(act));
148 act.sa_handler = SIG_IGN;
149 sigemptyset(&act.sa_mask);
150 sigaddset(&act.sa_mask, signum);
151 sigaction(signum, &act, NULL);
156 send a packet to a client
158 static int daemon_queue_send(struct ctdb_client *client, struct ctdb_req_header *hdr)
160 client->ctdb->statistics.client_packets_sent++;
161 return ctdb_queue_send(client->queue, (uint8_t *)hdr, hdr->length);
165 message handler for when we are in daemon mode. This redirects the message
168 static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
169 TDB_DATA data, void *private_data)
171 struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
172 struct ctdb_req_message *r;
175 /* construct a message to send to the client containing the data */
176 len = offsetof(struct ctdb_req_message, data) + data.dsize;
177 r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
178 len, struct ctdb_req_message);
179 CTDB_NO_MEMORY_VOID(ctdb, r);
181 talloc_set_name_const(r, "req_message packet");
184 r->datalen = data.dsize;
185 memcpy(&r->data[0], data.dptr, data.dsize);
187 daemon_queue_send(client, &r->hdr);
194 this is called when the ctdb daemon received a ctdb request to
195 set the srvid from the client
197 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
199 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
201 if (client == NULL) {
202 DEBUG(0,("Bad client_id in daemon_request_register_message_handler\n"));
205 res = ctdb_register_message_handler(ctdb, client, srvid, daemon_message_handler, client);
207 DEBUG(0,(__location__ " Failed to register handler %llu in daemon\n",
208 (unsigned long long)srvid));
210 DEBUG(2,(__location__ " Registered message handler for srvid=%llu\n",
211 (unsigned long long)srvid));
214 /* this is a hack for Samba - we now know the pid of the Samba client */
215 if ((srvid & 0xFFFFFFFF) == srvid &&
216 kill(srvid, 0) == 0) {
218 DEBUG(0,(__location__ " Registered PID %u for client %u\n",
219 (unsigned)client->pid, client_id));
225 this is called when the ctdb daemon received a ctdb request to
226 remove a srvid from the client
228 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
230 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
231 if (client == NULL) {
232 DEBUG(0,("Bad client_id in daemon_request_deregister_message_handler\n"));
235 return ctdb_deregister_message_handler(ctdb, srvid, client);
240 destroy a ctdb_client
242 static int ctdb_client_destructor(struct ctdb_client *client)
244 ctdb_takeover_client_destructor_hook(client);
245 ctdb_reqid_remove(client->ctdb, client->client_id);
246 client->ctdb->statistics.num_clients--;
252 this is called when the ctdb daemon received a ctdb request message
253 from a local client over the unix domain socket
255 static void daemon_request_message_from_client(struct ctdb_client *client,
256 struct ctdb_req_message *c)
261 /* maybe the message is for another client on this node */
262 if (ctdb_get_vnn(client->ctdb)==c->hdr.destnode) {
263 ctdb_request_message(client->ctdb, (struct ctdb_req_header *)c);
267 /* its for a remote node */
268 data.dptr = &c->data[0];
269 data.dsize = c->datalen;
270 res = ctdb_daemon_send_message(client->ctdb, c->hdr.destnode,
273 DEBUG(0,(__location__ " Failed to send message to remote node %u\n",
279 struct daemon_call_state {
280 struct ctdb_client *client;
282 struct ctdb_call *call;
283 struct timeval start_time;
287 complete a call from a client
289 static void daemon_call_from_client_callback(struct ctdb_call_state *state)
291 struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
292 struct daemon_call_state);
293 struct ctdb_reply_call *r;
296 struct ctdb_client *client = dstate->client;
298 talloc_steal(client, dstate);
299 talloc_steal(dstate, dstate->call);
301 res = ctdb_daemon_call_recv(state, dstate->call);
303 DEBUG(0, (__location__ " ctdbd_call_recv() returned error\n"));
304 client->ctdb->statistics.pending_calls--;
305 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
309 length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
310 r = ctdbd_allocate_pkt(client->ctdb, dstate, CTDB_REPLY_CALL,
311 length, struct ctdb_reply_call);
313 DEBUG(0, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
314 client->ctdb->statistics.pending_calls--;
315 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
318 r->hdr.reqid = dstate->reqid;
319 r->datalen = dstate->call->reply_data.dsize;
320 memcpy(&r->data[0], dstate->call->reply_data.dptr, r->datalen);
322 res = daemon_queue_send(client, &r->hdr);
324 DEBUG(0, (__location__ " Failed to queue packet from daemon to client\n"));
326 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
328 client->ctdb->statistics.pending_calls--;
332 static void daemon_request_call_from_client(struct ctdb_client *client,
333 struct ctdb_req_call *c);
336 this is called when the ctdb daemon received a ctdb request call
337 from a local client over the unix domain socket
339 static void daemon_request_call_from_client(struct ctdb_client *client,
340 struct ctdb_req_call *c)
342 struct ctdb_call_state *state;
343 struct ctdb_db_context *ctdb_db;
344 struct daemon_call_state *dstate;
345 struct ctdb_call *call;
346 struct ctdb_ltdb_header header;
349 struct ctdb_context *ctdb = client->ctdb;
351 ctdb->statistics.total_calls++;
352 ctdb->statistics.pending_calls++;
354 ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
356 DEBUG(0, (__location__ " Unknown database in request. db_id==0x%08x",
358 ctdb->statistics.pending_calls--;
363 key.dsize = c->keylen;
365 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
366 (struct ctdb_req_header *)c, &data,
367 daemon_incoming_packet, client, True);
369 /* will retry later */
370 ctdb->statistics.pending_calls--;
375 DEBUG(0,(__location__ " Unable to fetch record\n"));
376 ctdb->statistics.pending_calls--;
380 dstate = talloc(client, struct daemon_call_state);
381 if (dstate == NULL) {
382 ctdb_ltdb_unlock(ctdb_db, key);
383 DEBUG(0,(__location__ " Unable to allocate dstate\n"));
384 ctdb->statistics.pending_calls--;
387 dstate->start_time = timeval_current();
388 dstate->client = client;
389 dstate->reqid = c->hdr.reqid;
390 talloc_steal(dstate, data.dptr);
392 call = dstate->call = talloc_zero(dstate, struct ctdb_call);
394 ctdb_ltdb_unlock(ctdb_db, key);
395 DEBUG(0,(__location__ " Unable to allocate call\n"));
396 ctdb->statistics.pending_calls--;
397 ctdb_latency(&ctdb->statistics.max_call_latency, dstate->start_time);
401 call->call_id = c->callid;
403 call->call_data.dptr = c->data + c->keylen;
404 call->call_data.dsize = c->calldatalen;
405 call->flags = c->flags;
407 if (header.dmaster == ctdb->vnn) {
408 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
410 state = ctdb_daemon_call_send_remote(ctdb_db, call, &header);
413 ctdb_ltdb_unlock(ctdb_db, key);
416 DEBUG(0,(__location__ " Unable to setup call send\n"));
417 ctdb->statistics.pending_calls--;
418 ctdb_latency(&ctdb->statistics.max_call_latency, dstate->start_time);
421 talloc_steal(state, dstate);
422 talloc_steal(client, state);
424 state->async.fn = daemon_call_from_client_callback;
425 state->async.private_data = dstate;
429 static void daemon_request_control_from_client(struct ctdb_client *client,
430 struct ctdb_req_control *c);
432 /* data contains a packet from the client */
433 static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
435 struct ctdb_client *client = talloc_get_type(p, struct ctdb_client);
437 struct ctdb_context *ctdb = client->ctdb;
439 /* place the packet as a child of a tmp_ctx. We then use
440 talloc_free() below to free it. If any of the calls want
441 to keep it, then they will steal it somewhere else, and the
442 talloc_free() will be a no-op */
443 tmp_ctx = talloc_new(client);
444 talloc_steal(tmp_ctx, hdr);
446 if (hdr->ctdb_magic != CTDB_MAGIC) {
447 ctdb_set_error(client->ctdb, "Non CTDB packet rejected in daemon\n");
451 if (hdr->ctdb_version != CTDB_VERSION) {
452 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
456 switch (hdr->operation) {
458 ctdb->statistics.client.req_call++;
459 daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
462 case CTDB_REQ_MESSAGE:
463 ctdb->statistics.client.req_message++;
464 daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
467 case CTDB_REQ_CONTROL:
468 ctdb->statistics.client.req_control++;
469 daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
473 DEBUG(0,(__location__ " daemon: unrecognized operation %u\n",
478 talloc_free(tmp_ctx);
482 called when the daemon gets a incoming packet
484 static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
486 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
487 struct ctdb_req_header *hdr;
494 client->ctdb->statistics.client_packets_recv++;
496 if (cnt < sizeof(*hdr)) {
497 ctdb_set_error(client->ctdb, "Bad packet length %u in daemon\n",
501 hdr = (struct ctdb_req_header *)data;
502 if (cnt != hdr->length) {
503 ctdb_set_error(client->ctdb, "Bad header length %u expected %u\n in daemon",
504 (unsigned)hdr->length, (unsigned)cnt);
508 if (hdr->ctdb_magic != CTDB_MAGIC) {
509 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
513 if (hdr->ctdb_version != CTDB_VERSION) {
514 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
518 DEBUG(3,(__location__ " client request %u of type %u length %u from "
519 "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
520 hdr->srcnode, hdr->destnode));
522 /* it is the responsibility of the incoming packet function to free 'data' */
523 daemon_incoming_packet(client, hdr);
526 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
527 uint16_t flags, void *private_data)
529 struct sockaddr_in addr;
532 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
533 struct ctdb_client *client;
535 memset(&addr, 0, sizeof(addr));
537 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
543 set_close_on_exec(fd);
545 client = talloc_zero(ctdb, struct ctdb_client);
548 client->client_id = ctdb_reqid_new(ctdb, client);
549 ctdb->statistics.num_clients++;
551 client->queue = ctdb_queue_setup(ctdb, client, fd, CTDB_DS_ALIGNMENT,
552 ctdb_daemon_read_cb, client);
554 talloc_set_destructor(client, ctdb_client_destructor);
560 create a unix domain socket and bind it
561 return a file descriptor open on the socket
563 static int ux_socket_bind(struct ctdb_context *ctdb)
565 struct sockaddr_un addr;
567 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
568 if (ctdb->daemon.sd == -1) {
572 set_nonblocking(ctdb->daemon.sd);
573 set_close_on_exec(ctdb->daemon.sd);
576 /* AIX doesn't like this :( */
577 if (fchown(ctdb->daemon.sd, geteuid(), getegid()) != 0 ||
578 fchmod(ctdb->daemon.sd, 0700) != 0) {
579 DEBUG(0,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n"));
584 set_nonblocking(ctdb->daemon.sd);
586 memset(&addr, 0, sizeof(addr));
587 addr.sun_family = AF_UNIX;
588 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
590 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
591 DEBUG(0,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
594 if (listen(ctdb->daemon.sd, 10) != 0) {
595 DEBUG(0,("Unable to listen on ctdb socket '%s'\n", ctdb->daemon.name));
602 close(ctdb->daemon.sd);
603 ctdb->daemon.sd = -1;
608 delete the socket on exit - called on destruction of autofree context
610 static int unlink_destructor(const char *name)
618 start the protocol going as a daemon
620 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
623 struct fd_event *fde;
624 const char *domain_socket_name;
626 /* get rid of any old sockets */
627 unlink(ctdb->daemon.name);
629 /* create a unix domain stream socket to listen to */
630 res = ux_socket_bind(ctdb);
632 DEBUG(0,(__location__ " Failed to open CTDB unix domain socket\n"));
636 if (do_fork && fork()) {
640 tdb_reopen_all(False);
645 block_signal(SIGPIPE);
647 /* try to set us up as realtime */
648 ctdb_set_realtime(true);
650 /* ensure the socket is deleted on exit of the daemon */
651 domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
652 talloc_set_destructor(domain_socket_name, unlink_destructor);
654 ctdb->ev = event_context_init(NULL);
656 /* start frozen, then let the first election sort things out */
657 if (!ctdb_blocking_freeze(ctdb)) {
658 DEBUG(0,("Failed to get initial freeze\n"));
662 /* force initial recovery for election */
663 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
665 /* now start accepting clients, only can do this once frozen */
666 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd,
667 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
668 ctdb_accept_client, ctdb);
670 ctdb_main_loop(ctdb);
676 allocate a packet for use in daemon<->daemon communication
678 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
680 enum ctdb_operation operation,
681 size_t length, size_t slength,
685 struct ctdb_req_header *hdr;
687 length = MAX(length, slength);
688 size = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
690 hdr = (struct ctdb_req_header *)ctdb->methods->allocate_pkt(mem_ctx, size);
692 DEBUG(0,("Unable to allocate transport packet for operation %u of length %u\n",
693 operation, (unsigned)length));
696 talloc_set_name_const(hdr, type);
697 memset(hdr, 0, slength);
698 hdr->length = length;
699 hdr->operation = operation;
700 hdr->ctdb_magic = CTDB_MAGIC;
701 hdr->ctdb_version = CTDB_VERSION;
702 hdr->generation = ctdb->vnn_map->generation;
703 hdr->srcnode = ctdb->vnn;
708 struct daemon_control_state {
709 struct daemon_control_state *next, *prev;
710 struct ctdb_client *client;
711 struct ctdb_req_control *c;
713 struct ctdb_node *node;
717 callback when a control reply comes in
719 static void daemon_control_callback(struct ctdb_context *ctdb,
720 int32_t status, TDB_DATA data,
721 const char *errormsg,
724 struct daemon_control_state *state = talloc_get_type(private_data,
725 struct daemon_control_state);
726 struct ctdb_client *client = state->client;
727 struct ctdb_reply_control *r;
730 /* construct a message to send to the client containing the data */
731 len = offsetof(struct ctdb_reply_control, data) + data.dsize;
733 len += strlen(errormsg);
735 r = ctdbd_allocate_pkt(ctdb, state, CTDB_REPLY_CONTROL, len,
736 struct ctdb_reply_control);
737 CTDB_NO_MEMORY_VOID(ctdb, r);
739 r->hdr.reqid = state->reqid;
741 r->datalen = data.dsize;
743 memcpy(&r->data[0], data.dptr, data.dsize);
745 r->errorlen = strlen(errormsg);
746 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
749 daemon_queue_send(client, &r->hdr);
755 fail all pending controls to a disconnected node
757 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node)
759 struct daemon_control_state *state;
760 while ((state = node->pending_controls)) {
761 DLIST_REMOVE(node->pending_controls, state);
762 daemon_control_callback(ctdb, (uint32_t)-1, tdb_null,
763 "node is disconnected", state);
768 destroy a daemon_control_state
770 static int daemon_control_destructor(struct daemon_control_state *state)
773 DLIST_REMOVE(state->node->pending_controls, state);
779 this is called when the ctdb daemon received a ctdb request control
780 from a local client over the unix domain socket
782 static void daemon_request_control_from_client(struct ctdb_client *client,
783 struct ctdb_req_control *c)
787 struct daemon_control_state *state;
788 TALLOC_CTX *tmp_ctx = talloc_new(client);
790 if (c->hdr.destnode == CTDB_CURRENT_NODE) {
791 c->hdr.destnode = client->ctdb->vnn;
794 state = talloc(client, struct daemon_control_state);
795 CTDB_NO_MEMORY_VOID(client->ctdb, state);
797 state->client = client;
798 state->c = talloc_steal(state, c);
799 state->reqid = c->hdr.reqid;
800 if (ctdb_validate_vnn(client->ctdb, c->hdr.destnode)) {
801 state->node = client->ctdb->nodes[c->hdr.destnode];
802 DLIST_ADD(state->node->pending_controls, state);
807 talloc_set_destructor(state, daemon_control_destructor);
809 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
810 talloc_steal(tmp_ctx, state);
813 data.dptr = &c->data[0];
814 data.dsize = c->datalen;
815 res = ctdb_daemon_send_control(client->ctdb, c->hdr.destnode,
816 c->srvid, c->opcode, client->client_id,
818 data, daemon_control_callback,
821 DEBUG(0,(__location__ " Failed to send control to remote node %u\n",
825 talloc_free(tmp_ctx);
829 register a call function
831 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
832 ctdb_fn_t fn, int id)
834 struct ctdb_registered_call *call;
835 struct ctdb_db_context *ctdb_db;
837 ctdb_db = find_ctdb_db(ctdb, db_id);
838 if (ctdb_db == NULL) {
842 call = talloc(ctdb_db, struct ctdb_registered_call);
846 DLIST_ADD(ctdb_db->calls, call);
853 this local messaging handler is ugly, but is needed to prevent
854 recursion in ctdb_send_message() when the destination node is the
855 same as the source node
857 struct ctdb_local_message {
858 struct ctdb_context *ctdb;
863 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
864 struct timeval t, void *private_data)
866 struct ctdb_local_message *m = talloc_get_type(private_data,
867 struct ctdb_local_message);
870 res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
872 DEBUG(0, (__location__ " Failed to dispatch message for srvid=%llu\n",
873 (unsigned long long)m->srvid));
878 static int ctdb_local_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
880 struct ctdb_local_message *m;
881 m = talloc(ctdb, struct ctdb_local_message);
882 CTDB_NO_MEMORY(ctdb, m);
887 m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
888 if (m->data.dptr == NULL) {
893 /* this needs to be done as an event to prevent recursion */
894 event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
901 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
902 uint64_t srvid, TDB_DATA data)
904 struct ctdb_req_message *r;
907 /* see if this is a message to ourselves */
908 if (vnn == ctdb->vnn) {
909 return ctdb_local_message(ctdb, srvid, data);
912 len = offsetof(struct ctdb_req_message, data) + data.dsize;
913 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_MESSAGE, len,
914 struct ctdb_req_message);
915 CTDB_NO_MEMORY(ctdb, r);
917 r->hdr.destnode = vnn;
919 r->datalen = data.dsize;
920 memcpy(&r->data[0], data.dptr, data.dsize);
922 ctdb_queue_packet(ctdb, &r->hdr);