Some synchronizations
[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      * Try to shutdown the zone, if failed, destroy it.
102      */
103     void stop();
104
105     /**
106      * Activate this zone's VT
107      *
108      * @return Was activation successful?
109      */
110     bool activateVT();
111
112     /**
113      * Setup this zone to be put in the foreground.
114      * I.e. set appropriate scheduler level.
115      */
116     void goForeground();
117
118     /**
119      * Setup this zone to be put in the background.
120      * I.e. set appropriate scheduler level.
121      */
122     void goBackground();
123
124     /**
125      * Set if zone should be detached on exit.
126      *
127      * This sends detach flag to ZoneAdmin object and disables unmounting tmpfs
128      * in ZoneConnectionTransport.
129      */
130     void setDetachOnExit();
131
132     /**
133      * Set if zone should be destroyed on exit.
134      */
135     void setDestroyOnExit();
136
137     /**
138      * @return Is the zone running?
139      */
140     bool isRunning();
141
142     /**
143      * Check if the zone is stopped. It's NOT equivalent to !isRunning,
144      * because it checks different internal libvirt's states. There are other states,
145      * (e.g. paused) when the zone isn't running nor stopped.
146      *
147      * @return Is the zone stopped?
148      */
149     bool isStopped();
150
151     /**
152      * Suspend zone.
153      */
154     void suspend();
155
156     /**
157      * Resume zone.
158      */
159     void resume();
160
161     /**
162      * @return Is the zone in a paused state?
163      */
164     bool isPaused();
165
166     // ZoneConnection API
167
168     /**
169      * @return Is switching to default zone after timeout allowed?
170      */
171     bool isSwitchToDefaultAfterTimeoutAllowed() const;
172
173     /**
174      * Register notification request callback
175      */
176     void setNotifyActiveZoneCallback(const NotifyActiveZoneCallback& callback);
177
178     /**
179      * Register callback used when switching to default zone.
180      */
181     void setDisplayOffCallback(const DisplayOffCallback& callback);
182
183     /**
184      * Register proxy call callback
185      */
186     void setProxyCallCallback(const ProxyCallCallback& callback);
187
188     /**
189      * Send notification signal to this zone
190      *
191      * @param zone   name of zone in which the notification occurred
192      * @param application name of application that cause notification
193      * @param message     message to be send to zone
194      */
195     void sendNotification(const std::string& zone,
196                           const std::string& application,
197                           const std::string& message);
198
199     /**
200      * Register file move request callback
201      */
202     void setFileMoveRequestCallback(const FileMoveRequestCallback& callback);
203
204     /**
205      * Register dbus state changed callback
206      */
207     void setDbusStateChangedCallback(const DbusStateChangedCallback& callback);
208
209     /**
210      * Make a proxy call
211      */
212     void proxyCallAsync(const std::string& busName,
213                         const std::string& objectPath,
214                         const std::string& interface,
215                         const std::string& method,
216                         GVariant* parameters,
217                         const dbus::DbusConnection::AsyncMethodCallCallback& callback);
218
219     /**
220      * Get a dbus address
221      */
222     std::string getDbusAddress() const;
223
224     /**
225      * Get id of VT
226      */
227     int getVT() const;
228
229     /**
230      * Declare file, directory or pipe that will be created while zone startup
231      */
232     void declareFile(const int32_t& type,
233                      const std::string& path,
234                      const int32_t& flags,
235                      const int32_t& mode);
236     /**
237      * Declare mount that will be created while zone startup
238      */
239     void declareMount(const std::string& source,
240                       const std::string& target,
241                       const std::string& type,
242                       const int64_t& flags,
243                       const std::string& data);
244     /**
245      * Declare link that will be created while zone startup
246      */
247     void declareLink(const std::string& source,
248                      const std::string& target);
249
250    /**
251      * Get zone root path
252      */
253     std::string getRootPath() const;
254
255 private:
256     utils::Worker::Pointer mWorker;
257     ZoneConfig mConfig;
258     std::vector<boost::regex> mPermittedToSend;
259     std::vector<boost::regex> mPermittedToRecv;
260     std::unique_ptr<ZoneConnectionTransport> mConnectionTransport;
261     std::unique_ptr<ZoneAdmin> mAdmin;
262     std::unique_ptr<ZoneConnection> mConnection;
263     std::unique_ptr<ZoneProvision> mProvision;
264     mutable std::recursive_mutex mReconnectMutex;
265     NotifyActiveZoneCallback mNotifyCallback;
266     DisplayOffCallback mDisplayOffCallback;
267     FileMoveRequestCallback mFileMoveCallback;
268     ProxyCallCallback mProxyCallCallback;
269     DbusStateChangedCallback mDbusStateChangedCallback;
270     std::string mDbusAddress;
271     std::string mRunMountPoint;
272
273     void onNameLostCallback();
274     void reconnectHandler();
275     void connect();
276     void disconnect();
277 };
278
279
280 } // namespace vasum
281
282 #endif // SERVER_ZONE_HPP