From dd00c560ebed243a7b3098e890eb9afc5003f164 Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Tue, 30 May 2017 01:00:23 -0400 Subject: [PATCH] add path timeout for sensitive devices --- auxilary.py | 65 ++++++++++++++++++++++++++++------------------------ listeners.py | 6 ++--- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/auxilary.py b/auxilary.py index b69a1b6..91962bb 100644 --- a/auxilary.py +++ b/auxilary.py @@ -49,46 +49,51 @@ class CountdownTimer(Thread): def __del__(self): self.stop() + +def waitForPath(path, timeout=30): + for i in range(0, timeout): + if os.path.exists(path): + return + time.sleep(1) + raise FileNotFoundError('Could not find {} after {} seconds'.format(path, timeout)) -def freeBusyPath(path, logger=None): - # check if any other processes are using file path - # if found, politely ask them to exit, else nuke them +# we shouldn't need this if we are disabling the usb hub globally and nuking any past connections +#~ def freeBusyPath(path, logger=None): + #~ # check if any other processes are using file path + #~ # if found, politely ask them to exit, else nuke them - # NOTE: fuser sends diagnostic info (eg filenames and modes...which we - # don't want) to stderr. This is weird, but let's me route to /dev/null - # so I don't have to parse it later - try: - stdout = check_output(['fuser', path], universal_newlines=True, stderr=DEVNULL) - except CalledProcessError: - logger.debug('%s not in use. Execution may continue', path) - else: - # assume stdout is one PID first - try: - processes = [psutil.Process(int(stdout))] + #~ # NOTE: fuser sends diagnostic info (eg filenames and modes...which we + #~ # don't want) to stderr. This is weird, but let's me route to /dev/null + #~ # so I don't have to parse it later + #~ try: + #~ stdout = check_output(['fuser', path], universal_newlines=True, stderr=DEVNULL) + #~ except CalledProcessError: + #~ logger.debug('%s not in use. Execution may continue', path) + #~ else: + #~ # assume stdout is one PID first + #~ try: + #~ processes = [psutil.Process(int(stdout))] - # else assume we have multiple PIDs separated by arbitrary space - except ValueError: - processes = [psutil.Process(int(s)) for s in stdout.split()] + #~ # else assume we have multiple PIDs separated by arbitrary space + #~ except ValueError: + #~ processes = [psutil.Process(int(s)) for s in stdout.split()] - for p in processes: - if logger: - logger.warning('%s in use by PID %s. Sending SIGTERM', path, p.pid) - p.terminate() + #~ for p in processes: + #~ if logger: + #~ logger.warning('%s in use by PID %s. Sending SIGTERM', path, p.pid) + #~ p.terminate() - dead, alive = psutil.wait_procs(processes, timeout=10) + #~ dead, alive = psutil.wait_procs(processes, timeout=10) - for p in alive: - if logger: - logger.warning('Failed to terminate PID %s. Sending SIGKILL', p.pid) - p.kill() + #~ for p in alive: + #~ if logger: + #~ logger.warning('Failed to terminate PID %s. Sending SIGKILL', p.pid) + #~ p.kill() -# crude way to 'unplug and plug back in' for USB device +# crude way to reset USB device, pretty rough but works def resetUSBDevice(device): devpath = os.path.join('/sys/bus/usb/devices/' + device + '/authorized') with open(devpath, 'w') as f: f.write('0') with open(devpath, 'w') as f: f.write('1') - -def fallbackLogger(module, loglevel, msg): - print('[{}] [{}] [FALLBACK LOGGER]: {}'.format(module, loglevel, msg)) diff --git a/listeners.py b/listeners.py index fda6ad3..b117f01 100644 --- a/listeners.py +++ b/listeners.py @@ -2,7 +2,7 @@ import logging, os, sys, stat from threading import Thread from evdev import InputDevice, ecodes from select import select -from auxilary import freeBusyPath +from auxilary import waitForPath, freeBusyPath import stateMachine logger = logging.getLogger(__name__) @@ -22,9 +22,9 @@ class KeypadListener(Thread): } devPath = '/dev/input/by-id/usb-04d9_1203-event-kbd' - - freeBusyPath(devPath, logger) + waitForPath(devPath) + self._dev = InputDevice(devPath) self._dev.grab()