diff --git a/dbus_service/dbus-service b/dbus_service/dbus-service index 3c22625..12f3ce4 100755 --- a/dbus_service/dbus-service +++ b/dbus_service/dbus-service @@ -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()