Merge commit 'rusty/vacuum-fix-master'
[metze/ctdb/wip.git] / libctdb / sync.c
1 /*
2    synchronous wrappers for libctdb
3
4    Copyright (C) Rusty Russell 2010
5
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.
10
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.
15
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/>.
18 */
19 #include <ctdb.h>
20 #include <stdbool.h>
21 #include <poll.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include "libctdb_private.h"
25
26 /* Remove type-safety macros. */
27 #undef ctdb_set_message_handler
28
29 /* On failure, frees req and returns NULL. */
30 static struct ctdb_request *synchronous(struct ctdb_connection *ctdb,
31                                         struct ctdb_request *req,
32                                         bool *done)
33 {
34         struct pollfd fds;
35
36         /* Pass through allocation failures. */
37         if (!req)
38                 return NULL;
39
40         fds.fd = ctdb_get_fd(ctdb);
41         while (!*done) {
42                 fds.events = ctdb_which_events(ctdb);
43                 if (poll(&fds, 1, -1) < 0) {
44                         /* Signalled is OK, other error is bad. */
45                         if (errno == EINTR)
46                                 continue;
47                         ctdb_cancel(ctdb, req);
48                         DEBUG(ctdb, LOG_ERR, "ctdb_synchronous: poll failed");
49                         return NULL;
50                 }
51                 if (!ctdb_service(ctdb, fds.revents)) {
52                         /* It can have failed after it completed request. */
53                         if (!*done)
54                                 ctdb_cancel(ctdb, req);
55                         else
56                                 ctdb_request_free(ctdb, req);
57                         return NULL;
58                 }
59         }
60         return req;
61 }
62
63 static void set(struct ctdb_connection *ctdb,
64                 struct ctdb_request *req, bool *done)
65 {
66         *done = true;
67 }
68
69 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
70                        uint32_t destnode, uint32_t *recmaster)
71 {
72         struct ctdb_request *req;
73         bool done = false;
74         bool ret = false;
75
76         req = synchronous(ctdb,
77                           ctdb_getrecmaster_send(ctdb, destnode, set, &done),
78                           &done);
79         if (req != NULL) {
80                 ret = ctdb_getrecmaster_recv(ctdb, req, recmaster);
81                 ctdb_request_free(ctdb, req);
82         }
83         return ret;
84 }
85
86 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
87                               const char *name, bool persistent,
88                               uint32_t tdb_flags)
89 {
90         struct ctdb_request *req;
91         bool done = false;
92         struct ctdb_db *ret = NULL;
93
94         req = synchronous(ctdb,
95                           ctdb_attachdb_send(ctdb, name, persistent, tdb_flags,
96                                              set, &done),
97                           &done);
98         if (req != NULL) {
99                 ret = ctdb_attachdb_recv(ctdb, req);
100                 ctdb_request_free(ctdb, req);
101         }
102         return ret;
103 }
104
105 bool ctdb_getpnn(struct ctdb_connection *ctdb,
106                  uint32_t destnode, uint32_t *pnn)
107 {
108         struct ctdb_request *req;
109         bool done = false;
110         bool ret = false;
111
112         req = synchronous(ctdb,
113                           ctdb_getpnn_send(ctdb, destnode, set, &done),
114                           &done);
115         if (req != NULL) {
116                 ret = ctdb_getpnn_recv(ctdb, req, pnn);
117                 ctdb_request_free(ctdb, req);
118         }
119         return ret;
120 }
121
122 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
123                               ctdb_message_fn_t handler, void *cbdata)
124 {
125         struct ctdb_request *req;
126         bool done = false;
127         bool ret = false;
128
129         req = synchronous(ctdb,
130                           ctdb_set_message_handler_send(ctdb, srvid, handler,
131                                                         cbdata, set, &done),
132                           &done);
133         if (req != NULL) {
134                 ret = ctdb_set_message_handler_recv(ctdb, req);
135                 ctdb_request_free(ctdb, req);
136         }
137         return ret;
138 }
139
140 struct rrl_info {
141         bool done;
142         struct ctdb_lock *lock;
143         TDB_DATA *data;
144 };
145
146 static void rrl_callback(struct ctdb_db *ctdb_db,
147                          struct ctdb_lock *lock,
148                          TDB_DATA data,
149                          struct rrl_info *rrl)
150 {
151         rrl->done = true;
152         rrl->lock = lock;
153         *rrl->data = data;
154 }
155
156 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_connection *ctdb,
157                                       struct ctdb_db *ctdb_db, TDB_DATA key,
158                                       TDB_DATA *data)
159 {
160         struct pollfd fds;
161         struct rrl_info rrl;
162
163         rrl.done = false;
164         rrl.lock = NULL;
165         rrl.data = data;
166
167         /* Immediate failure is easy. */
168         if (!ctdb_readrecordlock_async(ctdb_db, key, rrl_callback, &rrl))
169                 return NULL;
170
171         /* Immediate success is easy. */
172         if (!rrl.done) {
173                 /* Otherwise wait until callback called. */
174                 fds.fd = ctdb_get_fd(ctdb);
175                 while (!rrl.done) {
176                         fds.events = ctdb_which_events(ctdb);
177                         if (poll(&fds, 1, -1) < 0) {
178                                 /* Signalled is OK, other error is bad. */
179                                 if (errno == EINTR)
180                                         continue;
181                                 DEBUG(ctdb, LOG_ERR,
182                                       "ctdb_readrecordlock: poll failed");
183                                 return NULL;
184                         }
185                         if (!ctdb_service(ctdb, fds.revents)) {
186                                 break;
187                         }
188                 }
189         }
190         return rrl.lock;
191 }