1 /*
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>
28 #include <filesystem>
29 #include <string>
30 #include <utility>
31 #include <vector>
32
33 #include <android-base/file.h>
34 #include <android-base/logging.h>
35 #include <android-base/properties.h>
36 #include <android-base/stringprintf.h>
37 #include <android-base/strings.h>
38 #include <fs_mgr.h>
39 #include <fstab/fstab.h>
40 #include <libdm/dm.h>
41
42 #include "epoll.h"
43
44 using android::base::Basename;
45 using android::base::StringPrintf;
46
47 namespace android {
48 namespace init {
49
50 namespace {
51
ParseMount(const std::string & line)52 MountHandlerEntry 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") {
56 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"))) {
63 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
75 // return sda25 for dm-4, sda25 for sda25, or mmcblk0p24 for mmcblk0p24
GetDiskPart(std::string blockdev)76 std::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
GetRootDisk(std::string blockdev)92 std::string GetRootDisk(std::string blockdev) {
93 if (blockdev.empty()) return {};
94 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 }
103 return {};
104 }
105
SetMountProperty(const MountHandlerEntry & entry,bool add)106 void SetMountProperty(const MountHandlerEntry& entry, bool add) {
107 static constexpr char devblock[] = "/dev/block/";
108 if (!android::base::StartsWith(entry.blk_device, devblock)) return;
109 auto target = entry.blk_device.substr(strlen(devblock));
110 std::string diskpart, rootdisk;
111 if (add) {
112 diskpart = GetDiskPart(target);
113 rootdisk = GetRootDisk(diskpart);
114
115 struct stat sb;
116 if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) rootdisk = "";
117 // Clear the noise associated with loopback and APEX.
118 if (android::base::StartsWith(target, "loop")) rootdisk = "";
119 if (android::base::StartsWith(entry.mount_point, "/apex/")) rootdisk = "";
120 }
121 auto mount_prop = entry.mount_point;
122 if (mount_prop == "/") mount_prop = "/root";
123 std::replace(mount_prop.begin(), mount_prop.end(), '/', '.');
124 auto blk_mount_prop = "dev.mnt.blk" + mount_prop;
125 auto dev_mount_prop = "dev.mnt.dev" + mount_prop;
126 auto rootdisk_mount_prop = "dev.mnt.rootdisk" + mount_prop;
127 // Set property even if its rootdisk does not change to trigger 'on property:'
128 // handling, except for clearing non-existent or already clear property.
129 // Goal is reduction of empty properties and associated triggers.
130 if (rootdisk.empty() && android::base::GetProperty(blk_mount_prop, "").empty()) return;
131
132 if (rootdisk.empty()) {
133 android::base::SetProperty(blk_mount_prop, "");
134 android::base::SetProperty(dev_mount_prop, "");
135 android::base::SetProperty(rootdisk_mount_prop, "");
136 return;
137 }
138
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);
151 }
152
153 } // namespace
154
MountHandlerEntry(const std::string & blk_device,const std::string & mount_point,const std::string & fs_type)155 MountHandlerEntry::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
operator <(const MountHandlerEntry & r) const159 bool 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
MountHandler(Epoll * epoll)167 MountHandler::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);
171 if (!result.ok()) LOG(FATAL) << result.error();
172 }
173
~MountHandler()174 MountHandler::~MountHandler() {
175 if (fp_) epoll_->UnregisterHandler(fileno(fp_.get()));
176 }
177
MountHandlerFunction()178 void MountHandler::MountHandlerFunction() {
179 rewind(fp_.get());
180 std::vector<MountHandlerEntry> touched;
181 auto untouched = mounts_;
182 char* buf = nullptr;
183 size_t len = 0;
184 while (getline(&buf, &len, fp_.get()) != -1) {
185 auto buf_string = std::string(buf);
186 if (buf_string.find("/emulated") != std::string::npos) {
187 continue;
188 }
189 auto entry = ParseMount(buf_string);
190 auto match = untouched.find(entry);
191 if (match == untouched.end()) {
192 touched.emplace_back(std::move(entry));
193 } else {
194 untouched.erase(match);
195 }
196 }
197 free(buf);
198 for (auto& entry : untouched) {
199 SetMountProperty(entry, false);
200 mounts_.erase(entry);
201 }
202 for (auto& entry : touched) {
203 SetMountProperty(entry, true);
204 mounts_.emplace(std::move(entry));
205 }
206 }
207
208 } // namespace init
209 } // namespace android
210