11 static unsigned int db_num;
12 static struct db *dbs;
15 struct db *next, *prev;
23 struct ctdb_db *find_db_by_id(unsigned int id)
27 for (db = dbs; db; db = db->next) {
34 static void attachdb_help(int agc, char **argv)
36 #include "generated-attachdb-help:attachdb"
38 <section id="c:attachdb">
39 <title><command>attachdb</command></title>
40 <para>Attach to a ctdb database</para>
42 <command>attachdb</command>
43 <arg choice="req"><replaceable>name</replaceable></arg>
44 <arg choice="req"><replaceable>persistent</replaceable></arg>
45 <arg choice="opt"><replaceable>tdb-flags</replaceable></arg>
47 <para>Attach to the database of the given <replaceable>name</replaceable>.
48 <replaceable>persistent</replaceable> is 'true' or 'false', an
50 <replaceable>tdb-flags</replaceable> an optional one or more
51 comma-separated values:</para>
56 <para>Use sequence numbers on the tdb</para>
61 <para>It uses a consecutive number for each attached db to
62 identify it for other ctdb-test commands, starting with 1.</para>
64 <para>Without any options, the <command>attachdb</command>
65 command lists all databases attached.</para>
70 static void detachdb_help(int agc, char **argv)
72 #include "generated-attachdb-help:detachdb"
74 <section id="c:detachdb">
75 <title><command>detachdb</command></title>
76 <para>Detach from a ctdb database</para>
78 <command>detachdb</command>
79 <arg choice="req"><replaceable>number</replaceable></arg>
81 <para>Detach from the database returned by <command>attachdb</command>.
86 static int db_destructor(struct db *db)
88 ctdb_detachdb(get_ctdb(), db->db);
89 DLIST_REMOVE(dbs, db);
93 static bool detachdb(int argc, char **argv)
98 log_line(LOG_ALWAYS, "Need database number");
102 for (db = dbs; db; db = db->next) {
103 if (db->num == atoi(argv[1]))
107 log_line(LOG_ALWAYS, "Unknown db number %s", argv[1]);
114 static bool attachdb(int argc, char **argv)
119 log_line(LOG_ALWAYS, "No ctdb connection");
124 log_line(LOG_UI, "Databases currently attached:");
125 for (db = dbs; db; db = db->next) {
126 log_line(LOG_ALWAYS, " %i: %s: %s %u",
129 ? "persistent" : "not persistent",
134 if (argc != 3 && argc != 4) {
135 log_line(LOG_ALWAYS, "Need 2 or 3 args");
138 db = talloc(working, struct db);
139 db->name = talloc_strdup(db, argv[1]);
140 if (strcasecmp(argv[2], "true") == 0)
141 db->persistent = true;
142 else if (strcasecmp(argv[2], "false") == 0)
143 db->persistent = false;
145 log_line(LOG_ALWAYS, "persistent should be true or false");
151 if (strcasecmp(argv[3], "seqnum") == 0)
152 db->tdb_flags |= TDB_SEQNUM;
154 log_line(LOG_ALWAYS, "invalid tdb-flags");
159 db->db = ctdb_attachdb(get_ctdb(), db->name, db->persistent,
162 log_line(LOG_UI, "ctdb_attachdb: %s", strerror(errno));
167 talloc_set_destructor(db, db_destructor);
168 log_line(LOG_UI, "attached: %u", db->num);
172 static void attachdb_init(void)
174 tui_register_command("attachdb", attachdb, attachdb_help);
175 tui_register_command("detachdb", detachdb, detachdb_help);
177 init_call(attachdb_init);