Add an error check in Ipc
[platform/framework/native/appfw.git] / src / server / io / FIo_IpcServer.cpp
index f6f9f83..a55ce49 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);
@@ -99,8 +98,7 @@ _IpcServer::_ClientInfo::~_ClientInfo(void)
 }
 
 _IpcServer::_IpcServer(void)
-       : __name("")
-       , __runOnCallerThread(false)
+       : __runOnCallerThread(false)
        , __pEventDispatcher(null)
        , __pListener(null)
        , __handlerThread(0)
@@ -254,6 +252,8 @@ _IpcServer::Construct(const String& name, const _IIpcServerEventListener& listen
        return E_SUCCESS;
 
 CATCH:
+       __name.Clear();
+
        if (pGIOChannel != null)
        {
                g_io_channel_unref(pGIOChannel);
@@ -277,8 +277,7 @@ struct HelloMessage
 {
        int pid;
        bool reverse;  // true if the connection is for reverse message
-       char pkgId[256];
-       char appExecutableName[256];
+       char appId[256];
 };
 
 gboolean
@@ -306,8 +305,7 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
        SysTryCatch(NID_IO, client != -1, , E_SYSTEM, "[E_SYSTEM] Accept failed.");
 
        read(client, &helloMessage, sizeof(helloMessage));
-       helloMessage.pkgId[255] = '\0';
-       helloMessage.appExecutableName[255] = '\0';
+       helloMessage.appId[255] = '\0';
 
        pChannel = g_io_channel_unix_new(client);
        SysTryCatch(NID_IO, pChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
@@ -326,8 +324,7 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
 
                pClientInfo->pIpcServer = pIpcServer;
                pClientInfo->clientId = helloMessage.pid;
-               pClientInfo->pkgId.Append((wchar_t*) helloMessage.pkgId);
-               pClientInfo->appExecutableName.Append((wchar_t*) helloMessage.appExecutableName);
+               pClientInfo->appId.Append((wchar_t*) helloMessage.appId);
                pClientInfo->pReverseChannel = null;
 
                pIpcServer->__clients[helloMessage.pid] = pClientInfo;
@@ -337,7 +334,7 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
                pIpcServer->__pCurrentClientInfo = null;
        }
 
-       if (helloMessage.reverse)
+       if (helloMessage.reverse != 0)
        {
                pClientInfo->pReverseChannel = pChannel;
        }
@@ -384,43 +381,6 @@ _IpcServer::GetClientId(void) const
        return -1;
 }
 
-int
-_IpcServer::GetClientProcessId(void) const
-{
-       if (__pCurrentClientInfo)
-       {
-               return __pCurrentClientInfo->clientId;
-       }
-
-       return -1;
-}
-
-String
-_IpcServer::GetClientAppId(void) const
-{
-       static String nullString;
-
-       if (__pCurrentClientInfo)
-       {
-               return __pCurrentClientInfo->pkgId;
-       }
-
-       return nullString;
-}
-
-String
-_IpcServer::GetClientAppExecutableName(void) const
-{
-       static String nullString;
-
-       if (__pCurrentClientInfo)
-       {
-               return __pCurrentClientInfo->appExecutableName;
-       }
-
-       return nullString;
-}
-
 PackageId
 _IpcServer::GetClientPackageId(void) const
 {
@@ -428,7 +388,10 @@ _IpcServer::GetClientPackageId(void) const
 
        if (__pCurrentClientInfo)
        {
-               return __pCurrentClientInfo->pkgId;
+               String pkgId;
+               __pCurrentClientInfo->appId.SubString(0, 10, pkgId);
+
+               return pkgId;
        }
 
        return nullString;
@@ -441,11 +404,7 @@ _IpcServer::GetClientApplicationId(void) const
 
        if (__pCurrentClientInfo)
        {
-               String appId(__pCurrentClientInfo->pkgId);
-               appId.Append(L'.');
-               appId.Append(__pCurrentClientInfo->appExecutableName);
-
-               return appId;
+               return __pCurrentClientInfo->appId;
        }
 
        return nullString;
@@ -462,7 +421,8 @@ _IpcServer::HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gp
 
        if (condition & G_IO_HUP)
        {
-               SysLog(NID_IO, "Connection closed");
+               SysLog(NID_IO, "G_IO_HUP, the connection is closed.");
+
                int clientId = pClientInfo->clientId;
 
                g_io_channel_shutdown(source, FALSE, &pGError);
@@ -745,6 +705,7 @@ _IpcServer::SendResponse(int client, IPC::Message* pMessage)
        char* pData = null;
        GError* pGError = null;
        _ClientInfo* pClientInfo = null;
+       int ret = 0;
 
        SysTryReturn(NID_IO, client >= 0 && pMessage != null, false, E_INVALID_ARG,
                                "[E_INVALID_ARG] pMessage(0x%x) is null or clinet(%d) < 0", pMessage,
@@ -762,7 +723,13 @@ _IpcServer::SendResponse(int client, IPC::Message* pMessage)
        while (remain > 0)
        {
                pGError = NULL;
-               g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
+               ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
+               if (ret != G_IO_STATUS_NORMAL)
+               {
+                       SysLog(NID_IO, "Failed to send a response: %d", ret);
+                       SysTryCatch(NID_IO, ret != G_IO_STATUS_ERROR, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
+               }
+
                SysTryCatch(NID_IO, pGError == null, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
 
                remain -= written;
@@ -789,6 +756,7 @@ _IpcServer::SendResponse(int client, const IPC::Message& message)
        char* pData = null;
        GError* pGError = null;
        _ClientInfo* pClientInfo = null;
+       int ret = 0;
 
        SysTryReturn(NID_IO, client >= 0, false, E_INVALID_ARG, "[E_INVALID_ARG] clinet(%d) < 0", client);
        SysTryCatch(NID_IO, !message.is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
@@ -804,7 +772,13 @@ _IpcServer::SendResponse(int client, const IPC::Message& message)
        while (remain > 0)
        {
                pGError = NULL;
-               g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
+               ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
+               if (ret != G_IO_STATUS_NORMAL)
+               {
+                       SysLog(NID_IO, "Failed to send a response: %d", ret);
+                       SysTryCatch(NID_IO, ret != G_IO_STATUS_ERROR, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
+               }
+
                SysTryCatch(NID_IO, pGError == null, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
 
                remain -= written;