Add an E_RESOURCE_UNAVAILABLE in IPC
[platform/framework/native/appfw.git] / src / io / FIo_IpcClient.cpp
index 1198e9d..70a77be 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -135,8 +134,7 @@ struct HelloMessage
 {
        int pid;
        bool reverse;
-       char pkgId[256];
-       char appExecutableName[256];
+       char appId[256];
 };
 
 result
@@ -148,6 +146,7 @@ _IpcClient::MakeConnection(bool forReverse)
        socklen_t serverLen = 0;
        int client = -1;
        int ret = 0;
+       int retry = 0;
        HelloMessage helloMessage = {0, 0};
        std::string socketName;
        char* pSocketName = null;
@@ -177,30 +176,17 @@ _IpcClient::MakeConnection(bool forReverse)
 
        if (__fdCount == 0)
        {
-               // Set an pkgId
-               String pkgId = _AppInfo::GetPackageId();
-               int length = (pkgId.GetLength() + 1) * sizeof(wchar_t);
+               // Set an appId
+               String appId = _AppInfo::GetApplicationId();
+               int length = (appId.GetLength() + 1) * sizeof(wchar_t);
                if (length > 255)
                {
                        length = 255;
                }
 
-               SysTryReturnResult(NID_IO, pkgId.GetLength() > 0, E_SYSTEM, "AppId dose not exist.");
+               SysTryReturnResult(NID_IO, appId.GetLength() > 0, E_SYSTEM, "AppId dose not exist.");
 
-               memcpy(helloMessage.pkgId, pkgId.GetPointer(), length);
-
-               // Set an executableName
-               String appExecutableName = _AppInfo::GetAppExecutableName();
-               length = (appExecutableName.GetLength() + 1) * sizeof(wchar_t);
-               if (length > 255)
-               {
-                       length = 255;
-               }
-
-               if (appExecutableName.GetLength() != 0)
-               {
-                       memcpy(helloMessage.appExecutableName, appExecutableName.GetPointer(), length);
-               }
+               memcpy(helloMessage.appId, appId.GetPointer(), length);
        }
 
        client = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -216,8 +202,26 @@ _IpcClient::MakeConnection(bool forReverse)
        strncpy(server.sun_path, socketName.c_str(), socketNameLength);
        serverLen = sizeof(server);
 
-       ret = connect(client, (struct sockaddr*) &server, serverLen);
-       if (ret != 0)
+       // Retry if the server is not ready
+       retry = 5;
+       while (retry > 0)
+       {
+               ret = connect(client, (struct sockaddr*) &server, serverLen);
+               if (ret < 0 && errno == ENOENT)
+               {
+                       SysLog(NID_IO, "The server is not ready. %d", retry);
+
+                       usleep(1000 * 1000);
+
+                       --retry;
+               }
+               else
+               {
+                       break;
+               }
+       }
+
+       if (ret < 0)
        {
                SysTryCatch(NID_IO, errno == EINPROGRESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to connect to server(%s) : %s",
                                   socketName.c_str(), strerror(errno));
@@ -487,6 +491,20 @@ _IpcClient::SendAsync(IPC::Message* pMessage)
        while (remain > 0)
        {
                written = write(fd, (char*) pData, remain);
+               if (written < 0)
+               {
+                       ReleaseFd(fd);
+
+                       if (errno == EAGAIN)
+                       {
+                               SysLogException(NID_IO, E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
+                               return E_RESOURCE_UNAVAILABLE;
+                       }
+
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
+                       return E_SYSTEM;
+               }
+
                remain -= written;
                pData += written;
        }
@@ -513,8 +531,6 @@ _IpcClient::SendSync(IPC::Message* pMessage)
 
        std::string message;
 
-       struct pollfd pfd;
-
        IPC::Message* pReply = null;
        MessageReplyDeserializer* pReplyDeserializer = null;
        IPC::SyncMessage* pSyncMessage = dynamic_cast <IPC::SyncMessage*>(pMessage);
@@ -531,21 +547,53 @@ _IpcClient::SendSync(IPC::Message* pMessage)
        while (remain > 0)
        {
                written = write(fd, (char*) pData, remain);
+               if (written < 0)
+               {
+                       ReleaseFd(fd);
+
+                       if (errno == EAGAIN)
+                       {
+                               SysLogException(NID_IO, E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
+                               return E_RESOURCE_UNAVAILABLE;
+                       }
+
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
+                       return E_SYSTEM;
+               }
+
                remain -= written;
                pData += written;
        }
 
        // Wait reply
+       struct pollfd pfd;
+
        pfd.fd = fd;
        pfd.events = POLLIN | POLLRDHUP;
        pfd.revents = 0;
 
+       int ret = 0;
+
        while (true)
        {
-               poll(&pfd, 1, -1);
+               ret = poll(&pfd, 1, -1);
+               if (ret < 0)
+               {
+                       if (errno == EINTR)
+                       {
+                               continue;
+                       }
+
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to poll (%d, %s).", errno, strerror(errno));
+
+                       ReleaseFd(fd);
+                       return E_SYSTEM;
+               }
 
                if (pfd.revents & POLLRDHUP)
                {
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] POLLRDHUP");
+
                        ReleaseFd(fd);
                        return E_SYSTEM;
                }
@@ -555,13 +603,23 @@ _IpcClient::SendSync(IPC::Message* pMessage)
                        readSize = read(fd, buffer, 1024);
                }
 
-               message.append(buffer, readSize);
+               if (readSize > 0)
+               {
+                       message.append(buffer, readSize);
+               }
 
                pEndOfMessage = (char*) IPC::Message::FindNext(message.data(), message.data() + message.size());
                if (pEndOfMessage)
                {
                        pReply = new (std::nothrow) IPC::Message(message.data(), pEndOfMessage - message.data());
-                       SysTryReturnResult(NID_IO, pReply != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+                       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;
+                       }
+
                        break;
                }
        }