4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
33 structure describing a connected client in the daemon
36 struct ctdb_context *ctdb;
38 struct ctdb_queue *queue;
43 static void daemon_incoming_packet(void *, uint8_t *, uint32_t );
45 static void ctdb_main_loop(struct ctdb_context *ctdb)
49 if (strcmp(ctdb->transport, "tcp") == 0) {
50 int ctdb_tcp_init(struct ctdb_context *);
51 ret = ctdb_tcp_init(ctdb);
54 if (strcmp(ctdb->transport, "ib") == 0) {
55 int ctdb_ibw_init(struct ctdb_context *);
56 ret = ctdb_ibw_init(ctdb);
60 DEBUG(0,("Failed to initialise transport '%s'\n", ctdb->transport));
64 /* start the transport running */
65 ctdb->methods->start(ctdb);
67 /* go into a wait loop to allow other nodes to complete */
68 event_loop_wait(ctdb->ev);
70 DEBUG(0,("event_loop_wait() returned. this should not happen\n"));
75 static void set_non_blocking(int fd)
78 v = fcntl(fd, F_GETFL, 0);
79 fcntl(fd, F_SETFL, v | O_NONBLOCK);
82 static void block_signal(int signum)
86 memset(&act, 0, sizeof(act));
88 act.sa_handler = SIG_IGN;
89 sigemptyset(&act.sa_mask);
90 sigaddset(&act.sa_mask, signum);
91 sigaction(signum, &act, NULL);
96 send a packet to a client
98 static int daemon_queue_send(struct ctdb_client *client, struct ctdb_req_header *hdr)
100 client->ctdb->status.client_packets_sent++;
101 return ctdb_queue_send(client->queue, (uint8_t *)hdr, hdr->length);
105 message handler for when we are in daemon mode. This redirects the message
108 static void daemon_message_handler(struct ctdb_context *ctdb, uint32_t srvid,
109 TDB_DATA data, void *private_data)
111 struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
112 struct ctdb_req_message *r;
115 /* construct a message to send to the client containing the data */
116 len = offsetof(struct ctdb_req_message, data) + data.dsize;
117 r = ctdbd_allocate_pkt(ctdb, len);
119 talloc_set_name_const(r, "req_message packet");
121 memset(r, 0, offsetof(struct ctdb_req_message, data));
124 r->hdr.ctdb_magic = CTDB_MAGIC;
125 r->hdr.ctdb_version = CTDB_VERSION;
126 r->hdr.operation = CTDB_REQ_MESSAGE;
128 r->datalen = data.dsize;
129 memcpy(&r->data[0], data.dptr, data.dsize);
131 daemon_queue_send(client, &r->hdr);
138 this is called when the ctdb daemon received a ctdb request to
139 set the srvid from the client
141 static void daemon_request_register_message_handler(struct ctdb_client *client,
142 struct ctdb_req_register *c)
145 res = ctdb_register_message_handler(client->ctdb, client,
146 c->srvid, daemon_message_handler,
149 DEBUG(0,(__location__ " Failed to register handler %u in daemon\n",
152 DEBUG(2,(__location__ " Registered message handler for srvid=%u\n",
159 called when the daemon gets a shutdown request from a client
161 static void daemon_request_shutdown(struct ctdb_client *client,
162 struct ctdb_req_shutdown *f)
164 struct ctdb_context *ctdb = talloc_get_type(client->ctdb, struct ctdb_context);
168 /* we dont send to ourself so we can already count one daemon as
170 ctdb->num_finished++;
173 /* loop over all nodes of the cluster */
174 for (node=0; node<ctdb->num_nodes;node++) {
175 struct ctdb_req_finished *rf;
177 /* dont send a message to ourself */
178 if (ctdb->vnn == node) {
182 len = sizeof(struct ctdb_req_finished);
183 rf = ctdb->methods->allocate_pkt(ctdb, len);
184 CTDB_NO_MEMORY_FATAL(ctdb, rf);
185 talloc_set_name_const(rf, "ctdb_req_finished packet");
188 rf->hdr.length = len;
189 rf->hdr.ctdb_magic = CTDB_MAGIC;
190 rf->hdr.ctdb_version = CTDB_VERSION;
191 rf->hdr.operation = CTDB_REQ_FINISHED;
192 rf->hdr.destnode = node;
193 rf->hdr.srcnode = ctdb->vnn;
196 ctdb_queue_packet(ctdb, &(rf->hdr));
201 /* wait until all nodes have are prepared to shutdown */
202 while (ctdb->num_finished != ctdb->num_nodes) {
203 event_loop_once(ctdb->ev);
206 /* all daemons have requested to finish - we now exit */
207 DEBUG(1,("All daemons finished - exiting\n"));
214 called when the daemon gets a connect wait request from a client
216 static void daemon_request_connect_wait(struct ctdb_client *client,
217 struct ctdb_req_connect_wait *c)
219 struct ctdb_reply_connect_wait r;
222 /* first wait - in the daemon */
223 ctdb_daemon_connect_wait(client->ctdb);
225 /* now send the reply */
228 r.hdr.length = sizeof(r);
229 r.hdr.ctdb_magic = CTDB_MAGIC;
230 r.hdr.ctdb_version = CTDB_VERSION;
231 r.hdr.operation = CTDB_REPLY_CONNECT_WAIT;
232 r.vnn = ctdb_get_vnn(client->ctdb);
233 r.num_connected = client->ctdb->num_connected;
235 res = daemon_queue_send(client, &r.hdr);
237 DEBUG(0,(__location__ " Failed to queue a connect wait response\n"));
244 destroy a ctdb_client
246 static int ctdb_client_destructor(struct ctdb_client *client)
255 this is called when the ctdb daemon received a ctdb request message
256 from a local client over the unix domain socket
258 static void daemon_request_message_from_client(struct ctdb_client *client,
259 struct ctdb_req_message *c)
264 /* maybe the message is for another client on this node */
265 if (ctdb_get_vnn(client->ctdb)==c->hdr.destnode) {
266 ctdb_request_message(client->ctdb, (struct ctdb_req_header *)c);
270 /* its for a remote node */
271 data.dptr = &c->data[0];
272 data.dsize = c->datalen;
273 res = ctdb_daemon_send_message(client->ctdb, c->hdr.destnode,
276 DEBUG(0,(__location__ " Failed to send message to remote node %u\n",
282 struct daemon_call_state {
283 struct ctdb_client *client;
285 struct ctdb_call *call;
286 struct timeval start_time;
290 complete a call from a client
292 static void daemon_call_from_client_callback(struct ctdb_call_state *state)
294 struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
295 struct daemon_call_state);
296 struct ctdb_reply_call *r;
299 struct ctdb_client *client = dstate->client;
301 talloc_steal(client, dstate);
302 talloc_steal(dstate, dstate->call);
304 res = ctdb_daemon_call_recv(state, dstate->call);
306 DEBUG(0, (__location__ " ctdbd_call_recv() returned error\n"));
307 client->ctdb->status.pending_calls--;
308 ctdb_latency(&client->ctdb->status.max_call_latency, dstate->start_time);
312 length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
313 r = ctdbd_allocate_pkt(dstate, length);
315 DEBUG(0, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
316 client->ctdb->status.pending_calls--;
317 ctdb_latency(&client->ctdb->status.max_call_latency, dstate->start_time);
320 memset(r, 0, offsetof(struct ctdb_reply_call, data));
321 r->hdr.length = length;
322 r->hdr.ctdb_magic = CTDB_MAGIC;
323 r->hdr.ctdb_version = CTDB_VERSION;
324 r->hdr.operation = CTDB_REPLY_CALL;
325 r->hdr.reqid = dstate->reqid;
326 r->datalen = dstate->call->reply_data.dsize;
327 memcpy(&r->data[0], dstate->call->reply_data.dptr, r->datalen);
329 res = daemon_queue_send(client, &r->hdr);
331 DEBUG(0, (__location__ "Failed to queue packet from daemon to client\n"));
333 ctdb_latency(&client->ctdb->status.max_call_latency, dstate->start_time);
335 client->ctdb->status.pending_calls--;
340 this is called when the ctdb daemon received a ctdb request call
341 from a local client over the unix domain socket
343 static void daemon_request_call_from_client(struct ctdb_client *client,
344 struct ctdb_req_call *c)
346 struct ctdb_call_state *state;
347 struct ctdb_db_context *ctdb_db;
348 struct daemon_call_state *dstate;
349 struct ctdb_call *call;
350 struct ctdb_ltdb_header header;
353 struct ctdb_context *ctdb = client->ctdb;
355 ctdb->status.total_calls++;
356 ctdb->status.pending_calls++;
358 ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
360 DEBUG(0, (__location__ " Unknown database in request. db_id==0x%08x",
362 ctdb->status.pending_calls--;
367 key.dsize = c->keylen;
369 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
370 (struct ctdb_req_header *)c, &data,
371 daemon_incoming_packet, client);
373 /* will retry later */
374 ctdb->status.pending_calls--;
379 DEBUG(0,(__location__ " Unable to fetch record\n"));
380 ctdb->status.pending_calls--;
384 dstate = talloc(client, struct daemon_call_state);
385 if (dstate == NULL) {
386 ctdb_ltdb_unlock(ctdb_db, key);
387 DEBUG(0,(__location__ " Unable to allocate dstate\n"));
388 ctdb->status.pending_calls--;
391 dstate->start_time = timeval_current();
392 dstate->client = client;
393 dstate->reqid = c->hdr.reqid;
394 talloc_steal(dstate, data.dptr);
396 call = dstate->call = talloc_zero(dstate, struct ctdb_call);
398 ctdb_ltdb_unlock(ctdb_db, key);
399 DEBUG(0,(__location__ " Unable to allocate call\n"));
400 ctdb->status.pending_calls--;
401 ctdb_latency(&ctdb->status.max_call_latency, dstate->start_time);
405 call->call_id = c->callid;
407 call->call_data.dptr = c->data + c->keylen;
408 call->call_data.dsize = c->calldatalen;
409 call->flags = c->flags;
411 if (header.dmaster == ctdb->vnn && !(ctdb->flags & CTDB_FLAG_SELF_CONNECT)) {
412 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
414 state = ctdb_daemon_call_send_remote(ctdb_db, call, &header);
417 ctdb_ltdb_unlock(ctdb_db, key);
420 DEBUG(0,(__location__ " Unable to setup call send\n"));
421 ctdb->status.pending_calls--;
422 ctdb_latency(&ctdb->status.max_call_latency, dstate->start_time);
425 talloc_steal(state, dstate);
426 talloc_steal(client, state);
428 state->async.fn = daemon_call_from_client_callback;
429 state->async.private_data = dstate;
433 static void daemon_request_control_from_client(struct ctdb_client *client,
434 struct ctdb_req_control *c);
436 /* data contains a packet from the client */
437 static void daemon_incoming_packet(void *p, uint8_t *data, uint32_t nread)
439 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
440 struct ctdb_client *client = talloc_get_type(p, struct ctdb_client);
442 struct ctdb_context *ctdb = client->ctdb;
444 /* place the packet as a child of a tmp_ctx. We then use
445 talloc_free() below to free it. If any of the calls want
446 to keep it, then they will steal it somewhere else, and the
447 talloc_free() will be a no-op */
448 tmp_ctx = talloc_new(client);
449 talloc_steal(tmp_ctx, hdr);
451 if (hdr->ctdb_magic != CTDB_MAGIC) {
452 ctdb_set_error(client->ctdb, "Non CTDB packet rejected in daemon\n");
456 if (hdr->ctdb_version != CTDB_VERSION) {
457 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
461 switch (hdr->operation) {
463 ctdb->status.client.req_call++;
464 daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
467 case CTDB_REQ_REGISTER:
468 ctdb->status.client.req_register++;
469 daemon_request_register_message_handler(client,
470 (struct ctdb_req_register *)hdr);
473 case CTDB_REQ_MESSAGE:
474 ctdb->status.client.req_message++;
475 daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
478 case CTDB_REQ_CONNECT_WAIT:
479 ctdb->status.client.req_connect_wait++;
480 daemon_request_connect_wait(client, (struct ctdb_req_connect_wait *)hdr);
483 case CTDB_REQ_SHUTDOWN:
484 ctdb->status.client.req_shutdown++;
485 daemon_request_shutdown(client, (struct ctdb_req_shutdown *)hdr);
488 case CTDB_REQ_CONTROL:
489 ctdb->status.client.req_control++;
490 daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
494 DEBUG(0,(__location__ " daemon: unrecognized operation %d\n",
499 talloc_free(tmp_ctx);
503 called when the daemon gets a incoming packet
505 static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
507 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
508 struct ctdb_req_header *hdr;
515 client->ctdb->status.client_packets_recv++;
517 if (cnt < sizeof(*hdr)) {
518 ctdb_set_error(client->ctdb, "Bad packet length %u in daemon\n", cnt);
521 hdr = (struct ctdb_req_header *)data;
522 if (cnt != hdr->length) {
523 ctdb_set_error(client->ctdb, "Bad header length %u expected %u\n in daemon",
528 if (hdr->ctdb_magic != CTDB_MAGIC) {
529 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
533 if (hdr->ctdb_version != CTDB_VERSION) {
534 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
538 DEBUG(3,(__location__ " client request %d of type %d length %d from "
539 "node %d to %d\n", hdr->reqid, hdr->operation, hdr->length,
540 hdr->srcnode, hdr->destnode));
542 /* it is the responsibility of the incoming packet function to free 'data' */
543 daemon_incoming_packet(client, data, cnt);
546 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
547 uint16_t flags, void *private_data)
549 struct sockaddr_in addr;
552 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
553 struct ctdb_client *client;
555 memset(&addr, 0, sizeof(addr));
557 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
561 set_non_blocking(fd);
563 client = talloc_zero(ctdb, struct ctdb_client);
567 client->queue = ctdb_queue_setup(ctdb, client, fd, CTDB_DS_ALIGNMENT,
568 ctdb_daemon_read_cb, client);
570 talloc_set_destructor(client, ctdb_client_destructor);
575 static void ctdb_read_from_parent(struct event_context *ev, struct fd_event *fde,
576 uint16_t flags, void *private_data)
578 int *fd = private_data;
582 /* XXX this is a good place to try doing some cleaning up before exiting */
583 cnt = read(*fd, &buf, 1);
585 DEBUG(2,(__location__ " parent process exited. filedescriptor dissappeared\n"));
588 DEBUG(0,(__location__ " ctdb: did not expect data from parent process\n"));
596 create a unix domain socket and bind it
597 return a file descriptor open on the socket
599 static int ux_socket_bind(struct ctdb_context *ctdb)
601 struct sockaddr_un addr;
603 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
604 if (ctdb->daemon.sd == -1) {
605 ctdb->daemon.sd = -1;
609 set_non_blocking(ctdb->daemon.sd);
611 memset(&addr, 0, sizeof(addr));
612 addr.sun_family = AF_UNIX;
613 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
615 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
616 close(ctdb->daemon.sd);
617 ctdb->daemon.sd = -1;
620 listen(ctdb->daemon.sd, 1);
626 delete the socket on exit - called on destruction of autofree context
628 static int unlink_destructor(const char *name)
635 start the protocol going
637 int ctdb_start(struct ctdb_context *ctdb)
642 struct fd_event *fde;
643 const char *domain_socket_name;
645 /* get rid of any old sockets */
646 unlink(ctdb->daemon.name);
648 /* create a unix domain stream socket to listen to */
649 res = ux_socket_bind(ctdb);
651 DEBUG(0,(__location__ " Failed to open CTDB unix domain socket\n"));
657 DEBUG(0,(__location__ " Failed to open pipe for CTDB\n"));
662 DEBUG(0,(__location__ " Failed to fork CTDB daemon\n"));
668 close(ctdb->daemon.sd);
669 ctdb->daemon.sd = -1;
673 block_signal(SIGPIPE);
675 /* ensure the socket is deleted on exit of the daemon */
676 domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
677 talloc_set_destructor(domain_socket_name, unlink_destructor);
681 ctdb->ev = event_context_init(NULL);
682 fde = event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ, ctdb_read_from_parent, &fd[0]);
683 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, EVENT_FD_READ, ctdb_accept_client, ctdb);
684 ctdb_main_loop(ctdb);
690 allocate a packet for use in client<->daemon communication
692 void *ctdbd_allocate_pkt(TALLOC_CTX *mem_ctx, size_t len)
696 size = (len+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
697 return talloc_size(mem_ctx, size);
701 called when a CTDB_REQ_FINISHED packet comes in
703 void ctdb_request_finished(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
705 ctdb->num_finished++;
709 struct daemon_control_state {
710 struct ctdb_client *client;
711 struct ctdb_req_control *c;
716 callback when a control reply comes in
718 static void daemon_control_callback(struct ctdb_context *ctdb,
719 uint32_t status, TDB_DATA data,
722 struct daemon_control_state *state = talloc_get_type(private_data,
723 struct daemon_control_state);
724 struct ctdb_client *client = state->client;
725 struct ctdb_reply_control *r;
728 /* construct a message to send to the client containing the data */
729 len = offsetof(struct ctdb_reply_control, data) + data.dsize;
730 r = ctdbd_allocate_pkt(client, len);
731 talloc_set_name_const(r, "reply_control packet");
733 memset(r, 0, offsetof(struct ctdb_reply_control, data));
736 r->hdr.ctdb_magic = CTDB_MAGIC;
737 r->hdr.ctdb_version = CTDB_VERSION;
738 r->hdr.operation = CTDB_REPLY_CONTROL;
739 r->hdr.reqid = state->reqid;
741 r->datalen = data.dsize;
742 memcpy(&r->data[0], data.dptr, data.dsize);
744 daemon_queue_send(client, &r->hdr);
750 this is called when the ctdb daemon received a ctdb request control
751 from a local client over the unix domain socket
753 static void daemon_request_control_from_client(struct ctdb_client *client,
754 struct ctdb_req_control *c)
758 struct daemon_control_state *state;
760 if (c->hdr.destnode == CTDB_CURRENT_NODE) {
761 c->hdr.destnode = client->ctdb->vnn;
764 state = talloc(client, struct daemon_control_state);
765 CTDB_NO_MEMORY_VOID(client->ctdb, state);
767 state->client = client;
768 state->c = talloc_steal(state, c);
769 state->reqid = c->hdr.reqid;
771 data.dptr = &c->data[0];
772 data.dsize = c->datalen;
773 res = ctdb_daemon_send_control(client->ctdb, c->hdr.destnode,
774 c->srvid, c->opcode, data, daemon_control_callback,
777 DEBUG(0,(__location__ " Failed to send control to remote node %u\n",