From 864059732e6d5147c1d241c40667e1e2f4a16465 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Tue, 11 Jun 2013 12:30:35 -0500 Subject: Fix tdmtsak behaviour when tsak is restarted due to hardware changes --- tdmlib/tdmtsak.cpp | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'tdmlib/tdmtsak.cpp') diff --git a/tdmlib/tdmtsak.cpp b/tdmlib/tdmtsak.cpp index b3ff08cc1..c893f86ec 100644 --- a/tdmlib/tdmtsak.cpp +++ b/tdmlib/tdmtsak.cpp @@ -140,25 +140,38 @@ int main (int argc, char *argv[]) } if (verifier_result == 0) { - // OK, the calling process is authorized to retrieve SAK data - // First, flush the buffer - mPipe_fd = open(FIFO_FILE, O_RDONLY | O_NONBLOCK); - if (checkonly) { - if (mPipe_fd < 0) { - return 6; // SAK not available - } - else { - return 0; - } + // OK, the calling process is authorized to retrieve SAK data + // First, flush the buffer + mPipe_fd = open(FIFO_FILE, O_RDONLY | O_NONBLOCK); + if (checkonly) { + if (mPipe_fd < 0) { + return 6; // SAK not available } - numread = 1; - while (numread > 0) { - numread = read(mPipe_fd, readbuf, 6); + else { + return 0; } - close(mPipe_fd); - // Now wait for SAK press + } + numread = 1; + while (numread > 0) { + numread = read(mPipe_fd, readbuf, 6); + } + close(mPipe_fd); + // Now wait for SAK press + while (mPipe_fd > -1) { mPipe_fd = open(FIFO_FILE, O_RDONLY); - while (mPipe_fd > -1) { + + if (mPipe_fd <= -1) { + // This may be a transient glitch, such as when a KVM is being toggled or a new keyboard has been added + // Wait up to 5 seconds while trying to open the pipe again + int timeout = 5; + while ((mPipe_fd <= -1) && (timeout > 0)) { + sleep(1); + mPipe_fd = open(FIFO_FILE, O_RDONLY); + timeout--; + } + } + + if (mPipe_fd > -1) { numread = read(mPipe_fd, readbuf, 6); readbuf[numread] = 0; readbuf[127] = 0; @@ -183,8 +196,10 @@ int main (int argc, char *argv[]) usleep(100); } } + close(mPipe_fd); - return 6; + } + return 6; } else { return verifier_result; -- cgit v1.2.1