dd0daca1d5048214670afdb82fe38ff3fd5a6ff8
[platform/core/security/vasum.git] / server / zone.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Lukasz Pawelczyk <l.pawelczyk@partner.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  Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22  * @brief   Declaration of the class for managing one zone
23  */
24
25
26 #ifndef SERVER_ZONE_HPP
27 #define SERVER_ZONE_HPP
28
29 #include "zone-config.hpp"
30 #include "zone-admin.hpp"
31 #include "zone-connection.hpp"
32 #include "zone-connection-transport.hpp"
33 #include "zone-provision.hpp"
34 #include "utils/worker.hpp"
35
36 #include <string>
37 #include <memory>
38 #include <thread>
39 #include <boost/regex.hpp>
40
41
42 namespace vasum {
43
44
45 class Zone {
46
47 public:
48     /**
49      * Zone constructor
50      * @param zonesPath directory where zones are defined (lxc configs, rootfs etc)
51      * @param zoneConfigPath path for zones config
52      * @param lxcTemplatePrefix directory where templates are stored
53      * @param baseRunMountPointPath base directory for run mount point
54      */
55     Zone(const utils::Worker::Pointer& worker,
56          const std::string& zonesPath,
57          const std::string& zoneConfigPath,
58          const std::string& lxcTemplatePrefix,
59          const std::string& baseRunMountPointPath);
60     Zone(Zone&&) = default;
61     virtual ~Zone();
62
63     typedef ZoneConnection::NotifyActiveZoneCallback NotifyActiveZoneCallback;
64     typedef ZoneConnection::DisplayOffCallback DisplayOffCallback;
65     typedef ZoneConnection::FileMoveRequestCallback FileMoveRequestCallback;
66     typedef ZoneConnection::ProxyCallCallback ProxyCallCallback;
67
68     typedef std::function<void(const std::string& address)> DbusStateChangedCallback;
69     typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
70
71     /**
72      * Returns a vector of regexps defining files permitted to be
73      * send to other zones using file move functionality
74      */
75     const std::vector<boost::regex>& getPermittedToSend() const;
76
77     /**
78      * Returns a vector of regexps defining files permitted to be
79      * send to other zones using file move functionality
80      */
81     const std::vector<boost::regex>& getPermittedToRecv() const;
82
83     // ZoneAdmin API
84
85     /**
86      * Get the zone id
87      */
88     const std::string& getId() const;
89
90     /**
91      * Get the zone privilege
92      */
93     int getPrivilege() const;
94
95     /**
96      * Boot the zone to the background.
97      */
98     void start();
99
100     /**
101      * Boot the zone to the background in separate thread. This function immediately exits
102      * after zone booting is started in another thread.
103      *
104      * @param callback Called after starting the zone. Passes bool with result of starting.
105      */
106     void startAsync(const StartAsyncResultCallback& callback);
107
108     /**
109      * Try to shutdown the zone, if failed, destroy it.
110      */
111     void stop();
112
113     /**
114      * Activate this zone's VT
115      *
116      * @return Was activation successful?
117      */
118     bool activateVT();
119
120     /**
121      * Setup this zone to be put in the foreground.
122      * I.e. set appropriate scheduler level.
123      */
124     void goForeground();
125
126     /**
127      * Setup this zone to be put in the background.
128      * I.e. set appropriate scheduler level.
129      */
130     void goBackground();
131
132     /**
133      * Set if zone should be detached on exit.
134      *
135      * This sends detach flag to ZoneAdmin object and disables unmounting tmpfs
136      * in ZoneConnectionTransport.
137      */
138     void setDetachOnExit();
139
140     /**
141      * Set if zone should be destroyed on exit.
142      */
143     void setDestroyOnExit();
144
145     /**
146      * @return Is the zone running?
147      */
148     bool isRunning();
149
150     /**
151      * Check if the zone is stopped. It's NOT equivalent to !isRunning,
152      * because it checks different internal libvirt's states. There are other states,
153      * (e.g. paused) when the zone isn't running nor stopped.
154      *
155      * @return Is the zone stopped?
156      */
157     bool isStopped();
158
159     /**
160      * Suspend zone.
161      */
162     void suspend();
163
164     /**
165      * Resume zone.
166      */
167     void resume();
168
169     /**
170      * @return Is the zone in a paused state?
171      */
172     bool isPaused();
173
174     // ZoneConnection API
175
176     /**
177      * @return Is switching to default zone after timeout allowed?
178      */
179     bool isSwitchToDefaultAfterTimeoutAllowed() const;
180
181     /**
182      * Register notification request callback
183      */
184     void setNotifyActiveZoneCallback(const NotifyActiveZoneCallback& callback);
185
186     /**
187      * Register callback used when switching to default zone.
188      */
189     void setDisplayOffCallback(const DisplayOffCallback& callback);
190
191     /**
192      * Register proxy call callback
193      */
194     void setProxyCallCallback(const ProxyCallCallback& callback);
195
196     /**
197      * Send notification signal to this zone
198      *
199      * @param zone   name of zone in which the notification occurred
200      * @param application name of application that cause notification
201      * @param message     message to be send to zone
202      */
203     void sendNotification(const std::string& zone,
204                           const std::string& application,
205                           const std::string& message);
206
207     /**
208      * Register file move request callback
209      */
210     void setFileMoveRequestCallback(const FileMoveRequestCallback& callback);
211
212     /**
213      * Register dbus state changed callback
214      */
215     void setDbusStateChangedCallback(const DbusStateChangedCallback& callback);
216
217     /**
218      * Make a proxy call
219      */
220     void proxyCallAsync(const std::string& busName,
221                         const std::string& objectPath,
222                         const std::string& interface,
223                         const std::string& method,
224                         GVariant* parameters,
225                         const dbus::DbusConnection::AsyncMethodCallCallback& callback);
226
227     /**
228      * Get a dbus address
229      */
230     std::string getDbusAddress() const;
231
232     /**
233      * Get id of VT
234      */
235     int getVT() const;
236
237     /**
238      * Declare file, directory or pipe that will be created while zone startup
239      */
240     void declareFile(const int32_t& type,
241                      const std::string& path,
242                      const int32_t& flags,
243                      const int32_t& mode);
244     /**
245      * Declare mount that will be created while zone startup
246      */
247     void declareMount(const std::string& source,
248                       const std::string& target,
249                       const std::string& type,
250                       const int64_t& flags,
251                       const std::string& data);
252     /**
253      * Declare link that will be created while zone startup
254      */
255     void declareLink(const std::string& source,
256                      const std::string& target);
257
258    /**
259      * Get zone root path
260      */
261     std::string getRootPath() const;
262
263 private:
264     utils::Worker::Pointer mWorker;
265     ZoneConfig mConfig;
266     std::vector<boost::regex> mPermittedToSend;
267     std::vector<boost::regex> mPermittedToRecv;
268     std::unique_ptr<ZoneConnectionTransport> mConnectionTransport;
269     std::unique_ptr<ZoneAdmin> mAdmin;
270     std::unique_ptr<ZoneConnection> mConnection;
271     std::unique_ptr<ZoneProvision> mProvision;
272     mutable std::recursive_mutex mReconnectMutex;
273     NotifyActiveZoneCallback mNotifyCallback;
274     DisplayOffCallback mDisplayOffCallback;
275     FileMoveRequestCallback mFileMoveCallback;
276     ProxyCallCallback mProxyCallCallback;
277     DbusStateChangedCallback mDbusStateChangedCallback;
278     std::string mDbusAddress;
279     std::string mRunMountPoint;
280
281     void onNameLostCallback();
282     void reconnectHandler();
283     void connect();
284     void disconnect();
285 };
286
287
288 } // namespace vasum
289
290 #endif // SERVER_ZONE_HPP