Make tsak far more robust

pull/1/head
Timothy Pearson 13 years ago
parent 8685539c5e
commit 7acb35ad4d

@ -43,8 +43,8 @@ License along with tsak. If not, see http://www.gnu.org/licenses/.
typedef unsigned char byte; typedef unsigned char byte;
bool mPipeOpen_out; bool mPipeOpen_out = false;
int mPipe_fd_out; int mPipe_fd_out = -1;
struct sigaction usr_action; struct sigaction usr_action;
sigset_t block_mask; sigset_t block_mask;
@ -137,10 +137,12 @@ int find_keyboard() {
void tearDownPipe() void tearDownPipe()
{ {
if (mPipeOpen_out == true) {
mPipeOpen_out = false; mPipeOpen_out = false;
close(mPipe_fd_out); close(mPipe_fd_out);
unlink(FIFO_FILE_OUT); unlink(FIFO_FILE_OUT);
} }
}
void setupPipe() void setupPipe()
{ {
@ -151,12 +153,32 @@ void setupPipe()
mknod(FIFO_FILE_OUT, S_IFIFO|0600, 0); mknod(FIFO_FILE_OUT, S_IFIFO|0600, 0);
chmod(FIFO_FILE_OUT, 0600); chmod(FIFO_FILE_OUT, 0600);
mPipe_fd_out = open(FIFO_FILE_OUT, O_RDWR | O_NONBLOCK); mPipe_fd_out = open(FIFO_FILE_OUT, O_WRONLY | O_NONBLOCK);
if (mPipe_fd_out > -1) { if (mPipe_fd_out > -1) {
mPipeOpen_out = true; mPipeOpen_out = true;
} }
} }
bool checkLocks()
{
int fdlock;
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 1;
fdlock = open(FIFO_FILE_OUT, O_RDWR | O_NONBLOCK);
if (fdlock != -1) {
if(fcntl(fdlock, F_SETLK, &fl) == -1) {
return 0;
}
}
return 1;
}
class PipeHandler class PipeHandler
{ {
public: public:
@ -166,7 +188,6 @@ public:
PipeHandler::PipeHandler() PipeHandler::PipeHandler()
{ {
setupPipe();
} }
PipeHandler::~PipeHandler() PipeHandler::~PipeHandler()
@ -193,6 +214,11 @@ int main (int argc, char *argv[])
} }
} }
if (!checkLocks()) {
fprintf(stderr, "Another instance of this program is already running\n");
return 8;
}
// Create the output pipe // Create the output pipe
PipeHandler controlpipe; PipeHandler controlpipe;
@ -214,7 +240,7 @@ int main (int argc, char *argv[])
else { else {
// Print Device Name // Print Device Name
ioctl (fd, EVIOCGNAME (sizeof (name)), name); ioctl (fd, EVIOCGNAME (sizeof (name)), name);
printf ("Reading From : (%s)\n", name); fprintf(stderr, "Reading From : (%s)\n", name);
// Create filtered virtual output device // Create filtered virtual output device
devout=open("/dev/misc/uinput",O_WRONLY|O_NONBLOCK); devout=open("/dev/misc/uinput",O_WRONLY|O_NONBLOCK);
@ -229,6 +255,15 @@ int main (int argc, char *argv[])
else else
return 3; return 3;
} }
else {
if(ioctl(fd, EVIOCGRAB, 2) < 0) {
close(fd);
fprintf(stderr, "Failed to grab exclusive input device lock");
if (established)
sleep(1);
else
return 1;
}
else { else {
ioctl(fd, EVIOCGNAME(UINPUT_MAX_NAME_SIZE), devinfo.name); ioctl(fd, EVIOCGNAME(UINPUT_MAX_NAME_SIZE), devinfo.name);
strncat(devinfo.name, "+tsak", UINPUT_MAX_NAME_SIZE-1); strncat(devinfo.name, "+tsak", UINPUT_MAX_NAME_SIZE-1);
@ -247,15 +282,9 @@ int main (int argc, char *argv[])
else { else {
fprintf(stderr,"Device created.\n"); fprintf(stderr,"Device created.\n");
if(ioctl(fd, EVIOCGRAB, 2) < 0) { if (established == false) {
close(fd); setupPipe();
fprintf(stderr, "Failed to grab exclusive input device lock");
if (established)
sleep(1);
else
return 1;
} }
else {
established = true; established = true;

Loading…
Cancel
Save