1#!/bin/bash 2# 3# Copyright (C) 2015 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# The work does by this script is (mostly) undone by tools/buildbot-teardown-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 red='\033[0;31m' 23 green='\033[0;32m' 24 yellow='\033[0;33m' 25 nc='\033[0m' 26fi 27 28if [ "$1" = --verbose ]; then 29 verbose=true 30else 31 verbose=false 32fi 33 34# Setup as root, as some actions performed here require it. 35adb root 36adb wait-for-device 37 38echo -e "${green}Date on host${nc}" 39date 40 41echo -e "${green}Date on device${nc}" 42adb shell date 43 44host_seconds_since_epoch=$(date -u +%s) 45device_seconds_since_epoch=$(adb shell date -u +%s) 46 47abs_time_difference_in_seconds=$(expr $host_seconds_since_epoch - $device_seconds_since_epoch) 48if [ $abs_time_difference_in_seconds -lt 0 ]; then 49 abs_time_difference_in_seconds=$(expr 0 - $abs_time_difference_in_seconds) 50fi 51 52seconds_per_hour=3600 53 54# b/187295147 : Disable live-lock kill daemon. 55# It can confuse long running processes for issues and kill them. 56# This usually manifests as temporarily lost adb connection. 57echo -e "${green}Killing llkd, seen killing adb${nc}" 58adb shell setprop ctl.stop llkd-0 59adb shell setprop ctl.stop llkd-1 60 61# Kill logd first, so that when we set the adb buffer size later in this file, 62# it is brought up again. 63echo -e "${green}Killing logd, seen leaking on fugu/N${nc}" 64adb shell pkill -9 -U logd logd && echo -e "${green}...logd killed${nc}" 65 66# Update date on device if the difference with host is more than one hour. 67if [ $abs_time_difference_in_seconds -gt $seconds_per_hour ]; then 68 echo -e "${green}Update date on device${nc}" 69 adb shell date -u @$host_seconds_since_epoch 70fi 71 72echo -e "${green}Turn off selinux${nc}" 73adb shell setenforce 0 74$verbose && adb shell getenforce 75 76echo -e "${green}Setting local loopback${nc}" 77adb shell ifconfig lo up 78$verbose && adb shell ifconfig 79 80if $verbose; then 81 echo -e "${green}List properties${nc}" 82 adb shell getprop 83 84 echo -e "${green}Uptime${nc}" 85 adb shell uptime 86 87 echo -e "${green}Battery info${nc}" 88 adb shell dumpsys battery 89fi 90 91# Fugu only handles buffer size up to 16MB. 92product_name=$(adb shell getprop ro.build.product) 93 94if [ "x$product_name" = xfugu ]; then 95 buffer_size=16MB 96else 97 buffer_size=32MB 98fi 99 100echo -e "${green}Setting adb buffer size to ${buffer_size}${nc}" 101adb logcat -G ${buffer_size} 102$verbose && adb logcat -g 103 104echo -e "${green}Removing adb spam filter${nc}" 105adb logcat -P "" 106$verbose && adb logcat -p 107 108echo -e "${green}Kill stalled dalvikvm processes${nc}" 109# 'ps' on M can sometimes hang. 110timeout 2s adb shell "ps" >/dev/null 111if [[ $? == 124 ]] && [[ "$ART_TEST_RUN_ON_ARM_FVP" != true ]]; then 112 echo -e "${green}Rebooting device to fix 'ps'${nc}" 113 adb reboot 114 adb wait-for-device root 115else 116 processes=$(adb shell "ps" | grep dalvikvm | awk '{print $2}') 117 for i in $processes; do adb shell kill -9 $i; done 118fi 119 120# Chroot environment. 121# =================== 122 123if [[ -n "$ART_TEST_CHROOT" ]]; then 124 # Prepare the chroot dir. 125 echo -e "${green}Prepare the chroot dir in $ART_TEST_CHROOT${nc}" 126 127 # Check that ART_TEST_CHROOT is correctly defined. 128 [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; } 129 130 # Create chroot. 131 adb shell mkdir -p "$ART_TEST_CHROOT" 132 133 # Provide property_contexts file(s) in chroot. 134 # This is required to have Android system properties work from the chroot. 135 # Notes: 136 # - In Android N, only '/property_contexts' is expected. 137 # - In Android O+, property_context files are expected under /system and /vendor. 138 # (See bionic/libc/bionic/system_properties.cpp or 139 # bionic/libc/system_properties/contexts_split.cpp for more information.) 140 property_context_files="/property_contexts \ 141 /system/etc/selinux/plat_property_contexts \ 142 /vendor/etc/selinux/nonplat_property_context \ 143 /plat_property_contexts \ 144 /nonplat_property_contexts" 145 for f in $property_context_files; do 146 adb shell test -f "$f" \ 147 "&&" mkdir -p "$ART_TEST_CHROOT$(dirname $f)" \ 148 "&&" cp -f "$f" "$ART_TEST_CHROOT$f" 149 done 150 151 # Create directories required for ART testing in chroot. 152 adb shell mkdir -p "$ART_TEST_CHROOT/tmp" 153 adb shell mkdir -p "$ART_TEST_CHROOT/data/dalvik-cache" 154 adb shell mkdir -p "$ART_TEST_CHROOT/data/local/tmp" 155 156 # Populate /etc in chroot with required files. 157 adb shell mkdir -p "$ART_TEST_CHROOT/system/etc" 158 adb shell test -L "$ART_TEST_CHROOT/etc" \ 159 || adb shell ln -s system/etc "$ART_TEST_CHROOT/etc" 160 161 # Provide /proc in chroot. 162 adb shell mkdir -p "$ART_TEST_CHROOT/proc" 163 adb shell mount | grep -q "^proc on $ART_TEST_CHROOT/proc type proc " \ 164 || adb shell mount -t proc proc "$ART_TEST_CHROOT/proc" 165 166 # Provide /sys in chroot. 167 adb shell mkdir -p "$ART_TEST_CHROOT/sys" 168 adb shell mount | grep -q "^sysfs on $ART_TEST_CHROOT/sys type sysfs " \ 169 || adb shell mount -t sysfs sysfs "$ART_TEST_CHROOT/sys" 170 # Provide /sys/kernel/debug in chroot. 171 adb shell mount | grep -q "^debugfs on $ART_TEST_CHROOT/sys/kernel/debug type debugfs " \ 172 || adb shell mount -t debugfs debugfs "$ART_TEST_CHROOT/sys/kernel/debug" 173 174 # Provide /dev in chroot. 175 adb shell mkdir -p "$ART_TEST_CHROOT/dev" 176 adb shell mount | grep -q "^tmpfs on $ART_TEST_CHROOT/dev type tmpfs " \ 177 || adb shell mount -o bind /dev "$ART_TEST_CHROOT/dev" 178 179 # Create /apex directory in chroot. 180 adb shell mkdir -p "$ART_TEST_CHROOT/apex" 181 182 # Create /linkerconfig directory in chroot. 183 adb shell mkdir -p "$ART_TEST_CHROOT/linkerconfig" 184 185 # Create /bin symlink for shebang compatibility. 186 adb shell test -L "$ART_TEST_CHROOT/bin" \ 187 || adb shell ln -s system/bin "$ART_TEST_CHROOT/bin" 188fi 189