blob: 7d0bf676474a890d8a9011db5f36c7c122ae70d5 [file] [log] [blame]
# Copyright 2017 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 sensors for a container"
author "chromium-os-dev@chromium.org"
start on started arc-boot-continue
stop on stopped arc-boot-continue
# The following environment variable is passed when this job is
# started from arc-boot-continue.
import CONTAINER_PID
pre-start script
{
echo "Pre-start arc-sensor"
set -x
# Sensor setting.
# Allow sending flush and frequency change commands:
find "/sys/devices" -path '*cros-ec-*iio*' \
\( -name 'flush' -o -name 'hwfifo_flush' -o \
-name 'frequency' -o -name 'sampling_frequency' -o \
-name 'hwfifo_timeout' \) \
-exec chgrp arc-sensor {} \+ -exec chmod g+w {} \+ \
-exec chcon -t cros_sensor_hal_sysfs {} \+
# Allow write access to the Significant Motion Detector. Without this the
# instance complains of not being able to listen / register to the sensor.
find "/sys/devices" -path '*cros-ec-*activity*' \
\( -name 'in_activity_still_change_falling_en' \) \
-exec chgrp arc-sensor {} \+ -exec chmod g+w {} \+ \
-exec chcon -t cros_sensor_hal_sysfs {} \+
ring_buffer_dir="$(find /sys/devices -path '*cros-ec-ring*' -name buffer)"
ring_dir=$(dirname "${ring_buffer_dir}")
if [ -d "${ring_buffer_dir}" ]; then
for name in "../trigger/current_trigger" "enable" "length"; do
attr="${ring_buffer_dir}/${name}"
if [ ! -e "${attr}" ]; then
continue
fi
chgrp arc-sensor "${attr}"
chmod g+rw "${attr}"
chcon -t cros_sensor_hal_sysfs "${attr}"
done
# Enable all channels for ARC++, be sure the buffer is not.
echo 0 > "${ring_buffer_dir}/enable"
for channel in ${ring_dir}/scan_elements/*_en; do
echo 1 > "${channel}"
done
ring_dev=$(cat "${ring_dir}/dev")
# TODO(gwendal): This is fragile, needs to find a better way.
session_manager="/sys/fs/cgroup/devices/session_manager_containers"
echo "c ${ring_dev} r" >> "${session_manager}/android/devices.allow"
fi
# TODO(yusukes): The script above can be executed on start-arc-instance.
# Copy the iio buffer device in the container.
# TODO(lhchavez): Move this to arc-setup to avoid all the fork(2)+execve(2)
# calls.
if [ -d "${ring_buffer_dir}" ]; then
ring=$(basename "${ring_dir}")
if [ -n "${ring}" ]; then
node_major=$(cat "${ring_dir}/dev" | cut -d: -f1)
node_minor=$(cat "${ring_dir}/dev" | cut -d: -f2)
# Create the iio buffer device files in the inner namespace.
nsenter -t "${CONTAINER_PID}" --mount --pid /system/bin/mknod -m 0700 \
"/dev/${ring}" c "${node_major}" "${node_minor}"
# user system, group system.
user=$(id -u android-root)
group=$(id -g android-root)
nsenter -t "${CONTAINER_PID}" --mount --pid /system/bin/chown \
$((user + 1000)):$((group + 1000)) "/dev/${ring}"
nsenter -t "${CONTAINER_PID}" --mount --pid /system/bin/restorecon \
"/dev/${ring}"
fi
fi
# Signal the container that sensor devices are ready.
exec nsenter -t "${CONTAINER_PID}" --mount --pid -- \
/system/bin/touch /dev/.arc_sensor_ready
} 2>&1 | logger -t "${UPSTART_JOB}"
end script
# This job does not have script/exec stanza, so it is considered running forever
# once started. See: http://upstart.ubuntu.com/cookbook/#jobs-that-run-forever
post-stop script
{
echo "Post-stop arc-sensor"
set +e -x
ring_buffer_dir="$(find /sys/devices -path '*cros-ec-ring*' -name buffer)"
if [ -d "${ring_buffer_dir}" ]; then
# Disable the ring buffer, nobody will process the entries anymore.
# Android Sensor HAL has code to disable the buffer, but it is never
# called.
echo 0 > "${ring_buffer_dir}/enable"
fi
} 2>&1 | logger -t "${UPSTART_JOB}"
end script