1#!/bin/bash 2if [[ -n "${verbose}" ]]; then 3 echo 'Current working directory:' 4 echo " - according to builtin: [$(pwd)]" 5 echo " - according to /bin/pwd: [$(/bin/pwd)]" 6 echo 7 8 echo 'Shell environment:' 9 env 10 echo 11 12 echo -n "net_test.sh (pid $$, parent ${PPID}, tty $(tty)) running [$0] with args:" 13 for arg in "$@"; do 14 echo -n " [${arg}]" 15 done 16 echo 17 echo 18fi 19 20if [[ "$(tty)" == 'not a tty' ]]; then 21 echo 'not a tty? perhaps not quite real kernel default /dev/console - trying to fix.' 22 if [[ -c /dev/console ]]; then 23 [[ "$(readlink /proc/$$/fd/0)" != '/dev/console' ]] || exec < /dev/console 24 [[ "$(readlink /proc/$$/fd/1)" != '/dev/console' ]] || exec > /dev/console 25 [[ "$(readlink /proc/$$/fd/2)" != '/dev/console' ]] || exec 2> /dev/console 26 fi 27fi 28 29if [[ "$(tty)" == '/dev/console' ]]; then 30 ARCH="$(uname -m)" 31 # Underscore is illegal in hostname, replace with hyphen 32 ARCH="${ARCH//_/-}" 33 34 # setsid + /dev/tty{,AMA,S}0 allows bash's job control to work, ie. Ctrl+C/Z 35 if [[ -e '/proc/exitcode' ]]; then 36 # exists only in UML 37 CON='/dev/tty0' 38 hostname "uml-${ARCH}" 39 elif [[ -c '/dev/ttyAMA0' ]]; then 40 # Qemu for arm (note: /dev/ttyS0 also exists for exitcode) 41 CON='/dev/ttyAMA0' 42 hostname "qemu-${ARCH}" 43 elif [[ -c '/dev/ttyS0' ]]; then 44 # Qemu for x86 (note: /dev/ttyS1 also exists for exitcode) 45 CON='/dev/ttyS0' 46 hostname "qemu-${ARCH}" 47 else 48 # Can't figure it out, job control won't work, tough luck 49 echo 'Unable to figure out proper console - job control will not work.' >&2 50 CON='' 51 hostname "local-${ARCH}" 52 fi 53 54 unset ARCH 55 56 echo -n "$(hostname): Currently tty[/dev/console], but it should be [${CON}]..." 57 58 if [[ -n "${CON}" ]]; then 59 # Redirect std{in,out,err} to the console equivalent tty 60 # which actually supports all standard tty ioctls 61 exec <"${CON}" >&"${CON}" 62 63 # Bash wants to be session leader, hence need for setsid 64 echo " re-executing..." 65 exec /usr/bin/setsid "$0" "$@" 66 # If the above exec fails, we just fall through... 67 # (this implies failure to *find* setsid, not error return from bash, 68 # in practice due to image construction this cannot happen) 69 else 70 echo 71 fi 72 73 # In case we fall through, clean up 74 unset CON 75fi 76 77if [[ -n "${verbose}" ]]; then 78 echo 'TTY settings:' 79 stty 80 echo 81 82 echo 'TTY settings (verbose):' 83 stty -a 84 echo 85 86 echo 'Restoring TTY sanity...' 87fi 88 89stty sane 90stty 115200 91[[ -z "${console_cols}" ]] || stty columns "${console_cols}" 92[[ -z "${console_rows}" ]] || stty rows "${console_rows}" 93 94if [[ -n "${verbose}" ]]; then 95 echo 96 97 echo 'TTY settings:' 98 stty 99 echo 100 101 echo 'TTY settings (verbose):' 102 stty -a 103 echo 104fi 105 106# By the time we get here we should have a sane console: 107# - 115200 baud rate 108# - appropriate (and known) width and height (note: this assumes 109# that the terminal doesn't get further resized) 110# - it is no longer /dev/console, so job control should function 111# (this means working ctrl+c [abort] and ctrl+z [suspend]) 112 113 114# This defaults to 60 which is needlessly long during boot 115# (we will reset it back to the default later) 116echo 0 > /proc/sys/kernel/random/urandom_min_reseed_secs 117 118if [[ -n "${entropy}" ]]; then 119 echo "adding entropy from hex string [${entropy}]" >&2 120 121 # In kernel/include/uapi/linux/random.h RNDADDENTROPY is defined as 122 # _IOW('R', 0x03, int[2]) =(R is 0x52)= 0x40085203 = 1074287107 123 /usr/bin/python3 3>/dev/random <<EOF 124import base64, fcntl, struct 125rnd = base64.b64decode('${entropy}') 126fcntl.ioctl(3, 0x40085203, struct.pack('ii', len(rnd) * 8, len(rnd)) + rnd) 127EOF 128 129fi 130 131# Make sure the urandom pool has a chance to initialize before we reset 132# the reseed timer back to 60 seconds. One timer tick should be enough. 133sleep 1.1 134 135# By this point either 'random: crng init done' (newer kernels) 136# or 'random: nonblocking pool is initialized' (older kernels) 137# should have been printed out to dmesg/console. 138 139# Reset it back to boot time default 140echo 60 > /proc/sys/kernel/random/urandom_min_reseed_secs 141 142# Make sure /sys is mounted 143[[ -d /sys/fs ]] || mount -t sysfs sysfs -o nosuid,nodev,noexec /sys 144 145if ! [[ "$(uname -r)" =~ ^([0-3]|4[.][0-8])[.] ]]; then 146 # Mount the bpf filesystem on Linux version 4.9+ 147 mount -t bpf bpf -o nosuid,nodev,noexec /sys/fs/bpf 148fi 149 150if ! [[ "$(uname -r)" =~ ^([0-3]|4[.][0-9]|4[.]1[0-3])[.] ]]; then 151 # Mount the Cgroup v2 filesystem on Linux version 4.14+ 152 mount -t cgroup2 cgroup2 -o nosuid,nodev,noexec /sys/fs/cgroup 153fi 154 155# In case IPv6 is compiled as a module. 156[ -f /proc/net/if_inet6 ] || insmod $DIR/kernel/net-next/net/ipv6/ipv6.ko 157 158# Minimal network setup. 159ip link set lo up 160ip link set lo mtu 16436 161if [[ -d /sys/class/net/eth0 ]]; then 162 ip link set eth0 up 163fi 164 165# Allow people to run ping. 166echo '0 2147483647' > /proc/sys/net/ipv4/ping_group_range 167 168# Adjust tcp_rmem_default on UML as needed by Linux 6.6 169if [[ -e /proc/exitcode ]]; then 170 # UML with mem=512M defaults to '4096 131072 ~4021664' 171 read tcp_rmem_min tcp_rmem_default tcp_rmem_max < /proc/sys/net/ipv4/tcp_rmem 172 if [[ tcp_rmem_default -lt 262144 ]]; then 173 echo "${tcp_rmem_min} 262144 ${tcp_rmem_max}" > /proc/sys/net/ipv4/tcp_rmem 174 fi 175fi 176 177# Allow unprivileged use of eBPF (matches Android OS) 178if [[ "$(< /proc/sys/kernel/unprivileged_bpf_disabled)" != '0' ]]; then 179 echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled 180fi 181 182# Read environment variables passed to the kernel to determine if script is 183# running on builder and to find which test to run. 184 185if [ "$net_test_mode" != "builder" ]; then 186 # Fall out to a shell once the test completes or if there's an error. 187 trap "exec /bin/bash" ERR EXIT 188fi 189 190echo -e "Running $net_test $net_test_args\n" 191$net_test $net_test_args 192rv="$?" 193 194# Write exit code of net_test to a file so that the builder can use it 195# to signal failure if any tests fail. 196echo "${rv}" > "${exitcode}" 197 198# Additionally on UML make it the exit code of UML kernel binary itself. 199if [[ -e '/proc/exitcode' ]]; then 200 echo "${rv}" > /proc/exitcode 201fi 202