Change file path of domain socket
[platform/framework/native/appfw.git] / src / server / io / FIo_IpcServer.cpp
index 7f6a4bd..fa3483a 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);
@@ -34,9 +33,9 @@
 #include <sys/stat.h>
 #include <sys/un.h>
 
-#include <ipc/ipc_message.h>
+#include <aul/aul.h>
 
-#include <app_manager.h>
+#include <ipc/ipc_message.h>
 
 #include <FBaseRtMutex.h>
 #include <FBaseSysLog.h>
@@ -172,7 +171,7 @@ _IpcServer::Construct(const String& name, const _IIpcServerEventListener& listen
        pName = _StringConverter::CopyToCharArrayN(name);
        SysTryReturnResult(NID_IO, pName != null, E_OUT_OF_MEMORY, "Not enough memory");
 
-       socketName.append("/tmp/");
+       socketName.append("/var/run/osp/");
        socketName.append(pName);
 
        delete[] pName;
@@ -218,7 +217,7 @@ _IpcServer::Construct(const String& name, const _IIpcServerEventListener& listen
        SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to change permission of a socket(%d, %s): %s", serverSocket,
                           socketName.c_str(), strerror(errno));
 
-       ret = listen(serverSocket, 15);
+       ret = listen(serverSocket, 128);
        SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to listen the socket(%d, %s): %s", serverSocket,
                           socketName.c_str(), strerror(errno));
 
@@ -278,8 +277,7 @@ CATCH:
 
 struct HelloMessage
 {
-       int pid;
-       bool reverse;  // true if the connection is for reverse message
+       int reverse;  // if the connection is for reverse message
 };
 
 gboolean
@@ -292,21 +290,28 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
        _ChannelInfo* pChannelInfo = null;
        GSource* pGSource = null;
        GIOChannel* pChannel = null;
+       ssize_t readBytes = 0;
+       int ret = 0;
 
        int server = -1;
        int client = -1;
        struct sockaddr_un clientAddress;
        socklen_t clientLen = sizeof(clientAddress);
 
+       struct ucred cr;
+       socklen_t ucredLen = sizeof(cr);
+
        SysAssertf(pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
        SysAssertf(pIpcServer->__pListener != null, "Listener is null.\n");
 
        server = g_io_channel_unix_get_fd(source);
 
        client = accept(server, (struct sockaddr*) &clientAddress, &clientLen);
-       SysTryCatch(NID_IO, client != -1, ,E_SYSTEM, "[E_SYSTEM] Accept failed.");
+       SysTryCatch(NID_IO, client != -1, , E_SYSTEM, "[E_SYSTEM] Accept failed.");
 
-       read(client, &helloMessage, sizeof(helloMessage));
+       readBytes = read(client, &helloMessage, sizeof(helloMessage));
+       SysTryCatch(NID_IO, readBytes >= 0, , E_SYSTEM, "[E_SYSTEM] Failed to receive hello message (%d, %s).",
+                       errno, strerror(errno));
 
        pChannel = g_io_channel_unix_new(client);
        SysTryCatch(NID_IO, pChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
@@ -315,33 +320,36 @@ _IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoi
        g_io_channel_set_flags(pChannel, G_IO_FLAG_NONBLOCK, &pGError);
 
        g_io_channel_set_close_on_unref(pChannel, TRUE);
+
+       ret = getsockopt(client, SOL_SOCKET, SO_PEERCRED, &cr, &ucredLen);
+       SysTryCatch(NID_IO, ret >= 0, , E_SYSTEM, "[E_SYSTEM] Failed to get peercred information: %s", strerror(errno));
+
        client = -1;
 
-       pClientInfo = pIpcServer->__clients[helloMessage.pid];
+       pClientInfo = pIpcServer->__clients[cr.pid];
        if (pClientInfo == null) // first connection request from this client
        {
                pClientInfo = new (std::nothrow) _ClientInfo;
                SysTryCatch(NID_IO, pClientInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
 
                pClientInfo->pIpcServer = pIpcServer;
-               pClientInfo->clientId = helloMessage.pid;
+               pClientInfo->clientId = cr.pid;
 
-               char* pAppId = NULL;
-               int ret = app_manager_get_app_id(helloMessage.pid, &pAppId);
-               SysTryCatch(NID_IO, ret >= 0, ,E_SYSTEM, "[E_SYSTEM] Failed to get_app_id: %d", ret);
+               char buffer[256] = {0, };
+               ret = aul_app_get_appid_bypid(cr.pid, buffer, sizeof(buffer));
+               SysTryCatch(NID_IO, ret == AUL_R_OK, delete pClientInfo, E_SYSTEM, "[E_SYSTEM] Failed to get the application ID of pid: %d", ret);
 
-               pClientInfo->appId = pAppId;
-               free(pAppId);
+               pClientInfo->appId = buffer;
 
                pClientInfo->pReverseChannel = null;
 
-               pIpcServer->__clients[helloMessage.pid] = pClientInfo;
+               pIpcServer->__clients[cr.pid] = pClientInfo;
                pIpcServer->__pCurrentClientInfo = pClientInfo;
-               pIpcServer->__pListener->OnIpcClientConnected(*pIpcServer, helloMessage.pid);
+               pIpcServer->__pListener->OnIpcClientConnected(*pIpcServer, cr.pid);
                pIpcServer->__pCurrentClientInfo = null;
        }
 
-       if (helloMessage.reverse)
+       if (helloMessage.reverse != 0)
        {
                pClientInfo->pReverseChannel = pChannel;
        }
@@ -388,49 +396,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)
-       {
-               String pkgId;
-               __pCurrentClientInfo->appId.SubString(0, 10, pkgId);
-
-               return pkgId;
-       }
-
-       return nullString;
-}
-
-String
-_IpcServer::GetClientAppExecutableName(void) const
-{
-       static String nullString;
-
-       if (__pCurrentClientInfo)
-       {
-               String appName;
-               __pCurrentClientInfo->appId.SubString(11, appName);
-
-               return appName;
-       }
-
-       return nullString;
-}
-
 PackageId
 _IpcServer::GetClientPackageId(void) const
 {
@@ -743,6 +708,8 @@ _IpcServer::Send(IPC::Message* msg)
                // empty statement;
        }
 
+       delete msg;
+
        return true;
 }
 
@@ -755,6 +722,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,
@@ -772,8 +740,21 @@ _IpcServer::SendResponse(int client, IPC::Message* pMessage)
        while (remain > 0)
        {
                pGError = NULL;
-               g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
-               SysTryCatch(NID_IO, pGError == null, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
+               ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
+               if (ret != G_IO_STATUS_NORMAL)
+               {
+                       SysTryCatch(NID_IO, ret != G_IO_STATUS_AGAIN, , E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
+
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a response: %d", ret);
+
+                       if (pGError)
+                       {
+                               SysLog(NID_IO, "GError is %s", pGError->message);
+                       }
+
+                       delete pMessage;
+                       return E_SYSTEM;
+               }
 
                remain -= written;
                pData += written;
@@ -799,6 +780,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.");
@@ -814,8 +796,20 @@ _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);
-               SysTryCatch(NID_IO, pGError == null, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
+               ret = g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
+               if (ret != G_IO_STATUS_NORMAL)
+               {
+                       SysTryCatch(NID_IO, ret != G_IO_STATUS_AGAIN, , E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
+
+                       SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a response: %d", ret);
+
+                       if (pGError)
+                       {
+                               SysLog(NID_IO, "GError is %s", pGError->message);
+                       }
+
+                       return E_SYSTEM;
+               }
 
                remain -= written;
                pData += written;