summaryrefslogtreecommitdiffstats
path: root/ctdb
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2010-06-18 15:35:52 +0930
committerRusty Russell <rusty@rustcorp.com.au>2010-06-18 15:35:52 +0930
commitb93e65eaf74ff637bc960356a418ab55f4ca9330 (patch)
tree2d08722c2b2e7e55b84d5a4405b3d881c80e11e0 /ctdb
parent3d126e8c1453f29f21ac918b5167753372282aa2 (diff)
downloadsamba-b93e65eaf74ff637bc960356a418ab55f4ca9330.tar.gz
samba-b93e65eaf74ff637bc960356a418ab55f4ca9330.tar.xz
samba-b93e65eaf74ff637bc960356a418ab55f4ca9330.zip
libctdb: implement ctdb_disconnect and ctdb_detachdb
These are important for testing, since we can easily tell if we leak memory if there are outstanding allocations after calling these. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (This used to be ctdb commit 18a212aa40d0ff9ff59775c6fcf9dc973e991460)
Diffstat (limited to 'ctdb')
-rw-r--r--ctdb/include/ctdb.h21
-rw-r--r--ctdb/libctdb/ctdb.c67
-rw-r--r--ctdb/libctdb/messages.c12
-rw-r--r--ctdb/libctdb/messages.h1
4 files changed, 89 insertions, 12 deletions
diff --git a/ctdb/include/ctdb.h b/ctdb/include/ctdb.h
index e4aff86723..a9ee63ff25 100644
--- a/ctdb/include/ctdb.h
+++ b/ctdb/include/ctdb.h
@@ -72,7 +72,7 @@ typedef void (*ctdb_log_fn_t)(void *log_priv,
* @log: the logging function
* @log_priv: the private argument to the logging function.
*
- * Returns a ctdb context if successful or NULL. Use ctdb_free() to
+ * Returns a ctdb context if successful or NULL. Use ctdb_disconnect() to
* release the returned ctdb_connection when finished.
*
* See Also:
@@ -100,6 +100,14 @@ void ctdb_log_file(FILE *, int, const char *, va_list);
*/
extern int ctdb_log_level;
+/**
+ * ctdb_disconnect - close down a connection to ctdbd.
+ * @ctdb: the ctdb connectio returned from ctdb_connect.
+ *
+ * The @ctdb arg will be freed by this call, and must not be used again.
+ */
+void ctdb_disconnect(struct ctdb_connection *ctdb);
+
/***
*
* Asynchronous API
@@ -185,7 +193,7 @@ typedef void (*ctdb_callback_t)(struct ctdb_connection *ctdb,
* This represents a particular open database: you receive it from
* ctdb_attachdb or ctdb_attachdb_recv to manipulate a database.
*
- * You have to free the handle with ctdb_detach_db() when finished with it.
+ * You have to free the handle with ctdb_detachdb() when finished with it.
*/
struct ctdb_db;
@@ -457,6 +465,15 @@ struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
uint32_t tdb_flags);
/**
+ * ctdb_detachdb - close a clustered TDB.
+ * @ctdb: the ctdb_connection from ctdb_connect.
+ * @db: the database from ctdb_attachdb/ctdb_attachdb_send
+ *
+ * Closes a clustered tdb.
+ */
+void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db);
+
+/**
* ctdb_readrecordlock - read and lock a record (synchronous)
* @ctdb_db: the database handle from ctdb_attachdb/ctdb_attachdb_recv.
* @key: the key of the record to lock.
diff --git a/ctdb/libctdb/ctdb.c b/ctdb/libctdb/ctdb.c
index 1c16d36a7c..096a9264f0 100644
--- a/ctdb/libctdb/ctdb.c
+++ b/ctdb/libctdb/ctdb.c
@@ -50,6 +50,17 @@ struct ctdb_lock {
ctdb_rrl_callback_t callback;
};
+struct ctdb_db {
+ struct ctdb_connection *ctdb;
+ bool persistent;
+ uint32_t tdb_flags;
+ uint32_t id;
+ struct tdb_context *tdb;
+
+ ctdb_callback_t callback;
+ void *private_data;
+};
+
static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
{
DLIST_REMOVE(ctdb->locks, lock);
@@ -67,6 +78,19 @@ static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
DLIST_ADD(ctdb->locks, lock);
}
+static void cleanup_locks(struct ctdb_connection *ctdb, struct ctdb_db *db)
+{
+ struct ctdb_lock *i, *next;
+
+ for (i = ctdb->locks; i; i = next) {
+ /* Grab next pointer, as release_lock will free i */
+ next = i->next;
+ if (i->ctdb_db == db) {
+ ctdb_release_lock(db, i);
+ }
+ }
+}
+
/* FIXME: Could be in shared util code with rest of ctdb */
static void close_noerr(int fd)
{
@@ -165,6 +189,33 @@ fail:
return NULL;
}
+void ctdb_disconnect(struct ctdb_connection *ctdb)
+{
+ struct ctdb_request *i;
+
+ DEBUG(ctdb, LOG_DEBUG, "ctdb_disconnect");
+
+ while ((i = ctdb->outq) != NULL) {
+ DLIST_REMOVE(ctdb->outq, i);
+ ctdb_request_free(ctdb, i);
+ }
+
+ while ((i = ctdb->doneq) != NULL) {
+ DLIST_REMOVE(ctdb->doneq, i);
+ ctdb_request_free(ctdb, i);
+ }
+
+ if (ctdb->in)
+ free_io_elem(ctdb->in);
+
+ remove_message_handlers(ctdb);
+
+ close(ctdb->fd);
+ /* Just in case they try to reuse */
+ ctdb->fd = -1;
+ free(ctdb);
+}
+
int ctdb_get_fd(struct ctdb_connection *ctdb)
{
return ctdb->fd;
@@ -478,16 +529,12 @@ void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
req->callback = ctdb_cancel_callback;
}
-struct ctdb_db {
- struct ctdb_connection *ctdb;
- bool persistent;
- uint32_t tdb_flags;
- uint32_t id;
- struct tdb_context *tdb;
-
- ctdb_callback_t callback;
- void *private_data;
-};
+void ctdb_detachdb(struct ctdb_connection *ctdb, struct ctdb_db *db)
+{
+ cleanup_locks(ctdb, db);
+ tdb_close(db->tdb);
+ free(db);
+}
static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
struct ctdb_request *req,
diff --git a/ctdb/libctdb/messages.c b/ctdb/libctdb/messages.c
index bab0e8c4cb..7ad48d5dc8 100644
--- a/ctdb/libctdb/messages.c
+++ b/ctdb/libctdb/messages.c
@@ -43,6 +43,18 @@ void deliver_message(struct ctdb_connection *ctdb, struct ctdb_req_header *hdr)
}
}
+void remove_message_handlers(struct ctdb_connection *ctdb)
+{
+ struct message_handler_info *i;
+
+ /* ctdbd should unregister automatically when we close fd, so we don't
+ need to do that here. */
+ while ((i = ctdb->message_handlers) != NULL) {
+ DLIST_REMOVE(ctdb->message_handlers, i);
+ free(i);
+ }
+}
+
bool ctdb_set_message_handler_recv(struct ctdb_connection *ctdb,
struct ctdb_request *req)
{
diff --git a/ctdb/libctdb/messages.h b/ctdb/libctdb/messages.h
index dcf19c8b6c..89a04608ec 100644
--- a/ctdb/libctdb/messages.h
+++ b/ctdb/libctdb/messages.h
@@ -5,4 +5,5 @@ struct ctdb_connection;
struct ctdb_req_header;
void deliver_message(struct ctdb_connection *ctdb, struct ctdb_req_header *hdr);
+void remove_message_handlers(struct ctdb_connection *ctdb);
#endif /* _LIBCTDB_MESSAGE_H */