(settings)
| 314 | return settings |
| 315 | |
| 316 | def loop(settings): |
| 317 | # Main loop that checks every 'sleep_time' seconds if computer should be killed. |
| 318 | # Allows only whitelisted usb devices to connect! |
| 319 | # Does not allow usb device that was present during program start to disconnect! |
| 320 | start_devices = lsusb() |
| 321 | acceptable_devices = start_devices + settings['whitelist'] |
| 322 | |
| 323 | # Write to logs that loop is starting: |
| 324 | msg = "[INFO] Started patrolling the USB ports every " + str(settings['sleep_time']) + " seconds..." |
| 325 | log(settings, msg) |
| 326 | print(msg) |
| 327 | |
| 328 | # Main loop |
| 329 | while True: |
| 330 | # List the current usb devices |
| 331 | current_devices = lsusb() |
| 332 | |
| 333 | # Check that all current devices are in the set of acceptable devices |
| 334 | # and their cardinality is less than or equal to what is allowed |
| 335 | for device, count in current_devices.items(): |
| 336 | if device not in acceptable_devices: |
| 337 | # A device with unknown usbid detected |
| 338 | kill_computer(settings) |
| 339 | if count > acceptable_devices[device]: |
| 340 | # Count of a usbid is larger than what is acceptable (too many devices sharing usbid) |
| 341 | kill_computer(settings) |
| 342 | |
| 343 | # Check that all start devices are still present in current devices |
| 344 | # and their cardinality still the same |
| 345 | for device, count in start_devices.items(): |
| 346 | if device not in current_devices: |
| 347 | # A usbid has disappeared completely |
| 348 | kill_computer(settings) |
| 349 | if count > current_devices[device]: |
| 350 | # Count of a usbid device is lower than at program start (not enough devices for given usbid) |
| 351 | kill_computer(settings) |
| 352 | |
| 353 | sleep(settings['sleep_time']) |
| 354 | |
| 355 | def startup_checks(): |
| 356 | # Splash |
no test coverage detected