blob: 21e978586f3d028b09a945be0b92df575a61f816 [file] [log] [blame]
# Copyright 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
description "Start servod"
author "chromium-os-dev@chromium.org"
respawn
# If you change the respawn limit, change the log rotation, below.
respawn limit 3 10
# This upstart job takes in 4 args, PORT, BOARD, SERIAL and CONFIG.
# * Default PORT is 9999.
# * The config file is determined by the port, and contains settings for
# BOARD (mandatory) and SERIAL (optional, unless there's more than one
# servo) and CONFIG (optional) used for passing an extra overlay file
# to load.
# * Parameters are taken from the config file.
# * Parameters on the `start` command will initialize/replace settings in the
# config file.
env PORT=9999
env BOARD=
env MODEL=
env SERIAL=
env CONFIG=
env CONFIG_FILE_DIR="/var/lib/servod"
import PORT
import BOARD
import MODEL
import SERIAL
import CONFIG
instance $PORT
pre-start script
mkdir -p /var/lib/servod
for CMD in iptables ip6tables ; do
$CMD -A INPUT -p tcp --dport $PORT -j ACCEPT ||
logger -t "${UPSTART_JOB}" "Failed to configure $CMD."
done
CONFIG_FILE=$CONFIG_FILE_DIR/config_$PORT
. /usr/share/cros/servod_utils.sh
# We'll want to update the config file with all the args passed in.
update_config $CONFIG_FILE BOARD $BOARD
update_config $CONFIG_FILE MODEL $MODEL
update_config $CONFIG_FILE SERIAL $SERIAL
update_config $CONFIG_FILE CONFIG $CONFIG
cache_servov4_hub $CONFIG_FILE $SERIAL
end script
script
LOG=/var/log/servod_$PORT.log
SERVO_V4_VIDPID=18d1:501b
. /usr/share/cros/servod_utils.sh
# We set OLD to ".4" to correspond to the respawn limit of 3
# above. That way, if we die from the respawn limit, we'll have
# logs from all failed respawns, plus the original log that
# presumably contains the first failure.
OLD=.4
for NEW in .3 .2 .1 ''; do
if [ -f $LOG$NEW ]; then
mv -f $LOG$NEW $LOG$OLD
fi
OLD=$NEW
done
exec >$LOG 2>&1
CONFIG_FILE=$CONFIG_FILE_DIR/config_$PORT
if [ ! -f $CONFIG_FILE ]; then
logger -t "$UPSTART_JOB" "No configuration file ($CONFIG_FILE); terminating"
stop
exit 0
fi
# Config file must have the board and serial (in multi-servo environment)
# declared like so:
# BOARD=veyron_minnie
# SERIAL=1234
. $CONFIG_FILE
if [ -z "$BOARD" ]; then
logger -t "$UPSTART_JOB" "No board specified; terminating"
stop
exit 0
fi
SERIAL_FLAG=""
SERIAL_MSG=""
if [ -z "$SERIAL" ]; then
logger -t "$UPSTART_JOB" "No serial specified"
else
SERIAL_FLAG="--serialname ${SERIAL}"
SERIAL_MSG="using servo serial $SERIAL"
fi
logger -t "$UPSTART_JOB" \
"Launching servod for $BOARD on port $PORT $SERIAL_MSG"
BOARD_FLAG="--board ${BOARD}"
PORT_FLAG="--port ${PORT}"
if [ ! -z "$MODEL" ]; then
MODEL_FLAG="--model ${MODEL}"
else
MODEL_FLAG=""
fi
if [ "$DEBUG" = "1" ]; then
DEBUG_FLAG="--debug"
else
DEBUG_FLAG=""
fi
CONFIG_FLAG=""
if [ ! -z "$CONFIG" ]; then
CONFIG_FLAG="--config ${CONFIG}"
fi
if [ -n "$SERIAL" ]; then
# Slam this device just in case.
# If the serial number is non-responsve, no problem, we cached the
# path on startup.
slam_servov4_hub "${HUB}"
# Check for the presence of a servo_v4 with this serialno.
if lsusb -d "${SERVO_V4_VIDPID}" -v | grep -q "${SERIAL}"; then
logger -t "$UPSTART_JOB" "Check for Servo V4 update"
servo_updater -b servo_v4 -s "${SERIAL}" --reboot
# We need to wait for the servo to finish booting and
# export its usb endpoints before calling servod.
sleep 5
fi
fi
exec servod --host 0.0.0.0 $BOARD_FLAG $MODEL_FLAG $SERIAL_FLAG $PORT_FLAG $DEBUG_FLAG $CONFIG_FLAG
end script
post-stop script
for CMD in iptables ip6tables ; do
$CMD -D INPUT -p tcp --dport $PORT -j ACCEPT || true
done
end script