blob: 15ac305906b056f6b6850c83601c691610314b1e [file] [log] [blame]
Mark Salyzyna73ed222019-03-13 10:18:24 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "mount_handler.h"
18
19#include <ctype.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <string.h>
23#include <sys/epoll.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <algorithm>
Juhyung Park491004b2022-02-15 19:18:46 +090028#include <filesystem>
Mark Salyzyna73ed222019-03-13 10:18:24 -070029#include <string>
30#include <utility>
Mark Salyzyn793f4b52019-03-25 14:24:32 -070031#include <vector>
Mark Salyzyna73ed222019-03-13 10:18:24 -070032
33#include <android-base/file.h>
34#include <android-base/logging.h>
35#include <android-base/properties.h>
Juhyung Park491004b2022-02-15 19:18:46 +090036#include <android-base/stringprintf.h>
Mark Salyzyna73ed222019-03-13 10:18:24 -070037#include <android-base/strings.h>
38#include <fs_mgr.h>
39#include <fstab/fstab.h>
Mark Salyzyn793f4b52019-03-25 14:24:32 -070040#include <libdm/dm.h>
Mark Salyzyna73ed222019-03-13 10:18:24 -070041
42#include "epoll.h"
Mark Salyzyna73ed222019-03-13 10:18:24 -070043
Juhyung Park491004b2022-02-15 19:18:46 +090044using android::base::Basename;
45using android::base::StringPrintf;
46
Mark Salyzyna73ed222019-03-13 10:18:24 -070047namespace android {
48namespace init {
49
50namespace {
51
52MountHandlerEntry ParseMount(const std::string& line) {
53 auto fields = android::base::Split(line, " ");
54 while (fields.size() < 3) fields.emplace_back("");
55 if (fields[0] == "/dev/root") {
Mark Salyzyn793f4b52019-03-25 14:24:32 -070056 auto& dm = dm::DeviceMapper::Instance();
57 std::string path;
58 if (dm.GetDmDevicePathByName("system", &path) || dm.GetDmDevicePathByName("vroot", &path)) {
59 fields[0] = path;
60 } else if (android::fs_mgr::Fstab fstab; android::fs_mgr::ReadDefaultFstab(&fstab)) {
61 auto entry = GetEntryForMountPoint(&fstab, "/");
62 if (entry || (entry = GetEntryForMountPoint(&fstab, "/system"))) {
Mark Salyzyna73ed222019-03-13 10:18:24 -070063 fields[0] = entry->blk_device;
64 }
65 }
66 }
67 if (android::base::StartsWith(fields[0], "/dev/")) {
68 if (std::string link; android::base::Readlink(fields[0], &link)) {
69 fields[0] = link;
70 }
71 }
72 return MountHandlerEntry(fields[0], fields[1], fields[2]);
73}
74
Juhyung Park491004b2022-02-15 19:18:46 +090075// return dm-4 or dm-8 for dm-4, sda for sda25, or mmcblk0 for mmcblk0p24
76std::string GetRootDisk(std::string blockdev) {
77 if (blockdev.find('/') != std::string::npos) return {};
78
79 std::error_code ec;
80 for (const auto& entry : std::filesystem::directory_iterator("/sys/block", ec)) {
81 const std::string path = entry.path().string();
82 if (std::filesystem::exists(StringPrintf("%s/%s", path.c_str(), blockdev.c_str()))) {
83 return Basename(path);
84 }
85 }
86 if (android::base::StartsWith(blockdev, "dm-")) return blockdev;
87 return {};
88}
89
Mark Salyzyna73ed222019-03-13 10:18:24 -070090void SetMountProperty(const MountHandlerEntry& entry, bool add) {
91 static constexpr char devblock[] = "/dev/block/";
92 if (!android::base::StartsWith(entry.blk_device, devblock)) return;
Juhyung Park491004b2022-02-15 19:18:46 +090093 auto target = entry.blk_device.substr(strlen(devblock));
Mark Salyzyna73ed222019-03-13 10:18:24 -070094 std::string value;
95 if (add) {
Juhyung Park491004b2022-02-15 19:18:46 +090096 value = GetRootDisk(target);
97
Mark Salyzyna73ed222019-03-13 10:18:24 -070098 struct stat sb;
Mark Salyzyna73ed222019-03-13 10:18:24 -070099 if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700100 // Clear the noise associated with loopback and APEX.
Mark Salyzyna73ed222019-03-13 10:18:24 -0700101 if (android::base::StartsWith(value, "loop")) value = "";
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700102 if (android::base::StartsWith(entry.mount_point, "/apex/")) value = "";
Mark Salyzyna73ed222019-03-13 10:18:24 -0700103 }
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700104 auto mount_prop = entry.mount_point;
105 if (mount_prop == "/") mount_prop = "/root";
106 std::replace(mount_prop.begin(), mount_prop.end(), '/', '.');
Guo Weichaobcefbb12021-05-26 11:12:11 +0800107 auto blk_mount_prop = "dev.mnt.blk" + mount_prop;
108 auto dev_mount_prop = "dev.mnt.dev" + mount_prop;
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700109 // Set property even if its value does not change to trigger 'on property:'
110 // handling, except for clearing non-existent or already clear property.
111 // Goal is reduction of empty properties and associated triggers.
Guo Weichaobcefbb12021-05-26 11:12:11 +0800112 if (value.empty() && android::base::GetProperty(blk_mount_prop, "").empty()) return;
113 android::base::SetProperty(blk_mount_prop, value);
114 if (!value.empty()) {
Juhyung Park491004b2022-02-15 19:18:46 +0900115 android::base::SetProperty(dev_mount_prop, target);
Guo Weichaobcefbb12021-05-26 11:12:11 +0800116 } else {
117 android::base::SetProperty(dev_mount_prop, "");
118 }
Mark Salyzyna73ed222019-03-13 10:18:24 -0700119}
120
121} // namespace
122
123MountHandlerEntry::MountHandlerEntry(const std::string& blk_device, const std::string& mount_point,
124 const std::string& fs_type)
125 : blk_device(blk_device), mount_point(mount_point), fs_type(fs_type) {}
126
127bool MountHandlerEntry::operator<(const MountHandlerEntry& r) const {
128 if (blk_device < r.blk_device) return true;
129 if (blk_device > r.blk_device) return false;
130 if (mount_point < r.mount_point) return true;
131 if (mount_point > r.mount_point) return false;
132 return fs_type < r.fs_type;
133}
134
135MountHandler::MountHandler(Epoll* epoll) : epoll_(epoll), fp_(fopen("/proc/mounts", "re"), fclose) {
136 if (!fp_) PLOG(FATAL) << "Could not open /proc/mounts";
137 auto result = epoll->RegisterHandler(
138 fileno(fp_.get()), [this]() { this->MountHandlerFunction(); }, EPOLLERR | EPOLLPRI);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900139 if (!result.ok()) LOG(FATAL) << result.error();
Mark Salyzyna73ed222019-03-13 10:18:24 -0700140}
141
142MountHandler::~MountHandler() {
Tom Cherry9949ec52019-05-16 16:54:49 -0700143 if (fp_) epoll_->UnregisterHandler(fileno(fp_.get()));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700144}
145
146void MountHandler::MountHandlerFunction() {
147 rewind(fp_.get());
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700148 std::vector<MountHandlerEntry> touched;
149 auto untouched = mounts_;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700150 char* buf = nullptr;
151 size_t len = 0;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700152 while (getline(&buf, &len, fp_.get()) != -1) {
Tom Cherrye1bcd932020-09-10 17:37:56 +0000153 auto buf_string = std::string(buf);
154 if (buf_string.find("/emulated") != std::string::npos) {
155 continue;
156 }
157 auto entry = ParseMount(buf_string);
Mark Salyzyna73ed222019-03-13 10:18:24 -0700158 auto match = untouched.find(entry);
159 if (match == untouched.end()) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700160 touched.emplace_back(std::move(entry));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700161 } else {
162 untouched.erase(match);
163 }
164 }
165 free(buf);
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700166 for (auto& entry : untouched) {
Mark Salyzyna73ed222019-03-13 10:18:24 -0700167 SetMountProperty(entry, false);
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700168 mounts_.erase(entry);
169 }
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700170 for (auto& entry : touched) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700171 SetMountProperty(entry, true);
172 mounts_.emplace(std::move(entry));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700173 }
174}
175
176} // namespace init
177} // namespace android