1#!/bin/bash 2# 3# Copyright (C) 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# This script undoes (most of) the work done by tools/buildbot-setup-device.sh. 18# Make sure to keep these files in sync. 19 20. "$(dirname $0)/buildbot-utils.sh" 21 22[[ -n "$ART_TEST_ON_VM" ]] && exit 0 23 24# Setup as root, as some actions performed here require it. 25adb root 26adb wait-for-device 27 28if [[ -n "$ART_TEST_CHROOT" ]]; then 29 # Check that ART_TEST_CHROOT is correctly defined. 30 [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; } 31 32 if adb shell test -d "$ART_TEST_CHROOT"; then 33 # Display users of the chroot dir. 34 35 msginfo "List open files under chroot dir $ART_TEST_CHROOT" 36 adb shell lsof | grep "$ART_TEST_CHROOT" 37 38 # for_all_chroot_process ACTION 39 # ----------------------------- 40 # Execute ACTION on all processes running from binaries located 41 # under the chroot directory. ACTION is passed two arguments: the 42 # PID of the process, and a string containing the command line 43 # that started this process. 44 for_all_chroot_process() { 45 local action=$1 46 adb shell ls -ld "/proc/*/root" \ 47 | sed -n -e "s,^.* \\(/proc/.*/root\\) -> $ART_TEST_CHROOT\$,\\1,p" \ 48 | while read link; do 49 local dir=$(dirname "$link") 50 local pid=$(basename "$dir") 51 local cmdline=$(adb shell cat "$dir"/cmdline | tr '\000' ' ') 52 $action "$pid" "$cmdline" 53 done 54 } 55 56 # display_process PID CMDLINE 57 # --------------------------- 58 # Display information about process with given PID, that was started with CMDLINE. 59 display_process() { 60 local pid=$1 61 local cmdline=$2 62 echo "$cmdline (PID: $pid)" 63 } 64 65 msginfo "List processes running from binaries under chroot dir $ART_TEST_CHROOT" 66 for_all_chroot_process display_process 67 68 # Tear down the chroot dir. 69 70 msginfo "Tear down the chroot set up in $ART_TEST_CHROOT" 71 72 # remove_filesystem_from_chroot DIR-IN-CHROOT FSTYPE REMOVE-DIR-IN-CHROOT 73 # ----------------------------------------------------------------------- 74 # Unmount filesystem with type FSTYPE mounted in directory DIR-IN-CHROOT 75 # under the chroot directory. 76 # Remove DIR-IN-CHROOT under the chroot if REMOVE-DIR-IN-CHROOT is 77 # true. 78 remove_filesystem_from_chroot() { 79 local dir_in_chroot=$1 80 local fstype=$2 81 local remove_dir=$3 82 local dir="$ART_TEST_CHROOT/$dir_in_chroot" 83 adb shell test -d "$dir" \ 84 && adb shell mount | grep -q " on $dir type $fstype " \ 85 && if adb shell umount "$dir"; then 86 $remove_dir && adb shell rmdir "$dir" 87 else 88 echo "Files still open in $dir:" 89 adb shell lsof | grep "$dir" 90 fi 91 } 92 93 # Remove /bin symlink from chroot. 94 adb shell rm -f "$ART_TEST_CHROOT/bin" 95 96 # Remove /apex from chroot. 97 adb shell rm -rf "$ART_TEST_CHROOT/apex" 98 99 # Remove /dev from chroot. 100 remove_filesystem_from_chroot dev/cpuset cgroup false 101 remove_filesystem_from_chroot dev/pts devpts false 102 remove_filesystem_from_chroot dev tmpfs true 103 104 # Remove /sys/kernel/debug from chroot. 105 # The /sys/kernel/debug directory under the chroot dir cannot be 106 # deleted, as it is part of the host device's /sys filesystem. 107 remove_filesystem_from_chroot sys/kernel/debug debugfs false 108 # Remove /sys from chroot. 109 remove_filesystem_from_chroot sys sysfs true 110 111 # Remove /proc from chroot. 112 remove_filesystem_from_chroot proc proc true 113 114 # Remove /etc from chroot. 115 adb shell rm -f "$ART_TEST_CHROOT/etc" 116 adb shell rm -rf "$ART_TEST_CHROOT/system/etc" 117 118 # Remove directories used for ART testing in chroot. 119 adb shell rm -rf "$ART_TEST_CHROOT/data/local/tmp" 120 adb shell rm -rf "$ART_TEST_CHROOT/data/dalvik-cache" 121 adb shell rm -rf "$ART_TEST_CHROOT/tmp" 122 123 # Remove property_contexts file(s) from chroot. 124 property_context_files="/property_contexts \ 125 /system/etc/selinux/plat_property_contexts \ 126 /vendor/etc/selinux/nonplat_property_context \ 127 /plat_property_contexts \ 128 /nonplat_property_contexts" 129 for f in $property_context_files; do 130 adb shell rm -f "$ART_TEST_CHROOT$f" 131 done 132 133 134 # Kill processes still running in the chroot. 135 136 # kill_process PID CMDLINE 137 # ------------------------ 138 # Kill process with given PID, that was started with CMDLINE. 139 kill_process() { 140 local pid=$1 141 local cmdline=$2 142 echo "Killing $cmdline (PID: $pid)" 143 adb shell kill -9 "$pid" 144 } 145 146 msginfo "Kill processes" \ 147 "still running from binaries under chroot dir $ART_TEST_CHROOT (if any)" 148 for_all_chroot_process kill_process 149 fi 150fi 151