2 * Copyright (C) 2011 Red Hat, Jeff Layton <jlayton@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 * This file contains the code to manage the sqlite backend database for the
24 * clstated upcall daemon.
26 * The main database is called main.sqlite and contains the following tables:
28 * parameters: simple key/value pairs for storing database info
30 * clients: one column containing a BLOB with the as sent by the client
31 * and a timestamp (in epoch seconds) of when the record was
34 * FIXME: should we also record the fsid being accessed?
39 #endif /* HAVE_CONFIG_H */
47 #include <sys/types.h>
51 #include <linux/limits.h>
55 #define CLD_SQLITE_SCHEMA_VERSION 1
58 #define CLD_SQLITE_BUSY_TIMEOUT 10000
60 /* private data structures */
62 /* global variables */
64 /* top level DB directory */
65 static char *sqlite_topdir;
67 /* reusable pathname and sql command buffer */
68 static char buf[PATH_MAX];
70 /* global database handle */
73 /* forward declarations */
75 /* make a directory, ignoring EEXIST errors unless it's not a directory */
77 mkdir_if_not_exist(char *dirname)
82 ret = mkdir(dirname, S_IRWXU);
83 if (ret && errno != EEXIST)
86 ret = stat(dirname, &statbuf);
90 if (!S_ISDIR(statbuf.st_mode))
97 * Open the "main" database, and attempt to initialize it by creating the
98 * parameters table and inserting the schema version into it. Ignore any errors
99 * from that, and then attempt to select the version out of it again. If the
100 * version appears wrong, then assume that the DB is corrupt or has been
101 * upgraded, and return an error. If all of that works, then attempt to create
102 * the "clients" table.
105 sqlite_maindb_init(char *topdir)
109 sqlite3_stmt *stmt = NULL;
111 sqlite_topdir = topdir;
113 ret = mkdir_if_not_exist(sqlite_topdir);
117 ret = snprintf(buf, PATH_MAX - 1, "%s/main.sqlite", sqlite_topdir);
121 buf[PATH_MAX - 1] = '\0';
123 ret = sqlite3_open(buf, &dbh);
124 if (ret != SQLITE_OK) {
125 xlog(L_ERROR, "Unable to open main database: %d", ret);
129 ret = sqlite3_busy_timeout(dbh, CLD_SQLITE_BUSY_TIMEOUT);
130 if (ret != SQLITE_OK) {
131 xlog(L_ERROR, "Unable to set sqlite busy timeout: %d", ret);
135 /* Try to create table */
136 ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS parameters "
137 "(key TEXT PRIMARY KEY, value TEXT);",
139 if (ret != SQLITE_OK) {
140 xlog(L_ERROR, "Unable to create parameter table: %d", ret);
144 /* insert version into table -- ignore error if it fails */
145 ret = snprintf(buf, sizeof(buf),
146 "INSERT OR IGNORE INTO parameters values (\"version\", "
147 "\"%d\");", CLD_SQLITE_SCHEMA_VERSION);
150 } else if ((size_t)ret >= sizeof(buf)) {
155 ret = sqlite3_exec(dbh, (const char *)buf, NULL, NULL, &err);
156 if (ret != SQLITE_OK) {
157 xlog(L_ERROR, "Unable to insert into parameter table: %d",
162 ret = sqlite3_prepare_v2(dbh,
163 "SELECT value FROM parameters WHERE key == \"version\";",
165 if (ret != SQLITE_OK) {
166 xlog(L_ERROR, "Unable to prepare select statement: %d", ret);
170 /* check schema version */
171 ret = sqlite3_step(stmt);
172 if (ret != SQLITE_ROW) {
173 xlog(L_ERROR, "Select statement execution failed: %s",
174 sqlite3_errmsg(dbh));
178 /* process SELECT result */
179 ret = sqlite3_column_int(stmt, 0);
180 if (ret != CLD_SQLITE_SCHEMA_VERSION) {
181 xlog(L_ERROR, "Unsupported database schema version! "
182 "Expected %d, got %d.",
183 CLD_SQLITE_SCHEMA_VERSION, ret);
188 /* now create the "clients" table */
189 ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS clients "
190 "(id BLOB PRIMARY KEY, time INTEGER);",
192 if (ret != SQLITE_OK) {
193 xlog(L_ERROR, "Unable to create clients table: %s", err);
198 sqlite3_finalize(stmt);
203 xlog(L_ERROR, "sqlite error: %s", err);
206 sqlite3_finalize(stmt);
212 * Create a client record
214 * Returns a non-zero sqlite error code, or SQLITE_OK (aka 0)
217 sqlite_insert_client(const unsigned char *clname, const size_t namelen)
220 sqlite3_stmt *stmt = NULL;
222 ret = sqlite3_prepare_v2(dbh, "INSERT OR REPLACE INTO clients VALUES "
223 "(?, strftime('%s', 'now'));", -1,
225 if (ret != SQLITE_OK) {
226 xlog(L_ERROR, "%s: insert statement prepare failed: %s",
227 __func__, sqlite3_errmsg(dbh));
231 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
233 if (ret != SQLITE_OK) {
234 xlog(L_ERROR, "%s: bind blob failed: %s", __func__,
235 sqlite3_errmsg(dbh));
239 ret = sqlite3_step(stmt);
240 if (ret == SQLITE_DONE)
243 xlog(L_ERROR, "%s: unexpected return code from insert: %s",
244 __func__, sqlite3_errmsg(dbh));
247 xlog(D_GENERAL, "%s: returning %d", __func__, ret);
248 sqlite3_finalize(stmt);
252 /* Remove a client record */
254 sqlite_remove_client(const unsigned char *clname, const size_t namelen)
257 sqlite3_stmt *stmt = NULL;
259 ret = sqlite3_prepare_v2(dbh, "DELETE FROM clients WHERE id==?", -1,
261 if (ret != SQLITE_OK) {
262 xlog(L_ERROR, "%s: statement prepare failed: %s",
263 __func__, sqlite3_errmsg(dbh));
267 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
269 if (ret != SQLITE_OK) {
270 xlog(L_ERROR, "%s: bind blob failed: %s", __func__,
271 sqlite3_errmsg(dbh));
275 ret = sqlite3_step(stmt);
276 if (ret == SQLITE_DONE)
279 xlog(L_ERROR, "%s: unexpected return code from delete: %d",
283 xlog(D_GENERAL, "%s: returning %d", __func__, ret);
284 sqlite3_finalize(stmt);
289 * Is the given clname in the clients table? If so, then update its timestamp
290 * and return success. If the record isn't present, or the update fails, then
294 sqlite_check_client(const unsigned char *clname, const size_t namelen)
297 sqlite3_stmt *stmt = NULL;
299 ret = sqlite3_prepare_v2(dbh, "SELECT count(*) FROM clients WHERE "
300 "id==?", -1, &stmt, NULL);
301 if (ret != SQLITE_OK) {
302 xlog(L_ERROR, "%s: unable to prepare update statement: %s",
303 __func__, sqlite3_errmsg(dbh));
307 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
309 if (ret != SQLITE_OK) {
310 xlog(L_ERROR, "%s: bind blob failed: %s",
311 __func__, sqlite3_errmsg(dbh));
315 ret = sqlite3_step(stmt);
316 if (ret != SQLITE_ROW) {
317 xlog(L_ERROR, "%s: unexpected return code from select: %d",
322 ret = sqlite3_column_int(stmt, 0);
323 xlog(D_GENERAL, "%s: select returned %d rows", ret);
329 sqlite3_finalize(stmt);
331 ret = sqlite3_prepare_v2(dbh, "UPDATE OR FAIL clients SET "
332 "time=strftime('%s', 'now') WHERE id==?",
334 if (ret != SQLITE_OK) {
335 xlog(L_ERROR, "%s: unable to prepare update statement: %s",
336 __func__, sqlite3_errmsg(dbh));
340 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
342 if (ret != SQLITE_OK) {
343 xlog(L_ERROR, "%s: bind blob failed: %s",
344 __func__, sqlite3_errmsg(dbh));
348 ret = sqlite3_step(stmt);
349 if (ret == SQLITE_DONE)
352 xlog(L_ERROR, "%s: unexpected return code from update: %s",
353 __func__, sqlite3_errmsg(dbh));
356 xlog(D_GENERAL, "%s: returning %d", __func__, ret);
357 sqlite3_finalize(stmt);
362 * remove any client records that were not reclaimed since grace_start.
365 sqlite_remove_unreclaimed(time_t grace_start)
370 ret = snprintf(buf, sizeof(buf), "DELETE FROM clients WHERE time < %ld",
374 } else if ((size_t)ret >= sizeof(buf)) {
379 ret = sqlite3_exec(dbh, buf, NULL, NULL, &err);
380 if (ret != SQLITE_OK)
381 xlog(L_ERROR, "%s: delete failed: %s", __func__, err);
383 xlog(D_GENERAL, "%s: returning %d", __func__, ret);