diff --git a/util/ec3po/console.py b/util/ec3po/console.py index 962d042ff3..39278e343d 100755 --- a/util/ec3po/console.py +++ b/util/ec3po/console.py @@ -635,32 +635,42 @@ def StartLoop(console): """ console.logger.info('EC Console is being served on %s.', console.user_pty) console.logger.debug(console) - while True: - # Check to see if pipes or the console are ready for reading. - read_list = [console.master_pty, console.cmd_pipe, console.dbg_pipe] - ready_for_reading = select.select(read_list, [], [])[0] + try: + while True: + # Check to see if pipes or the console are ready for reading. + read_list = [console.master_pty, console.cmd_pipe, console.dbg_pipe] + ready_for_reading = select.select(read_list, [], [])[0] - for obj in ready_for_reading: - if obj is console.master_pty: - console.logger.debug('Input from user') - # Convert to bytes so we can look for non-printable chars such as - # Ctrl+A, Ctrl+E, etc. - line = bytearray(os.read(console.master_pty, CONSOLE_MAX_READ)) - for i in line: - # Handle each character as it arrives. - console.HandleChar(i) + for obj in ready_for_reading: + if obj is console.master_pty: + console.logger.debug('Input from user') + # Convert to bytes so we can look for non-printable chars such as + # Ctrl+A, Ctrl+E, etc. + line = bytearray(os.read(console.master_pty, CONSOLE_MAX_READ)) + for i in line: + # Handle each character as it arrives. + console.HandleChar(i) - elif obj is console.cmd_pipe: - data = console.cmd_pipe.recv() - # Write it to the user console. - console.logger.debug('|CMD|->\'%s\'', data) - os.write(console.master_pty, data) + elif obj is console.cmd_pipe: + data = console.cmd_pipe.recv() + # Write it to the user console. + console.logger.debug('|CMD|->\'%s\'', data) + os.write(console.master_pty, data) - elif obj is console.dbg_pipe: - data = console.dbg_pipe.recv() - # Write it to the user console. - console.logger.debug('|DBG|->\'%s\'', data) - os.write(console.master_pty, data) + elif obj is console.dbg_pipe: + data = console.dbg_pipe.recv() + # Write it to the user console. + console.logger.debug('|DBG|->\'%s\'', data) + os.write(console.master_pty, data) + + finally: + # Close pipes. + console.dbg_pipe.close() + console.cmd_pipe.close() + # Close file descriptor. + os.close(console.master_pty) + # Exit. + sys.exit(0) def main(argv): @@ -676,7 +686,6 @@ def main(argv): # Set up argument parser. parser = argparse.ArgumentParser(description=('Start interactive EC console ' 'and interpreter.')) - # TODO(aaboagye): Eventually get this from servod. parser.add_argument('ec_uart_pty', help=('The full PTY name that the EC UART' ' is present on. eg: /dev/pts/12')) diff --git a/util/ec3po/interpreter.py b/util/ec3po/interpreter.py index 31079fb908..b71607f254 100644 --- a/util/ec3po/interpreter.py +++ b/util/ec3po/interpreter.py @@ -20,6 +20,7 @@ import logging import os import Queue import select +import sys COMMAND_RETRIES = 3 # Number of attempts to retry a command. @@ -312,19 +313,29 @@ def StartLoop(interp): Args: interp: An Interpreter object that has been properly initialised. """ - while True: - readable, writeable, _ = select.select(interp.inputs, interp.outputs, []) + try: + while True: + readable, writeable, _ = select.select(interp.inputs, interp.outputs, []) - for obj in readable: - # Handle any debug prints from the EC. - if obj is interp.ec_uart_pty: - interp.HandleECData() + for obj in readable: + # Handle any debug prints from the EC. + if obj is interp.ec_uart_pty: + interp.HandleECData() - # Handle any commands from the user. - elif obj is interp.cmd_pipe: - interp.HandleUserData() + # Handle any commands from the user. + elif obj is interp.cmd_pipe: + interp.HandleUserData() - for obj in writeable: - # Send a command to the EC. - if obj is interp.ec_uart_pty: - interp.SendCmdToEC() + for obj in writeable: + # Send a command to the EC. + if obj is interp.ec_uart_pty: + interp.SendCmdToEC() + + finally: + # Close pipes. + interp.cmd_pipe.close() + interp.dbg_pipe.close() + # Close file descriptor. + interp.ec_uart_pty.close() + # Exit. + sys.exit(0)