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 "../include/ctdb.h"
29 #include "../include/ctdb_private.h"
31 #define CTDB_PATH "/tmp/ctdb.socket"
34 static void ctdb_main_loop(struct ctdb_context *ctdb)
36 ctdb->methods->start(ctdb);
38 /* go into a wait loop to allow other nodes to complete */
39 event_loop_wait(ctdb->ev);
41 printf("event_loop_wait() returned. this should not happen\n");
46 static void set_non_blocking(int fd)
49 v = fcntl(fd, F_GETFL, 0);
50 fcntl(fd, F_SETFL, v | O_NONBLOCK);
56 struct ctdb_context *ctdb;
59 struct ctdb_partial partial;
66 static int ctdb_client_destructor(struct ctdb_client *client)
75 static void client_request_call(struct ctdb_client *client, struct ctdb_req_call *c)
77 struct ctdb_call_state *state;
78 struct ctdb_db_context *ctdb_db;
79 struct ctdb_call call;
80 struct ctdb_reply_call r;
83 for (ctdb_db=client->ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
84 if (ctdb_db->db_id == c->db_id) {
89 printf("Unknown database in request. db_id==0x%08x",c->db_id);
96 call.call_id = c->callid;
97 call.key.dptr = c->data;
98 call.key.dsize = c->keylen;
99 call.call_data.dptr = c->data + c->keylen;
100 call.call_data.dsize = c->calldatalen;
102 state = ctdb_call_send(ctdb_db, &call);
104 /* XXX this must be converted to fully async */
105 res = ctdb_call_recv(state, &call);
107 printf("ctdbd_call_recv() returned error\n");
115 r.datalen = call.reply_data.dsize;
117 r.hdr.length = offsetof(struct ctdb_reply_call, data) + r.datalen;
118 r.hdr.ctdb_magic = c->hdr.ctdb_magic;
119 r.hdr.ctdb_version = c->hdr.ctdb_version;
120 r.hdr.operation = CTDB_REPLY_CALL;
125 r.hdr.reqid = c->hdr.reqid;
128 /*XXX need to handle the case of partial writes logic for partial writes in tcp/ctdb_tcp_node_write */
129 res = write(client->fd, &r, offsetof(struct ctdb_reply_call, data));
131 res = write(client->fd, call.reply_data.dptr, r.datalen);
136 /* data contains a packet from the client */
137 static void client_incoming_packet(struct ctdb_client *client, void *data, size_t nread)
139 struct ctdb_req_header *hdr = data;
141 if (hdr->ctdb_magic != CTDB_MAGIC) {
142 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
146 if (hdr->ctdb_version != CTDB_VERSION) {
147 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
151 switch (hdr->operation) {
153 client_request_call(client, (struct ctdb_req_call *)hdr);
162 static void ctdb_client_read_cb(uint8_t *data, int cnt, void *args)
164 struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
165 struct ctdb_req_header *hdr;
167 if (cnt < sizeof(*hdr)) {
168 ctdb_set_error(client->ctdb, "Bad packet length %d\n", cnt);
171 hdr = (struct ctdb_req_header *)data;
172 if (cnt != hdr->length) {
173 ctdb_set_error(client->ctdb, "Bad header length %d expected %d\n",
178 if (hdr->ctdb_magic != CTDB_MAGIC) {
179 ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
183 if (hdr->ctdb_version != CTDB_VERSION) {
184 ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
188 /* it is the responsibility of the incoming packet function to free 'data' */
189 client_incoming_packet(client, data, cnt);
192 static void ctdb_client_read(struct event_context *ev, struct fd_event *fde,
193 uint16_t flags, void *private)
195 struct ctdb_client *client = talloc_get_type(private, struct ctdb_client);
197 ctdb_read_pdu(client->fd, client, &client->partial, ctdb_client_read_cb, client);
201 static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
202 uint16_t flags, void *private)
204 struct sockaddr_in addr;
207 struct ctdb_context *ctdb = talloc_get_type(private, struct ctdb_context);
208 struct ctdb_client *client;
210 memset(&addr, 0, sizeof(addr));
212 fd = accept(ctdb->daemon.sd, (struct sockaddr *)&addr, &len);
216 set_non_blocking(fd);
218 client = talloc_zero(ctdb, struct ctdb_client);
222 event_add_fd(ctdb->ev, client, client->fd, EVENT_FD_READ,
223 ctdb_client_read, client);
225 talloc_set_destructor(client, ctdb_client_destructor);
230 static void ctdb_read_from_parent(struct event_context *ev, struct fd_event *fde,
231 uint16_t flags, void *private)
237 /* XXX this is a good place to try doing some cleaning up before exiting */
238 cnt = read(*fd, &buf, 1);
240 printf("parent process exited. filedescriptor dissappeared\n");
243 printf("ctdb: did not expect data from parent process\n");
251 create a unix domain socket and bind it
252 return a file descriptor open on the socket
254 static int ux_socket_bind(struct ctdb_context *ctdb)
256 struct sockaddr_un addr;
258 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
259 if (ctdb->daemon.sd == -1) {
260 ctdb->daemon.sd = -1;
264 set_non_blocking(ctdb->daemon.sd);
266 memset(&addr, 0, sizeof(addr));
267 addr.sun_family = AF_UNIX;
268 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
270 if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
271 close(ctdb->daemon.sd);
272 ctdb->daemon.sd = -1;
275 listen(ctdb->daemon.sd, 1);
280 static char *domain_socket_name=NULL;
281 static void unlink_domain_socket(void)
283 if (domain_socket_name) {
284 unlink(domain_socket_name);
289 start the protocol going
291 int ctdbd_start(struct ctdb_context *ctdb)
296 struct fd_event *fde;
298 /* generate a name to use for our local socket */
299 ctdb->daemon.name = talloc_asprintf(ctdb, "%s.%s", CTDB_PATH, ctdb->address.address);
300 /* get rid of any old sockets */
301 unlink(ctdb->daemon.name);
303 domain_socket_name = ctdb->daemon.name;
304 atexit(unlink_domain_socket);
306 /* create a unix domain stream socket to listen to */
307 res = ux_socket_bind(ctdb);
309 printf("Failed to open CTDB unix domain socket\n");
315 printf("Failed to open pipe for CTDB\n");
320 printf("Failed to fork CTDB daemon\n");
326 close(ctdb->daemon.sd);
327 ctdb->daemon.sd = -1;
333 ctdb_clear_flags(ctdb, CTDB_FLAG_DAEMON_MODE);
334 ctdb->ev = event_context_init(NULL);
335 fde = event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ, ctdb_read_from_parent, &fd[0]);
336 fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, EVENT_FD_READ, ctdb_accept_client, ctdb);
337 ctdb_main_loop(ctdb);
343 static void ctdb_daemon_read_cb(uint8_t *data, int cnt, void *args)
345 struct ctdb_context *ctdb = talloc_get_type(args, struct ctdb_context);
346 struct ctdb_req_header *hdr;
348 if (cnt < sizeof(*hdr)) {
349 ctdb_set_error(ctdb, "Bad packet length %d\n", cnt);
352 hdr = (struct ctdb_req_header *)data;
353 if (cnt != hdr->length) {
354 ctdb_set_error(ctdb, "Bad header length %d expected %d\n",
359 if (hdr->ctdb_magic != CTDB_MAGIC) {
360 ctdb_set_error(ctdb, "Non CTDB packet rejected\n");
364 if (hdr->ctdb_version != CTDB_VERSION) {
365 ctdb_set_error(ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
369 ctdb_reply_call(ctdb, hdr);
374 static void ctdb_daemon_io(struct event_context *ev, struct fd_event *fde,
375 uint16_t flags, void *private)
377 struct ctdb_context *ctdb = talloc_get_type(private, struct ctdb_context);
380 if (flags&EVENT_FD_READ) {
381 ctdb_read_pdu(ctdb->daemon.sd, ctdb, &ctdb->daemon.partial, ctdb_daemon_read_cb, ctdb);
383 if (flags&EVENT_FD_WRITE) {
384 printf("socket is filled. fix this see tcp_io/ctdb_tcp_node_write how to do this\n");
385 /* ctdb_daemon_write(ctdb);*/
390 connect to a unix domain socket
392 static int ux_socket_connect(struct ctdb_context *ctdb)
394 struct sockaddr_un addr;
396 memset(&addr, 0, sizeof(addr));
397 addr.sun_family = AF_UNIX;
398 strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
400 ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
401 if (ctdb->daemon.sd == -1) {
405 if (connect(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
406 close(ctdb->daemon.sd);
407 ctdb->daemon.sd = -1;
411 ctdb->daemon.fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, EVENT_FD_READ,
412 ctdb_daemon_io, ctdb);
419 static int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
421 return tdb_chainlock(ctdb_db->ltdb->tdb, key);
424 static int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
426 return tdb_chainunlock(ctdb_db->ltdb->tdb, key);
430 #define CTDB_DS_ALIGNMENT 8
431 static void *ctdbd_allocate_pkt(struct ctdb_context *ctdb, size_t len)
435 size = (len+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
436 return talloc_size(ctdb, size);
440 struct ctdbd_queue_packet {
441 struct ctdbd_queue_packet *next, *prev;
447 queue a packet for sending
449 int ctdbd_queue_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
451 uint8_t *data = (uint8_t *)hdr;
452 uint32_t length = hdr->length;
453 struct ctdbd_queue_packet *pkt;
456 /* enforce the length and alignment rules from the tcp packet allocator */
457 length2 = (length+(CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
458 *(uint32_t *)data = length2;
460 if (length2 != length) {
461 memset(data+length, 0, length2-length);
464 /* if the queue is empty then try an immediate write, avoiding
465 queue overhead. This relies on non-blocking sockets */
466 if (ctdb->daemon.queue == NULL) {
467 ssize_t n = write(ctdb->daemon.sd, data, length2);
468 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
469 printf("socket to ctdb daemon has died\n");
476 if (length2 == 0) return 0;
479 pkt = talloc(ctdb, struct ctdbd_queue_packet);
480 CTDB_NO_MEMORY(ctdb, pkt);
482 pkt->data = talloc_memdup(pkt, data, length2);
483 CTDB_NO_MEMORY(ctdb, pkt->data);
485 pkt->length = length2;
487 if (ctdb->daemon.queue == NULL) {
488 EVENT_FD_WRITEABLE(ctdb->daemon.fde);
491 DLIST_ADD_END(ctdb->daemon.queue, pkt, struct ctdbd_queue_packet *);
500 static int ctdbd_call_destructor(struct ctdb_call_state *state)
502 idr_remove(state->node->ctdb->idr, state->c->hdr.reqid);
507 make a recv call to the local ctdb daemon
509 This is called when the program wants to wait for a ctdb_call to complete and get the
510 results. This call will block unless the call has already completed.
512 int ctdbd_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
514 struct ctdb_record_handle *rec;
516 while (state->state < CTDB_CALL_DONE) {
517 event_loop_once(state->node->ctdb->ev);
519 if (state->state != CTDB_CALL_DONE) {
520 ctdb_set_error(state->node->ctdb, "%s", state->errmsg);
525 rec = state->fetch_private;
527 /* ugly hack to manage forced migration */
529 rec->data->dptr = talloc_steal(rec, state->call.reply_data.dptr);
530 rec->data->dsize = state->call.reply_data.dsize;
535 if (state->call.reply_data.dsize) {
536 call->reply_data.dptr = talloc_memdup(state->node->ctdb,
537 state->call.reply_data.dptr,
538 state->call.reply_data.dsize);
539 call->reply_data.dsize = state->call.reply_data.dsize;
541 call->reply_data.dptr = NULL;
542 call->reply_data.dsize = 0;
544 call->status = state->call.status;
551 make a ctdb call to the local daemon - async send
553 This constructs a ctdb_call request and queues it for processing.
554 This call never blocks.
556 struct ctdb_call_state *ctdbd_call_send(struct ctdb_db_context *ctdb_db, struct ctdb_call *call)
558 struct ctdb_call_state *state;
559 struct ctdb_context *ctdb = ctdb_db->ctdb;
560 struct ctdb_ltdb_header header;
565 /* if the domain socket is not yet open, open it */
566 if (ctdb->daemon.sd==-1) {
567 ux_socket_connect(ctdb);
570 ret = ctdb_ltdb_lock(ctdb_db, call->key);
572 printf("failed to lock ltdb record\n");
576 ret = ctdb_ltdb_fetch(ctdb_db, call->key, &header, ctdb_db, &data);
578 ctdb_ltdb_unlock(ctdb_db, call->key);
583 if (header.dmaster == ctdb->vnn && !(ctdb->flags & CTDB_FLAG_SELF_CONNECT)) {
584 state = ctdb_call_local_send(ctdb_db, call, &header, &data);
585 ctdb_ltdb_unlock(ctdb_db, call->key);
590 state = talloc_zero(ctdb_db, struct ctdb_call_state);
592 printf("failed to allocate state\n");
593 ctdb_ltdb_unlock(ctdb_db, call->key);
597 talloc_steal(state, data.dptr);
599 len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
600 state->c = ctdbd_allocate_pkt(ctdb, len);
601 if (state->c == NULL) {
602 printf("failed to allocate packet\n");
603 ctdb_ltdb_unlock(ctdb_db, call->key);
606 talloc_set_name_const(state->c, "ctdbd req_call packet");
607 talloc_steal(state, state->c);
609 state->c->hdr.length = len;
610 state->c->hdr.ctdb_magic = CTDB_MAGIC;
611 state->c->hdr.ctdb_version = CTDB_VERSION;
612 state->c->hdr.operation = CTDB_REQ_CALL;
613 state->c->hdr.destnode = header.dmaster;
614 state->c->hdr.srcnode = ctdb->vnn;
615 /* this limits us to 16k outstanding messages - not unreasonable */
616 state->c->hdr.reqid = idr_get_new(ctdb->idr, state, 0xFFFF);
617 state->c->flags = call->flags;
618 state->c->db_id = ctdb_db->db_id;
619 state->c->callid = call->call_id;
620 state->c->keylen = call->key.dsize;
621 state->c->calldatalen = call->call_data.dsize;
622 memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
623 memcpy(&state->c->data[call->key.dsize],
624 call->call_data.dptr, call->call_data.dsize);
626 state->call.call_data.dptr = &state->c->data[call->key.dsize];
627 state->call.key.dptr = &state->c->data[0];
629 state->node = ctdb->nodes[header.dmaster];
630 state->state = CTDB_CALL_WAIT;
631 state->header = header;
632 state->ctdb_db = ctdb_db;
634 talloc_set_destructor(state, ctdbd_call_destructor);
636 ctdbd_queue_pkt(ctdb, &state->c->hdr);
638 /*XXX set up timeout to cleanup if server doesnt respond
639 event_add_timed(ctdb->ev, state, timeval_current_ofs(CTDB_REQ_TIMEOUT, 0),
640 ctdb_call_timeout, state);
643 ctdb_ltdb_unlock(ctdb_db, call->key);