Modify the initialization module of IpcClient for AccessController
[platform/framework/native/appfw.git] / src / io / FIo_IpcClient.cpp
index 6e1061f..748c557 100644 (file)
@@ -37,8 +37,6 @@
 #include <map>
 
 #include <FBaseRtMutex.h>
-#include <FApp_AppInfo.h>
-#include <FAppPkg_PackageManagerImpl.h>
 #include <FBaseSysLog.h>
 #include <FBase_StringConverter.h>
 #include "FIo_IpcClient.h"
@@ -47,7 +45,6 @@
 using namespace IPC;
 using namespace std;
 using namespace Tizen::App;
-using namespace Tizen::App::Package;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Runtime;
 
@@ -56,7 +53,6 @@ namespace Tizen { namespace Io
 
 _IpcClient::_IpcClient(void)
        : __pReverseSource(null)
-       , __fdCount(0)
        , __pFdLock(null)
        , __pListener(null)
 {
@@ -132,9 +128,7 @@ _IpcClient::GetName(void) const
 
 struct HelloMessage
 {
-       int pid;
-       bool reverse;
-       char appId[256];
+       int reverse;
 };
 
 result
@@ -147,13 +141,12 @@ _IpcClient::MakeConnection(bool forReverse)
        int client = -1;
        int ret = 0;
        int retry = 0;
-       HelloMessage helloMessage = {0, 0};
+       HelloMessage helloMessage = {0};
        std::string socketName;
        char* pSocketName = null;
        size_t socketNameLength = 0;
        int flags = 0;
 
-       helloMessage.pid = getpid();
        if (forReverse)
        {
                helloMessage.reverse = 1;
@@ -174,21 +167,6 @@ _IpcClient::MakeConnection(bool forReverse)
        socketNameLength = socketName.size() + 1;
        SysTryReturnResult(NID_IO, socketNameLength < 108, E_INVALID_ARG, "Server name is too long.");
 
-       if (__fdCount == 0)
-       {
-               // Set an appId
-               String appId = _AppInfo::GetApplicationId();
-               int length = (appId.GetLength() + 1) * sizeof(wchar_t);
-               if (length > 255)
-               {
-                       length = 255;
-               }
-
-               SysTryReturnResult(NID_IO, appId.GetLength() > 0, E_SYSTEM, "AppId dose not exist.");
-
-               memcpy(helloMessage.appId, appId.GetPointer(), length);
-       }
-
        client = socket(AF_UNIX, SOCK_STREAM, 0);
        SysTryCatch(NID_IO, client != -1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to create a socket : %s.", strerror(errno));
 
@@ -276,10 +254,12 @@ _IpcClient::MakeConnection(bool forReverse)
        }
 
        ret = fcntl(client, F_SETFL, flags);
-       SysTryCatch(NID_IO, ret >= 0 , r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to set file status flags (%d, %s).",
+       SysTryCatch(NID_IO, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to set file status flags (%d, %s).",
                                           errno, strerror(errno));
 
-       write(client, &helloMessage, sizeof(helloMessage));
+       ret = write(client, &helloMessage, sizeof(helloMessage));
+       SysTryCatch(NID_IO, ret >= 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to send hello message (%d, %s).",
+                       errno, strerror(errno));
 
        if (forReverse)
        {
@@ -302,8 +282,6 @@ _IpcClient::MakeConnection(bool forReverse)
        }
        else
        {
-               ++__fdCount;
-
                ReleaseFd(client);
        }
 
@@ -493,9 +471,20 @@ _IpcClient::SendAsync(IPC::Message* pMessage)
                written = write(fd, (char*) pData, remain);
                if (written < 0)
                {
-                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
-
                        ReleaseFd(fd);
+
+                       if (errno == EAGAIN)
+                       {
+                               SysLogException(NID_IO, E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
+                               return E_RESOURCE_UNAVAILABLE;
+                       }
+                       else if (errno == EPIPE)
+                       {
+                               SysLogException(NID_IO, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] The socket connection is closed.");
+                               return E_INVALID_CONNECTION;
+                       }
+
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
                        return E_SYSTEM;
                }
 
@@ -515,6 +504,7 @@ _IpcClient::SendSync(IPC::Message* pMessage)
 
        int messageId = 0;
        int fd = -1;
+       int ret = 0;
 
        char* pData = null;
        int remain = 0;
@@ -526,14 +516,20 @@ _IpcClient::SendSync(IPC::Message* pMessage)
        std::string message;
 
        IPC::Message* pReply = null;
-       MessageReplyDeserializer* pReplyDeserializer = null;
        IPC::SyncMessage* pSyncMessage = dynamic_cast <IPC::SyncMessage*>(pMessage);
        SysTryReturnResult(NID_IO, pSyncMessage != null, E_INVALID_ARG, "pMessage is not a sync message.");
 
+       MessageReplyDeserializer* pReplyDeserializer = pSyncMessage->GetReplyDeserializer();
        messageId = SyncMessage::GetMessageId(*pSyncMessage);
 
        fd = AcquireFd();
-       SysTryReturnResult(NID_IO, fd != -1, E_SYSTEM, "Failed to get fd.");
+       if (fd < 0)
+       {
+               SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to get an fd");
+
+               delete pReplyDeserializer;
+               return E_SYSTEM;
+       }
 
        pData = (char*) pSyncMessage->data();
        remain = pSyncMessage->size();
@@ -543,10 +539,14 @@ _IpcClient::SendSync(IPC::Message* pMessage)
                written = write(fd, (char*) pData, remain);
                if (written < 0)
                {
+                       SysTryCatch(NID_IO, errno != EAGAIN, r = E_RESOURCE_UNAVAILABLE, E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
+
+                       SysTryCatch(NID_IO, errno != EPIPE, r = E_INVALID_CONNECTION, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] The socket connection is closed.");
+
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
 
-                       ReleaseFd(fd);
-                       return E_SYSTEM;
+                       r = E_SYSTEM;
+                       goto CATCH;
                }
 
                remain -= written;
@@ -560,8 +560,6 @@ _IpcClient::SendSync(IPC::Message* pMessage)
        pfd.events = POLLIN | POLLRDHUP;
        pfd.revents = 0;
 
-       int ret = 0;
-
        while (true)
        {
                ret = poll(&pfd, 1, -1);
@@ -574,16 +572,16 @@ _IpcClient::SendSync(IPC::Message* pMessage)
 
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to poll (%d, %s).", errno, strerror(errno));
 
-                       ReleaseFd(fd);
-                       return E_SYSTEM;
+                       r = E_SYSTEM;
+                       goto CATCH;
                }
 
                if (pfd.revents & POLLRDHUP)
                {
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] POLLRDHUP");
 
-                       ReleaseFd(fd);
-                       return E_SYSTEM;
+                       r = E_SYSTEM;
+                       goto CATCH;
                }
 
                if (pfd.revents & POLLIN)
@@ -600,22 +598,16 @@ _IpcClient::SendSync(IPC::Message* pMessage)
                if (pEndOfMessage)
                {
                        pReply = new (std::nothrow) IPC::Message(message.data(), pEndOfMessage - message.data());
-                       if (pReply == null)
-                       {
-                               SysLogException(NID_IO, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
-
-                               ReleaseFd(fd);
-                               return E_OUT_OF_MEMORY;
-                       }
+                       SysTryCatch(NID_IO, pReply != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
                        break;
                }
        }
 
-       pReplyDeserializer = pSyncMessage->GetReplyDeserializer();
        pReplyDeserializer->SerializeOutputParameters(*pReply);
-
        delete pReply;
+
+CATCH:
        delete pReplyDeserializer;
 
        ReleaseFd(fd);