| #!/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. | |
| #### CATEGORY=Software delivery | |
| ### start `pave` and `serve-updates` in a single command | |
| ## usage: fx serve [-v] [-l host[:port]] [-a target] | |
| ## -l host:port for "pm serve" to listen on | |
| ## --no-auto-config do not configure this host as a package server on the device | |
| ## -v enable more verbose output (must be first argument) | |
| ## -a target ipv6 address for "fx pave --no-bind" | |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? | |
| 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 | |
| serve_args=() | |
| pave_args=() | |
| fx-standard-switches "$@" | |
| set -- "${FX_ARGV[@]}" | |
| while (($#)); do | |
| case "$1" in | |
| -v|-vv|--verbose) | |
| serve_args+=("$1") | |
| ;; | |
| --no-auto-config) | |
| serve_args+=("$1") | |
| ;; | |
| -l) | |
| serve_args+=("$1" "$2") | |
| shift | |
| ;; | |
| -a) | |
| pave_args+=("--no-bind" "$1" "$2") | |
| shift | |
| ;; | |
| *) | |
| echo 2>&1 "Unknown argument: \"${1}\" ignored" | |
| ;; | |
| esac | |
| shift | |
| done | |
| fx-command-exec pave "${pave_args[@]}"& | |
| pave_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 "${pave_pid}" "${serve_pid}"; do | |
| if ! kill -0 $pid 2> /dev/null; then | |
| exit | |
| fi | |
| done | |
| done | |
| # See EXIT trap above for cleanup that occurs |