2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22 * @brief Declaration of the class for managing one container
26 #ifndef SERVER_CONTAINER_HPP
27 #define SERVER_CONTAINER_HPP
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"
39 #include <boost/regex.hpp>
42 namespace security_containers {
48 Container(const std::string& containerConfigPath,
49 const std::string& baseRunMountPointPath = "");
50 Container(Container&&) = default;
53 typedef ContainerConnection::NotifyActiveContainerCallback NotifyActiveContainerCallback;
54 typedef ContainerConnection::DisplayOffCallback DisplayOffCallback;
55 typedef ContainerConnection::FileMoveRequestCallback FileMoveRequestCallback;
56 typedef ContainerConnection::ProxyCallCallback ProxyCallCallback;
58 typedef std::function<void(const std::string& address)> DbusStateChangedCallback;
59 typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
62 * Returns a vector of regexps defining files permitted to be
63 * send to other containers using file move functionality
65 const std::vector<boost::regex>& getPermittedToSend() const;
68 * Returns a vector of regexps defining files permitted to be
69 * send to other containers using file move functionality
71 const std::vector<boost::regex>& getPermittedToRecv() const;
76 * Get the container id
78 const std::string& getId() const;
81 * Get the container privilege
83 int getPrivilege() const;
86 * Boot the container to the background.
91 * Boot the container to the background in separate thread. This function immediately exits
92 * after container booting is started in another thread.
94 * @param callback Called after starting the container. Passes bool with result of starting.
96 void startAsync(const StartAsyncResultCallback& callback);
99 * Try to shutdown the container, if failed, destroy it.
104 * Setup this container to be put in the foreground.
105 * I.e. set appropriate scheduler level.
110 * Setup this container to be put in the background.
111 * I.e. set appropriate scheduler level.
116 * Set if container should be detached on exit.
118 * This sends detach flag to ContainerAdmin object and disables unmounting tmpfs
119 * in ContainerConnectionTransport.
121 void setDetachOnExit();
124 * @return Is the container running?
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.
133 * @return Is the container stopped?
138 * @return Is the container in a paused state?
142 // ContainerConnection API
145 * @return Is switching to default container after timeout allowed?
147 bool isSwitchToDefaultAfterTimeoutAllowed() const;
150 * Register notification request callback
152 void setNotifyActiveContainerCallback(const NotifyActiveContainerCallback& callback);
155 * Register callback used when switching to default container.
157 void setDisplayOffCallback(const DisplayOffCallback& callback);
160 * Register proxy call callback
162 void setProxyCallCallback(const ProxyCallCallback& callback);
165 * Send notification signal to this container
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
171 void sendNotification(const std::string& container,
172 const std::string& application,
173 const std::string& message);
176 * Register file move request callback
178 void setFileMoveRequestCallback(const FileMoveRequestCallback& callback);
181 * Register dbus state changed callback
183 void setDbusStateChangedCallback(const DbusStateChangedCallback& callback);
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);
198 std::string getDbusAddress();
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;
219 void onNameLostCallback();
220 void reconnectHandler();
229 #endif // SERVER_CONTAINER_HPP