2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "Credential.h"
20 #include "ServiceRunner.h"
21 #include "ServiceClient.h"
23 #define BUFFER_SIZE 256
27 ServiceClient::ServiceClient(ServiceRunner* runner, const std::string& busName) :
28 __serviceRunner(runner),
29 __methodCallHandler(NULL),
35 ServiceClient::~ServiceClient()
38 delete __methodCallHandler;
41 void ServiceClient::setHandler(IMethodCallHandler* handler)
43 __methodCallHandler = handler;
46 const std::string& ServiceClient::getName()
51 char buffer[BUFFER_SIZE];
54 int err = aul_app_get_pkgid_bypid_for_uid(__credential->getPid(), buffer, BUFFER_SIZE, __credential->getUid());
55 if (IS_SUCCESS(err)) {
57 _I("PkgId: %s", __name.c_str());
63 g_snprintf(path, 32, "/proc/%d/cmdline", __credential->getPid());
65 std::ifstream cmdfile(path);
68 if (std::getline(cmdfile, cmdline)) {
70 _I("cmd: %s", __name.c_str());
74 _E("Failed to get the client's name");
78 const std::string& ServiceClient::getSecurityLabel()
80 return __credential->getClientId();
83 const std::string& ServiceClient::getBusName()
88 uid_t ServiceClient::getUid()
90 return __credential->getUid();
93 bool ServiceClient::isSystem()
95 return __credential->isSystem();
98 bool ServiceClient::isVerified()
100 IF_FAIL_RETURN(__getCredential(), false);
101 return __credential->valid();
104 bool ServiceClient::hasPrivilege(const char* privil)
106 return __credential->hasPrivilege(privil);
109 bool ServiceClient::hasPrivileges(const std::vector<std::string>& privil)
111 for (auto& item : privil) {
112 if (!hasPrivilege(item.c_str()))
118 void ServiceClient::publish(const std::string& signalName, GVariant* param)
120 __serviceRunner->publish(__busName, signalName, param);
123 IService* ServiceClient::getHostService()
125 return __serviceRunner->getService();
128 ServiceRunner* ServiceClient::getHostServiceRunner()
130 return __serviceRunner;
133 void ServiceClient::onMethodCalled(IMethodCall* methodCall)
135 __methodCallHandler->onMethodCalled(methodCall);
138 void ServiceClient::onDisconnected()
140 __methodCallHandler->onDisconnected();
143 bool ServiceClient::__getCredential()
148 __credential = new Credential(__serviceRunner->getConnection(), __busName);