Service connection info refactoring
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 21 Nov 2013 10:06:47 +0000 (11:06 +0100)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 6 Feb 2014 16:13:23 +0000 (17:13 +0100)
[Issue#] N/A
[Feature/Bug] N/A
[Problem] Services use similar connection info maps.
[Cause] N/A
[Solution] Commonly used types moved to common header. Service specific types
renamed and moved to class internals.

[Verification] Successfull compilation

Change-Id: I5296bbc7b23b2ed4183a11213398ca8cd85d3691

src/server2/client/client-app-permissions.cpp
src/server2/common/connection-info.h [new file with mode: 0644]
src/server2/common/protocols.h
src/server2/service/app-permissions.cpp
src/server2/service/app-permissions.h
src/server2/service/cookie.cpp
src/server2/service/cookie.h
src/server2/service/open-for.cpp
src/server2/service/open-for.h

index 2f9ec2f..35bda12 100644 (file)
@@ -184,7 +184,7 @@ int security_server_app_has_privilege(const char *app_id,
         LogDebug("privilege_name: " << privilege_name);
 
         //put data into buffer
-        Serialization::Serialize(send, static_cast<int>(PrivilegeCheckCall::CHECK_GIVEN_APP));
+        Serialization::Serialize(send, static_cast<int>(PrivilegeCheckHdrs::CHECK_GIVEN_APP));
         Serialization::Serialize(send, std::string(app_id));
         Serialization::Serialize(send, static_cast<int>(app_type));
         Serialization::Serialize(send, std::string(privilege_name));
@@ -239,7 +239,7 @@ int security_server_app_caller_has_privilege(app_type_t app_type,
         LogDebug("privilege_name: " << privilege_name);
 
         //put data into buffer
-        Serialization::Serialize(send, static_cast<int>(PrivilegeCheckCall::CHECK_CALLER_APP));
+        Serialization::Serialize(send, static_cast<int>(PrivilegeCheckHdrs::CHECK_CALLER_APP));
         Serialization::Serialize(send, static_cast<int>(app_type));
         Serialization::Serialize(send, std::string(privilege_name));
 
diff --git a/src/server2/common/connection-info.h b/src/server2/common/connection-info.h
new file mode 100644 (file)
index 0000000..946b5c8
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        connection-info.h
+ * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
+ * @version     1.0
+ * @brief       Definition of ConnectionInfo structure and ConnectionInfoMap type.
+ */
+
+#ifndef _CONNECTION_INFO_H_
+#define _CONNECTION_INFO_H_
+
+#include <map>
+#include <generic-socket-manager.h>
+#include <message-buffer.h>
+
+namespace SecurityServer
+{
+    struct ConnectionInfo {
+        InterfaceID interfaceID;
+        MessageBuffer buffer;
+    };
+
+    typedef std::map<int, ConnectionInfo> ConnectionInfoMap;
+} //namespace SecurityServer
+
+#endif //_CONNECTION_INFO_H_
index 73a9490..afdb159 100644 (file)
@@ -54,7 +54,7 @@ enum class CookieCall
     CHECK_UID
 };
 
-enum class PrivilegeCheckCall
+enum class PrivilegeCheckHdrs
 {
     CHECK_GIVEN_APP,
     CHECK_CALLER_APP
index f622d02..290650d 100644 (file)
@@ -37,6 +37,7 @@
 #include <security-server.h>
 #include <security-server-common.h>
 
+namespace SecurityServer {
 
 namespace {
 
@@ -55,18 +56,20 @@ int privilegeToSecurityServerError(int error) {
     return SECURITY_SERVER_API_ERROR_UNKNOWN;
 }
 
-} // namespace anonymous
+// interface ids
+const InterfaceID CHANGE_APP_PERMISSIONS = 0;
+const InterfaceID CHECK_APP_PRIVILEGE = 1;
 
-namespace SecurityServer {
+} // namespace anonymous
 
 GenericSocketService::ServiceDescriptionVector AppPermissionsService::GetServiceDescription() {
     return ServiceDescriptionVector {
         { SERVICE_SOCKET_APP_PERMISSIONS,
           "security-server::api-app-permissions",
-          static_cast<int>(InterfaceType::CHANGE_APP_PERMISSIONS) },
+          CHANGE_APP_PERMISSIONS },
         { SERVICE_SOCKET_APP_PRIVILEGE_BY_NAME,
           "security-server::api-app-privilege-by-name",
-          static_cast<int>(InterfaceType::CHECK_APP_PRIVILEGE) }
+          CHECK_APP_PRIVILEGE }
     };
 }
 
@@ -74,8 +77,8 @@ void AppPermissionsService::accept(const AcceptEvent &event) {
     LogDebug("Accept event. ConnectionID.sock: " << event.connectionID.sock
         << " ConnectionID.counter: " << event.connectionID.counter
         << " ServiceID: " << event.interfaceID);
-    auto &info = m_socketInfoMap[event.connectionID.counter];
-    info.interfaceID = static_cast<InterfaceType>(event.interfaceID);
+    auto &info = m_connectionInfoMap[event.connectionID.counter];
+    info.interfaceID = event.interfaceID;
 }
 
 void AppPermissionsService::write(const WriteEvent &event) {
@@ -87,7 +90,7 @@ void AppPermissionsService::write(const WriteEvent &event) {
 
 void AppPermissionsService::process(const ReadEvent &event) {
     LogDebug("Read event for counter: " << event.connectionID.counter);
-    auto &info = m_socketInfoMap[event.connectionID.counter];
+    auto &info = m_connectionInfoMap[event.connectionID.counter];
     info.buffer.Push(event.rawBuffer);
 
     // We can get several requests in one package.
@@ -97,12 +100,12 @@ void AppPermissionsService::process(const ReadEvent &event) {
 
 void AppPermissionsService::close(const CloseEvent &event) {
     LogDebug("CloseEvent. ConnectionID: " << event.connectionID.sock);
-    m_socketInfoMap.erase(event.connectionID.counter);
+    m_connectionInfoMap.erase(event.connectionID.counter);
 }
 
 bool AppPermissionsService::processOne(const ConnectionID &conn,
                                        MessageBuffer &buffer,
-                                       InterfaceType interfaceID)
+                                       InterfaceID interfaceID)
 {
     LogDebug("Iteration begin");
 
@@ -114,10 +117,10 @@ bool AppPermissionsService::processOne(const ConnectionID &conn,
     LogDebug("Entering app_permissions server side handler");
 
     switch(interfaceID) {
-    case InterfaceType::CHANGE_APP_PERMISSIONS:
+    case CHANGE_APP_PERMISSIONS:
         return processPermissionsChange(conn, buffer);
 
-    case InterfaceType::CHECK_APP_PRIVILEGE:
+    case CHECK_APP_PRIVILEGE:
         return processCheckAppPrivilege(conn, buffer);
 
     default:
@@ -206,7 +209,7 @@ bool AppPermissionsService::processCheckAppPrivilege(const ConnectionID &conn, M
     int result = SECURITY_SERVER_API_ERROR_SERVER_ERROR;
     app_type_t app_type;
     bool has_permission = false;
-    PrivilegeCheckCall checkType = PrivilegeCheckCall::CHECK_GIVEN_APP;
+    PrivilegeCheckHdrs checkType = PrivilegeCheckHdrs::CHECK_GIVEN_APP;
 
     LogDebug("Processing app privilege check request");
 
@@ -214,11 +217,11 @@ bool AppPermissionsService::processCheckAppPrivilege(const ConnectionID &conn, M
     Try {
         int temp;
         Deserialization::Deserialize(buffer, temp); // call type
-        checkType = static_cast<PrivilegeCheckCall>(temp);
+        checkType = static_cast<PrivilegeCheckHdrs>(temp);
         LogDebug("App privilege check call type: "
-                 << (checkType == PrivilegeCheckCall::CHECK_GIVEN_APP ?
+                 << (checkType == PrivilegeCheckHdrs::CHECK_GIVEN_APP ?
                      "CHECK_GIVEN_APP":"CHECK_CALLER_APP"));
-        if (checkType == PrivilegeCheckCall::CHECK_GIVEN_APP) { //app_id present only in this case
+        if (checkType == PrivilegeCheckHdrs::CHECK_GIVEN_APP) { //app_id present only in this case
             Deserialization::Deserialize(buffer, app_id); //get app id
         }
         Deserialization::Deserialize(buffer, temp); //get app type
@@ -231,7 +234,7 @@ bool AppPermissionsService::processCheckAppPrivilege(const ConnectionID &conn, M
         return false;
     }
 
-    if (checkType == PrivilegeCheckCall::CHECK_CALLER_APP) { //get sender app_id in this case
+    if (checkType == PrivilegeCheckHdrs::CHECK_CALLER_APP) { //get sender app_id in this case
         char *label = NULL;
         if (smack_new_label_from_socket(conn.sock, &label) < 0) {
             LogDebug("Error in smack_new_label_from_socket(): "
index a22bfbc..c445198 100644 (file)
@@ -30,6 +30,7 @@
 #include <generic-socket-manager.h>
 #include <dpl/serialization.h>
 #include <message-buffer.h>
+#include <connection-info.h>
 #include <security-server-common.h>
 
 namespace SecurityServer {
@@ -39,18 +40,6 @@ class AppPermissionsService  :
   , public SecurityServer::ServiceThread<AppPermissionsService>
 {
 public:
-    enum class InterfaceType {
-        CHANGE_APP_PERMISSIONS,
-        CHECK_APP_PRIVILEGE,
-    };
-
-    struct SocketInfo {
-        InterfaceType interfaceID;
-        MessageBuffer buffer;
-    };
-
-    typedef std::map<int, SocketInfo> SocketInfoMap;
-
     ServiceDescriptionVector GetServiceDescription();
 
     DECLARE_THREAD_EVENT(AcceptEvent, accept)
@@ -64,12 +53,12 @@ public:
     void close(const CloseEvent &event);
 
 private:
-    bool processOne(const ConnectionID &conn, MessageBuffer &buffer, InterfaceType interfaceID);
+    bool processOne(const ConnectionID &conn, MessageBuffer &buffer, InterfaceID interfaceID);
 
     bool processPermissionsChange(const ConnectionID &conn, MessageBuffer &buffer);
     bool processCheckAppPrivilege(const ConnectionID &conn, MessageBuffer &buffer);
 
-    SocketInfoMap m_socketInfoMap;
+    ConnectionInfoMap m_connectionInfoMap;
 };
 
 } // namespace SecurityServer
index f6408d1..7659dbd 100644 (file)
@@ -53,7 +53,7 @@ void CookieService::accept(const AcceptEvent &event) {
     LogDebug("Accept event. ConnectionID.sock: " << event.connectionID.sock
         << " ConnectionID.counter: " << event.connectionID.counter
         << " ServiceID: " << event.interfaceID);
-    auto &info = m_socketInfoMap[event.connectionID.counter];
+    auto &info = m_connectionInfoMap[event.connectionID.counter];
     info.interfaceID = event.interfaceID;
 }
 
@@ -66,7 +66,7 @@ void CookieService::write(const WriteEvent &event) {
 
 void CookieService::process(const ReadEvent &event) {
     LogDebug("Read event for counter: " << event.connectionID.counter);
-    auto &info = m_socketInfoMap[event.connectionID.counter];
+    auto &info = m_connectionInfoMap[event.connectionID.counter];
     info.buffer.Push(event.rawBuffer);
 
     // We can get several requests in one package.
@@ -76,10 +76,10 @@ void CookieService::process(const ReadEvent &event) {
 
 void CookieService::close(const CloseEvent &event) {
     LogDebug("CloseEvent. ConnectionID: " << event.connectionID.sock);
-    m_socketInfoMap.erase(event.connectionID.counter);
+    m_connectionInfoMap.erase(event.connectionID.counter);
 }
 
-bool CookieService::processOne(const ConnectionID &conn, MessageBuffer &buffer, int interfaceID)
+bool CookieService::processOne(const ConnectionID &conn, MessageBuffer &buffer, InterfaceID interfaceID)
 {
     LogDebug("Iteration begin");
     MessageBuffer send, recv;
index 309d212..b899e2f 100644 (file)
@@ -29,6 +29,7 @@
 #include <generic-socket-manager.h>
 #include <dpl/serialization.h>
 #include <message-buffer.h>
+#include <connection-info.h>
 #include <security-server-common.h>
 #include <cookie-jar.h>
 
@@ -39,13 +40,6 @@ class CookieService  :
   , public SecurityServer::ServiceThread<CookieService>
 {
 public:
-    struct SocketInfo
-    {
-        int interfaceID;
-        MessageBuffer buffer;
-    };
-
-    typedef std::map<int, SocketInfo> SocketInfoMap;
 
     ServiceDescriptionVector GetServiceDescription();
 
@@ -60,7 +54,7 @@ public:
     void close(const CloseEvent &event);
 
 private:
-    bool processOne(const ConnectionID &conn, MessageBuffer &buffer, int interfaceID);
+    bool processOne(const ConnectionID &conn, MessageBuffer &buffer, InterfaceID interfaceID);
 
     bool cookieRequest(MessageBuffer &send, int socket);
 
@@ -74,7 +68,7 @@ private:
 
     CookieJar m_cookieJar;
 
-    SocketInfoMap m_socketInfoMap;
+    ConnectionInfoMap m_connectionInfoMap;
 };
 
 } // namespace SecurityServer
index 227744c..6b60459 100644 (file)
@@ -28,6 +28,7 @@
 #include <protocols.h>
 #include <open-for.h>
 #include <unistd.h>
+#include <algorithm>
 
 #include <security-server.h>
 #include <security-server-util.h>
@@ -53,6 +54,10 @@ const int SERVICE_SOCKET_ID = 0;
 
 namespace SecurityServer {
 
+OpenForService::OpenForConnInfo::~OpenForConnInfo() {
+    std::for_each(descriptorsVector.begin(),descriptorsVector.end(), ::close);
+}
+
 GenericSocketService::ServiceDescriptionVector OpenForService::GetServiceDescription() {
     return ServiceDescriptionVector
         {{SERVICE_SOCKET_OPEN_FOR, "security-server::api-open-for", SERVICE_SOCKET_ID, true}};
@@ -76,7 +81,7 @@ void OpenForService::write(const WriteEvent &event)
 void OpenForService::process(const ReadEvent &event)
 {
     LogDebug("Read event for counter: " << event.connectionID.counter);
-    auto &info = m_socketInfoMap[event.connectionID.counter];
+    auto &info = m_connectionInfoMap[event.connectionID.counter];
     info.buffer.Push(event.rawBuffer);
 
     // We can get several requests in one package.
@@ -87,12 +92,12 @@ void OpenForService::process(const ReadEvent &event)
 void OpenForService::close(const CloseEvent &event)
 {
     LogDebug("CloseEvent. ConnectionID: " << event.connectionID.sock);
-    auto &descVector = m_socketInfoMap[event.connectionID.counter].descriptorsVector;
+    auto &descVector = m_connectionInfoMap[event.connectionID.counter].descriptorsVector;
 
     for (auto iter = descVector.begin(); iter != descVector.end(); ++iter)
         TEMP_FAILURE_RETRY(::close(*iter));
 
-    m_socketInfoMap.erase(event.connectionID.counter);
+    m_connectionInfoMap.erase(event.connectionID.counter);
 }
 
 bool OpenForService::processOne(const ConnectionID &conn, MessageBuffer &buffer, std::vector<int> &descVector)
index e56b05f..6de08d5 100644 (file)
 
 namespace SecurityServer
 {
-    typedef std::vector<int> DescriptorVector;
-
-    struct SocketInfo {
-        DescriptorVector descriptorsVector;
-        MessageBuffer buffer;
-    };
-
-    typedef std::map<int, SocketInfo> SocketInfoMap;
-
     class OpenForService
       : public SecurityServer::GenericSocketService
       , public SecurityServer::ServiceThread<OpenForService>
@@ -62,10 +53,21 @@ namespace SecurityServer
         void close(const CloseEvent &event);
 
     private:
+        typedef std::vector<int> DescriptorVector;
+
+        struct OpenForConnInfo {
+            ~OpenForConnInfo();
+
+            DescriptorVector descriptorsVector;
+            MessageBuffer buffer;
+        };
+
+        typedef std::map<int, OpenForConnInfo> OpenForConnInfoMap;
+
         //internal service functions
         bool processOne(const ConnectionID &conn, MessageBuffer &buffer, DescriptorVector &descVector);
 
-        SocketInfoMap m_socketInfoMap;
+        OpenForConnInfoMap m_connectionInfoMap;
         SharedFile m_sharedFile;
     };
 } // namespace SecurityServer