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_pnn(ctdb, c->pnn)) {
42 DEBUG(0,(__location__ "Invalid data in ctdb_node_flag_change\n"));
46 if (!ctdb_validate_pnn(ctdb, c->pnn)) {
47 DEBUG(0,("Bad pnn %u in flag_change_handler\n", c->pnn));
51 /* don't get the disconnected flag from the other node */
52 ctdb->nodes[c->pnn]->flags =
53 (ctdb->nodes[c->pnn]->flags&NODE_FLAGS_DISCONNECTED)
54 | (c->new_flags & ~NODE_FLAGS_DISCONNECTED);
55 DEBUG(2,("Node flags for node %u are now 0x%x\n", c->pnn, ctdb->nodes[c->pnn]->flags));
57 /* make sure we don't hold any IPs when we shouldn't */
58 if (c->pnn == ctdb->pnn &&
59 (ctdb->nodes[c->pnn]->flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_BANNED))) {
60 ctdb_release_all_ips(ctdb);
64 static void print_exit_message(void)
66 DEBUG(0,("CTDB daemon shutting down\n"));
70 /* called when the "startup" event script has finished */
71 static void ctdb_start_transport(struct ctdb_context *ctdb)
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 /* Make sure we log something when the daemon terminates */
86 atexit(print_exit_message);
88 /* a handler for when nodes are disabled/enabled */
89 ctdb_register_message_handler(ctdb, ctdb, CTDB_SRVID_NODE_FLAGS_CHANGED,
90 flag_change_handler, NULL);
92 /* start monitoring for connected/disconnected nodes */
93 ctdb_start_keepalive(ctdb);
95 /* start monitoring for node health */
96 ctdb_start_monitoring(ctdb);
98 /* start periodic update of tcp tickle lists */
99 ctdb_start_tcp_tickle_update(ctdb);
102 static void block_signal(int signum)
104 struct sigaction act;
106 memset(&act, 0, sizeof(act));
108 act.sa_handler = SIG_IGN;
109 sigemptyset(&act.sa_mask);
110 sigaddset(&act.sa_mask, signum);
111 sigaction(signum, &act, NULL);
116 send a packet to a client
118 static int daemon_queue_send(struct ctdb_client *client, struct ctdb_req_header *hdr)
120 client->ctdb->statistics.client_packets_sent++;
121 return ctdb_queue_send(client->queue, (uint8_t *)hdr, hdr->length);
125 message handler for when we are in daemon mode. This redirects the message
128 static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
129 TDB_DATA data, void *private_data)
131 struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
132 struct ctdb_req_message *r;
135 /* construct a message to send to the client containing the data */
136 len = offsetof(struct ctdb_req_message, data) + data.dsize;
137 r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
138 len, struct ctdb_req_message);
139 CTDB_NO_MEMORY_VOID(ctdb, r);
141 talloc_set_name_const(r, "req_message packet");
144 r->datalen = data.dsize;
145 memcpy(&r->data[0], data.dptr, data.dsize);
147 daemon_queue_send(client, &r->hdr);
154 this is called when the ctdb daemon received a ctdb request to
155 set the srvid from the client
157 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
159 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
161 if (client == NULL) {
162 DEBUG(0,("Bad client_id in daemon_request_register_message_handler\n"));
165 res = ctdb_register_message_handler(ctdb, client, srvid, daemon_message_handler, client);
167 DEBUG(0,(__location__ " Failed to register handler %llu in daemon\n",
168 (unsigned long long)srvid));
170 DEBUG(1,(__location__ " Registered message handler for srvid=%llu\n",
171 (unsigned long long)srvid));
174 /* this is a hack for Samba - we now know the pid of the Samba client */
175 if ((srvid & 0xFFFFFFFF) == srvid &&
176 kill(srvid, 0) == 0) {
178 DEBUG(1,(__location__ " Registered PID %u for client %u\n",
179 (unsigned)client->pid, client_id));
185 this is called when the ctdb daemon received a ctdb request to
186 remove a srvid from the client
188 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
190 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
191 if (client == NULL) {
192 DEBUG(0,("Bad client_id in daemon_request_deregister_message_handler\n"));
195 return ctdb_deregister_message_handler(ctdb, srvid, client);
200 destroy a ctdb_client
202 static int ctdb_client_destructor(struct ctdb_client *client)
204 ctdb_takeover_client_destructor_hook(client);
205 ctdb_reqid_remove(client->ctdb, client->client_id);
206 client->ctdb->statistics.num_clients--;
212 this is called when the ctdb daemon received a ctdb request message
213 from a local client over the unix domain socket
215 static void daemon_request_message_from_client(struct ctdb_client *client,
216 struct ctdb_req_message *c)
221 /* maybe the message is for another client on this node */
222 if (ctdb_get_pnn(client->ctdb)==c->hdr.destnode) {
223 ctdb_request_message(client->ctdb, (struct ctdb_req_header *)c);
227 /* its for a remote node */
228 data.dptr = &c->data[0];
229 data.dsize = c->datalen;
230 res = ctdb_daemon_send_message(client->ctdb, c->hdr.destnode,
233 DEBUG(0,(__location__ " Failed to send message to remote node %u\n",
239 struct daemon_call_state {
240 struct ctdb_client *client;
242 struct ctdb_call *call;
243 struct timeval start_time;
247 complete a call from a client
249 static void daemon_call_from_client_callback(struct ctdb_call_state *state)
251 struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
252 struct daemon_call_state);
253 struct ctdb_reply_call *r;
256 struct ctdb_client *client = dstate->client;
258 talloc_steal(client, dstate);
259 talloc_steal(dstate, dstate->call);
261 res = ctdb_daemon_call_recv(state, dstate->call);
263 DEBUG(0, (__location__ " ctdbd_call_recv() returned error\n"));
264 client->ctdb->statistics.pending_calls--;
265 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
269 length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
270 r = ctdbd_allocate_pkt(client->ctdb, dstate, CTDB_REPLY_CALL,
271 length, struct ctdb_reply_call);
273 DEBUG(0, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
274 client->ctdb->statistics.pending_calls--;
275 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
278 r->hdr.reqid = dstate->reqid;
279 r->datalen = dstate->call->reply_data.dsize;
280 memcpy(&r->data[0], dstate->call->reply_data.dptr, r->datalen);
282 res = daemon_queue_send(client, &r->hdr);
284 DEBUG(0, (__location__ " Failed to queue packet from daemon to client\n"));
286 ctdb_latency(&client->ctdb->statistics.max_call_latency, dstate->start_time);
288 client->ctdb->statistics.pending_calls--;
292 static void daemon_request_call_from_client(struct ctdb_client *client,
293 struct ctdb_req_call *c);
296 this is called when the ctdb daemon received a ctdb request call
297 from a local client over the unix domain socket
299 static void daemon_request_call_from_client(struct ctdb_client *client,
300 struct ctdb_req_call *c)
302 struct ctdb_call_state *state;
303 struct ctdb_db_context *ctdb_db;
304 struct daemon_call_state *dstate;
305 struct ctdb_call *call;
306 struct ctdb_ltdb_header header;
309 struct ctdb_context *ctdb = client->ctdb;
311 ctdb->statistics.total_calls++;
312 ctdb->statistics.pending_calls++;
314 ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
316 DEBUG(0, (__location__ " Unknown database in request. db_id==0x%08x",
318 ctdb->statistics.pending_calls--;
323 key.dsize = c->keylen;
325 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
326 (struct ctdb_req_header *)c, &data,
327 daemon_incoming_packet, client, True);
329 /* will retry later */
330 ctdb->statistics.pending_calls--;
335 DEBUG(0,(__location__ " Unable to fetch record\n"));
336 ctdb->statistics.pending_calls--;
340 dstate = talloc(client, struct daemon_call_state);
341 if (dstate == NULL) {
342 ctdb_ltdb_unlock(ctdb_db, key);
343 DEBUG(0,(__location__ " Unable to allocate dstate\n"));
344 ctdb->statistics.pending_calls--;
347 dstate->start_time = timeval_current();
348 dstate->client = client;
349 dstate->reqid = c->hdr.reqid;
350 talloc_steal(dstate, data.dptr);
352 call = dstate->call = talloc_zero(dstate, struct ctdb_call);
354 ctdb_ltdb_unlock(ctdb_db, key);
355 DEBUG(0,(__location__ " Unable to allocate call\n"));
356 ctdb->statistics.pending_calls--;
357 ctdb_latency(&ctdb->statistics.max_call_latency, dstate->start_time);
361 call->call_id = c->callid;
363 call->call_data.dptr = c->data + c->keylen;
364 call->call_data.dsize = c->calldatalen;
365 call->flags = c->flags;
367 if (header.dmaster == ctdb->pnn) {
368 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
370 state = ctdb_daemon_call_send_remote(ctdb_db, call, &header);
373 ctdb_ltdb_unlock(ctdb_db, key);
376 DEBUG(0,(__location__ " Unable to setup call send\n"));
377 ctdb->statistics.pending_calls--;
378 ctdb_latency(&ctdb->statistics.max_call_latency, dstate->start_time);
381 talloc_steal(state, dstate);
382 talloc_steal(client, state);
384 state->async.fn = daemon_call_from_client_callback;
385 state->async.private_data = dstate;
389 static void daemon_request_control_from_client(struct ctdb_client *client,
390 struct ctdb_req_control *c);
392 /* data contains a packet from the client */
393 static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
395 struct ctdb_client *client = talloc_get_type(p, struct ctdb_client);
397 struct ctdb_context *ctdb = client->ctdb;
399 /* place the packet as a child of a tmp_ctx. We then use
400 talloc_free() below to free it. If any of the calls want
401 to keep it, then they will steal it somewhere else, and the
402 talloc_free() will be a no-op */
403 tmp_ctx = talloc_new(client);
404 talloc_steal(tmp_ctx, hdr);
406 if (hdr->ctdb_magic != CTDB_MAGIC) {
407 ctdb_set_error(client->ctdb, "Non CTDB packet rejected in daemon\n");
411 if (hdr->ctdb_version != CTDB_VERSION) {
412 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
416 switch (hdr->operation) {
418 ctdb->statistics.client.req_call++;
419 daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
422 case CTDB_REQ_MESSAGE:
423 ctdb->statistics.client.req_message++;
424 daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
427 case CTDB_REQ_CONTROL:
428 ctdb->statistics.client.req_control++;
429 daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
433 DEBUG(0,(__location__ " daemon: unrecognized operation %u\n",
438 talloc_free(tmp_ctx);
442 called when the daemon gets a incoming packet
444 static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
446 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
447 struct ctdb_req_header *hdr;
454 client->ctdb->statistics.client_packets_recv++;
456 if (cnt < sizeof(*hdr)) {
457 ctdb_set_error(client->ctdb, "Bad packet length %u in daemon\n",
461 hdr = (struct ctdb_req_header *)data;
462 if (cnt != hdr->length) {
463 ctdb_set_error(client->ctdb, "Bad header length %u expected %u\n in daemon",
464 (unsigned)hdr->length, (unsigned)cnt);
468 if (hdr->ctdb_magic != CTDB_MAGIC) {
469 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
473 if (hdr->ctdb_version != CTDB_VERSION) {
474 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
478 DEBUG(3,(__location__ " client request %u of type %u length %u from "
479 "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
480 hdr->srcnode, hdr->destnode));
482 /* it is the responsibility of the incoming packet function to free 'data' */
483 daemon_incoming_packet(client, hdr);
486 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
487 uint16_t flags, void *private_data)
489 struct sockaddr_in addr;
492 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
493 struct ctdb_client *client;
495 memset(&addr, 0, sizeof(addr));
497 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
503 set_close_on_exec(fd);
505 client = talloc_zero(ctdb, struct ctdb_client);
508 client->client_id = ctdb_reqid_new(ctdb, client);
509 ctdb->statistics.num_clients++;
511 client->queue = ctdb_queue_setup(ctdb, client, fd, CTDB_DS_ALIGNMENT,
512 ctdb_daemon_read_cb, client);
514 talloc_set_destructor(client, ctdb_client_destructor);
520 create a unix domain socket and bind it
521 return a file descriptor open on the socket
523 static int ux_socket_bind(struct ctdb_context *ctdb)
525 struct sockaddr_un addr;
527 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
528 if (ctdb->daemon.sd == -1) {
532 set_nonblocking(ctdb->daemon.sd);
533 set_close_on_exec(ctdb->daemon.sd);
536 /* AIX doesn't like this :( */
537 if (fchown(ctdb->daemon.sd, geteuid(), getegid()) != 0 ||
538 fchmod(ctdb->daemon.sd, 0700) != 0) {
539 DEBUG(0,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n"));
544 set_nonblocking(ctdb->daemon.sd);
546 memset(&addr, 0, sizeof(addr));
547 addr.sun_family = AF_UNIX;
548 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
550 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
551 DEBUG(0,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
554 if (listen(ctdb->daemon.sd, 10) != 0) {
555 DEBUG(0,("Unable to listen on ctdb socket '%s'\n", ctdb->daemon.name));
562 close(ctdb->daemon.sd);
563 ctdb->daemon.sd = -1;
568 delete the socket on exit - called on destruction of autofree context
570 static int unlink_destructor(const char *name)
577 start the protocol going as a daemon
579 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
582 struct fd_event *fde;
583 const char *domain_socket_name;
585 /* get rid of any old sockets */
586 unlink(ctdb->daemon.name);
588 /* create a unix domain stream socket to listen to */
589 res = ux_socket_bind(ctdb);
591 DEBUG(0,(__location__ " Failed to open CTDB unix domain socket\n"));
595 if (do_fork && fork()) {
599 tdb_reopen_all(False);
604 if (open("/dev/null", O_RDONLY) != 0) {
605 DEBUG(0,(__location__ " Failed to setup stdin on /dev/null\n"));
609 block_signal(SIGPIPE);
611 if (ctdb->do_setsched) {
612 /* try to set us up as realtime */
613 ctdb_set_scheduler(ctdb);
616 /* ensure the socket is deleted on exit of the daemon */
617 domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
618 talloc_set_destructor(domain_socket_name, unlink_destructor);
620 ctdb->ev = event_context_init(NULL);
622 /* force initial recovery for election */
623 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
625 if (strcmp(ctdb->transport, "tcp") == 0) {
626 int ctdb_tcp_init(struct ctdb_context *);
627 ret = ctdb_tcp_init(ctdb);
629 #ifdef USE_INFINIBAND
630 if (strcmp(ctdb->transport, "ib") == 0) {
631 int ctdb_ibw_init(struct ctdb_context *);
632 ret = ctdb_ibw_init(ctdb);
636 DEBUG(0,("Failed to initialise transport '%s'\n", ctdb->transport));
640 /* initialise the transport */
641 if (ctdb->methods->initialise(ctdb) != 0) {
642 DEBUG(0,("transport failed to initialise!\n"));
643 ctdb_fatal(ctdb, "transport failed to initialise");
646 /* attach to any existing persistent databases */
647 if (ctdb_attach_persistent(ctdb) != 0) {
648 ctdb_fatal(ctdb, "Failed to attach to persistent databases\n");
651 /* start frozen, then let the first election sort things out */
652 if (!ctdb_blocking_freeze(ctdb)) {
653 ctdb_fatal(ctdb, "Failed to get initial freeze\n");
656 /* now start accepting clients, only can do this once frozen */
657 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd,
658 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
659 ctdb_accept_client, ctdb);
661 /* tell all other nodes we've just started up */
662 ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL,
663 0, CTDB_CONTROL_STARTUP, 0,
664 CTDB_CTRL_FLAG_NOREPLY,
665 tdb_null, NULL, NULL);
667 /* release any IPs we hold from previous runs of the daemon */
668 ctdb_release_all_ips(ctdb);
670 /* start the transport going */
671 ctdb_start_transport(ctdb);
673 /* go into a wait loop to allow other nodes to complete */
674 event_loop_wait(ctdb->ev);
676 DEBUG(0,("event_loop_wait() returned. this should not happen\n"));
681 allocate a packet for use in daemon<->daemon communication
683 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
685 enum ctdb_operation operation,
686 size_t length, size_t slength,
690 struct ctdb_req_header *hdr;
692 length = MAX(length, slength);
693 size = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
695 hdr = (struct ctdb_req_header *)ctdb->methods->allocate_pkt(mem_ctx, size);
697 DEBUG(0,("Unable to allocate transport packet for operation %u of length %u\n",
698 operation, (unsigned)length));
701 talloc_set_name_const(hdr, type);
702 memset(hdr, 0, slength);
703 hdr->length = length;
704 hdr->operation = operation;
705 hdr->ctdb_magic = CTDB_MAGIC;
706 hdr->ctdb_version = CTDB_VERSION;
707 hdr->generation = ctdb->vnn_map->generation;
708 hdr->srcnode = ctdb->pnn;
713 struct daemon_control_state {
714 struct daemon_control_state *next, *prev;
715 struct ctdb_client *client;
716 struct ctdb_req_control *c;
718 struct ctdb_node *node;
722 callback when a control reply comes in
724 static void daemon_control_callback(struct ctdb_context *ctdb,
725 int32_t status, TDB_DATA data,
726 const char *errormsg,
729 struct daemon_control_state *state = talloc_get_type(private_data,
730 struct daemon_control_state);
731 struct ctdb_client *client = state->client;
732 struct ctdb_reply_control *r;
735 /* construct a message to send to the client containing the data */
736 len = offsetof(struct ctdb_reply_control, data) + data.dsize;
738 len += strlen(errormsg);
740 r = ctdbd_allocate_pkt(ctdb, state, CTDB_REPLY_CONTROL, len,
741 struct ctdb_reply_control);
742 CTDB_NO_MEMORY_VOID(ctdb, r);
744 r->hdr.reqid = state->reqid;
746 r->datalen = data.dsize;
748 memcpy(&r->data[0], data.dptr, data.dsize);
750 r->errorlen = strlen(errormsg);
751 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
754 daemon_queue_send(client, &r->hdr);
760 fail all pending controls to a disconnected node
762 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node)
764 struct daemon_control_state *state;
765 while ((state = node->pending_controls)) {
766 DLIST_REMOVE(node->pending_controls, state);
767 daemon_control_callback(ctdb, (uint32_t)-1, tdb_null,
768 "node is disconnected", state);
773 destroy a daemon_control_state
775 static int daemon_control_destructor(struct daemon_control_state *state)
778 DLIST_REMOVE(state->node->pending_controls, state);
784 this is called when the ctdb daemon received a ctdb request control
785 from a local client over the unix domain socket
787 static void daemon_request_control_from_client(struct ctdb_client *client,
788 struct ctdb_req_control *c)
792 struct daemon_control_state *state;
793 TALLOC_CTX *tmp_ctx = talloc_new(client);
795 if (c->hdr.destnode == CTDB_CURRENT_NODE) {
796 c->hdr.destnode = client->ctdb->pnn;
799 state = talloc(client, struct daemon_control_state);
800 CTDB_NO_MEMORY_VOID(client->ctdb, state);
802 state->client = client;
803 state->c = talloc_steal(state, c);
804 state->reqid = c->hdr.reqid;
805 if (ctdb_validate_pnn(client->ctdb, c->hdr.destnode)) {
806 state->node = client->ctdb->nodes[c->hdr.destnode];
807 DLIST_ADD(state->node->pending_controls, state);
812 talloc_set_destructor(state, daemon_control_destructor);
814 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
815 talloc_steal(tmp_ctx, state);
818 data.dptr = &c->data[0];
819 data.dsize = c->datalen;
820 res = ctdb_daemon_send_control(client->ctdb, c->hdr.destnode,
821 c->srvid, c->opcode, client->client_id,
823 data, daemon_control_callback,
826 DEBUG(0,(__location__ " Failed to send control to remote node %u\n",
830 talloc_free(tmp_ctx);
834 register a call function
836 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
837 ctdb_fn_t fn, int id)
839 struct ctdb_registered_call *call;
840 struct ctdb_db_context *ctdb_db;
842 ctdb_db = find_ctdb_db(ctdb, db_id);
843 if (ctdb_db == NULL) {
847 call = talloc(ctdb_db, struct ctdb_registered_call);
851 DLIST_ADD(ctdb_db->calls, call);
858 this local messaging handler is ugly, but is needed to prevent
859 recursion in ctdb_send_message() when the destination node is the
860 same as the source node
862 struct ctdb_local_message {
863 struct ctdb_context *ctdb;
868 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
869 struct timeval t, void *private_data)
871 struct ctdb_local_message *m = talloc_get_type(private_data,
872 struct ctdb_local_message);
875 res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
877 DEBUG(0, (__location__ " Failed to dispatch message for srvid=%llu\n",
878 (unsigned long long)m->srvid));
883 static int ctdb_local_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
885 struct ctdb_local_message *m;
886 m = talloc(ctdb, struct ctdb_local_message);
887 CTDB_NO_MEMORY(ctdb, m);
892 m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
893 if (m->data.dptr == NULL) {
898 /* this needs to be done as an event to prevent recursion */
899 event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
906 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
907 uint64_t srvid, TDB_DATA data)
909 struct ctdb_req_message *r;
912 /* see if this is a message to ourselves */
913 if (pnn == ctdb->pnn) {
914 return ctdb_local_message(ctdb, srvid, data);
917 len = offsetof(struct ctdb_req_message, data) + data.dsize;
918 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_MESSAGE, len,
919 struct ctdb_req_message);
920 CTDB_NO_MEMORY(ctdb, r);
922 r->hdr.destnode = pnn;
924 r->datalen = data.dsize;
925 memcpy(&r->data[0], data.dptr, data.dsize);
927 ctdb_queue_packet(ctdb, &r->hdr);