blob: 12dfc6ddc7ad77cbc1ae2789116965f016e38e16 [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>
30
31#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/properties.h>
34#include <android-base/strings.h>
35#include <fs_mgr.h>
36#include <fstab/fstab.h>
37
38#include "epoll.h"
39#include "property_service.h"
40
41namespace android {
42namespace init {
43
44namespace {
45
46MountHandlerEntry ParseMount(const std::string& line) {
47 auto fields = android::base::Split(line, " ");
48 while (fields.size() < 3) fields.emplace_back("");
49 if (fields[0] == "/dev/root") {
50 if (android::fs_mgr::Fstab fstab; android::fs_mgr::ReadDefaultFstab(&fstab)) {
51 if (auto entry = GetEntryForMountPoint(&fstab, "/")) {
52 fields[0] = entry->blk_device;
53 }
54 }
55 }
56 if (android::base::StartsWith(fields[0], "/dev/")) {
57 if (std::string link; android::base::Readlink(fields[0], &link)) {
58 fields[0] = link;
59 }
60 }
61 return MountHandlerEntry(fields[0], fields[1], fields[2]);
62}
63
64void SetMountProperty(const MountHandlerEntry& entry, bool add) {
65 static constexpr char devblock[] = "/dev/block/";
66 if (!android::base::StartsWith(entry.blk_device, devblock)) return;
67 std::string value;
68 if (add) {
69 value = entry.blk_device.substr(strlen(devblock));
70 if (android::base::StartsWith(value, "sd")) {
71 // All sd partitions inherit their queue characteristics
72 // from the whole device reference. Strip partition number.
73 auto it = std::find_if(value.begin(), value.end(), [](char c) { return isdigit(c); });
74 if (it != value.end()) value.erase(it, value.end());
75 }
76 auto queue = "/sys/block/" + value + "/queue";
77 struct stat sb;
78 if (stat(queue.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
79 if (stat(entry.mount_point.c_str(), &sb) || !S_ISDIR(sb.st_mode)) value = "";
80 // Skip the noise associated with APEX until there is a need
81 if (android::base::StartsWith(value, "loop")) value = "";
82 }
83 std::string property =
84 "dev.mnt.blk" + ((entry.mount_point == "/") ? "/root" : entry.mount_point);
85 std::replace(property.begin(), property.end(), '/', '.');
86 if (value.empty() && android::base::GetProperty(property, "").empty()) return;
87 property_set(property, value);
88}
89
90} // namespace
91
92MountHandlerEntry::MountHandlerEntry(const std::string& blk_device, const std::string& mount_point,
93 const std::string& fs_type)
94 : blk_device(blk_device), mount_point(mount_point), fs_type(fs_type) {}
95
96bool MountHandlerEntry::operator<(const MountHandlerEntry& r) const {
97 if (blk_device < r.blk_device) return true;
98 if (blk_device > r.blk_device) return false;
99 if (mount_point < r.mount_point) return true;
100 if (mount_point > r.mount_point) return false;
101 return fs_type < r.fs_type;
102}
103
104MountHandler::MountHandler(Epoll* epoll) : epoll_(epoll), fp_(fopen("/proc/mounts", "re"), fclose) {
105 if (!fp_) PLOG(FATAL) << "Could not open /proc/mounts";
106 auto result = epoll->RegisterHandler(
107 fileno(fp_.get()), [this]() { this->MountHandlerFunction(); }, EPOLLERR | EPOLLPRI);
108 if (!result) LOG(FATAL) << result.error();
109}
110
111MountHandler::~MountHandler() {
112 if (fp_) epoll_->UnregisterHandler(fileno(fp_.get())).IgnoreError();
113}
114
115void MountHandler::MountHandlerFunction() {
116 rewind(fp_.get());
117 char* buf = nullptr;
118 size_t len = 0;
119 auto untouched = mounts_;
120 while (getline(&buf, &len, fp_.get()) != -1) {
121 auto entry = ParseMount(std::string(buf, len));
122 auto match = untouched.find(entry);
123 if (match == untouched.end()) {
124 SetMountProperty(entry, true);
125 mounts_.emplace(std::move(entry));
126 } else {
127 untouched.erase(match);
128 }
129 }
130 free(buf);
131 for (auto entry : untouched) {
132 auto match = mounts_.find(entry);
133 if (match == mounts_.end()) continue;
134 mounts_.erase(match);
135 SetMountProperty(entry, false);
136 }
137}
138
139} // namespace init
140} // namespace android