Add a safety back off, so we don't spam device with indefinite reboot loop.

This commit is contained in:
Viktor Dragomiretskyy 2020-07-12 16:10:43 +12:00
parent 600202b4d6
commit ab73c2c47f

View file

@ -2,7 +2,9 @@
import argparse
import re
import os
import sys
import time
import dbus
import dbus.mainloop.glib
import dbus.service
@ -156,6 +158,28 @@ class Device(dbus.service.Object):
logging.debug('RunCmd')
return hexlify(tls.app(unhexlify(cmd))).decode()
backoff_file='/usr/share/python-validity/backoff'
# I don't know how to tell systemd to backoff in case of multiple instance of the same template service, help!
def backoff():
if os.path.isfile(backoff_file):
with open(backoff_file, 'r') as f:
lines = list(map(float, f.readlines()))
else:
lines=[]
while len(lines) > 0 and lines[0] + 60 < time.time():
lines=lines[1:]
lines += [time.time()]
with open(backoff_file, 'w') as f:
for l in lines:
f.write('%f\n' % l)
if len(lines) > 10:
raise Exception('Refusing to start more than 10 times per minute')
if __name__ == '__main__':
parser = argparse.ArgumentParser('Open fprintd DBus service')
parser.add_argument('--debug', help='Enable tracing', action='store_true')
@ -167,6 +191,8 @@ if __name__ == '__main__':
usb.trace_enabled = True
tls.trace_enabled = True
backoff()
try:
if args.devpath is None:
init.open()