Add IClient::getName() for getting the package id (or the executable path) of a client
[platform/core/context/context-service.git] / src / server / ServiceClient.cpp
index 757b787..fe8ced2 100644 (file)
  * limitations under the License.
  */
 
+#include <fstream>
+#include <aul.h>
 #include "Credential.h"
 #include "ServiceRunner.h"
 #include "ServiceClient.h"
 
+#define BUFFER_SIZE    256
+
 using namespace ctx;
 
 ServiceClient::ServiceClient(ServiceRunner* runner, const std::string& busName) :
@@ -39,16 +43,48 @@ void ServiceClient::setHandler(IMethodCallHandler* handler)
        __methodCallHandler = handler;
 }
 
-const std::string& ServiceClient::getBusName()
+const std::string& ServiceClient::getName()
 {
-       return __busName;
+       if (!__name.empty())
+               return __name;
+
+       char buffer[BUFFER_SIZE];
+
+       // Package ID
+       int err = aul_app_get_pkgid_bypid_for_uid(__credential->getPid(), buffer, BUFFER_SIZE, __credential->getUid());
+       if (IS_SUCCESS(err)) {
+               __name = buffer;
+               _I("PkgId: %s", __name.c_str());
+               return __name;
+       }
+
+       // Executable Path
+       char path[32];
+       g_snprintf(path, 32, "/proc/%d/cmdline", __credential->getPid());
+
+       std::ifstream cmdfile(path);
+       std::string cmdline;
+
+       if (std::getline(cmdfile, cmdline)) {
+               __name = cmdline;
+               _I("cmd: %s", __name.c_str());
+               return __name;
+       }
+
+       _E("Failed to get the client's name");
+       return __name;
 }
 
-const std::string& ServiceClient::getId()
+const std::string& ServiceClient::getSecurityLabel()
 {
        return __credential->getClientId();
 }
 
+const std::string& ServiceClient::getBusName()
+{
+       return __busName;
+}
+
 uid_t ServiceClient::getUid()
 {
        return __credential->getUid();