net_test: fix job control in console's bash terminal

This makes Ctrl+C and Ctrl+Z work.

(while we're at it add some more debugging print out to ease
 debugging and better explain how the transition works)

Change-Id: I5d6d82a53161f7ff1bc6f1b5329ba864d0759a48
diff --git a/net/test/net_test.sh b/net/test/net_test.sh
index c0c8c47..72c67a9 100755
--- a/net/test/net_test.sh
+++ b/net/test/net_test.sh
@@ -1,10 +1,113 @@
 #!/bin/bash
+if [[ -n "${verbose}" ]]; then
+  echo 'Current working directory:'
+  echo " - according to builtin:  [$(pwd)]"
+  echo " - according to /bin/pwd: [$(/bin/pwd)]"
+  echo
+
+  echo 'Shell environment:'
+  env
+  echo
+
+  echo -n "net_test.sh (pid $$, parent ${PPID}, tty $(tty)) running [$0] with args:"
+  for arg in "$@"; do
+    echo -n " [${arg}]"
+  done
+  echo
+  echo
+fi
+
+if [[ "$(tty)" == '/dev/console' ]]; then
+  ARCH="$(uname -m)"
+  # Underscore is illegal in hostname, replace with hyphen
+  ARCH="${ARCH//_/-}"
+
+  # setsid + /dev/tty{,AMA,S}0 allows bash's job control to work, ie. Ctrl+C/Z
+  if [[ -c '/dev/tty0' ]]; then
+    # exists in UML, does not exist on graphics/vga/curses-less QEMU
+    CON='/dev/tty0'
+    hostname "uml-${ARCH}"
+  elif [[ -c '/dev/ttyAMA0' ]]; then
+    # Qemu for arm (note: /dev/ttyS0 also exists for exitcode)
+    CON='/dev/ttyAMA0'
+    hostname "qemu-${ARCH}"
+  elif [[ -c '/dev/ttyS0' ]]; then
+    # Qemu for x86 (note: /dev/ttyS1 also exists for exitcode)
+    CON='/dev/ttyS0'
+    hostname "qemu-${ARCH}"
+  else
+    # Can't figure it out, job control won't work, tough luck
+    echo 'Unable to figure out proper console - job control will not work.' >&2
+    CON=''
+    hostname "local-${ARCH}"
+  fi
+
+  unset ARCH
+
+  echo -n "$(hostname): Currently tty[/dev/console], but it should be [${CON}]..."
+
+  if [[ -n "${CON}" ]]; then
+    # Redirect std{in,out,err} to the console equivalent tty
+    # which actually supports all standard tty ioctls
+    exec <"${CON}" >&"${CON}"
+
+    # Bash wants to be session leader, hence need for setsid
+    echo " re-executing..."
+    exec /usr/bin/setsid "$0" "$@"
+    # If the above exec fails, we just fall through...
+    # (this implies failure to *find* setsid, not error return from bash,
+    #  in practice due to image construction this cannot happen)
+  else
+    echo
+  fi
+
+  # In case we fall through, clean up
+  unset CON
+fi
+
+if [[ -n "${verbose}" ]]; then
+  echo 'TTY settings:'
+  stty
+  echo
+
+  echo 'TTY settings (verbose):'
+  stty -a
+  echo
+
+  echo 'Restoring TTY sanity...'
+fi
+
+stty sane
+stty 115200
+[[ -z "${console_cols}" ]] || stty columns "${console_cols}"
+[[ -z "${console_rows}" ]] || stty rows    "${console_rows}"
+
+if [[ -n "${verbose}" ]]; then
+  echo
+
+  echo 'TTY settings:'
+  stty
+  echo
+
+  echo 'TTY settings (verbose):'
+  stty -a
+  echo
+fi
+
+# By the time we get here we should have a sane console:
+#  - 115200 baud rate
+#  - appropriate (and known) width and height (note: this assumes
+#    that the terminal doesn't get further resized)
+#  - it is no longer /dev/console, so job control should function
+#    (this means working ctrl+c [abort] and ctrl+z [suspend])
+
+
 # This defaults to 60 which is needlessly long during boot
 # (we will reset it back to the default later)
 echo 0 > /proc/sys/kernel/random/urandom_min_reseed_secs
 
 if [[ -n "${entropy}" ]]; then
-  echo "adding entropy from hex string [${entropy}]" 1>&2
+  echo "adding entropy from hex string [${entropy}]" >&2
 
   # In kernel/include/uapi/linux/random.h RNDADDENTROPY is defined as
   # _IOW('R', 0x03, int[2]) =(R is 0x52)= 0x40085203 = 1074287107
diff --git a/net/test/run_net_test.sh b/net/test/run_net_test.sh
index 0712655..17b44d9 100755
--- a/net/test/run_net_test.sh
+++ b/net/test/run_net_test.sh
@@ -105,6 +105,12 @@
 nobuild=0
 norun=0
 
+if tty >/dev/null; then
+  verbose=
+else
+  verbose=1
+fi
+
 while [[ -n "$1" ]]; do
   if [[ "$1" == "--builder" ]]; then
     consolemode="con=null,fd:1"
@@ -122,6 +128,12 @@
   elif [[ "$1" == "--norun" ]]; then
     norun=1
     shift
+  elif [[ "$1" == "--verbose" ]]; then
+    verbose=1
+    shift
+  elif [[ "$1" == "--noverbose" ]]; then
+    verbose=
+    shift
   else
     test=$1
     break  # Arguments after the test file are passed to the test itself.
@@ -153,7 +165,7 @@
 
 if ! isRunningTest && ! isBuildOnly; then
   echo "Usage:" >&2
-  echo "  $0 [--builder] [--readonly|--ro|--readwrite|--rw] [--nobuild] <test>" >&2
+  echo "  $0 [--builder] [--readonly|--ro|--readwrite|--rw] [--nobuild] [--verbose] <test>" >&2
   echo "  $0 --norun" >&2
   exit 1
 fi
@@ -257,6 +269,11 @@
 if (( nowrite == 1 )); then
   cmdline="ro"
 fi
+
+if (( verbose == 1 )); then
+  cmdline="$cmdline verbose=1"
+fi
+
 cmdline="$cmdline init=/sbin/net_test.sh"
 cmdline="$cmdline net_test_args=\"$test_args\" net_test_mode=$testmode"
 
@@ -308,6 +325,12 @@
   blockdevice="-drive file=$SCRIPT_DIR/$ROOTFS,format=raw,if=none,id=drive-virtio-disk0$blockdevice"
   blockdevice="$blockdevice -device virtio-blk-pci,drive=drive-virtio-disk0"
 
+  # Pass through our current console/screen size to inner shell session
+  read rows cols < <(stty size 2>/dev/null)
+  [[ -z "${rows}" ]] || cmdline="${cmdline} console_rows=${rows}"
+  [[ -z "${cols}" ]] || cmdline="${cmdline} console_cols=${cols}"
+  unset rows cols
+
   # QEMU has no way to modify its exitcode; simulate it with a serial port.
   #
   # Choose to do it this way over writing a file to /host, because QEMU will
@@ -335,18 +358,22 @@
 
   $qemu >&2 -name net_test -m 512 \
     -kernel $KERNEL_BINARY \
-    -no-user-config -nodefaults -no-reboot -display none \
+    -no-user-config -nodefaults -no-reboot \
+    -display none -nographic -serial mon:stdio -parallel none \
     -smp 4,sockets=4,cores=1,threads=1 \
     -device virtio-rng-pci \
     -chardev file,id=exitcode,path=exitcode \
     -device pci-serial,chardev=exitcode \
     -fsdev local,security_model=mapped-xattr,id=fsdev0,fmode=0644,dmode=0755,path=$SCRIPT_DIR \
     -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=host \
-    $blockdevice $netconfig -serial stdio -append "$cmdline"
-  [ -s exitcode ] && exitcode=`cat exitcode | tr -d '\r'` || exitcode=1
+    $blockdevice $netconfig -append "$cmdline"
+  [[ -s exitcode ]] && exitcode=`cat exitcode | tr -d '\r'` || exitcode=1
   rm -f exitcode
 fi
 
 # UML reliably screws up the ptys, QEMU probably can as well...
 fixup_ptys
+stty sane || :
+
+echo "Returning exit code ${exitcode}." 1>&2
 exit "${exitcode}"