From b031def229aaa3e1fcd304e5fb7ad85dab61b829 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 30 Oct 2020 00:00:33 -0700 Subject: [PATCH] Start snapuserd if needed as soon as possible during second-stage init. snapuserd is used as a user-space block device implementation during Virtual A/B Compression-enabled updates. It has to be started in first-stage init, so that updated partitions can be mounted. Once init reaches second-stage, and sepolicy is loaded, we want to re-launch snapuserd at the correct privilege level. We accomplish this by rebuilding the device-mapper tables of each block device, which allows us to re-bind the kernel driver to a new instance of snapuserd. After this, the old daemon can be shut down. Ideally this transition happens as soon as possible, before any .rc scripts are run. This minimizes the amount of time the original snapuserd is running, as well as any ambiguity about which instance of snapuserd is the correct one. The original daemon is sent a SIGTERM signal once the transition is complete. The pid is stored in an environment variable to make this possible (these details are implemented in libsnapshot). Bug: 168259959 Test: manual test Change-Id: Ife9518e502ce02f11ec54e7f3e6adc6f04d94133 --- init/init.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/init/init.cpp b/init/init.cpp index ea0449446..c6f206644 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -94,6 +95,7 @@ using android::base::StringPrintf; using android::base::Timer; using android::base::Trim; using android::fs_mgr::AvbHandle; +using android::snapshot::SnapshotManager; namespace android { namespace init { @@ -722,6 +724,32 @@ void SendLoadPersistentPropertiesMessage() { } } +static Result TransitionSnapuserdAction(const BuiltinArguments&) { + if (!SnapshotManager::IsSnapshotManagerNeeded() || + !android::base::GetBoolProperty(android::snapshot::kVirtualAbCompressionProp, false)) { + return {}; + } + + auto sm = SnapshotManager::New(); + if (!sm) { + LOG(FATAL) << "Failed to create SnapshotManager, will not transition snapuserd"; + return {}; + } + + ServiceList& service_list = ServiceList::GetInstance(); + auto svc = service_list.FindService("snapuserd"); + if (!svc) { + LOG(FATAL) << "Failed to find snapuserd service, aborting transition"; + return {}; + } + svc->Start(); + + if (!sm->PerformSecondStageTransition()) { + LOG(FATAL) << "Failed to transition snapuserd to second-stage"; + } + return {}; +} + int SecondStageMain(int argc, char** argv) { if (REBOOT_BOOTLOADER_ON_PANIC) { InstallRebootSignalHandlers(); @@ -847,6 +875,7 @@ int SecondStageMain(int argc, char** argv) { SetProperty(gsi::kGsiInstalledProp, is_installed); am.QueueBuiltinAction(SetupCgroupsAction, "SetupCgroups"); + am.QueueBuiltinAction(TransitionSnapuserdAction, "TransitionSnapuserd"); am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict"); am.QueueBuiltinAction(TestPerfEventSelinuxAction, "TestPerfEventSelinux"); am.QueueEventTrigger("early-init");