From 4d4eb9ac88f0bb633c469a4cd0b7b70adc98f02b Mon Sep 17 00:00:00 2001 From: Chen Bai Date: Mon, 14 Nov 2022 15:14:15 -0800 Subject: [PATCH] fs-verity: fix broken test testInstallEverything - Force GC on all processes listed by lsof to help this test walk around the FD leaks. BUG: 253463456 Change-Id: Ida8cdfd8469452e028ad736497f5862a0df2a9a0 (cherry picked from commit 67884da910daeca4cb6da7568e7978fcaf4a55b3) --- .../com/android/apkverity/ApkVerityTest.java | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java b/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java index 16f005f288564..591ffeb397211 100644 --- a/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java +++ b/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java @@ -42,6 +42,7 @@ import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.Set; /** * This test makes sure app installs with fs-verity signature, and on-access verification works. @@ -465,10 +466,10 @@ public class ApkVerityTest extends BaseHostJUnit4Test { break; } try { - CLog.d("lsof: " + expectRemoteCommandToSucceed("lsof " + apkPath)); + String openFiles = expectRemoteCommandToSucceed("lsof " + apkPath); + CLog.d("lsof: " + openFiles); Thread.sleep(1000); - String pid = expectRemoteCommandToSucceed("pidof system_server"); - mDevice.executeShellV2Command("kill -10 " + pid); // force GC + forceGCOnOpenFilesProcess(getOpenFilesPIDs(openFiles)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; @@ -478,6 +479,35 @@ public class ApkVerityTest extends BaseHostJUnit4Test { } } + /** + * This is a helper method that parses the lsof output to get PIDs of process holding FD. + * Here is an example output of lsof. This method extracts the second columns(PID). + * + * Example lsof output: + * COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME + * .example.app 1063 u0_a38 mem REG 253,6 8599 12826 example.apk + * .example.app 1063 u0_a38 99r REG 253,6 8599 12826 example.apk + */ + private Set getOpenFilesPIDs(String lsof) { + Set openFilesPIDs = new HashSet<>(); + String[] lines = lsof.split("\n"); + for (int i = 1; i < lines.length; i++) { + openFilesPIDs.add(lines[i].split("\\s+")[1]); + } + return openFilesPIDs; + } + + /** + * This is a helper method that forces GC on processes given their PIDs. + * That is to execute shell command "kill -10" on PIDs. + */ + private void forceGCOnOpenFilesProcess(Set openFilesPIDs) + throws DeviceNotAvailableException { + for (String openFilePID : openFilesPIDs) { + mDevice.executeShellV2Command("kill -10 " + openFilePID); + } + } + private void verifyInstalledFiles(String... filenames) throws DeviceNotAvailableException { String apkPath = getApkPath(TARGET_PACKAGE); String appDir = apkPath.substring(0, apkPath.lastIndexOf("/"));