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)
47 ctdb->methods->start(ctdb);
49 /* go into a wait loop to allow other nodes to complete */
50 event_loop_wait(ctdb->ev);
52 DEBUG(0,("event_loop_wait() returned. this should not happen\n"));
57 static void set_non_blocking(int fd)
60 v = fcntl(fd, F_GETFL, 0);
61 fcntl(fd, F_SETFL, v | O_NONBLOCK);
64 static void block_signal(int signum)
68 memset(&act, 0, sizeof(act));
70 act.sa_handler = SIG_IGN;
71 sigemptyset(&act.sa_mask);
72 sigaddset(&act.sa_mask, signum);
73 sigaction(signum, &act, NULL);
78 message handler for when we are in daemon mode. This redirects the message
81 static void daemon_message_handler(struct ctdb_context *ctdb, uint32_t srvid,
82 TDB_DATA data, void *private_data)
84 struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
85 struct ctdb_req_message *r;
88 /* construct a message to send to the client containing the data */
89 len = offsetof(struct ctdb_req_message, data) + data.dsize;
90 r = ctdbd_allocate_pkt(ctdb, len);
92 talloc_set_name_const(r, "req_message packet");
94 memset(r, 0, offsetof(struct ctdb_req_message, data));
97 r->hdr.ctdb_magic = CTDB_MAGIC;
98 r->hdr.ctdb_version = CTDB_VERSION;
99 r->hdr.operation = CTDB_REQ_MESSAGE;
101 r->datalen = data.dsize;
102 memcpy(&r->data[0], data.dptr, data.dsize);
104 ctdb_queue_send(client->queue, (uint8_t *)&r->hdr, len);
111 this is called when the ctdb daemon received a ctdb request to
112 set the srvid from the client
114 static void daemon_request_register_message_handler(struct ctdb_client *client,
115 struct ctdb_req_register *c)
118 res = ctdb_register_message_handler(client->ctdb, client,
119 c->srvid, daemon_message_handler,
122 DEBUG(0,(__location__ " Failed to register handler %u in daemon\n",
125 DEBUG(2,(__location__ " Registered message handler for srvid=%u\n",
132 called when the daemon gets a shutdown request from a client
134 static void daemon_request_shutdown(struct ctdb_client *client,
135 struct ctdb_req_shutdown *f)
137 struct ctdb_context *ctdb = talloc_get_type(client->ctdb, struct ctdb_context);
141 /* we dont send to ourself so we can already count one daemon as
143 ctdb->num_finished++;
146 /* loop over all nodes of the cluster */
147 for (node=0; node<ctdb->num_nodes;node++) {
148 struct ctdb_req_finished *rf;
150 /* dont send a message to ourself */
151 if (ctdb->vnn == node) {
155 len = sizeof(struct ctdb_req_finished);
156 rf = ctdb->methods->allocate_pkt(ctdb, len);
157 CTDB_NO_MEMORY_FATAL(ctdb, rf);
158 talloc_set_name_const(rf, "ctdb_req_finished packet");
161 rf->hdr.length = len;
162 rf->hdr.ctdb_magic = CTDB_MAGIC;
163 rf->hdr.ctdb_version = CTDB_VERSION;
164 rf->hdr.operation = CTDB_REQ_FINISHED;
165 rf->hdr.destnode = node;
166 rf->hdr.srcnode = ctdb->vnn;
169 ctdb_queue_packet(ctdb, &(rf->hdr));
174 /* wait until all nodes have are prepared to shutdown */
175 while (ctdb->num_finished != ctdb->num_nodes) {
176 event_loop_once(ctdb->ev);
179 /* all daemons have requested to finish - we now exit */
180 DEBUG(1,("All daemons finished - exiting\n"));
187 called when the daemon gets a connect wait request from a client
189 static void daemon_request_connect_wait(struct ctdb_client *client,
190 struct ctdb_req_connect_wait *c)
192 struct ctdb_reply_connect_wait r;
195 /* first wait - in the daemon */
196 ctdb_daemon_connect_wait(client->ctdb);
198 /* now send the reply */
201 r.hdr.length = sizeof(r);
202 r.hdr.ctdb_magic = CTDB_MAGIC;
203 r.hdr.ctdb_version = CTDB_VERSION;
204 r.hdr.operation = CTDB_REPLY_CONNECT_WAIT;
205 r.vnn = ctdb_get_vnn(client->ctdb);
206 r.num_connected = client->ctdb->num_connected;
208 res = ctdb_queue_send(client->queue, (uint8_t *)&r.hdr, r.hdr.length);
210 DEBUG(0,(__location__ " Failed to queue a connect wait response\n"));
216 destroy a ctdb_client
218 static int ctdb_client_destructor(struct ctdb_client *client)
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_vnn(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(0,(__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;
261 complete a call from a client
263 static void daemon_call_from_client_callback(struct ctdb_call_state *state)
265 struct daemon_call_state *dstate = talloc_get_type(state->async.private_data,
266 struct daemon_call_state);
267 struct ctdb_reply_call *r;
270 struct ctdb_client *client = dstate->client;
272 talloc_steal(client, dstate);
273 talloc_steal(dstate, dstate->call);
275 res = ctdb_daemon_call_recv(state, dstate->call);
277 DEBUG(0, (__location__ " ctdbd_call_recv() returned error\n"));
281 length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
282 r = ctdbd_allocate_pkt(dstate, length);
284 DEBUG(0, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
287 memset(r, 0, offsetof(struct ctdb_reply_call, data));
288 r->hdr.length = length;
289 r->hdr.ctdb_magic = CTDB_MAGIC;
290 r->hdr.ctdb_version = CTDB_VERSION;
291 r->hdr.operation = CTDB_REPLY_CALL;
292 r->hdr.reqid = dstate->reqid;
293 r->datalen = dstate->call->reply_data.dsize;
294 memcpy(&r->data[0], dstate->call->reply_data.dptr, r->datalen);
296 res = ctdb_queue_send(client->queue, (uint8_t *)&r->hdr, r->hdr.length);
298 DEBUG(0, (__location__ "Failed to queue packet from daemon to client\n"));
305 this is called when the ctdb daemon received a ctdb request call
306 from a local client over the unix domain socket
308 static void daemon_request_call_from_client(struct ctdb_client *client,
309 struct ctdb_req_call *c)
311 struct ctdb_call_state *state;
312 struct ctdb_db_context *ctdb_db;
313 struct daemon_call_state *dstate;
314 struct ctdb_call *call;
315 struct ctdb_ltdb_header header;
318 struct ctdb_context *ctdb = client->ctdb;
320 ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
322 DEBUG(0, (__location__ " Unknown database in request. db_id==0x%08x",
328 key.dsize = c->keylen;
330 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header,
331 (struct ctdb_req_header *)c, &data,
332 daemon_incoming_packet, client);
334 /* will retry later */
339 DEBUG(0,(__location__ " Unable to fetch record\n"));
343 dstate = talloc(client, struct daemon_call_state);
344 if (dstate == NULL) {
345 ctdb_ltdb_unlock(ctdb_db, key);
346 DEBUG(0,(__location__ " Unable to allocate dstate\n"));
349 dstate->client = client;
350 dstate->reqid = c->hdr.reqid;
351 talloc_steal(dstate, data.dptr);
353 call = dstate->call = talloc_zero(dstate, struct ctdb_call);
355 ctdb_ltdb_unlock(ctdb_db, key);
356 DEBUG(0,(__location__ " Unable to allocate call\n"));
360 call->call_id = c->callid;
362 call->call_data.dptr = c->data + c->keylen;
363 call->call_data.dsize = c->calldatalen;
365 if (header.dmaster == ctdb->vnn && !(ctdb->flags & CTDB_FLAG_SELF_CONNECT)) {
366 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
368 state = ctdb_daemon_call_send_remote(ctdb_db, call, &header);
371 ctdb_ltdb_unlock(ctdb_db, key);
374 DEBUG(0,(__location__ " Unable to setup call send\n"));
377 talloc_steal(state, dstate);
378 talloc_steal(client, state);
380 state->async.fn = daemon_call_from_client_callback;
381 state->async.private_data = dstate;
384 /* data contains a packet from the client */
385 static void daemon_incoming_packet(void *p, uint8_t *data, uint32_t nread)
387 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
388 struct ctdb_client *client = talloc_get_type(p, struct ctdb_client);
391 /* place the packet as a child of a tmp_ctx. We then use
392 talloc_free() below to free it. If any of the calls want
393 to keep it, then they will steal it somewhere else, and the
394 talloc_free() will be a no-op */
395 tmp_ctx = talloc_new(client);
396 talloc_steal(tmp_ctx, hdr);
398 if (hdr->ctdb_magic != CTDB_MAGIC) {
399 ctdb_set_error(client->ctdb, "Non CTDB packet rejected in daemon\n");
403 if (hdr->ctdb_version != CTDB_VERSION) {
404 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
408 switch (hdr->operation) {
410 daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
413 case CTDB_REQ_REGISTER:
414 daemon_request_register_message_handler(client,
415 (struct ctdb_req_register *)hdr);
417 case CTDB_REQ_MESSAGE:
418 daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
421 case CTDB_REQ_CONNECT_WAIT:
422 daemon_request_connect_wait(client, (struct ctdb_req_connect_wait *)hdr);
425 case CTDB_REQ_SHUTDOWN:
426 daemon_request_shutdown(client, (struct ctdb_req_shutdown *)hdr);
430 DEBUG(0,(__location__ " daemon: unrecognized operation %d\n",
435 talloc_free(tmp_ctx);
439 static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
441 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
442 struct ctdb_req_header *hdr;
449 if (cnt < sizeof(*hdr)) {
450 ctdb_set_error(client->ctdb, "Bad packet length %d in daemon\n", cnt);
453 hdr = (struct ctdb_req_header *)data;
454 if (cnt != hdr->length) {
455 ctdb_set_error(client->ctdb, "Bad header length %d expected %d\n in daemon",
460 if (hdr->ctdb_magic != CTDB_MAGIC) {
461 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
465 if (hdr->ctdb_version != CTDB_VERSION) {
466 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
470 /* it is the responsibility of the incoming packet function to free 'data' */
471 daemon_incoming_packet(client, data, cnt);
474 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
475 uint16_t flags, void *private_data)
477 struct sockaddr_in addr;
480 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
481 struct ctdb_client *client;
483 memset(&addr, 0, sizeof(addr));
485 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
489 set_non_blocking(fd);
491 client = talloc_zero(ctdb, struct ctdb_client);
495 client->queue = ctdb_queue_setup(ctdb, client, fd, CTDB_DS_ALIGNMENT,
496 ctdb_daemon_read_cb, client);
498 talloc_set_destructor(client, ctdb_client_destructor);
503 static void ctdb_read_from_parent(struct event_context *ev, struct fd_event *fde,
504 uint16_t flags, void *private_data)
506 int *fd = private_data;
510 /* XXX this is a good place to try doing some cleaning up before exiting */
511 cnt = read(*fd, &buf, 1);
513 DEBUG(2,(__location__ " parent process exited. filedescriptor dissappeared\n"));
516 DEBUG(0,(__location__ " ctdb: did not expect data from parent process\n"));
524 create a unix domain socket and bind it
525 return a file descriptor open on the socket
527 static int ux_socket_bind(struct ctdb_context *ctdb)
529 struct sockaddr_un addr;
531 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
532 if (ctdb->daemon.sd == -1) {
533 ctdb->daemon.sd = -1;
537 set_non_blocking(ctdb->daemon.sd);
539 memset(&addr, 0, sizeof(addr));
540 addr.sun_family = AF_UNIX;
541 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
543 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
544 close(ctdb->daemon.sd);
545 ctdb->daemon.sd = -1;
548 listen(ctdb->daemon.sd, 1);
554 delete the socket on exit - called on destruction of autofree context
556 static int unlink_destructor(const char *name)
563 start the protocol going
565 int ctdb_start(struct ctdb_context *ctdb)
570 struct fd_event *fde;
571 const char *domain_socket_name;
573 /* generate a name to use for our local socket */
574 ctdb->daemon.name = talloc_asprintf(ctdb, "%s.%s", CTDB_PATH, ctdb->address.address);
575 /* get rid of any old sockets */
576 unlink(ctdb->daemon.name);
578 /* create a unix domain stream socket to listen to */
579 res = ux_socket_bind(ctdb);
581 DEBUG(0,(__location__ " Failed to open CTDB unix domain socket\n"));
587 DEBUG(0,(__location__ " Failed to open pipe for CTDB\n"));
592 DEBUG(0,(__location__ " Failed to fork CTDB daemon\n"));
598 close(ctdb->daemon.sd);
599 ctdb->daemon.sd = -1;
603 block_signal(SIGPIPE);
605 /* ensure the socket is deleted on exit of the daemon */
606 domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
607 talloc_set_destructor(domain_socket_name, unlink_destructor);
611 ctdb->ev = event_context_init(NULL);
612 fde = event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ, ctdb_read_from_parent, &fd[0]);
613 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, EVENT_FD_READ, ctdb_accept_client, ctdb);
614 ctdb_main_loop(ctdb);
620 allocate a packet for use in client<->daemon communication
622 void *ctdbd_allocate_pkt(TALLOC_CTX *mem_ctx, size_t len)
626 size = (len+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
627 return talloc_size(mem_ctx, size);
631 called when a CTDB_REQ_FINISHED packet comes in
633 void ctdb_request_finished(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
635 ctdb->num_finished++;