From 672d37789f6a9ba93550c229f72a56b0cc8e041d Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Thu, 29 Jan 2015 13:44:18 -0800 Subject: flash_ec: Add --no-ns-pid check and freeze instead of kill PTY clients The switch to PID namespaces inside the chroot broke flash_ec's ability to detect (and then kill) other processes that use the EC serial PTY (leading to potential flashing failures). After a long discussion we decided that users who need features like this should be forced to run their chroot without PID namespacing (using cros_sdk --no-ns-pid). This patch adds a hard check for this to flash_ec, so that using it in an unsafe way becomes impossible. In addition, this ports the more advanced SIGSTOP/SIGCONT logic to flash_ec that was pioneered in fwgdb. With this, other processes accessing that PTY will just freeze and become available again after flash_ec finished. BRANCH=none BUG=chromium:444931 TEST=Ran on a Jerry with and without --no-ns-pid, with and without an open EC terminal, all results as expected. Change-Id: I45ffc3ec6cfe9c25a0b82b4d5288a41485c326c4 Signed-off-by: Julius Werner Reviewed-on: https://chromium-review.googlesource.com/249835 Reviewed-by: Mike Frysinger Reviewed-by: Randall Spangler --- util/flash_ec | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/util/flash_ec b/util/flash_ec index a3ba191e12..1c0246ae92 100755 --- a/util/flash_ec +++ b/util/flash_ec @@ -199,12 +199,18 @@ ec_enable_boot0() { } # Put back the servo and the system in a clean state at exit +FROZEN_PIDS="" cleanup() { if [ -n "${save}" ]; then info "Restoring servo settings..." servo_restore "$save" fi + for pid in ${FROZEN_PIDS}; do + info "Sending SIGCONT to process ${pid}!" + kill -CONT ${pid} + done + ec_reset } trap cleanup EXIT @@ -301,29 +307,20 @@ function servo_restore() { done } -function free_pty() { - local pids - - pids=$(lsof -F p 2>/dev/null -- $1 | cut -d'p' -f2) - if [ "${pids}" == "" ]; then - return +function claim_pty() { + if grep -q cros_sdk /proc/1/cmdline; then + die "You must run this tool in a chroot that was entered with" \ + "'cros_sdk --no-ns-pid' (see crbug.com/444931 for details)" fi - # Try to kill nicely at first... - kill ${pids} - info "You'll need to re-launch console on $1" + FROZEN_PIDS=$(lsof -FR 2>/dev/null -- $1 | tr -d 'pR') - # Wait up to 3 seconds for them to die... - for i in $(seq 30); do - pids=$(lsof -F p 2>/dev/null -- $1 | cut -d'p' -f2) - if [ "${pids}" == "" ]; then - return - fi - sleep .1 + # reverse order to SIGSTOP parents before children + for pid in $(echo ${FROZEN_PIDS} | tac -s " "); do + info "Sending SIGSTOP to process ${pid}!" + sleep 0.02 + kill -STOP ${pid} done - - # Forcibly kill - kill -9 ${pids} } # Board specific flashing scripts @@ -361,7 +358,7 @@ function flash_stm32() { fi info "Using serial flasher : ${STM32MON}" - free_pty ${EC_UART} + claim_pty ${EC_UART} if [ "${SERVO_TYPE}" = "servo" ] ; then dut_control ${MCU}_uart_en:on -- cgit v1.2.1