3 Utility functions to connect to the ctdb daemon
5 Copyright (C) Andrew Tridgell 2006
6 Copyright (C) Ronnie sahlberg 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "lib/events/events.h"
25 #include "include/ctdb.h"
26 #include "include/ctdb_protocol.h"
27 #include "include/ctdb_private.h"
29 struct ctdb_context *ctdb_connect(const char *addr)
31 struct event_context *ev;
32 struct ctdb_context *ctdb;
35 ev = event_context_init(NULL);
40 fprintf(stderr, "Failed to init ctdb\n");
44 ret = ctdb_set_socketname(ctdb, addr);
46 fprintf(stderr, __location__ " ctdb_set_socketname failed - %s\n", ctdb_errstr(ctdb));
51 ret = ctdb_socket_connect(ctdb);
53 fprintf(stderr, __location__ " Failed to connect to daemon\n");
62 int ctdb_get_fd(struct ctdb_context *ctdb)
64 return ctdb->daemon.sd;
67 int ctdb_which_events(struct ctdb_context *ctdb)
69 if (ctdb_queue_length(ctdb->daemon.queue) > 0) {
70 return POLLIN|POLLOUT;
79 initialise the ctdb daemon for client applications
81 struct ctdb_context *ctdb_init(struct event_context *ev)
84 struct ctdb_context *ctdb;
86 ctdb = talloc_zero(ev, struct ctdb_context);
88 DEBUG(DEBUG_ERR,(__location__ " talloc_zero failed.\n"));
92 ctdb->idr = idr_init(ctdb);
93 CTDB_NO_MEMORY_NULL(ctdb, ctdb->idr);
95 ret = ctdb_set_socketname(ctdb, CTDB_PATH);
97 DEBUG(DEBUG_ERR,(__location__ " ctdb_set_socketname failed.\n"));
110 * This is a bit hairy. due to the way ctdbd uses events.
111 * ctdbd quite frequently uses
112 * event_add_timed(... timeval_zero() ...)
114 * for example once it has finished reading off a full pdu off the
115 * domain socket, before calling the actual recdeive function.
117 * we probably need a new event function to handle these timed events
118 * event_loop_all_queued() or similar
120 int ctdb_service(struct ctdb_context *ctdb)
124 ret = event_loop_once(ctdb->ev);
125 ret = event_loop_once(ctdb->ev);
132 int ctdb_cancel(ctdb_handle *handle)
138 struct ctdb_control_cb_data {
144 ctdb_getrecmaster_recv_cb(struct ctdb_client_control_state *state)
146 struct ctdb_control_cb_data *cb_data = state->async.private_data;
147 get_recmaster_cb callback = (get_recmaster_cb)cb_data->callback;
149 callback(0, state->status, cb_data->private_data);
154 get the recovery master of a remote node
157 ctdb_getrecmaster_send(struct ctdb_context *ctdb,
159 get_recmaster_cb callback,
162 struct ctdb_client_control_state *state;
163 struct ctdb_control_cb_data *cb_data;
165 state = ctdb_control_send(ctdb, destnode, 0,
166 CTDB_CONTROL_GET_RECMASTER, 0, tdb_null,
169 if (callback != NULL) {
170 cb_data = talloc(state, struct ctdb_control_cb_data);
171 cb_data->callback = callback;
172 cb_data->private_data = private_data;
174 state->async.fn = ctdb_getrecmaster_recv_cb;
175 state->async.private_data = cb_data;
178 return (ctdb_handle *)state;
181 int ctdb_getrecmaster_recv(struct ctdb_context *ctdb, ctdb_handle *handle, uint32_t *recmaster)
183 struct ctdb_client_control_state *state = talloc_get_type(handle, struct ctdb_client_control_state);
187 ret = ctdb_control_recv(ctdb, state, state, NULL, &res, NULL);
189 DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_recv failed\n"));
194 *recmaster = (uint32_t)res;
200 int ctdb_getrecmaster(struct ctdb_context *ctdb, uint32_t destnode, uint32_t *recmaster)
202 struct ctdb_client_control_state *state;
204 state = ctdb_getrecmaster_send(ctdb, destnode, NULL, recmaster);
206 DEBUG(DEBUG_ERR,(__location__ " ctdb_getrecmaster_send() failed.\n"));
210 return ctdb_getrecmaster_recv(ctdb, state, recmaster);