blob: 7eb5cc33c16d6835c166b30b5c989025da4ff08a [file] [log] [blame]
Roland Levillain56049382018-05-23 18:26:22 +01001#!/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
Roland Levillain56049382018-05-23 18:26:22 +010028 # 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
Roland Levillainf647c4c2018-06-12 14:44:20 +010031 if adb shell test -d "$ART_TEST_CHROOT"; then
32 # Display users of the chroot dir.
Roland Levillain56049382018-05-23 18:26:22 +010033
Roland Levillainf647c4c2018-06-12 14:44:20 +010034 echo -e "${green}List open files under chroot dir $ART_TEST_CHROOT${nc}"
35 adb shell lsof | grep "$ART_TEST_CHROOT"
Roland Levillain56049382018-05-23 18:26:22 +010036
Roland Levillain9ebb41f2018-06-15 17:50:58 +010037 # 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
Roland Levillain7d9052c2018-08-14 13:57:33 +010045 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
Roland Levillain9ebb41f2018-06-15 17:50:58 +010053 }
Roland Levillain56049382018-05-23 18:26:22 +010054
Roland Levillain9ebb41f2018-06-15 17:50:58 +010055 # 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
Roland Levillain56049382018-05-23 18:26:22 +010066
Roland Levillainf647c4c2018-06-12 14:44:20 +010067 # Tear down the chroot dir.
Roland Levillain56049382018-05-23 18:26:22 +010068
Roland Levillainf647c4c2018-06-12 14:44:20 +010069 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
Roland Levillain7b7ea792019-01-08 19:47:50 +000092 # Remove /apex from chroot.
93 remove_filesystem_from_chroot apex tmpfs true
94
Roland Levillainf647c4c2018-06-12 14:44:20 +010095 # 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
Roland Levillain9ebb41f2018-06-15 17:50:58 +0100126
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
Roland Levillainf647c4c2018-06-12 14:44:20 +0100143 fi
Roland Levillain56049382018-05-23 18:26:22 +0100144fi