Add API to create new containers
[platform/core/security/vasum.git] / server / container.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 container
23  */
24
25
26 #ifndef SERVER_CONTAINER_HPP
27 #define SERVER_CONTAINER_HPP
28
29 #include "container-config.hpp"
30 #include "container-admin.hpp"
31 #include "container-connection.hpp"
32 #include "container-connection-transport.hpp"
33 #include "network-admin.hpp"
34
35
36 #include <string>
37 #include <memory>
38 #include <thread>
39 #include <boost/regex.hpp>
40
41
42 namespace security_containers {
43
44
45 class Container {
46
47 public:
48     Container(const std::string& containerConfigPath,
49               const std::string& baseRunMountPointPath = "");
50     Container(Container&&) = default;
51     virtual ~Container();
52
53     typedef ContainerConnection::NotifyActiveContainerCallback NotifyActiveContainerCallback;
54     typedef ContainerConnection::DisplayOffCallback DisplayOffCallback;
55     typedef ContainerConnection::FileMoveRequestCallback FileMoveRequestCallback;
56     typedef ContainerConnection::ProxyCallCallback ProxyCallCallback;
57
58     typedef std::function<void(const std::string& address)> DbusStateChangedCallback;
59     typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
60
61     /**
62      * Returns a vector of regexps defining files permitted to be
63      * send to other containers using file move functionality
64      */
65     const std::vector<boost::regex>& getPermittedToSend() const;
66
67     /**
68      * Returns a vector of regexps defining files permitted to be
69      * send to other containers using file move functionality
70      */
71     const std::vector<boost::regex>& getPermittedToRecv() const;
72
73     // ContainerAdmin API
74
75     /**
76      * Get the container id
77      */
78     const std::string& getId() const;
79
80     /**
81      * Get the container privilege
82      */
83     int getPrivilege() const;
84
85     /**
86      * Boot the container to the background.
87      */
88     void start();
89
90     /**
91      * Boot the container to the background in separate thread. This function immediately exits
92      * after container booting is started in another thread.
93      *
94      * @param callback Called after starting the container. Passes bool with result of starting.
95      */
96     void startAsync(const StartAsyncResultCallback& callback);
97
98     /**
99      * Try to shutdown the container, if failed, destroy it.
100      */
101     void stop();
102
103     /**
104      * Setup this container to be put in the foreground.
105      * I.e. set appropriate scheduler level.
106      */
107     void goForeground();
108
109     /**
110      * Setup this container to be put in the background.
111      * I.e. set appropriate scheduler level.
112      */
113     void goBackground();
114
115     /**
116      * Set if container should be detached on exit.
117      *
118      * This sends detach flag to ContainerAdmin object and disables unmounting tmpfs
119      * in ContainerConnectionTransport.
120      */
121     void setDetachOnExit();
122
123     /**
124      * @return Is the container running?
125      */
126     bool isRunning();
127
128     /**
129      * Check if the container is stopped. It's NOT equivalent to !isRunning,
130      * because it checks different internal libvirt's states. There are other states,
131      * (e.g. paused) when the container isn't running nor stopped.
132      *
133      * @return Is the container stopped?
134      */
135     bool isStopped();
136
137     /**
138      * @return Is the container in a paused state?
139      */
140     bool isPaused();
141
142     // ContainerConnection API
143
144     /**
145      * @return Is switching to default container after timeout allowed?
146      */
147     bool isSwitchToDefaultAfterTimeoutAllowed() const;
148
149     /**
150      * Register notification request callback
151      */
152     void setNotifyActiveContainerCallback(const NotifyActiveContainerCallback& callback);
153
154     /**
155      * Register callback used when switching to default container.
156      */
157     void setDisplayOffCallback(const DisplayOffCallback& callback);
158
159     /**
160      * Register proxy call callback
161      */
162     void setProxyCallCallback(const ProxyCallCallback& callback);
163
164     /**
165      * Send notification signal to this container
166      *
167      * @param container   name of container in which the notification occurred
168      * @param application name of application that cause notification
169      * @param message     message to be send to container
170      */
171     void sendNotification(const std::string& container,
172                           const std::string& application,
173                           const std::string& message);
174
175     /**
176      * Register file move request callback
177      */
178     void setFileMoveRequestCallback(const FileMoveRequestCallback& callback);
179
180     /**
181      * Register dbus state changed callback
182      */
183     void setDbusStateChangedCallback(const DbusStateChangedCallback& callback);
184
185     /**
186      * Make a proxy call
187      */
188     void proxyCallAsync(const std::string& busName,
189                         const std::string& objectPath,
190                         const std::string& interface,
191                         const std::string& method,
192                         GVariant* parameters,
193                         const dbus::DbusConnection::AsyncMethodCallCallback& callback);
194
195     /**
196      * Get a dbus address
197      */
198     std::string getDbusAddress();
199
200 private:
201     ContainerConfig mConfig;
202     std::vector<boost::regex> mPermittedToSend;
203     std::vector<boost::regex> mPermittedToRecv;
204     std::unique_ptr<ContainerConnectionTransport> mConnectionTransport;
205     std::unique_ptr<NetworkAdmin> mNetworkAdmin;
206     std::unique_ptr<ContainerAdmin> mAdmin;
207     std::unique_ptr<ContainerConnection> mConnection;
208     std::thread mReconnectThread;
209     std::thread mStartThread;
210     mutable std::recursive_mutex mReconnectMutex;
211     NotifyActiveContainerCallback mNotifyCallback;
212     DisplayOffCallback mDisplayOffCallback;
213     FileMoveRequestCallback mFileMoveCallback;
214     ProxyCallCallback mProxyCallCallback;
215     DbusStateChangedCallback mDbusStateChangedCallback;
216     std::string mDbusAddress;
217     std::string mRunMountPoint;
218
219     void onNameLostCallback();
220     void reconnectHandler();
221     void connect();
222     void disconnect();
223 };
224
225
226 }
227
228
229 #endif // SERVER_CONTAINER_HPP