integration with cynara - phase 1 32/72132/2
authorKyoungyong Lee <bluevox@naver.com>
Tue, 31 May 2016 00:51:45 +0000 (09:51 +0900)
committerKyoungyong Lee <bluevox@naver.com>
Tue, 31 May 2016 04:02:16 +0000 (13:02 +0900)
Change-Id: I2c2f824ddd157af62aeae4e5947e786cb82b7344

common/inc/PrivacyGuardTypes.h
common/src/PrivacyIdInfo.cpp
server/src/CynaraService.cpp
server/src/PrivacyGuardDaemon.cpp
server/src/PrivacyGuardDb.cpp
server/src/SocketService.cpp

index bf72ff6..ea5b703 100755 (executable)
@@ -24,7 +24,7 @@
 #define PRIVACY_DB_PATH         tzplatform_mkpath(TZ_SYS_DB,".privacy_guard.db")
 
 //#define CYNARA_BUFFER_SIZE 1024
-#define CYNARA_BUFFER_SIZE 10
+#define CYNARA_BUFFER_SIZE 100
 
 typedef struct _privacy_data_s {
        char *privacy_id;
index d42dc93..11764d7 100755 (executable)
@@ -49,7 +49,7 @@ PrivacyIdInfo::initialize(void)
 
                for (k = privilege_list; k != NULL; k = k->next) {
                        char *privilege_id = (char*)k->data;
-                       PG_LOGD("(privacy, privilege): (%s, %s)", privacy_id, privilege_id);
+                       //PG_LOGD("(privacy, privilege): (%s, %s)", privacy_id, privilege_id);
                        m_privilegeToPrivacyMap.insert(std::map< std::string, std::string >::value_type(std::string(privilege_id), std::string(privacy_id)));
                }
        }
index 4b8428b..17589c3 100755 (executable)
@@ -33,6 +33,7 @@
 static cynara_monitor_configuration *p_conf;
 static cynara_monitor *p_cynara_monitor;
 static cynara_monitor_entry **monitor_entries;
+static bool exit_flag;
 
 CynaraService::CynaraService(void)
        : m_signalToClose(-1)
@@ -88,12 +89,16 @@ CynaraService::start(void)
        TryReturn( res >= 0, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "pthread_sigmask : %s", strerror_r(errno, buf, sizeof(buf)));
 
        pthread_t cynaraThread;
+       PG_LOGD("starting new thread (getEntriesThread)");
        res = pthread_create(&cynaraThread, NULL, &getEntriesThread, this);
        TryReturn( res >= 0, PRIV_GUARD_ERROR_SYSTEM_ERROR, errno = res, "pthread_create : %s", strerror_r(errno, buf, sizeof(buf)));
+       PG_LOGD("new thread (getEntriesThread) started");
 
        m_cynaraThread = cynaraThread;
 
-       PG_LOGI("CynaraService started");
+       exit_flag = false;
+
+       PG_LOGD("CynaraService started");
 
        return PRIV_GUARD_ERROR_SUCCESS;
 }
@@ -101,35 +106,36 @@ CynaraService::start(void)
 void*
 CynaraService::getEntriesThread(void* pData)
 {
-       pthread_detach(pthread_self());
        PG_LOGD("[cynara_service] Running get entries thread");
 
-//     pthread_t testThread;
-//     int result = pthread_create(&testThread, NULL, &flushThread, NULL);
-//     if(result){
-//             PG_LOGE("pthread_create FAIL");
-//             return (void*) PRIV_GUARD_ERROR_SYSTEM_ERROR;
-//     }
+       int res = -1;
+       pthread_detach(pthread_self());
 
-       // cynara_monitor_entries_get
-       // returned when the cynara buffer is full or cynara_monitor_entries_flush() is called from another thread
-       int res = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
-       if(res != CYNARA_API_SUCCESS){
-               PG_LOGE("cynara_monitor_entries_get FAIL");
-               return (void*) PRIV_GUARD_ERROR_SYSTEM_ERROR;
-       }
+       while (exit_flag == false) {
+               PG_LOGD("[cynara_service] waiting for monitor entries");
 
-       res = CynaraService::updateDb(monitor_entries);
-       if(res != PRIV_GUARD_ERROR_SUCCESS){
-               PG_LOGE("updateDb FAIL");
-               return (void*) res;
-       }
+               // cynara_monitor_entries_get
+               // returned when the cynara buffer is full or cynara_monitor_entries_flush() is called from another thread
+               res = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
+               if(res != CYNARA_API_SUCCESS){
+                       PG_LOGE("cynara_monitor_entries_get() is failed. [%d]", res);
+                       return (void*) PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
+
+               res = CynaraService::updateDb(monitor_entries);
+               if(res != PRIV_GUARD_ERROR_SUCCESS){
+                       PG_LOGE("updateDb FAIL");
+                       return (void*) res;
+               }
 
-//     pthread_join(testThread, NULL);
+       //      pthread_join(testThread, NULL);
+
+               cynara_monitor_entries_free(monitor_entries);
+       }
 
        cynara_monitor_entries_free(monitor_entries);
 
-       return (void*) 0;
+       return (void*) PRIV_GUARD_ERROR_SUCCESS;
 }
 
 /*void*
@@ -155,11 +161,11 @@ CynaraService::flushThread(void* pData)
 int
 CynaraService::updateDb(cynara_monitor_entry **monitor_entries)
 {
-       PG_LOGD("[cynara_service]");
+       PG_LOGD("[cynara_service] updateDb called");
 
        cynara_monitor_entry **entryIter = monitor_entries;
 
-       PG_LOGD("entryIter = %x", entryIter);
+       //PG_LOGD("entryIter = %x", entryIter);
 
        // DB update
        const char *user = NULL, *client = NULL, *privilege = NULL;
@@ -171,16 +177,27 @@ CynaraService::updateDb(cynara_monitor_entry **monitor_entries)
        while (*entryIter != nullptr) {
                user = cynara_monitor_entry_get_user(*entryIter);
                TryReturn(user != NULL, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "User Id in the entry is NULL");
+               PG_LOGD("@@ userid: [%s]", user);
                client = cynara_monitor_entry_get_client(*entryIter);
                TryReturn(user != NULL, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "Package Id in the entry is NULL");
+               PG_LOGD("@@ client: [%s]", client);
                privilege = cynara_monitor_entry_get_privilege(*entryIter);
                TryReturn(user != NULL, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "Privilege Id in the entry is NULL");
+               PG_LOGD("@@ privilege: [%s]", privilege);
                timestamp = cynara_monitor_entry_get_timestamp(*entryIter);
                TryReturn(user != NULL, PRIV_GUARD_ERROR_SYSTEM_ERROR, , "timestamp in the entry is NULL");
 
                userId = atoi(user);
-               packageId = client;
+               PG_LOGD("## userId: [%d]", userId);
+               std::string tempPackageId = client;
+               if (tempPackageId.substr(0, 11).compare("User::App::") == 0) {
+                       packageId = tempPackageId.substr(11, tempPackageId.length() - 11);
+               } else {
+                       packageId = client;
+               }
+               PG_LOGD("## packageId: [%s]", packageId.c_str());
                privilegeId = privilege;
+               PG_LOGD("## privilegeId: [%s]", privilegeId.c_str());
                date = timestamp->tv_sec;
 
                // add access log
@@ -207,6 +224,20 @@ CynaraService::stop(void)
        char buf[BUF_SIZE];
        int ret;
 
+       // set thread exit condition
+       exit_flag = true;
+
+       // [CYNARA] Fluch Entries
+       ret = cynara_monitor_entries_flush(p_cynara_monitor);
+       if(ret != CYNARA_API_SUCCESS){
+               if (ret == CYNARA_API_OPERATION_NOT_ALLOWED) {
+                       PG_LOGD("There is no logs in the cynara buffer.");
+               } else {
+                       PG_LOGE("cynara_monitor_entries_flush FAIL [%d]", ret);
+                       return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
+       }
+
        if((ret = pthread_kill(m_cynaraThread, m_signalToClose)) < 0)
        {
                //errno = ret;
index 525a539..f16a733 100755 (executable)
@@ -80,14 +80,14 @@ PrivacyGuardDaemon::start(void)
        }
 
        // [CYNARA]
-/*     PG_LOGD("calling pCynaraService->start()");
+       PG_LOGD("calling pCynaraService->start()");
        if (pCynaraService == NULL)
                return PRIV_GUARD_ERROR_NOT_INITIALIZED;
        res = pCynaraService->start();
        if(res != PRIV_GUARD_ERROR_SUCCESS){
                PG_LOGE("FAIL");
        }
-*/
+
        return res;
 }
 
index 90108ad..819c1c9 100755 (executable)
@@ -146,6 +146,8 @@ PrivacyGuardDb::PgAddPrivacyAccessLog(const int userId, std::list < std::pair <
 int
 PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::string packageId, const std::string privilegeId, const time_t date)
 {
+       PG_LOGD("start.");
+
        if(userId < 0 || date <= 0) {
                PG_LOGE("Invalid parameter: userId: [%d], date: [%d]", userId, date);
                return PRIV_GUARD_ERROR_INVALID_PARAMETER;
@@ -154,6 +156,8 @@ PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::stri
        int res = -1;
        std::string privacyId;
 
+       PG_LOGD("getting privacy id from privilege [%s]", privilegeId.c_str());
+
        // change from privilege to privacy
        res = PrivacyIdInfo::getPrivacyIdFromPrivilege(privilegeId, privacyId);
        if (res == PRIV_GUARD_ERROR_NO_DATA) {
@@ -162,6 +166,8 @@ PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::stri
        }
        TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "getPrivacyIdFromPrivilege is failed: [%d]", res);
 
+       PG_LOGD("getting monitor policy");
+
        // check monitor policy using userId, packageId, privacyId
        int monitorPolicy;
        res = PgGetMonitorPolicy(userId, packageId, privacyId, monitorPolicy);
@@ -171,6 +177,8 @@ PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::stri
                return PRIV_GUARD_ERROR_SUCCESS;
        }
 
+       PG_LOGD("## UserID[%d], PackageId[%s], PrivacyId[%s], Policy[%d]", userId, packageId.c_str(), privacyId.c_str(), monitorPolicy);
+
        static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
 
        m_dbMutex.lock();
@@ -505,12 +513,17 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPackage(const int userId, const int
        int res = -1;
 
        // [CYNARA] Fluch Entries
-       int ret= cynara_monitor_entries_flush(p_cynara_monitor);
-       if(ret != CYNARA_API_SUCCESS){
-               PG_LOGE("cynara_monitor_entries_flush FAIL");
-               return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+       res = cynara_monitor_entries_flush(p_cynara_monitor);
+       if(res != CYNARA_API_SUCCESS){
+               if (res == CYNARA_API_OPERATION_NOT_ALLOWED) {
+                       PG_LOGD("There is no logs in the cynara buffer.");
+               } else {
+                       PG_LOGE("cynara_monitor_entries_flush FAIL [%d]", res);
+                       return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
        }
 
+
 /*     // [CYNARA] Get Entries
        ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
        if(ret != CYNARA_API_SUCCESS){
@@ -597,12 +610,17 @@ PrivacyGuardDb::PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int
        int res = -1;
 
        // [CYNARA] Fluch Entries
-       int ret= cynara_monitor_entries_flush(p_cynara_monitor);
-       if(ret != CYNARA_API_SUCCESS){
-               PG_LOGE("cynara_monitor_entries_flush FAIL");
-               return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+       res = cynara_monitor_entries_flush(p_cynara_monitor);
+       if(res != CYNARA_API_SUCCESS){
+               if (res == CYNARA_API_OPERATION_NOT_ALLOWED) {
+                       PG_LOGD("There is no logs in the cynara buffer.");
+               } else {
+                       PG_LOGE("cynara_monitor_entries_flush FAIL [%d]", res);
+                       return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
        }
 
+
 /*     // [CYNARA] Get Entries
        ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
        if(ret != CYNARA_API_SUCCESS){
@@ -676,12 +694,17 @@ PrivacyGuardDb::PgForeachPrivacyCountByPrivacyId(const int userId, const int sta
        int res = -1;
 
        // [CYNARA] Fluch Entries
-       int ret= cynara_monitor_entries_flush(p_cynara_monitor);
-       if(ret != CYNARA_API_SUCCESS){
-               PG_LOGE("cynara_monitor_entries_flush FAIL");
-               return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+       res = cynara_monitor_entries_flush(p_cynara_monitor);
+       if(res != CYNARA_API_SUCCESS){
+               if (res == CYNARA_API_OPERATION_NOT_ALLOWED) {
+                       PG_LOGD("There is no logs in the cynara buffer.");
+               } else {
+                       PG_LOGE("cynara_monitor_entries_flush FAIL [%d]", res);
+                       return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
        }
 
+
 /*     // [CYNARA] Get Entries
        ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
        if(ret != CYNARA_API_SUCCESS){
@@ -777,12 +800,17 @@ PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int sta
        int res = -1;
 
        // [CYNARA] Fluch Entries
-       int ret= cynara_monitor_entries_flush(p_cynara_monitor);
-       if(ret != CYNARA_API_SUCCESS){
-               PG_LOGE("cynara_monitor_entries_flush FAIL");
-               return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+       res = cynara_monitor_entries_flush(p_cynara_monitor);
+       if(res != CYNARA_API_SUCCESS){
+               if (res == CYNARA_API_OPERATION_NOT_ALLOWED) {
+                       PG_LOGD("There is no logs in the cynara buffer.");
+               } else {
+                       PG_LOGE("cynara_monitor_entries_flush FAIL [%d]", res);
+                       return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
        }
 
+
 /*     // [CYNARA] Get Entries
        ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
        if(ret != CYNARA_API_SUCCESS){
@@ -1062,6 +1090,17 @@ PrivacyGuardDb::PgForeachPackageInfoByPrivacyId(const int userId, const std::str
        date = localtime(&start_date);
        PG_LOGD("start time [%d]: %4d/%2d/%2d %2d:%2d", start_date, date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min);
 
+       // [CYNARA] Fluch Entries
+       res = cynara_monitor_entries_flush(p_cynara_monitor);
+       if(res != CYNARA_API_SUCCESS){
+               if (res == CYNARA_API_OPERATION_NOT_ALLOWED) {
+                       PG_LOGD("There is no logs in the cynara buffer.");
+               } else {
+                       PG_LOGE("cynara_monitor_entries_flush FAIL [%d]", res);
+                       return PRIV_GUARD_ERROR_SYSTEM_ERROR;
+               }
+       }
+
        m_dbMutex.lock();
 
        // initialize
index 471f92d..8744ae1 100755 (executable)
@@ -39,18 +39,18 @@ SocketService::SocketService(void)
        , m_signalToClose(-1)
        , m_mainThread(-1)
 {
-       PG_LOGD("SocketService() called.");
+       //PG_LOGD("SocketService() called.");
 }
 
 SocketService::~SocketService(void)
 {
-       PG_LOGD("~SocketService() called.");
+       //PG_LOGD("~SocketService() called.");
 }
 
 int
 SocketService::initialize(void)
 {
-       PG_LOGD("SocketService initializing");
+       //PG_LOGD("SocketService initializing");
 
        char buf[BUF_SIZE];
        m_listenFd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -78,7 +78,7 @@ SocketService::initialize(void)
 
        umask(original_umask);
 
-       PG_LOGI("SocketService initialized");
+       //PG_LOGI("SocketService initialized");
 
        return PRIV_GUARD_ERROR_SUCCESS;
 }
@@ -86,7 +86,7 @@ SocketService::initialize(void)
 int
 SocketService::start(void)
 {
-       PG_LOGI("SocketService starting");
+       //PG_LOGI("SocketService starting");
 
        sigset_t sigset;
        sigemptyset(&sigset);
@@ -107,7 +107,7 @@ SocketService::start(void)
 
        m_mainThread = mainThread;
 
-       PG_LOGI("SocketService started");
+       //PG_LOGI("SocketService started");
 
        return PRIV_GUARD_ERROR_SUCCESS;
 }
@@ -115,11 +115,11 @@ SocketService::start(void)
 void*
 SocketService::serverThread(void* pData)
 {
-       PG_LOGD("serverThread() called.");
+       //PG_LOGD("serverThread() called.");
        pthread_detach(pthread_self());
        SocketService &t = *static_cast< SocketService* > (pData);
 
-       PG_LOGD("Running main thread");
+       //PG_LOGD("Running main thread");
        int ret = t.mainloop();
        if (ret < 0) {
                return (void*) 1;
@@ -131,7 +131,7 @@ SocketService::serverThread(void* pData)
 int
 SocketService::mainloop(void)
 {
-       PG_LOGD("mainloop() called.");
+       //PG_LOGD("mainloop() called.");
 
        char buf[BUF_SIZE];
 
@@ -226,11 +226,11 @@ SocketService::mainloop(void)
 void*
 SocketService::connectionThread(void* pData)
 {
-       PG_LOGD("connectionThread() called.");
+       //PG_LOGD("connectionThread() called.");
        pthread_detach(pthread_self());
        std::unique_ptr<ConnectionInfo> connectionInfo (static_cast<ConnectionInfo *>(pData));
        SocketService &t = *static_cast<SocketService *>(connectionInfo->pData);
-       PG_LOGI("Starting connection thread");
+       //PG_LOGI("Starting connection thread");
        int ret = t.connectionService(connectionInfo->connFd);
        if (ret < 0)
        {
@@ -239,14 +239,14 @@ SocketService::connectionThread(void* pData)
                close(connectionInfo->connFd);
                return (void*)1;
        }
-       PG_LOGI("Client serviced");
+       //PG_LOGI("Client serviced");
        return (void*)0;
 }
 
 int
 SocketService::connectionService(int fd)
 {
-       PG_LOGD("connectionService() called.");
+       //PG_LOGD("connectionService() called.");
 
        SocketConnection connector = SocketConnection(fd);
        std::string interfaceName, methodName;
@@ -280,14 +280,14 @@ SocketService::connectionService(int fd)
 //             }
 //     }
 
-       PG_LOGI("Calling service");
+       //PG_LOGI("Calling service");
        m_callbackMap[interfaceName][methodName]->serviceCallback(&connector);
 
-       PG_LOGI("Removing client");
+       //PG_LOGI("Removing client");
        removeClientSocket(fd);
        close(fd);
 
-       PG_LOGI("Call served");
+       //PG_LOGI("Call served");
 
        return PRIV_GUARD_ERROR_SUCCESS;
 }
@@ -295,7 +295,7 @@ SocketService::connectionService(int fd)
 int
 SocketService::stop(void)
 {
-       PG_LOGD("Stopping");
+       //PG_LOGD("Stopping");
 
        char buf[BUF_SIZE];
 
@@ -315,7 +315,7 @@ SocketService::stop(void)
        }
        pthread_join(m_mainThread, NULL);
 
-       PG_LOGD("Stopped");
+       //PG_LOGD("Stopped");
 
        return PRIV_GUARD_ERROR_SUCCESS;
 }
@@ -323,14 +323,14 @@ SocketService::stop(void)
 int
 SocketService::shutdown(void)
 {
-       PG_LOGD("called");
+       //PG_LOGD("called");
        return PRIV_GUARD_ERROR_SUCCESS;
 }
 
 int
 SocketService::registerServiceCallback(const std::string &interfaceName,  const std::string &methodName, socketServiceCallback callbackMethod)
 {
-       PG_LOGD("called");
+       //PG_LOGD("called");
 
        if(NULL == callbackMethod)
        {
@@ -352,7 +352,7 @@ SocketService::registerServiceCallback(const std::string &interfaceName,  const
 void
 SocketService::addClientSocket(int clientSocket)
 {
-       PG_LOGD("called");
+       //PG_LOGD("called");
 
        std::lock_guard<std::mutex> guard(m_clientSocketListMutex);
        m_clientSocketList.push_back(clientSocket);
@@ -361,7 +361,7 @@ SocketService::addClientSocket(int clientSocket)
 void
 SocketService::removeClientSocket(int clientSocket)
 {
-       PG_LOGD("called");
+       //PG_LOGD("called");
 
        std::lock_guard<std::mutex> guard(m_clientSocketListMutex);
        m_clientSocketList.remove(clientSocket);
@@ -370,7 +370,7 @@ SocketService::removeClientSocket(int clientSocket)
 bool
 SocketService::popClientSocket(int * pClientSocket)
 {
-       PG_LOGD("called");
+       //PG_LOGD("called");
 
        std::lock_guard<std::mutex> guard(m_clientSocketListMutex);
        if(m_clientSocketList.empty())
@@ -384,7 +384,7 @@ SocketService::popClientSocket(int * pClientSocket)
 void
 SocketService::closeConnections(void)
 {
-       PG_LOGD("called");
+       //PG_LOGD("called");
 
        int clientSocket;
        char buf[BUF_SIZE];
@@ -397,5 +397,5 @@ SocketService::closeConnections(void)
                }
        }
 
-       PG_LOGD("Connections closed");
+       //PG_LOGD("Connections closed");
 }