blob: a20d43fa806f921e587cd32fd0610319594d6386 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
### start `boot` and `serve-updates` in a single command
## usage: fx serve [-v] [-d device] [--direct]
## -v enable more verbose output (must be first argument)
## -d target a specifc device
## --no-direct configure target to use an SSH tunnel as transport (deprecated)
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh
fx-config-read
kill_child_processes() {
child_pids=$(jobs -p)
if [[ -n "${child_pids}" ]]; then
# Note: child_pids must be expanded to args here.
kill ${child_pids} 2> /dev/null
wait 2> /dev/null
fi
}
trap kill_child_processes EXIT
boot_args=()
serve_args=()
device=""
fx-standard-switches "$@"
set -- "${FX_ARGV[@]}"
while (($#)); do
case "$1" in
-d|--device)
serve_args+=("$1")
shift
if [[ "$1" != *"-"* ]]; then
echo "Device argument must be a name, eg. 'acid-grab-stuck-line'"
exit 1
fi
serve_args+=("$1")
boot_args+=("-a")
device="$(get-device-addr --device $1 -z)"
if [[ ! -n "${device}" ]]; then
echo "'$1' does not resolve"
exit 1
fi
sep="%"
prefix="${device%%$sep*}"
device="${device:0:${#prefix}}"
boot_args+=("${device}")
;;
-v|--verbose)
serve_args+=("$1")
;;
--no-direct|--direct)
serve_args+=("$1")
;;
*)
boot_args+=("$1")
;;
esac
shift
done
fx-command-exec boot "${boot_args[@]}" &
boot_pid=$!
fx-command-exec serve-updates "${serve_args[@]}" &
serve_pid=$!
while true; do
sleep 1
# If any child exits, then exit the whole process, causing other children to
# be cleaned up by the exit trap.
for pid in "${boot_pid}" "${serve_pid}"; do
if ! kill -0 $pid 2> /dev/null; then
exit
fi
done
done
# See EXIT trap above for cleanup that occurs