]> git.decadent.org.uk Git - nfs-utils.git/commitdiff
nfsdcld: add check/update functionality
authorJeff Layton <jlayton@redhat.com>
Thu, 26 Apr 2012 15:47:58 +0000 (11:47 -0400)
committerSteve Dickson <steved@redhat.com>
Thu, 26 Apr 2012 17:25:04 +0000 (13:25 -0400)
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>
utils/nfsdcld/nfsdcld.c
utils/nfsdcld/sqlite.c
utils/nfsdcld/sqlite.h

index a6c5239976d43e49e02baa4e23e58558591810af..2dfd7ecc7d9c08765fa92a9afc4aaf34297fa224 100644 (file)
@@ -199,6 +199,38 @@ cld_remove(struct cld_client *clnt)
        }
 }
 
+static void
+cld_check(struct cld_client *clnt)
+{
+       int ret;
+       ssize_t bsize, wsize;
+       struct cld_msg *cmsg = &clnt->cl_msg;
+
+       xlog(D_GENERAL, "%s: check client record", __func__);
+
+       ret = sqlite_check_client(cmsg->cm_u.cm_name.cn_id,
+                                 cmsg->cm_u.cm_name.cn_len);
+
+       /* set up reply */
+       cmsg->cm_status = ret ? -EACCES : ret;
+
+       bsize = sizeof(*cmsg);
+
+       xlog(D_GENERAL, "%s: downcall with status %d", __func__,
+                       cmsg->cm_status);
+       wsize = atomicio((void *)write, clnt->cl_fd, cmsg, bsize);
+       if (wsize != bsize) {
+               xlog(L_ERROR, "%s: problem writing to cld pipe (%ld): %m",
+                        __func__, wsize);
+               ret = cld_pipe_open(clnt);
+               if (ret) {
+                       xlog(L_FATAL, "%s: unable to reopen pipe: %d",
+                                       __func__, ret);
+                       exit(ret);
+               }
+       }
+}
+
 static void
 cldcb(int UNUSED(fd), short which, void *data)
 {
@@ -230,6 +262,9 @@ cldcb(int UNUSED(fd), short which, void *data)
        case Cld_Remove:
                cld_remove(clnt);
                break;
+       case Cld_Check:
+               cld_check(clnt);
+               break;
        default:
                xlog(L_WARNING, "%s: command %u is not yet implemented",
                                __func__, cmsg->cm_cmd);
index a198c34aa6537737a350da2081a0819007482336..01bba1a13c276d80aef95a0af7ea43e3ade6cd6b 100644 (file)
@@ -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;
+}
index 425f5bab67888c6ea8c0543a81be9d35ee1844d2..59ebd723f5ea13c61e7617f7d7f70383fd9c9804 100644 (file)
@@ -23,5 +23,6 @@
 int sqlite_maindb_init(char *topdir);
 int sqlite_insert_client(const unsigned char *clname, const size_t namelen);
 int sqlite_remove_client(const unsigned char *clname, const size_t namelen);
+int sqlite_check_client(const unsigned char *clname, const size_t namelen);
 
 #endif /* _SQLITE_H */