blob: 227ce2fe4b88734a53e696697533008f9d9342be [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
Jaegeuk Kim120f6b22022-03-04 15:06:02 -080075// return sda25 for dm-4, sda25 for sda25, or mmcblk0p24 for mmcblk0p24
76std::string GetDiskPart(std::string blockdev) {
77 if (blockdev.find('/') != std::string::npos) return {};
78
79 while (android::base::StartsWith(blockdev, "dm-")) {
80 auto& dm = dm::DeviceMapper::Instance();
81 std::optional<std::string> parent = dm.GetParentBlockDeviceByPath("/dev/block/" + blockdev);
82 if (parent) {
83 blockdev = android::base::Basename(*parent);
84 } else {
85 return {};
86 }
87 }
88 return blockdev;
89}
90
91// return sda for sda25, or mmcblk0 for mmcblk0p24
Juhyung Park491004b2022-02-15 19:18:46 +090092std::string GetRootDisk(std::string blockdev) {
Jaegeuk Kim120f6b22022-03-04 15:06:02 -080093 if (blockdev.empty()) return {};
Juhyung Park491004b2022-02-15 19:18:46 +090094 if (blockdev.find('/') != std::string::npos) return {};
95
96 std::error_code ec;
97 for (const auto& entry : std::filesystem::directory_iterator("/sys/block", ec)) {
98 const std::string path = entry.path().string();
99 if (std::filesystem::exists(StringPrintf("%s/%s", path.c_str(), blockdev.c_str()))) {
100 return Basename(path);
101 }
102 }
Juhyung Park491004b2022-02-15 19:18:46 +0900103 return {};
104}
105
Mark Salyzyna73ed222019-03-13 10:18:24 -0700106void SetMountProperty(const MountHandlerEntry& entry, bool add) {
107 static constexpr char devblock[] = "/dev/block/";
108 if (!android::base::StartsWith(entry.blk_device, devblock)) return;
Juhyung Park491004b2022-02-15 19:18:46 +0900109 auto target = entry.blk_device.substr(strlen(devblock));
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800110 std::string diskpart, rootdisk;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700111 if (add) {
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800112 diskpart = GetDiskPart(target);
113 rootdisk = GetRootDisk(diskpart);
Juhyung Park491004b2022-02-15 19:18:46 +0900114
Mark Salyzyna73ed222019-03-13 10:18:24 -0700115 struct stat sb;
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800116 if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) rootdisk = "";
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700117 // Clear the noise associated with loopback and APEX.
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800118 if (android::base::StartsWith(target, "loop")) rootdisk = "";
119 if (android::base::StartsWith(entry.mount_point, "/apex/")) rootdisk = "";
Mark Salyzyna73ed222019-03-13 10:18:24 -0700120 }
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700121 auto mount_prop = entry.mount_point;
122 if (mount_prop == "/") mount_prop = "/root";
123 std::replace(mount_prop.begin(), mount_prop.end(), '/', '.');
Guo Weichaobcefbb12021-05-26 11:12:11 +0800124 auto blk_mount_prop = "dev.mnt.blk" + mount_prop;
125 auto dev_mount_prop = "dev.mnt.dev" + mount_prop;
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800126 auto rootdisk_mount_prop = "dev.mnt.rootdisk" + mount_prop;
127 // Set property even if its rootdisk does not change to trigger 'on property:'
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700128 // handling, except for clearing non-existent or already clear property.
129 // Goal is reduction of empty properties and associated triggers.
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800130 if (rootdisk.empty() && android::base::GetProperty(blk_mount_prop, "").empty()) return;
131
132 if (rootdisk.empty()) {
133 android::base::SetProperty(blk_mount_prop, "");
Guo Weichaobcefbb12021-05-26 11:12:11 +0800134 android::base::SetProperty(dev_mount_prop, "");
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800135 android::base::SetProperty(rootdisk_mount_prop, "");
136 return;
Guo Weichaobcefbb12021-05-26 11:12:11 +0800137 }
Jaegeuk Kim120f6b22022-03-04 15:06:02 -0800138
139 // 1. dm-N
140 // dev.mnt.dev.data = dm-N
141 // dev.mnt.blk.data = sdaN or mmcblk0pN
142 // dev.mnt.rootdisk.data = sda or mmcblk0
143 //
144 // 2. sdaN or mmcblk0pN
145 // dev.mnt.dev.data = sdaN or mmcblk0pN
146 // dev.mnt.blk.data = sdaN or mmcblk0pN
147 // dev.mnt.rootdisk.data = sda or mmcblk0
148 android::base::SetProperty(dev_mount_prop, target);
149 android::base::SetProperty(blk_mount_prop, diskpart);
150 android::base::SetProperty(rootdisk_mount_prop, rootdisk);
Mark Salyzyna73ed222019-03-13 10:18:24 -0700151}
152
153} // namespace
154
155MountHandlerEntry::MountHandlerEntry(const std::string& blk_device, const std::string& mount_point,
156 const std::string& fs_type)
157 : blk_device(blk_device), mount_point(mount_point), fs_type(fs_type) {}
158
159bool MountHandlerEntry::operator<(const MountHandlerEntry& r) const {
160 if (blk_device < r.blk_device) return true;
161 if (blk_device > r.blk_device) return false;
162 if (mount_point < r.mount_point) return true;
163 if (mount_point > r.mount_point) return false;
164 return fs_type < r.fs_type;
165}
166
167MountHandler::MountHandler(Epoll* epoll) : epoll_(epoll), fp_(fopen("/proc/mounts", "re"), fclose) {
168 if (!fp_) PLOG(FATAL) << "Could not open /proc/mounts";
169 auto result = epoll->RegisterHandler(
170 fileno(fp_.get()), [this]() { this->MountHandlerFunction(); }, EPOLLERR | EPOLLPRI);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900171 if (!result.ok()) LOG(FATAL) << result.error();
Mark Salyzyna73ed222019-03-13 10:18:24 -0700172}
173
174MountHandler::~MountHandler() {
Tom Cherry9949ec52019-05-16 16:54:49 -0700175 if (fp_) epoll_->UnregisterHandler(fileno(fp_.get()));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700176}
177
178void MountHandler::MountHandlerFunction() {
179 rewind(fp_.get());
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700180 std::vector<MountHandlerEntry> touched;
181 auto untouched = mounts_;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700182 char* buf = nullptr;
183 size_t len = 0;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700184 while (getline(&buf, &len, fp_.get()) != -1) {
Tom Cherrye1bcd932020-09-10 17:37:56 +0000185 auto buf_string = std::string(buf);
186 if (buf_string.find("/emulated") != std::string::npos) {
187 continue;
188 }
189 auto entry = ParseMount(buf_string);
Mark Salyzyna73ed222019-03-13 10:18:24 -0700190 auto match = untouched.find(entry);
191 if (match == untouched.end()) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700192 touched.emplace_back(std::move(entry));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700193 } else {
194 untouched.erase(match);
195 }
196 }
197 free(buf);
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700198 for (auto& entry : untouched) {
Mark Salyzyna73ed222019-03-13 10:18:24 -0700199 SetMountProperty(entry, false);
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700200 mounts_.erase(entry);
201 }
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700202 for (auto& entry : touched) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700203 SetMountProperty(entry, true);
204 mounts_.emplace(std::move(entry));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700205 }
206}
207
208} // namespace init
209} // namespace android