lxcpp: provisioning implementation (part 2)
[platform/core/security/vasum.git] / server / host-ipc-connection.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Mateusz Malicki (m.malicki2@samsung.com)
22  * @brief   HostIPCConnection class
23  */
24
25 #include "config.hpp"
26
27 #include <functional>
28
29 #include "host-ipc-connection.hpp"
30 #include "host-ipc-definitions.hpp"
31 #include "exception.hpp"
32 #include "logger/logger.hpp"
33 #include "zones-manager.hpp"
34
35
36 namespace vasum {
37
38 HostIPCConnection::HostIPCConnection(ipc::epoll::EventPoll& eventPoll, ZonesManager* zonesManagerPtr)
39     : mZonesManagerPtr(zonesManagerPtr)
40 {
41     LOGT("Connecting to host IPC socket");
42
43     ipc::PeerCallback removedCallback = [this](const ipc::PeerID peerID,
44                                                const ipc::FileDescriptor) {
45         std::string id = api::IPC_CONNECTION_PREFIX + peerID;
46         mZonesManagerPtr->disconnectedCallback(id);
47     };
48     mService.reset(new ipc::Service(eventPoll, HOST_IPC_SOCKET,
49                                     nullptr, removedCallback));
50
51     using namespace std::placeholders;
52     setLockQueueCallback(std::bind(&ZonesManager::handleLockQueueCall,
53                                    mZonesManagerPtr, _1));
54
55     setUnlockQueueCallback(std::bind(&ZonesManager::handleUnlockQueueCall,
56                                      mZonesManagerPtr, _1));
57
58     setGetZoneIdsCallback(std::bind(&ZonesManager::handleGetZoneIdsCall,
59                                     mZonesManagerPtr, _1));
60
61     setGetActiveZoneIdCallback(std::bind(&ZonesManager::handleGetActiveZoneIdCall,
62                                          mZonesManagerPtr, _1));
63
64     setGetZoneInfoCallback(std::bind(&ZonesManager::handleGetZoneInfoCall,
65                                      mZonesManagerPtr, _1, _2));
66
67     setSetNetdevAttrsCallback(std::bind(&ZonesManager::handleSetNetdevAttrsCall,
68                                         mZonesManagerPtr, _1, _2));
69
70     setGetNetdevAttrsCallback(std::bind(&ZonesManager::handleGetNetdevAttrsCall,
71                                         mZonesManagerPtr, _1, _2));
72
73     setGetNetdevListCallback(std::bind(&ZonesManager::handleGetNetdevListCall,
74                                        mZonesManagerPtr, _1, _2));
75
76     setCreateNetdevVethCallback(std::bind(&ZonesManager::handleCreateNetdevVethCall,
77                                           mZonesManagerPtr, _1, _2));
78
79     setCreateNetdevMacvlanCallback(std::bind(&ZonesManager::handleCreateNetdevMacvlanCall,
80                                              mZonesManagerPtr, _1, _2));
81
82     setCreateNetdevPhysCallback(std::bind(&ZonesManager::handleCreateNetdevPhysCall,
83                                           mZonesManagerPtr, _1, _2));
84
85     setDestroyNetdevCallback(std::bind(&ZonesManager::handleDestroyNetdevCall,
86                                        mZonesManagerPtr, _1, _2));
87
88     setDeleteNetdevIpAddressCallback(std::bind(&ZonesManager::handleDeleteNetdevIpAddressCall,
89                                                mZonesManagerPtr, _1, _2));
90
91     setDeclareFileCallback(std::bind(&ZonesManager::handleDeclareFileCall,
92                                      mZonesManagerPtr, _1, _2));
93
94     setDeclareMountCallback(std::bind(&ZonesManager::handleDeclareMountCall,
95                                       mZonesManagerPtr, _1, _2));
96
97     setDeclareLinkCallback(std::bind(&ZonesManager::handleDeclareLinkCall,
98                                      mZonesManagerPtr, _1, _2));
99
100     setGetDeclarationsCallback(std::bind(&ZonesManager::handleGetDeclarationsCall,
101                                          mZonesManagerPtr, _1, _2));
102
103     setRemoveDeclarationCallback(std::bind(&ZonesManager::handleRemoveDeclarationCall,
104                                            mZonesManagerPtr, _1, _2));
105
106     setSetActiveZoneCallback(std::bind(&ZonesManager::handleSetActiveZoneCall,
107                                        mZonesManagerPtr, _1, _2));
108
109     setCreateZoneCallback(std::bind(&ZonesManager::handleCreateZoneCall,
110                                     mZonesManagerPtr, _1, _2));
111
112     setDestroyZoneCallback(std::bind(&ZonesManager::handleDestroyZoneCall,
113                                      mZonesManagerPtr, _1, _2));
114
115     setShutdownZoneCallback(std::bind(&ZonesManager::handleShutdownZoneCall,
116                                       mZonesManagerPtr, _1, _2));
117
118     setStartZoneCallback(std::bind(&ZonesManager::handleStartZoneCall,
119                                    mZonesManagerPtr, _1, _2));
120
121     setLockZoneCallback(std::bind(&ZonesManager::handleLockZoneCall,
122                                   mZonesManagerPtr, _1, _2));
123
124     setUnlockZoneCallback(std::bind(&ZonesManager::handleUnlockZoneCall,
125                                     mZonesManagerPtr, _1, _2));
126
127     setGrantDeviceCallback(std::bind(&ZonesManager::handleGrantDeviceCall,
128                                      mZonesManagerPtr, _1, _2));
129
130     setRevokeDeviceCallback(std::bind(&ZonesManager::handleRevokeDeviceCall,
131                                       mZonesManagerPtr, _1, _2));
132
133     setSwitchToDefaultCallback(std::bind(&ZonesManager::handleSwitchToDefaultCall,
134                                          mZonesManagerPtr, "", _1));
135
136     setCreateFileCallback(std::bind(&ZonesManager::handleCreateFileCall,
137                                     mZonesManagerPtr, _1, _2));
138
139     setCleanUpZonesRootCallback(std::bind(&ZonesManager::handleCleanUpZonesRootCall,
140                                           mZonesManagerPtr, _1));
141 }
142
143 HostIPCConnection::~HostIPCConnection()
144 {
145 }
146
147 void HostIPCConnection::start()
148 {
149     LOGT("Starting IPC");
150     mService->start();
151     LOGD("Connected");
152 }
153
154 void HostIPCConnection::stop(bool wait)
155 {
156     LOGT("Stopping IPC");
157     mService->stop(wait);
158 }
159
160 bool HostIPCConnection::isRunning()
161 {
162     return mService->isStarted();
163 }
164
165 void HostIPCConnection::setLockQueueCallback(const Method<api::Void>::type& callback)
166 {
167     typedef IPCMethodWrapper<api::Void> Callback;
168     mService->setMethodHandler<Callback::out, Callback::in>(
169         api::ipc::METHOD_LOCK_QUEUE,
170         Callback::getWrapper(callback));
171 }
172
173 void HostIPCConnection::setUnlockQueueCallback(const Method<api::Void>::type& callback)
174 {
175     typedef IPCMethodWrapper<api::Void> Callback;
176     mService->setMethodHandler<Callback::out, Callback::in>(
177         api::ipc::METHOD_UNLOCK_QUEUE,
178         Callback::getWrapper(callback));
179 }
180
181 void HostIPCConnection::setGetZoneIdsCallback(const Method<api::ZoneIds>::type& callback)
182 {
183     typedef IPCMethodWrapper<api::ZoneIds> Callback;
184     mService->setMethodHandler<Callback::out, Callback::in>(
185         api::ipc::METHOD_GET_ZONE_ID_LIST,
186         Callback::getWrapper(callback));
187 }
188
189 void HostIPCConnection::setGetActiveZoneIdCallback(const Method<api::ZoneId>::type& callback)
190 {
191     typedef IPCMethodWrapper<api::ZoneId> Callback;
192     mService->setMethodHandler<Callback::out, Callback::in>(
193         api::ipc::METHOD_GET_ACTIVE_ZONE_ID,
194         Callback::getWrapper(callback));
195 }
196
197 void HostIPCConnection::setGetZoneInfoCallback(const Method<const api::ZoneId, api::ZoneInfoOut>::type& callback)
198 {
199     typedef IPCMethodWrapper<const api::ZoneId, api::ZoneInfoOut> Callback;
200     mService->setMethodHandler<Callback::out, Callback::in>(
201         api::ipc::METHOD_GET_ZONE_INFO,
202         Callback::getWrapper(callback));
203 }
204
205 void HostIPCConnection::setSetNetdevAttrsCallback(const Method<const api::SetNetDevAttrsIn>::type& callback)
206 {
207     typedef IPCMethodWrapper<const api::SetNetDevAttrsIn> Callback;
208     mService->setMethodHandler<Callback::out, Callback::in>(
209         api::ipc::METHOD_SET_NETDEV_ATTRS,
210         Callback::getWrapper(callback));
211 }
212
213 void HostIPCConnection::setGetNetdevAttrsCallback(const Method<const api::GetNetDevAttrsIn, api::GetNetDevAttrs>::type& callback)
214 {
215     typedef IPCMethodWrapper<const api::GetNetDevAttrsIn, api::GetNetDevAttrs> Callback;
216     mService->setMethodHandler<Callback::out, Callback::in>(
217         api::ipc::METHOD_GET_NETDEV_ATTRS,
218         Callback::getWrapper(callback));
219 }
220
221 void HostIPCConnection::setGetNetdevListCallback(const Method<const api::ZoneId, api::NetDevList>::type& callback)
222 {
223     typedef IPCMethodWrapper<const api::ZoneId, api::NetDevList> Callback;
224     mService->setMethodHandler<Callback::out, Callback::in>(
225         api::ipc::METHOD_GET_NETDEV_LIST,
226         Callback::getWrapper(callback));
227 }
228
229 void HostIPCConnection::setCreateNetdevVethCallback(const Method<const api::CreateNetDevVethIn>::type& callback)
230 {
231     typedef IPCMethodWrapper<const api::CreateNetDevVethIn> Callback;
232     mService->setMethodHandler<Callback::out, Callback::in>(
233         api::ipc::METHOD_CREATE_NETDEV_VETH,
234         Callback::getWrapper(callback));
235 }
236
237 void HostIPCConnection::setCreateNetdevMacvlanCallback(const Method<const api::CreateNetDevMacvlanIn>::type& callback)
238 {
239     typedef IPCMethodWrapper<const api::CreateNetDevMacvlanIn> Callback;
240     mService->setMethodHandler<Callback::out, Callback::in>(
241         api::ipc::METHOD_CREATE_NETDEV_MACVLAN,
242         Callback::getWrapper(callback));
243 }
244
245 void HostIPCConnection::setCreateNetdevPhysCallback(const Method<const api::CreateNetDevPhysIn>::type& callback)
246 {
247     typedef IPCMethodWrapper<const api::CreateNetDevPhysIn> Callback;
248     mService->setMethodHandler<Callback::out, Callback::in>(
249         api::ipc::METHOD_CREATE_NETDEV_PHYS,
250         Callback::getWrapper(callback));
251 }
252
253 void HostIPCConnection::setDestroyNetdevCallback(const Method<const api::DestroyNetDevIn>::type& callback)
254 {
255     typedef IPCMethodWrapper<const api::DestroyNetDevIn> Callback;
256     mService->setMethodHandler<Callback::out, Callback::in>(
257         api::ipc::METHOD_DESTROY_NETDEV,
258         Callback::getWrapper(callback));
259 }
260
261 void HostIPCConnection::setDeleteNetdevIpAddressCallback(const Method<const api::DeleteNetdevIpAddressIn>::type& callback)
262 {
263     typedef IPCMethodWrapper<const api::DeleteNetdevIpAddressIn> Callback;
264     mService->setMethodHandler<Callback::out, Callback::in>(
265         api::ipc::METHOD_DELETE_NETDEV_IP_ADDRESS,
266         Callback::getWrapper(callback));
267 }
268
269 void HostIPCConnection::setDeclareFileCallback(const Method<const api::DeclareFileIn, api::Declaration>::type& callback)
270 {
271     typedef IPCMethodWrapper<const api::DeclareFileIn, api::Declaration> Callback;
272     mService->setMethodHandler<Callback::out, Callback::in>(
273         api::ipc::METHOD_DECLARE_FILE,
274         Callback::getWrapper(callback));
275 }
276
277 void HostIPCConnection::setDeclareMountCallback(const Method<const api::DeclareMountIn, api::Declaration>::type& callback)
278 {
279     typedef IPCMethodWrapper<const api::DeclareMountIn, api::Declaration> Callback;
280     mService->setMethodHandler<Callback::out, Callback::in>(
281         api::ipc::METHOD_DECLARE_MOUNT,
282         Callback::getWrapper(callback));
283 }
284
285 void HostIPCConnection::setDeclareLinkCallback(const Method<const api::DeclareLinkIn, api::Declaration>::type& callback)
286 {
287     typedef IPCMethodWrapper<const api::DeclareLinkIn, api::Declaration> Callback;
288     mService->setMethodHandler<Callback::out, Callback::in>(
289         api::ipc::METHOD_DECLARE_LINK,
290         Callback::getWrapper(callback));
291 }
292
293 void HostIPCConnection::setGetDeclarationsCallback(const Method<const api::ZoneId, api::Declarations>::type& callback)
294 {
295     typedef IPCMethodWrapper<const api::ZoneId, api::Declarations> Callback;
296     mService->setMethodHandler<Callback::out, Callback::in>(
297         api::ipc::METHOD_GET_DECLARATIONS,
298         Callback::getWrapper(callback));
299 }
300
301 void HostIPCConnection::setRemoveDeclarationCallback(const Method<const api::RemoveDeclarationIn>::type& callback)
302 {
303     typedef IPCMethodWrapper<const api::RemoveDeclarationIn> Callback;
304     mService->setMethodHandler<Callback::out, Callback::in>(
305         api::ipc::METHOD_REMOVE_DECLARATION,
306         Callback::getWrapper(callback));
307 }
308
309 void HostIPCConnection::setSetActiveZoneCallback(const Method<const api::ZoneId>::type& callback)
310 {
311     typedef IPCMethodWrapper<const api::ZoneId> Callback;
312     mService->setMethodHandler<Callback::out, Callback::in>(
313         api::ipc::METHOD_SET_ACTIVE_ZONE,
314         Callback::getWrapper(callback));
315 }
316
317 void HostIPCConnection::setCreateZoneCallback(const Method<const api::CreateZoneIn>::type& callback)
318 {
319     typedef IPCMethodWrapper<const api::CreateZoneIn> Callback;
320     mService->setMethodHandler<Callback::out, Callback::in>(
321         api::ipc::METHOD_CREATE_ZONE,
322         Callback::getWrapper(callback));
323 }
324
325 void HostIPCConnection::setDestroyZoneCallback(const Method<const api::ZoneId>::type& callback)
326 {
327     typedef IPCMethodWrapper<const api::ZoneId> Callback;
328     mService->setMethodHandler<Callback::out, Callback::in>(
329         api::ipc::METHOD_DESTROY_ZONE,
330         Callback::getWrapper(callback));
331 }
332
333 void HostIPCConnection::setShutdownZoneCallback(const Method<const api::ZoneId>::type& callback)
334 {
335     typedef IPCMethodWrapper<const api::ZoneId> Callback;
336     mService->setMethodHandler<Callback::out, Callback::in>(
337         api::ipc::METHOD_SHUTDOWN_ZONE,
338         Callback::getWrapper(callback));
339 }
340
341 void HostIPCConnection::setStartZoneCallback(const Method<const api::ZoneId>::type& callback)
342 {
343     typedef IPCMethodWrapper<const api::ZoneId> Callback;
344     mService->setMethodHandler<Callback::out, Callback::in>(
345         api::ipc::METHOD_START_ZONE,
346         Callback::getWrapper(callback));
347 }
348
349 void HostIPCConnection::setLockZoneCallback(const Method<const api::ZoneId>::type& callback)
350 {
351     typedef IPCMethodWrapper<const api::ZoneId> Callback;
352     mService->setMethodHandler<Callback::out, Callback::in>(
353         api::ipc::METHOD_LOCK_ZONE,
354         Callback::getWrapper(callback));
355 }
356
357 void HostIPCConnection::setUnlockZoneCallback(const Method<const api::ZoneId>::type& callback)
358 {
359     typedef IPCMethodWrapper<const api::ZoneId> Callback;
360     mService->setMethodHandler<Callback::out, Callback::in>(
361         api::ipc::METHOD_UNLOCK_ZONE,
362         Callback::getWrapper(callback));
363 }
364
365 void HostIPCConnection::setGrantDeviceCallback(const Method<const api::GrantDeviceIn>::type& callback)
366 {
367     typedef IPCMethodWrapper<const api::GrantDeviceIn> Callback;
368     mService->setMethodHandler<Callback::out, Callback::in>(
369         api::ipc::METHOD_GRANT_DEVICE,
370         Callback::getWrapper(callback));
371 }
372
373 void HostIPCConnection::setRevokeDeviceCallback(const Method<const api::RevokeDeviceIn>::type& callback)
374 {
375     typedef IPCMethodWrapper<const api::RevokeDeviceIn> Callback;
376     mService->setMethodHandler<Callback::out, Callback::in>(
377         api::ipc::METHOD_REVOKE_DEVICE,
378         Callback::getWrapper(callback));
379 }
380
381 void HostIPCConnection::setSwitchToDefaultCallback(const Method<api::Void>::type& callback)
382 {
383     typedef IPCMethodWrapper<api::Void> Callback;
384     mService->setMethodHandler<Callback::out, Callback::in>(
385         api::ipc::METHOD_SWITCH_TO_DEFAULT,
386         Callback::getWrapper(callback));
387 }
388
389 void HostIPCConnection::setCreateFileCallback(const Method<const api::CreateFileIn,
390                                               api::CreateFileOut>::type& callback)
391 {
392     typedef IPCMethodWrapper<const api::CreateFileIn, api::CreateFileOut> Method;
393     mService->setMethodHandler<Method::out, Method::in>(
394         api::ipc::METHOD_CREATE_FILE,
395         Method::getWrapper(callback));
396 }
397
398 void HostIPCConnection::setCleanUpZonesRootCallback(const Method<api::Void>::type& callback)
399 {
400     typedef IPCMethodWrapper<api::Void> Callback;
401     mService->setMethodHandler<Callback::out, Callback::in>(
402         api::ipc::METHOD_CLEAN_UP_ZONES_ROOT,
403         Callback::getWrapper(callback));
404 }
405
406 } // namespace vasum