Add a mutex for netlink socket 69/175869/2
authorSungbae Yoo <sungbae.yoo@samsung.com>
Fri, 13 Apr 2018 04:48:41 +0000 (13:48 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Fri, 13 Apr 2018 05:20:20 +0000 (14:20 +0900)
This is to prevent the race conditions between parser and controller.

Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
Change-Id: Ib836d82d286231fdd6b7cb7ee9c88177939c4183

common/audit/audit-message-parser.cpp
common/audit/audit-message-parser.h
common/audit/audit.cpp
common/audit/audit.h

index 63f9cf319be479fc6d9a7d76f3f93f3d847f89c6..1d49185a3f8db6937fc34aa8639ac63136bc73e5 100644 (file)
@@ -29,7 +29,7 @@
 #include "audit-message-parser.h"
 
 AuditMessageParser::AuditMessageParser(Audit &audit, runtime::Mainloop &ml) :
-       nl(audit.nl), mainloop(ml)
+       nlLock(audit.nlLock), nl(audit.nl), mainloop(ml)
 {
        int fd = nl.getFd();
 
@@ -50,6 +50,7 @@ AuditMessageParser::~AuditMessageParser()
 
 void AuditMessageParser::parse()
 {
+       std::lock_guard<std::mutex> lock(nlLock);
        while (1) {
                try {
                        auto msg = nl.recv(MSG_DONTWAIT);
index 579a854fcf3956d58ebd2dc52373e310e545e38a..afa319b1acf6268d20a79a6e00004f1df9719040 100644 (file)
@@ -41,6 +41,7 @@ public:
 private:
        void parse();
 
+       std::mutex &nlLock;
        netlink::Netlink &nl;
        runtime::Mainloop &mainloop;
 };
index 5bdf0ed6d93389324338209a933aeeeba75e60fb..7aedd29ebc609c3be7fa84e65daa45d5a2635b11 100644 (file)
@@ -65,12 +65,15 @@ void Audit::setEnabled(int enabled)
        s.mask          = AUDIT_STATUS_ENABLED;
        s.enabled       = enabled;
 
+       std::lock_guard<std::mutex> lock(nlLock);
        nl.send(AUDIT_SET, arrayToVector((char*)&s, sizeof(s)));
 }
 
 int Audit::isEnabled()
 {
        int ret = 0;
+
+       std::lock_guard<std::mutex> lock(nlLock);
        nl.send(AUDIT_GET, std::vector<char>());
 
        while (1) {
@@ -94,6 +97,7 @@ std::vector<AuditRule> Audit::getRules()
 {
        std::vector<AuditRule> ret;
 
+       std::lock_guard<std::mutex> lock(nlLock);
        nl.send(AUDIT_LIST_RULES, std::vector<char>());
 
        while (1) {
@@ -115,10 +119,12 @@ std::vector<AuditRule> Audit::getRules()
 
 void Audit::addRule(const AuditRule& rule)
 {
+       std::lock_guard<std::mutex> lock(nlLock);
        nl.send(AUDIT_ADD_RULE, rule.data());
 }
 
 void Audit::removeRule(const AuditRule& rule)
 {
+       std::lock_guard<std::mutex> lock(nlLock);
        nl.send(AUDIT_DEL_RULE, rule.data());
 }
index 09f1d55ffcf96014a64d612a9483e94968190b47..b1328578b3561353d357662c49435de0c8cff729 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __AUDIT_TRAIL_AUDIT_H__
 #define __AUDIT_TRAIL_AUDIT_H__
 
+#include <mutex>
 #include <vector>
 
 #include <klay/netlink/netlink.h>
@@ -46,6 +47,7 @@ public:
        }
 
 private:
+       std::mutex nlLock;
        netlink::Netlink nl;
 
        friend class AuditMessageParser;