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 --- 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 ++++++++++++++ 13 files changed, 518 insertions(+), 518 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 (limited to 'otautil') 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; +} -- cgit v1.2.3