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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "lib/tdb/include/tdb.h"
24 #include "lib/events/events.h"
25 #include "lib/util/dlinklist.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28 #include "system/wait.h"
29 #include "../include/ctdb.h"
30 #include "../include/ctdb_private.h"
32 static void daemon_incoming_packet(void *, struct ctdb_req_header *);
35 handler for when a node changes its flags
37 static void flag_change_handler(struct ctdb_context *ctdb, uint64_t srvid,
38 TDB_DATA data, void *private_data)
40 struct ctdb_node_flag_change *c = (struct ctdb_node_flag_change *)data.dptr;
42 if (data.dsize != sizeof(*c) || !ctdb_validate_vnn(ctdb, c->vnn)) {
43 DEBUG(0,(__location__ "Invalid data in ctdb_node_flag_change\n"));
47 if (!ctdb_validate_vnn(ctdb, c->vnn)) {
48 DEBUG(0,("Bad vnn %u in flag_change_handler\n", c->vnn));
52 /* don't get the disconnected flag from the other node */
53 ctdb->nodes[c->vnn]->flags =
54 (ctdb->nodes[c->vnn]->flags&NODE_FLAGS_DISCONNECTED)
55 | (c->flags & ~NODE_FLAGS_DISCONNECTED);
56 DEBUG(2,("Node flags for node %u are now 0x%x\n", c->vnn, ctdb->nodes[c->vnn]->flags));
58 /* make sure we don't hold any IPs when we shouldn't */
59 if (c->vnn == ctdb->vnn &&
60 (ctdb->nodes[c->vnn]->flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_BANNED))) {
61 ctdb_release_all_ips(ctdb);
65 /* called when the "startup" event script has finished */
66 static void ctdb_start_transport(struct ctdb_context *ctdb, int status, void *p)
69 DEBUG(0,("startup event failed!\n"));
70 ctdb_fatal(ctdb, "startup event script failed");
73 /* start the transport running */
74 if (ctdb->methods->start(ctdb) != 0) {
75 DEBUG(0,("transport failed to start!\n"));
76 ctdb_fatal(ctdb, "transport failed to start");
79 /* start the recovery daemon process */
80 if (ctdb_start_recoverd(ctdb) != 0) {
81 DEBUG(0,("Failed to start recovery daemon\n"));
85 /* a handler for when nodes are disabled/enabled */
86 ctdb_register_message_handler(ctdb, ctdb, CTDB_SRVID_NODE_FLAGS_CHANGED,
87 flag_change_handler, NULL);
89 /* start monitoring for dead nodes */
90 ctdb_start_monitoring(ctdb);
93 /* go into main ctdb loop */
94 static void ctdb_main_loop(struct ctdb_context *ctdb)
98 if (strcmp(ctdb->transport, "tcp") == 0) {
99 int ctdb_tcp_init(struct ctdb_context *);
100 ret = ctdb_tcp_init(ctdb);
102 #ifdef USE_INFINIBAND
103 if (strcmp(ctdb->transport, "ib") == 0) {
104 int ctdb_ibw_init(struct ctdb_context *);
105 ret = ctdb_ibw_init(ctdb);
109 DEBUG(0,("Failed to initialise transport '%s'\n", ctdb->transport));
113 /* initialise the transport */
114 if (ctdb->methods->initialise(ctdb) != 0) {
115 DEBUG(0,("transport failed to initialise!\n"));
116 ctdb_fatal(ctdb, "transport failed to initialise");
119 /* tell all other nodes we've just started up */
120 ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL,
121 0, CTDB_CONTROL_STARTUP, 0,
122 CTDB_CTRL_FLAG_NOREPLY,
123 tdb_null, NULL, NULL);
125 /* release any IPs we hold from previous runs of the daemon */
126 ctdb_release_all_ips(ctdb);
128 ret = ctdb_event_script_callback(ctdb, timeval_zero(), ctdb,
129 ctdb_start_transport, NULL, "startup");
131 DEBUG(0,("Failed startup event script\n"));
135 /* go into a wait loop to allow other nodes to complete */
136 event_loop_wait(ctdb->ev);
138 DEBUG(0,("event_loop_wait() returned. this should not happen\n"));
143 static void block_signal(int signum)
145 struct sigaction act;
147 memset(&act, 0, sizeof(act));
149 act.sa_handler = SIG_IGN;
150 sigemptyset(&act.sa_mask);
151 sigaddset(&act.sa_mask, signum);
152 sigaction(signum, &act, NULL);
157 send a packet to a client
159 static int daemon_queue_send(struct ctdb_client *client, struct ctdb_req_header *hdr)
161 client->ctdb->statistics.client_packets_sent++;
162 return ctdb_queue_send(client->queue, (uint8_t *)hdr, hdr->length);
166 message handler for when we are in daemon mode. This redirects the message
169 static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
170 TDB_DATA data, void *private_data)
172 struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
173 struct ctdb_req_message *r;
176 /* construct a message to send to the client containing the data */
177 len = offsetof(struct ctdb_req_message, data) + data.dsize;
178 r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
179 len, struct ctdb_req_message);
180 CTDB_NO_MEMORY_VOID(ctdb, r);
182 talloc_set_name_const(r, "req_message packet");
185 r->datalen = data.dsize;
186 memcpy(&r->data[0], data.dptr, data.dsize);
188 daemon_queue_send(client, &r->hdr);
195 this is called when the ctdb daemon received a ctdb request to
196 set the srvid from the client
198 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
200 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
202 if (client == NULL) {
203 DEBUG(0,("Bad client_id in daemon_request_register_message_handler\n"));
206 res = ctdb_register_message_handler(ctdb, client, srvid, daemon_message_handler, client);
208 DEBUG(0,(__location__ " Failed to register handler %llu in daemon\n",
209 (unsigned long long)srvid));
211 DEBUG(2,(__location__ " Registered message handler for srvid=%llu\n",
212 (unsigned long long)srvid));
215 /* this is a hack for Samba - we now know the pid of the Samba client */
216 if ((srvid & 0xFFFFFFFF) == srvid &&
217 kill(srvid, 0) == 0) {
219 DEBUG(0,(__location__ " Registered PID %u for client %u\n",
220 (unsigned)client->pid, client_id));
226 this is called when the ctdb daemon received a ctdb request to
227 remove a srvid from the client
229 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
231 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
232 if (client == NULL) {
233 DEBUG(0,("Bad client_id in daemon_request_deregister_message_handler\n"));
236 return ctdb_deregister_message_handler(ctdb, srvid, client);
241 destroy a ctdb_client
243 static int ctdb_client_destructor(struct ctdb_client *client)
245 ctdb_takeover_client_destructor_hook(client);
246 ctdb_reqid_remove(client->ctdb, client->client_id);
247 client->ctdb->statistics.num_clients--;
253 this is called when the ctdb daemon received a ctdb request message
254 from a local client over the unix domain socket
256 static void daemon_request_message_from_client(struct ctdb_client *client,
257 struct ctdb_req_message *c)
262 /* maybe the message is for another client on this node */
263 if (ctdb_get_vnn(client->ctdb)==c->hdr.destnode) {
264 ctdb_request_message(client->ctdb, (struct ctdb_req_header *)c);
268 /* its for a remote node */
269 data.dptr = &c->data[0];
270 data.dsize = c->datalen;
271 res = ctdb_daemon_send_message(client->ctdb, c->hdr.destnode,
274 DEBUG(0,(__location__ " Failed to send message to remote node %u\n",
280 struct daemon_call_state {
281 struct ctdb_client *client;
283 struct ctdb_call *call;
284 struct timeval start_time;
288 complete a call from a client
290 static void daemon_call_from_client_callback(struct ctdb_call_state *state)
292 struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
293 struct daemon_call_state);
294 struct ctdb_reply_call *r;
297 struct ctdb_client *client = dstate->client;
299 talloc_steal(client, dstate);
300 talloc_steal(dstate, dstate->call);
302 res = ctdb_daemon_call_recv(state, dstate->call);
304 DEBUG(0, (__location__ " ctdbd_call_recv() returned error\n"));
305 client->ctdb->statistics.pending_calls--;
306 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
310 length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
311 r = ctdbd_allocate_pkt(client->ctdb, dstate, CTDB_REPLY_CALL,
312 length, struct ctdb_reply_call);
314 DEBUG(0, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
315 client->ctdb->statistics.pending_calls--;
316 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
319 r->hdr.reqid = dstate->reqid;
320 r->datalen = dstate->call->reply_data.dsize;
321 memcpy(&r->data[0], dstate->call->reply_data.dptr, r->datalen);
323 res = daemon_queue_send(client, &r->hdr);
325 DEBUG(0, (__location__ " Failed to queue packet from daemon to client\n"));
327 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
329 client->ctdb->statistics.pending_calls--;
333 static void daemon_request_call_from_client(struct ctdb_client *client,
334 struct ctdb_req_call *c);
337 this is called when the ctdb daemon received a ctdb request call
338 from a local client over the unix domain socket
340 static void daemon_request_call_from_client(struct ctdb_client *client,
341 struct ctdb_req_call *c)
343 struct ctdb_call_state *state;
344 struct ctdb_db_context *ctdb_db;
345 struct daemon_call_state *dstate;
346 struct ctdb_call *call;
347 struct ctdb_ltdb_header header;
350 struct ctdb_context *ctdb = client->ctdb;
352 ctdb->statistics.total_calls++;
353 ctdb->statistics.pending_calls++;
355 ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
357 DEBUG(0, (__location__ " Unknown database in request. db_id==0x%08x",
359 ctdb->statistics.pending_calls--;
364 key.dsize = c->keylen;
366 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
367 (struct ctdb_req_header *)c, &data,
368 daemon_incoming_packet, client, True);
370 /* will retry later */
371 ctdb->statistics.pending_calls--;
376 DEBUG(0,(__location__ " Unable to fetch record\n"));
377 ctdb->statistics.pending_calls--;
381 dstate = talloc(client, struct daemon_call_state);
382 if (dstate == NULL) {
383 ctdb_ltdb_unlock(ctdb_db, key);
384 DEBUG(0,(__location__ " Unable to allocate dstate\n"));
385 ctdb->statistics.pending_calls--;
388 dstate->start_time = timeval_current();
389 dstate->client = client;
390 dstate->reqid = c->hdr.reqid;
391 talloc_steal(dstate, data.dptr);
393 call = dstate->call = talloc_zero(dstate, struct ctdb_call);
395 ctdb_ltdb_unlock(ctdb_db, key);
396 DEBUG(0,(__location__ " Unable to allocate call\n"));
397 ctdb->statistics.pending_calls--;
398 ctdb_latency(&ctdb->statistics.max_call_latency, dstate->start_time);
402 call->call_id = c->callid;
404 call->call_data.dptr = c->data + c->keylen;
405 call->call_data.dsize = c->calldatalen;
406 call->flags = c->flags;
408 if (header.dmaster == ctdb->vnn) {
409 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
411 state = ctdb_daemon_call_send_remote(ctdb_db, call, &header);
414 ctdb_ltdb_unlock(ctdb_db, key);
417 DEBUG(0,(__location__ " Unable to setup call send\n"));
418 ctdb->statistics.pending_calls--;
419 ctdb_latency(&ctdb->statistics.max_call_latency, dstate->start_time);
422 talloc_steal(state, dstate);
423 talloc_steal(client, state);
425 state->async.fn = daemon_call_from_client_callback;
426 state->async.private_data = dstate;
430 static void daemon_request_control_from_client(struct ctdb_client *client,
431 struct ctdb_req_control *c);
433 /* data contains a packet from the client */
434 static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
436 struct ctdb_client *client = talloc_get_type(p, struct ctdb_client);
438 struct ctdb_context *ctdb = client->ctdb;
440 /* place the packet as a child of a tmp_ctx. We then use
441 talloc_free() below to free it. If any of the calls want
442 to keep it, then they will steal it somewhere else, and the
443 talloc_free() will be a no-op */
444 tmp_ctx = talloc_new(client);
445 talloc_steal(tmp_ctx, hdr);
447 if (hdr->ctdb_magic != CTDB_MAGIC) {
448 ctdb_set_error(client->ctdb, "Non CTDB packet rejected in daemon\n");
452 if (hdr->ctdb_version != CTDB_VERSION) {
453 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
457 switch (hdr->operation) {
459 ctdb->statistics.client.req_call++;
460 daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
463 case CTDB_REQ_MESSAGE:
464 ctdb->statistics.client.req_message++;
465 daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
468 case CTDB_REQ_CONTROL:
469 ctdb->statistics.client.req_control++;
470 daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
474 DEBUG(0,(__location__ " daemon: unrecognized operation %u\n",
479 talloc_free(tmp_ctx);
483 called when the daemon gets a incoming packet
485 static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
487 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
488 struct ctdb_req_header *hdr;
495 client->ctdb->statistics.client_packets_recv++;
497 if (cnt < sizeof(*hdr)) {
498 ctdb_set_error(client->ctdb, "Bad packet length %u in daemon\n",
502 hdr = (struct ctdb_req_header *)data;
503 if (cnt != hdr->length) {
504 ctdb_set_error(client->ctdb, "Bad header length %u expected %u\n in daemon",
505 (unsigned)hdr->length, (unsigned)cnt);
509 if (hdr->ctdb_magic != CTDB_MAGIC) {
510 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
514 if (hdr->ctdb_version != CTDB_VERSION) {
515 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
519 DEBUG(3,(__location__ " client request %u of type %u length %u from "
520 "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
521 hdr->srcnode, hdr->destnode));
523 /* it is the responsibility of the incoming packet function to free 'data' */
524 daemon_incoming_packet(client, hdr);
527 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
528 uint16_t flags, void *private_data)
530 struct sockaddr_in addr;
533 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
534 struct ctdb_client *client;
536 memset(&addr, 0, sizeof(addr));
538 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
544 set_close_on_exec(fd);
546 client = talloc_zero(ctdb, struct ctdb_client);
549 client->client_id = ctdb_reqid_new(ctdb, client);
550 ctdb->statistics.num_clients++;
552 client->queue = ctdb_queue_setup(ctdb, client, fd, CTDB_DS_ALIGNMENT,
553 ctdb_daemon_read_cb, client);
555 talloc_set_destructor(client, ctdb_client_destructor);
561 create a unix domain socket and bind it
562 return a file descriptor open on the socket
564 static int ux_socket_bind(struct ctdb_context *ctdb)
566 struct sockaddr_un addr;
568 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
569 if (ctdb->daemon.sd == -1) {
573 set_nonblocking(ctdb->daemon.sd);
574 set_close_on_exec(ctdb->daemon.sd);
577 /* AIX doesn't like this :( */
578 if (fchown(ctdb->daemon.sd, geteuid(), getegid()) != 0 ||
579 fchmod(ctdb->daemon.sd, 0700) != 0) {
580 DEBUG(0,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n"));
585 set_nonblocking(ctdb->daemon.sd);
587 memset(&addr, 0, sizeof(addr));
588 addr.sun_family = AF_UNIX;
589 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
591 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
592 DEBUG(0,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
595 if (listen(ctdb->daemon.sd, 10) != 0) {
596 DEBUG(0,("Unable to listen on ctdb socket '%s'\n", ctdb->daemon.name));
603 close(ctdb->daemon.sd);
604 ctdb->daemon.sd = -1;
609 delete the socket on exit - called on destruction of autofree context
611 static int unlink_destructor(const char *name)
619 start the protocol going as a daemon
621 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
624 struct fd_event *fde;
625 const char *domain_socket_name;
627 /* get rid of any old sockets */
628 unlink(ctdb->daemon.name);
630 /* create a unix domain stream socket to listen to */
631 res = ux_socket_bind(ctdb);
633 DEBUG(0,(__location__ " Failed to open CTDB unix domain socket\n"));
637 if (do_fork && fork()) {
641 tdb_reopen_all(False);
646 block_signal(SIGPIPE);
648 /* try to set us up as realtime */
649 ctdb_set_realtime(true);
651 /* ensure the socket is deleted on exit of the daemon */
652 domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
653 talloc_set_destructor(domain_socket_name, unlink_destructor);
655 ctdb->ev = event_context_init(NULL);
657 /* start frozen, then let the first election sort things out */
658 if (!ctdb_blocking_freeze(ctdb)) {
659 DEBUG(0,("Failed to get initial freeze\n"));
663 /* force initial recovery for election */
664 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
666 /* now start accepting clients, only can do this once frozen */
667 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd,
668 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
669 ctdb_accept_client, ctdb);
671 ctdb_main_loop(ctdb);
677 allocate a packet for use in daemon<->daemon communication
679 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
681 enum ctdb_operation operation,
682 size_t length, size_t slength,
686 struct ctdb_req_header *hdr;
688 length = MAX(length, slength);
689 size = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
691 hdr = (struct ctdb_req_header *)ctdb->methods->allocate_pkt(mem_ctx, size);
693 DEBUG(0,("Unable to allocate transport packet for operation %u of length %u\n",
694 operation, (unsigned)length));
697 talloc_set_name_const(hdr, type);
698 memset(hdr, 0, slength);
699 hdr->length = length;
700 hdr->operation = operation;
701 hdr->ctdb_magic = CTDB_MAGIC;
702 hdr->ctdb_version = CTDB_VERSION;
703 hdr->generation = ctdb->vnn_map->generation;
704 hdr->srcnode = ctdb->vnn;
709 struct daemon_control_state {
710 struct daemon_control_state *next, *prev;
711 struct ctdb_client *client;
712 struct ctdb_req_control *c;
714 struct ctdb_node *node;
718 callback when a control reply comes in
720 static void daemon_control_callback(struct ctdb_context *ctdb,
721 int32_t status, TDB_DATA data,
722 const char *errormsg,
725 struct daemon_control_state *state = talloc_get_type(private_data,
726 struct daemon_control_state);
727 struct ctdb_client *client = state->client;
728 struct ctdb_reply_control *r;
731 /* construct a message to send to the client containing the data */
732 len = offsetof(struct ctdb_reply_control, data) + data.dsize;
734 len += strlen(errormsg);
736 r = ctdbd_allocate_pkt(ctdb, state, CTDB_REPLY_CONTROL, len,
737 struct ctdb_reply_control);
738 CTDB_NO_MEMORY_VOID(ctdb, r);
740 r->hdr.reqid = state->reqid;
742 r->datalen = data.dsize;
744 memcpy(&r->data[0], data.dptr, data.dsize);
746 r->errorlen = strlen(errormsg);
747 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
750 daemon_queue_send(client, &r->hdr);
756 fail all pending controls to a disconnected node
758 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node)
760 struct daemon_control_state *state;
761 while ((state = node->pending_controls)) {
762 DLIST_REMOVE(node->pending_controls, state);
763 daemon_control_callback(ctdb, (uint32_t)-1, tdb_null,
764 "node is disconnected", state);
769 destroy a daemon_control_state
771 static int daemon_control_destructor(struct daemon_control_state *state)
774 DLIST_REMOVE(state->node->pending_controls, state);
780 this is called when the ctdb daemon received a ctdb request control
781 from a local client over the unix domain socket
783 static void daemon_request_control_from_client(struct ctdb_client *client,
784 struct ctdb_req_control *c)
788 struct daemon_control_state *state;
789 TALLOC_CTX *tmp_ctx = talloc_new(client);
791 if (c->hdr.destnode == CTDB_CURRENT_NODE) {
792 c->hdr.destnode = client->ctdb->vnn;
795 state = talloc(client, struct daemon_control_state);
796 CTDB_NO_MEMORY_VOID(client->ctdb, state);
798 state->client = client;
799 state->c = talloc_steal(state, c);
800 state->reqid = c->hdr.reqid;
801 if (ctdb_validate_vnn(client->ctdb, c->hdr.destnode)) {
802 state->node = client->ctdb->nodes[c->hdr.destnode];
803 DLIST_ADD(state->node->pending_controls, state);
808 talloc_set_destructor(state, daemon_control_destructor);
810 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
811 talloc_steal(tmp_ctx, state);
814 data.dptr = &c->data[0];
815 data.dsize = c->datalen;
816 res = ctdb_daemon_send_control(client->ctdb, c->hdr.destnode,
817 c->srvid, c->opcode, client->client_id,
819 data, daemon_control_callback,
822 DEBUG(0,(__location__ " Failed to send control to remote node %u\n",
826 talloc_free(tmp_ctx);
830 register a call function
832 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
833 ctdb_fn_t fn, int id)
835 struct ctdb_registered_call *call;
836 struct ctdb_db_context *ctdb_db;
838 ctdb_db = find_ctdb_db(ctdb, db_id);
839 if (ctdb_db == NULL) {
843 call = talloc(ctdb_db, struct ctdb_registered_call);
847 DLIST_ADD(ctdb_db->calls, call);
854 this local messaging handler is ugly, but is needed to prevent
855 recursion in ctdb_send_message() when the destination node is the
856 same as the source node
858 struct ctdb_local_message {
859 struct ctdb_context *ctdb;
864 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
865 struct timeval t, void *private_data)
867 struct ctdb_local_message *m = talloc_get_type(private_data,
868 struct ctdb_local_message);
871 res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
873 DEBUG(0, (__location__ " Failed to dispatch message for srvid=%llu\n",
874 (unsigned long long)m->srvid));
879 static int ctdb_local_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
881 struct ctdb_local_message *m;
882 m = talloc(ctdb, struct ctdb_local_message);
883 CTDB_NO_MEMORY(ctdb, m);
888 m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
889 if (m->data.dptr == NULL) {
894 /* this needs to be done as an event to prevent recursion */
895 event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
902 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t vnn,
903 uint64_t srvid, TDB_DATA data)
905 struct ctdb_req_message *r;
908 /* see if this is a message to ourselves */
909 if (vnn == ctdb->vnn) {
910 return ctdb_local_message(ctdb, srvid, data);
913 len = offsetof(struct ctdb_req_message, data) + data.dsize;
914 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_MESSAGE, len,
915 struct ctdb_req_message);
916 CTDB_NO_MEMORY(ctdb, r);
918 r->hdr.destnode = vnn;
920 r->datalen = data.dsize;
921 memcpy(&r->data[0], data.dptr, data.dsize);
923 ctdb_queue_packet(ctdb, &r->hdr);