• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)" == '/dev/console' ]]; then
21  ARCH="$(uname -m)"
22  # Underscore is illegal in hostname, replace with hyphen
23  ARCH="${ARCH//_/-}"
24
25  # setsid + /dev/tty{,AMA,S}0 allows bash's job control to work, ie. Ctrl+C/Z
26  if [[ -c '/dev/tty0' ]]; then
27    # exists in UML, does not exist on graphics/vga/curses-less QEMU
28    CON='/dev/tty0'
29    hostname "uml-${ARCH}"
30  elif [[ -c '/dev/ttyAMA0' ]]; then
31    # Qemu for arm (note: /dev/ttyS0 also exists for exitcode)
32    CON='/dev/ttyAMA0'
33    hostname "qemu-${ARCH}"
34  elif [[ -c '/dev/ttyS0' ]]; then
35    # Qemu for x86 (note: /dev/ttyS1 also exists for exitcode)
36    CON='/dev/ttyS0'
37    hostname "qemu-${ARCH}"
38  else
39    # Can't figure it out, job control won't work, tough luck
40    echo 'Unable to figure out proper console - job control will not work.' >&2
41    CON=''
42    hostname "local-${ARCH}"
43  fi
44
45  unset ARCH
46
47  echo -n "$(hostname): Currently tty[/dev/console], but it should be [${CON}]..."
48
49  if [[ -n "${CON}" ]]; then
50    # Redirect std{in,out,err} to the console equivalent tty
51    # which actually supports all standard tty ioctls
52    exec <"${CON}" >&"${CON}"
53
54    # Bash wants to be session leader, hence need for setsid
55    echo " re-executing..."
56    exec /usr/bin/setsid "$0" "$@"
57    # If the above exec fails, we just fall through...
58    # (this implies failure to *find* setsid, not error return from bash,
59    #  in practice due to image construction this cannot happen)
60  else
61    echo
62  fi
63
64  # In case we fall through, clean up
65  unset CON
66fi
67
68if [[ -n "${verbose}" ]]; then
69  echo 'TTY settings:'
70  stty
71  echo
72
73  echo 'TTY settings (verbose):'
74  stty -a
75  echo
76
77  echo 'Restoring TTY sanity...'
78fi
79
80stty sane
81stty 115200
82[[ -z "${console_cols}" ]] || stty columns "${console_cols}"
83[[ -z "${console_rows}" ]] || stty rows    "${console_rows}"
84
85if [[ -n "${verbose}" ]]; then
86  echo
87
88  echo 'TTY settings:'
89  stty
90  echo
91
92  echo 'TTY settings (verbose):'
93  stty -a
94  echo
95fi
96
97# By the time we get here we should have a sane console:
98#  - 115200 baud rate
99#  - appropriate (and known) width and height (note: this assumes
100#    that the terminal doesn't get further resized)
101#  - it is no longer /dev/console, so job control should function
102#    (this means working ctrl+c [abort] and ctrl+z [suspend])
103
104
105# This defaults to 60 which is needlessly long during boot
106# (we will reset it back to the default later)
107echo 0 > /proc/sys/kernel/random/urandom_min_reseed_secs
108
109if [[ -n "${entropy}" ]]; then
110  echo "adding entropy from hex string [${entropy}]" >&2
111
112  # In kernel/include/uapi/linux/random.h RNDADDENTROPY is defined as
113  # _IOW('R', 0x03, int[2]) =(R is 0x52)= 0x40085203 = 1074287107
114  /usr/bin/python 3>/dev/random <<EOF
115import fcntl, struct
116rnd = '${entropy}'.decode('base64')
117fcntl.ioctl(3, 0x40085203, struct.pack('ii', len(rnd) * 8, len(rnd)) + rnd)
118EOF
119
120fi
121
122# Make sure the urandom pool has a chance to initialize before we reset
123# the reseed timer back to 60 seconds.  One timer tick should be enough.
124sleep 1.1
125
126# By this point either 'random: crng init done' (newer kernels)
127# or 'random: nonblocking pool is initialized' (older kernels)
128# should have been printed out to dmesg/console.
129
130# Reset it back to boot time default
131echo 60 > /proc/sys/kernel/random/urandom_min_reseed_secs
132
133
134# In case IPv6 is compiled as a module.
135[ -f /proc/net/if_inet6 ] || insmod $DIR/kernel/net-next/net/ipv6/ipv6.ko
136
137# Minimal network setup.
138ip link set lo up
139ip link set lo mtu 16436
140ip link set eth0 up
141
142# Allow people to run ping.
143echo "0 65536" > /proc/sys/net/ipv4/ping_group_range
144
145# Read environment variables passed to the kernel to determine if script is
146# running on builder and to find which test to run.
147
148if [ "$net_test_mode" != "builder" ]; then
149  # Fall out to a shell once the test completes or if there's an error.
150  trap "exec /bin/bash" ERR EXIT
151fi
152
153echo -e "Running $net_test $net_test_args\n"
154$net_test $net_test_args
155
156# Write exit code of net_test to a file so that the builder can use it
157# to signal failure if any tests fail.
158echo $? >$net_test_exitcode
159