Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 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 <string> |
| 29 | #include <utility> |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 30 | #include <vector> |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 31 | |
| 32 | #include <android-base/file.h> |
| 33 | #include <android-base/logging.h> |
| 34 | #include <android-base/properties.h> |
| 35 | #include <android-base/strings.h> |
| 36 | #include <fs_mgr.h> |
| 37 | #include <fstab/fstab.h> |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 38 | #include <libdm/dm.h> |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 39 | |
| 40 | #include "epoll.h" |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace init { |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | MountHandlerEntry ParseMount(const std::string& line) { |
| 48 | auto fields = android::base::Split(line, " "); |
| 49 | while (fields.size() < 3) fields.emplace_back(""); |
| 50 | if (fields[0] == "/dev/root") { |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 51 | auto& dm = dm::DeviceMapper::Instance(); |
| 52 | std::string path; |
| 53 | if (dm.GetDmDevicePathByName("system", &path) || dm.GetDmDevicePathByName("vroot", &path)) { |
| 54 | fields[0] = path; |
| 55 | } else if (android::fs_mgr::Fstab fstab; android::fs_mgr::ReadDefaultFstab(&fstab)) { |
| 56 | auto entry = GetEntryForMountPoint(&fstab, "/"); |
| 57 | if (entry || (entry = GetEntryForMountPoint(&fstab, "/system"))) { |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 58 | fields[0] = entry->blk_device; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | if (android::base::StartsWith(fields[0], "/dev/")) { |
| 63 | if (std::string link; android::base::Readlink(fields[0], &link)) { |
| 64 | fields[0] = link; |
| 65 | } |
| 66 | } |
| 67 | return MountHandlerEntry(fields[0], fields[1], fields[2]); |
| 68 | } |
| 69 | |
| 70 | void SetMountProperty(const MountHandlerEntry& entry, bool add) { |
| 71 | static constexpr char devblock[] = "/dev/block/"; |
| 72 | if (!android::base::StartsWith(entry.blk_device, devblock)) return; |
| 73 | std::string value; |
| 74 | if (add) { |
| 75 | value = entry.blk_device.substr(strlen(devblock)); |
| 76 | if (android::base::StartsWith(value, "sd")) { |
| 77 | // All sd partitions inherit their queue characteristics |
| 78 | // from the whole device reference. Strip partition number. |
| 79 | auto it = std::find_if(value.begin(), value.end(), [](char c) { return isdigit(c); }); |
| 80 | if (it != value.end()) value.erase(it, value.end()); |
| 81 | } |
| 82 | auto queue = "/sys/block/" + value + "/queue"; |
| 83 | struct stat sb; |
| 84 | if (stat(queue.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = ""; |
| 85 | if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = ""; |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 86 | // Clear the noise associated with loopback and APEX. |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 87 | if (android::base::StartsWith(value, "loop")) value = ""; |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 88 | if (android::base::StartsWith(entry.mount_point, "/apex/")) value = ""; |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 89 | } |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 90 | auto mount_prop = entry.mount_point; |
| 91 | if (mount_prop == "/") mount_prop = "/root"; |
| 92 | std::replace(mount_prop.begin(), mount_prop.end(), '/', '.'); |
Guo Weichao | bcefbb1 | 2021-05-26 11:12:11 +0800 | [diff] [blame^] | 93 | auto blk_mount_prop = "dev.mnt.blk" + mount_prop; |
| 94 | auto dev_mount_prop = "dev.mnt.dev" + mount_prop; |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 95 | // Set property even if its value does not change to trigger 'on property:' |
| 96 | // handling, except for clearing non-existent or already clear property. |
| 97 | // Goal is reduction of empty properties and associated triggers. |
Guo Weichao | bcefbb1 | 2021-05-26 11:12:11 +0800 | [diff] [blame^] | 98 | if (value.empty() && android::base::GetProperty(blk_mount_prop, "").empty()) return; |
| 99 | android::base::SetProperty(blk_mount_prop, value); |
| 100 | if (!value.empty()) { |
| 101 | android::base::SetProperty(dev_mount_prop, entry.blk_device.substr(strlen(devblock))); |
| 102 | } else { |
| 103 | android::base::SetProperty(dev_mount_prop, ""); |
| 104 | } |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace |
| 108 | |
| 109 | MountHandlerEntry::MountHandlerEntry(const std::string& blk_device, const std::string& mount_point, |
| 110 | const std::string& fs_type) |
| 111 | : blk_device(blk_device), mount_point(mount_point), fs_type(fs_type) {} |
| 112 | |
| 113 | bool MountHandlerEntry::operator<(const MountHandlerEntry& r) const { |
| 114 | if (blk_device < r.blk_device) return true; |
| 115 | if (blk_device > r.blk_device) return false; |
| 116 | if (mount_point < r.mount_point) return true; |
| 117 | if (mount_point > r.mount_point) return false; |
| 118 | return fs_type < r.fs_type; |
| 119 | } |
| 120 | |
| 121 | MountHandler::MountHandler(Epoll* epoll) : epoll_(epoll), fp_(fopen("/proc/mounts", "re"), fclose) { |
| 122 | if (!fp_) PLOG(FATAL) << "Could not open /proc/mounts"; |
| 123 | auto result = epoll->RegisterHandler( |
| 124 | fileno(fp_.get()), [this]() { this->MountHandlerFunction(); }, EPOLLERR | EPOLLPRI); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 125 | if (!result.ok()) LOG(FATAL) << result.error(); |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | MountHandler::~MountHandler() { |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame] | 129 | if (fp_) epoll_->UnregisterHandler(fileno(fp_.get())); |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void MountHandler::MountHandlerFunction() { |
| 133 | rewind(fp_.get()); |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 134 | std::vector<MountHandlerEntry> touched; |
| 135 | auto untouched = mounts_; |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 136 | char* buf = nullptr; |
| 137 | size_t len = 0; |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 138 | while (getline(&buf, &len, fp_.get()) != -1) { |
Tom Cherry | e1bcd93 | 2020-09-10 17:37:56 +0000 | [diff] [blame] | 139 | auto buf_string = std::string(buf); |
| 140 | if (buf_string.find("/emulated") != std::string::npos) { |
| 141 | continue; |
| 142 | } |
| 143 | auto entry = ParseMount(buf_string); |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 144 | auto match = untouched.find(entry); |
| 145 | if (match == untouched.end()) { |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 146 | touched.emplace_back(std::move(entry)); |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 147 | } else { |
| 148 | untouched.erase(match); |
| 149 | } |
| 150 | } |
| 151 | free(buf); |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 152 | for (auto& entry : untouched) { |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 153 | SetMountProperty(entry, false); |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 154 | mounts_.erase(entry); |
| 155 | } |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 156 | for (auto& entry : touched) { |
Mark Salyzyn | 793f4b5 | 2019-03-25 14:24:32 -0700 | [diff] [blame] | 157 | SetMountProperty(entry, true); |
| 158 | mounts_.emplace(std::move(entry)); |
Mark Salyzyn | a73ed22 | 2019-03-13 10:18:24 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | } // namespace init |
| 163 | } // namespace android |