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