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