1 /*
2  * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19  /*
20   *  Create a virtual device (mouse), send events to /dev/uinput
21   *  and check that the events are not received in /dev/input/eventX
22   *  because the device is grabbed by another process
23   */
24 
25 #include <linux/input.h>
26 
27 #include "test.h"
28 #include "safe_macros.h"
29 #include "lapi/fcntl.h"
30 #include "input_helper.h"
31 
32 #define NB_TEST 20
33 
34 static void setup(void);
35 static void send_information(void);
36 static void cleanup(void);
37 
38 static int fd;
39 static int fd2;
40 
41 char *TCID = "input02";
42 
43 int main(int ac, char **av)
44 {
45 	int lc;
46 	int pid;
47 
48 	tst_parse_opts(ac, av, NULL, NULL);
49 
50 	setup();
51 
52 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
53 		pid = tst_fork();
54 
55 		fd2 = open_device();
56 
57 		switch (pid) {
58 		case 0:
59 			send_information();
60 			exit(0);
61 		case -1:
62 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
63 		default:
64 			if (no_events_queued(fd2, 0))
65 				tst_resm(TPASS, "No data received in eventX");
66 			else
67 				tst_resm(TFAIL, "Data received in eventX");
68 			SAFE_CLOSE(NULL, fd2);
69 		break;
70 		}
71 
72 		SAFE_WAITPID(NULL, pid, NULL, 0);
73 	}
74 
75 	cleanup();
76 	tst_exit();
77 }
78 
79 static void setup(void)
80 {
81 	tst_require_root();
82 
83 	fd = open_uinput();
84 	setup_mouse_events(fd);
85 	create_device(fd);
86 }
87 
88 static void send_information(void)
89 {
90 	int nb;
91 
92 	SAFE_IOCTL(NULL, fd2, EVIOCGRAB, 1);
93 	tst_resm(TINFO, "The virtual device was grabbed");
94 
95 	for (nb = 0; nb < NB_TEST; ++nb) {
96 		send_rel_move(fd, 10, 1);
97 		usleep(1000);
98 	}
99 
100 	SAFE_CLOSE(NULL, fd2);
101 }
102 
103 static void cleanup(void)
104 {
105 	destroy_device(fd);
106 }
107