diff --git a/ykman/pcsc/__init__.py b/ykman/pcsc/__init__.py index 32b8738f..8dd0515d 100644 --- a/ykman/pcsc/__init__.py +++ b/ykman/pcsc/__init__.py @@ -41,6 +41,10 @@ import subprocess # nosec import logging +import os +import psutil +import signal + logger = logging.getLogger(__name__) @@ -95,7 +99,7 @@ def _open_smartcard_connection(self) -> SmartCardConnection: try: return ScardSmartCardConnection(self.reader.createConnection()) except CardConnectionException as e: - if kill_scdaemon(): + if kill_scdaemon() or kill_yubikey_agent(): return ScardSmartCardConnection(self.reader.createConnection()) raise e @@ -152,6 +156,17 @@ def kill_scdaemon(): return killed +def kill_yubikey_agent(): + killed = False + return_code = subprocess.call(["pkill", "-HUP", "yubikey-agent"]) + if return_code == 0: + killed = True + if killed: + sleep(0.1) + + return killed + + def list_readers(): try: return System.readers()