From 54a071e9e09e08497ac6c01c511372f722638b38 Mon Sep 17 00:00:00 2001 From: Kazimierz Krosman Date: Mon, 30 May 2016 15:43:01 +0200 Subject: [PATCH] Fix: xml parser + minor fixes Fixed xml parser bug that causes that only first works as it should. Removed deadlock in init when logs are enabled Minor refactors Change-Id: I94c97efff478cf919cc5809f106bca1bb1465889 --- src/internal/policy.cpp | 5 +++-- src/internal/policy.hpp | 2 +- src/internal/xml_parser.hpp | 20 +++++++++++--------- src/libdbuspolicy1.c | 10 +++++++--- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/internal/policy.cpp b/src/internal/policy.cpp index 021a4be..da65a5c 100644 --- a/src/internal/policy.cpp +++ b/src/internal/policy.cpp @@ -1,6 +1,5 @@ #include "policy.hpp" #include "naive_policy_db.hpp" -//#include "tslog.hpp" #include #include #include @@ -192,7 +191,7 @@ void DbAdapter::xmlTraversal(bool bus, } } -void DbAdapter::updateDb(bool bus, boost::property_tree::ptree& xmlTree) { +void DbAdapter::updateDb(bool bus, boost::property_tree::ptree& xmlTree, std::vector& incl_dirs) { const auto& children = xmlTree.get_child("busconfig"); PolicyType policy_type; PolicyTypeValue policy_type_value; @@ -201,6 +200,8 @@ void DbAdapter::updateDb(bool bus, boost::property_tree::ptree& xmlTree) { __tag_state = POLICY; __attr = false; xmlTraversal(bus, x.second, POLICY, policy_type, policy_type_value); + } else if (x.first == "includedir") { + incl_dirs.push_back(x.second.data()); } } } diff --git a/src/internal/policy.hpp b/src/internal/policy.hpp index 4909e4e..aa93cc8 100644 --- a/src/internal/policy.hpp +++ b/src/internal/policy.hpp @@ -186,7 +186,7 @@ namespace ldp_xml_parser int level = 0); public: DbAdapter(NaivePolicyDb& system, NaivePolicyDb& session); - void updateDb(bool bus, boost::property_tree::ptree& xmlTree); + void updateDb(bool bus, boost::property_tree::ptree& xmlTree, std::vector& incl_dirs); static uid_t convertToUid(const char* user); static gid_t convertToGid(const char* group); }; diff --git a/src/internal/xml_parser.hpp b/src/internal/xml_parser.hpp index 26aee38..0dfb668 100644 --- a/src/internal/xml_parser.hpp +++ b/src/internal/xml_parser.hpp @@ -23,7 +23,6 @@ #include #include #include -//#include "xml_policy.hpp" #include "libdbuspolicy1-private.hpp" #include "tslog.hpp" #include "policy.hpp" @@ -65,13 +64,14 @@ namespace ldp_xml_parser ErrCode parse(bool bus, bool first, const std::string& filename, std::vector& included_files) { std::pair errparam; - + std::vector incl_dirs; if (tslog::verbose()) std::cout << "=== XML PARSING BEGIN === : " << filename << '\n'; - errparam = parseXml(bus, filename); - if (first && errparam.first.get() >= 0 && errparam.second != "") - getIncludedFiles(filename, errparam.second, included_files); + errparam = parseXml(bus, filename, incl_dirs); + for (int i = 0; i < incl_dirs.size(); i++) { + getIncludedFiles(filename, incl_dirs[i], included_files); + } if (tslog::enabled()) { if (tslog::verbose()) @@ -88,7 +88,10 @@ namespace ldp_xml_parser std::string fname; std::copy(filename.begin(), filename.end(), fname.begin()); std::string dname = dirname(const_cast(fname.c_str())); - dname += (std::string("/") + incldir); + if (incldir[0] != '/') + dname += (std::string("/") + incldir); + else + dname = incldir; files.clear(); if((dir = opendir(dname.c_str())) != NULL) { while((ent = readdir(dir)) != NULL) { @@ -108,7 +111,7 @@ namespace ldp_xml_parser std::cout << "could not open directory " << dname << '\n'; } - std::pair parseXml(bool bus, const std::string& filename) { + std::pair parseXml(bool bus, const std::string& filename, std::vector& incl_dirs) { std::pair ret; if (__parsed.insert(filename).second) @@ -116,8 +119,7 @@ namespace ldp_xml_parser boost::property_tree::ptree pt; read_xml(filename, pt); if (!pt.empty()) { - __adapter->updateDb(bus, pt); - ret.second = pt.get("busconfig.includedir", ""); + __adapter->updateDb(bus, pt, incl_dirs); } } catch(const boost::property_tree::xml_parser::xml_parser_error& ex) { ret.first = ErrCode::error(ex.what()); diff --git a/src/libdbuspolicy1.c b/src/libdbuspolicy1.c index 7ad44dd..c274833 100644 --- a/src/libdbuspolicy1.c +++ b/src/libdbuspolicy1.c @@ -219,13 +219,17 @@ DBUSPOLICY1_EXPORT void* dbuspolicy1_init(const char *bus_path) goto err_close; rp = __internal_init(bus_type, (bus_type == SYSTEM_BUS) ? SYSTEM_BUS_CONF_FILE_PRIMARY : SESSION_BUS_CONF_FILE_PRIMARY); - rs = __internal_init(bus_type, (bus_type == SYSTEM_BUS) ? SYSTEM_BUS_CONF_FILE_SECONDARY : SESSION_BUS_CONF_FILE_SECONDARY); + if (rp < 0) + rs = __internal_init(bus_type, (bus_type == SYSTEM_BUS) ? SYSTEM_BUS_CONF_FILE_SECONDARY : SESSION_BUS_CONF_FILE_SECONDARY); + else + rs = 1; + + pthread_mutex_unlock(&g_mutex); __internal_init_flush_logs(); - if ((rp & rs) < 0) /* when both negative */ + if (rp < 0 && rs < 0) /* when both negative */ goto err_close; - pthread_mutex_unlock(&g_mutex); return &g_conn[bus_type]; err_close: -- 2.7.4