summaryrefslogtreecommitdiffstats
path: root/utils/nfsdcld/sqlite.c
diff options
context:
space:
mode:
authorJeff Layton <jlayton@redhat.com>2012-04-26 11:47:58 -0400
committerSteve Dickson <steved@redhat.com>2012-04-26 13:25:04 -0400
commit1b0d2b29df2e089bbcabc37a4cd716a448d48a5e (patch)
tree1720bef648eeded19bda17a8cc750cc3c2c4d767 /utils/nfsdcld/sqlite.c
parent53f6dbee36fb72acb9a88413fc35c4e71200d3b8 (diff)
downloadnfs-utils-1b0d2b29df2e089bbcabc37a4cd716a448d48a5e.tar.gz
nfs-utils-1b0d2b29df2e089bbcabc37a4cd716a448d48a5e.tar.xz
nfs-utils-1b0d2b29df2e089bbcabc37a4cd716a448d48a5e.zip
nfsdcld: add check/update functionality
Add functions to check whether a client is allowed to reclaim, and update its timestamp in the DB if so. If either the query or update fails, then the host is not allowed to reclaim state. Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Steve Dickson <steved@redhat.com>
Diffstat (limited to 'utils/nfsdcld/sqlite.c')
-rw-r--r--utils/nfsdcld/sqlite.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/utils/nfsdcld/sqlite.c b/utils/nfsdcld/sqlite.c
index a198c34..01bba1a 100644
--- a/utils/nfsdcld/sqlite.c
+++ b/utils/nfsdcld/sqlite.c
@@ -287,3 +287,76 @@ out_err:
sqlite3_finalize(stmt);
return ret;
}
+
+/*
+ * Is the given clname in the clients table? If so, then update its timestamp
+ * and return success. If the record isn't present, or the update fails, then
+ * return an error.
+ */
+int
+sqlite_check_client(const unsigned char *clname, const size_t namelen)
+{
+ int ret;
+ sqlite3_stmt *stmt = NULL;
+
+ ret = sqlite3_prepare_v2(dbh, "SELECT count(*) FROM clients WHERE "
+ "id==?", -1, &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ xlog(L_ERROR, "%s: unable to prepare update statement: %s",
+ __func__, sqlite3_errmsg(dbh));
+ goto out_err;
+ }
+
+ ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
+ SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ xlog(L_ERROR, "%s: bind blob failed: %s",
+ __func__, sqlite3_errmsg(dbh));
+ goto out_err;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (ret != SQLITE_ROW) {
+ xlog(L_ERROR, "%s: unexpected return code from select: %d",
+ __func__, ret);
+ goto out_err;
+ }
+
+ ret = sqlite3_column_int(stmt, 0);
+ xlog(D_GENERAL, "%s: select returned %d rows", ret);
+ if (ret != 1) {
+ ret = -EACCES;
+ goto out_err;
+ }
+
+ sqlite3_finalize(stmt);
+ stmt = NULL;
+ ret = sqlite3_prepare_v2(dbh, "UPDATE OR FAIL clients SET "
+ "time=strftime('%s', 'now') WHERE id==?",
+ -1, &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ xlog(L_ERROR, "%s: unable to prepare update statement: %s",
+ __func__, sqlite3_errmsg(dbh));
+ goto out_err;
+ }
+
+ ret = sqlite3_bind_blob(stmt, 1, (const void *)clname, namelen,
+ SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ xlog(L_ERROR, "%s: bind blob failed: %s",
+ __func__, sqlite3_errmsg(dbh));
+ goto out_err;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_DONE)
+ ret = SQLITE_OK;
+ else
+ xlog(L_ERROR, "%s: unexpected return code from update: %s",
+ __func__, sqlite3_errmsg(dbh));
+
+out_err:
+ xlog(D_GENERAL, "%s: returning %d", __func__, ret);
+ sqlite3_finalize(stmt);
+ return ret;
+}