2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "anbox/container/management_api_skeleton.h"
19 #include "anbox/container/configuration.h"
20 #include "anbox/container/container.h"
21 #include "anbox/defer_action.h"
22 #include "anbox/logger.h"
23 #include "anbox/utils.h"
25 #include "anbox_container.pb.h"
26 #include "anbox_rpc.pb.h"
28 #ifdef USE_PROTOBUF_CALLBACK_HEADER
29 #include <google/protobuf/stubs/callback.h>
34 ManagementApiSkeleton::ManagementApiSkeleton(
35 const std::shared_ptr<rpc::PendingCallCache> &pending_calls,
36 const std::shared_ptr<Container> &container)
37 : pending_calls_(pending_calls), container_(container) {}
39 ManagementApiSkeleton::~ManagementApiSkeleton() {}
41 void ManagementApiSkeleton::start_container(
42 anbox::protobuf::container::StartContainer const *request,
43 anbox::protobuf::rpc::Void *response, google::protobuf::Closure *done) {
46 if (container_->state() == Container::State::running) {
47 response->set_error("Container is already running");
52 Configuration container_configuration;
54 const auto configuration = request->configuration();
55 for (int n = 0; n < configuration.bind_mounts_size(); n++) {
56 const auto bind_mount = configuration.bind_mounts(n);
57 container_configuration.bind_mounts.insert({bind_mount.source(), bind_mount.target()});
60 for (int n = 0; n < configuration.devices_size(); n++) {
61 const auto device = configuration.devices(n);
62 container_configuration.devices.insert({device.path(), {device.permission()}});
65 for (int n = 0; n < configuration.extra_properties_size(); n++) {
66 const auto prop = configuration.extra_properties(n);
67 container_configuration.extra_properties.push_back(prop);
71 container_->start(container_configuration);
72 } catch (std::exception &err) {
73 response->set_error(utils::string_format("Failed to start container: %s", err.what()));
79 void ManagementApiSkeleton::stop_container(
80 anbox::protobuf::container::StopContainer const *request,
81 anbox::protobuf::rpc::Void *response, google::protobuf::Closure *done) {
85 if (container_->state() != Container::State::running) {
86 response->set_error("Container is not running");
93 } catch (std::exception &err) {
94 response->set_error(utils::string_format("Failed to stop container: %s", err.what()));
99 } // namespace container