[svn] commit: r1270 - in /trunk/src/bin/auth: auth_srv.cc auth_srv.h

BIND 10 source code commits bind10-changes at lists.isc.org
Wed Mar 10 08:13:21 UTC 2010


Author: jinmei
Date: Wed Mar 10 08:13:20 2010
New Revision: 1270

Log:
made AuthSrv::updateConfig and setDbFile exception safe.
also hid setDbFile in the implementation class because it's currently only
used internally, and it made the exception handling simpler.

Modified:
    trunk/src/bin/auth/auth_srv.cc
    trunk/src/bin/auth/auth_srv.h

Modified: trunk/src/bin/auth/auth_srv.cc
==============================================================================
--- trunk/src/bin/auth/auth_srv.cc (original)
+++ trunk/src/bin/auth/auth_srv.cc Wed Mar 10 08:13:20 2010
@@ -62,6 +62,9 @@
     AuthSrvImpl& operator=(const AuthSrvImpl& source);
 public:
     AuthSrvImpl();
+
+    isc::data::ElementPtr setDbFile(const isc::data::ElementPtr config);
+
     std::string db_file_;
     isc::auth::MetaDataSrc data_sources_;
     /// We keep a pointer to the currently running sqlite datasource
@@ -131,50 +134,53 @@
 }
 
 ElementPtr
-AuthSrv::setDbFile(const isc::data::ElementPtr config) {
+AuthSrvImpl::setDbFile(const isc::data::ElementPtr config) {
     if (config) {
-        impl_->db_file_ = config->get("database_file")->stringValue();
-        cout << "[AuthSrv] Data source database file: " << impl_->db_file_
-             << endl;
+        db_file_ = config->get("database_file")->stringValue();
+        cout << "[AuthSrv] Data source database file: " << db_file_ << endl;
     }
 
+    // create SQL data source
+    // config may be empty here; in that case it will load the default
+    // database file
+    // Note: the following step is tricky to be exception-safe and to ensure
+    // exception guarantee: We first need to perform all operations that can
+    // fail, while acquiring resources in the RAII manner.  We then perform
+    // delete and swap operations which should not fail.
+    DataSrcPtr datasrc_ptr(DataSrcPtr(new Sqlite3DataSrc));
+    datasrc_ptr->init(config);
+    ElementPtr answer = isc::config::createAnswer(0);
+    data_sources_.addDataSrc(datasrc_ptr);
+
+    // The following code should be exception free.
+    if (cur_datasrc_ != NULL) {
+        data_sources_.removeDataSrc(cur_datasrc_);
+    }
+    cur_datasrc_ = datasrc_ptr;
+
+    return answer;
+}
+
+ElementPtr
+AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
     try {
-        // create SQL data source
-        // config may be empty here; in that case it will load the default
-        // database file
-        Sqlite3DataSrc* sd = new Sqlite3DataSrc;
-        sd->init(config);
-
-        if (impl_->cur_datasrc_) {
-            impl_->data_sources_.removeDataSrc(impl_->cur_datasrc_);
+        ElementPtr answer = isc::config::createAnswer(0);
+        if (new_config != NULL) {
+            // the ModuleCCSession has already checked if we have
+            // the correct ElementPtr type as specified in our .spec file
+            if (new_config->contains("database_file")) {
+                answer = impl_->setDbFile(new_config);
+            }
         }
 
-        ConstDataSrcPtr csd = ConstDataSrcPtr(sd);
-        impl_->data_sources_.addDataSrc(csd);
-        impl_->cur_datasrc_ = csd;
-
-        return isc::config::createAnswer(0);
-    } catch (isc::Exception error) {
+        // if we have no sqlite3 data source, use the default
+        if (impl_->cur_datasrc_ == NULL) {
+            impl_->setDbFile(ElementPtr());
+        }
+    
+        return answer;
+    } catch (const isc::Exception& error) {
         cout << "[AuthSrv] error: " << error.what() << endl;
         return isc::config::createAnswer(1, error.what());
     }
 }
-
-ElementPtr
-AuthSrv::updateConfig(isc::data::ElementPtr new_config) {
-    ElementPtr answer = isc::config::createAnswer(0);
-    if (new_config) {
-        // the ModuleCCSession has already checked if we have
-        // the correct ElementPtr type as specified in our .spec file
-        if (new_config->contains("database_file")) {
-            answer = setDbFile(new_config);
-        }
-    }
-
-    // if we have no sqlite3 data source, use the default
-    if (impl_->cur_datasrc_ == NULL) {
-        setDbFile(ElementPtr());
-    }
-    
-    return answer;
-}

Modified: trunk/src/bin/auth/auth_srv.h
==============================================================================
--- trunk/src/bin/auth/auth_srv.h (original)
+++ trunk/src/bin/auth/auth_srv.h Wed Mar 10 08:13:20 2010
@@ -50,7 +50,6 @@
                        isc::dns::MessageRenderer& response_renderer,
                        bool udp_buffer, bool verbose_mode);
     void serve(std::string zone_name);
-    isc::data::ElementPtr setDbFile(const isc::data::ElementPtr config);
     isc::data::ElementPtr updateConfig(isc::data::ElementPtr config);
 private:
     AuthSrvImpl* impl_;




More information about the bind10-changes mailing list