93a43bbb727bec8a915b22f784c126dfe397db54
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / container / management_api_skeleton.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
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"
24
25 #include "anbox_container.pb.h"
26 #include "anbox_rpc.pb.h"
27
28 #ifdef USE_PROTOBUF_CALLBACK_HEADER
29 #include <google/protobuf/stubs/callback.h>
30 #endif
31
32 namespace anbox {
33 namespace container {
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) {}
38
39 ManagementApiSkeleton::~ManagementApiSkeleton() {}
40
41 void ManagementApiSkeleton::start_container(
42     anbox::protobuf::container::StartContainer const *request,
43     anbox::protobuf::rpc::Void *response, google::protobuf::Closure *done) {
44   DEBUG("");
45
46   if (container_->state() == Container::State::running) {
47     response->set_error("Container is already running");
48     done->Run();
49     return;
50   }
51
52   Configuration container_configuration;
53
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()});
58   }
59
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()}});
63   }
64
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);
68   }
69
70   try {
71     container_->start(container_configuration);
72   } catch (std::exception &err) {
73     response->set_error(utils::string_format("Failed to start container: %s", err.what()));
74   }
75
76   done->Run();
77 }
78
79 void ManagementApiSkeleton::stop_container(
80     anbox::protobuf::container::StopContainer const *request,
81     anbox::protobuf::rpc::Void *response, google::protobuf::Closure *done) {
82
83   (void)request;
84
85   if (container_->state() != Container::State::running) {
86     response->set_error("Container is not running");
87     done->Run();
88     return;
89   }
90
91   try {
92     container_->stop();
93   } catch (std::exception &err) {
94     response->set_error(utils::string_format("Failed to stop container: %s", err.what()));
95   }
96
97   done->Run();
98 }
99 }  // namespace container
100 }  // namespace anbox