Get the pid info from the socket fd
authorSunwook Bae <sunwook45.bae@samsung.com>
Mon, 19 Aug 2013 05:39:57 +0000 (14:39 +0900)
committerSunwook Bae <sunwook45.bae@samsung.com>
Mon, 19 Aug 2013 05:39:57 +0000 (14:39 +0900)
Change-Id: Ibe555b06cee84ac2a64e3054bb5b26fce51a5f74
Signed-off-by: Sunwook Bae <sunwook45.bae@samsung.com>
src/IpcServer.cpp

index 93074cf..2d13367 100644 (file)
@@ -289,7 +289,6 @@ CATCH:
 
 struct HelloMessage
 {
-       int pid;
        int reverse;  // if the connection is for reverse message
 };
 
@@ -304,12 +303,16 @@ IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoin
        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");
 
@@ -329,19 +332,23 @@ IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoin
        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);
+               ret = app_manager_get_app_id(cr.pid, &pAppId);
                SysTryCatch(NID_IO, ret >= 0, delete pClientInfo, E_SYSTEM, "[E_SYSTEM] Failed to get_app_id: %d", ret);
 
                pClientInfo->appId = pAppId;
@@ -349,9 +356,9 @@ IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpoin
 
                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;
        }