Remove trailing bytes from the cmd string read from /proc/PID/cmdline
[platform/core/context/context-service.git] / src / server / ServiceClient.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <fstream>
18 #include <aul.h>
19 #include "Credential.h"
20 #include "ServiceRunner.h"
21 #include "ServiceClient.h"
22
23 #define BUFFER_SIZE     256
24
25 using namespace ctx;
26
27 ServiceClient::ServiceClient(ServiceRunner* runner, const std::string& busName) :
28         __serviceRunner(runner),
29         __methodCallHandler(NULL),
30         __busName(busName),
31         __credential(NULL)
32 {
33 }
34
35 ServiceClient::~ServiceClient()
36 {
37         delete __credential;
38         delete __methodCallHandler;
39 }
40
41 void ServiceClient::setHandler(IMethodCallHandler* handler)
42 {
43         __methodCallHandler = handler;
44 }
45
46 const std::string& ServiceClient::getName()
47 {
48         if (!__name.empty())
49                 return __name;
50
51         char buffer[BUFFER_SIZE];
52
53         // Package ID
54         int err = aul_app_get_pkgid_bypid_for_uid(__credential->getPid(), buffer, BUFFER_SIZE, __credential->getUid());
55         if (IS_SUCCESS(err)) {
56                 __name = buffer;
57                 _I("PkgId: %s", __name.c_str());
58                 return __name;
59         }
60
61         // Executable Path
62         char path[32];
63         g_snprintf(path, 32, "/proc/%d/cmdline", __credential->getPid());
64
65         std::ifstream cmdfile(path);
66         std::string cmdline;
67
68         if (std::getline(cmdfile, cmdline)) {
69                 g_snprintf(buffer, BUFFER_SIZE, "%s", cmdline.c_str());
70                 __name = buffer;
71                 _I("cmd: %s", __name.c_str());
72                 return __name;
73         }
74
75         _E("Failed to get the client's name");
76         return __name;
77 }
78
79 const std::string& ServiceClient::getSecurityLabel()
80 {
81         return __credential->getClientId();
82 }
83
84 const std::string& ServiceClient::getBusName()
85 {
86         return __busName;
87 }
88
89 uid_t ServiceClient::getUid()
90 {
91         return __credential->getUid();
92 }
93
94 bool ServiceClient::isSystem()
95 {
96         return __credential->isSystem();
97 }
98
99 bool ServiceClient::isVerified()
100 {
101         IF_FAIL_RETURN(__getCredential(), false);
102         return __credential->valid();
103 }
104
105 bool ServiceClient::hasPrivilege(const char* privil)
106 {
107         return __credential->hasPrivilege(privil);
108 }
109
110 bool ServiceClient::hasPrivileges(const std::vector<std::string>& privil)
111 {
112         for (auto& item : privil) {
113                 if (!hasPrivilege(item.c_str()))
114                         return false;
115         }
116         return true;
117 }
118
119 void ServiceClient::publish(const std::string& signalName, GVariant* param)
120 {
121         __serviceRunner->publish(__busName, signalName, param);
122 }
123
124 IService* ServiceClient::getHostService()
125 {
126         return __serviceRunner->getService();
127 }
128
129 ServiceRunner* ServiceClient::getHostServiceRunner()
130 {
131         return __serviceRunner;
132 }
133
134 void ServiceClient::onMethodCalled(IMethodCall* methodCall)
135 {
136         __methodCallHandler->onMethodCalled(methodCall);
137 }
138
139 void ServiceClient::onDisconnected()
140 {
141         __methodCallHandler->onDisconnected();
142 }
143
144 bool ServiceClient::__getCredential()
145 {
146         if (__credential)
147                 return true;
148
149         __credential = new Credential(__serviceRunner->getConnection(), __busName);
150
151         return true;
152 }