libctdb: use bool in API
[rusty/ctdb.git] / include / ctdb.h
1 /*
2    ctdb database library
3
4    Copyright (C) Ronnie sahlberg 2010
5    Copyright (C) Rusty Russell 2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _CTDB_H
22 #define _CTDB_H
23 #include <sys/types.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <tdb.h>
29
30 /* The type of the first arg should match the arg given to ctdb_connect() */
31 typedef void (*ctdb_log_fn_t)(void *log_priv,
32                               int severity, const char *format, va_list ap);
33
34
35 /* All *_send() functions are guaranteed to be non-blocking and fully
36  * asynchronous.  The non-_send variants are synchronous.
37  */
38
39 /*
40  * Connect to ctdb using the specified domain socket.
41  * Returns a ctdb context if successful or NULL.
42  *
43  */
44 struct ctdb_connection *ctdb_connect(const char *addr,
45                                      ctdb_log_fn_t log_fn, void *log_priv);
46
47 int ctdb_get_fd(struct ctdb_connection *ctdb);
48
49 int ctdb_which_events(struct ctdb_connection *ctdb);
50
51 bool ctdb_service(struct ctdb_connection *ctdb, int revents);
52
53 struct ctdb_request;
54
55 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req);
56
57 /*
58  * Callback for completed requests: it would normally unpack the request
59  * using ctdb_*_recv().
60  * You must free the request using ctdb_request_free().
61  *
62  * Note that due to macro magic, your callback doesn't have to take void *,
63  * it can take a type which matches the actual private parameter.
64  */
65 typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
66                                 struct ctdb_request *req, void *private);
67
68 /*
69  * Special node addresses :
70  */
71 /* used on the domain socket, send a pdu to the local daemon */
72 #define CTDB_CURRENT_NODE     0xF0000001
73 /* send a broadcast to all nodes in the cluster, active or not */
74 #define CTDB_BROADCAST_ALL    0xF0000002
75 /* send a broadcast to all nodes in the current vnn map */
76 #define CTDB_BROADCAST_VNNMAP 0xF0000003
77 /* send a broadcast to all connected nodes */
78 #define CTDB_BROADCAST_CONNECTED 0xF0000004
79
80
81 /*
82  * functions to attach to a database
83  * if the database does not exist it will be created.
84  *
85  * You have to free the handle with ctdb_detach_db() when finished with it.
86  */
87 struct ctdb_db;
88
89 struct ctdb_request *
90 ctdb_attachdb_send(struct ctdb_connection *ctdb,
91                    const char *name, int persistent, uint32_t tdb_flags,
92                    ctdb_callback_t callback, void *private_data);
93
94 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
95                                    struct ctdb_request *req);
96
97 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
98                               const char *name, int persistent,
99                               uint32_t tdb_flags);
100
101 struct ctdb_lock;
102
103 /*
104  * functions to read a record from the database
105  * when the callback is invoked, the client will hold an exclusive lock
106  * on the record, the client MUST NOT block during holding this lock and MUST
107  * release it quickly by performing ctdb_release_lock(lock).
108  *
109  * When the lock is released, data is freed too, so make sure to copy the data
110  * before that.
111  *
112  * This returns true on success: the callback may have already been called,
113  * or it might be awaiting a response from ctdbd.
114  */
115 typedef void (*ctdb_rrl_callback_t)(struct ctdb_db *ctdb_db,
116                                     struct ctdb_lock *lock,
117                                     TDB_DATA data,
118                                     void *private);
119 bool
120 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
121                           ctdb_rrl_callback_t callback, void *private_data);
122
123 /* Returns null on failure. */
124 struct ctdb_lock *ctdb_readrecordlock(struct ctdb_db *ctdb_db, TDB_DATA key,
125                                       TDB_DATA *data);
126
127 /*
128  * Function to write data to a record
129  * This function may ONLY be called while holding a lock to the record
130  * created by ctdb_readrecordlock*, and before calling
131  * ctdb_release_lock() to release the lock.
132  */
133 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data);
134
135
136 void ctdb_release_lock(struct ctdb_lock *lock);
137
138 /*
139  * messaging functions
140  * these functions provide a messaging layer for applications to communicate
141  * with eachother across
142  */
143 typedef void (*ctdb_message_fn_t)(struct ctdb_connection *, uint64_t srvid, TDB_DATA data, void *);
144
145 struct ctdb_request *
146 ctdb_set_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
147                               ctdb_message_fn_t handler,
148                               ctdb_callback_t callback,
149                               void *private_data);
150
151 bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
152                                    struct ctdb_request *handle);
153
154 bool ctdb_set_message_handler(struct ctdb_connection *ctdb, uint64_t srvid,
155                               ctdb_message_fn_t handler, void *private_data);
156
157
158
159 /*
160  * unregister a message handler and stop listening on teh specified port
161  */
162 struct ctdb_request *
163 ctdb_remove_message_handler_send(struct ctdb_connection *ctdb, uint64_t srvid,
164                                  ctdb_callback_t callback,
165                                  void *private_data);
166
167 bool ctdb_remove_message_handler_recv(struct ctdb_request *handle);
168
169 bool ctdb_remove_message_handler(struct ctdb_connection *ctdb, uint64_t srvid);
170
171
172
173 /*
174  * send a message to a specific node/port
175  * this function is non-blocking
176  */
177 bool ctdb_send_message(struct ctdb_connection *ctdb, uint32_t pnn, uint64_t srvid, TDB_DATA data);
178
179
180
181 /*
182  * functions to read the pnn number of the local node
183  */
184 struct ctdb_request *
185 ctdb_getpnn_send(struct ctdb_connection *ctdb,
186                  uint32_t destnode,
187                  ctdb_callback_t callback,
188                  void *private_data);
189 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
190                       struct ctdb_request *req, uint32_t *pnn);
191
192 bool ctdb_getpnn(struct ctdb_connection *ctdb,
193                  uint32_t destnode,
194                  uint32_t *pnn);
195
196
197
198
199 /*
200  * functions to read the recovery master of a node
201  */
202 struct ctdb_request *
203 ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
204                         uint32_t destnode,
205                         ctdb_callback_t callback,
206                         void *private_data);
207 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
208                             struct ctdb_request *handle,
209                             uint32_t *recmaster);
210 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
211                        uint32_t destnode,
212                        uint32_t *recmaster);
213
214
215
216
217 /*
218  * cancel a request
219  */
220 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
221
222
223 /*
224  * functions for logging errors
225  */
226 extern int ctdb_log_level; /* LOG_WARNING and above by default. */
227 void ctdb_log_file(FILE *, int severity, const char *format, va_list ap);
228
229
230 /* These ugly macro wrappers make the callbacks typesafe. */
231 #include <ctdb_typesafe_cb.h>
232 #define ctdb_sendcb(cb, cbdata)                                         \
233          typesafe_cb_preargs(void, (cb), (cbdata),                      \
234                              struct ctdb_connection *, struct ctdb_request *)
235
236 #define ctdb_connect(addr, log, logpriv)                                \
237         ctdb_connect((addr),                                            \
238                      typesafe_cb_postargs(void, (log), (logpriv),       \
239                                           int, const char *, va_list),  \
240                      (logpriv))
241
242
243 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
244         ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags),   \
245                            ctdb_sendcb((cb), (cbdata)), (cbdata))
246
247 #define ctdb_readrecordlock_async(_ctdb_db, key, cb, cbdata)            \
248         ctdb_readrecordlock_async((_ctdb_db), (key),                    \
249                 typesafe_cb_preargs(void, (cb), (cbdata),               \
250                                     struct ctdb_db *, struct ctdb_lock *, \
251                                     TDB_DATA), (cbdata))
252
253 #define ctdb_set_message_handler_send(ctdb, srvid, handler, cb, cbdata) \
254         ctdb_set_message_handler_send((ctdb), (srvid), (handler),       \
255               ctdb_sendcb((cb), (cbdata)), (cbdata))
256
257 #define ctdb_remove_message_handler_send(ctdb, srvid, cb, cbdata)       \
258         ctdb_remove_message_handler_send((ctdb), (srvid),               \
259               ctdb_sendcb((cb), (cbdata)), (cbdata))
260
261 #define ctdb_getpnn_send(ctdb, destnode, cb, cbdata)                    \
262         ctdb_getpnn_send((ctdb), (destnode),                            \
263                          ctdb_sendcb((cb), (cbdata)), (cbdata))
264
265 #define ctdb_getrecmaster_send(ctdb, destnode, cb, cbdata)              \
266         ctdb_getrecmaster_send((ctdb), (destnode),                      \
267                                ctdb_sendcb((cb), (cbdata)), (cbdata))
268 #endif