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
20if [ -t 1 ]; then
21  # Color sequences if terminal is a tty.
22  green='\033[0;32m'
23  nc='\033[0m'
24fi
25
26# Setup as root, as some actions performed here require it.
27adb root
28adb wait-for-device
29
30if [[ -n "$ART_TEST_CHROOT" ]]; then
31  # Check that ART_TEST_CHROOT is correctly defined.
32  [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
33
34  if adb shell test -d "$ART_TEST_CHROOT"; then
35    # Display users of the chroot dir.
36
37    echo -e "${green}List open files under chroot dir $ART_TEST_CHROOT${nc}"
38    adb shell lsof | grep "$ART_TEST_CHROOT"
39
40    # for_all_chroot_process ACTION
41    # -----------------------------
42    # Execute ACTION on all processes running from binaries located
43    # under the chroot directory. ACTION is passed two arguments: the
44    # PID of the process, and a string containing the command line
45    # that started this process.
46    for_all_chroot_process() {
47      local action=$1
48      adb shell ls -ld "/proc/*/root" \
49        | sed -n -e "s,^.* \\(/proc/.*/root\\) -> $ART_TEST_CHROOT\$,\\1,p" \
50        | while read link; do
51            local dir=$(dirname "$link")
52            local pid=$(basename "$dir")
53            local cmdline=$(adb shell cat "$dir"/cmdline | tr '\000' ' ')
54            $action "$pid" "$cmdline"
55          done
56    }
57
58    # display_process PID CMDLINE
59    # ---------------------------
60    # Display information about process with given PID, that was started with CMDLINE.
61    display_process() {
62      local pid=$1
63      local cmdline=$2
64      echo "$cmdline (PID: $pid)"
65    }
66
67    echo -e "${green}List processes running from binaries under chroot dir $ART_TEST_CHROOT${nc}"
68    for_all_chroot_process display_process
69
70    # Tear down the chroot dir.
71
72    echo -e "${green}Tear down the chroot set up in $ART_TEST_CHROOT${nc}"
73
74    # remove_filesystem_from_chroot DIR-IN-CHROOT FSTYPE REMOVE-DIR-IN-CHROOT
75    # -----------------------------------------------------------------------
76    # Unmount filesystem with type FSTYPE mounted in directory DIR-IN-CHROOT
77    # under the chroot directory.
78    # Remove DIR-IN-CHROOT under the chroot if REMOVE-DIR-IN-CHROOT is
79    # true.
80    remove_filesystem_from_chroot() {
81      local dir_in_chroot=$1
82      local fstype=$2
83      local remove_dir=$3
84      local dir="$ART_TEST_CHROOT/$dir_in_chroot"
85      adb shell test -d "$dir" \
86        && adb shell mount | grep -q "^$fstype on $dir type $fstype " \
87        && if adb shell umount "$dir"; then
88             $remove_dir && adb shell rmdir "$dir"
89           else
90             echo "Files still open in $dir:"
91             adb shell lsof | grep "$dir"
92           fi
93    }
94
95    # Remove /apex from chroot.
96    adb shell rm -rf "$ART_TEST_CHROOT/apex"
97
98    # Remove /dev from chroot.
99    remove_filesystem_from_chroot dev tmpfs true
100
101    # Remove /sys/kernel/debug from chroot.
102    # The /sys/kernel/debug directory under the chroot dir cannot be
103    # deleted, as it is part of the host device's /sys filesystem.
104    remove_filesystem_from_chroot sys/kernel/debug debugfs false
105    # Remove /sys from chroot.
106    remove_filesystem_from_chroot sys sysfs true
107
108    # Remove /proc from chroot.
109    remove_filesystem_from_chroot proc proc true
110
111    # Remove /etc from chroot.
112    adb shell rm -f "$ART_TEST_CHROOT/etc"
113    adb shell rm -rf "$ART_TEST_CHROOT/system/etc"
114
115    # Remove directories used for ART testing in chroot.
116    adb shell rm -rf "$ART_TEST_CHROOT/data/local/tmp"
117    adb shell rm -rf "$ART_TEST_CHROOT/data/dalvik-cache"
118    adb shell rm -rf "$ART_TEST_CHROOT/tmp"
119
120    # Remove property_contexts file(s) from chroot.
121    property_context_files="/property_contexts \
122      /system/etc/selinux/plat_property_contexts \
123      /vendor/etc/selinux/nonplat_property_context \
124      /plat_property_contexts \
125      /nonplat_property_contexts"
126    for f in $property_context_files; do
127      adb shell rm -f "$ART_TEST_CHROOT$f"
128    done
129
130
131    # Kill processes still running in the chroot.
132
133    # kill_process PID CMDLINE
134    # ------------------------
135    # Kill process with given PID, that was started with CMDLINE.
136    kill_process() {
137      local pid=$1
138      local cmdline=$2
139      echo "Killing $cmdline (PID: $pid)"
140      adb shell kill -9 "$pid"
141    }
142
143    echo -e "${green}Kill processes still running from binaries under" \
144      "chroot dir $ART_TEST_CHROOT (if any)${nc} "
145    for_all_chroot_process kill_process
146  fi
147fi
148