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"
30 #include <sys/socket.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_pnn(ctdb, c->pnn)) {
43 DEBUG(DEBUG_CRIT,(__location__ "Invalid data in ctdb_node_flag_change\n"));
47 if (!ctdb_validate_pnn(ctdb, c->pnn)) {
48 DEBUG(DEBUG_CRIT,("Bad pnn %u in flag_change_handler\n", c->pnn));
52 /* don't get the disconnected flag from the other node */
53 ctdb->nodes[c->pnn]->flags =
54 (ctdb->nodes[c->pnn]->flags&NODE_FLAGS_DISCONNECTED)
55 | (c->new_flags & ~NODE_FLAGS_DISCONNECTED);
56 DEBUG(DEBUG_DEBUG,("Node flags for node %u are now 0x%x\n", c->pnn, ctdb->nodes[c->pnn]->flags));
58 /* make sure we don't hold any IPs when we shouldn't */
59 if (c->pnn == ctdb->pnn &&
60 (ctdb->nodes[c->pnn]->flags & (NODE_FLAGS_INACTIVE|NODE_FLAGS_BANNED))) {
61 ctdb_release_all_ips(ctdb);
65 static void print_exit_message(void)
67 DEBUG(DEBUG_NOTICE,("CTDB daemon shutting down\n"));
71 /* called when the "startup" event script has finished */
72 static void ctdb_start_transport(struct ctdb_context *ctdb)
74 if (ctdb->methods == NULL) {
75 DEBUG(DEBUG_ALERT,(__location__ " startup event finished but transport is DOWN.\n"));
76 ctdb_fatal(ctdb, "transport is not initialized but startup completed");
79 /* start the transport running */
80 if (ctdb->methods->start(ctdb) != 0) {
81 DEBUG(DEBUG_ALERT,("transport failed to start!\n"));
82 ctdb_fatal(ctdb, "transport failed to start");
85 /* start the recovery daemon process */
86 if (ctdb_start_recoverd(ctdb) != 0) {
87 DEBUG(DEBUG_ALERT,("Failed to start recovery daemon\n"));
91 /* Make sure we log something when the daemon terminates */
92 atexit(print_exit_message);
94 /* a handler for when nodes are disabled/enabled */
95 ctdb_register_message_handler(ctdb, ctdb, CTDB_SRVID_NODE_FLAGS_CHANGED,
96 flag_change_handler, NULL);
98 /* start monitoring for connected/disconnected nodes */
99 ctdb_start_keepalive(ctdb);
101 /* start monitoring for node health */
102 ctdb_start_monitoring(ctdb);
104 /* start periodic update of tcp tickle lists */
105 ctdb_start_tcp_tickle_update(ctdb);
107 /* start listening for recovery daemon pings */
108 ctdb_control_recd_ping(ctdb);
111 static void block_signal(int signum)
113 struct sigaction act;
115 memset(&act, 0, sizeof(act));
117 act.sa_handler = SIG_IGN;
118 sigemptyset(&act.sa_mask);
119 sigaddset(&act.sa_mask, signum);
120 sigaction(signum, &act, NULL);
125 send a packet to a client
127 static int daemon_queue_send(struct ctdb_client *client, struct ctdb_req_header *hdr)
129 client->ctdb->statistics.client_packets_sent++;
130 return ctdb_queue_send(client->queue, (uint8_t *)hdr, hdr->length);
134 message handler for when we are in daemon mode. This redirects the message
137 static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
138 TDB_DATA data, void *private_data)
140 struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
141 struct ctdb_req_message *r;
144 /* construct a message to send to the client containing the data */
145 len = offsetof(struct ctdb_req_message, data) + data.dsize;
146 r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE,
147 len, struct ctdb_req_message);
148 CTDB_NO_MEMORY_VOID(ctdb, r);
150 talloc_set_name_const(r, "req_message packet");
153 r->datalen = data.dsize;
154 memcpy(&r->data[0], data.dptr, data.dsize);
156 daemon_queue_send(client, &r->hdr);
163 this is called when the ctdb daemon received a ctdb request to
164 set the srvid from the client
166 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
168 struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
170 if (client == NULL) {
171 DEBUG(DEBUG_ERR,("Bad client_id in daemon_request_register_message_handler\n"));
174 res = ctdb_register_message_handler(ctdb, client, srvid, daemon_message_handler, client);
176 DEBUG(DEBUG_ERR,(__location__ " Failed to register handler %llu in daemon\n",
177 (unsigned long long)srvid));
179 DEBUG(DEBUG_INFO,(__location__ " Registered message handler for srvid=%llu\n",
180 (unsigned long long)srvid));
183 /* this is a hack for Samba - we now know the pid of the Samba client */
184 if ((srvid & 0xFFFFFFFF) == srvid &&
185 kill(srvid, 0) == 0) {
187 DEBUG(DEBUG_INFO,(__location__ " Registered PID %u for client %u\n",
188 (unsigned)client->pid, client_id));
194 this is called when the ctdb daemon received a ctdb request to
195 remove a srvid from the client
197 int daemon_deregister_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);
200 if (client == NULL) {
201 DEBUG(DEBUG_ERR,("Bad client_id in daemon_request_deregister_message_handler\n"));
204 return ctdb_deregister_message_handler(ctdb, srvid, client);
209 destroy a ctdb_client
211 static int ctdb_client_destructor(struct ctdb_client *client)
213 ctdb_takeover_client_destructor_hook(client);
214 ctdb_reqid_remove(client->ctdb, client->client_id);
215 client->ctdb->statistics.num_clients--;
217 if (client->num_persistent_updates != 0) {
218 DEBUG(DEBUG_ERR,(__location__ " Client disconnecting with %u persistent updates in flight. Starting recovery\n", client->num_persistent_updates));
219 client->ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
227 this is called when the ctdb daemon received a ctdb request message
228 from a local client over the unix domain socket
230 static void daemon_request_message_from_client(struct ctdb_client *client,
231 struct ctdb_req_message *c)
236 /* maybe the message is for another client on this node */
237 if (ctdb_get_pnn(client->ctdb)==c->hdr.destnode) {
238 ctdb_request_message(client->ctdb, (struct ctdb_req_header *)c);
242 /* its for a remote node */
243 data.dptr = &c->data[0];
244 data.dsize = c->datalen;
245 res = ctdb_daemon_send_message(client->ctdb, c->hdr.destnode,
248 DEBUG(DEBUG_ERR,(__location__ " Failed to send message to remote node %u\n",
254 struct daemon_call_state {
255 struct ctdb_client *client;
257 struct ctdb_call *call;
258 struct timeval start_time;
262 complete a call from a client
264 static void daemon_call_from_client_callback(struct ctdb_call_state *state)
266 struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
267 struct daemon_call_state);
268 struct ctdb_reply_call *r;
271 struct ctdb_client *client = dstate->client;
273 talloc_steal(client, dstate);
274 talloc_steal(dstate, dstate->call);
276 res = ctdb_daemon_call_recv(state, dstate->call);
278 DEBUG(DEBUG_ERR, (__location__ " ctdbd_call_recv() returned error\n"));
279 client->ctdb->statistics.pending_calls--;
280 ctdb_latency(state->ctdb_db, "call_from_client_cb 1", &client->ctdb->statistics.max_call_latency, dstate->start_time);
284 length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
285 r = ctdbd_allocate_pkt(client->ctdb, dstate, CTDB_REPLY_CALL,
286 length, struct ctdb_reply_call);
288 DEBUG(DEBUG_ERR, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
289 client->ctdb->statistics.pending_calls--;
290 ctdb_latency(state->ctdb_db, "call_from_client_cb 2", &client->ctdb->statistics.max_call_latency, dstate->start_time);
293 r->hdr.reqid = dstate->reqid;
294 r->datalen = dstate->call->reply_data.dsize;
295 memcpy(&r->data[0], dstate->call->reply_data.dptr, r->datalen);
297 res = daemon_queue_send(client, &r->hdr);
299 DEBUG(DEBUG_ERR, (__location__ " Failed to queue packet from daemon to client\n"));
301 ctdb_latency(state->ctdb_db, "call_from_client_cb 3", &client->ctdb->statistics.max_call_latency, dstate->start_time);
303 client->ctdb->statistics.pending_calls--;
306 struct ctdb_daemon_packet_wrap {
307 struct ctdb_context *ctdb;
312 a wrapper to catch disconnected clients
314 static void daemon_incoming_packet_wrap(void *p, struct ctdb_req_header *hdr)
316 struct ctdb_client *client;
317 struct ctdb_daemon_packet_wrap *w = talloc_get_type(p,
318 struct ctdb_daemon_packet_wrap);
320 DEBUG(DEBUG_CRIT,(__location__ " Bad packet type '%s'\n", talloc_get_name(p)));
324 client = ctdb_reqid_find(w->ctdb, w->client_id, struct ctdb_client);
325 if (client == NULL) {
326 DEBUG(DEBUG_ERR,(__location__ " Packet for disconnected client %u\n",
334 daemon_incoming_packet(client, hdr);
339 this is called when the ctdb daemon received a ctdb request call
340 from a local client over the unix domain socket
342 static void daemon_request_call_from_client(struct ctdb_client *client,
343 struct ctdb_req_call *c)
345 struct ctdb_call_state *state;
346 struct ctdb_db_context *ctdb_db;
347 struct daemon_call_state *dstate;
348 struct ctdb_call *call;
349 struct ctdb_ltdb_header header;
352 struct ctdb_context *ctdb = client->ctdb;
353 struct ctdb_daemon_packet_wrap *w;
355 ctdb->statistics.total_calls++;
356 ctdb->statistics.pending_calls++;
358 ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
360 DEBUG(DEBUG_ERR, (__location__ " Unknown database in request. db_id==0x%08x",
362 ctdb->statistics.pending_calls--;
367 key.dsize = c->keylen;
369 w = talloc(ctdb, struct ctdb_daemon_packet_wrap);
370 CTDB_NO_MEMORY_VOID(ctdb, w);
373 w->client_id = client->client_id;
375 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
376 (struct ctdb_req_header *)c, &data,
377 daemon_incoming_packet_wrap, w, True);
379 /* will retry later */
380 ctdb->statistics.pending_calls--;
387 DEBUG(DEBUG_ERR,(__location__ " Unable to fetch record\n"));
388 ctdb->statistics.pending_calls--;
392 dstate = talloc(client, struct daemon_call_state);
393 if (dstate == NULL) {
394 ctdb_ltdb_unlock(ctdb_db, key);
395 DEBUG(DEBUG_ERR,(__location__ " Unable to allocate dstate\n"));
396 ctdb->statistics.pending_calls--;
399 dstate->start_time = timeval_current();
400 dstate->client = client;
401 dstate->reqid = c->hdr.reqid;
402 talloc_steal(dstate, data.dptr);
404 call = dstate->call = talloc_zero(dstate, struct ctdb_call);
406 ctdb_ltdb_unlock(ctdb_db, key);
407 DEBUG(DEBUG_ERR,(__location__ " Unable to allocate call\n"));
408 ctdb->statistics.pending_calls--;
409 ctdb_latency(ctdb_db, "call_from_client 1", &ctdb->statistics.max_call_latency, dstate->start_time);
413 call->call_id = c->callid;
415 call->call_data.dptr = c->data + c->keylen;
416 call->call_data.dsize = c->calldatalen;
417 call->flags = c->flags;
419 if (header.dmaster == ctdb->pnn) {
420 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
422 state = ctdb_daemon_call_send_remote(ctdb_db, call, &header);
425 ctdb_ltdb_unlock(ctdb_db, key);
428 DEBUG(DEBUG_ERR,(__location__ " Unable to setup call send\n"));
429 ctdb->statistics.pending_calls--;
430 ctdb_latency(ctdb_db, "call_from_client 2", &ctdb->statistics.max_call_latency, dstate->start_time);
433 talloc_steal(state, dstate);
434 talloc_steal(client, state);
436 state->async.fn = daemon_call_from_client_callback;
437 state->async.private_data = dstate;
441 static void daemon_request_control_from_client(struct ctdb_client *client,
442 struct ctdb_req_control *c);
444 /* data contains a packet from the client */
445 static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
447 struct ctdb_client *client = talloc_get_type(p, struct ctdb_client);
449 struct ctdb_context *ctdb = client->ctdb;
451 /* place the packet as a child of a tmp_ctx. We then use
452 talloc_free() below to free it. If any of the calls want
453 to keep it, then they will steal it somewhere else, and the
454 talloc_free() will be a no-op */
455 tmp_ctx = talloc_new(client);
456 talloc_steal(tmp_ctx, hdr);
458 if (hdr->ctdb_magic != CTDB_MAGIC) {
459 ctdb_set_error(client->ctdb, "Non CTDB packet rejected in daemon\n");
463 if (hdr->ctdb_version != CTDB_VERSION) {
464 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
468 switch (hdr->operation) {
470 ctdb->statistics.client.req_call++;
471 daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
474 case CTDB_REQ_MESSAGE:
475 ctdb->statistics.client.req_message++;
476 daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
479 case CTDB_REQ_CONTROL:
480 ctdb->statistics.client.req_control++;
481 daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
485 DEBUG(DEBUG_CRIT,(__location__ " daemon: unrecognized operation %u\n",
490 talloc_free(tmp_ctx);
494 called when the daemon gets a incoming packet
496 static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
498 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
499 struct ctdb_req_header *hdr;
506 client->ctdb->statistics.client_packets_recv++;
508 if (cnt < sizeof(*hdr)) {
509 ctdb_set_error(client->ctdb, "Bad packet length %u in daemon\n",
513 hdr = (struct ctdb_req_header *)data;
514 if (cnt != hdr->length) {
515 ctdb_set_error(client->ctdb, "Bad header length %u expected %u\n in daemon",
516 (unsigned)hdr->length, (unsigned)cnt);
520 if (hdr->ctdb_magic != CTDB_MAGIC) {
521 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
525 if (hdr->ctdb_version != CTDB_VERSION) {
526 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
530 DEBUG(DEBUG_DEBUG,(__location__ " client request %u of type %u length %u from "
531 "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
532 hdr->srcnode, hdr->destnode));
534 /* it is the responsibility of the incoming packet function to free 'data' */
535 daemon_incoming_packet(client, hdr);
538 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
539 uint16_t flags, void *private_data)
541 struct sockaddr_un addr;
544 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
545 struct ctdb_client *client;
547 struct peercred_struct cr;
548 socklen_t crl = sizeof(struct peercred_struct);
551 socklen_t crl = sizeof(struct ucred);
554 memset(&addr, 0, sizeof(addr));
556 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
562 set_close_on_exec(fd);
564 client = talloc_zero(ctdb, struct ctdb_client);
566 if (getsockopt(fd, SOL_SOCKET, SO_PEERID, &cr, &crl) == 0) {
568 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crl) == 0) {
570 talloc_asprintf(client, "struct ctdb_client: pid:%u", (unsigned)cr.pid);
575 client->client_id = ctdb_reqid_new(ctdb, client);
576 ctdb->statistics.num_clients++;
578 client->queue = ctdb_queue_setup(ctdb, client, fd, CTDB_DS_ALIGNMENT,
579 ctdb_daemon_read_cb, client);
581 talloc_set_destructor(client, ctdb_client_destructor);
587 create a unix domain socket and bind it
588 return a file descriptor open on the socket
590 static int ux_socket_bind(struct ctdb_context *ctdb)
592 struct sockaddr_un addr;
594 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
595 if (ctdb->daemon.sd == -1) {
599 set_close_on_exec(ctdb->daemon.sd);
600 set_nonblocking(ctdb->daemon.sd);
602 memset(&addr, 0, sizeof(addr));
603 addr.sun_family = AF_UNIX;
604 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
606 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
607 DEBUG(DEBUG_CRIT,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
611 if (chown(ctdb->daemon.name, geteuid(), getegid()) != 0 ||
612 chmod(ctdb->daemon.name, 0700) != 0) {
613 DEBUG(DEBUG_CRIT,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n", ctdb->daemon.name));
618 if (listen(ctdb->daemon.sd, 10) != 0) {
619 DEBUG(DEBUG_CRIT,("Unable to listen on ctdb socket '%s'\n", ctdb->daemon.name));
626 close(ctdb->daemon.sd);
627 ctdb->daemon.sd = -1;
632 delete the socket on exit - called on destruction of autofree context
634 static int unlink_destructor(const char *name)
640 static void sig_child_handler(struct event_context *ev,
641 struct signal_event *se, int signum, int count,
645 // struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
650 pid = waitpid(-1, &status, WNOHANG);
652 DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
656 DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d\n", (int)pid));
662 start the protocol going as a daemon
664 int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
667 struct fd_event *fde;
668 const char *domain_socket_name;
669 struct signal_event *se;
671 /* get rid of any old sockets */
672 unlink(ctdb->daemon.name);
674 /* create a unix domain stream socket to listen to */
675 res = ux_socket_bind(ctdb);
677 DEBUG(DEBUG_ALERT,(__location__ " Failed to open CTDB unix domain socket\n"));
681 if (do_fork && fork()) {
685 tdb_reopen_all(False);
690 if (open("/dev/null", O_RDONLY) != 0) {
691 DEBUG(DEBUG_ALERT,(__location__ " Failed to setup stdin on /dev/null\n"));
695 block_signal(SIGPIPE);
697 if (ctdb->do_setsched) {
698 /* try to set us up as realtime */
699 ctdb_set_scheduler(ctdb);
702 /* ensure the socket is deleted on exit of the daemon */
703 domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
704 talloc_set_destructor(domain_socket_name, unlink_destructor);
706 ctdb->ev = event_context_init(NULL);
708 ctdb_set_child_logging(ctdb);
710 /* force initial recovery for election */
711 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
713 if (strcmp(ctdb->transport, "tcp") == 0) {
714 int ctdb_tcp_init(struct ctdb_context *);
715 ret = ctdb_tcp_init(ctdb);
717 #ifdef USE_INFINIBAND
718 if (strcmp(ctdb->transport, "ib") == 0) {
719 int ctdb_ibw_init(struct ctdb_context *);
720 ret = ctdb_ibw_init(ctdb);
724 DEBUG(DEBUG_ERR,("Failed to initialise transport '%s'\n", ctdb->transport));
728 if (ctdb->methods == NULL) {
729 DEBUG(DEBUG_ALERT,(__location__ " Can not initialize transport. ctdb->methods is NULL\n"));
730 ctdb_fatal(ctdb, "transport is unavailable. can not initialize.");
733 /* initialise the transport */
734 if (ctdb->methods->initialise(ctdb) != 0) {
735 ctdb_fatal(ctdb, "transport failed to initialise");
738 /* attach to any existing persistent databases */
739 if (ctdb_attach_persistent(ctdb) != 0) {
740 ctdb_fatal(ctdb, "Failed to attach to persistent databases\n");
743 /* start frozen, then let the first election sort things out */
744 if (!ctdb_blocking_freeze(ctdb)) {
745 ctdb_fatal(ctdb, "Failed to get initial freeze\n");
748 /* now start accepting clients, only can do this once frozen */
749 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd,
750 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
751 ctdb_accept_client, ctdb);
753 /* tell all other nodes we've just started up */
754 ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_ALL,
755 0, CTDB_CONTROL_STARTUP, 0,
756 CTDB_CTRL_FLAG_NOREPLY,
757 tdb_null, NULL, NULL);
759 /* release any IPs we hold from previous runs of the daemon */
760 ctdb_release_all_ips(ctdb);
762 /* start the transport going */
763 ctdb_start_transport(ctdb);
765 /* set up a handler to pick up sigchld */
766 se = event_add_signal(ctdb->ev, ctdb,
771 DEBUG(DEBUG_CRIT,("Failed to set up signal handler for SIGCHLD\n"));
775 /* go into a wait loop to allow other nodes to complete */
776 event_loop_wait(ctdb->ev);
778 DEBUG(DEBUG_CRIT,("event_loop_wait() returned. this should not happen\n"));
783 allocate a packet for use in daemon<->daemon communication
785 struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
787 enum ctdb_operation operation,
788 size_t length, size_t slength,
792 struct ctdb_req_header *hdr;
794 length = MAX(length, slength);
795 size = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
797 if (ctdb->methods == NULL) {
798 DEBUG(DEBUG_ERR,(__location__ " Unable to allocate transport packet for operation %u of length %u. Transport is DOWN.\n",
799 operation, (unsigned)length));
803 hdr = (struct ctdb_req_header *)ctdb->methods->allocate_pkt(mem_ctx, size);
805 DEBUG(DEBUG_ERR,("Unable to allocate transport packet for operation %u of length %u\n",
806 operation, (unsigned)length));
809 talloc_set_name_const(hdr, type);
810 memset(hdr, 0, slength);
811 hdr->length = length;
812 hdr->operation = operation;
813 hdr->ctdb_magic = CTDB_MAGIC;
814 hdr->ctdb_version = CTDB_VERSION;
815 hdr->generation = ctdb->vnn_map->generation;
816 hdr->srcnode = ctdb->pnn;
821 struct daemon_control_state {
822 struct daemon_control_state *next, *prev;
823 struct ctdb_client *client;
824 struct ctdb_req_control *c;
826 struct ctdb_node *node;
830 callback when a control reply comes in
832 static void daemon_control_callback(struct ctdb_context *ctdb,
833 int32_t status, TDB_DATA data,
834 const char *errormsg,
837 struct daemon_control_state *state = talloc_get_type(private_data,
838 struct daemon_control_state);
839 struct ctdb_client *client = state->client;
840 struct ctdb_reply_control *r;
843 /* construct a message to send to the client containing the data */
844 len = offsetof(struct ctdb_reply_control, data) + data.dsize;
846 len += strlen(errormsg);
848 r = ctdbd_allocate_pkt(ctdb, state, CTDB_REPLY_CONTROL, len,
849 struct ctdb_reply_control);
850 CTDB_NO_MEMORY_VOID(ctdb, r);
852 r->hdr.reqid = state->reqid;
854 r->datalen = data.dsize;
856 memcpy(&r->data[0], data.dptr, data.dsize);
858 r->errorlen = strlen(errormsg);
859 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
862 daemon_queue_send(client, &r->hdr);
868 fail all pending controls to a disconnected node
870 void ctdb_daemon_cancel_controls(struct ctdb_context *ctdb, struct ctdb_node *node)
872 struct daemon_control_state *state;
873 while ((state = node->pending_controls)) {
874 DLIST_REMOVE(node->pending_controls, state);
875 daemon_control_callback(ctdb, (uint32_t)-1, tdb_null,
876 "node is disconnected", state);
881 destroy a daemon_control_state
883 static int daemon_control_destructor(struct daemon_control_state *state)
886 DLIST_REMOVE(state->node->pending_controls, state);
892 this is called when the ctdb daemon received a ctdb request control
893 from a local client over the unix domain socket
895 static void daemon_request_control_from_client(struct ctdb_client *client,
896 struct ctdb_req_control *c)
900 struct daemon_control_state *state;
901 TALLOC_CTX *tmp_ctx = talloc_new(client);
903 if (c->hdr.destnode == CTDB_CURRENT_NODE) {
904 c->hdr.destnode = client->ctdb->pnn;
907 state = talloc(client, struct daemon_control_state);
908 CTDB_NO_MEMORY_VOID(client->ctdb, state);
910 state->client = client;
911 state->c = talloc_steal(state, c);
912 state->reqid = c->hdr.reqid;
913 if (ctdb_validate_pnn(client->ctdb, c->hdr.destnode)) {
914 state->node = client->ctdb->nodes[c->hdr.destnode];
915 DLIST_ADD(state->node->pending_controls, state);
920 talloc_set_destructor(state, daemon_control_destructor);
922 if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
923 talloc_steal(tmp_ctx, state);
926 data.dptr = &c->data[0];
927 data.dsize = c->datalen;
928 res = ctdb_daemon_send_control(client->ctdb, c->hdr.destnode,
929 c->srvid, c->opcode, client->client_id,
931 data, daemon_control_callback,
934 DEBUG(DEBUG_ERR,(__location__ " Failed to send control to remote node %u\n",
938 talloc_free(tmp_ctx);
942 register a call function
944 int ctdb_daemon_set_call(struct ctdb_context *ctdb, uint32_t db_id,
945 ctdb_fn_t fn, int id)
947 struct ctdb_registered_call *call;
948 struct ctdb_db_context *ctdb_db;
950 ctdb_db = find_ctdb_db(ctdb, db_id);
951 if (ctdb_db == NULL) {
955 call = talloc(ctdb_db, struct ctdb_registered_call);
959 DLIST_ADD(ctdb_db->calls, call);
966 this local messaging handler is ugly, but is needed to prevent
967 recursion in ctdb_send_message() when the destination node is the
968 same as the source node
970 struct ctdb_local_message {
971 struct ctdb_context *ctdb;
976 static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te,
977 struct timeval t, void *private_data)
979 struct ctdb_local_message *m = talloc_get_type(private_data,
980 struct ctdb_local_message);
983 res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
985 DEBUG(DEBUG_ERR, (__location__ " Failed to dispatch message for srvid=%llu\n",
986 (unsigned long long)m->srvid));
991 static int ctdb_local_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DATA data)
993 struct ctdb_local_message *m;
994 m = talloc(ctdb, struct ctdb_local_message);
995 CTDB_NO_MEMORY(ctdb, m);
1000 m->data.dptr = talloc_memdup(m, m->data.dptr, m->data.dsize);
1001 if (m->data.dptr == NULL) {
1006 /* this needs to be done as an event to prevent recursion */
1007 event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
1014 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
1015 uint64_t srvid, TDB_DATA data)
1017 struct ctdb_req_message *r;
1020 /* see if this is a message to ourselves */
1021 if (pnn == ctdb->pnn) {
1022 return ctdb_local_message(ctdb, srvid, data);
1025 len = offsetof(struct ctdb_req_message, data) + data.dsize;
1026 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_MESSAGE, len,
1027 struct ctdb_req_message);
1028 CTDB_NO_MEMORY(ctdb, r);
1030 r->hdr.destnode = pnn;
1032 r->datalen = data.dsize;
1033 memcpy(&r->data[0], data.dptr, data.dsize);
1035 ctdb_queue_packet(ctdb, &r->hdr);