Merge "init: add tips for debugging linker errors when launching init services"
diff --git a/debuggerd/tombstoned/tombstoned.cpp b/debuggerd/tombstoned/tombstoned.cpp
index bbeb181..d09b8e8 100644
--- a/debuggerd/tombstoned/tombstoned.cpp
+++ b/debuggerd/tombstoned/tombstoned.cpp
@@ -100,7 +100,7 @@
static CrashQueue* for_tombstones() {
static CrashQueue queue("/data/tombstones", "tombstone_" /* file_name_prefix */,
- GetIntProperty("tombstoned.max_tombstone_count", 10),
+ GetIntProperty("tombstoned.max_tombstone_count", 32),
1 /* max_concurrent_dumps */);
return &queue;
}
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index 7a0d019..4ba1c49 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -222,13 +222,11 @@
} else {
LINFO << "Running " << E2FSCK_BIN << " on " << realpath(blk_device);
if (should_force_check(*fs_stat)) {
- ret = android_fork_execvp_ext(
- ARRAY_SIZE(e2fsck_forced_argv), const_cast<char**>(e2fsck_forced_argv), &status,
- true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
+ ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_forced_argv), e2fsck_forced_argv,
+ &status, false, LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
} else {
- ret = android_fork_execvp_ext(
- ARRAY_SIZE(e2fsck_argv), const_cast<char**>(e2fsck_argv), &status, true,
- LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
+ ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, &status, false,
+ LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
}
if (ret < 0) {
@@ -246,14 +244,12 @@
if (should_force_check(*fs_stat)) {
LINFO << "Running " << F2FS_FSCK_BIN << " -f " << realpath(blk_device);
- ret = android_fork_execvp_ext(
- ARRAY_SIZE(f2fs_fsck_forced_argv), const_cast<char**>(f2fs_fsck_forced_argv), &status,
- true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
+ ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_forced_argv), f2fs_fsck_forced_argv,
+ &status, false, LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
} else {
LINFO << "Running " << F2FS_FSCK_BIN << " -a " << realpath(blk_device);
- ret = android_fork_execvp_ext(
- ARRAY_SIZE(f2fs_fsck_argv), const_cast<char**>(f2fs_fsck_argv), &status, true,
- LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
+ ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv, &status, false,
+ LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
}
if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */
@@ -331,8 +327,7 @@
static bool run_tune2fs(const char* argv[], int argc) {
int ret;
- ret = android_fork_execvp_ext(argc, const_cast<char**>(argv), nullptr, true,
- LOG_KLOG | LOG_FILE, true, nullptr, nullptr, 0);
+ ret = logwrap_fork_execvp(argc, argv, nullptr, false, LOG_KLOG, true, nullptr);
return ret == 0;
}
@@ -852,37 +847,22 @@
}
}
-static bool call_vdc(const std::vector<std::string>& args) {
+static bool call_vdc(const std::vector<std::string>& args, int* ret) {
std::vector<char const*> argv;
argv.emplace_back("/system/bin/vdc");
for (auto& arg : args) {
argv.emplace_back(arg.c_str());
}
LOG(INFO) << "Calling: " << android::base::Join(argv, ' ');
- int ret =
- android_fork_execvp(argv.size(), const_cast<char**>(argv.data()), nullptr, false, true);
- if (ret != 0) {
- LOG(ERROR) << "vdc returned error code: " << ret;
- return false;
- }
- LOG(DEBUG) << "vdc finished successfully";
- return true;
-}
-
-static bool call_vdc_ret(const std::vector<std::string>& args, int* ret) {
- std::vector<char const*> argv;
- argv.emplace_back("/system/bin/vdc");
- for (auto& arg : args) {
- argv.emplace_back(arg.c_str());
- }
- LOG(INFO) << "Calling: " << android::base::Join(argv, ' ');
- int err = android_fork_execvp(argv.size(), const_cast<char**>(argv.data()), ret, false, true);
+ int err = logwrap_fork_execvp(argv.size(), argv.data(), ret, false, LOG_ALOG, false, nullptr);
if (err != 0) {
LOG(ERROR) << "vdc call failed with error code: " << err;
return false;
}
LOG(DEBUG) << "vdc finished successfully";
- *ret = WEXITSTATUS(*ret);
+ if (ret != nullptr) {
+ *ret = WEXITSTATUS(*ret);
+ }
return true;
}
@@ -914,11 +894,11 @@
}
if (entry->fs_mgr_flags.checkpoint_blk) {
- call_vdc({"checkpoint", "restoreCheckpoint", entry->blk_device});
+ call_vdc({"checkpoint", "restoreCheckpoint", entry->blk_device}, nullptr);
}
if (needs_checkpoint_ == UNKNOWN &&
- !call_vdc_ret({"checkpoint", "needsCheckpoint"}, &needs_checkpoint_)) {
+ !call_vdc({"checkpoint", "needsCheckpoint"}, &needs_checkpoint_)) {
LERROR << "Failed to find if checkpointing is needed. Assuming no.";
needs_checkpoint_ = NO;
}
@@ -1193,7 +1173,8 @@
encryptable = status;
if (status == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
if (!call_vdc({"cryptfs", "encryptFstab", attempted_entry.blk_device,
- attempted_entry.mount_point})) {
+ attempted_entry.mount_point},
+ nullptr)) {
LERROR << "Encryption failed";
return FS_MGR_MNTALL_FAIL;
}
@@ -1265,7 +1246,8 @@
} else if (mount_errno != EBUSY && mount_errno != EACCES &&
should_use_metadata_encryption(attempted_entry)) {
if (!call_vdc({"cryptfs", "mountFstab", attempted_entry.blk_device,
- attempted_entry.mount_point})) {
+ attempted_entry.mount_point},
+ nullptr)) {
++error_count;
}
encryptable = FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED;
@@ -1615,10 +1597,8 @@
MKSWAP_BIN,
entry.blk_device.c_str(),
};
- int err = 0;
- int status;
- err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), const_cast<char**>(mkswap_argv),
- &status, true, LOG_KLOG, false, nullptr, nullptr, 0);
+ int err = logwrap_fork_execvp(ARRAY_SIZE(mkswap_argv), mkswap_argv, nullptr, false,
+ LOG_KLOG, false, nullptr);
if (err) {
LERROR << "mkswap failed for " << entry.blk_device;
ret = false;
diff --git a/fs_mgr/fs_mgr_format.cpp b/fs_mgr/fs_mgr_format.cpp
index 1c6652a..acf4d7b 100644
--- a/fs_mgr/fs_mgr_format.cpp
+++ b/fs_mgr/fs_mgr_format.cpp
@@ -76,8 +76,8 @@
"/system/bin/mke2fs", "-t", "ext4", "-b", "4096", fs_blkdev.c_str(),
size_str.c_str(), nullptr};
- rc = android_fork_execvp_ext(arraysize(mke2fs_args), const_cast<char**>(mke2fs_args), NULL,
- true, LOG_KLOG, true, nullptr, nullptr, 0);
+ rc = logwrap_fork_execvp(arraysize(mke2fs_args), mke2fs_args, nullptr, false, LOG_KLOG, true,
+ nullptr);
if (rc) {
LERROR << "mke2fs returned " << rc;
return rc;
@@ -86,8 +86,8 @@
const char* const e2fsdroid_args[] = {
"/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr};
- rc = android_fork_execvp_ext(arraysize(e2fsdroid_args), const_cast<char**>(e2fsdroid_args),
- NULL, true, LOG_KLOG, true, nullptr, nullptr, 0);
+ rc = logwrap_fork_execvp(arraysize(e2fsdroid_args), e2fsdroid_args, nullptr, false, LOG_KLOG,
+ true, nullptr);
if (rc) {
LERROR << "e2fsdroid returned " << rc;
}
@@ -119,8 +119,7 @@
};
// clang-format on
- return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true,
- LOG_KLOG, true, nullptr, nullptr, 0);
+ return logwrap_fork_execvp(arraysize(args), args, nullptr, false, LOG_KLOG, true, nullptr);
}
int fs_mgr_do_format(const FstabEntry& entry, bool crypt_footer) {
diff --git a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
index 0dd275a..0d6aa2c 100644
--- a/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
+++ b/fs_mgr/libsnapshot/include/libsnapshot/snapshot.h
@@ -406,6 +406,20 @@
// |needs_merge| is set to true.
bool TryCancelUpdate(bool* needs_merge);
+ // Helper for CreateUpdateSnapshots.
+ // Creates all underlying images, COW partitions and snapshot files. Does not initialize them.
+ bool CreateUpdateSnapshotsInternal(LockedFile* lock, const DeltaArchiveManifest& manifest,
+ PartitionCowCreator* cow_creator,
+ AutoDeviceList* created_devices,
+ std::map<std::string, SnapshotStatus>* all_snapshot_status);
+
+ // Initialize snapshots so that they can be mapped later.
+ // Map the COW partition and zero-initialize the header.
+ bool InitializeUpdateSnapshots(
+ LockedFile* lock, MetadataBuilder* target_metadata,
+ const LpMetadata* exported_target_metadata, const std::string& target_suffix,
+ const std::map<std::string, SnapshotStatus>& all_snapshot_status);
+
std::string gsid_dir_;
std::string metadata_dir_;
std::unique_ptr<IDeviceInfo> device_;
diff --git a/fs_mgr/libsnapshot/snapshot.cpp b/fs_mgr/libsnapshot/snapshot.cpp
index 2e689bd..0200077 100644
--- a/fs_mgr/libsnapshot/snapshot.cpp
+++ b/fs_mgr/libsnapshot/snapshot.cpp
@@ -1761,6 +1761,15 @@
auto lock = LockExclusive();
if (!lock) return false;
+ // TODO(b/134949511): remove this check. Right now, with overlayfs mounted, the scratch
+ // partition takes up a big chunk of space in super, causing COW images to be created on
+ // retrofit Virtual A/B devices.
+ if (device_->IsOverlayfsSetup()) {
+ LOG(ERROR) << "Cannot create update snapshots with overlayfs setup. Run `adb enable-verity`"
+ << ", reboot, then try again.";
+ return false;
+ }
+
const auto& opener = device_->GetPartitionOpener();
auto current_suffix = device_->GetSlotSuffix();
uint32_t current_slot = SlotNumberForSlotSuffix(current_suffix);
@@ -1778,32 +1787,6 @@
return false;
}
- if (!target_metadata->AddGroup(kCowGroupName, 0)) {
- LOG(ERROR) << "Cannot add group " << kCowGroupName;
- return false;
- }
-
- std::map<std::string, const RepeatedPtrField<InstallOperation>*> install_operation_map;
- for (const auto& partition_update : manifest.partitions()) {
- auto suffixed_name = partition_update.partition_name() + target_suffix;
- auto&& [it, inserted] = install_operation_map.emplace(std::move(suffixed_name),
- &partition_update.operations());
- if (!inserted) {
- LOG(ERROR) << "Duplicated partition " << partition_update.partition_name()
- << " in update manifest.";
- return false;
- }
- }
-
- // TODO(b/134949511): remove this check. Right now, with overlayfs mounted, the scratch
- // partition takes up a big chunk of space in super, causing COW images to be created on
- // retrofit Virtual A/B devices.
- if (device_->IsOverlayfsSetup()) {
- LOG(ERROR) << "Cannot create update snapshots with overlayfs setup. Run `adb enable-verity`"
- << ", reboot, then try again.";
- return false;
- }
-
// Delete previous COW partitions in current_metadata so that PartitionCowCreator marks those as
// free regions.
UnmapAndDeleteCowPartition(current_metadata.get());
@@ -1823,17 +1806,78 @@
// these devices.
AutoDeviceList created_devices;
- for (auto* target_partition : ListPartitionsWithSuffix(target_metadata.get(), target_suffix)) {
- const RepeatedPtrField<InstallOperation>* operations = nullptr;
+ PartitionCowCreator cow_creator{.target_metadata = target_metadata.get(),
+ .target_suffix = target_suffix,
+ .target_partition = nullptr,
+ .current_metadata = current_metadata.get(),
+ .current_suffix = current_suffix,
+ .operations = nullptr};
+
+ if (!CreateUpdateSnapshotsInternal(lock.get(), manifest, &cow_creator, &created_devices,
+ &all_snapshot_status)) {
+ return false;
+ }
+
+ auto exported_target_metadata = target_metadata->Export();
+ if (exported_target_metadata == nullptr) {
+ LOG(ERROR) << "Cannot export target metadata";
+ return false;
+ }
+
+ if (!InitializeUpdateSnapshots(lock.get(), target_metadata.get(),
+ exported_target_metadata.get(), target_suffix,
+ all_snapshot_status)) {
+ return false;
+ }
+
+ if (!UpdatePartitionTable(opener, device_->GetSuperDevice(target_slot),
+ *exported_target_metadata, target_slot)) {
+ LOG(ERROR) << "Cannot write target metadata";
+ return false;
+ }
+
+ created_devices.Release();
+ LOG(INFO) << "Successfully created all snapshots for target slot " << target_suffix;
+
+ return true;
+}
+
+bool SnapshotManager::CreateUpdateSnapshotsInternal(
+ LockedFile* lock, const DeltaArchiveManifest& manifest, PartitionCowCreator* cow_creator,
+ AutoDeviceList* created_devices,
+ std::map<std::string, SnapshotStatus>* all_snapshot_status) {
+ CHECK(lock);
+
+ auto* target_metadata = cow_creator->target_metadata;
+ const auto& target_suffix = cow_creator->target_suffix;
+
+ if (!target_metadata->AddGroup(kCowGroupName, 0)) {
+ LOG(ERROR) << "Cannot add group " << kCowGroupName;
+ return false;
+ }
+
+ std::map<std::string, const RepeatedPtrField<InstallOperation>*> install_operation_map;
+ for (const auto& partition_update : manifest.partitions()) {
+ auto suffixed_name = partition_update.partition_name() + target_suffix;
+ auto&& [it, inserted] = install_operation_map.emplace(std::move(suffixed_name),
+ &partition_update.operations());
+ if (!inserted) {
+ LOG(ERROR) << "Duplicated partition " << partition_update.partition_name()
+ << " in update manifest.";
+ return false;
+ }
+ }
+
+ for (auto* target_partition : ListPartitionsWithSuffix(target_metadata, target_suffix)) {
+ cow_creator->target_partition = target_partition;
+ cow_creator->operations = nullptr;
auto operations_it = install_operation_map.find(target_partition->name());
if (operations_it != install_operation_map.end()) {
- operations = operations_it->second;
+ cow_creator->operations = operations_it->second;
}
// Compute the device sizes for the partition.
- PartitionCowCreator cow_creator{target_metadata.get(), target_suffix, target_partition,
- current_metadata.get(), current_suffix, operations};
- auto cow_creator_ret = cow_creator.Run();
+ auto cow_creator_ret = cow_creator->Run();
if (!cow_creator_ret.has_value()) {
return false;
}
@@ -1846,7 +1890,7 @@
<< ", cow file size = " << cow_creator_ret->snapshot_status.cow_file_size;
// Delete any existing snapshot before re-creating one.
- if (!DeleteSnapshot(lock.get(), target_partition->name())) {
+ if (!DeleteSnapshot(lock, target_partition->name())) {
LOG(ERROR) << "Cannot delete existing snapshot before creating a new one for partition "
<< target_partition->name();
return false;
@@ -1866,11 +1910,10 @@
}
// Store these device sizes to snapshot status file.
- if (!CreateSnapshot(lock.get(), target_partition->name(),
- cow_creator_ret->snapshot_status)) {
+ if (!CreateSnapshot(lock, target_partition->name(), cow_creator_ret->snapshot_status)) {
return false;
}
- created_devices.EmplaceBack<AutoDeleteSnapshot>(this, lock.get(), target_partition->name());
+ created_devices->EmplaceBack<AutoDeleteSnapshot>(this, lock, target_partition->name());
// Create the COW partition. That is, use any remaining free space in super partition before
// creating the COW images.
@@ -1898,32 +1941,36 @@
// Create the backing COW image if necessary.
if (cow_creator_ret->snapshot_status.cow_file_size > 0) {
- if (!CreateCowImage(lock.get(), target_partition->name())) {
+ if (!CreateCowImage(lock, target_partition->name())) {
return false;
}
}
- all_snapshot_status[target_partition->name()] = std::move(cow_creator_ret->snapshot_status);
+ all_snapshot_status->emplace(target_partition->name(),
+ std::move(cow_creator_ret->snapshot_status));
LOG(INFO) << "Successfully created snapshot for " << target_partition->name();
}
+ return true;
+}
+
+bool SnapshotManager::InitializeUpdateSnapshots(
+ LockedFile* lock, MetadataBuilder* target_metadata,
+ const LpMetadata* exported_target_metadata, const std::string& target_suffix,
+ const std::map<std::string, SnapshotStatus>& all_snapshot_status) {
+ CHECK(lock);
auto& dm = DeviceMapper::Instance();
- auto exported_target_metadata = target_metadata->Export();
- if (exported_target_metadata == nullptr) {
- LOG(ERROR) << "Cannot export target metadata";
- return false;
- }
CreateLogicalPartitionParams cow_params{
.block_device = LP_METADATA_DEFAULT_PARTITION_NAME,
- .metadata = exported_target_metadata.get(),
+ .metadata = exported_target_metadata,
.timeout_ms = std::chrono::milliseconds::max(),
.partition_opener = &device_->GetPartitionOpener(),
};
- for (auto* target_partition : ListPartitionsWithSuffix(target_metadata.get(), target_suffix)) {
+ for (auto* target_partition : ListPartitionsWithSuffix(target_metadata, target_suffix)) {
AutoDeviceList created_devices_for_cow;
- if (!UnmapPartitionWithSnapshot(lock.get(), target_partition->name())) {
+ if (!UnmapPartitionWithSnapshot(lock, target_partition->name())) {
LOG(ERROR) << "Cannot unmap existing COW devices before re-mapping them for zero-fill: "
<< target_partition->name();
return false;
@@ -1933,8 +1980,7 @@
CHECK(it != all_snapshot_status.end()) << target_partition->name();
cow_params.partition_name = target_partition->name();
std::string cow_name;
- if (!MapCowDevices(lock.get(), cow_params, it->second, &created_devices_for_cow,
- &cow_name)) {
+ if (!MapCowDevices(lock, cow_params, it->second, &created_devices_for_cow, &cow_name)) {
return false;
}
@@ -1951,16 +1997,6 @@
}
// Let destructor of created_devices_for_cow to unmap the COW devices.
};
-
- if (!UpdatePartitionTable(opener, device_->GetSuperDevice(target_slot),
- *exported_target_metadata, target_slot)) {
- LOG(ERROR) << "Cannot write target metadata";
- return false;
- }
-
- created_devices.Release();
- LOG(INFO) << "Successfully created all snapshots for target slot " << target_suffix;
-
return true;
}
diff --git a/init/devices.cpp b/init/devices.cpp
index f6e453a..9fbec64 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -441,6 +441,23 @@
}
}
+void DeviceHandler::HandleAshmemUevent(const Uevent& uevent) {
+ if (uevent.device_name == "ashmem") {
+ static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id";
+ std::string boot_id;
+ if (!ReadFileToString(boot_id_path, &boot_id)) {
+ PLOG(ERROR) << "Cannot duplicate ashmem device node. Failed to read " << boot_id_path;
+ return;
+ };
+ boot_id = Trim(boot_id);
+
+ Uevent dup_ashmem_uevent = uevent;
+ dup_ashmem_uevent.device_name += boot_id;
+ dup_ashmem_uevent.path += boot_id;
+ HandleUevent(dup_ashmem_uevent);
+ }
+}
+
void DeviceHandler::HandleUevent(const Uevent& uevent) {
if (uevent.action == "add" || uevent.action == "change" || uevent.action == "online") {
FixupSysPermissions(uevent.path, uevent.subsystem);
@@ -485,6 +502,10 @@
mkdir_recursive(Dirname(devpath), 0755);
HandleDevice(uevent.action, devpath, block, uevent.major, uevent.minor, links);
+
+ // Duplicate /dev/ashmem device and name it /dev/ashmem<boot_id>.
+ // TODO(b/111903542): remove once all users of /dev/ashmem are migrated to libcutils API.
+ HandleAshmemUevent(uevent);
}
void DeviceHandler::ColdbootDone() {
diff --git a/init/devices.h b/init/devices.h
index 442c53f..05d64da 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -130,6 +130,7 @@
void HandleDevice(const std::string& action, const std::string& devpath, bool block, int major,
int minor, const std::vector<std::string>& links) const;
void FixupSysPermissions(const std::string& upath, const std::string& subsystem) const;
+ void HandleAshmemUevent(const Uevent& uevent);
std::vector<Permissions> dev_permissions_;
std::vector<SysfsPermissions> sysfs_permissions_;
diff --git a/init/reboot.cpp b/init/reboot.cpp
index 2cf0f5c..30836d2 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -116,16 +116,16 @@
"-a",
mnt_fsname_.c_str(),
};
- android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
- true, nullptr, nullptr, 0);
+ logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
+ nullptr);
} else if (IsExt4()) {
const char* ext4_argv[] = {
"/system/bin/e2fsck",
"-y",
mnt_fsname_.c_str(),
};
- android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
- true, nullptr, nullptr, 0);
+ logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
+ nullptr);
}
}
@@ -163,8 +163,7 @@
static void ShutdownVold() {
const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
int status;
- android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
- nullptr, nullptr, 0);
+ logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true, nullptr);
}
static void LogShutdownTime(UmountStat stat, Timer* t) {
@@ -221,8 +220,8 @@
if (!security_getenforce()) {
LOG(INFO) << "Run lsof";
const char* lsof_argv[] = {"/system/bin/lsof"};
- android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
- true, nullptr, nullptr, 0);
+ logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
+ nullptr);
}
FindPartitionsToUmount(nullptr, nullptr, true);
// dump current CPU stack traces and uninterruptible tasks
@@ -317,8 +316,8 @@
LOG(INFO) << "Try to dump init process call trace:";
const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
int status;
- android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true,
- LOG_KLOG, true, nullptr, nullptr, 0);
+ logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
+ true, nullptr);
}
LOG(INFO) << "Show stack for all active CPU:";
WriteStringToFile("l", PROC_SYSRQ);
diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp
index e67b458..340572c 100644
--- a/libcutils/ashmem-dev.cpp
+++ b/libcutils/ashmem-dev.cpp
@@ -23,9 +23,6 @@
*/
#define LOG_TAG "ashmem"
-#ifndef __ANDROID_VNDK__
-#include <dlfcn.h>
-#endif
#include <errno.h>
#include <fcntl.h>
#include <linux/ashmem.h>
@@ -42,11 +39,11 @@
#include <sys/types.h>
#include <unistd.h>
+#include <android-base/file.h>
#include <android-base/properties.h>
+#include <android-base/strings.h>
#include <android-base/unique_fd.h>
-#define ASHMEM_DEVICE "/dev/ashmem"
-
/* Will be added to UAPI once upstream change is merged */
#define F_SEAL_FUTURE_WRITE 0x0010
@@ -66,32 +63,6 @@
static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER;
/*
- * We use ashmemd to enforce that apps don't open /dev/ashmem directly. Vendor
- * code can't access system aidl services per Treble requirements. So we limit
- * ashmemd access to the system variant of libcutils.
- */
-#ifndef __ANDROID_VNDK__
-using openFdType = int (*)();
-
-static openFdType openFd;
-
-openFdType initOpenAshmemFd() {
- openFdType openFd = nullptr;
- void* handle = dlopen("libashmemd_client.so", RTLD_NOW);
- if (!handle) {
- ALOGE("Failed to dlopen() libashmemd_client.so: %s", dlerror());
- return openFd;
- }
-
- openFd = reinterpret_cast<openFdType>(dlsym(handle, "openAshmemdFd"));
- if (!openFd) {
- ALOGE("Failed to dlsym() openAshmemdFd() function: %s", dlerror());
- }
- return openFd;
-}
-#endif
-
-/*
* has_memfd_support() determines if the device can use memfd. memfd support
* has been there for long time, but certain things in it may be missing. We
* check for needed support in it. Also we check if the VNDK version of
@@ -215,25 +186,31 @@
return memfd_supported;
}
+static std::string get_ashmem_device_path() {
+ static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id";
+ std::string boot_id;
+ if (!android::base::ReadFileToString(boot_id_path, &boot_id)) {
+ ALOGE("Failed to read %s: %s.\n", boot_id_path.c_str(), strerror(errno));
+ return "";
+ };
+ boot_id = android::base::Trim(boot_id);
+
+ return "/dev/ashmem" + boot_id;
+}
+
/* logistics of getting file descriptor for ashmem */
static int __ashmem_open_locked()
{
+ static const std::string ashmem_device_path = get_ashmem_device_path();
+
int ret;
struct stat st;
- int fd = -1;
-#ifndef __ANDROID_VNDK__
- if (!openFd) {
- openFd = initOpenAshmemFd();
+ if (ashmem_device_path.empty()) {
+ return -1;
}
- if (openFd) {
- fd = openFd();
- }
-#endif
- if (fd < 0) {
- fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR | O_CLOEXEC));
- }
+ int fd = TEMP_FAILURE_RETRY(open(ashmem_device_path.c_str(), O_RDWR | O_CLOEXEC));
if (fd < 0) {
return fd;
}
@@ -485,11 +462,3 @@
return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)));
}
-
-void ashmem_init() {
-#ifndef __ANDROID_VNDK__
- pthread_mutex_lock(&__ashmem_lock);
- openFd = initOpenAshmemFd();
- pthread_mutex_unlock(&__ashmem_lock);
-#endif //__ANDROID_VNDK__
-}
diff --git a/libcutils/ashmem-host.cpp b/libcutils/ashmem-host.cpp
index 6c7655a..2ba1eb0 100644
--- a/libcutils/ashmem-host.cpp
+++ b/libcutils/ashmem-host.cpp
@@ -94,5 +94,3 @@
return buf.st_size;
}
-
-void ashmem_init() {}
diff --git a/libcutils/include/cutils/ashmem.h b/libcutils/include/cutils/ashmem.h
index abc5068..d80caa6 100644
--- a/libcutils/include/cutils/ashmem.h
+++ b/libcutils/include/cutils/ashmem.h
@@ -26,7 +26,6 @@
int ashmem_pin_region(int fd, size_t offset, size_t len);
int ashmem_unpin_region(int fd, size_t offset, size_t len);
int ashmem_get_size_region(int fd);
-void ashmem_init();
#ifdef __cplusplus
}
diff --git a/libmeminfo/vts/Android.bp b/libmeminfo/vts/Android.bp
index 5a3a23b..a92f669 100644
--- a/libmeminfo/vts/Android.bp
+++ b/libmeminfo/vts/Android.bp
@@ -12,9 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-cc_test {
- name: "vts_meminfo_test",
+cc_defaults {
+ name: "vts_meminfo_defaults",
defaults: ["libmeminfo_defaults"],
srcs: ["vts_meminfo_test.cpp"],
static_libs: ["libmeminfo"],
}
+
+cc_test {
+ name: "vts_meminfo_test",
+ defaults: ["vts_meminfo_defaults"],
+}
+
+cc_test {
+ name: "vts_core_meminfo_test",
+ defaults: ["vts_meminfo_defaults"],
+ test_suites: ["vts-core"],
+ auto_gen_config: true,
+ test_min_api_level: 29,
+}
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
index d17da12..f06717d 100644
--- a/lmkd/lmkd.c
+++ b/lmkd/lmkd.c
@@ -362,6 +362,9 @@
/* PAGE_SIZE / 1024 */
static long page_k;
+static char* proc_get_name(int pid);
+static void poll_kernel();
+
static bool parse_int64(const char* str, int64_t* ret) {
char* endptr;
long long val = strtoll(str, &endptr, 10);
@@ -634,6 +637,9 @@
}
if (use_inkernel_interface) {
+#ifdef LMKD_LOG_STATS
+ stats_store_taskname(params.pid, proc_get_name(params.pid));
+#endif
return;
}
@@ -704,6 +710,16 @@
struct lmk_procremove params;
if (use_inkernel_interface) {
+#ifdef LMKD_LOG_STATS
+ /* Perform an extra check before the pid is removed, after which it
+ * will be impossible for poll_kernel to get the taskname. poll_kernel()
+ * is potentially a long-running blocking function; however this method
+ * handles AMS requests but does not block AMS.*/
+ if (enable_stats_log) {
+ poll_kernel();
+ }
+ stats_remove_taskname(params.pid);
+#endif
return;
}
@@ -721,6 +737,9 @@
struct proc *next;
if (use_inkernel_interface) {
+#ifdef LMKD_LOG_STATS
+ stats_purge_tasknames();
+#endif
return;
}
@@ -1925,7 +1944,6 @@
#ifdef LMKD_LOG_STATS
static int kernel_poll_fd = -1;
-
static void poll_kernel() {
if (kernel_poll_fd == -1) {
// not waiting
@@ -1958,9 +1976,9 @@
/* only the death of the group leader process is logged */
if (fields_read == 10 && group_leader_pid == pid) {
int64_t process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK));
- stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, oom_score_adj,
- min_flt, maj_flt, rss_in_pages * PAGE_SIZE, 0, 0,
- process_start_time_ns, min_score_adj);
+ stats_write_lmk_kill_occurred_pid(log_ctx, LMK_KILL_OCCURRED, uid, pid, oom_score_adj,
+ min_flt, maj_flt, rss_in_pages * PAGE_SIZE, 0, 0,
+ process_start_time_ns, min_score_adj);
}
free(taskname);
diff --git a/lmkd/statslog.c b/lmkd/statslog.c
index 0c230ae..f3a6e55 100644
--- a/lmkd/statslog.c
+++ b/lmkd/statslog.c
@@ -18,8 +18,22 @@
#include <errno.h>
#include <log/log_id.h>
#include <stats_event_list.h>
+#include <stdlib.h>
+#include <string.h>
#include <time.h>
+#define LINE_MAX 128
+
+struct proc {
+ int pid;
+ char taskname[LINE_MAX];
+ struct proc* pidhash_next;
+};
+
+#define PIDHASH_SZ 1024
+static struct proc** pidhash = NULL;
+#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
+
static int64_t getElapsedRealTimeNs() {
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
@@ -57,6 +71,17 @@
return write_to_logger(ctx, LOG_ID_STATS);
}
+static struct proc* pid_lookup(int pid) {
+ struct proc* procp;
+
+ if (!pidhash) return NULL;
+
+ for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; procp = procp->pidhash_next)
+ ;
+
+ return procp;
+}
+
/**
* Logs the event when LMKD kills a process to reduce memory pressure.
* Code: LMK_KILL_OCCURRED = 51
@@ -124,3 +149,74 @@
return write_to_logger(ctx, LOG_ID_STATS);
}
+
+int stats_write_lmk_kill_occurred_pid(android_log_context ctx, int32_t code, int32_t uid, int pid,
+ int32_t oom_score, int64_t pgfault, int64_t pgmajfault,
+ int64_t rss_in_bytes, int64_t cache_in_bytes,
+ int64_t swap_in_bytes, int64_t process_start_time_ns,
+ int32_t min_oom_score) {
+ struct proc* proc = pid_lookup(pid);
+ if (!proc) return -EINVAL;
+
+ return stats_write_lmk_kill_occurred(ctx, code, uid, proc->taskname, oom_score, pgfault,
+ pgmajfault, rss_in_bytes, cache_in_bytes, swap_in_bytes,
+ process_start_time_ns, min_oom_score);
+}
+
+static void proc_insert(struct proc* procp) {
+ if (!pidhash)
+ pidhash = calloc(PIDHASH_SZ, sizeof(struct proc));
+ int hval = pid_hashfn(procp->pid);
+ procp->pidhash_next = pidhash[hval];
+ pidhash[hval] = procp;
+}
+
+void stats_remove_taskname(int pid) {
+ if (!pidhash) return;
+
+ int hval = pid_hashfn(pid);
+ struct proc* procp;
+ struct proc* prevp;
+
+ for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
+ procp = procp->pidhash_next)
+ prevp = procp;
+
+ if (!procp)
+ return;
+
+ if (!prevp)
+ pidhash[hval] = procp->pidhash_next;
+ else
+ prevp->pidhash_next = procp->pidhash_next;
+
+ free(procp);
+}
+
+void stats_store_taskname(int pid, const char* taskname) {
+ struct proc* procp = pid_lookup(pid);
+ if (procp != NULL && strcmp(procp->taskname, taskname) == 0)
+ return;
+ procp = malloc(sizeof(struct proc));
+ stats_remove_taskname(pid);
+ procp->pid = pid;
+ strncpy(procp->taskname, taskname, LINE_MAX - 1);
+ procp->taskname[LINE_MAX - 1] = '\0';
+ proc_insert(procp);
+}
+
+void stats_purge_tasknames() {
+ if (!pidhash) return;
+ struct proc* procp;
+ struct proc* next;
+ int i;
+ for (i = 0; i < PIDHASH_SZ; i++) {
+ procp = pidhash[i];
+ while (procp) {
+ next = procp->pidhash_next;
+ free(procp);
+ procp = next;
+ }
+ }
+ memset(pidhash, 0, PIDHASH_SZ * sizeof(struct proc));
+}
diff --git a/lmkd/statslog.h b/lmkd/statslog.h
index 2edba7a..50d69f7 100644
--- a/lmkd/statslog.h
+++ b/lmkd/statslog.h
@@ -85,12 +85,41 @@
* Code: LMK_KILL_OCCURRED = 51
*/
int
+stats_write_lmk_kill_occurred_pid(android_log_context ctx, int32_t code, int32_t uid, int pid,
+ int32_t oom_score, int64_t pgfault, int64_t pgmajfault,
+ int64_t rss_in_bytes, int64_t cache_in_bytes,
+ int64_t swap_in_bytes, int64_t process_start_time_ns,
+ int32_t min_oom_score);
+
+/**
+ * Logs the event when LMKD kills a process to reduce memory pressure.
+ * Code: LMK_KILL_OCCURRED = 51
+ */
+int
stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
char const* process_name, int32_t oom_score, int64_t pgfault,
int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
int64_t swap_in_bytes, int64_t process_start_time_ns,
int32_t min_oom_score);
+/**
+ * Registers a process taskname by pid, while it is still alive.
+ */
+void
+stats_store_taskname(int pid, const char* taskname);
+
+/**
+ * Unregister all process tasknames.
+ */
+void
+stats_purge_tasknames();
+
+/**
+ * Unregister a process taskname, e.g. after it has been killed.
+ */
+void
+stats_remove_taskname(int pid);
+
__END_DECLS
#endif /* _STATSLOG_H_ */
diff --git a/logwrapper/Android.bp b/logwrapper/Android.bp
index c378646..e360a85 100644
--- a/logwrapper/Android.bp
+++ b/logwrapper/Android.bp
@@ -13,11 +13,12 @@
name: "liblogwrap",
defaults: ["logwrapper_defaults"],
recovery_available: true,
- srcs: ["logwrap.c"],
+ srcs: ["logwrap.cpp"],
shared_libs: [
"libcutils",
"liblog",
],
+ header_libs: ["libbase_headers"],
export_include_dirs: ["include"],
local_include_dirs: ["include"],
}
@@ -31,9 +32,10 @@
defaults: ["logwrapper_defaults"],
local_include_dirs: ["include"],
srcs: [
- "logwrap.c",
- "logwrapper.c",
+ "logwrap.cpp",
+ "logwrapper.cpp",
],
+ header_libs: ["libbase_headers"],
shared_libs: ["libcutils", "liblog"],
}
diff --git a/logwrapper/include/logwrap/logwrap.h b/logwrapper/include/logwrap/logwrap.h
index b1f6410..c329586 100644
--- a/logwrapper/include/logwrap/logwrap.h
+++ b/logwrapper/include/logwrap/logwrap.h
@@ -17,11 +17,6 @@
#pragma once
-#include <stdbool.h>
-#include <stdint.h>
-
-__BEGIN_DECLS
-
/*
* Run a command while logging its stdout and stderr
*
@@ -56,35 +51,32 @@
*
*/
-/* Values for the log_target parameter android_fork_execvp_ext() */
+/* Values for the log_target parameter logwrap_fork_execvp() */
#define LOG_NONE 0
#define LOG_ALOG 1
#define LOG_KLOG 2
#define LOG_FILE 4
-int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_signals,
- int log_target, bool abbreviated, char* file_path);
+int logwrap_fork_execvp(int argc, const char* const* argv, int* status, bool forward_signals,
+ int log_target, bool abbreviated, const char* file_path);
// TODO: Actually deprecate this and the below.
static inline int android_fork_execvp_ext(int argc, char* argv[], int* status, bool ignore_int_quit,
- int log_target, bool abbreviated, char* file_path,
+ int log_target, bool abbreviated, const char* file_path,
void* unused_opts, int unused_opts_len) {
(void)ignore_int_quit;
(void)unused_opts;
(void)unused_opts_len;
- return android_fork_execvp_ext2(argc, argv, status, false, log_target, abbreviated, file_path);
+ return logwrap_fork_execvp(argc, argv, status, false, log_target, abbreviated, file_path);
}
/* Similar to above, except abbreviated logging is not available, and if logwrap
* is true, logging is to the Android system log, and if false, there is no
* logging.
*/
-static inline int android_fork_execvp(int argc, char* argv[], int *status,
- bool ignore_int_quit, bool logwrap)
-{
- return android_fork_execvp_ext(argc, argv, status, ignore_int_quit,
- (logwrap ? LOG_ALOG : LOG_NONE), false, NULL,
- NULL, 0);
+static inline int android_fork_execvp(int argc, char* argv[], int* status, bool ignore_int_quit,
+ bool logwrap) {
+ (void)ignore_int_quit;
+ return logwrap_fork_execvp(argc, argv, status, false, (logwrap ? LOG_ALOG : LOG_NONE), false,
+ nullptr);
}
-
-__END_DECLS
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.cpp
similarity index 77%
rename from logwrapper/logwrap.c
rename to logwrapper/logwrap.cpp
index 0314f37..5337801 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.cpp
@@ -19,7 +19,6 @@
#include <libgen.h>
#include <poll.h>
#include <pthread.h>
-#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -28,13 +27,13 @@
#include <sys/wait.h>
#include <unistd.h>
+#include <algorithm>
+
+#include <android-base/macros.h>
#include <cutils/klog.h>
#include <log/log.h>
#include <logwrap/logwrap.h>
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
-#define MIN(a,b) (((a)<(b))?(a):(b))
-
static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
// Protected by fd_mutex. These signals must be blocked while modifying as well.
static pid_t child_pid;
@@ -42,17 +41,17 @@
static struct sigaction old_quit;
static struct sigaction old_hup;
-#define ERROR(fmt, args...) \
-do { \
- fprintf(stderr, fmt, ## args); \
- ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
-} while(0)
+#define ERROR(fmt, args...) \
+ do { \
+ fprintf(stderr, fmt, ##args); \
+ ALOG(LOG_ERROR, "logwrapper", fmt, ##args); \
+ } while (0)
-#define FATAL_CHILD(fmt, args...) \
-do { \
- ERROR(fmt, ## args); \
- _exit(-1); \
-} while(0)
+#define FATAL_CHILD(fmt, args...) \
+ do { \
+ ERROR(fmt, ##args); \
+ _exit(-1); \
+ } while (0)
#define MAX_KLOG_TAG 16
@@ -61,7 +60,7 @@
*/
#define BEGINNING_BUF_SIZE 0x1000
struct beginning_buf {
- char *buf;
+ char* buf;
size_t alloc_len;
/* buf_size is the usable space, which is one less than the allocated size */
size_t buf_size;
@@ -74,7 +73,7 @@
*/
#define ENDING_BUF_SIZE 0x1000
struct ending_buf {
- char *buf;
+ char* buf;
ssize_t alloc_len;
/* buf_size is the usable space, which is one less than the allocated size */
ssize_t buf_size;
@@ -84,7 +83,7 @@
int write;
};
- /* A structure to hold all the abbreviated buf data */
+/* A structure to hold all the abbreviated buf data */
struct abbr_buf {
struct beginning_buf b_buf;
struct ending_buf e_buf;
@@ -95,19 +94,17 @@
struct log_info {
int log_target;
char klog_fmt[MAX_KLOG_TAG * 2];
- char *btag;
+ const char* btag;
bool abbreviated;
- FILE *fp;
+ FILE* fp;
struct abbr_buf a_buf;
};
/* Forware declaration */
-static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen);
+static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen);
/* Return 0 on success, and 1 when full */
-static int add_line_to_linear_buf(struct beginning_buf *b_buf,
- char *line, ssize_t line_len)
-{
+static int add_line_to_linear_buf(struct beginning_buf* b_buf, char* line, ssize_t line_len) {
int full = 0;
if ((line_len + b_buf->used_len) > b_buf->buf_size) {
@@ -121,20 +118,18 @@
return full;
}
-static void add_line_to_circular_buf(struct ending_buf *e_buf,
- char *line, ssize_t line_len)
-{
+static void add_line_to_circular_buf(struct ending_buf* e_buf, char* line, ssize_t line_len) {
ssize_t free_len;
ssize_t needed_space;
int cnt;
- if (e_buf->buf == NULL) {
+ if (e_buf->buf == nullptr) {
return;
}
- if (line_len > e_buf->buf_size) {
- return;
- }
+ if (line_len > e_buf->buf_size) {
+ return;
+ }
free_len = e_buf->buf_size - e_buf->used_len;
@@ -149,7 +144,7 @@
/* Copy the line into the circular buffer, dealing with possible
* wraparound.
*/
- cnt = MIN(line_len, e_buf->buf_size - e_buf->write);
+ cnt = std::min(line_len, e_buf->buf_size - e_buf->write);
memcpy(e_buf->buf + e_buf->write, line, cnt);
if (cnt < line_len) {
memcpy(e_buf->buf, line + cnt, line_len - cnt);
@@ -159,7 +154,7 @@
}
/* Log directly to the specified log */
-static void do_log_line(struct log_info *log_info, char *line) {
+static void do_log_line(struct log_info* log_info, const char* line) {
if (log_info->log_target & LOG_KLOG) {
klog_write(6, log_info->klog_fmt, line);
}
@@ -174,7 +169,7 @@
/* Log to either the abbreviated buf, or directly to the specified log
* via do_log_line() above.
*/
-static void log_line(struct log_info *log_info, char *line, int len) {
+static void log_line(struct log_info* log_info, char* line, int len) {
if (log_info->abbreviated) {
add_line_to_abbr_buf(&log_info->a_buf, line, len);
} else {
@@ -189,9 +184,8 @@
* than buf_size (the usable size of the buffer) to make sure there is
* room to temporarily stuff a null byte to terminate a line for logging.
*/
-static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size)
-{
- char *line_start;
+static void print_buf_lines(struct log_info* log_info, char* buf, int buf_size) {
+ char* line_start;
char c;
int i;
@@ -217,17 +211,17 @@
*/
}
-static void init_abbr_buf(struct abbr_buf *a_buf) {
- char *new_buf;
+static void init_abbr_buf(struct abbr_buf* a_buf) {
+ char* new_buf;
memset(a_buf, 0, sizeof(struct abbr_buf));
- new_buf = malloc(BEGINNING_BUF_SIZE);
+ new_buf = static_cast<char*>(malloc(BEGINNING_BUF_SIZE));
if (new_buf) {
a_buf->b_buf.buf = new_buf;
a_buf->b_buf.alloc_len = BEGINNING_BUF_SIZE;
a_buf->b_buf.buf_size = BEGINNING_BUF_SIZE - 1;
}
- new_buf = malloc(ENDING_BUF_SIZE);
+ new_buf = static_cast<char*>(malloc(ENDING_BUF_SIZE));
if (new_buf) {
a_buf->e_buf.buf = new_buf;
a_buf->e_buf.alloc_len = ENDING_BUF_SIZE;
@@ -235,23 +229,22 @@
}
}
-static void free_abbr_buf(struct abbr_buf *a_buf) {
+static void free_abbr_buf(struct abbr_buf* a_buf) {
free(a_buf->b_buf.buf);
free(a_buf->e_buf.buf);
}
-static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen) {
+static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen) {
if (!a_buf->beginning_buf_full) {
- a_buf->beginning_buf_full =
- add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
+ a_buf->beginning_buf_full = add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
}
if (a_buf->beginning_buf_full) {
add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen);
}
}
-static void print_abbr_buf(struct log_info *log_info) {
- struct abbr_buf *a_buf = &log_info->a_buf;
+static void print_abbr_buf(struct log_info* log_info) {
+ struct abbr_buf* a_buf = &log_info->a_buf;
/* Add the abbreviated output to the kernel log */
if (a_buf->b_buf.alloc_len) {
@@ -274,14 +267,13 @@
* and then cal print_buf_lines on it */
if (a_buf->e_buf.read < a_buf->e_buf.write) {
/* no wrap around, just print it */
- print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read,
- a_buf->e_buf.used_len);
+ print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read, a_buf->e_buf.used_len);
} else {
/* The circular buffer will always have at least 1 byte unused,
* so by allocating alloc_len here we will have at least
* 1 byte of space available as required by print_buf_lines().
*/
- char * nbuf = malloc(a_buf->e_buf.alloc_len);
+ char* nbuf = static_cast<char*>(malloc(a_buf->e_buf.alloc_len));
if (!nbuf) {
return;
}
@@ -307,7 +299,7 @@
}
static void unblock_signals(sigset_t* oldset) {
- pthread_sigmask(SIG_SETMASK, oldset, NULL);
+ pthread_sigmask(SIG_SETMASK, oldset, nullptr);
}
static void setup_signal_handlers(pid_t pid) {
@@ -320,9 +312,9 @@
}
static void restore_signal_handlers() {
- sigaction(SIGINT, &old_int, NULL);
- sigaction(SIGQUIT, &old_quit, NULL);
- sigaction(SIGHUP, &old_hup, NULL);
+ sigaction(SIGINT, &old_int, nullptr);
+ sigaction(SIGQUIT, &old_quit, nullptr);
+ sigaction(SIGHUP, &old_hup, nullptr);
child_pid = 0;
}
@@ -334,14 +326,14 @@
}
static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, int log_target,
- bool abbreviated, char* file_path, bool forward_signals) {
+ bool abbreviated, const char* file_path, bool forward_signals) {
int status = 0;
char buffer[4096];
struct pollfd poll_fds[] = {
- [0] = {
- .fd = parent_read,
- .events = POLLIN,
- },
+ {
+ .fd = parent_read,
+ .events = POLLIN,
+ },
};
int rc = 0;
int fd;
@@ -361,7 +353,7 @@
log_info.btag = basename(tag);
if (!log_info.btag) {
- log_info.btag = (char*) tag;
+ log_info.btag = tag;
}
if (abbreviated && (log_target == LOG_NONE)) {
@@ -372,8 +364,8 @@
}
if (log_target & LOG_KLOG) {
- snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt),
- "<6>%.*s: %%s\n", MAX_KLOG_TAG, log_info.btag);
+ snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt), "<6>%.*s: %%s\n", MAX_KLOG_TAG,
+ log_info.btag);
}
if ((log_target & LOG_FILE) && !file_path) {
@@ -397,7 +389,7 @@
while (!found_child) {
int timeout = received_messages ? -1 : 1000;
- if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), timeout)) < 0) {
+ if (TEMP_FAILURE_RETRY(poll(poll_fds, arraysize(poll_fds), timeout)) < 0) {
ERROR("poll failed\n");
rc = -1;
goto err_poll;
@@ -405,8 +397,7 @@
if (poll_fds[0].revents & POLLIN) {
received_messages = true;
- sz = TEMP_FAILURE_RETRY(
- read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
+ sz = TEMP_FAILURE_RETRY(read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
sz += b;
// Log one line at a time
@@ -479,19 +470,19 @@
}
}
- if (chld_sts != NULL) {
+ if (chld_sts != nullptr) {
*chld_sts = status;
} else {
- if (WIFEXITED(status))
- rc = WEXITSTATUS(status);
- else
- rc = -ECHILD;
+ if (WIFEXITED(status))
+ rc = WEXITSTATUS(status);
+ else
+ rc = -ECHILD;
}
// Flush remaining data
if (a != b) {
- buffer[b] = '\0';
- log_line(&log_info, &buffer[a], b - a);
+ buffer[b] = '\0';
+ log_line(&log_info, &buffer[a], b - a);
}
/* All the output has been processed, time to dump the abbreviated output */
@@ -500,21 +491,21 @@
}
if (WIFEXITED(status)) {
- if (WEXITSTATUS(status)) {
- snprintf(tmpbuf, sizeof(tmpbuf),
- "%s terminated by exit(%d)\n", log_info.btag, WEXITSTATUS(status));
- do_log_line(&log_info, tmpbuf);
- }
+ if (WEXITSTATUS(status)) {
+ snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by exit(%d)\n", log_info.btag,
+ WEXITSTATUS(status));
+ do_log_line(&log_info, tmpbuf);
+ }
} else {
- if (WIFSIGNALED(status)) {
- snprintf(tmpbuf, sizeof(tmpbuf),
- "%s terminated by signal %d\n", log_info.btag, WTERMSIG(status));
- do_log_line(&log_info, tmpbuf);
- } else if (WIFSTOPPED(status)) {
- snprintf(tmpbuf, sizeof(tmpbuf),
- "%s stopped by signal %d\n", log_info.btag, WSTOPSIG(status));
- do_log_line(&log_info, tmpbuf);
- }
+ if (WIFSIGNALED(status)) {
+ snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by signal %d\n", log_info.btag,
+ WTERMSIG(status));
+ do_log_line(&log_info, tmpbuf);
+ } else if (WIFSTOPPED(status)) {
+ snprintf(tmpbuf, sizeof(tmpbuf), "%s stopped by signal %d\n", log_info.btag,
+ WSTOPSIG(status));
+ do_log_line(&log_info, tmpbuf);
+ }
}
err_waitpid:
@@ -528,23 +519,21 @@
return rc;
}
-static void child(int argc, char* argv[]) {
+static void child(int argc, const char* const* argv) {
// create null terminated argv_child array
char* argv_child[argc + 1];
- memcpy(argv_child, argv, argc * sizeof(char *));
- argv_child[argc] = NULL;
+ memcpy(argv_child, argv, argc * sizeof(char*));
+ argv_child[argc] = nullptr;
if (execvp(argv_child[0], argv_child)) {
- FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
- strerror(errno));
+ FATAL_CHILD("executing %s failed: %s\n", argv_child[0], strerror(errno));
}
}
-int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_signals,
- int log_target, bool abbreviated, char* file_path) {
+int logwrap_fork_execvp(int argc, const char* const* argv, int* status, bool forward_signals,
+ int log_target, bool abbreviated, const char* file_path) {
pid_t pid;
int parent_ptty;
- sigset_t blockset;
sigset_t oldset;
int rc = 0;
@@ -564,7 +553,7 @@
char child_devname[64];
if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
- ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
+ ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
ERROR("Problem with /dev/ptmx\n");
rc = -1;
goto err_ptty;
@@ -582,7 +571,9 @@
goto err_fork;
} else if (pid == 0) {
pthread_mutex_unlock(&fd_mutex);
- unblock_signals(&oldset);
+ if (forward_signals) {
+ unblock_signals(&oldset);
+ }
setsid();
diff --git a/logwrapper/logwrapper.c b/logwrapper/logwrapper.cpp
similarity index 63%
rename from logwrapper/logwrapper.c
rename to logwrapper/logwrapper.cpp
index e786609..7118d12 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.cpp
@@ -24,27 +24,26 @@
#include <log/log.h>
#include <logwrap/logwrap.h>
-void fatal(const char *msg) {
+void fatal(const char* msg) {
fprintf(stderr, "%s", msg);
ALOG(LOG_ERROR, "logwrapper", "%s", msg);
exit(-1);
}
void usage() {
- fatal(
- "Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
- "\n"
- "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
- "the Android logging system. Tag is set to BINARY, priority is\n"
- "always LOG_INFO.\n"
- "\n"
- "-a: Causes logwrapper to do abbreviated logging.\n"
- " This logs up to the first 4K and last 4K of the command\n"
- " being run, and logs the output when the command exits\n"
- "-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
- " fault address is set to the status of wait()\n"
- "-k: Causes logwrapper to log to the kernel log instead of\n"
- " the Android system log\n");
+ fatal("Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
+ "\n"
+ "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
+ "the Android logging system. Tag is set to BINARY, priority is\n"
+ "always LOG_INFO.\n"
+ "\n"
+ "-a: Causes logwrapper to do abbreviated logging.\n"
+ " This logs up to the first 4K and last 4K of the command\n"
+ " being run, and logs the output when the command exits\n"
+ "-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
+ " fault address is set to the status of wait()\n"
+ "-k: Causes logwrapper to log to the kernel log instead of\n"
+ " the Android system log\n");
}
int main(int argc, char* argv[]) {
@@ -69,7 +68,7 @@
break;
case '?':
default:
- usage();
+ usage();
}
}
argc -= optind;
@@ -79,7 +78,7 @@
usage();
}
- rc = android_fork_execvp_ext2(argc, &argv[0], &status, true, log_target, abbreviated, NULL);
+ rc = logwrap_fork_execvp(argc, &argv[0], &status, true, log_target, abbreviated, nullptr);
if (!rc) {
if (WIFEXITED(status))
rc = WEXITSTATUS(status);
@@ -88,8 +87,8 @@
}
if (seg_fault_on_exit) {
- uintptr_t fault_address = (uintptr_t) status;
- *(int *) fault_address = 0; // causes SIGSEGV with fault_address = status
+ uintptr_t fault_address = (uintptr_t)status;
+ *(int*)fault_address = 0; // causes SIGSEGV with fault_address = status
}
return rc;
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index 451f5ad..9c2cdf2 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -33,7 +33,7 @@
/dev/urandom 0666 root root
# Make HW RNG readable by group system to let EntropyMixer read it.
/dev/hw_random 0440 root system
-/dev/ashmem 0666 root root
+/dev/ashmem* 0666 root root
/dev/binder 0666 root root
/dev/hwbinder 0666 root root
/dev/vndbinder 0666 root root