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
22 #include "lib/events/events.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "ctdb_private.h"
28 initialise the ctdb daemon.
30 if the ctdb dispatcher daemon has already been started then this
31 does nothing. Otherwise it forks the ctdb dispatcher daemon and
32 starts the daemons connecting to each other
34 NOTE: In current code the daemon does not fork. This is for testing purposes only
35 and to simplify the code.
38 struct ctdb_context *ctdb_init(struct event_context *ev)
40 struct ctdb_context *ctdb;
42 ctdb = talloc_zero(ev, struct ctdb_context);
48 const char *ctdb_errstr(struct ctdb_context *ctdb)
55 remember an error message
57 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
60 talloc_free(ctdb->err_msg);
62 ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
67 choose the transport we will use
69 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
71 int ctdb_tcp_init(struct ctdb_context *ctdb);
73 if (strcmp(transport, "tcp") == 0) {
74 return ctdb_tcp_init(ctdb);
76 ctdb_set_error(ctdb, "Unknown transport '%s'\n", transport);
84 static int ctdb_parse_address(struct ctdb_context *ctdb,
85 TALLOC_CTX *mem_ctx, const char *str,
86 struct ctdb_address *address)
91 ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
95 address->address = talloc_strndup(mem_ctx, str, p-str);
96 address->port = strtoul(p+1, NULL, 0);
102 add a node to the list of active nodes
104 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
106 struct ctdb_node *node;
108 node = talloc(ctdb, struct ctdb_node);
109 if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
114 if (ctdb->methods->add_node(node) != 0) {
119 DLIST_ADD(ctdb->nodes, node);
124 setup the node list from a file
126 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
132 lines = file_lines_load(nlist, &nlines, ctdb);
134 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
138 for (i=0;i<nlines;i++) {
139 if (ctdb_add_node(ctdb, lines[i]) != 0) {
150 setup the local node address
152 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
154 return ctdb_parse_address(ctdb, ctdb, address, &ctdb->address);
158 add a node to the list of active nodes
160 int ctdb_set_call(struct ctdb_context *ctdb, ctdb_fn_t fn, int id)
162 struct ctdb_registered_call *call;
164 call = talloc(ctdb, struct ctdb_registered_call);
168 DLIST_ADD(ctdb->calls, call);
173 attach to a specific database
175 int ctdb_attach(struct ctdb_context *ctdb, const char *name, int tdb_flags,
176 int open_flags, mode_t mode)
178 /* when we have a separate daemon this will need to be a real
179 file, not a TDB_INTERNAL, so the parent can access it to
181 ctdb->ltdb = tdb_open(name, 0, TDB_INTERNAL, 0, 0);
182 if (ctdb->ltdb == NULL) {
183 ctdb_set_error(ctdb, "Failed to open tdb %s\n", name);
190 start the protocol going
192 int ctdb_start(struct ctdb_context *ctdb)
194 return ctdb->methods->start(ctdb);
198 make a remote ctdb call
200 int ctdb_call(struct ctdb_context *ctdb, TDB_DATA key, int call_id,
201 TDB_DATA *call_data, TDB_DATA *reply_data)
203 printf("ctdb_call not implemented\n");
208 check if two addresses are the same
210 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
212 return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;