Merge "Creating logical and snapshot partitions uses timeout in tests"
diff --git a/fs_mgr/Android.bp b/fs_mgr/Android.bp
index 34c64d2..7a88aa3 100644
--- a/fs_mgr/Android.bp
+++ b/fs_mgr/Android.bp
@@ -91,6 +91,7 @@
},
header_libs: [
"libfiemap_headers",
+ "libstorage_literals_headers",
],
export_header_lib_headers: [
"libfiemap_headers",
@@ -165,7 +166,7 @@
"libcrypto",
"libext4_utils",
"libfec",
- "libfs_mgr",
+ "libfs_mgr_binder",
"liblog",
"liblp",
"libselinux",
@@ -187,4 +188,26 @@
],
},
},
+ required: [
+ "clean_scratch_files",
+ ],
+}
+
+cc_binary {
+ name: "clean_scratch_files",
+ defaults: ["fs_mgr_defaults"],
+ shared_libs: [
+ "libbase",
+ "libfs_mgr_binder",
+ ],
+ srcs: [
+ "clean_scratch_files.cpp",
+ ],
+ product_variables: {
+ debuggable: {
+ init_rc: [
+ "clean_scratch_files.rc",
+ ],
+ },
+ },
}
diff --git a/fs_mgr/clean_scratch_files.cpp b/fs_mgr/clean_scratch_files.cpp
new file mode 100644
index 0000000..42fe35a
--- /dev/null
+++ b/fs_mgr/clean_scratch_files.cpp
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2020 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 <fs_mgr_overlayfs.h>
+
+int main() {
+ android::fs_mgr::CleanupOldScratchFiles();
+ return 0;
+}
diff --git a/fs_mgr/clean_scratch_files.rc b/fs_mgr/clean_scratch_files.rc
new file mode 100644
index 0000000..738d1aa
--- /dev/null
+++ b/fs_mgr/clean_scratch_files.rc
@@ -0,0 +1,2 @@
+on post-fs-data && property:ro.debuggable=1
+ exec_background - root root -- clean_scratch_files
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index c043754..2bc53d3 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -48,15 +48,21 @@
#include <fs_mgr_overlayfs.h>
#include <fstab/fstab.h>
#include <libdm/dm.h>
+#include <libfiemap/image_manager.h>
#include <libgsi/libgsi.h>
#include <liblp/builder.h>
#include <liblp/liblp.h>
+#include <storage_literals/storage_literals.h>
#include "fs_mgr_priv.h"
+#include "libfiemap/utility.h"
using namespace std::literals;
using namespace android::dm;
using namespace android::fs_mgr;
+using namespace android::storage_literals;
+using android::fiemap::FilesystemHasReliablePinning;
+using android::fiemap::IImageManager;
namespace {
@@ -104,6 +110,14 @@
return false;
}
+namespace android {
+namespace fs_mgr {
+
+void MapScratchPartitionIfNeeded(Fstab*,
+ const std::function<bool(const std::set<std::string>&)>&) {}
+} // namespace fs_mgr
+} // namespace android
+
#else // ALLOW_ADBD_DISABLE_VERITY == 0
namespace {
@@ -153,6 +167,12 @@
}
const auto kPhysicalDevice = "/dev/block/by-name/"s;
+constexpr char kScratchImageMetadata[] = "/metadata/gsi/remount/lp_metadata";
+
+// Note: this is meant only for recovery/first-stage init.
+bool ScratchIsOnData() {
+ return fs_mgr_access(kScratchImageMetadata);
+}
bool fs_mgr_update_blk_device(FstabEntry* entry) {
if (entry->fs_mgr_flags.logical) {
@@ -443,20 +463,37 @@
bool fs_mgr_overlayfs_teardown_scratch(const std::string& overlay, bool* change) {
// umount and delete kScratchMountPoint storage if we have logical partitions
if (overlay != kScratchMountPoint) return true;
- auto slot_number = fs_mgr_overlayfs_slot_number();
- auto super_device = fs_mgr_overlayfs_super_device(slot_number);
- if (!fs_mgr_rw_access(super_device)) return true;
auto save_errno = errno;
if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) {
fs_mgr_overlayfs_umount_scratch();
}
+
+ const auto partition_name = android::base::Basename(kScratchMountPoint);
+
+ auto images = IImageManager::Open("remount", 10s);
+ if (images && images->BackingImageExists(partition_name)) {
+#if defined __ANDROID_RECOVERY__
+ if (!images->DisableImage(partition_name)) {
+ return false;
+ }
+#else
+ if (!images->UnmapImageIfExists(partition_name) ||
+ !images->DeleteBackingImage(partition_name)) {
+ return false;
+ }
+#endif
+ }
+
+ auto slot_number = fs_mgr_overlayfs_slot_number();
+ auto super_device = fs_mgr_overlayfs_super_device(slot_number);
+ if (!fs_mgr_rw_access(super_device)) return true;
+
auto builder = MetadataBuilder::New(super_device, slot_number);
if (!builder) {
errno = save_errno;
return true;
}
- const auto partition_name = android::base::Basename(kScratchMountPoint);
if (builder->FindPartition(partition_name) == nullptr) {
errno = save_errno;
return true;
@@ -836,7 +873,8 @@
// This returns the scratch device that was detected during early boot (first-
// stage init). If the device was created later, for example during setup for
// the adb remount command, it can return an empty string since it does not
-// query ImageManager.
+// query ImageManager. (Note that ImageManager in first-stage init will always
+// use device-mapper, since /data is not available to use loop devices.)
static std::string GetBootScratchDevice() {
auto& dm = DeviceMapper::Instance();
@@ -992,12 +1030,66 @@
return true;
}
-static bool CanUseSuperPartition(const Fstab& fstab) {
+static bool CreateScratchOnData(std::string* scratch_device, bool* partition_exists, bool* change) {
+ *partition_exists = false;
+ *change = false;
+
+ auto images = IImageManager::Open("remount", 10s);
+ if (!images) {
+ return false;
+ }
+
+ auto partition_name = android::base::Basename(kScratchMountPoint);
+ if (images->GetMappedImageDevice(partition_name, scratch_device)) {
+ *partition_exists = true;
+ return true;
+ }
+
+ BlockDeviceInfo info;
+ PartitionOpener opener;
+ if (!opener.GetInfo(fs_mgr_get_super_partition_name(), &info)) {
+ LERROR << "could not get block device info for super";
+ return false;
+ }
+
+ *change = true;
+
+ // Note: calling RemoveDisabledImages here ensures that we do not race with
+ // clean_scratch_files and accidentally try to map an image that will be
+ // deleted.
+ if (!images->RemoveDisabledImages()) {
+ return false;
+ }
+ if (!images->BackingImageExists(partition_name)) {
+ static constexpr uint64_t kMinimumSize = 16_MiB;
+ static constexpr uint64_t kMaximumSize = 2_GiB;
+
+ uint64_t size = std::clamp(info.size / 2, kMinimumSize, kMaximumSize);
+ auto flags = IImageManager::CREATE_IMAGE_DEFAULT;
+
+ if (!images->CreateBackingImage(partition_name, size, flags)) {
+ LERROR << "could not create scratch image of " << size << " bytes";
+ return false;
+ }
+ }
+ if (!images->MapImageDevice(partition_name, 10s, scratch_device)) {
+ LERROR << "could not map scratch image";
+ return false;
+ }
+ return true;
+}
+
+static bool CanUseSuperPartition(const Fstab& fstab, bool* is_virtual_ab) {
auto slot_number = fs_mgr_overlayfs_slot_number();
auto super_device = fs_mgr_overlayfs_super_device(slot_number);
if (!fs_mgr_rw_access(super_device) || !fs_mgr_overlayfs_has_logical(fstab)) {
return false;
}
+ auto metadata = ReadMetadata(super_device, slot_number);
+ if (!metadata) {
+ return false;
+ }
+ *is_virtual_ab = !!(metadata->header.flags & LP_HEADER_FLAG_VIRTUAL_AB_DEVICE);
return true;
}
@@ -1011,7 +1103,12 @@
}
// If that fails, see if we can land on super.
- if (CanUseSuperPartition(fstab)) {
+ bool is_virtual_ab;
+ if (CanUseSuperPartition(fstab, &is_virtual_ab)) {
+ bool can_use_data = false;
+ if (is_virtual_ab && FilesystemHasReliablePinning("/data", &can_use_data) && can_use_data) {
+ return CreateScratchOnData(scratch_device, partition_exists, change);
+ }
return CreateDynamicScratch(scratch_device, partition_exists, change);
}
@@ -1053,19 +1150,6 @@
return fs_mgr_overlayfs_mount_scratch(scratch_device, mnt_type);
}
-bool fs_mgr_overlayfs_scratch_can_be_mounted(const std::string& scratch_device) {
- if (scratch_device.empty()) return false;
- if (fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) return false;
- if (android::base::StartsWith(scratch_device, kPhysicalDevice)) return true;
- if (fs_mgr_rw_access(scratch_device)) return true;
- auto slot_number = fs_mgr_overlayfs_slot_number();
- auto super_device = fs_mgr_overlayfs_super_device(slot_number);
- if (!fs_mgr_rw_access(super_device)) return false;
- auto builder = MetadataBuilder::New(super_device, slot_number);
- if (!builder) return false;
- return builder->FindPartition(android::base::Basename(kScratchMountPoint)) != nullptr;
-}
-
bool fs_mgr_overlayfs_invalid() {
if (fs_mgr_overlayfs_valid() == OverlayfsValidResult::kNotSupported) return true;
@@ -1114,7 +1198,7 @@
// if verity is still disabled, i.e. no reboot occurred), and skips calling
// fs_mgr_overlayfs_mount_all().
auto scratch_device = GetBootScratchDevice();
- if (!fs_mgr_overlayfs_scratch_can_be_mounted(scratch_device)) {
+ if (!fs_mgr_rw_access(scratch_device)) {
return;
}
if (!WaitForFile(scratch_device, 10s)) {
@@ -1152,35 +1236,6 @@
return ret;
}
-std::vector<std::string> fs_mgr_overlayfs_required_devices(Fstab* fstab) {
- if (fs_mgr_overlayfs_invalid()) return {};
-
- if (GetEntryForMountPoint(fstab, kScratchMountPoint) != nullptr) {
- return {};
- }
-
- bool want_scratch = false;
- for (const auto& entry : fs_mgr_overlayfs_candidate_list(*fstab)) {
- if (fs_mgr_is_verity_enabled(entry)) {
- continue;
- }
- if (fs_mgr_overlayfs_already_mounted(fs_mgr_mount_point(entry.mount_point))) {
- continue;
- }
- want_scratch = true;
- break;
- }
- if (!want_scratch) {
- return {};
- }
-
- auto device = GetBootScratchDevice();
- if (!device.empty()) {
- return {device};
- }
- return {};
-}
-
// Returns false if setup not permitted, errno set to last error.
// If something is altered, set *change.
bool fs_mgr_overlayfs_setup(const char* backing, const char* mount_point, bool* change,
@@ -1246,13 +1301,27 @@
return ret;
}
-static bool GetAndMapScratchDeviceIfNeeded(std::string* device, bool* mapped) {
+static bool EnsureScratchMapped(std::string* device, bool* mapped) {
*mapped = false;
*device = GetBootScratchDevice();
if (!device->empty()) {
return true;
}
+ auto partition_name = android::base::Basename(kScratchMountPoint);
+
+ // Check for scratch on /data first, before looking for a modified super
+ // partition. We should only reach this code in recovery, because scratch
+ // would otherwise always be mapped.
+ auto images = IImageManager::Open("remount", 10s);
+ if (images && images->BackingImageExists(partition_name)) {
+ if (!images->MapImageDevice(partition_name, 10s, device)) {
+ return false;
+ }
+ *mapped = true;
+ return true;
+ }
+
// Avoid uart spam by first checking for a scratch partition.
auto metadata_slot = fs_mgr_overlayfs_slot_number();
auto super_device = fs_mgr_overlayfs_super_device(metadata_slot);
@@ -1261,7 +1330,6 @@
return false;
}
- auto partition_name = android::base::Basename(kScratchMountPoint);
auto partition = FindPartition(*metadata.get(), partition_name);
if (!partition) {
return false;
@@ -1281,6 +1349,12 @@
return true;
}
+static void UnmapScratchDevice() {
+ // This should only be reachable in recovery, where scratch is not
+ // automatically mapped and therefore can be unmapped.
+ DestroyLogicalPartition(android::base::Basename(kScratchMountPoint));
+}
+
// Returns false if teardown not permitted, errno set to last error.
// If something is altered, set *change.
bool fs_mgr_overlayfs_teardown(const char* mount_point, bool* change) {
@@ -1293,7 +1367,7 @@
bool unmap = false;
if ((mount_point != nullptr) && !fs_mgr_overlayfs_already_mounted(kScratchMountPoint, false)) {
std::string scratch_device;
- if (GetAndMapScratchDeviceIfNeeded(&scratch_device, &unmap)) {
+ if (EnsureScratchMapped(&scratch_device, &unmap)) {
mount_scratch = fs_mgr_overlayfs_mount_scratch(scratch_device,
fs_mgr_overlayfs_scratch_mount_type());
}
@@ -1319,7 +1393,7 @@
fs_mgr_overlayfs_umount_scratch();
}
if (unmap) {
- DestroyLogicalPartition(android::base::Basename(kScratchMountPoint));
+ UnmapScratchDevice();
}
return ret;
}
@@ -1338,6 +1412,59 @@
return false;
}
+namespace android {
+namespace fs_mgr {
+
+void MapScratchPartitionIfNeeded(Fstab* fstab,
+ const std::function<bool(const std::set<std::string>&)>& init) {
+ if (fs_mgr_overlayfs_invalid()) {
+ return;
+ }
+ if (GetEntryForMountPoint(fstab, kScratchMountPoint) != nullptr) {
+ return;
+ }
+
+ bool want_scratch = false;
+ for (const auto& entry : fs_mgr_overlayfs_candidate_list(*fstab)) {
+ if (fs_mgr_is_verity_enabled(entry)) {
+ continue;
+ }
+ if (fs_mgr_overlayfs_already_mounted(fs_mgr_mount_point(entry.mount_point))) {
+ continue;
+ }
+ want_scratch = true;
+ break;
+ }
+ if (!want_scratch) {
+ return;
+ }
+
+ if (ScratchIsOnData()) {
+ if (auto images = IImageManager::Open("remount", 0ms)) {
+ images->MapAllImages(init);
+ }
+ }
+
+ // Physical or logical partitions will have already been mapped here,
+ // so just ensure /dev/block symlinks exist.
+ auto device = GetBootScratchDevice();
+ if (!device.empty()) {
+ init({android::base::Basename(device)});
+ }
+}
+
+void CleanupOldScratchFiles() {
+ if (!ScratchIsOnData()) {
+ return;
+ }
+ if (auto images = IImageManager::Open("remount", 0ms)) {
+ images->RemoveDisabledImages();
+ }
+}
+
+} // namespace fs_mgr
+} // namespace android
+
#endif // ALLOW_ADBD_DISABLE_VERITY != 0
bool fs_mgr_has_shared_blocks(const std::string& mount_point, const std::string& dev) {
diff --git a/fs_mgr/include/fs_mgr_overlayfs.h b/fs_mgr/include/fs_mgr_overlayfs.h
index 9a7381f..34aded9 100644
--- a/fs_mgr/include/fs_mgr_overlayfs.h
+++ b/fs_mgr/include/fs_mgr_overlayfs.h
@@ -16,8 +16,11 @@
#pragma once
+#include <functional>
+
#include <fstab/fstab.h>
+#include <set>
#include <string>
#include <vector>
@@ -38,3 +41,13 @@
kOverrideCredsRequired,
};
OverlayfsValidResult fs_mgr_overlayfs_valid();
+
+namespace android {
+namespace fs_mgr {
+
+void MapScratchPartitionIfNeeded(Fstab* fstab,
+ const std::function<bool(const std::set<std::string>&)>& init);
+void CleanupOldScratchFiles();
+
+} // namespace fs_mgr
+} // namespace android
diff --git a/fs_mgr/libfiemap/image_manager.cpp b/fs_mgr/libfiemap/image_manager.cpp
index 280318e..0195716 100644
--- a/fs_mgr/libfiemap/image_manager.cpp
+++ b/fs_mgr/libfiemap/image_manager.cpp
@@ -124,7 +124,7 @@
return images;
}
-bool ImageManager::PartitionExists(const std::string& name) {
+bool ImageManager::BackingImageExists(const std::string& name) {
if (!MetadataExists(metadata_dir_)) {
return false;
}
@@ -135,11 +135,6 @@
return !!FindPartition(*metadata.get(), name);
}
-bool ImageManager::BackingImageExists(const std::string& name) {
- auto header_file = GetImageHeaderPath(name);
- return access(header_file.c_str(), F_OK) == 0;
-}
-
static bool IsUnreliablePinningAllowed(const std::string& path) {
return android::base::StartsWith(path, "/data/gsi/dsu/") ||
android::base::StartsWith(path, "/data/gsi/test/") ||
@@ -261,6 +256,10 @@
return false;
}
+#if defined __ANDROID_RECOVERY__
+ LOG(ERROR) << "Cannot remove images backed by /data in recovery";
+ return false;
+#else
std::string message;
auto header_file = GetImageHeaderPath(name);
if (!SplitFiemap::RemoveSplitFiles(header_file, &message)) {
@@ -274,6 +273,7 @@
LOG(ERROR) << "Error removing " << status_file << ": " << message;
}
return RemoveImageMetadata(metadata_dir_, name);
+#endif
}
// Create a block device for an image file, using its extents in its
@@ -507,6 +507,7 @@
auto image_header = GetImageHeaderPath(name);
+#if !defined __ANDROID_RECOVERY__
// If there is a device-mapper node wrapping the block device, then we're
// able to create another node around it; the dm layer does not carry the
// exclusion lock down the stack when a mount occurs.
@@ -530,6 +531,13 @@
} else if (!MapWithLoopDevice(name, timeout_ms, path)) {
return false;
}
+#else
+ // In recovery, we can *only* use device-mapper, since partitions aren't
+ // mounted. That also means we cannot call GetBlockDeviceForFile.
+ if (!MapWithDmLinear(*partition_opener_.get(), name, timeout_ms, path)) {
+ return false;
+ }
+#endif
// Set a property so we remember this is mapped.
auto prop_name = GetStatusPropertyName(name);
diff --git a/fs_mgr/libfiemap/include/libfiemap/image_manager.h b/fs_mgr/libfiemap/include/libfiemap/image_manager.h
index 2c13229..60b98dc 100644
--- a/fs_mgr/libfiemap/include/libfiemap/image_manager.h
+++ b/fs_mgr/libfiemap/include/libfiemap/image_manager.h
@@ -76,7 +76,9 @@
// Unmap a block device previously mapped with mapBackingImage.
virtual bool UnmapImageDevice(const std::string& name) = 0;
- // Returns true whether the named backing image exists.
+ // Returns true whether the named backing image exists. This does not check
+ // consistency with the /data partition, so that it can return true in
+ // recovery.
virtual bool BackingImageExists(const std::string& name) = 0;
// Returns true if the specified image is mapped to a device.
@@ -154,10 +156,6 @@
std::vector<std::string> GetAllBackingImages();
- // Returns true if the named partition exists. This does not check the
- // consistency of the backing image/data file.
- bool PartitionExists(const std::string& name);
-
// Validates that all images still have pinned extents. This will be removed
// once b/134588268 is fixed.
bool Validate();
diff --git a/fs_mgr/libstorage_literals/Android.bp b/fs_mgr/libstorage_literals/Android.bp
index 11611dd..beb18ef 100644
--- a/fs_mgr/libstorage_literals/Android.bp
+++ b/fs_mgr/libstorage_literals/Android.bp
@@ -2,5 +2,6 @@
cc_library_headers {
name: "libstorage_literals_headers",
host_supported: true,
+ recovery_available: true,
export_include_dirs: ["."],
}
diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh
index 4226e95..c66f307 100755
--- a/fs_mgr/tests/adb-remount-test.sh
+++ b/fs_mgr/tests/adb-remount-test.sh
@@ -1160,13 +1160,16 @@
ret=${?}
uses_dynamic_scratch=false
scratch_partition=
+virtual_ab=`get_property ro.virtual_ab.enabled`
if ${overlayfs_needed}; then
if [ ${ret} != 0 ]; then
die -t ${T} "overlay takeover failed"
fi
echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
echo "${ORANGE}[ WARNING ]${NORMAL} overlay takeover not complete" >&2
- scratch_partition=scratch
+ if [ -z "${virtual_ab}" ]; then
+ scratch_partition=scratch
+ fi
if echo "${D}" | grep " /mnt/scratch" >/dev/null; then
echo "${BLUE}[ INFO ]${NORMAL} using ${scratch_partition} dynamic partition for overrides" >&2
fi
diff --git a/init/first_stage_mount.cpp b/init/first_stage_mount.cpp
index 9da32e4..d8c4843 100644
--- a/init/first_stage_mount.cpp
+++ b/init/first_stage_mount.cpp
@@ -591,14 +591,18 @@
}
// heads up for instantiating required device(s) for overlayfs logic
- const auto devices = fs_mgr_overlayfs_required_devices(&fstab_);
- for (auto const& device : devices) {
- if (android::base::StartsWith(device, "/dev/block/by-name/")) {
- InitRequiredDevices({basename(device.c_str())});
- } else {
- InitMappedDevice(device);
+ auto init_devices = [this](std::set<std::string> devices) -> bool {
+ for (auto iter = devices.begin(); iter != devices.end();) {
+ if (android::base::StartsWith(*iter, "/dev/block/dm-")) {
+ if (!InitMappedDevice(*iter)) return false;
+ iter = devices.erase(iter);
+ } else {
+ iter++;
+ }
}
- }
+ return InitRequiredDevices(std::move(devices));
+ };
+ MapScratchPartitionIfNeeded(&fstab_, init_devices);
fs_mgr_overlayfs_mount_all(&fstab_);
diff --git a/liblog/Android.bp b/liblog/Android.bp
index 2e87b5b..2cf60e0 100644
--- a/liblog/Android.bp
+++ b/liblog/Android.bp
@@ -21,14 +21,11 @@
"logger_read.cpp",
"logger_write.cpp",
"logprint.cpp",
-]
-liblog_host_sources = [
- "fake_log_device.cpp",
+ "properties.cpp",
]
liblog_target_sources = [
"event_tag_map.cpp",
"log_time.cpp",
- "properties.cpp",
"pmsg_reader.cpp",
"pmsg_writer.cpp",
"logd_reader.cpp",
@@ -69,10 +66,6 @@
srcs: liblog_sources,
target: {
- host: {
- srcs: liblog_host_sources,
- cflags: ["-DFAKE_LOG_DEVICE=1"],
- },
android: {
version_script: "liblog.map.txt",
srcs: liblog_target_sources,
diff --git a/liblog/fake_log_device.cpp b/liblog/fake_log_device.cpp
deleted file mode 100644
index cd4c11e..0000000
--- a/liblog/fake_log_device.cpp
+++ /dev/null
@@ -1,533 +0,0 @@
-/*
- * Copyright (C) 2008-2014 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.
- */
-/*
- * Intercepts log messages intended for the Android log device.
- * Messages are printed to stderr.
- */
-
-#include "fake_log_device.h"
-
-#include <ctype.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include <mutex>
-
-#include <android-base/no_destructor.h>
-#include <android/log.h>
-#include <log/log_id.h>
-#include <log/logprint.h>
-
-#include "logger.h"
-
-#define kMaxTagLen 16 /* from the long-dead utils/Log.cpp */
-
-#define kTagSetSize 16 /* arbitrary */
-
-#if 0
-#define TRACE(...) printf("fake_log_device: " __VA_ARGS__)
-#else
-#define TRACE(...) ((void)0)
-#endif
-
-typedef struct LogState {
- bool initialized = false;
- /* global minimum priority */
- int global_min_priority;
-
- /* output format */
- AndroidLogPrintFormat output_format;
-
- /* tags and priorities */
- struct {
- char tag[kMaxTagLen];
- int minPriority;
- } tagSet[kTagSetSize];
-} LogState;
-
-static LogState log_state;
-static android::base::NoDestructor<std::mutex> fake_log_mutex;
-
-/*
- * Configure logging based on ANDROID_LOG_TAGS environment variable. We
- * need to parse a string that looks like
- *
- * *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
- *
- * The tag (or '*' for the global level) comes first, followed by a colon
- * and a letter indicating the minimum priority level we're expected to log.
- * This can be used to reveal or conceal logs with specific tags.
- *
- * We also want to check ANDROID_PRINTF_LOG to determine how the output
- * will look.
- */
-void InitializeLogStateLocked() {
- log_state.initialized = true;
-
- /* global min priority defaults to "info" level */
- log_state.global_min_priority = ANDROID_LOG_INFO;
-
- /*
- * This is based on the the long-dead utils/Log.cpp code.
- */
- const char* tags = getenv("ANDROID_LOG_TAGS");
- TRACE("Found ANDROID_LOG_TAGS='%s'\n", tags);
- if (tags != NULL) {
- int entry = 0;
-
- while (*tags != '\0') {
- char tagName[kMaxTagLen];
- int i, minPrio;
-
- while (isspace(*tags)) tags++;
-
- i = 0;
- while (*tags != '\0' && !isspace(*tags) && *tags != ':' && i < kMaxTagLen) {
- tagName[i++] = *tags++;
- }
- if (i == kMaxTagLen) {
- TRACE("ERROR: env tag too long (%d chars max)\n", kMaxTagLen - 1);
- return;
- }
- tagName[i] = '\0';
-
- /* default priority, if there's no ":" part; also zero out '*' */
- minPrio = ANDROID_LOG_VERBOSE;
- if (tagName[0] == '*' && tagName[1] == '\0') {
- minPrio = ANDROID_LOG_DEBUG;
- tagName[0] = '\0';
- }
-
- if (*tags == ':') {
- tags++;
- if (*tags >= '0' && *tags <= '9') {
- if (*tags >= ('0' + ANDROID_LOG_SILENT))
- minPrio = ANDROID_LOG_VERBOSE;
- else
- minPrio = *tags - '\0';
- } else {
- switch (*tags) {
- case 'v':
- minPrio = ANDROID_LOG_VERBOSE;
- break;
- case 'd':
- minPrio = ANDROID_LOG_DEBUG;
- break;
- case 'i':
- minPrio = ANDROID_LOG_INFO;
- break;
- case 'w':
- minPrio = ANDROID_LOG_WARN;
- break;
- case 'e':
- minPrio = ANDROID_LOG_ERROR;
- break;
- case 'f':
- minPrio = ANDROID_LOG_FATAL;
- break;
- case 's':
- minPrio = ANDROID_LOG_SILENT;
- break;
- default:
- minPrio = ANDROID_LOG_DEFAULT;
- break;
- }
- }
-
- tags++;
- if (*tags != '\0' && !isspace(*tags)) {
- TRACE("ERROR: garbage in tag env; expected whitespace\n");
- TRACE(" env='%s'\n", tags);
- return;
- }
- }
-
- if (tagName[0] == 0) {
- log_state.global_min_priority = minPrio;
- TRACE("+++ global min prio %d\n", logState->globalMinPriority);
- } else {
- log_state.tagSet[entry].minPriority = minPrio;
- strcpy(log_state.tagSet[entry].tag, tagName);
- TRACE("+++ entry %d: %s:%d\n", entry, logState->tagSet[entry].tag,
- logState->tagSet[entry].minPriority);
- entry++;
- }
- }
- }
-
- /*
- * Taken from the long-dead utils/Log.cpp
- */
- const char* fstr = getenv("ANDROID_PRINTF_LOG");
- AndroidLogPrintFormat format;
- if (fstr == NULL) {
- format = FORMAT_BRIEF;
- } else {
- if (strcmp(fstr, "brief") == 0)
- format = FORMAT_BRIEF;
- else if (strcmp(fstr, "process") == 0)
- format = FORMAT_PROCESS;
- else if (strcmp(fstr, "tag") == 0)
- format = FORMAT_PROCESS;
- else if (strcmp(fstr, "thread") == 0)
- format = FORMAT_PROCESS;
- else if (strcmp(fstr, "raw") == 0)
- format = FORMAT_PROCESS;
- else if (strcmp(fstr, "time") == 0)
- format = FORMAT_PROCESS;
- else if (strcmp(fstr, "long") == 0)
- format = FORMAT_PROCESS;
- else
- format = (AndroidLogPrintFormat)atoi(fstr); // really?!
- }
-
- log_state.output_format = format;
-}
-
-/*
- * Return a human-readable string for the priority level. Always returns
- * a valid string.
- */
-static const char* getPriorityString(int priority) {
- /* the first character of each string should be unique */
- static const char* priorityStrings[] = {"Verbose", "Debug", "Info", "Warn", "Error", "Assert"};
- int idx;
-
- idx = (int)priority - (int)ANDROID_LOG_VERBOSE;
- if (idx < 0 || idx >= (int)(sizeof(priorityStrings) / sizeof(priorityStrings[0])))
- return "?unknown?";
- return priorityStrings[idx];
-}
-
-#if defined(_WIN32)
-/*
- * WIN32 does not have writev().
- * Make up something to replace it.
- */
-static ssize_t fake_writev(int fd, const struct iovec* iov, int iovcnt) {
- ssize_t result = 0;
- const struct iovec* end = iov + iovcnt;
- for (; iov < end; iov++) {
- ssize_t w = write(fd, iov->iov_base, iov->iov_len);
- if (w != (ssize_t)iov->iov_len) {
- if (w < 0) return w;
- return result + w;
- }
- result += w;
- }
- return result;
-}
-
-#define writev fake_writev
-#endif
-
-/*
- * Write a filtered log message to stderr.
- *
- * Log format parsing taken from the long-dead utils/Log.cpp.
- */
-static void ShowLog(int logPrio, const char* tag, const char* msg) {
-#if !defined(_WIN32)
- struct tm tmBuf;
-#endif
- struct tm* ptm;
- char timeBuf[32];
- char prefixBuf[128], suffixBuf[128];
- char priChar;
- time_t when;
-#if !defined(_WIN32)
- pid_t pid, tid;
-#else
- uint32_t pid, tid;
-#endif
-
- TRACE("LOG %d: %s %s", logPrio, tag, msg);
-
- priChar = getPriorityString(logPrio)[0];
- when = time(NULL);
- pid = tid = getpid(); // find gettid()?
-
-/*
- * Get the current date/time in pretty form
- *
- * It's often useful when examining a log with "less" to jump to
- * a specific point in the file by searching for the date/time stamp.
- * For this reason it's very annoying to have regexp meta characters
- * in the time stamp. Don't use forward slashes, parenthesis,
- * brackets, asterisks, or other special chars here.
- */
-#if !defined(_WIN32)
- ptm = localtime_r(&when, &tmBuf);
-#else
- ptm = localtime(&when);
-#endif
- // strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm);
- strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
-
- /*
- * Construct a buffer containing the log header and log message.
- */
- size_t prefixLen, suffixLen;
-
- switch (log_state.output_format) {
- case FORMAT_TAG:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c/%-8s: ", priChar, tag);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
- break;
- case FORMAT_PROCESS:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d) ", priChar, pid);
- suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), " (%s)\n", tag);
- break;
- case FORMAT_THREAD:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c(%5d:%5d) ", priChar, pid, tid);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
- break;
- case FORMAT_RAW:
- prefixBuf[0] = 0;
- prefixLen = 0;
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
- break;
- case FORMAT_TIME:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%s %-8s\n\t", timeBuf, tag);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
- break;
- case FORMAT_THREADTIME:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%s %5d %5d %c %-8s \n\t", timeBuf, pid,
- tid, priChar, tag);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
- break;
- case FORMAT_LONG:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "[ %s %5d:%5d %c/%-8s ]\n", timeBuf, pid,
- tid, priChar, tag);
- strcpy(suffixBuf, "\n\n");
- suffixLen = 2;
- break;
- default:
- prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "%c/%-8s(%5d): ", priChar, tag, pid);
- strcpy(suffixBuf, "\n");
- suffixLen = 1;
- break;
- }
-
- /*
- * Figure out how many lines there will be.
- */
- const char* end = msg + strlen(msg);
- size_t numLines = 0;
- const char* p = msg;
- while (p < end) {
- if (*p++ == '\n') numLines++;
- }
- if (p > msg && *(p - 1) != '\n') {
- numLines++;
- }
-
- /*
- * Create an array of iovecs large enough to write all of
- * the lines with a prefix and a suffix.
- */
- const size_t INLINE_VECS = 64;
- const size_t MAX_LINES = ((size_t)~0) / (3 * sizeof(struct iovec*));
- struct iovec stackVec[INLINE_VECS];
- struct iovec* vec = stackVec;
- size_t numVecs;
-
- if (numLines > MAX_LINES) numLines = MAX_LINES;
-
- numVecs = numLines * 3; // 3 iovecs per line.
- if (numVecs > INLINE_VECS) {
- vec = (struct iovec*)malloc(sizeof(struct iovec) * numVecs);
- if (vec == NULL) {
- msg = "LOG: write failed, no memory";
- numVecs = INLINE_VECS;
- numLines = numVecs / 3;
- vec = stackVec;
- }
- }
-
- /*
- * Fill in the iovec pointers.
- */
- p = msg;
- struct iovec* v = vec;
- int totalLen = 0;
- while (numLines > 0 && p < end) {
- if (prefixLen > 0) {
- v->iov_base = prefixBuf;
- v->iov_len = prefixLen;
- totalLen += prefixLen;
- v++;
- }
- const char* start = p;
- while (p < end && *p != '\n') {
- p++;
- }
- if ((p - start) > 0) {
- v->iov_base = (void*)start;
- v->iov_len = p - start;
- totalLen += p - start;
- v++;
- }
- if (*p == '\n') p++;
- if (suffixLen > 0) {
- v->iov_base = suffixBuf;
- v->iov_len = suffixLen;
- totalLen += suffixLen;
- v++;
- }
- numLines -= 1;
- }
-
- /*
- * Write the entire message to the log file with a single writev() call.
- * We need to use this rather than a collection of printf()s on a FILE*
- * because of multi-threading and multi-process issues.
- *
- * If the file was not opened with O_APPEND, this will produce interleaved
- * output when called on the same file from multiple processes.
- *
- * If the file descriptor is actually a network socket, the writev()
- * call may return with a partial write. Putting the writev() call in
- * a loop can result in interleaved data. This can be alleviated
- * somewhat by wrapping the writev call in the Mutex.
- */
-
- for (;;) {
- int cc = writev(fileno(stderr), vec, v - vec);
-
- if (cc == totalLen) break;
-
- if (cc < 0) {
- if (errno == EINTR) continue;
-
- /* can't really log the failure; for now, throw out a stderr */
- fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
- break;
- } else {
- /* shouldn't happen when writing to file or tty */
- fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", cc, totalLen);
- break;
- }
- }
-
- /* if we allocated storage for the iovecs, free it */
- if (vec != stackVec) free(vec);
-}
-
-/*
- * Receive a log message. We happen to know that "vector" has three parts:
- *
- * priority (1 byte)
- * tag (N bytes -- null-terminated ASCII string)
- * message (N bytes -- null-terminated ASCII string)
- */
-int FakeWrite(log_id_t log_id, struct timespec*, struct iovec* vector, size_t count) {
- /* Make sure that no-one frees the LogState while we're using it.
- * Also guarantees that only one thread is in showLog() at a given
- * time (if it matters).
- */
- auto lock = std::lock_guard{*fake_log_mutex};
-
- if (!log_state.initialized) {
- InitializeLogStateLocked();
- }
-
- if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS || log_id == LOG_ID_SECURITY) {
- TRACE("%s: ignoring binary log\n", android_log_id_to_name(log_id));
- int len = 0;
- for (size_t i = 0; i < count; ++i) {
- len += vector[i].iov_len;
- }
- return len;
- }
-
- if (count != 3) {
- TRACE("%s: writevLog with count=%d not expected\n", android_log_id_to_name(log_id), count);
- return -1;
- }
-
- /* pull out the three fields */
- int logPrio = *(const char*)vector[0].iov_base;
- const char* tag = (const char*)vector[1].iov_base;
- const char* msg = (const char*)vector[2].iov_base;
-
- /* see if this log tag is configured */
- int minPrio = log_state.global_min_priority;
- for (size_t i = 0; i < kTagSetSize; i++) {
- if (log_state.tagSet[i].minPriority == ANDROID_LOG_UNKNOWN)
- break; /* reached end of configured values */
-
- if (strcmp(log_state.tagSet[i].tag, tag) == 0) {
- minPrio = log_state.tagSet[i].minPriority;
- break;
- }
- }
-
- if (logPrio >= minPrio) {
- ShowLog(logPrio, tag, msg);
- }
-
- int len = 0;
- for (size_t i = 0; i < count; ++i) {
- len += vector[i].iov_len;
- }
- return len;
-}
-
-/*
- * Reset out state.
- *
- * The logger API has no means or need to 'stop' or 'close' using the logs,
- * and as such, there is no way for that 'stop' or 'close' to translate into
- * a close operation to the fake log handler. fakeLogClose is provided for
- * completeness only.
- *
- * We have no intention of adding a log close operation as it would complicate
- * every user of the logging API with no gain since the only valid place to
- * call is in the exit handler. Logging can continue in the exit handler to
- * help debug HOST tools ...
- */
-void FakeClose() {
- auto lock = std::lock_guard{*fake_log_mutex};
-
- memset(&log_state, 0, sizeof(log_state));
-}
-
-int __android_log_is_loggable(int prio, const char*, int) {
- int minimum_priority = __android_log_get_minimum_priority();
- if (minimum_priority == ANDROID_LOG_DEFAULT) {
- minimum_priority = ANDROID_LOG_INFO;
- }
- return prio >= minimum_priority;
-}
-
-int __android_log_is_loggable_len(int prio, const char*, size_t, int def) {
- return __android_log_is_loggable(prio, nullptr, def);
-}
-
-int __android_log_is_debuggable() {
- return 1;
-}
diff --git a/liblog/fake_log_device.h b/liblog/fake_log_device.h
deleted file mode 100644
index 53f1b41..0000000
--- a/liblog/fake_log_device.h
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#pragma once
-
-#include <sys/cdefs.h>
-#include <sys/types.h>
-
-#include <android/log.h>
-
-__BEGIN_DECLS
-
-void FakeClose();
-int FakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec, size_t nr);
-
-int __android_log_is_loggable(int prio, const char*, int def);
-int __android_log_is_loggable_len(int prio, const char*, size_t, int def);
-int __android_log_is_debuggable();
-
-__END_DECLS
diff --git a/liblog/logger_read.cpp b/liblog/logger_read.cpp
index e0598de..a0c526b 100644
--- a/liblog/logger_read.cpp
+++ b/liblog/logger_read.cpp
@@ -97,7 +97,7 @@
int ret = 0;
-#if (FAKE_LOG_DEVICE == 0)
+#ifdef __ANDROID__
if (logger_list->mode & ANDROID_LOG_PSTORE) {
ret = PmsgRead(logger_list, log_msg);
} else {
@@ -135,7 +135,7 @@
return;
}
-#if (FAKE_LOG_DEVICE == 0)
+#ifdef __ANDROID__
if (logger_list->mode & ANDROID_LOG_PSTORE) {
PmsgClose(logger_list);
} else {
diff --git a/liblog/logger_write.cpp b/liblog/logger_write.cpp
index ca68296..cf82e0f 100644
--- a/liblog/logger_write.cpp
+++ b/liblog/logger_write.cpp
@@ -40,11 +40,9 @@
#include "rwlock.h"
#include "uio.h"
-#if (FAKE_LOG_DEVICE == 0)
+#ifdef __ANDROID__
#include "logd_writer.h"
#include "pmsg_writer.h"
-#else
-#include "fake_log_device.h"
#endif
#if defined(__APPLE__)
@@ -105,11 +103,9 @@
* Release any logger resources. A new log write will immediately re-acquire.
*/
void __android_log_close() {
-#if (FAKE_LOG_DEVICE == 0)
+#ifdef __ANDROID__
LogdClose();
PmsgClose();
-#else
- FakeClose();
#endif
}
@@ -311,6 +307,12 @@
void __android_log_write_logger_data(__android_logger_data* logger_data, const char* msg) {
ErrnoRestorer errno_restorer;
+ if (logger_data->buffer_id != LOG_ID_DEFAULT && logger_data->buffer_id != LOG_ID_MAIN &&
+ logger_data->buffer_id != LOG_ID_SYSTEM && logger_data->buffer_id != LOG_ID_RADIO &&
+ logger_data->buffer_id != LOG_ID_CRASH) {
+ return;
+ }
+
auto tag_lock = std::shared_lock{default_tag_lock, std::defer_lock};
if (logger_data->tag == nullptr) {
tag_lock.lock();
diff --git a/liblog/logprint.cpp b/liblog/logprint.cpp
index 0745a1e..e32878a 100644
--- a/liblog/logprint.cpp
+++ b/liblog/logprint.cpp
@@ -1520,12 +1520,7 @@
* This code is Android specific, bionic guarantees that
* calls to non-reentrant getpwuid() are thread safe.
*/
-#if !defined(__MINGW32__)
-#if (FAKE_LOG_DEVICE == 0)
-#ifndef __BIONIC__
-#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
-#endif
-#endif
+#ifdef __ANDROID__
struct passwd* pwd = getpwuid(entry->uid);
if (pwd && (strlen(pwd->pw_name) <= 5)) {
snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
diff --git a/liblog/properties.cpp b/liblog/properties.cpp
index b13662f..f30058a 100644
--- a/liblog/properties.cpp
+++ b/liblog/properties.cpp
@@ -20,8 +20,6 @@
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
-#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
-#include <sys/_system_properties.h>
#include <unistd.h>
#include <algorithm>
@@ -31,6 +29,10 @@
#include "logger_write.h"
+#ifdef __ANDROID__
+#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
+#include <sys/_system_properties.h>
+
static pthread_mutex_t lock_loggable = PTHREAD_MUTEX_INITIALIZER;
static int lock() {
@@ -647,3 +649,23 @@
return property_size;
}
+
+#else
+
+int __android_log_is_loggable(int prio, const char*, int) {
+ int minimum_priority = __android_log_get_minimum_priority();
+ if (minimum_priority == ANDROID_LOG_DEFAULT) {
+ minimum_priority = ANDROID_LOG_INFO;
+ }
+ return prio >= minimum_priority;
+}
+
+int __android_log_is_loggable_len(int prio, const char*, size_t, int def) {
+ return __android_log_is_loggable(prio, nullptr, def);
+}
+
+int __android_log_is_debuggable() {
+ return 1;
+}
+
+#endif
\ No newline at end of file
diff --git a/liblog/tests/Android.bp b/liblog/tests/Android.bp
index b3364f9..b4bb77f 100644
--- a/liblog/tests/Android.bp
+++ b/liblog/tests/Android.bp
@@ -104,6 +104,11 @@
name: "liblog-host-test",
static_libs: ["liblog"],
shared_libs: ["libbase"],
- srcs: ["liblog_host_test.cpp"],
+ srcs: [
+ "liblog_host_test.cpp",
+ "liblog_default_tag.cpp",
+ "liblog_global_state.cpp",
+ "rwlock_test.cpp",
+ ],
isolated: true,
}
diff --git a/liblog/tests/liblog_default_tag.cpp b/liblog/tests/liblog_default_tag.cpp
index 5643f63..31b7467 100644
--- a/liblog/tests/liblog_default_tag.cpp
+++ b/liblog/tests/liblog_default_tag.cpp
@@ -22,10 +22,17 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
+#include <android-base/scopeguard.h>
#include <android/log.h>
#include <gtest/gtest.h>
+#ifndef __ANDROID__
+static const char* getprogname() {
+ return program_invocation_short_name;
+}
+#endif
+
TEST(liblog_default_tag, no_default_tag_libbase_write_first) {
using namespace android::base;
bool message_seen = false;
@@ -99,6 +106,7 @@
}
TEST(liblog_default_tag, default_tag_plus_log_severity) {
+#ifdef __ANDROID__
using namespace android::base;
bool message_seen = false;
std::string expected_tag = "liblog_test_tag";
@@ -110,6 +118,7 @@
auto log_tag_property = "log.tag." + expected_tag;
SetProperty(log_tag_property, "V");
+ auto reset_tag_property_guard = make_scope_guard([=] { SetProperty(log_tag_property, ""); });
__android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_VERBOSE, nullptr, "message");
EXPECT_TRUE(message_seen);
@@ -117,9 +126,13 @@
LOG(VERBOSE) << "message";
EXPECT_TRUE(message_seen);
+#else
+ GTEST_SKIP() << "No log tag properties on host";
+#endif
}
TEST(liblog_default_tag, generated_default_tag_plus_log_severity) {
+#ifdef __ANDROID__
using namespace android::base;
bool message_seen = false;
std::string expected_tag = getprogname();
@@ -133,6 +146,7 @@
// case checks that we can log a Verbose message when log.tag.<getprogname()> is set to 'V'.
auto log_tag_property = "log.tag." + expected_tag;
SetProperty(log_tag_property, "V");
+ auto reset_tag_property_guard = make_scope_guard([=] { SetProperty(log_tag_property, ""); });
__android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_VERBOSE, nullptr, "message");
EXPECT_TRUE(message_seen);
@@ -140,4 +154,7 @@
LOG(VERBOSE) << "message";
EXPECT_TRUE(message_seen);
+#else
+ GTEST_SKIP() << "No log tag properties on host";
+#endif
}
\ No newline at end of file
diff --git a/liblog/tests/liblog_global_state.cpp b/liblog/tests/liblog_global_state.cpp
index 8d73bb8..9a181ef 100644
--- a/liblog/tests/liblog_global_state.cpp
+++ b/liblog/tests/liblog_global_state.cpp
@@ -188,6 +188,7 @@
}
TEST(liblog_global_state, is_loggable_tag_log_priority_only) {
+#ifdef __ANDROID__
EXPECT_EQ(0, __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO));
EXPECT_EQ(1, __android_log_is_loggable(ANDROID_LOG_INFO, LOG_TAG, ANDROID_LOG_INFO));
EXPECT_EQ(1, __android_log_is_loggable(ANDROID_LOG_WARN, LOG_TAG, ANDROID_LOG_INFO));
@@ -204,9 +205,13 @@
EXPECT_EQ(1, __android_log_is_loggable(ANDROID_LOG_WARN, LOG_TAG, ANDROID_LOG_INFO));
android::base::SetProperty(log_tag_property, "");
+#else
+ GTEST_SKIP() << "No log tag properties on host";
+#endif
}
TEST(liblog_global_state, is_loggable_both_set) {
+#ifdef __ANDROID__
EXPECT_EQ(0, __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO));
EXPECT_EQ(1, __android_log_is_loggable(ANDROID_LOG_INFO, LOG_TAG, ANDROID_LOG_INFO));
EXPECT_EQ(1, __android_log_is_loggable(ANDROID_LOG_WARN, LOG_TAG, ANDROID_LOG_INFO));
@@ -240,4 +245,7 @@
EXPECT_EQ(1, __android_log_is_loggable(ANDROID_LOG_WARN, LOG_TAG, ANDROID_LOG_INFO));
android::base::SetProperty(log_tag_property, "");
+#else
+ GTEST_SKIP() << "No log tag properties on host";
+#endif
}
diff --git a/liblog/tests/liblog_host_test.cpp b/liblog/tests/liblog_host_test.cpp
index 377550f..ec186d4 100644
--- a/liblog/tests/liblog_host_test.cpp
+++ b/liblog/tests/liblog_host_test.cpp
@@ -20,15 +20,37 @@
#include <stdlib.h>
#include <unistd.h>
+#include <regex>
+#include <string>
+
+#include <android-base/logging.h>
+#include <android-base/macros.h>
#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
#include <android-base/test_utils.h>
#include <gtest/gtest.h>
+using android::base::InitLogging;
+using android::base::StderrLogger;
using android::base::StringPrintf;
-using android::base::StringReplace;
-void GenerateLogContent() {
+static std::string MakeLogPattern(int priority, const char* tag, const char* message) {
+ static const char log_characters[] = "XXVDIWEF";
+ static_assert(arraysize(log_characters) - 1 == ANDROID_LOG_SILENT,
+ "Mismatch in size of log_characters and values in android_LogPriority");
+ priority = priority > ANDROID_LOG_SILENT ? ANDROID_LOG_FATAL : priority;
+ char log_char = log_characters[priority];
+
+ return StringPrintf("%s %c \\d+-\\d+ \\d+:\\d+:\\d+ \\s*\\d+ \\s*\\d+ %s", tag, log_char,
+ message);
+}
+
+static void CheckMessage(bool expected, const std::string& output, int priority, const char* tag,
+ const char* message) {
+ std::regex message_regex(MakeLogPattern(priority, tag, message));
+ EXPECT_EQ(expected, std::regex_search(output, message_regex)) << message;
+}
+
+static void GenerateLogContent() {
__android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_VERBOSE, "tag", "verbose main");
__android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, "tag", "info main");
__android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_ERROR, "tag", "error main");
@@ -52,137 +74,86 @@
}
TEST(liblog, default_write) {
- setenv("ANDROID_PRINTF_LOG", "brief", true);
CapturedStderr captured_stderr;
+ InitLogging(nullptr, StderrLogger);
GenerateLogContent();
- std::string expected_output = StringReplace(R"init(I/tag (<pid>): info main
-E/tag (<pid>): error main
-I/tag (<pid>): info radio
-E/tag (<pid>): error radio
-I/tag (<pid>): info system
-E/tag (<pid>): error system
-I/tag (<pid>): info crash
-E/tag (<pid>): error crash
-)init",
- "<pid>", GetPidString(), true);
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose main");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info main");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error main");
- EXPECT_EQ(expected_output, captured_stderr.str());
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose radio");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info radio");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error radio");
+
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose system");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info system");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error system");
+
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose crash");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info crash");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error crash");
}
-TEST(liblog, format) {
- setenv("ANDROID_PRINTF_LOG", "process", true);
+TEST(liblog, verbose_write) {
+ setenv("ANDROID_LOG_TAGS", "*:v", true);
CapturedStderr captured_stderr;
+ InitLogging(nullptr, StderrLogger);
GenerateLogContent();
- std::string expected_output = StringReplace(R"init(I(<pid>) info main (tag)
-E(<pid>) error main (tag)
-I(<pid>) info radio (tag)
-E(<pid>) error radio (tag)
-I(<pid>) info system (tag)
-E(<pid>) error system (tag)
-I(<pid>) info crash (tag)
-E(<pid>) error crash (tag)
-)init",
- "<pid>", GetPidString(), true);
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose main");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info main");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error main");
- EXPECT_EQ(expected_output, captured_stderr.str());
- captured_stderr.Stop();
- captured_stderr.Reset();
- captured_stderr.Start();
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose radio");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info radio");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error radio");
- // Changing the environment after starting writing doesn't change the format.
- setenv("ANDROID_PRINTF_LOG", "brief", true);
- GenerateLogContent();
- EXPECT_EQ(expected_output, captured_stderr.str());
- captured_stderr.Stop();
- captured_stderr.Reset();
- captured_stderr.Start();
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose system");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info system");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error system");
- // However calling __android_log_close() does reset logging and allow changing the format.
- __android_log_close();
- GenerateLogContent();
-
- expected_output = StringReplace(R"init(I/tag (<pid>): info main
-E/tag (<pid>): error main
-I/tag (<pid>): info radio
-E/tag (<pid>): error radio
-I/tag (<pid>): info system
-E/tag (<pid>): error system
-I/tag (<pid>): info crash
-E/tag (<pid>): error crash
-)init",
- "<pid>", GetPidString(), true);
-
- EXPECT_EQ(expected_output, captured_stderr.str());
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose crash");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info crash");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error crash");
}
-TEST(liblog, filter) {
- setenv("ANDROID_PRINTF_LOG", "brief", true);
- setenv("ANDROID_LOG_TAGS", "*:w verbose_tag:v debug_tag:d", true);
- CapturedStderr captured_stderr;
-
- auto generate_logs = [](log_id_t log_id) {
- // Check that we show verbose logs when requesting for a given tag.
- __android_log_buf_print(log_id, ANDROID_LOG_VERBOSE, "verbose_tag", "verbose verbose_tag");
- __android_log_buf_print(log_id, ANDROID_LOG_ERROR, "verbose_tag", "error verbose_tag");
-
- // Check that we don't show verbose logs when explicitly requesting debug+ for a given tag.
- __android_log_buf_print(log_id, ANDROID_LOG_VERBOSE, "debug_tag", "verbose debug_tag");
- __android_log_buf_print(log_id, ANDROID_LOG_DEBUG, "debug_tag", "debug debug_tag");
- __android_log_buf_print(log_id, ANDROID_LOG_ERROR, "debug_tag", "error debug_tag");
-
- // Check that we don't show info logs when requesting globally warn+.
- __android_log_buf_print(log_id, ANDROID_LOG_INFO, "default_tag", "info default_tag");
- __android_log_buf_print(log_id, ANDROID_LOG_WARN, "default_tag", "warn default_tag");
- __android_log_buf_print(log_id, ANDROID_LOG_ERROR, "default_tag", "error default_tag");
- };
-
- auto expected_output = StringReplace(R"init(V/verbose_tag(<pid>): verbose verbose_tag
-E/verbose_tag(<pid>): error verbose_tag
-D/debug_tag(<pid>): debug debug_tag
-E/debug_tag(<pid>): error debug_tag
-W/default_tag(<pid>): warn default_tag
-E/default_tag(<pid>): error default_tag
-)init",
- "<pid>", GetPidString(), true);
-
- auto test_all_logs = [&] {
- for (auto log_id : {LOG_ID_MAIN, LOG_ID_SYSTEM, LOG_ID_RADIO, LOG_ID_CRASH}) {
- generate_logs(log_id);
- EXPECT_EQ(expected_output, captured_stderr.str());
- captured_stderr.Stop();
- captured_stderr.Reset();
- captured_stderr.Start();
- }
- };
-
- test_all_logs();
-
- // Changing the environment after starting writing doesn't change the filter.
+TEST(liblog, error_write) {
setenv("ANDROID_LOG_TAGS", "*:e", true);
- test_all_logs();
+ CapturedStderr captured_stderr;
+ InitLogging(nullptr, StderrLogger);
- // However calling __android_log_close() does reset logging and allow changing the format.
- __android_log_close();
- expected_output = StringReplace(R"init(E/verbose_tag(<pid>): error verbose_tag
-E/debug_tag(<pid>): error debug_tag
-E/default_tag(<pid>): error default_tag
-)init",
- "<pid>", GetPidString(), true);
- test_all_logs();
+ GenerateLogContent();
+
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose main");
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info main");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error main");
+
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose radio");
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info radio");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error radio");
+
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose system");
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info system");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error system");
+
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose crash");
+ CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info crash");
+ CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error crash");
}
TEST(liblog, kernel_no_write) {
CapturedStderr captured_stderr;
+ InitLogging(nullptr, StderrLogger);
__android_log_buf_print(LOG_ID_KERNEL, ANDROID_LOG_ERROR, "tag", "kernel error");
EXPECT_EQ("", captured_stderr.str());
}
TEST(liblog, binary_no_write) {
CapturedStderr captured_stderr;
+ InitLogging(nullptr, StderrLogger);
__android_log_buf_print(LOG_ID_EVENTS, ANDROID_LOG_ERROR, "tag", "error events");
__android_log_buf_print(LOG_ID_STATS, ANDROID_LOG_ERROR, "tag", "error stats");
__android_log_buf_print(LOG_ID_SECURITY, ANDROID_LOG_ERROR, "tag", "error security");
diff --git a/libprocessgroup/cgroup_map.cpp b/libprocessgroup/cgroup_map.cpp
index 20ae2be..2fc920b 100644
--- a/libprocessgroup/cgroup_map.cpp
+++ b/libprocessgroup/cgroup_map.cpp
@@ -71,7 +71,12 @@
if (!HasValue()) return false;
if (state_ == UNKNOWN) {
- state_ = access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0 ? USABLE : MISSING;
+ if (ACgroupController_getFlags != nullptr) {
+ uint32_t flags = ACgroupController_getFlags(controller_);
+ state_ = (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0 ? USABLE : MISSING;
+ } else {
+ state_ = access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0 ? USABLE : MISSING;
+ }
}
return state_ == USABLE;
@@ -161,9 +166,16 @@
auto controller_count = ACgroupFile_getControllerCount();
for (uint32_t i = 0; i < controller_count; ++i) {
const ACgroupController* controller = ACgroupFile_getController(i);
- LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
- << ACgroupController_getVersion(controller) << " path "
- << ACgroupController_getPath(controller);
+ if (ACgroupController_getFlags != nullptr) {
+ LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
+ << ACgroupController_getVersion(controller) << " path "
+ << ACgroupController_getPath(controller) << " flags "
+ << ACgroupController_getFlags(controller);
+ } else {
+ LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
+ << ACgroupController_getVersion(controller) << " path "
+ << ACgroupController_getPath(controller);
+ }
}
}
diff --git a/libprocessgroup/cgrouprc/cgroup_controller.cpp b/libprocessgroup/cgrouprc/cgroup_controller.cpp
index d064d31..5a326e5 100644
--- a/libprocessgroup/cgrouprc/cgroup_controller.cpp
+++ b/libprocessgroup/cgrouprc/cgroup_controller.cpp
@@ -27,6 +27,11 @@
return controller->version();
}
+uint32_t ACgroupController_getFlags(const ACgroupController* controller) {
+ CHECK(controller != nullptr);
+ return controller->flags();
+}
+
const char* ACgroupController_getName(const ACgroupController* controller) {
CHECK(controller != nullptr);
return controller->name();
diff --git a/libprocessgroup/cgrouprc/include/android/cgrouprc.h b/libprocessgroup/cgrouprc/include/android/cgrouprc.h
index 0f6a9cd..0ce5123 100644
--- a/libprocessgroup/cgrouprc/include/android/cgrouprc.h
+++ b/libprocessgroup/cgrouprc/include/android/cgrouprc.h
@@ -66,10 +66,21 @@
__INTRODUCED_IN(29);
/**
- * Flag bitmask to be used when ACgroupController_getFlags can be exported
+ * Flag bitmask used in ACgroupController_getFlags
*/
#define CGROUPRC_CONTROLLER_FLAG_MOUNTED 0x1
+#if __ANDROID_API__ >= __ANDROID_API_R__
+
+/**
+ * Returns the flags bitmask of the given controller.
+ * If the given controller is null, return 0.
+ */
+__attribute__((warn_unused_result, weak)) uint32_t ACgroupController_getFlags(
+ const ACgroupController*) __INTRODUCED_IN(30);
+
+#endif
+
/**
* Returns the name of the given controller.
* If the given controller is null, return nullptr.
diff --git a/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt b/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt
index 91df392..ce5c419 100644
--- a/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt
+++ b/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt
@@ -9,3 +9,10 @@
local:
*;
};
+
+LIBCGROUPRC_30 { # introduced=30
+ global:
+ ACgroupController_getFlags;
+ local:
+ *;
+};
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 7f4f025..5f4b8c3 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -489,6 +489,7 @@
on late-fs
# Ensure that tracefs has the correct permissions.
# This does not work correctly if it is called in post-fs.
+ chmod 0755 /sys/kernel/tracing
chmod 0755 /sys/kernel/debug/tracing
# HALs required before storage encryption can get unlocked (FBE/FDE)
diff --git a/set-verity-state/Android.bp b/set-verity-state/Android.bp
index cd8c8c5..da112c9 100644
--- a/set-verity-state/Android.bp
+++ b/set-verity-state/Android.bp
@@ -9,8 +9,9 @@
"libcrypto_utils",
"libcutils",
"libfec",
- "libfs_mgr",
+ "libfs_mgr_binder",
"liblog",
+ "libutils",
],
static_libs: [
"libavb_user",