cb580de95e7c25c837642c9627c62f907c9928d0
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / common / binder_device_allocator.cpp
1 /*
2  * Copyright (C) 2018 Canonical Ltd.
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/common/binder_device_allocator.h"
19 #include "anbox/common/binder_device.h"
20 #include "anbox/common/binderfs.h"
21 #include "anbox/defer_action.h"
22 #include "anbox/logger.h"
23 #include "anbox/utils.h"
24
25 #include <boost/filesystem.hpp>
26
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <linux/loop.h>
30 #include <sys/ioctl.h>
31
32 #include <system_error>
33
34 namespace fs = boost::filesystem;
35
36 namespace {
37 const constexpr char* binderfs_base_path{BINDERFS_PATH};
38 const constexpr char* binderfs_control_path{BINDERFS_PATH "/binder-control"};
39 const constexpr char* default_binder_device_name{"binder"};
40 } // namespace
41
42 namespace anbox {
43 namespace common {
44 bool BinderDeviceAllocator::is_supported() {
45   return fs::exists(binderfs_control_path);
46 }
47
48 std::unique_ptr<BinderDevice> BinderDeviceAllocator::new_device() {
49   static uint32_t next_id = 0;
50
51   const auto ctl_fd = ::open(binderfs_control_path, O_RDWR);
52   if (ctl_fd < 0) {
53     ERROR("Failed to access binder control: %s", std::strerror(errno));
54     return nullptr;
55   }
56
57   DeferAction close_ctl_fd{[&]() { ::close(ctl_fd); }};
58
59   binderfs_device dev;
60   std::memset(&dev, 0, sizeof(binderfs_device));
61
62   const auto device_name = utils::string_format("%s%d", default_binder_device_name, next_id++);
63   if (device_name.length() > BINDERFS_MAX_NAME) {
64     ERROR("Invalid binder device name: %s", device_name);
65     return nullptr;
66   }
67
68   std::memcpy(dev.name, device_name.c_str(),  device_name.length());
69
70   if (::ioctl(ctl_fd, BINDER_CTL_ADD, &dev) < 0) {
71     ERROR("Failed to allocate new binder node: %s", std::strerror(errno));
72     return nullptr;
73   }
74
75   const auto path = utils::string_format("%s/%s", binderfs_base_path,  device_name);
76   if (!fs::exists(path)) {
77     ERROR("Allocated binder device %s is missing", path);
78     return nullptr;
79   }
80
81   return BinderDevice::create(path);
82 }
83 } // namespace common
84 } // namespace anbox