Skip to content

Commit

Permalink
vicc: catch ctrl-c via KeyboardInterrupt
Browse files Browse the repository at this point in the history
... and ignore EINT in recv as it may be triggered for various other reasons

fixes frankmorgner#95
  • Loading branch information
frankmorgner committed Apr 5, 2017
1 parent 3abf29d commit 377774f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion virtualsmartcard/src/vpicc/vicc.in
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ vicc = VirtualICC(args.datasetfile, args.type, hostname, args.port,
ef_cardsecurity=ef_cardsecurity_data, ca_key=ca_key_data, cvca=cvca,
disable_checks=args.disable_ta_checks, esign_ca_cert=esign_ca_cert,
esign_cert=esign_cert, logginglevel=logginglevel)
vicc.run()
try:
vicc.run()
except KeyboardInterrupt:
pass
24 changes: 15 additions & 9 deletions virtualsmartcard/src/vpicc/virtualsmartcard/VirtualSmartcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#

import atexit
import errno
import logging
import signal
import socket
import struct
import sys
Expand Down Expand Up @@ -475,14 +475,8 @@ def __init__(self, datasetfile, card_type, host, port,

logging.info("Connected to virtual PCD at %s:%u", host, port)

signal.signal(signal.SIGINT, self.signalHandler)
atexit.register(self.stop)

def signalHandler(self, sig=None, frame=None):
"""Basically this signal handler just surpresses a traceback from being
printed when the user presses crtl-c"""
sys.exit()

@staticmethod
def connectToPort(host, port):
"""
Expand All @@ -509,15 +503,27 @@ def __sendToVPICC(self, msg):
def __recvFromVPICC(self):
""" Receive a message from the vpcd """
# receive message size
sizestr = self.sock.recv(_Csizeof_short)
while True:
try:
sizestr = self.sock.recv(_Csizeof_short)
except socket.error as e:
if e.errno == errno.EINTR:
continue
break
if len(sizestr) == 0:
logging.info("Virtual PCD shut down")
raise socket.error
size = struct.unpack('!H', sizestr)[0]

# receive and return message
if size:
msg = self.sock.recv(size)
while True:
try:
msg = self.sock.recv(size)
except socket.error as e:
if e.errno == errno.EINTR:
continue
break
if len(msg) == 0:
logging.info("Virtual PCD shut down")
raise socket.error
Expand Down

0 comments on commit 377774f

Please sign in to comment.