xref: /aosp_15_r20/external/ltp/testcases/kernel/input/input_helper.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015 Cedric Hnyda <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
7*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
16*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <linux/input.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <linux/uinput.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <fnmatch.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <poll.h>
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "input_helper.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/uinput.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #define VIRTUAL_DEVICE "virtual-device-ltp"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #define VIRTUAL_DEVICE_REGEX "*virtual-device-ltp*"
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static int uinput_loaded;
35*49cdfc7eSAndroid Build Coastguard Worker static int check_device(void);
36*49cdfc7eSAndroid Build Coastguard Worker 
try_open_device(void)37*49cdfc7eSAndroid Build Coastguard Worker static int try_open_device(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	char path[256];
40*49cdfc7eSAndroid Build Coastguard Worker 	char name[256];
41*49cdfc7eSAndroid Build Coastguard Worker 	int ret, fd = -1;
42*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 1024; i++) {
45*49cdfc7eSAndroid Build Coastguard Worker 		snprintf(path, sizeof(path), "/dev/input/event%i", i);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 		fd = open(path, O_RDONLY);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 		if (fd < 0 && errno == ENOENT)
50*49cdfc7eSAndroid Build Coastguard Worker 			continue;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 		if (fd < 0) {
53*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO | TERRNO, "failed to open %s", path);
54*49cdfc7eSAndroid Build Coastguard Worker 			break;
55*49cdfc7eSAndroid Build Coastguard Worker 		}
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 		ret = ioctl(fd, EVIOCGNAME(sizeof(name)), name);
58*49cdfc7eSAndroid Build Coastguard Worker 		if (ret < 0) {
59*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO | TERRNO,
60*49cdfc7eSAndroid Build Coastguard Worker 				"ioctl(%s, EVIOCGNAME(256), ...) failed",
61*49cdfc7eSAndroid Build Coastguard Worker 				path);
62*49cdfc7eSAndroid Build Coastguard Worker 			break;
63*49cdfc7eSAndroid Build Coastguard Worker 		}
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 		if (strcmp(name, VIRTUAL_DEVICE) == 0)
66*49cdfc7eSAndroid Build Coastguard Worker 			return fd;
67*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
open_device(void)73*49cdfc7eSAndroid Build Coastguard Worker int open_device(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
76*49cdfc7eSAndroid Build Coastguard Worker 	int retries = 10;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	while (retries--) {
79*49cdfc7eSAndroid Build Coastguard Worker 		fd = try_open_device();
80*49cdfc7eSAndroid Build Coastguard Worker 		if (fd > 0)
81*49cdfc7eSAndroid Build Coastguard Worker 			return fd;
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Device not found, retrying...");
83*49cdfc7eSAndroid Build Coastguard Worker 		usleep(10000);
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TBROK, NULL, "Unable to find the input device");
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
try_load_uinput(void)89*49cdfc7eSAndroid Build Coastguard Worker static int try_load_uinput(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	const char *argv[] = {"modprobe", "uinput", NULL};
92*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Trying to load uinput kernel module");
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	ret = tst_cmd(NULL, argv, NULL, NULL, TST_CMD_PASS_RETVAL);
97*49cdfc7eSAndroid Build Coastguard Worker 	if (ret) {
98*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Failed to load the uinput module");
99*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker 
unload_uinput(void)105*49cdfc7eSAndroid Build Coastguard Worker static void unload_uinput(void)
106*49cdfc7eSAndroid Build Coastguard Worker {
107*49cdfc7eSAndroid Build Coastguard Worker 	const char *argv[] = {"modprobe", "-r", "uinput", NULL};
108*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Unloading uinput kernel module");
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	ret = tst_cmd(NULL, argv, NULL, NULL, TST_CMD_PASS_RETVAL);
113*49cdfc7eSAndroid Build Coastguard Worker 	if (ret)
114*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN, "Failed to unload uinput module");
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker static const char *uinput_paths[] = {
118*49cdfc7eSAndroid Build Coastguard Worker 	"/dev/input/uinput",
119*49cdfc7eSAndroid Build Coastguard Worker 	"/dev/uinput",
120*49cdfc7eSAndroid Build Coastguard Worker };
121*49cdfc7eSAndroid Build Coastguard Worker 
try_open_uinput(void)122*49cdfc7eSAndroid Build Coastguard Worker static int try_open_uinput(void)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
125*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(uinput_paths); i++) {
128*49cdfc7eSAndroid Build Coastguard Worker 		fd = open(uinput_paths[i], O_WRONLY | O_NONBLOCK);
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 		if (fd > 0) {
131*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Found uinput dev at %s",
132*49cdfc7eSAndroid Build Coastguard Worker 			         uinput_paths[i]);
133*49cdfc7eSAndroid Build Coastguard Worker 			return fd;
134*49cdfc7eSAndroid Build Coastguard Worker 		}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 		if (fd < 0 && errno != ENOENT) {
137*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, NULL,
138*49cdfc7eSAndroid Build Coastguard Worker 			         "open(%s)", uinput_paths[i]);
139*49cdfc7eSAndroid Build Coastguard Worker 		}
140*49cdfc7eSAndroid Build Coastguard Worker 	}
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker 
open_uinput(void)145*49cdfc7eSAndroid Build Coastguard Worker int open_uinput(void)
146*49cdfc7eSAndroid Build Coastguard Worker {
147*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
148*49cdfc7eSAndroid Build Coastguard Worker 	int retries = 10;
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	fd = try_open_uinput();
151*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
152*49cdfc7eSAndroid Build Coastguard Worker 		return fd;
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	if (try_load_uinput()) {
155*49cdfc7eSAndroid Build Coastguard Worker 		while (retries--) {
156*49cdfc7eSAndroid Build Coastguard Worker 			fd = try_open_uinput();
157*49cdfc7eSAndroid Build Coastguard Worker 			if (fd > 0) {
158*49cdfc7eSAndroid Build Coastguard Worker 				uinput_loaded = 1;
159*49cdfc7eSAndroid Build Coastguard Worker 				return fd;
160*49cdfc7eSAndroid Build Coastguard Worker 			}
161*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Uinput dev not found, retrying...");
162*49cdfc7eSAndroid Build Coastguard Worker 			usleep(10000);
163*49cdfc7eSAndroid Build Coastguard Worker 		}
164*49cdfc7eSAndroid Build Coastguard Worker 
165*49cdfc7eSAndroid Build Coastguard Worker 		unload_uinput();
166*49cdfc7eSAndroid Build Coastguard Worker 	}
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "Unable to find and open uinput");
169*49cdfc7eSAndroid Build Coastguard Worker }
170*49cdfc7eSAndroid Build Coastguard Worker 
send_event(int fd,int event,int code,int value)171*49cdfc7eSAndroid Build Coastguard Worker void send_event(int fd, int event, int code, int value)
172*49cdfc7eSAndroid Build Coastguard Worker {
173*49cdfc7eSAndroid Build Coastguard Worker 	struct input_event ev = {
174*49cdfc7eSAndroid Build Coastguard Worker 		.type = event,
175*49cdfc7eSAndroid Build Coastguard Worker 		.code = code,
176*49cdfc7eSAndroid Build Coastguard Worker 		.value = value,
177*49cdfc7eSAndroid Build Coastguard Worker 	};
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(NULL, SAFE_WRITE_ALL, fd, &ev, sizeof(ev));
180*49cdfc7eSAndroid Build Coastguard Worker }
181*49cdfc7eSAndroid Build Coastguard Worker 
send_rel_move(int fd,int x,int y)182*49cdfc7eSAndroid Build Coastguard Worker void send_rel_move(int fd, int x, int y)
183*49cdfc7eSAndroid Build Coastguard Worker {
184*49cdfc7eSAndroid Build Coastguard Worker 	send_event(fd, EV_REL, REL_X, x);
185*49cdfc7eSAndroid Build Coastguard Worker 	send_event(fd, EV_REL, REL_Y, y);
186*49cdfc7eSAndroid Build Coastguard Worker 	send_event(fd, EV_SYN, 0, 0);
187*49cdfc7eSAndroid Build Coastguard Worker }
188*49cdfc7eSAndroid Build Coastguard Worker 
check_ui_get_sysname_ioctl(int fd)189*49cdfc7eSAndroid Build Coastguard Worker static void check_ui_get_sysname_ioctl(int fd)
190*49cdfc7eSAndroid Build Coastguard Worker {
191*49cdfc7eSAndroid Build Coastguard Worker 	char sys_name[256];
192*49cdfc7eSAndroid Build Coastguard Worker 	char dev_name[256];
193*49cdfc7eSAndroid Build Coastguard Worker 	char *path;
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_GET_SYSNAME(sizeof(sys_name)), sys_name, NULL);
196*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_ASPRINTF(NULL, &path, "/sys/devices/virtual/input/%s/name", sys_name);
197*49cdfc7eSAndroid Build Coastguard Worker 
198*49cdfc7eSAndroid Build Coastguard Worker 	if (FILE_SCANF(path, "%s", dev_name)) {
199*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL|TERRNO, "Failed to read '%s'", path);
200*49cdfc7eSAndroid Build Coastguard Worker 		free(path);
201*49cdfc7eSAndroid Build Coastguard Worker 		return;
202*49cdfc7eSAndroid Build Coastguard Worker 	}
203*49cdfc7eSAndroid Build Coastguard Worker 
204*49cdfc7eSAndroid Build Coastguard Worker 	if (!strcmp(VIRTUAL_DEVICE, dev_name))
205*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "ioctl UI_GET_SYSNAME returned correct name");
206*49cdfc7eSAndroid Build Coastguard Worker 	else
207*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "ioctl UI_GET_SYSNAME returned wrong name");
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	free(path);
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker 
create_device(int fd)212*49cdfc7eSAndroid Build Coastguard Worker void create_device(int fd)
213*49cdfc7eSAndroid Build Coastguard Worker {
214*49cdfc7eSAndroid Build Coastguard Worker 	int nb;
215*49cdfc7eSAndroid Build Coastguard Worker 	struct uinput_user_dev uidev = {
216*49cdfc7eSAndroid Build Coastguard Worker 		.name = VIRTUAL_DEVICE,
217*49cdfc7eSAndroid Build Coastguard Worker 		.id = {
218*49cdfc7eSAndroid Build Coastguard Worker 			.bustype = BUS_USB,
219*49cdfc7eSAndroid Build Coastguard Worker 			.vendor = 0x1,
220*49cdfc7eSAndroid Build Coastguard Worker 			.product = 0x1,
221*49cdfc7eSAndroid Build Coastguard Worker 			.version = 1,
222*49cdfc7eSAndroid Build Coastguard Worker 		}
223*49cdfc7eSAndroid Build Coastguard Worker 	};
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(NULL, SAFE_WRITE_ALL, fd, &uidev, sizeof(uidev));
226*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_DEV_CREATE, NULL);
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker 	for (nb = 100; nb > 0; nb--) {
229*49cdfc7eSAndroid Build Coastguard Worker 		if (check_device()) {
230*49cdfc7eSAndroid Build Coastguard Worker 			check_ui_get_sysname_ioctl(fd);
231*49cdfc7eSAndroid Build Coastguard Worker 			return;
232*49cdfc7eSAndroid Build Coastguard Worker 		}
233*49cdfc7eSAndroid Build Coastguard Worker 		usleep(10000);
234*49cdfc7eSAndroid Build Coastguard Worker 	}
235*49cdfc7eSAndroid Build Coastguard Worker 
236*49cdfc7eSAndroid Build Coastguard Worker 	destroy_device(fd);
237*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TBROK, NULL, "Failed to create device");
238*49cdfc7eSAndroid Build Coastguard Worker }
239*49cdfc7eSAndroid Build Coastguard Worker 
setup_mouse_events(int fd)240*49cdfc7eSAndroid Build Coastguard Worker void setup_mouse_events(int fd)
241*49cdfc7eSAndroid Build Coastguard Worker {
242*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_KEY);
243*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_KEYBIT, BTN_LEFT);
244*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_REL);
245*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_RELBIT, REL_X);
246*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_RELBIT, REL_Y);
247*49cdfc7eSAndroid Build Coastguard Worker }
248*49cdfc7eSAndroid Build Coastguard Worker 
destroy_device(int fd)249*49cdfc7eSAndroid Build Coastguard Worker void destroy_device(int fd)
250*49cdfc7eSAndroid Build Coastguard Worker {
251*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_DEV_DESTROY, NULL);
252*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(NULL, fd);
253*49cdfc7eSAndroid Build Coastguard Worker 
254*49cdfc7eSAndroid Build Coastguard Worker 	if (uinput_loaded)
255*49cdfc7eSAndroid Build Coastguard Worker 		unload_uinput();
256*49cdfc7eSAndroid Build Coastguard Worker }
257*49cdfc7eSAndroid Build Coastguard Worker 
check_event_code(struct input_event * iev,int event,int code)258*49cdfc7eSAndroid Build Coastguard Worker int check_event_code(struct input_event *iev, int event, int code)
259*49cdfc7eSAndroid Build Coastguard Worker {
260*49cdfc7eSAndroid Build Coastguard Worker 	return iev->type == event && iev->code == code;
261*49cdfc7eSAndroid Build Coastguard Worker }
262*49cdfc7eSAndroid Build Coastguard Worker 
check_sync_event(struct input_event * iev)263*49cdfc7eSAndroid Build Coastguard Worker int check_sync_event(struct input_event *iev)
264*49cdfc7eSAndroid Build Coastguard Worker {
265*49cdfc7eSAndroid Build Coastguard Worker 	return check_event_code(iev, EV_SYN, SYN_REPORT);
266*49cdfc7eSAndroid Build Coastguard Worker }
267*49cdfc7eSAndroid Build Coastguard Worker 
268*49cdfc7eSAndroid Build Coastguard Worker /*
269*49cdfc7eSAndroid Build Coastguard Worker  * the value of stray_sync_event:
270*49cdfc7eSAndroid Build Coastguard Worker  * 0: EV_SYN/SYN_REPORT events should not be received in /dev/input/eventX
271*49cdfc7eSAndroid Build Coastguard Worker  * 1: EV_SYN/SYN_REPORT events may be received in /dev/input/eventX
272*49cdfc7eSAndroid Build Coastguard Worker  * On an old kernel(before v3.7.0), EV_SYN/SYN_REPORT events are always
273*49cdfc7eSAndroid Build Coastguard Worker  * received even though we send empty moves.
274*49cdfc7eSAndroid Build Coastguard Worker  */
no_events_queued(int fd,int stray_sync_event)275*49cdfc7eSAndroid Build Coastguard Worker int no_events_queued(int fd, int stray_sync_event)
276*49cdfc7eSAndroid Build Coastguard Worker {
277*49cdfc7eSAndroid Build Coastguard Worker 	struct pollfd fds = {.fd = fd, .events = POLLIN};
278*49cdfc7eSAndroid Build Coastguard Worker 	int ret, res;
279*49cdfc7eSAndroid Build Coastguard Worker 	struct input_event ev;
280*49cdfc7eSAndroid Build Coastguard Worker 
281*49cdfc7eSAndroid Build Coastguard Worker 	ret = poll(&fds, 1, 30);
282*49cdfc7eSAndroid Build Coastguard Worker 
283*49cdfc7eSAndroid Build Coastguard Worker 	if (ret > 0) {
284*49cdfc7eSAndroid Build Coastguard Worker 		res = read(fd, &ev, sizeof(ev));
285*49cdfc7eSAndroid Build Coastguard Worker 
286*49cdfc7eSAndroid Build Coastguard Worker 		if (res == sizeof(ev)) {
287*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO,
288*49cdfc7eSAndroid Build Coastguard Worker 				"Unexpected ev type=%i code=%i value=%i",
289*49cdfc7eSAndroid Build Coastguard Worker 				ev.type, ev.code, ev.value);
290*49cdfc7eSAndroid Build Coastguard Worker 		}
291*49cdfc7eSAndroid Build Coastguard Worker 	}
292*49cdfc7eSAndroid Build Coastguard Worker 
293*49cdfc7eSAndroid Build Coastguard Worker 	return ret == 0;
294*49cdfc7eSAndroid Build Coastguard Worker }
295*49cdfc7eSAndroid Build Coastguard Worker 
check_device(void)296*49cdfc7eSAndroid Build Coastguard Worker static int check_device(void)
297*49cdfc7eSAndroid Build Coastguard Worker {
298*49cdfc7eSAndroid Build Coastguard Worker 	FILE *file;
299*49cdfc7eSAndroid Build Coastguard Worker 	char line[256];
300*49cdfc7eSAndroid Build Coastguard Worker 
301*49cdfc7eSAndroid Build Coastguard Worker 	file = fopen("/proc/bus/input/devices", "r");
302*49cdfc7eSAndroid Build Coastguard Worker 	if (!file)
303*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
304*49cdfc7eSAndroid Build Coastguard Worker 
305*49cdfc7eSAndroid Build Coastguard Worker 	while (fgets(line, 256, file)) {
306*49cdfc7eSAndroid Build Coastguard Worker 		if (fnmatch(VIRTUAL_DEVICE_REGEX, line, 0) == 0)
307*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
308*49cdfc7eSAndroid Build Coastguard Worker 	}
309*49cdfc7eSAndroid Build Coastguard Worker 
310*49cdfc7eSAndroid Build Coastguard Worker 	fclose(file);
311*49cdfc7eSAndroid Build Coastguard Worker 
312*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
313*49cdfc7eSAndroid Build Coastguard Worker }
314