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
20. "$(dirname $0)/buildbot-utils.sh"
21
22if [ "$1" = --verbose ]; then
23  verbose=true
24else
25  verbose=false
26fi
27
28# Testing on a Linux VM requires special setup.
29if [[ -n "$ART_TEST_ON_VM" ]]; then
30  [[ -d "$ART_TEST_VM_DIR" ]] || { msgfatal "no VM found in $ART_TEST_VM_DIR"; }
31  $ART_SSH_CMD "true" || { msgerror "no VM (tried \"$ART_SSH_CMD true\""; }
32  $ART_SSH_CMD "
33    mkdir $ART_TEST_CHROOT
34
35    mkdir $ART_TEST_CHROOT/apex
36    mkdir $ART_TEST_CHROOT/bin
37    mkdir -p $ART_TEST_CHROOT/data/local/tmp
38    mkdir $ART_TEST_CHROOT/dev
39    mkdir $ART_TEST_CHROOT/etc
40    mkdir $ART_TEST_CHROOT/lib
41    mkdir $ART_TEST_CHROOT/linkerconfig
42    mkdir $ART_TEST_CHROOT/proc
43    mkdir $ART_TEST_CHROOT/sys
44    mkdir $ART_TEST_CHROOT/system
45    mkdir $ART_TEST_CHROOT/tmp
46    mkdir -p $ART_TEST_CHROOT/usr/lib
47    mkdir -p $ART_TEST_CHROOT/usr/share/gdb
48
49    sudo mount -t proc /proc $ART_TEST_CHROOT_BASENAME/proc
50    sudo mount -t sysfs /sys $ART_TEST_CHROOT_BASENAME/sys
51    sudo mount --bind /dev $ART_TEST_CHROOT_BASENAME/dev
52    sudo mount --bind /bin $ART_TEST_CHROOT_BASENAME/bin
53    sudo mount --bind /lib $ART_TEST_CHROOT_BASENAME/lib
54    sudo mount --bind /lib $ART_TEST_CHROOT_BASENAME/usr/lib
55    sudo mount --bind /usr/share/gdb $ART_TEST_CHROOT_BASENAME/usr/share/gdb
56    $ART_CHROOT_CMD echo \"Hello from chroot! I am \$(uname -a).\"
57  "
58  exit 0
59fi
60
61# Regular Android device. Setup as root, as some actions performed here require it.
62adb version
63adb root
64adb wait-for-device
65
66msginfo "Date on host"
67date
68
69msginfo "Date on device"
70adb shell date
71
72host_seconds_since_epoch=$(date -u +%s)
73
74# Get the device time in seconds, but filter the output as some
75# devices emit CRLF at the end of the command which then breaks the
76# time comparisons in this script (Hammerhead, MRA59G 2457013).
77device_seconds_since_epoch=$(adb shell date -u +%s | tr -c -d '[:digit:]')
78
79abs_time_difference_in_seconds=$(expr $host_seconds_since_epoch - $device_seconds_since_epoch)
80if [ $abs_time_difference_in_seconds -lt 0 ]; then
81  abs_time_difference_in_seconds=$(expr 0 - $abs_time_difference_in_seconds)
82fi
83
84seconds_per_hour=3600
85
86# b/187295147 : Disable live-lock kill daemon.
87# It can confuse long running processes for issues and kill them.
88# This usually manifests as temporarily lost adb connection.
89msginfo "Killing llkd, seen killing adb"
90adb shell setprop ctl.stop llkd-0
91adb shell setprop ctl.stop llkd-1
92
93product_name=$(adb shell getprop ro.build.product)
94
95if [ "x$product_name" = xfugu ]; then
96  # Kill logd first, so that when we set the adb buffer size later in this file,
97  # it is brought up again.
98  msginfo "Killing logd, seen leaking on fugu/N"
99  adb shell pkill -9 -U logd logd && msginfo "...logd killed"
100fi
101
102# Update date on device if the difference with host is more than one hour.
103if [ $abs_time_difference_in_seconds -gt $seconds_per_hour ]; then
104  msginfo "Update date on device"
105  adb shell date -u @$host_seconds_since_epoch
106fi
107
108msginfo "Turn off selinux"
109adb shell setenforce 0
110$verbose && adb shell getenforce
111
112msginfo "Setting local loopback"
113adb shell ifconfig lo up
114$verbose && adb shell ifconfig
115
116if $verbose; then
117  msginfo "List properties"
118  adb shell getprop
119
120  msginfo "Uptime"
121  adb shell uptime
122
123  msginfo "Battery info"
124  adb shell dumpsys battery
125fi
126
127# Fugu only handles buffer size up to 16MB.
128if [ "x$product_name" = xfugu ]; then
129  buffer_size=16MB
130else
131  buffer_size=32MB
132fi
133
134msginfo "Setting adb buffer size to ${buffer_size}"
135adb logcat -G ${buffer_size}
136$verbose && adb logcat -g
137
138msginfo "Removing adb spam filter"
139adb logcat -P ""
140$verbose && adb logcat -p
141
142msginfo "Kill stalled dalvikvm processes"
143# 'ps' on M can sometimes hang.
144timeout 5s adb shell "ps" >/dev/null
145if [[ $? == 124 ]] && [[ "$ART_TEST_RUN_ON_ARM_FVP" != true ]]; then
146  msginfo "Rebooting device to fix 'ps'"
147  adb reboot
148  adb wait-for-device root
149else
150  processes=$(adb shell "ps" | grep dalvikvm | awk '{print $2}')
151  for i in $processes; do adb shell kill -9 $i; done
152fi
153
154# Chroot environment.
155# ===================
156
157if [[ -n "$ART_TEST_CHROOT" ]]; then
158  # Prepare the chroot dir.
159  msginfo "Prepare the chroot dir in $ART_TEST_CHROOT"
160
161  # Check that ART_TEST_CHROOT is correctly defined.
162  [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
163
164  # Create chroot.
165  adb shell mkdir -p "$ART_TEST_CHROOT"
166
167  # Provide property_contexts file(s) in chroot.
168  # This is required to have Android system properties work from the chroot.
169  # Notes:
170  # - In Android N, only '/property_contexts' is expected.
171  # - In Android O+, property_context files are expected under /system and /vendor.
172  # (See bionic/libc/bionic/system_properties.cpp or
173  # bionic/libc/system_properties/contexts_split.cpp for more information.)
174  property_context_files="/property_contexts \
175    /system/etc/selinux/plat_property_contexts \
176    /vendor/etc/selinux/nonplat_property_context \
177    /plat_property_contexts \
178    /nonplat_property_contexts"
179  for f in $property_context_files; do
180    adb shell test -f "$f" \
181      "&&" mkdir -p "$ART_TEST_CHROOT$(dirname $f)" \
182      "&&" cp -f "$f" "$ART_TEST_CHROOT$f"
183  done
184
185  # Create directories required for ART testing in chroot.
186  adb shell mkdir -p "$ART_TEST_CHROOT/tmp"
187  adb shell mkdir -p "$ART_TEST_CHROOT/data/dalvik-cache"
188  adb shell mkdir -p "$ART_TEST_CHROOT/data/local/tmp"
189
190  # Populate /etc in chroot with required files.
191  adb shell mkdir -p "$ART_TEST_CHROOT/system/etc"
192  adb shell test -L "$ART_TEST_CHROOT/etc" \
193    || adb shell ln -s system/etc "$ART_TEST_CHROOT/etc"
194
195  # Provide /proc in chroot.
196  adb shell mkdir -p "$ART_TEST_CHROOT/proc"
197  adb shell mount | grep -q "^proc on $ART_TEST_CHROOT/proc type proc " \
198    || adb shell mount -t proc proc "$ART_TEST_CHROOT/proc"
199
200  # Provide /sys in chroot.
201  adb shell mkdir -p "$ART_TEST_CHROOT/sys"
202  adb shell mount | grep -q "^sysfs on $ART_TEST_CHROOT/sys type sysfs " \
203    || adb shell mount -t sysfs sysfs "$ART_TEST_CHROOT/sys"
204  # Provide /sys/kernel/debug in chroot.
205  adb shell mount | grep -q "^debugfs on $ART_TEST_CHROOT/sys/kernel/debug type debugfs " \
206    || adb shell mount -t debugfs debugfs "$ART_TEST_CHROOT/sys/kernel/debug"
207  # Provide /sys/kernel/tracing in chroot. Using a bind mount is important,
208  # otherwise mounting tracefs multiple times confuses the
209  # android.hardware.atrace service.
210  adb shell mount | grep -q "^tracefs on $ART_TEST_CHROOT/sys/kernel/tracing type tracefs " \
211    || adb shell mount -o bind /sys/kernel/tracing "$ART_TEST_CHROOT/sys/kernel/tracing"
212
213  # Provide /dev in chroot.
214  adb shell mkdir -p "$ART_TEST_CHROOT/dev"
215  adb shell mount | grep -q "^tmpfs on $ART_TEST_CHROOT/dev type tmpfs " \
216    || adb shell mount -o bind /dev "$ART_TEST_CHROOT/dev"
217  adb shell mount | grep -q "^devpts on $ART_TEST_CHROOT/dev/pts type devpts " \
218    || adb shell mount -o bind /dev/pts "$ART_TEST_CHROOT/dev/pts"
219  adb shell mount | grep -q " on $ART_TEST_CHROOT/dev/cpuset type cgroup " \
220    || adb shell mount -o bind /dev/cpuset "$ART_TEST_CHROOT/dev/cpuset"
221
222  # Create /apex directory in chroot.
223  adb shell mkdir -p "$ART_TEST_CHROOT/apex"
224
225  # Create /linkerconfig directory in chroot.
226  adb shell mkdir -p "$ART_TEST_CHROOT/linkerconfig"
227
228  # Create /bin symlink for shebang compatibility.
229  adb shell test -L "$ART_TEST_CHROOT/bin" \
230    || adb shell ln -s system/bin "$ART_TEST_CHROOT/bin"
231fi
232