2 * Copyright (C) 2018 Canonical Ltd.
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/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"
25 #include <boost/filesystem.hpp>
29 #include <linux/loop.h>
30 #include <sys/ioctl.h>
32 #include <system_error>
34 namespace fs = boost::filesystem;
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"};
44 bool BinderDeviceAllocator::is_supported() {
45 return fs::exists(binderfs_control_path);
48 std::unique_ptr<BinderDevice> BinderDeviceAllocator::new_device() {
49 static uint32_t next_id = 0;
51 const auto ctl_fd = ::open(binderfs_control_path, O_RDWR);
53 ERROR("Failed to access binder control: %s", std::strerror(errno));
57 DeferAction close_ctl_fd{[&]() { ::close(ctl_fd); }};
60 std::memset(&dev, 0, sizeof(binderfs_device));
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);
68 std::memcpy(dev.name, device_name.c_str(), device_name.length());
70 if (::ioctl(ctl_fd, BINDER_CTL_ADD, &dev) < 0) {
71 ERROR("Failed to allocate new binder node: %s", std::strerror(errno));
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);
81 return BinderDevice::create(path);