• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /bin symlink from chroot.
96    adb shell rm -f "$ART_TEST_CHROOT/bin"
97
98    # Remove /apex from chroot.
99    adb shell rm -rf "$ART_TEST_CHROOT/apex"
100
101    # Remove /dev from chroot.
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    echo -e "${green}Kill processes still running from binaries under" \
147      "chroot dir $ART_TEST_CHROOT (if any)${nc} "
148    for_all_chroot_process kill_process
149  fi
150fi
151