Provide a flag to kill webplot

This patch provides a flag for the webplot shell script to kill
the existing webplot process from the command line.

This also requires to register the interrupt handler for SIGTERM
in addition to SIGINT.

BUG=chromium:443539
TEST=Run the webplot script in a chromebook by typing
$ webplot
Then type the following command to kill the webplot process.
$ webplot -k
It should display the message about killing the webplot process.

Change-Id: I3c84d068dea5c3171ebb9bc34af56c5cc40bfc7d
Reviewed-on: https://chromium-review.googlesource.com/246431
Reviewed-by: Charlie Mooney <[email protected]>
Tested-by: Shyh-In Hwang <[email protected]>
Reviewed-by: Shyh-In Hwang <[email protected]>
Commit-Queue: Shyh-In Hwang <[email protected]>
diff --git a/webplot.sh b/webplot.sh
index bd46d02..a197316 100755
--- a/webplot.sh
+++ b/webplot.sh
@@ -4,7 +4,11 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-PROG="$(basename $0)"
+# This script may or may not come with a suffix.
+# If this script is installed with emerge, the suffix has been removed.
+# If this script is installed with scp, there still exists the suffix.
+# Try to remove the suffix any way.
+PROG="$(basename $0 .sh)"
 
 # A local die function to print the message and then exit
 die() {
@@ -12,6 +16,33 @@
   exit 1
 }
 
+# Read command flags
+. /usr/share/misc/shflags
+DEFINE_boolean kill false 'kill the existing webplot process' 'k'
+
+FLAGS_HELP="USAGE: $PROG [flags]"
+
+FLAGS "$@" || exit 1
+eval set -- "${FLAGS_ARGV}"
+set -e
+
+get_webplot_process_status() {
+  echo $(ps a | egrep "python\s.+${PROG}" | grep -v grep)
+}
+
+if [ "$FLAGS_kill" = "$FLAGS_TRUE" ]; then
+  process=$(get_webplot_process_status)
+  if [ -z "$process" ]; then
+    echo 'No existing webplot process.'
+  else
+    for p in "$process"; do
+      echo killing $p
+      kill $(echo $p | awk '{print $1}')
+    done
+  fi
+  exit 0
+fi
+
 # Search the webplot directory.
 # Stop at the first found webplot directory. Priority is given to /usr/lib*.
 DIRS="/usr/lib* /usr/local/lib*"
@@ -28,7 +59,7 @@
 fi
 
 # Start webplot if not yet.
-if [ -n "$(ps a | egrep "python\s+${PROG}" | grep -v grep)" ]; then
+if [ -n "$(get_webplot_process_status)" ]; then
   echo "$PROG server has been started."
 else
   # Must run webplot as root as it needs to access system device nodes.
@@ -37,13 +68,13 @@
   fi
 
   echo "Start $PROG server..."
-  sudo python "${PROG_DIR}"/"${PROG}".py &
+  python "${PROG_DIR}"/"${PROG}".py &
 
   # Wait a while for the webplot server to get ready before launching
   # a chrome tab to connect to it.
   sleep 1
-fi
 
-# Tell the user to type URL in chrome as there is no reliable way to
-# launch a chrome tab from command line in chrome os.
-echo "Please type \"localhost\" in the browser."
+  # Tell the user to type URL in chrome as there is no reliable way to
+  # launch a chrome tab from command line in chrome os.
+  echo "Please type \"localhost\" in the browser."
+fi
diff --git a/webplot/webplot.py b/webplot/webplot.py
index a38405b..b27b236 100755
--- a/webplot/webplot.py
+++ b/webplot/webplot.py
@@ -35,8 +35,8 @@
 SAVED_IMAGE = '/tmp/webplot.png'
 
 
-def KeyboardInterruptHandler():
-  """A keyboard interrupt (SIGINT) handler
+def InterruptHandler():
+  """An interrupt handler for both SIGINT and SIGTERM
 
   The stop procedure triggered is as follows:
   1. This handler sends a 'quit' message to the listening client.
@@ -361,7 +361,8 @@
   # zombie processes.
   cherrypy.engine.subscribe('exit',  device.close)
 
-  cherrypy.engine.signal_handler.handlers['SIGINT'] = KeyboardInterruptHandler
+  cherrypy.engine.signal_handler.handlers['SIGINT'] = InterruptHandler
+  cherrypy.engine.signal_handler.handlers['SIGTERM'] = InterruptHandler
 
   cherrypy.quickstart(
     Root(args.dut_addr, args.server_port,