blob: 791a0198f58989cf9f0ced96f615251804994101 [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>
28#include <string>
29#include <utility>
Mark Salyzyn793f4b52019-03-25 14:24:32 -070030#include <vector>
Mark Salyzyna73ed222019-03-13 10:18:24 -070031
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 Salyzyn793f4b52019-03-25 14:24:32 -070038#include <libdm/dm.h>
Mark Salyzyna73ed222019-03-13 10:18:24 -070039
40#include "epoll.h"
41#include "property_service.h"
42
43namespace android {
44namespace init {
45
46namespace {
47
48MountHandlerEntry ParseMount(const std::string& line) {
49 auto fields = android::base::Split(line, " ");
50 while (fields.size() < 3) fields.emplace_back("");
51 if (fields[0] == "/dev/root") {
Mark Salyzyn793f4b52019-03-25 14:24:32 -070052 auto& dm = dm::DeviceMapper::Instance();
53 std::string path;
54 if (dm.GetDmDevicePathByName("system", &path) || dm.GetDmDevicePathByName("vroot", &path)) {
55 fields[0] = path;
56 } else if (android::fs_mgr::Fstab fstab; android::fs_mgr::ReadDefaultFstab(&fstab)) {
57 auto entry = GetEntryForMountPoint(&fstab, "/");
58 if (entry || (entry = GetEntryForMountPoint(&fstab, "/system"))) {
Mark Salyzyna73ed222019-03-13 10:18:24 -070059 fields[0] = entry->blk_device;
60 }
61 }
62 }
63 if (android::base::StartsWith(fields[0], "/dev/")) {
64 if (std::string link; android::base::Readlink(fields[0], &link)) {
65 fields[0] = link;
66 }
67 }
68 return MountHandlerEntry(fields[0], fields[1], fields[2]);
69}
70
71void SetMountProperty(const MountHandlerEntry& entry, bool add) {
72 static constexpr char devblock[] = "/dev/block/";
73 if (!android::base::StartsWith(entry.blk_device, devblock)) return;
74 std::string value;
75 if (add) {
76 value = entry.blk_device.substr(strlen(devblock));
77 if (android::base::StartsWith(value, "sd")) {
78 // All sd partitions inherit their queue characteristics
79 // from the whole device reference. Strip partition number.
80 auto it = std::find_if(value.begin(), value.end(), [](char c) { return isdigit(c); });
81 if (it != value.end()) value.erase(it, value.end());
82 }
83 auto queue = "/sys/block/" + value + "/queue";
84 struct stat sb;
85 if (stat(queue.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
86 if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
Mark Salyzyn793f4b52019-03-25 14:24:32 -070087 // Clear the noise associated with loopback and APEX.
Mark Salyzyna73ed222019-03-13 10:18:24 -070088 if (android::base::StartsWith(value, "loop")) value = "";
Mark Salyzyn793f4b52019-03-25 14:24:32 -070089 if (android::base::StartsWith(entry.mount_point, "/apex/")) value = "";
Mark Salyzyna73ed222019-03-13 10:18:24 -070090 }
Mark Salyzyn793f4b52019-03-25 14:24:32 -070091 auto mount_prop = entry.mount_point;
92 if (mount_prop == "/") mount_prop = "/root";
93 std::replace(mount_prop.begin(), mount_prop.end(), '/', '.');
94 mount_prop = "dev.mnt.blk" + mount_prop;
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.
98 if (value.empty() && android::base::GetProperty(mount_prop, "").empty()) return;
99 property_set(mount_prop, value);
Mark Salyzyna73ed222019-03-13 10:18:24 -0700100}
101
102} // namespace
103
104MountHandlerEntry::MountHandlerEntry(const std::string& blk_device, const std::string& mount_point,
105 const std::string& fs_type)
106 : blk_device(blk_device), mount_point(mount_point), fs_type(fs_type) {}
107
108bool MountHandlerEntry::operator<(const MountHandlerEntry& r) const {
109 if (blk_device < r.blk_device) return true;
110 if (blk_device > r.blk_device) return false;
111 if (mount_point < r.mount_point) return true;
112 if (mount_point > r.mount_point) return false;
113 return fs_type < r.fs_type;
114}
115
116MountHandler::MountHandler(Epoll* epoll) : epoll_(epoll), fp_(fopen("/proc/mounts", "re"), fclose) {
117 if (!fp_) PLOG(FATAL) << "Could not open /proc/mounts";
118 auto result = epoll->RegisterHandler(
119 fileno(fp_.get()), [this]() { this->MountHandlerFunction(); }, EPOLLERR | EPOLLPRI);
120 if (!result) LOG(FATAL) << result.error();
121}
122
123MountHandler::~MountHandler() {
Tom Cherry9949ec52019-05-16 16:54:49 -0700124 if (fp_) epoll_->UnregisterHandler(fileno(fp_.get()));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700125}
126
127void MountHandler::MountHandlerFunction() {
128 rewind(fp_.get());
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700129 std::vector<MountHandlerEntry> touched;
130 auto untouched = mounts_;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700131 char* buf = nullptr;
132 size_t len = 0;
Mark Salyzyna73ed222019-03-13 10:18:24 -0700133 while (getline(&buf, &len, fp_.get()) != -1) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700134 auto entry = ParseMount(std::string(buf));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700135 auto match = untouched.find(entry);
136 if (match == untouched.end()) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700137 touched.emplace_back(std::move(entry));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700138 } else {
139 untouched.erase(match);
140 }
141 }
142 free(buf);
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700143 for (auto& entry : untouched) {
Mark Salyzyna73ed222019-03-13 10:18:24 -0700144 SetMountProperty(entry, false);
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700145 mounts_.erase(entry);
146 }
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700147 for (auto& entry : touched) {
Mark Salyzyn793f4b52019-03-25 14:24:32 -0700148 SetMountProperty(entry, true);
149 mounts_.emplace(std::move(entry));
Mark Salyzyna73ed222019-03-13 10:18:24 -0700150 }
151}
152
153} // namespace init
154} // namespace android