4297c95a4a78ba38fa6e19ff00f1e46db6479c97
[platform/core/security/vasum.git] / client / vasum-client-impl.hpp
1 /*
2  *  Copyright (c) 2014 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 /**
21  * @file
22  * @author  Mateusz Malicki (m.malicki2@samsung.com)
23  * @brief   This file contains vasum-server's client definition
24  */
25
26 #ifndef VASUM_CLIENT_IMPL_HPP
27 #define VASUM_CLIENT_IMPL_HPP
28
29 #include "vasum-client.h"
30 #include "ipc/epoll/thread-dispatcher.hpp"
31 #include "ipc/epoll/event-poll.hpp"
32 #include "ipc/client.hpp"
33
34 #include <mutex>
35 #include <memory>
36 #include <functional>
37 #include <linux/if_link.h>
38
39 /**
40  * Zone's D-Bus state change callback function signature.
41  *
42  * @param[in] zoneId affected zone id
43  * @param[in] dbusAddress new D-Bus address
44  * @param data custom user's data pointer passed to vsm_add_state_callback() function
45  */
46 typedef std::function<void (const char *zoneId, const char *dbusAddress, void *data)> VsmZoneDbusStateFunction;
47
48 /**
49  * Zone information structure
50  */
51 typedef struct ZoneStructure {
52     char *id;
53     int terminal;
54     VsmZoneState state;
55     char *rootfs_path;
56 } *Zone;
57
58 /**
59  * Network device information structure
60  */
61 typedef struct NetdevStructure {
62     char *name;
63     VsmNetdevType type;
64 } *Netdev;
65
66 /**
67  * vasum's client definition.
68  *
69  * Client uses dbus API.
70  */
71 class Client {
72 public:
73     Client() noexcept;
74     ~Client() noexcept;
75
76     /**
77      * Connect client with system ipc address.
78      *
79      * @return status of this function call
80      */
81     VsmStatus connectSystem() noexcept;
82
83     /**
84      * Connect client.
85      *
86      * @param address ipc socket address
87      * @return status of this function call
88      */
89     VsmStatus connect(const std::string& address) noexcept;
90
91     /**
92      * Disconnect client
93      */
94     VsmStatus disconnect() noexcept;
95
96     /**
97      *  @see ::vsm_get_poll_fd
98      */
99     VsmStatus vsm_get_poll_fd(int* fd) noexcept;
100
101     /**
102      *  @see ::vsm_enter_eventloop
103      */
104     VsmStatus vsm_enter_eventloop(int flags, int timeout) noexcept;
105
106     /**
107      *  @see ::vsm_set_dispatcher_type
108      */
109     VsmStatus vsm_set_dispatcher_type(VsmDispacherType dispacher) noexcept;
110
111     /**
112      *  @see ::vsm_get_dispatcher_type
113      */
114     VsmStatus vsm_get_dispatcher_type(VsmDispacherType* dispacher) noexcept;
115
116     /**
117      *  @see ::vsm_get_status_message
118      */
119     const char* vsm_get_status_message() const noexcept;
120
121     /**
122      *  @see ::vsm_get_status
123      */
124     VsmStatus vsm_get_status() const noexcept;
125
126     /**
127      *  @see ::vsm_get_zone_dbuses
128      */
129     VsmStatus vsm_get_zone_dbuses(VsmArrayString* keys, VsmArrayString* values) noexcept;
130
131     /**
132      *  @see ::vsm_lock_queue
133      */
134     VsmStatus vsm_lock_queue() noexcept;
135
136     /**
137      *  @see ::vsm_unlock_queue
138      */
139     VsmStatus vsm_unlock_queue() noexcept;
140
141     /**
142      *  @see ::vsm_get_zone_ids
143      */
144     VsmStatus vsm_get_zone_ids(VsmArrayString* array) noexcept;
145
146     /**
147      *  @see ::vsm_get_active_zone_id
148      */
149     VsmStatus vsm_get_active_zone_id(VsmString* id) noexcept;
150
151     /**
152      *  @see ::vsm_lookup_zone_by_pid
153      */
154     VsmStatus vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept;
155
156     /**
157      * @see ::vsm_lookup_zone_by_id
158      */
159     VsmStatus vsm_lookup_zone_by_id(const char* id, Zone* zone) noexcept;
160
161     /**
162      * @see ::vsm_lookup_zone_by_terminal_id
163      */
164     VsmStatus vsm_lookup_zone_by_terminal_id(int terminal, VsmString* id) noexcept;
165
166     /**
167      *  @see ::vsm_set_active_zone
168      */
169     VsmStatus vsm_set_active_zone(const char* id) noexcept;
170
171     /**
172      *  @see ::vsm_create_zone
173      */
174     VsmStatus vsm_create_zone(const char* id, const char* tname) noexcept;
175
176     /**
177      *  @see ::vsm_destroy_zone
178      */
179     VsmStatus vsm_destroy_zone(const char* id) noexcept;
180
181     /**
182      *  @see ::vsm_shutdown_zone
183      */
184     VsmStatus vsm_shutdown_zone(const char* id) noexcept;
185
186     /**
187      *  @see ::vsm_start_zone
188      */
189     VsmStatus vsm_start_zone(const char* id) noexcept;
190
191     /**
192      *  @see ::vsm_lock_zone
193      */
194     VsmStatus vsm_lock_zone(const char* id) noexcept;
195
196     /**
197      *  @see ::vsm_unlock_zone
198      */
199     VsmStatus vsm_unlock_zone(const char* id) noexcept;
200
201     /**
202      *  @see ::vsm_add_state_callback
203      */
204     VsmStatus vsm_add_state_callback(VsmZoneDbusStateFunction zoneDbusStateCallback,
205                                      void* data,
206                                      VsmSubscriptionId* subscriptionId) noexcept;
207
208     /**
209      *  @see ::vsm_del_state_callback
210      */
211     VsmStatus vsm_del_state_callback(VsmSubscriptionId subscriptionId) noexcept;
212
213     /**
214      *  @see ::vsm_del_state_callback
215      */
216     VsmStatus vsm_grant_device(const char* id,
217                                const char* device,
218                                uint32_t flags) noexcept;
219
220     /**
221      *  @see ::vsm_revoke_device
222      */
223     VsmStatus vsm_revoke_device(const char* id, const char* device) noexcept;
224
225     /**
226      *  @see ::vsm_zone_get_netdevs
227      */
228     VsmStatus vsm_zone_get_netdevs(const char* zone, VsmArrayString* netdevIds) noexcept;
229
230     /**
231      *  @see ::vsm_netdev_get_ipv4_addr
232      */
233     VsmStatus vsm_netdev_get_ipv4_addr(const char* zone,
234                                        const char* netdevId,
235                                        struct in_addr *addr) noexcept;
236
237     /**
238      *  @see ::vsm_netdev_get_ipv6_addr
239      */
240     VsmStatus vsm_netdev_get_ipv6_addr(const char* zone,
241                                        const char* netdevId,
242                                        struct in6_addr *addr) noexcept;
243
244     /**
245      *  @see ::vsm_netdev_set_ipv4_addr
246      */
247     VsmStatus vsm_netdev_set_ipv4_addr(const char* zone,
248                                        const char* netdevId,
249                                        struct in_addr *addr,
250                                        int prefix) noexcept;
251
252     /**
253      *  @see ::vsm_netdev_set_ipv6_addr
254      */
255     VsmStatus vsm_netdev_set_ipv6_addr(const char* zone,
256                                        const char* netdevId,
257                                        struct in6_addr *addr,
258                                        int prefix) noexcept;
259
260     /**
261      *  @see ::vsm_netdev_del_ipv4_addr
262      */
263     VsmStatus vsm_netdev_del_ipv4_addr(const char* zone,
264                                       const char* netdevId,
265                                       struct in_addr* addr,
266                                       int prefix) noexcept;
267
268     /**
269      *  @see ::vsm_netdev_del_ipv6_addr
270      */
271     VsmStatus vsm_netdev_del_ipv6_addr(const char* zone,
272                                       const char* netdevId,
273                                       struct in6_addr* addr,
274                                       int prefix) noexcept;
275
276     /**
277      *  @see ::vsm_netdev_up
278      */
279     VsmStatus vsm_netdev_up(const char* zone, const char* netdevId) noexcept;
280
281     /**
282      *  @see ::vsm_netdev_down
283      */
284     VsmStatus vsm_netdev_down(const char* zone, const char* netdevId) noexcept;
285
286     /**
287      *  @see ::vsm_create_netdev_veth
288      */
289     VsmStatus vsm_create_netdev_veth(const char* zone,
290                                      const char* zoneDev,
291                                      const char* hostDev) noexcept;
292
293     /**
294      *  @see ::vsm_create_netdev_macvlan
295      */
296     VsmStatus vsm_create_netdev_macvlan(const char* zone,
297                                         const char* zoneDev,
298                                         const char* hostDev,
299                                         enum macvlan_mode mode) noexcept;
300
301     /**
302      *  @see ::vsm_create_netdev_phys
303      */
304     VsmStatus vsm_create_netdev_phys(const char* zone, const char* devId) noexcept;
305
306     /**
307      *  @see ::vsm_lookup_netdev_by_name
308      */
309     VsmStatus vsm_lookup_netdev_by_name(const char* zone,
310                                         const char* netdevId,
311                                         Netdev* netdev) noexcept;
312
313     /**
314      *  @see ::vsm_destroy_netdev
315      */
316     VsmStatus vsm_destroy_netdev(const char* zone, const char* devId) noexcept;
317
318     /**
319      *  @see ::vsm_declare_file
320      */
321     VsmStatus vsm_declare_file(const char* zone,
322                                VsmFileType type,
323                                const char* path,
324                                int32_t flags,
325                                mode_t mode,
326                                VsmString* id) noexcept;
327
328     /**
329      * @see ::vsm_declare_mount
330      */
331     VsmStatus vsm_declare_mount(const char* source,
332                                 const char* zone,
333                                 const char* target,
334                                 const char* type,
335                                 uint64_t flags,
336                                 const char* data,
337                                 VsmString* id) noexcept;
338     /**
339      * @see ::vsm_declare_link
340      */
341     VsmStatus vsm_declare_link(const char* source,
342                                const char* zone,
343                                const char* target,
344                                VsmString* id) noexcept;
345
346     /**
347      * @see ::vsm_list_declarations
348      */
349     VsmStatus vsm_list_declarations(const char* zone, VsmArrayString* declarations) noexcept;
350
351     /**
352      * @see ::vsm_remove_declaration
353      */
354     VsmStatus vsm_remove_declaration(const char* zone, VsmString declaration) noexcept;
355
356     /**
357      * @see ::vsm_clean_up_zones_root
358      */
359     VsmStatus vsm_clean_up_zones_root() noexcept;
360
361 private:
362     struct Status {
363         Status();
364         Status(VsmStatus status, const std::string& msg = "");
365         VsmStatus mVsmStatus;
366         std::string mMsg;
367     };
368     Status mStatus;
369
370     mutable std::mutex mStatusMutex;
371     std::unique_ptr<ipc::epoll::ThreadDispatcher> mInternalDispatcher;
372     std::unique_ptr<ipc::epoll::EventPoll> mEventPoll;
373     std::unique_ptr<ipc::Client> mClient;
374
375     bool isConnected() const;
376     bool isInternalDispatcherEnabled() const;
377     ipc::epoll::EventPoll& getEventPoll() const;
378     VsmStatus coverException(const std::function<void(void)>& worker) noexcept;
379     VsmStatus vsm_netdev_get_ip_addr(const char* zone,
380                                      const char* netdevId,
381                                      int type,
382                                      void* addr) noexcept;
383 };
384
385 #endif /* VASUM_CLIENT_IMPL_HPP */