TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / container / service.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/service.h"
19 #include "anbox/system_configuration.h"
20 #include "anbox/container/lxc_container.h"
21 #include "anbox/container/management_api_message_processor.h"
22 #include "anbox/container/management_api_skeleton.h"
23 #include "anbox/logger.h"
24 #include "anbox/network/delegate_connection_creator.h"
25 #include "anbox/network/delegate_message_processor.h"
26 #include "anbox/network/local_socket_messenger.h"
27 #include "anbox/qemu/null_message_processor.h"
28 #include "anbox/rpc/channel.h"
29 #include "anbox/rpc/pending_call_cache.h"
30
31 #include <boost/filesystem.hpp>
32
33 namespace fs = boost::filesystem;
34
35 namespace anbox {
36 namespace container {
37 std::shared_ptr<Service> Service::create(const std::shared_ptr<Runtime> &rt, const Configuration &config) {
38   auto sp = std::shared_ptr<Service>(new Service(rt, config));
39
40   auto wp = std::weak_ptr<Service>(sp);
41   auto delegate_connector = std::make_shared<network::DelegateConnectionCreator<boost::asio::local::stream_protocol>>(
42       [wp](std::shared_ptr<boost::asio::local::stream_protocol::socket> const &socket) {
43         if (auto service = wp.lock())
44           service->new_client(socket);
45   });
46
47   const auto container_socket_path = SystemConfiguration::instance().container_socket_path();
48   const auto socket_parent_path = fs::path(container_socket_path).parent_path();
49   if (!fs::exists(socket_parent_path))
50     fs::create_directories(socket_parent_path);
51
52   sp->connector_ = std::make_shared<network::PublishedSocketConnector>(container_socket_path, rt, delegate_connector);
53
54   // Make sure others can connect to our socket
55   ::chmod(container_socket_path.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
56
57   DEBUG("Everything setup. Waiting for incoming connections.");
58
59   return sp;
60 }
61
62 Service::Service(const std::shared_ptr<Runtime> &rt, const Configuration &config)
63     : dispatcher_(anbox::common::create_dispatcher_for_runtime(rt)),
64       next_connection_id_(0),
65       connections_(std::make_shared<network::Connections<network::SocketConnection>>()),
66       config_(config) {
67 }
68
69 Service::~Service() {
70   connections_->clear();
71 }
72
73 int Service::next_id() { return next_connection_id_++; }
74
75 void Service::new_client(std::shared_ptr<boost::asio::local::stream_protocol::socket> const
76         &socket) {
77   if (connections_->size() >= 1) {
78     socket->close();
79     return;
80   }
81
82   auto const messenger = std::make_shared<network::LocalSocketMessenger>(socket);
83
84   DEBUG("Got connection from pid %d", messenger->creds().pid());
85
86   auto pending_calls = std::make_shared<rpc::PendingCallCache>();
87   auto rpc_channel = std::make_shared<rpc::Channel>(pending_calls, messenger);
88   auto server = std::make_shared<container::ManagementApiSkeleton>(
89       pending_calls, std::make_shared<LxcContainer>(config_.privileged,
90                                                     config_.rootfs_overlay,
91                                                     config_.container_network_address,
92                                                     config_.container_network_gateway,
93                                                     config_.container_network_dns_servers,
94                                                     messenger->creds()));
95   auto processor = std::make_shared<container::ManagementApiMessageProcessor>(
96       messenger, pending_calls, server);
97
98   auto const &connection = std::make_shared<network::SocketConnection>(
99       messenger, messenger, next_id(), connections_, processor);
100   connection->set_name("container-service");
101
102   connections_->add(connection);
103   connection->read_next_message();
104 }
105 }  // namespace container
106 }  // namespace anbox