Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 24 | #include <sys/statfs.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 26 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <limits> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <set> |
| 31 | #include <string> |
| 32 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 35 | #include <android-base/parseint.h> |
| 36 | #include <android-base/stringprintf.h> |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 37 | #include <android-base/strings.h> |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 38 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 39 | #include "applypatch/applypatch.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 40 | #include "otautil/paths.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 41 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 42 | static int EliminateOpenFiles(const std::string& dirname, std::set<std::string>* files) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 43 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir); |
| 44 | if (!d) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 45 | PLOG(ERROR) << "Failed to open /proc"; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 46 | return -1; |
| 47 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 48 | struct dirent* de; |
| 49 | while ((de = readdir(d.get())) != 0) { |
| 50 | unsigned int pid; |
| 51 | if (!android::base::ParseUint(de->d_name, &pid)) { |
| 52 | continue; |
| 53 | } |
| 54 | std::string path = android::base::StringPrintf("/proc/%s/fd/", de->d_name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 55 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 56 | struct dirent* fdde; |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 57 | std::unique_ptr<DIR, decltype(&closedir)> fdd(opendir(path.c_str()), closedir); |
| 58 | if (!fdd) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 59 | PLOG(ERROR) << "Failed to open " << path; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 60 | continue; |
| 61 | } |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 62 | while ((fdde = readdir(fdd.get())) != 0) { |
| 63 | std::string fd_path = path + fdde->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 64 | char link[FILENAME_MAX]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 66 | int count = readlink(fd_path.c_str(), link, sizeof(link)-1); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 67 | if (count >= 0) { |
| 68 | link[count] = '\0'; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 69 | if (android::base::StartsWith(link, dirname)) { |
Yabin Cui | d483c20 | 2016-02-03 17:08:52 -0800 | [diff] [blame] | 70 | if (files->erase(link) > 0) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 71 | LOG(INFO) << link << " is open by " << de->d_name; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 76 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 80 | static std::vector<std::string> FindExpendableFiles( |
| 81 | const std::string& dirname, const std::function<bool(const std::string&)>& name_filter) { |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 82 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(dirname.c_str()), closedir); |
| 83 | if (!d) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 84 | PLOG(ERROR) << "Failed to open " << dirname; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 85 | return {}; |
| 86 | } |
| 87 | |
| 88 | // Look for regular files in the directory (not in any subdirectories). |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 89 | std::set<std::string> files; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 90 | struct dirent* de; |
| 91 | while ((de = readdir(d.get())) != 0) { |
| 92 | std::string path = dirname + "/" + de->d_name; |
| 93 | |
| 94 | // We can't delete cache_temp_source; if it's there we might have restarted during |
| 95 | // installation and could be depending on it to be there. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 96 | if (path == Paths::Get().cache_temp_source()) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 97 | continue; |
| 98 | } |
| 99 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 100 | // Do not delete the file if it doesn't have the expected format. |
| 101 | if (name_filter != nullptr && !name_filter(de->d_name)) { |
| 102 | continue; |
| 103 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 104 | |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 105 | struct stat st; |
| 106 | if (stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { |
| 107 | files.insert(path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 108 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 111 | LOG(INFO) << files.size() << " regular files in deletable directory"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 112 | if (EliminateOpenFiles(dirname, &files) < 0) { |
| 113 | return {}; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 114 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 115 | |
| 116 | return std::vector<std::string>(files.begin(), files.end()); |
| 117 | } |
| 118 | |
| 119 | // Parses the index of given log file, e.g. 3 for last_log.3; returns max number if the log name |
| 120 | // doesn't have the expected format so that we'll delete these ones first. |
| 121 | static unsigned int GetLogIndex(const std::string& log_name) { |
| 122 | if (log_name == "last_log" || log_name == "last_kmsg") { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | unsigned int index; |
| 127 | if (sscanf(log_name.c_str(), "last_log.%u", &index) == 1 || |
| 128 | sscanf(log_name.c_str(), "last_kmsg.%u", &index) == 1) { |
| 129 | return index; |
| 130 | } |
| 131 | |
| 132 | return std::numeric_limits<unsigned int>::max(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 135 | // Returns the amount of free space (in bytes) on the filesystem containing filename, or -1 on |
| 136 | // error. |
| 137 | static int64_t FreeSpaceForFile(const std::string& filename) { |
| 138 | struct statfs sf; |
| 139 | if (statfs(filename.c_str(), &sf) == -1) { |
| 140 | PLOG(ERROR) << "Failed to statfs " << filename; |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | int64_t free_space = static_cast<int64_t>(sf.f_bsize) * sf.f_bavail; |
| 145 | if (sf.f_bsize == 0 || free_space / sf.f_bsize != sf.f_bavail) { |
| 146 | LOG(ERROR) << "Invalid block size or overflow (sf.f_bsize " << sf.f_bsize << ", sf.f_bavail " |
| 147 | << sf.f_bavail << ")"; |
| 148 | return -1; |
| 149 | } |
| 150 | return free_space; |
| 151 | } |
| 152 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 153 | int MakeFreeSpaceOnCache(size_t bytes_needed) { |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 154 | #ifndef __ANDROID__ |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 155 | // TODO(xunchang): Implement a heuristic cache size check during host simulation. |
| 156 | LOG(WARNING) << "Skipped making (" << bytes_needed |
| 157 | << ") bytes free space on /cache; program is running on host"; |
Tianjie Xu | e40c80d | 2018-02-03 17:20:56 -0800 | [diff] [blame] | 158 | return 0; |
| 159 | #endif |
| 160 | |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 161 | std::vector<std::string> dirs{ "/cache", Paths::Get().cache_log_directory() }; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 162 | for (const auto& dirname : dirs) { |
| 163 | if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) { |
| 164 | return 0; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 167 | |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 172 | const std::function<int64_t(const std::string&)>& space_checker) { |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 173 | struct stat st; |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 174 | if (stat(dirname.c_str(), &st) == -1) { |
| 175 | PLOG(ERROR) << "Failed to stat " << dirname; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | if (!S_ISDIR(st.st_mode)) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 179 | LOG(ERROR) << dirname << " is not a directory"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 183 | int64_t free_now = space_checker(dirname); |
| 184 | if (free_now == -1) { |
| 185 | LOG(ERROR) << "Failed to check free space for " << dirname; |
| 186 | return false; |
| 187 | } |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 188 | LOG(INFO) << free_now << " bytes free on " << dirname << " (" << bytes_needed << " needed)"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 189 | |
| 190 | if (free_now >= bytes_needed) { |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | std::vector<std::string> files; |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 195 | if (dirname == Paths::Get().cache_log_directory()) { |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 196 | // Deletes the log files only. |
| 197 | auto log_filter = [](const std::string& file_name) { |
| 198 | return android::base::StartsWith(file_name, "last_log") || |
| 199 | android::base::StartsWith(file_name, "last_kmsg"); |
| 200 | }; |
| 201 | |
| 202 | files = FindExpendableFiles(dirname, log_filter); |
| 203 | |
| 204 | // Older logs will come to the top of the queue. |
| 205 | auto comparator = [](const std::string& name1, const std::string& name2) -> bool { |
| 206 | unsigned int index1 = GetLogIndex(android::base::Basename(name1)); |
| 207 | unsigned int index2 = GetLogIndex(android::base::Basename(name2)); |
| 208 | if (index1 == index2) { |
| 209 | return name1 < name2; |
| 210 | } |
| 211 | |
| 212 | return index1 > index2; |
| 213 | }; |
| 214 | |
| 215 | std::sort(files.begin(), files.end(), comparator); |
| 216 | } else { |
| 217 | // We're allowed to delete unopened regular files in the directory. |
| 218 | files = FindExpendableFiles(dirname, nullptr); |
| 219 | } |
| 220 | |
| 221 | for (const auto& file : files) { |
| 222 | if (unlink(file.c_str()) == -1) { |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 223 | PLOG(ERROR) << "Failed to delete " << file; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 224 | continue; |
| 225 | } |
| 226 | |
| 227 | free_now = space_checker(dirname); |
Tao Bao | 49750f1 | 2018-07-11 16:32:10 -0700 | [diff] [blame] | 228 | if (free_now == -1) { |
| 229 | LOG(ERROR) << "Failed to check free space for " << dirname; |
| 230 | return false; |
| 231 | } |
Tao Bao | bd0fb23 | 2018-07-11 23:33:57 -0700 | [diff] [blame] | 232 | LOG(INFO) << "Deleted " << file << "; now " << free_now << " bytes free"; |
Tianjie Xu | d5fbcc1 | 2018-04-11 14:38:09 -0700 | [diff] [blame] | 233 | if (free_now >= bytes_needed) { |
| 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return false; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 239 | } |