Fix input/return data type for rule and loader 26/176326/4
authorseolheui kim <s414.kim@samsung.com>
Wed, 18 Apr 2018 10:29:29 +0000 (19:29 +0900)
committerseolheui kim <s414.kim@samsung.com>
Thu, 19 Apr 2018 09:25:30 +0000 (18:25 +0900)
Change-Id: Id5942993bfd912b2174d72b50555c4f7c81d249b
Signed-off-by: seolheui kim <s414.kim@samsung.com>
lib/rule/field.h
lib/rule/rule-set.h
lib/rule/rule.cpp
lib/rule/rule.h
server/loader.cpp
server/loader.h

index 372901f137351d1f3afa4618a87d00f03e9c61db..5e0c707e4c82887752251bd1e84577bcf1f8121c 100644 (file)
@@ -74,8 +74,8 @@ enum class Operator : unsigned int {
 
 class FieldBase {
 public:
-       virtual void emit(std::vector<char> &rule) = 0;
-       virtual unsigned int type() = 0;
+       virtual void emit(std::vector<char> &rule) const = 0;
+       virtual unsigned int type() const = 0;
 };
 
 template <typename T>
@@ -84,7 +84,8 @@ public:
        using RuleData = struct audit_rule_data;
 
        Field(FieldType type)
-               : _type(static_cast<unsigned int>(type))
+               : _type(static_cast<unsigned int>(type)),
+               _op(0)
        {
        }
        /* TODO: to be removed below constructor */
@@ -96,55 +97,56 @@ public:
        {
        }
 
-       unsigned int type()
+       unsigned int type() const
        {
                return _type;
        }
 
-       Field &&operator == (T value) {
+       Field operator == (T value) {
                return create(value, Operator::Equal);
        }
 
-       Field &&operator != (T value) {
+       Field operator != (T value) {
                return create(value, Operator::NotEqual);
        }
 
-       Field &&operator < (T value) {
+       Field operator < (T value) {
                return create(value, Operator::LessThan);
        }
 
-       Field &&operator <= (T value) {
+       Field operator <= (T value) {
                return create(value, Operator::LessThanEqual);
        }
 
-       Field &&operator > (T value) {
+       Field operator > (T value) {
                return create(value, Operator::GreaterThan);
        }
 
-       Field &&operator >= (T value) {
+       Field operator >= (T value) {
                return create(value, Operator::GreaterThanEqual);
        }
 
-       void emit(std::vector<char> &rule) {
+       void emit(std::vector<char> &rule) const {
                auto r = reinterpret_cast<RuleData *>(rule.data());
                r->fields[r->field_count] = _type;
                r->fieldflags[r->field_count] = _op;
-               setValue(rule, _value);
+               emitValue(rule, _value);
        }
 
 private:
-       Field &&create(T value, Operator op) {
+       Field create(T value, Operator op) {
                _op = static_cast<unsigned int>(op);
                _value = value;
-               return std::move(*this);
+               return *this;
        }
 
-       void setValue(std::vector<char> &rule, int value)
+       void emitValue(std::vector<char> &rule, int value) const
        {
                auto r = reinterpret_cast<RuleData *>(rule.data());
                r->values[r->field_count++] = value;
        }
-       void setValue(std::vector<char> &rule, const std::string &value)
+
+       void emitValue(std::vector<char> &rule, const std::string &value) const
        {
                auto r = reinterpret_cast<RuleData *>(rule.data());
                r->values[r->field_count] = value.size();
index b7510fedadc5bab4b27a4f459004389800a3a053..0fbc80603fc4712449c74a1e8faaaddfb41fe9a7 100644 (file)
@@ -35,11 +35,10 @@ public:
 
        virtual void initialize() = 0;
 
-       RuleList &&operator *()
+       RuleList get() const
        {
-               return std::move(list);
+               return list;
        }
-
 protected:
        void add(const Rule &rule);
 
index a21fd3d7295c9a1a766b1f264022e5395bea0e6c..b90f229a020271a7bbc600d31671789cde7a51b2 100644 (file)
@@ -32,12 +32,8 @@ Rule::Rule(const std::vector<char> &rule)
 Rule::Rule(const Rule &rule)
        : _type(rule._type), buf(sizeof(RuleData))
 {
-       std::vector<char> bufdata(rule.buf);
-       for (auto &c : rule.conditions) {
-               if (c.second)
-                       c.second->emit(bufdata);
-       }
-       setComponents(bufdata);
+       conditions.insert(rule.conditions.begin(), rule.conditions.end());
+       setComponents(rule.buf);
 }
 
 Rule::~Rule()
@@ -110,7 +106,7 @@ std::vector<unsigned int> Rule::getMask()
        return ret;
 }
 
-bool Rule::isStringField(unsigned int type)
+bool Rule::isStringField(unsigned int type) const
 {
        FieldType ftype = static_cast<FieldType>(type);
        return (ftype == FieldType::WatchPath) ||
@@ -134,12 +130,10 @@ void Rule::setComponents(const std::vector<char> &rule)
        for (unsigned int i = 0; i < r->field_count; i++) {
                if (isStringField(r->fields[i])) {
                        std::string value(ruleBuf, ruleBuf + r->values[i]);
-                       conditions[r->fields[i]].reset(
-                                       new Field<std::string>(r->fields[i], r->fieldflags[i], value));
+                       conditions[r->fields[i]].reset(new Field<std::string>(r->fields[i], r->fieldflags[i], value));
                        ruleBuf += r->values[i];
                } else {
-                       conditions[r->fields[i]].reset(
-                                       new Field<int>(r->fields[i], r->fieldflags[i], r->values[i]));
+                       conditions[r->fields[i]].reset(new Field<int>(r->fields[i], r->fieldflags[i], r->values[i]));
                }
        }
 }
@@ -147,7 +141,6 @@ void Rule::setComponents(const std::vector<char> &rule)
 void Rule::updateConditions()
 {
        for (auto &c : conditions) {
-               if (c.second)
-                       c.second->emit(buf);
+               c.second->emit(buf);
        }
 }
index 1babb4eead86360f986a9362abd049f66d446404..065ebc422b1b518380f1c7f981c4954c9637ffa4 100644 (file)
@@ -59,10 +59,10 @@ public:
        Rule(const Rule &rule);
 
        template <typename T>
-       Rule &&operator << (Field<T> &&field)
+       Rule operator << (const Field<T> &field)
        {
-               setCondition(std::move(field));
-               return std::move(*this);
+               setCondition(field);
+               return *this;
        }
 
        bool operator == (const Rule &rule)
@@ -70,7 +70,7 @@ public:
                return buf == rule.buf;
        }
 
-       RuleType type()
+       RuleType type() const
        {
                return _type;
        }
@@ -82,14 +82,15 @@ public:
        void setTag(const std::string &tag);
 
        template <typename T>
-       void setCondition(Field<T> &&field);
+       void setCondition(const Field<T> &field);
        template <typename T>
-       void unsetCondition(Field<T> &&field);
+       void unsetCondition(const Field<T> &field);
 
        void setMask();
-       void setMask(unsigned int syscall = 0);
+       void setMask(unsigned int syscall);
        void unsetMask();
-       void unsetMask(unsigned int syscall = 0);
+       void unsetMask(unsigned int syscall);
+
        std::vector<unsigned int> getMask();
 
 protected:
@@ -107,26 +108,27 @@ private:
        {
                return reinterpret_cast<RuleData*>(buf.data());
        }
-       bool isStringField(unsigned int type);
+
+       bool isStringField(unsigned int type) const;
        void setComponents(const std::vector<char> &rule);
        void updateConditions();
 private:
        RuleType _type;
        std::vector<char> buf;
-       std::unordered_map<unsigned int, std::unique_ptr<FieldBase>> conditions;
+       std::unordered_map<unsigned int, std::shared_ptr<FieldBase>> conditions;
 };
 
 template <typename T>
-void Rule::setCondition(Field<T> &&field)
+void Rule::setCondition(const Field<T> &field)
 {
        conditions[field.type()].reset(new Field<T>(field));
 }
 
 template <typename T>
-void Rule::unsetCondition(Field<T> &&field)
+void Rule::unsetCondition(const Field<T> &field)
 {
        if (conditions.find(field.type()) != conditions.end())
-               conditions[field.type()].reset();
+               conditions.erase(field.type());
 }
 
 #endif /*__AUDIT_RULE_H__*/
index 3bdb21bbd210fcbfff359bae940c6fb91b440a48..8e714145ced2e8537baa34973535b5d4bd15fcd7 100644 (file)
@@ -29,8 +29,10 @@ RuleSetLoader::~RuleSetLoader()
        close();
 }
 
-AbstractRuleSet::RuleList &&RuleSetLoader::load(const std::string &path)
+AbstractRuleSet::RuleList RuleSetLoader::load(const std::string &path)
 {
+       std::unique_ptr<AbstractRuleSet> ruleSet;
+
        if (open(basePath + path, RTLD_LAZY | RTLD_LOCAL) != 0)
                throw runtime::Exception("Failed to load rule set library");
 
@@ -39,7 +41,8 @@ AbstractRuleSet::RuleList &&RuleSetLoader::load(const std::string &path)
                ruleSet.reset(reinterpret_cast<AbstractRuleSet*>(factory()));
                ruleSet->initialize();
        }
-       return **ruleSet;
+
+       return ruleSet->get();
 }
 
 int RuleSetLoader::open(const std::string &path, int flags)
index 00c425b41ac270038701da001b6950edfb6ed58c..f180e354da7aee64c74b1fff38f3974b996c77f6 100644 (file)
@@ -32,14 +32,13 @@ public:
        RuleSetLoader();
        ~RuleSetLoader();
 
-       AbstractRuleSet::RuleList &&load(const std::string &path);
+       AbstractRuleSet::RuleList load(const std::string &path);
 
 private:
        int open(const std::string &path, int flags);
        void close();
 
 private:
-       std::unique_ptr<AbstractRuleSet> ruleSet;
        void *handle;
        std::string basePath;