From patchwork Thu Oct 25 17:21:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 1646091 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 9480CDF2AB for ; Thu, 25 Oct 2012 17:21:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1946162Ab2JYRV6 (ORCPT ); Thu, 25 Oct 2012 13:21:58 -0400 Received: from mail-vb0-f46.google.com ([209.85.212.46]:50610 "EHLO mail-vb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965500Ab2JYRV4 (ORCPT ); Thu, 25 Oct 2012 13:21:56 -0400 Received: by mail-vb0-f46.google.com with SMTP id ff1so2161469vbb.19 for ; Thu, 25 Oct 2012 10:21:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=otTzslSYJgvyYQihjb+34fY/uEj+cW4jqEyMR0u/yQA=; b=ElcC7lWwXmAL6dfChHpLWzDbCz2eeN1zE+x5wwmY+gLchBtU/8mcztpLgQKsPzXyDW f5LQgZl5sgmlQycixXgVsM+rsc/b1vgcdkQKncfK+tCnTh0jdwDQFN+N72u1MQ/pfPpv bGkIW1kbYcX2YxW284f8CzbWvTzBodMps9cICm2iMSiO3XOm1g+jpDbfGGLT1FS0kW5H OLv2suPX5z9smRSxMj/lpz5jIRpP++mBtaLTUOGaik+IYus5egTvzL6ML+nkdNZWTKYf 2M12Rum5Bucrbq1sptlBE8fUR855AGO0VR1SOhE8dtXgK9ryYwu7XmqIzwkCl86iDekc 53Kg== Received: by 10.220.154.83 with SMTP id n19mr15122121vcw.49.1351185716144; Thu, 25 Oct 2012 10:21:56 -0700 (PDT) Received: from salusa.poochiereds.net (cpe-107-015-110-129.nc.res.rr.com. [107.15.110.129]) by mx.google.com with ESMTPS id k4sm19373961vdg.2.2012.10.25.10.21.54 (version=SSLv3 cipher=OTHER); Thu, 25 Oct 2012 10:21:54 -0700 (PDT) From: Jeff Layton To: steved@redhat.com Cc: linux-nfs@vger.kernel.org Subject: [PATCH v3 4/9] nfsdcltrack: break out a function to open the database handle Date: Thu, 25 Oct 2012 13:21:38 -0400 Message-Id: <1351185703-14191-5-git-send-email-jlayton@redhat.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1351185703-14191-1-git-send-email-jlayton@redhat.com> References: <1351185703-14191-1-git-send-email-jlayton@redhat.com> X-Gm-Message-State: ALoCoQlPWZ63pR4zgMQ57L5OBfcRIX8lxhp+Vm5v5HabZvhnesrQ+kVKxWeXRov3VPXmJRTtVQmn Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org When we add a new usermodehelper upcall program to do the database access, the existing "init" function will be overkill every time we start up the program. Break out the database handle establishment routine into a separate function that we can call from each upcall command in the one-shot program. Signed-off-by: Jeff Layton --- utils/nfsdcltrack/sqlite.c | 49 ++++++++++++++++++++++++++++++++-------------- utils/nfsdcltrack/sqlite.h | 1 + 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/utils/nfsdcltrack/sqlite.c b/utils/nfsdcltrack/sqlite.c index c19af7e..bac6789 100644 --- a/utils/nfsdcltrack/sqlite.c +++ b/utils/nfsdcltrack/sqlite.c @@ -90,24 +90,15 @@ mkdir_if_not_exist(const char *dirname) return ret; } -/* - * Open the "main" database, and attempt to initialize it by creating the - * parameters table and inserting the schema version into it. Ignore any errors - * from that, and then attempt to select the version out of it again. If the - * version appears wrong, then assume that the DB is corrupt or has been - * upgraded, and return an error. If all of that works, then attempt to create - * the "clients" table. - */ +/* Open the database and set up the database handle for it */ int -sqlite_maindb_init(const char *topdir) +sqlite_prepare_dbh(const char *topdir) { int ret; - char *err = NULL; - sqlite3_stmt *stmt = NULL; - ret = mkdir_if_not_exist(topdir); - if (ret) - return ret; + /* Do nothing if the database handle is already set up */ + if (dbh) + return 0; ret = snprintf(buf, PATH_MAX - 1, "%s/main.sqlite", topdir); if (ret < 0) @@ -118,15 +109,43 @@ sqlite_maindb_init(const char *topdir) ret = sqlite3_open(buf, &dbh); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to open main database: %d", ret); + dbh = NULL; return ret; } ret = sqlite3_busy_timeout(dbh, CLD_SQLITE_BUSY_TIMEOUT); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to set sqlite busy timeout: %d", ret); - goto out_err; + sqlite3_close(dbh); + dbh = NULL; } + return ret; +} + +/* + * Open the "main" database, and attempt to initialize it by creating the + * parameters table and inserting the schema version into it. Ignore any errors + * from that, and then attempt to select the version out of it again. If the + * version appears wrong, then assume that the DB is corrupt or has been + * upgraded, and return an error. If all of that works, then attempt to create + * the "clients" table. + */ +int +sqlite_maindb_init(const char *topdir) +{ + int ret; + char *err = NULL; + sqlite3_stmt *stmt = NULL; + + ret = mkdir_if_not_exist(topdir); + if (ret) + return ret; + + ret = sqlite_prepare_dbh(topdir); + if (ret) + return ret; + /* Try to create table */ ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS parameters " "(key TEXT PRIMARY KEY, value TEXT);", diff --git a/utils/nfsdcltrack/sqlite.h b/utils/nfsdcltrack/sqlite.h index 8748948..ebf04c3 100644 --- a/utils/nfsdcltrack/sqlite.h +++ b/utils/nfsdcltrack/sqlite.h @@ -20,6 +20,7 @@ #ifndef _SQLITE_H_ #define _SQLITE_H_ +int sqlite_prepare_dbh(const char *topdir); int sqlite_maindb_init(const 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);