Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 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 | |
Orion Hodson | 1c5b1ea | 2020-01-22 15:34:33 +0000 | [diff] [blame] | 17 | # This script undoes (most of) the work done by tools/buildbot-setup-device.sh. |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 18 | # Make sure to keep these files in sync. |
| 19 | |
Orion Hodson | 5697fab | 2019-11-19 15:01:02 +0000 | [diff] [blame] | 20 | if [ -t 1 ]; then |
| 21 | # Color sequences if terminal is a tty. |
| 22 | green='\033[0;32m' |
| 23 | nc='\033[0m' |
| 24 | fi |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 25 | |
| 26 | # Setup as root, as some actions performed here require it. |
| 27 | adb root |
| 28 | adb wait-for-device |
| 29 | |
| 30 | if [[ -n "$ART_TEST_CHROOT" ]]; then |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 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 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 34 | if adb shell test -d "$ART_TEST_CHROOT"; then |
| 35 | # Display users of the chroot dir. |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 36 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 37 | echo -e "${green}List open files under chroot dir $ART_TEST_CHROOT${nc}" |
| 38 | adb shell lsof | grep "$ART_TEST_CHROOT" |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 39 | |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 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 |
Roland Levillain | 7d9052c | 2018-08-14 13:57:33 +0100 | [diff] [blame] | 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 |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 56 | } |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 57 | |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 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 |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 69 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 70 | # Tear down the chroot dir. |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 71 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 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 | |
Martin Stjernholm | 8c7e219 | 2020-06-26 15:54:16 +0100 | [diff] [blame] | 95 | # Remove /bin symlink from chroot. |
| 96 | adb shell rm -f "$ART_TEST_CHROOT/bin" |
| 97 | |
Roland Levillain | 7b7ea79 | 2019-01-08 19:47:50 +0000 | [diff] [blame] | 98 | # Remove /apex from chroot. |
Roland Levillain | 0f9823e | 2019-06-18 16:49:24 +0100 | [diff] [blame] | 99 | adb shell rm -rf "$ART_TEST_CHROOT/apex" |
Roland Levillain | 7b7ea79 | 2019-01-08 19:47:50 +0000 | [diff] [blame] | 100 | |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 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 |
Roland Levillain | 9ebb41f | 2018-06-15 17:50:58 +0100 | [diff] [blame] | 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 |
Roland Levillain | f647c4c | 2018-06-12 14:44:20 +0100 | [diff] [blame] | 149 | fi |
Roland Levillain | 5604938 | 2018-05-23 18:26:22 +0100 | [diff] [blame] | 150 | fi |