From 17054c051a846743ecbfacec5986fe8b35152c9d Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 3 May 2018 22:41:23 -0700 Subject: otautil: Rename dir/sys/thermal utils. Test: mmma -j bootable/recovery Change-Id: I32ab98549e91f993364306e4a88dc654221b3869 --- install.cpp | 4 +- otautil/Android.bp | 6 +- otautil/DirUtil.cpp | 116 ------------------- otautil/SysUtil.cpp | 203 ---------------------------------- otautil/ThermalUtil.cpp | 80 -------------- otautil/dirutil.cpp | 116 +++++++++++++++++++ otautil/include/otautil/DirUtil.h | 39 ------- otautil/include/otautil/SysUtil.h | 53 --------- otautil/include/otautil/ThermalUtil.h | 24 ---- otautil/include/otautil/dirutil.h | 39 +++++++ otautil/include/otautil/sysutil.h | 53 +++++++++ otautil/include/otautil/thermalutil.h | 24 ++++ otautil/sysutil.cpp | 203 ++++++++++++++++++++++++++++++++++ otautil/thermalutil.cpp | 80 ++++++++++++++ recovery.cpp | 2 +- tests/component/updater_test.cpp | 2 +- tests/component/verifier_test.cpp | 4 +- tests/unit/dirutil_test.cpp | 3 +- tests/unit/sysutil_test.cpp | 5 +- tests/unit/zip_test.cpp | 2 +- updater/install.cpp | 2 +- updater/updater.cpp | 4 +- 22 files changed, 532 insertions(+), 532 deletions(-) delete mode 100644 otautil/DirUtil.cpp delete mode 100644 otautil/SysUtil.cpp delete mode 100644 otautil/ThermalUtil.cpp create mode 100644 otautil/dirutil.cpp delete mode 100644 otautil/include/otautil/DirUtil.h delete mode 100644 otautil/include/otautil/SysUtil.h delete mode 100644 otautil/include/otautil/ThermalUtil.h create mode 100644 otautil/include/otautil/dirutil.h create mode 100644 otautil/include/otautil/sysutil.h create mode 100644 otautil/include/otautil/thermalutil.h create mode 100644 otautil/sysutil.cpp create mode 100644 otautil/thermalutil.cpp diff --git a/install.cpp b/install.cpp index 30fd2c6be..a4c6986a6 100644 --- a/install.cpp +++ b/install.cpp @@ -49,10 +49,10 @@ #include #include "common.h" -#include "otautil/SysUtil.h" -#include "otautil/ThermalUtil.h" #include "otautil/error_code.h" #include "otautil/paths.h" +#include "otautil/sysutil.h" +#include "otautil/thermalutil.h" #include "private/install.h" #include "roots.h" #include "ui.h" diff --git a/otautil/Android.bp b/otautil/Android.bp index 572d8df86..0be019c06 100644 --- a/otautil/Android.bp +++ b/otautil/Android.bp @@ -40,10 +40,10 @@ cc_library_static { target: { android: { srcs: [ - "DirUtil.cpp", - "SysUtil.cpp", - "ThermalUtil.cpp", + "dirutil.cpp", "mounts.cpp", + "sysutil.cpp", + "thermalutil.cpp", ], static_libs: [ diff --git a/otautil/DirUtil.cpp b/otautil/DirUtil.cpp deleted file mode 100644 index 61c832813..000000000 --- a/otautil/DirUtil.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "otautil/DirUtil.h" - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -enum class DirStatus { DMISSING, DDIR, DILLEGAL }; - -static DirStatus dir_status(const std::string& path) { - struct stat sb; - if (stat(path.c_str(), &sb) == 0) { - // Something's there; make sure it's a directory. - if (S_ISDIR(sb.st_mode)) { - return DirStatus::DDIR; - } - errno = ENOTDIR; - return DirStatus::DILLEGAL; - } else if (errno != ENOENT) { - // Something went wrong, or something in the path is bad. Can't do anything in this situation. - return DirStatus::DILLEGAL; - } - return DirStatus::DMISSING; -} - -int mkdir_recursively(const std::string& input_path, mode_t mode, bool strip_filename, - const selabel_handle* sehnd) { - // Check for an empty string before we bother making any syscalls. - if (input_path.empty()) { - errno = ENOENT; - return -1; - } - - // Allocate a path that we can modify; stick a slash on the end to make things easier. - std::string path = input_path; - if (strip_filename) { - // Strip everything after the last slash. - size_t pos = path.rfind('/'); - if (pos == std::string::npos) { - errno = ENOENT; - return -1; - } - path.resize(pos + 1); - } else { - // Make sure that the path ends in a slash. - path.push_back('/'); - } - - // See if it already exists. - DirStatus ds = dir_status(path); - if (ds == DirStatus::DDIR) { - return 0; - } else if (ds == DirStatus::DILLEGAL) { - return -1; - } - - // Walk up the path from the root and make each level. - size_t prev_end = 0; - while (prev_end < path.size()) { - size_t next_end = path.find('/', prev_end + 1); - if (next_end == std::string::npos) { - break; - } - std::string dir_path = path.substr(0, next_end); - // Check this part of the path and make a new directory if necessary. - switch (dir_status(dir_path)) { - case DirStatus::DILLEGAL: - // Could happen if some other process/thread is messing with the filesystem. - return -1; - case DirStatus::DMISSING: { - char* secontext = nullptr; - if (sehnd) { - selabel_lookup(const_cast(sehnd), &secontext, dir_path.c_str(), mode); - setfscreatecon(secontext); - } - int err = mkdir(dir_path.c_str(), mode); - if (secontext) { - freecon(secontext); - setfscreatecon(nullptr); - } - if (err != 0) { - return -1; - } - break; - } - default: - // Already exists. - break; - } - prev_end = next_end; - } - return 0; -} diff --git a/otautil/SysUtil.cpp b/otautil/SysUtil.cpp deleted file mode 100644 index 48336ad07..000000000 --- a/otautil/SysUtil.cpp +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "otautil/SysUtil.h" - -#include // TEMP_FAILURE_RETRY -#include -#include // SIZE_MAX -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -bool MemMapping::MapFD(int fd) { - struct stat sb; - if (fstat(fd, &sb) == -1) { - PLOG(ERROR) << "fstat(" << fd << ") failed"; - return false; - } - - void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (memPtr == MAP_FAILED) { - PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed"; - return false; - } - - addr = static_cast(memPtr); - length = sb.st_size; - ranges_.clear(); - ranges_.emplace_back(MappedRange{ memPtr, static_cast(sb.st_size) }); - - return true; -} - -// A "block map" which looks like this (from uncrypt/uncrypt.cpp): -// -// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device -// 49652 4096 # file size in bytes, block size -// 3 # count of block ranges -// 1000 1008 # block range 0 -// 2100 2102 # ... block range 1 -// 30 33 # ... block range 2 -// -// Each block range represents a half-open interval; the line "30 33" reprents the blocks -// [30, 31, 32]. -bool MemMapping::MapBlockFile(const std::string& filename) { - std::string content; - if (!android::base::ReadFileToString(filename, &content)) { - PLOG(ERROR) << "Failed to read " << filename; - return false; - } - - std::vector lines = android::base::Split(android::base::Trim(content), "\n"); - if (lines.size() < 4) { - LOG(ERROR) << "Block map file is too short: " << lines.size(); - return false; - } - - size_t size; - size_t blksize; - if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) { - LOG(ERROR) << "Failed to parse file size and block size: " << lines[1]; - return false; - } - - size_t range_count; - if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) { - LOG(ERROR) << "Failed to parse block map header: " << lines[2]; - return false; - } - - size_t blocks; - if (blksize != 0) { - blocks = ((size - 1) / blksize) + 1; - } - if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 || - lines.size() != 3 + range_count) { - LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize - << ", range_count " << range_count << ", lines " << lines.size(); - return false; - } - - // Reserve enough contiguous address space for the whole file. - void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); - if (reserve == MAP_FAILED) { - PLOG(ERROR) << "failed to reserve address space"; - return false; - } - - const std::string& block_dev = lines[0]; - android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY))); - if (fd == -1) { - PLOG(ERROR) << "failed to open block device " << block_dev; - munmap(reserve, blocks * blksize); - return false; - } - - ranges_.clear(); - - unsigned char* next = static_cast(reserve); - size_t remaining_size = blocks * blksize; - bool success = true; - for (size_t i = 0; i < range_count; ++i) { - const std::string& line = lines[i + 3]; - - size_t start, end; - if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) { - LOG(ERROR) << "failed to parse range " << i << ": " << line; - success = false; - break; - } - size_t range_size = (end - start) * blksize; - if (end <= start || (end - start) > SIZE_MAX / blksize || range_size > remaining_size) { - LOG(ERROR) << "Invalid range: " << start << " " << end; - success = false; - break; - } - - void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, - static_cast(start) * blksize); - if (range_start == MAP_FAILED) { - PLOG(ERROR) << "failed to map range " << i << ": " << line; - success = false; - break; - } - ranges_.emplace_back(MappedRange{ range_start, range_size }); - - next += range_size; - remaining_size -= range_size; - } - if (success && remaining_size != 0) { - LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size; - success = false; - } - if (!success) { - munmap(reserve, blocks * blksize); - return false; - } - - addr = static_cast(reserve); - length = size; - - LOG(INFO) << "mmapped " << range_count << " ranges"; - - return true; -} - -bool MemMapping::MapFile(const std::string& fn) { - if (fn.empty()) { - LOG(ERROR) << "Empty filename"; - return false; - } - - if (fn[0] == '@') { - // Block map file "@/cache/recovery/block.map". - if (!MapBlockFile(fn.substr(1))) { - LOG(ERROR) << "Map of '" << fn << "' failed"; - return false; - } - } else { - // This is a regular file. - android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY))); - if (fd == -1) { - PLOG(ERROR) << "Unable to open '" << fn << "'"; - return false; - } - - if (!MapFD(fd)) { - LOG(ERROR) << "Map of '" << fn << "' failed"; - return false; - } - } - return true; -} - -MemMapping::~MemMapping() { - for (const auto& range : ranges_) { - if (munmap(range.addr, range.length) == -1) { - PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")"; - } - }; - ranges_.clear(); -} diff --git a/otautil/ThermalUtil.cpp b/otautil/ThermalUtil.cpp deleted file mode 100644 index 5d9bd45c0..000000000 --- a/otautil/ThermalUtil.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "otautil/ThermalUtil.h" - -#include -#include - -#include -#include -#include - -#include -#include -#include -#include - -static constexpr auto THERMAL_PREFIX = "/sys/class/thermal/"; - -static int thermal_filter(const dirent* de) { - if (android::base::StartsWith(de->d_name, "thermal_zone")) { - return 1; - } - return 0; -} - -static std::vector InitThermalPaths() { - dirent** namelist; - int n = scandir(THERMAL_PREFIX, &namelist, thermal_filter, alphasort); - if (n == -1) { - PLOG(ERROR) << "Failed to scandir " << THERMAL_PREFIX; - return {}; - } - if (n == 0) { - LOG(ERROR) << "Failed to find CPU thermal info in " << THERMAL_PREFIX; - return {}; - } - - std::vector thermal_paths; - while (n--) { - thermal_paths.push_back(THERMAL_PREFIX + std::string(namelist[n]->d_name) + "/temp"); - free(namelist[n]); - } - free(namelist); - return thermal_paths; -} - -int GetMaxValueFromThermalZone() { - static std::vector thermal_paths = InitThermalPaths(); - int max_temperature = -1; - for (const auto& path : thermal_paths) { - std::string content; - if (!android::base::ReadFileToString(path, &content)) { - PLOG(WARNING) << "Failed to read " << path; - continue; - } - - int temperature; - if (!android::base::ParseInt(android::base::Trim(content), &temperature)) { - LOG(WARNING) << "Failed to parse integer in " << content; - continue; - } - max_temperature = std::max(temperature, max_temperature); - } - LOG(INFO) << "current maximum temperature: " << max_temperature; - return max_temperature; -} diff --git a/otautil/dirutil.cpp b/otautil/dirutil.cpp new file mode 100644 index 000000000..ae1cd5c28 --- /dev/null +++ b/otautil/dirutil.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "otautil/dirutil.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +enum class DirStatus { DMISSING, DDIR, DILLEGAL }; + +static DirStatus dir_status(const std::string& path) { + struct stat sb; + if (stat(path.c_str(), &sb) == 0) { + // Something's there; make sure it's a directory. + if (S_ISDIR(sb.st_mode)) { + return DirStatus::DDIR; + } + errno = ENOTDIR; + return DirStatus::DILLEGAL; + } else if (errno != ENOENT) { + // Something went wrong, or something in the path is bad. Can't do anything in this situation. + return DirStatus::DILLEGAL; + } + return DirStatus::DMISSING; +} + +int mkdir_recursively(const std::string& input_path, mode_t mode, bool strip_filename, + const selabel_handle* sehnd) { + // Check for an empty string before we bother making any syscalls. + if (input_path.empty()) { + errno = ENOENT; + return -1; + } + + // Allocate a path that we can modify; stick a slash on the end to make things easier. + std::string path = input_path; + if (strip_filename) { + // Strip everything after the last slash. + size_t pos = path.rfind('/'); + if (pos == std::string::npos) { + errno = ENOENT; + return -1; + } + path.resize(pos + 1); + } else { + // Make sure that the path ends in a slash. + path.push_back('/'); + } + + // See if it already exists. + DirStatus ds = dir_status(path); + if (ds == DirStatus::DDIR) { + return 0; + } else if (ds == DirStatus::DILLEGAL) { + return -1; + } + + // Walk up the path from the root and make each level. + size_t prev_end = 0; + while (prev_end < path.size()) { + size_t next_end = path.find('/', prev_end + 1); + if (next_end == std::string::npos) { + break; + } + std::string dir_path = path.substr(0, next_end); + // Check this part of the path and make a new directory if necessary. + switch (dir_status(dir_path)) { + case DirStatus::DILLEGAL: + // Could happen if some other process/thread is messing with the filesystem. + return -1; + case DirStatus::DMISSING: { + char* secontext = nullptr; + if (sehnd) { + selabel_lookup(const_cast(sehnd), &secontext, dir_path.c_str(), mode); + setfscreatecon(secontext); + } + int err = mkdir(dir_path.c_str(), mode); + if (secontext) { + freecon(secontext); + setfscreatecon(nullptr); + } + if (err != 0) { + return -1; + } + break; + } + default: + // Already exists. + break; + } + prev_end = next_end; + } + return 0; +} diff --git a/otautil/include/otautil/DirUtil.h b/otautil/include/otautil/DirUtil.h deleted file mode 100644 index 85d6c16d1..000000000 --- a/otautil/include/otautil/DirUtil.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OTAUTIL_DIRUTIL_H_ -#define OTAUTIL_DIRUTIL_H_ - -#include // mode_t - -#include - -struct selabel_handle; - -// Like "mkdir -p", try to guarantee that all directories specified in path are present, creating as -// many directories as necessary. The specified mode is passed to all mkdir calls; no modifications -// are made to umask. -// -// If strip_filename is set, everything after the final '/' is stripped before creating the -// directory -// hierarchy. -// -// Returns 0 on success; returns -1 (and sets errno) on failure (usually if some element of path is -// not a directory). -int mkdir_recursively(const std::string& path, mode_t mode, bool strip_filename, - const struct selabel_handle* sehnd); - -#endif // OTAUTIL_DIRUTIL_H_ diff --git a/otautil/include/otautil/SysUtil.h b/otautil/include/otautil/SysUtil.h deleted file mode 100644 index 52f6d20a7..000000000 --- a/otautil/include/otautil/SysUtil.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OTAUTIL_SYSUTIL -#define _OTAUTIL_SYSUTIL - -#include - -#include -#include - -/* - * Use this to keep track of mapped segments. - */ -class MemMapping { - public: - ~MemMapping(); - // Map a file into a private, read-only memory segment. If 'filename' begins with an '@' - // character, it is a map of blocks to be mapped, otherwise it is treated as an ordinary file. - bool MapFile(const std::string& filename); - size_t ranges() const { - return ranges_.size(); - }; - - unsigned char* addr; // start of data - size_t length; // length of data - - private: - struct MappedRange { - void* addr; - size_t length; - }; - - bool MapBlockFile(const std::string& filename); - bool MapFD(int fd); - - std::vector ranges_; -}; - -#endif // _OTAUTIL_SYSUTIL diff --git a/otautil/include/otautil/ThermalUtil.h b/otautil/include/otautil/ThermalUtil.h deleted file mode 100644 index 43ab55940..000000000 --- a/otautil/include/otautil/ThermalUtil.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OTAUTIL_THERMALUTIL_H -#define OTAUTIL_THERMALUTIL_H - -// We can find the temperature reported by all sensors in /sys/class/thermal/thermal_zone*/temp. -// Their values are in millidegree Celsius; and we will log the maximum one. -int GetMaxValueFromThermalZone(); - -#endif // OTAUTIL_THERMALUTIL_H diff --git a/otautil/include/otautil/dirutil.h b/otautil/include/otautil/dirutil.h new file mode 100644 index 000000000..85d6c16d1 --- /dev/null +++ b/otautil/include/otautil/dirutil.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OTAUTIL_DIRUTIL_H_ +#define OTAUTIL_DIRUTIL_H_ + +#include // mode_t + +#include + +struct selabel_handle; + +// Like "mkdir -p", try to guarantee that all directories specified in path are present, creating as +// many directories as necessary. The specified mode is passed to all mkdir calls; no modifications +// are made to umask. +// +// If strip_filename is set, everything after the final '/' is stripped before creating the +// directory +// hierarchy. +// +// Returns 0 on success; returns -1 (and sets errno) on failure (usually if some element of path is +// not a directory). +int mkdir_recursively(const std::string& path, mode_t mode, bool strip_filename, + const struct selabel_handle* sehnd); + +#endif // OTAUTIL_DIRUTIL_H_ diff --git a/otautil/include/otautil/sysutil.h b/otautil/include/otautil/sysutil.h new file mode 100644 index 000000000..52f6d20a7 --- /dev/null +++ b/otautil/include/otautil/sysutil.h @@ -0,0 +1,53 @@ +/* + * Copyright 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _OTAUTIL_SYSUTIL +#define _OTAUTIL_SYSUTIL + +#include + +#include +#include + +/* + * Use this to keep track of mapped segments. + */ +class MemMapping { + public: + ~MemMapping(); + // Map a file into a private, read-only memory segment. If 'filename' begins with an '@' + // character, it is a map of blocks to be mapped, otherwise it is treated as an ordinary file. + bool MapFile(const std::string& filename); + size_t ranges() const { + return ranges_.size(); + }; + + unsigned char* addr; // start of data + size_t length; // length of data + + private: + struct MappedRange { + void* addr; + size_t length; + }; + + bool MapBlockFile(const std::string& filename); + bool MapFD(int fd); + + std::vector ranges_; +}; + +#endif // _OTAUTIL_SYSUTIL diff --git a/otautil/include/otautil/thermalutil.h b/otautil/include/otautil/thermalutil.h new file mode 100644 index 000000000..43ab55940 --- /dev/null +++ b/otautil/include/otautil/thermalutil.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OTAUTIL_THERMALUTIL_H +#define OTAUTIL_THERMALUTIL_H + +// We can find the temperature reported by all sensors in /sys/class/thermal/thermal_zone*/temp. +// Their values are in millidegree Celsius; and we will log the maximum one. +int GetMaxValueFromThermalZone(); + +#endif // OTAUTIL_THERMALUTIL_H diff --git a/otautil/sysutil.cpp b/otautil/sysutil.cpp new file mode 100644 index 000000000..e6385c4e9 --- /dev/null +++ b/otautil/sysutil.cpp @@ -0,0 +1,203 @@ +/* + * Copyright 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "otautil/sysutil.h" + +#include // TEMP_FAILURE_RETRY +#include +#include // SIZE_MAX +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +bool MemMapping::MapFD(int fd) { + struct stat sb; + if (fstat(fd, &sb) == -1) { + PLOG(ERROR) << "fstat(" << fd << ") failed"; + return false; + } + + void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (memPtr == MAP_FAILED) { + PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed"; + return false; + } + + addr = static_cast(memPtr); + length = sb.st_size; + ranges_.clear(); + ranges_.emplace_back(MappedRange{ memPtr, static_cast(sb.st_size) }); + + return true; +} + +// A "block map" which looks like this (from uncrypt/uncrypt.cpp): +// +// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device +// 49652 4096 # file size in bytes, block size +// 3 # count of block ranges +// 1000 1008 # block range 0 +// 2100 2102 # ... block range 1 +// 30 33 # ... block range 2 +// +// Each block range represents a half-open interval; the line "30 33" reprents the blocks +// [30, 31, 32]. +bool MemMapping::MapBlockFile(const std::string& filename) { + std::string content; + if (!android::base::ReadFileToString(filename, &content)) { + PLOG(ERROR) << "Failed to read " << filename; + return false; + } + + std::vector lines = android::base::Split(android::base::Trim(content), "\n"); + if (lines.size() < 4) { + LOG(ERROR) << "Block map file is too short: " << lines.size(); + return false; + } + + size_t size; + size_t blksize; + if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) { + LOG(ERROR) << "Failed to parse file size and block size: " << lines[1]; + return false; + } + + size_t range_count; + if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) { + LOG(ERROR) << "Failed to parse block map header: " << lines[2]; + return false; + } + + size_t blocks; + if (blksize != 0) { + blocks = ((size - 1) / blksize) + 1; + } + if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 || + lines.size() != 3 + range_count) { + LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize + << ", range_count " << range_count << ", lines " << lines.size(); + return false; + } + + // Reserve enough contiguous address space for the whole file. + void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); + if (reserve == MAP_FAILED) { + PLOG(ERROR) << "failed to reserve address space"; + return false; + } + + const std::string& block_dev = lines[0]; + android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY))); + if (fd == -1) { + PLOG(ERROR) << "failed to open block device " << block_dev; + munmap(reserve, blocks * blksize); + return false; + } + + ranges_.clear(); + + unsigned char* next = static_cast(reserve); + size_t remaining_size = blocks * blksize; + bool success = true; + for (size_t i = 0; i < range_count; ++i) { + const std::string& line = lines[i + 3]; + + size_t start, end; + if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) { + LOG(ERROR) << "failed to parse range " << i << ": " << line; + success = false; + break; + } + size_t range_size = (end - start) * blksize; + if (end <= start || (end - start) > SIZE_MAX / blksize || range_size > remaining_size) { + LOG(ERROR) << "Invalid range: " << start << " " << end; + success = false; + break; + } + + void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, + static_cast(start) * blksize); + if (range_start == MAP_FAILED) { + PLOG(ERROR) << "failed to map range " << i << ": " << line; + success = false; + break; + } + ranges_.emplace_back(MappedRange{ range_start, range_size }); + + next += range_size; + remaining_size -= range_size; + } + if (success && remaining_size != 0) { + LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size; + success = false; + } + if (!success) { + munmap(reserve, blocks * blksize); + return false; + } + + addr = static_cast(reserve); + length = size; + + LOG(INFO) << "mmapped " << range_count << " ranges"; + + return true; +} + +bool MemMapping::MapFile(const std::string& fn) { + if (fn.empty()) { + LOG(ERROR) << "Empty filename"; + return false; + } + + if (fn[0] == '@') { + // Block map file "@/cache/recovery/block.map". + if (!MapBlockFile(fn.substr(1))) { + LOG(ERROR) << "Map of '" << fn << "' failed"; + return false; + } + } else { + // This is a regular file. + android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY))); + if (fd == -1) { + PLOG(ERROR) << "Unable to open '" << fn << "'"; + return false; + } + + if (!MapFD(fd)) { + LOG(ERROR) << "Map of '" << fn << "' failed"; + return false; + } + } + return true; +} + +MemMapping::~MemMapping() { + for (const auto& range : ranges_) { + if (munmap(range.addr, range.length) == -1) { + PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")"; + } + }; + ranges_.clear(); +} diff --git a/otautil/thermalutil.cpp b/otautil/thermalutil.cpp new file mode 100644 index 000000000..4660e057e --- /dev/null +++ b/otautil/thermalutil.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "otautil/thermalutil.h" + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +static constexpr auto THERMAL_PREFIX = "/sys/class/thermal/"; + +static int thermal_filter(const dirent* de) { + if (android::base::StartsWith(de->d_name, "thermal_zone")) { + return 1; + } + return 0; +} + +static std::vector InitThermalPaths() { + dirent** namelist; + int n = scandir(THERMAL_PREFIX, &namelist, thermal_filter, alphasort); + if (n == -1) { + PLOG(ERROR) << "Failed to scandir " << THERMAL_PREFIX; + return {}; + } + if (n == 0) { + LOG(ERROR) << "Failed to find CPU thermal info in " << THERMAL_PREFIX; + return {}; + } + + std::vector thermal_paths; + while (n--) { + thermal_paths.push_back(THERMAL_PREFIX + std::string(namelist[n]->d_name) + "/temp"); + free(namelist[n]); + } + free(namelist); + return thermal_paths; +} + +int GetMaxValueFromThermalZone() { + static std::vector thermal_paths = InitThermalPaths(); + int max_temperature = -1; + for (const auto& path : thermal_paths) { + std::string content; + if (!android::base::ReadFileToString(path, &content)) { + PLOG(WARNING) << "Failed to read " << path; + continue; + } + + int temperature; + if (!android::base::ParseInt(android::base::Trim(content), &temperature)) { + LOG(WARNING) << "Failed to parse integer in " << content; + continue; + } + max_temperature = std::max(temperature, max_temperature); + } + LOG(INFO) << "current maximum temperature: " << max_temperature; + return max_temperature; +} diff --git a/recovery.cpp b/recovery.cpp index 890f99c53..d8f56b83e 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -67,7 +67,7 @@ #include "fuse_sideload.h" #include "install.h" #include "minui/minui.h" -#include "otautil/DirUtil.h" +#include "otautil/dirutil.h" #include "otautil/error_code.h" #include "otautil/paths.h" #include "roots.h" diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp index 5d3b2d996..48363a62b 100644 --- a/tests/component/updater_test.cpp +++ b/tests/component/updater_test.cpp @@ -40,10 +40,10 @@ #include "common/test_constants.h" #include "edify/expr.h" -#include "otautil/SysUtil.h" #include "otautil/error_code.h" #include "otautil/paths.h" #include "otautil/print_sha1.h" +#include "otautil/sysutil.h" #include "updater/blockimg.h" #include "updater/install.h" #include "updater/updater.h" diff --git a/tests/component/verifier_test.cpp b/tests/component/verifier_test.cpp index 2ef3828ad..3246ecdbc 100644 --- a/tests/component/verifier_test.cpp +++ b/tests/component/verifier_test.cpp @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -28,9 +27,10 @@ #include #include #include +#include #include "common/test_constants.h" -#include "otautil/SysUtil.h" +#include "otautil/sysutil.h" #include "verifier.h" using namespace std::string_literals; diff --git a/tests/unit/dirutil_test.cpp b/tests/unit/dirutil_test.cpp index 7f85d13ea..1ca786c28 100644 --- a/tests/unit/dirutil_test.cpp +++ b/tests/unit/dirutil_test.cpp @@ -22,7 +22,8 @@ #include #include -#include + +#include "otautil/dirutil.h" TEST(DirUtilTest, create_invalid) { // Requesting to create an empty dir is invalid. diff --git a/tests/unit/sysutil_test.cpp b/tests/unit/sysutil_test.cpp index 434ee25bf..19fa4c59e 100644 --- a/tests/unit/sysutil_test.cpp +++ b/tests/unit/sysutil_test.cpp @@ -14,14 +14,13 @@ * limitations under the License. */ -#include - #include #include #include +#include -#include "otautil/SysUtil.h" +#include "otautil/sysutil.h" TEST(SysUtilTest, InvalidArgs) { MemMapping mapping; diff --git a/tests/unit/zip_test.cpp b/tests/unit/zip_test.cpp index 827668521..47f33d9ea 100644 --- a/tests/unit/zip_test.cpp +++ b/tests/unit/zip_test.cpp @@ -23,10 +23,10 @@ #include #include #include -#include #include #include "common/test_constants.h" +#include "otautil/sysutil.h" TEST(ZipTest, OpenFromMemory) { std::string zip_path = from_testdata_base("ziptest_dummy-update.zip"); diff --git a/updater/install.cpp b/updater/install.cpp index 5623c3379..b41d48c48 100644 --- a/updater/install.cpp +++ b/updater/install.cpp @@ -58,7 +58,7 @@ #include "edify/expr.h" #include "otafault/ota_io.h" -#include "otautil/DirUtil.h" +#include "otautil/dirutil.h" #include "otautil/error_code.h" #include "otautil/mounts.h" #include "otautil/print_sha1.h" diff --git a/updater/updater.cpp b/updater/updater.cpp index bf7c36caf..40e3f1fc1 100644 --- a/updater/updater.cpp +++ b/updater/updater.cpp @@ -32,9 +32,9 @@ #include "edify/expr.h" #include "otafault/config.h" -#include "otautil/DirUtil.h" -#include "otautil/SysUtil.h" +#include "otautil/dirutil.h" #include "otautil/error_code.h" +#include "otautil/sysutil.h" #include "updater/blockimg.h" #include "updater/install.h" -- cgit v1.2.3 From 2c52639d01cb2ff9f0a805e0dda72d39a5487d88 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 3 May 2018 23:01:13 -0700 Subject: Move reboot() from common.h into otautil/sysutil.h. This breaks the dependency on common.h (which belongs to recovery/librecovery) from librecovery_ui. reboot() is now owned by libotautil, which is expected to be a leaf node to be depended on. With the change, recovery and updater also share the same reboot() code now. Test: mmma -j bootable/recovery Change-Id: I1cc5d702cfe49302048db33d31c9c87ddc97ac71 --- Android.mk | 1 + common.h | 2 -- otautil/Android.bp | 1 + otautil/include/otautil/sysutil.h | 4 ++++ otautil/sysutil.cpp | 10 ++++++++++ recovery.cpp | 9 +-------- ui.cpp | 6 ++---- updater/install.cpp | 8 ++------ 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Android.mk b/Android.mk index acff24a73..abe1b838f 100644 --- a/Android.mk +++ b/Android.mk @@ -65,6 +65,7 @@ LOCAL_MODULE := librecovery_ui LOCAL_STATIC_LIBRARIES := \ libminui \ + libotautil \ libbase ifneq ($(TARGET_RECOVERY_UI_MARGIN_HEIGHT),) diff --git a/common.h b/common.h index 33c5ba08f..de536fdb4 100644 --- a/common.h +++ b/common.h @@ -48,6 +48,4 @@ void ui_print(const char* format, ...) __printflike(1, 2); bool is_ro_debuggable(); -bool reboot(const std::string& command); - #endif // RECOVERY_COMMON_H diff --git a/otautil/Android.bp b/otautil/Android.bp index 0be019c06..b058f7b35 100644 --- a/otautil/Android.bp +++ b/otautil/Android.bp @@ -48,6 +48,7 @@ cc_library_static { static_libs: [ "libselinux", + "libcutils", ], }, }, diff --git a/otautil/include/otautil/sysutil.h b/otautil/include/otautil/sysutil.h index 52f6d20a7..649f8ffae 100644 --- a/otautil/include/otautil/sysutil.h +++ b/otautil/include/otautil/sysutil.h @@ -50,4 +50,8 @@ class MemMapping { std::vector ranges_; }; +// Wrapper function to trigger a reboot, by additionally handling quiescent reboot mode. The +// command should start with "reboot," (e.g. "reboot,bootloader" or "reboot,"). +bool reboot(const std::string& command); + #endif // _OTAUTIL_SYSUTIL diff --git a/otautil/sysutil.cpp b/otautil/sysutil.cpp index e6385c4e9..ab1513088 100644 --- a/otautil/sysutil.cpp +++ b/otautil/sysutil.cpp @@ -28,8 +28,10 @@ #include #include +#include #include #include +#include bool MemMapping::MapFD(int fd) { struct stat sb; @@ -201,3 +203,11 @@ MemMapping::~MemMapping() { }; ranges_.clear(); } + +bool reboot(const std::string& command) { + std::string cmd = command; + if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { + cmd += ",quiescent"; + } + return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd); +} diff --git a/recovery.cpp b/recovery.cpp index d8f56b83e..7219771cb 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -70,6 +70,7 @@ #include "otautil/dirutil.h" #include "otautil/error_code.h" #include "otautil/paths.h" +#include "otautil/sysutil.h" #include "roots.h" #include "rotate_logs.h" #include "screen_ui.h" @@ -177,14 +178,6 @@ bool is_ro_debuggable() { return android::base::GetBoolProperty("ro.debuggable", false); } -bool reboot(const std::string& command) { - std::string cmd = command; - if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { - cmd += ",quiescent"; - } - return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd); -} - // command line args come from, in decreasing precedence: // - the actual command line // - the bootloader control block (one per line, after "recovery") diff --git a/ui.cpp b/ui.cpp index 3c9ded735..8983d7692 100644 --- a/ui.cpp +++ b/ui.cpp @@ -36,14 +36,12 @@ #include #include #include -#include #include -#include #include -#include "common.h" -#include "roots.h" #include "device.h" +#include "otautil/sysutil.h" +#include "roots.h" static constexpr int UI_WAIT_KEY_TIMEOUT_SEC = 120; static constexpr const char* BRIGHTNESS_FILE = "/sys/class/leds/lcd-backlight/brightness"; diff --git a/updater/install.cpp b/updater/install.cpp index b41d48c48..bd22467ab 100644 --- a/updater/install.cpp +++ b/updater/install.cpp @@ -48,7 +48,6 @@ #include #include #include -#include #include #include #include @@ -62,6 +61,7 @@ #include "otautil/error_code.h" #include "otautil/mounts.h" #include "otautil/print_sha1.h" +#include "otautil/sysutil.h" #include "updater/updater.h" // Send over the buffer to recovery though the command pipe. @@ -874,11 +874,7 @@ Value* RebootNowFn(const char* name, State* state, const std::vector