fe8ced21dd09a40f0220c3352cbcfa0cb8fd9d66
[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                 __name = cmdline;
70                 _I("cmd: %s", __name.c_str());
71                 return __name;
72         }
73
74         _E("Failed to get the client's name");
75         return __name;
76 }
77
78 const std::string& ServiceClient::getSecurityLabel()
79 {
80         return __credential->getClientId();
81 }
82
83 const std::string& ServiceClient::getBusName()
84 {
85         return __busName;
86 }
87
88 uid_t ServiceClient::getUid()
89 {
90         return __credential->getUid();
91 }
92
93 bool ServiceClient::isSystem()
94 {
95         return __credential->isSystem();
96 }
97
98 bool ServiceClient::isVerified()
99 {
100         IF_FAIL_RETURN(__getCredential(), false);
101         return __credential->valid();
102 }
103
104 bool ServiceClient::hasPrivilege(const char* privil)
105 {
106         return __credential->hasPrivilege(privil);
107 }
108
109 bool ServiceClient::hasPrivileges(const std::vector<std::string>& privil)
110 {
111         for (auto& item : privil) {
112                 if (!hasPrivilege(item.c_str()))
113                         return false;
114         }
115         return true;
116 }
117
118 void ServiceClient::publish(const std::string& signalName, GVariant* param)
119 {
120         __serviceRunner->publish(__busName, signalName, param);
121 }
122
123 IService* ServiceClient::getHostService()
124 {
125         return __serviceRunner->getService();
126 }
127
128 ServiceRunner* ServiceClient::getHostServiceRunner()
129 {
130         return __serviceRunner;
131 }
132
133 void ServiceClient::onMethodCalled(IMethodCall* methodCall)
134 {
135         __methodCallHandler->onMethodCalled(methodCall);
136 }
137
138 void ServiceClient::onDisconnected()
139 {
140         __methodCallHandler->onDisconnected();
141 }
142
143 bool ServiceClient::__getCredential()
144 {
145         if (__credential)
146                 return true;
147
148         __credential = new Credential(__serviceRunner->getConnection(), __busName);
149
150         return true;
151 }