2 synchronous wrappers for libctdb
4 Copyright (C) Rusty Russell 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "libctdb_private.h"
26 /* Remove type-safety macros. */
27 #undef ctdb_set_message_handler
29 /* On failure, frees req and returns NULL. */
30 static struct ctdb_request *synchronous(struct ctdb_connection *ctdb,
31 struct ctdb_request *req,
36 /* Pass through allocation failures. */
40 fds.fd = ctdb_get_fd(ctdb);
42 fds.events = ctdb_which_events(ctdb);
43 if (poll(&fds, 1, -1) < 0) {
44 /* Signalled is OK, other error is bad. */
47 ctdb_cancel(ctdb, req);
48 DEBUG(ctdb, LOG_ERR, "ctdb_synchronous: poll failed");
51 if (!ctdb_service(ctdb, fds.revents)) {
52 /* It can have failed after it completed request. */
54 ctdb_cancel(ctdb, req);
56 ctdb_request_free(req);
63 static void set(struct ctdb_connection *ctdb,
64 struct ctdb_request *req, bool *done)
69 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
70 uint32_t destnode, uint32_t *recmaster)
72 struct ctdb_request *req;
76 req = synchronous(ctdb,
77 ctdb_getrecmaster_send(ctdb, destnode, set, &done),
80 ret = ctdb_getrecmaster_recv(ctdb, req, recmaster);
81 ctdb_request_free(req);
86 bool ctdb_getrecmode(struct ctdb_connection *ctdb,
87 uint32_t destnode, uint32_t *recmode)
89 struct ctdb_request *req;
93 req = synchronous(ctdb,
94 ctdb_getrecmode_send(ctdb, destnode, set, &done),
97 ret = ctdb_getrecmode_recv(ctdb, req, recmode);
98 ctdb_request_free(req);
103 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
104 const char *name, bool persistent,
107 struct ctdb_request *req;
109 struct ctdb_db *ret = NULL;
111 req = synchronous(ctdb,
112 ctdb_attachdb_send(ctdb, name, persistent, tdb_flags,
116 ret = ctdb_attachdb_recv(ctdb, req);
117 ctdb_request_free(req);
122 bool ctdb_getpnn(struct ctdb_connection *ctdb,
123 uint32_t destnode, uint32_t *pnn)
125 struct ctdb_request *req;
129 req = synchronous(ctdb,
130 ctdb_getpnn_send(ctdb, destnode, set, &done),
133 ret = ctdb_getpnn_recv(ctdb, req, pnn);
134 ctdb_request_free(req);
139 bool ctdb_getnodemap(struct ctdb_connection *ctdb,
140 uint32_t destnode, struct ctdb_node_map **nodemap)
142 struct ctdb_request *req;
148 req = synchronous(ctdb,
149 ctdb_getnodemap_send(ctdb, destnode, set, &done),
152 ret = ctdb_getnodemap_recv(ctdb, req, nodemap);
153 ctdb_request_free(req);
158 bool ctdb_getpublicips(struct ctdb_connection *ctdb,
159 uint32_t destnode, struct ctdb_all_public_ips **ips)
161 struct ctdb_request *req;
167 req = synchronous(ctdb,
168 ctdb_getpublicips_send(ctdb, destnode, set, &done),
171 ret = ctdb_getpublicips_recv(ctdb, req, ips);
172 ctdb_request_free(req);
177 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
178 ctdb_message_fn_t handler, void *cbdata)
180 struct ctdb_request *req;
184 req = synchronous(ctdb,
185 ctdb_set_message_handler_send(ctdb, srvid, handler,
189 ret = ctdb_set_message_handler_recv(ctdb, req);
190 ctdb_request_free(req);
197 struct ctdb_lock *lock;
201 static void rrl_callback(struct ctdb_db *ctdb_db,
202 struct ctdb_lock *lock,
204 struct rrl_info *rrl)
211 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_connection *ctdb,
212 struct ctdb_db *ctdb_db, TDB_DATA key,
222 /* Immediate failure is easy. */
223 if (!ctdb_readrecordlock_async(ctdb_db, key, rrl_callback, &rrl))
226 /* Immediate success is easy. */
228 /* Otherwise wait until callback called. */
229 fds.fd = ctdb_get_fd(ctdb);
231 fds.events = ctdb_which_events(ctdb);
232 if (poll(&fds, 1, -1) < 0) {
233 /* Signalled is OK, other error is bad. */
237 "ctdb_readrecordlock: poll failed");
240 if (!ctdb_service(ctdb, fds.revents)) {
248 bool ctdb_getdbseqnum(struct ctdb_connection *ctdb,
249 uint32_t destnode, uint32_t dbid,
252 struct ctdb_request *req;
256 req = synchronous(ctdb,
257 ctdb_getdbseqnum_send(ctdb, destnode, dbid, set, &done),
260 ret = ctdb_getdbseqnum_recv(ctdb, req, seqnum);
261 ctdb_request_free(req);