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 /* reusable pathname and sql command buffer */
65 static char buf[PATH_MAX];
67 /* global database handle */
70 /* forward declarations */
72 /* make a directory, ignoring EEXIST errors unless it's not a directory */
74 mkdir_if_not_exist(const char *dirname)
79 ret = mkdir(dirname, S_IRWXU);
80 if (ret && errno != EEXIST)
83 ret = stat(dirname, &statbuf);
87 if (!S_ISDIR(statbuf.st_mode))
94 * Open the "main" database, and attempt to initialize it by creating the
95 * parameters table and inserting the schema version into it. Ignore any errors
96 * from that, and then attempt to select the version out of it again. If the
97 * version appears wrong, then assume that the DB is corrupt or has been
98 * upgraded, and return an error. If all of that works, then attempt to create
99 * the "clients" table.
102 sqlite_maindb_init(const char *topdir)
106 sqlite3_stmt *stmt = NULL;
108 ret = mkdir_if_not_exist(topdir);
112 ret = snprintf(buf, PATH_MAX - 1, "%s/main.sqlite", topdir);
116 buf[PATH_MAX - 1] = '\0';
118 ret = sqlite3_open(buf, &dbh);
119 if (ret != SQLITE_OK) {
120 xlog(L_ERROR, "Unable to open main database: %d", ret);
124 ret = sqlite3_busy_timeout(dbh, CLD_SQLITE_BUSY_TIMEOUT);
125 if (ret != SQLITE_OK) {
126 xlog(L_ERROR, "Unable to set sqlite busy timeout: %d", ret);
130 /* Try to create table */
131 ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS parameters "
132 "(key TEXT PRIMARY KEY, value TEXT);",
134 if (ret != SQLITE_OK) {
135 xlog(L_ERROR, "Unable to create parameter table: %d", ret);
139 /* insert version into table -- ignore error if it fails */
140 ret = snprintf(buf, sizeof(buf),
141 "INSERT OR IGNORE INTO parameters values (\"version\", "
142 "\"%d\");", CLD_SQLITE_SCHEMA_VERSION);
145 } else if ((size_t)ret >= sizeof(buf)) {
150 ret = sqlite3_exec(dbh, (const char *)buf, NULL, NULL, &err);
151 if (ret != SQLITE_OK) {
152 xlog(L_ERROR, "Unable to insert into parameter table: %d",
157 ret = sqlite3_prepare_v2(dbh,
158 "SELECT value FROM parameters WHERE key == \"version\";",
160 if (ret != SQLITE_OK) {
161 xlog(L_ERROR, "Unable to prepare select statement: %d", ret);
165 /* check schema version */
166 ret = sqlite3_step(stmt);
167 if (ret != SQLITE_ROW) {
168 xlog(L_ERROR, "Select statement execution failed: %s",
169 sqlite3_errmsg(dbh));
173 /* process SELECT result */
174 ret = sqlite3_column_int(stmt, 0);
175 if (ret != CLD_SQLITE_SCHEMA_VERSION) {
176 xlog(L_ERROR, "Unsupported database schema version! "
177 "Expected %d, got %d.",
178 CLD_SQLITE_SCHEMA_VERSION, ret);
183 /* now create the "clients" table */
184 ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS clients "
185 "(id BLOB PRIMARY KEY, time INTEGER);",
187 if (ret != SQLITE_OK) {
188 xlog(L_ERROR, "Unable to create clients table: %s", err);
193 sqlite3_finalize(stmt);
198 xlog(L_ERROR, "sqlite error: %s", err);
201 sqlite3_finalize(stmt);
207 * Create a client record
209 * Returns a non-zero sqlite error code, or SQLITE_OK (aka 0)
212 sqlite_insert_client(const unsigned char *clname, const size_t namelen)
215 sqlite3_stmt *stmt = NULL;
217 ret = sqlite3_prepare_v2(dbh, "INSERT OR REPLACE INTO clients VALUES "
218 "(?, strftime('%s', 'now'));", -1,
220 if (ret != SQLITE_OK) {
221 xlog(L_ERROR, "%s: insert statement prepare failed: %s",
222 __func__, sqlite3_errmsg(dbh));
226 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
228 if (ret != SQLITE_OK) {
229 xlog(L_ERROR, "%s: bind blob failed: %s", __func__,
230 sqlite3_errmsg(dbh));
234 ret = sqlite3_step(stmt);
235 if (ret == SQLITE_DONE)
238 xlog(L_ERROR, "%s: unexpected return code from insert: %s",
239 __func__, sqlite3_errmsg(dbh));
242 xlog(D_GENERAL, "%s: returning %d", __func__, ret);
243 sqlite3_finalize(stmt);
247 /* Remove a client record */
249 sqlite_remove_client(const unsigned char *clname, const size_t namelen)
252 sqlite3_stmt *stmt = NULL;
254 ret = sqlite3_prepare_v2(dbh, "DELETE FROM clients WHERE id==?", -1,
256 if (ret != SQLITE_OK) {
257 xlog(L_ERROR, "%s: statement prepare failed: %s",
258 __func__, sqlite3_errmsg(dbh));
262 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
264 if (ret != SQLITE_OK) {
265 xlog(L_ERROR, "%s: bind blob failed: %s", __func__,
266 sqlite3_errmsg(dbh));
270 ret = sqlite3_step(stmt);
271 if (ret == SQLITE_DONE)
274 xlog(L_ERROR, "%s: unexpected return code from delete: %d",
278 xlog(D_GENERAL, "%s: returning %d", __func__, ret);
279 sqlite3_finalize(stmt);
284 * Is the given clname in the clients table? If so, then update its timestamp
285 * and return success. If the record isn't present, or the update fails, then
289 sqlite_check_client(const unsigned char *clname, const size_t namelen)
292 sqlite3_stmt *stmt = NULL;
294 ret = sqlite3_prepare_v2(dbh, "SELECT count(*) FROM clients WHERE "
295 "id==?", -1, &stmt, NULL);
296 if (ret != SQLITE_OK) {
297 xlog(L_ERROR, "%s: unable to prepare update statement: %s",
298 __func__, sqlite3_errmsg(dbh));
302 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
304 if (ret != SQLITE_OK) {
305 xlog(L_ERROR, "%s: bind blob failed: %s",
306 __func__, sqlite3_errmsg(dbh));
310 ret = sqlite3_step(stmt);
311 if (ret != SQLITE_ROW) {
312 xlog(L_ERROR, "%s: unexpected return code from select: %d",
317 ret = sqlite3_column_int(stmt, 0);
318 xlog(D_GENERAL, "%s: select returned %d rows", __func__, ret);
324 sqlite3_finalize(stmt);
326 ret = sqlite3_prepare_v2(dbh, "UPDATE OR FAIL clients SET "
327 "time=strftime('%s', 'now') WHERE id==?",
329 if (ret != SQLITE_OK) {
330 xlog(L_ERROR, "%s: unable to prepare update statement: %s",
331 __func__, sqlite3_errmsg(dbh));
335 ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
337 if (ret != SQLITE_OK) {
338 xlog(L_ERROR, "%s: bind blob failed: %s",
339 __func__, sqlite3_errmsg(dbh));
343 ret = sqlite3_step(stmt);
344 if (ret == SQLITE_DONE)
347 xlog(L_ERROR, "%s: unexpected return code from update: %s",
348 __func__, sqlite3_errmsg(dbh));
351 xlog(D_GENERAL, "%s: returning %d", __func__, ret);
352 sqlite3_finalize(stmt);
357 * remove any client records that were not reclaimed since grace_start.
360 sqlite_remove_unreclaimed(time_t grace_start)
365 ret = snprintf(buf, sizeof(buf), "DELETE FROM clients WHERE time < %ld",
369 } else if ((size_t)ret >= sizeof(buf)) {
374 ret = sqlite3_exec(dbh, buf, NULL, NULL, &err);
375 if (ret != SQLITE_OK)
376 xlog(L_ERROR, "%s: delete failed: %s", __func__, err);
378 xlog(D_GENERAL, "%s: returning %d", __func__, ret);