From bcc68e697378eb9dc2bfa9cebad061835438f04d Mon Sep 17 00:00:00 2001 From: Brandon Skari Date: Mon, 15 May 2017 15:35:51 -0600 Subject: [PATCH] Try to open /dev/mem before calling rpitx mailbox.c tries to open /dev/mem and calls exit if it doesn't have permission, which will forcefully exit the Python runtime. Try to open it and throwing an exception if it fails before umping into C to give users a chance to recover. Also, because avconv is writing to a pipe, having the read end of the pipe unexpectedly close breaks the terminal. I kept having to run `reset` to get it back. --- src/python/rpitx/_hidden.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/python/rpitx/_hidden.py b/src/python/rpitx/_hidden.py index b4c1b24..2601513 100644 --- a/src/python/rpitx/_hidden.py +++ b/src/python/rpitx/_hidden.py @@ -6,6 +6,7 @@ import libavwrapper import logging import os import subprocess +import sys import threading PIPE = 'pipe:1' @@ -18,6 +19,22 @@ def broadcast_fm(media_file_name, frequency): logging.basicConfig() logger = logging.getLogger('rpitx') + # Python < 3.3 throws IOError instead of PermissionError + if sys.version_info.major <= 2 or (sys.version_info.major == 3 and sys.version_info.minor < 3): + Error = IOError + else: + Error = PermissionError + + # mailbox.c calls exit if /dev/mem can't be opened, which causes the avconv + # pipe to be left open and messes with the terminal, so try it here first + # just to be safe + try: + open('/dev/mem') + except Error: + raise Error( + 'This program should be run as root. Try prefixing command with: sudo' + ) + dev_null = open(os.devnull, 'w') if subprocess.call(('which', 'ffmpeg'), stdout=dev_null) == 0: Input = ffmpegwrapper.Input