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(ctdb, 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(ctdb, req);
86 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
87 const char *name, bool persistent,
90 struct ctdb_request *req;
92 struct ctdb_db *ret = NULL;
94 req = synchronous(ctdb,
95 ctdb_attachdb_send(ctdb, name, persistent, tdb_flags,
99 ret = ctdb_attachdb_recv(ctdb, req);
100 ctdb_request_free(ctdb, req);
105 bool ctdb_getpnn(struct ctdb_connection *ctdb,
106 uint32_t destnode, uint32_t *pnn)
108 struct ctdb_request *req;
112 req = synchronous(ctdb,
113 ctdb_getpnn_send(ctdb, destnode, set, &done),
116 ret = ctdb_getpnn_recv(ctdb, req, pnn);
117 ctdb_request_free(ctdb, req);
122 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
123 ctdb_message_fn_t handler, void *cbdata)
125 struct ctdb_request *req;
129 req = synchronous(ctdb,
130 ctdb_set_message_handler_send(ctdb, srvid, handler,
134 ret = ctdb_set_message_handler_recv(ctdb, req);
135 ctdb_request_free(ctdb, req);
142 struct ctdb_lock *lock;
146 static void rrl_callback(struct ctdb_db *ctdb_db,
147 struct ctdb_lock *lock,
149 struct rrl_info *rrl)
156 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_connection *ctdb,
157 struct ctdb_db *ctdb_db, TDB_DATA key,
167 /* Immediate failure is easy. */
168 if (!ctdb_readrecordlock_async(ctdb_db, key, rrl_callback, &rrl))
171 /* Immediate success is easy. */
173 /* Otherwise wait until callback called. */
174 fds.fd = ctdb_get_fd(ctdb);
176 fds.events = ctdb_which_events(ctdb);
177 if (poll(&fds, 1, -1) < 0) {
178 /* Signalled is OK, other error is bad. */
182 "ctdb_readrecordlock: poll failed");
185 if (!ctdb_service(ctdb, fds.revents)) {