Skip to contentSkip to conversation

Add basic test for uevent netlink socket

PATCH New Cyril Hrubis 2 participants 14 messages Aug 20, 2019 – Aug 26, 2019 Library code

lore
Share & tools
lore
#0 lore raw

cover Add basic test for uevent netlink socket

This patchset adds three basic test for uevent netlink socket.

v2:

Finished the input event test, now we match all events.

Cyril Hrubis (5):
lib/tst_device: Export more functions.
kernel/uevent: Add uevent01
kernel/uevent: Add uevent02
libs/libltpuinput: Add uinput library.
kernel/uevent: Add uevent03

 include/tst_device.h                |   7 ++
 include/tst_uinput.h                |  31 +++++
 lib/tst_device.c                    |   8 +-
 libs/libltpuinput/Makefile          |  12 ++
 libs/libltpuinput/tst_uinput.c      |  93 +++++++++++++++
 runtest/uevent                      |   3 +
 scenario_groups/default             |   1 +
 testcases/kernel/uevents/.gitignore |   3 +
 testcases/kernel/uevents/Makefile   |  10 ++
 testcases/kernel/uevents/uevent.h   | 176 ++++++++++++++++++++++++++++
 testcases/kernel/uevents/uevent01.c |  90 ++++++++++++++
 testcases/kernel/uevents/uevent02.c | 141 ++++++++++++++++++++++
 testcases/kernel/uevents/uevent03.c |  98 ++++++++++++++++
 13 files changed, 669 insertions(+), 4 deletions(-)
 create mode 100644 include/tst_uinput.h
 create mode 100644 libs/libltpuinput/Makefile
 create mode 100644 libs/libltpuinput/tst_uinput.c
 create mode 100644 runtest/uevent
 create mode 100644 testcases/kernel/uevents/.gitignore
 create mode 100644 testcases/kernel/uevents/Makefile
 create mode 100644 testcases/kernel/uevents/uevent.h
 create mode 100644 testcases/kernel/uevents/uevent01.c
 create mode 100644 testcases/kernel/uevents/uevent02.c
 create mode 100644 testcases/kernel/uevents/uevent03.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/5 lib/tst_device: Export more functions.

Export tst_attach_device() and tst_deteach_device() which will be later on used by a kernel netlink uevent tests.

---
 include/tst_device.h | 7 +++++++
 lib/tst_device.c     | 8 ++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
include/tst_device.h +7 -0
index f0ddc3e93..c6e7b45b1 100644
@@ -50,6 +50,13 @@ int tst_clear_device(const char *dev);
  *
  */
 int tst_find_free_loopdev(const char *path, size_t path_len);
+
+
+int tst_attach_device(const char *dev_path, const char *file_path);
+
+
+int tst_detach_device(const char *dev_path);
+
 /*
  * Reads test block device stat file and returns the bytes written since the
  * last call of this function.
lib/tst_device.c +4 -4
index 22abdef46..10f71901d 100644
@@ -139,7 +139,7 @@ int tst_find_free_loopdev(char *path, size_t path_len)
 	return -1;
 }
 
-static int attach_device(const char *dev, const char *file)
+int tst_attach_device(const char *dev, const char *file)
 {
 	int dev_fd, file_fd;
 	struct loop_info loopinfo;
@@ -185,7 +185,7 @@ static int attach_device(const char *dev, const char *file)
 	return 0;
 }
 
-static int detach_device(const char *dev)
+int tst_detach_device(const char *dev)
 {
 	int dev_fd, ret, i;
 
@@ -284,7 +284,7 @@ const char *tst_acquire_device__(unsigned int size)
 	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
 		return NULL;
 
-	if (attach_device(dev_path, DEV_FILE))
+	if (tst_attach_device(dev_path, DEV_FILE))
 		return NULL;
 
 	device_acquired = 1;
@@ -329,7 +329,7 @@ int tst_release_device(const char *dev)
 	 *
 	 * The file image is deleted in tst_rmdir();
 	 */
-	ret = detach_device(dev);
+	ret = tst_detach_device(dev);
 
 	device_acquired = 0;
 
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 1/5 lib/tst_device: Export more functions.

Quoted 76 lines · Cyril Hrubis

Export tst_attach_device() and tst_deteach_device() which will be later
on used by a kernel netlink uevent tests.

---
 include/tst_device.h | 7 +++++++
 lib/tst_device.c     | 8 ++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
include/tst_device.h +7 -0
index f0ddc3e93..c6e7b45b1 100644
@@ -50,6 +50,13 @@ int tst_clear_device(const char *dev);
  *
  */
 int tst_find_free_loopdev(const char *path, size_t path_len);
+
+
+int tst_attach_device(const char *dev_path, const char *file_path);
+
+
+int tst_detach_device(const char *dev_path);
+
 /*
  * Reads test block device stat file and returns the bytes written

since the
* last call of this function.

lib/tst_device.c +1 -1
index 22abdef46..10f71901d 100644
@@ -139,7 +139,7 @@ int tst_find_free_loopdev(char *path, size_t
 path_len)
 	return -1;
 }
 
-static int attach_device(const char *dev, const char *file)
+int tst_attach_device(const char *dev, const char *file)
 {
 	int dev_fd, file_fd;
lib/tst_device.c +2 -2
 	struct loop_info loopinfo;
@@ -185,7 +185,7 @@ static int attach_device(const char *dev, const
 char *file)
 	return 0;
 }
 
-static int detach_device(const char *dev)
+int tst_detach_device(const char *dev)
 {
 	int dev_fd, ret, i;
@@ -284,7 +284,7 @@ const char *tst_acquire_device__(unsigned int
 size)
 	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
 		return NULL;
 
-	if (attach_device(dev_path, DEV_FILE))
+	if (tst_attach_device(dev_path, DEV_FILE))
 		return NULL;
 
lib/tst_device.c +1 -1
 	device_acquired = 1;
@@ -329,7 +329,7 @@ int tst_release_device(const char *dev)
 	 *
 	 * The file image is deleted in tst_rmdir();
 	 */
-	ret = detach_device(dev);
+	ret = tst_detach_device(dev);
 
 	device_acquired = 0;
 
Signature · 1 line
2.21.0
#3 lore raw

PATCH v2 2/5 kernel/uevent: Add uevent01

In reply to Cyril Hrubis

Simple test that attached and detaches a file to a loop device and checks that kernel broadcasts correct events to the kernel uevent broadcast group.

---
 runtest/uevent                      |   1 +
 scenario_groups/default             |   1 +
 testcases/kernel/uevents/.gitignore |   1 +
 testcases/kernel/uevents/Makefile   |   6 +
 testcases/kernel/uevents/uevent.h   | 176 ++++++++++++++++++++++++++++
 testcases/kernel/uevents/uevent01.c |  90 ++++++++++++++
 6 files changed, 275 insertions(+)
 create mode 100644 runtest/uevent
 create mode 100644 testcases/kernel/uevents/.gitignore
 create mode 100644 testcases/kernel/uevents/Makefile
 create mode 100644 testcases/kernel/uevents/uevent.h
 create mode 100644 testcases/kernel/uevents/uevent01.c
runtest/uevent new file +1 -0
new file mode 100644
index 000000000..e9cdf26b8
@@ -0,0 +1 @@
+uevent01 uevent01
scenario_groups/default +1 -0
index 093f5f706..62ae0759d 100644
@@ -29,3 +29,4 @@ input
 cve
 crypto
 kernel_misc
+uevent
testcases/kernel/uevents/.gitignore new file +1 -0
new file mode 100644
index 000000000..53d0b546a
@@ -0,0 +1 @@
+uevent01
testcases/kernel/uevents/Makefile new file +6 -0
new file mode 100644
index 000000000..cba769739
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir			?= ../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/uevents/uevent.h new file +176 -0

176 lines not on this page · raw message

testcases/kernel/uevents/uevent01.c new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
2.21.0
#4 lore raw

PATCH v2 2/5 kernel/uevent: Add uevent01

Hi Cyril,

Quoted 307 lines · Cyril Hrubis

Simple test that attached and detaches a file to a loop device and checks that kernel broadcasts correct events to the kernel uevent broadcast group.

---
 runtest/uevent                      |   1 +
 scenario_groups/default             |   1 +
 testcases/kernel/uevents/.gitignore |   1 +
 testcases/kernel/uevents/Makefile   |   6 +
 testcases/kernel/uevents/uevent.h   | 176
++++++++++++++++++++++++++++
 testcases/kernel/uevents/uevent01.c |  90 ++++++++++++++
 6 files changed, 275 insertions(+)
 create mode 100644 runtest/uevent
 create mode 100644 testcases/kernel/uevents/.gitignore
 create mode 100644 testcases/kernel/uevents/Makefile
 create mode 100644 testcases/kernel/uevents/uevent.h
 create mode 100644 testcases/kernel/uevents/uevent01.c
runtest/uevent new file +1 -0
new file mode 100644
index 000000000..e9cdf26b8
@@ -0,0 +1 @@
+uevent01 uevent01
scenario_groups/default +1 -0
index 093f5f706..62ae0759d 100644
@@ -29,3 +29,4 @@ input
 cve
 crypto
 kernel_misc
+uevent
testcases/kernel/uevents/.gitignore new file +1 -0
new file mode 100644
index 000000000..53d0b546a
@@ -0,0 +1 @@
+uevent01
testcases/kernel/uevents/Makefile new file +6 -0
new file mode 100644
index 000000000..cba769739
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir			?= ../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/uevents/uevent.h new file +12 -0
new file mode 100644
index 000000000..2c32dd534
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2019 Cyril Hrubis <[email protected]>
+ */
+
+#ifndef UEVENT_H__
+#define UEVENT_H__
+
+#include "tst_netlink.h"
+
+/*
+ * There are two broadcast groups defined for the

NETLINK_KOBJECT_UEVENT. The
+ * primary consument of the KERNEL group is udev which handles the hotplug
+ * events and then, once udev does it's magic the events are rebroadcasted to
+ * the UDEV group which is consumed by various daemons in the userspace.
+ */

testcases/kernel/uevents/uevent.h new file +8 -0
+enum monitor_netlink_group {
+	MONITOR_GROUP_NONE,
+	MONITOR_GROUP_KERNEL,
+	MONITOR_GROUP_UDEV,
+};
+
+/*
+ * The messages received from the NETLINK_KOBJECT_UEVENT socket are

stored as a
+ * sequence of a null-terminated strings. First in the buffer is a summary of a
+ * action i.e. "$ACTION@$DEVPATH" which is then followed by a bunch of
+ * key-value pairs.
+ *
+ * For example attaching a file to loopback device generates event:
+ *
+ * "change@/devices/virtual/block/loop0\0

+ *  ACTION=change\0
+ *  DEVPATH=/devices/virtual/block/loop0\0
+ *  SUBSYSTEM=block\0
+ *  MAJOR=7\0
+ *  MINOR=0\0
+ *  DEVNAME=loop0\0
+ *  DEVTYPE=disk\0
+ *  SEQNUM=2677\0"

+ */

testcases/kernel/uevents/uevent.h new file +20 -0
+
+/*
+ * Prints uevent.
+ */
+static inline void print_uevent(const char *event, int len)
+{
+	int consumed = 0;
+
+	tst_res(TINFO, "Got uevent:");
+
+	while (consumed < len) {
+		tst_res(TINFO, "%s", event);
+		int l = strlen(event) + 1;
+		consumed += l;
+		event += l;
+	}
+}
+
+/*
+ * Uevents read from the socket are matched against this

description.
+ *
+ * The msg is the overall action description e.g.
+ * "add@/class/input/input4/mouse1" which has to be matched exactly before we
+ * event attempt to check the key-value pairs stored in the values array. The
+ * event is considered to match if all key-value pairs in the values has been
+ * found in the received event.
+ */

testcases/kernel/uevents/uevent.h new file +46 -0
+struct uevent_desc {
+	const char *msg;
+	int value_cnt;
+	const char **values;
+};
+
+static inline int uevent_match(const char *event, int len,
+                               const struct uevent_desc *uevent)
+{
+	int consumed = 0;
+	int val_matches = 0;
+
+	if (memcmp(event, uevent->msg, strlen(uevent->msg)))
+		return 0;
+
+	int l = strlen(event) + 1;
+
+	consumed += l;
+	event += l;
+
+	while (consumed < len) {
+		int i;
+		for (i = 0; i < uevent->value_cnt; i++) {
+			if (!strcmp(event, uevent->values[i])) {
+				val_matches++;
+				break;
+			}
+		}
+
+		l = strlen(event) + 1;
+		consumed += l;
+		event += l;
+	}
+
+	return val_matches == uevent->value_cnt;
+}
+
+static inline int open_uevent_netlink(void)
+{
+	int fd;
+	struct sockaddr_nl nl_addr = {
+		.nl_family = AF_NETLINK,
+		.nl_groups = MONITOR_GROUP_KERNEL,
+	};
+
+	fd = SAFE_SOCKET(AF_NETLINK, SOCK_RAW,

NETLINK_KOBJECT_UEVENT);

testcases/kernel/uevents/uevent.h new file +8 -0
+
+	SAFE_BIND(fd, (struct sockaddr *)&nl_addr, sizeof(nl_addr));
+
+	return fd;
+}
+
+/*
+ * Reads events from uevent netlink socket until all expected events

passed in
+ * the uevent array are matched.
+ */
+static inline void wait_for_uevents(int fd, const struct uevent_desc *const uevents[])

testcases/kernel/uevents/uevent.h new file +26 -0
+{
+	int i = 0;
+
+	while (1) {
+		int len;
+		char buf[4096];
+
+		len = recv(fd, &buf, sizeof(buf), 0);
+
+		if (len == 0)
+			continue;
+
+		print_uevent(buf, len);
+
+		if (uevent_match(buf, len, uevents[i])) {
+			tst_res(TPASS, "Got expected UEVENT");
+			if (!uevents[++i]) {
+				close(fd);
+				exit(0);
+			}
+		}
+	}
+}
+
+/*
+ * Waits 5 seconds for a child to exit, kills the child after a

timeout.
+ */

testcases/kernel/uevents/uevent.h new file +15 -0
+static inline void wait_for_pid(int pid)
+{
+	int status, ret;
+	int retries = 5000;
+
+	do {
+		ret = waitpid(pid, &status, WNOHANG);
+		usleep(1000);
+	} while (ret == 0 && retries--);
+
+	if (ret == pid) {
+		if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+			return;
+
+		tst_res(TFAIL, "Child exitted with %s",

tst_strstatus(status));

testcases/kernel/uevents/uevent.h new file +10 -0
+	}
+
+	SAFE_KILL(pid, SIGKILL);
+
+	SAFE_WAITPID(pid, NULL, 0);
+
+	tst_res(TFAIL, "Did not get all expected UEVENTS");
+}
+
+#endif /* UEVENT_H__ */
testcases/kernel/uevents/uevent01.c new file +9 -0
new file mode 100644
index 000000000..41cd01b1f
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2019 Cyril Hrubis <[email protected]>
+ */
+
+/*
+ * Very simple uevent netlink socket test.
+ *
+ * We fork a child that listens for a kernel events while parents 

attaches and
+ * detaches a loop device which should produce two change events.
+ */

testcases/kernel/uevents/uevent01.c new file +32 -0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include "tst_test.h"
+
+#include "uevent.h"
+
+static void generate_device_events(const char *dev_path)
+{
+	tst_fill_file("loop.img", 0, 1024, 1024);
+
+	tst_res(TINFO, "Attaching device %s", dev_path);
+	tst_attach_device(dev_path, "loop.img");
+	tst_res(TINFO, "Detaching device %s", dev_path);
+	tst_detach_device(dev_path);
+}
+
+static void verify_uevent(void)
+{
+	int pid, fd, dev_num;
+	char dev_path[1024];
+	char ev_msg[1024];
+	char ev_dev_path[1024];
+	char ev_dev_minor[128];
+	char ev_dev_name[128];
+
+	struct uevent_desc desc = {
+		.msg = ev_msg,
+		.value_cnt = 7,
+		.values = (const char*[]) {
+			"ACTION=change",

testcases/kernel/uevents/uevent01.c line 53

Quoted 10 lines
testcases/kernel/uevents/uevent01.c new file +10 -0
+			ev_dev_path,
+			"SUBSYSTEM=block",
+			"MAJOR=7",
+			ev_dev_minor,
+			ev_dev_name,
+			"DEVTYPE=disk",
+		}
+	};
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));

Maybe it isn't worth to check if dev_num is a valid number.

Quoted 13 lines
testcases/kernel/uevents/uevent01.c new file +8 -0
+
+	snprintf(ev_msg, sizeof(ev_msg),
+	         "change@/devices/virtual/block/loop%i", dev_num);
+
+	snprintf(ev_dev_path, sizeof(ev_dev_path),
+	         "DEVPATH=/devices/virtual/block/loop%i", dev_num);
+
+	snprintf(ev_dev_minor, sizeof(ev_dev_minor), "MINOR=%i",

dev_num);

testcases/kernel/uevents/uevent01.c new file +1 -0
+	snprintf(ev_dev_name, sizeof(ev_dev_name), "DEVNAME=loop%i",

dev_num);

testcases/kernel/uevents/uevent01.c new file +2 -0
+
+	const struct uevent_desc *const uevents[] = {

testcases/kernel/uevents/uevent01.c line 74

Quoted 10 lines
testcases/kernel/uevents/uevent01.c new file +10 -0
+		&desc,
+		&desc,
+		NULL
+	};
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		fd = open_uevent_netlink();
+		TST_CHECKPOINT_WAKE(0);
+		wait_for_uevents(fd, uevents);

For me it wasn't obvious that wait_for_uevents() does the exit(). Not sure if we should do the exit better here or name the function like exit_on_uevents().

testcases/kernel/uevents/uevent01.c line 87

Quoted 13 lines
testcases/kernel/uevents/uevent01.c new file +13 -0
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+
+	generate_device_events(dev_path);
+
+	wait_for_pid(pid);
+}
+
+static struct tst_test test = {
+	.test_all = verify_uevent,
+	.forks_child = 1,
+	.needs_tmpdir = 1,

Just curious, where do we need the tmpdir?

Quoted 7 lines
testcases/kernel/uevents/uevent01.c new file +3 -0
+	.needs_checkpoints = 1,
+	.needs_root = 1,
+};
Signature · 1 line
2.21.0

thx
Clemens

#5 lore raw

PATCH v2 2/5 kernel/uevent: Add uevent01

Hi!

Quoted 4 lines

testcases/kernel/uevents/uevent01.c line 53

Quoted 2 lines
testcases/kernel/uevents/uevent01.c new file +2 -0
+
+	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));

Maybe it isn't worth to check if dev_num is a valid number.

Sure.

Quoted 19 lines
Quoted 19 lines
testcases/kernel/uevents/uevent01.c new file +8 -0
+
+	snprintf(ev_msg, sizeof(ev_msg),
+	         "change@/devices/virtual/block/loop%i", dev_num);
+
+	snprintf(ev_dev_path, sizeof(ev_dev_path),
+	         "DEVPATH=/devices/virtual/block/loop%i", dev_num);
+
+	snprintf(ev_dev_minor, sizeof(ev_dev_minor), "MINOR=%i",

dev_num);

(quoted diff) +1 -0
+	snprintf(ev_dev_name, sizeof(ev_dev_name), "DEVNAME=loop%i",

dev_num);

testcases/kernel/uevents/uevent01.c new file +8 -0
+
+	const struct uevent_desc *const uevents[] = {
+		&desc,
+		&desc,
+		NULL
+	};
+
+	pid = SAFE_FORK();
Quoted 8 lines

testcases/kernel/uevents/uevent01.c line 74

Quoted 4 lines
testcases/kernel/uevents/uevent01.c new file +4 -0
+	if (!pid) {
+		fd = open_uevent_netlink();
+		TST_CHECKPOINT_WAKE(0);
+		wait_for_uevents(fd, uevents);

For me it wasn't obvious that wait_for_uevents() does the exit(). Not sure if we should do the exit better here or name the function like exit_on_uevents().

I was just lazy, I guess that the cleanest solution would be to call the exit here after the wait_for_uevents() call.

Quoted 7 lines
Quoted 7 lines
testcases/kernel/uevents/uevent01.c new file +7 -0
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+
+	generate_device_events(dev_path);
+
+	wait_for_pid(pid);
Quoted 8 lines

testcases/kernel/uevents/uevent01.c line 87

Quoted 6 lines
testcases/kernel/uevents/uevent01.c new file +6 -0
+}
+
+static struct tst_test test = {
+	.test_all = verify_uevent,
+	.forks_child = 1,
+	.needs_tmpdir = 1,

Just curious, where do we need the tmpdir?

We are creating a disk image to be attached to the loop device in the generate_device_events().

Signature · 2 lines
Cyril Hrubis
[email protected]
#6 lore raw

PATCH v2 3/5 kernel/uevent: Add uevent02

In reply to Cyril Hrubis

Similar to uevent01 but we create and remove a tun network card instead.

---
 runtest/uevent                      |   1 +
 testcases/kernel/uevents/.gitignore |   1 +
 testcases/kernel/uevents/uevent02.c | 141 ++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/uevents/uevent02.c
runtest/uevent +1 -0
index e9cdf26b8..30b1114a4 100644
@@ -1 +1,2 @@
 uevent01 uevent01
+uevent02 uevent02
testcases/kernel/uevents/.gitignore +1 -0
index 53d0b546a..0afc95534 100644
@@ -1 +1,2 @@
 uevent01
+uevent02
testcases/kernel/uevents/uevent02.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.21.0
#7 lore raw

PATCH v2 3/5 kernel/uevent: Add uevent02

Hi Cyril,

Quoted 43 lines · Cyril Hrubis

Similar to uevent01 but we create and remove a tun network card instead.

---
 runtest/uevent                      |   1 +
 testcases/kernel/uevents/.gitignore |   1 +
 testcases/kernel/uevents/uevent02.c | 141
++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/uevents/uevent02.c
runtest/uevent +1 -0
index e9cdf26b8..30b1114a4 100644
@@ -1 +1,2 @@
 uevent01 uevent01
+uevent02 uevent02
testcases/kernel/uevents/.gitignore +1 -0
index 53d0b546a..0afc95534 100644
@@ -1 +1,2 @@
 uevent01
+uevent02
testcases/kernel/uevents/uevent02.c new file +9 -0
new file mode 100644
index 000000000..2c28d1810
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2019 Cyril Hrubis <[email protected]>
+ */
+
+/*
+ * Very simple uevent netlink socket test.
+ *
+ * We fork a child that listens for a kernel events while parents
Quoted 1 line

creates and removes

^maybe line wrap?

Quoted 29 lines

+ * a tun network device which should produce two several add and remove events.
+ */

testcases/kernel/uevents/uevent02.c new file +26 -0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
+
+#include "tst_test.h"
+
+#include "uevent.h"
+
+#define TUN_PATH "/dev/net/tun"
+
+static void generate_tun_uevents(void)
+{
+	int fd = SAFE_OPEN(TUN_PATH, O_RDWR);
+
+	struct ifreq ifr = {
+		.ifr_flags = IFF_TUN,
+		.ifr_name = "ltp-tun0",
+	};
+
+	SAFE_IOCTL(fd, TUNSETIFF, (void*)&ifr);
+
+	SAFE_IOCTL(fd, TUNSETPERSIST, 0);
+

testcases/kernel/uevents/uevent02.c line 47

Quoted 10 lines
testcases/kernel/uevents/uevent02.c new file +10 -0
+	SAFE_CLOSE(fd);
+}
+
+static void verify_uevent(void)
+{
+	int pid, fd;
+
+	struct uevent_desc add = {
+		.msg = "add@/devices/virtual/net/ltp-tun0",
+		.value_cnt = 0,

^should it be 4?

testcases/kernel/uevents/uevent02.c line 52

Quoted 5 lines
testcases/kernel/uevents/uevent02.c new file +5 -0
+		.values = (const char*[]) {
+			"ACTION=add",
+			"DEVPATH=/devices/virtual/net/ltp-tun0",
+			"SUBSYSTEM=net",
+			"ITERFACE=ltp-tun0",

^INTERFACE

testcases/kernel/uevents/uevent02.c

Quoted 7 lines
testcases/kernel/uevents/uevent02.c new file +5 -0
+		}
+	};
+
+	struct uevent_desc add_rx = {
+		.msg = "add@/devices/virtual/net/ltp-tun0/queues/rx-

0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+		.value_cnt = 0,
                             ^3

testcases/kernel/uevents/uevent02.c

Quoted 5 lines
testcases/kernel/uevents/uevent02.c new file +3 -0
+		.values = (const char*[]) {
+			"ACTION=add",
+			"DEVPATH=/devices/virtual/net/ltp-

tun0/queues/rx-0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+			"SUBSYSTEM=queueus",

^queues

testcases/kernel/uevents/uevent02.c

Quoted 7 lines
testcases/kernel/uevents/uevent02.c new file +5 -0
+		}
+	};
+
+	struct uevent_desc add_tx = {
+		.msg = "add@/devices/virtual/net/ltp-tun0/queues/tx-

0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+		.value_cnt = 0,
                             ^3

testcases/kernel/uevents/uevent02.c

Quoted 5 lines
testcases/kernel/uevents/uevent02.c new file +3 -0
+		.values = (const char*[]) {
+			"ACTION=add",
+			"DEVPATH=/devices/virtual/net/ltp-

tun0/queues/tx-0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+			"SUBSYSTEM=queueus",

^queues

testcases/kernel/uevents/uevent02.c

Quoted 7 lines
testcases/kernel/uevents/uevent02.c new file +5 -0
+		}
+	};
+
+	struct uevent_desc rem_rx = {
+		.msg = "remove@/devices/virtual/net/ltp-

tun0/queues/rx-0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+		.value_cnt = 0,
                             ^3

testcases/kernel/uevents/uevent02.c

Quoted 5 lines
testcases/kernel/uevents/uevent02.c new file +3 -0
+		.values = (const char*[]) {
+			"ACTION=remove",
+			"DEVPATH=/devices/virtual/net/ltp-

tun0/queues/rx-0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+			"SUBSYSTEM=queueus",

^queues

testcases/kernel/uevents/uevent02.c

Quoted 7 lines
testcases/kernel/uevents/uevent02.c new file +5 -0
+		}
+	};
+
+	struct uevent_desc rem_tx = {
+		.msg = "remove@/devices/virtual/net/ltp-

tun0/queues/tx-0",

testcases/kernel/uevents/uevent02.c new file +1 -0
+		.value_cnt = 0,
                             ^3

testcases/kernel/uevents/uevent02.c line 98

Quoted 11 lines
testcases/kernel/uevents/uevent02.c new file +3 -0
+		.values = (const char*[]) {
+			"ACTION=remove",
+			"DEVPATH=/devices/virtual/net/ltp-

tun0/queues/tx-0",

testcases/kernel/uevents/uevent02.c new file +7 -0
+			"SUBSYSTEM=queueus",
+		}
+	};
+
+	struct uevent_desc rem = {
+		.msg = "remove@/devices/virtual/net/ltp-tun0",
+		.value_cnt = 0,
                             ^4

testcases/kernel/uevents/uevent02.c line 103

Quoted 5 lines
testcases/kernel/uevents/uevent02.c new file +5 -0
+		.values = (const char*[]) {
+			"ACTION=remove",
+			"DEVPATH=/devices/virtual/net/ltp-tun0",
+			"SUBSYSTEM=net",
+			"ITERFACE=ltp-tun0",

^should be INTERFACE

Quoted 42 lines
testcases/kernel/uevents/uevent02.c new file +38 -0
+		}
+	};
+
+	const struct uevent_desc *const uevents[] = {
+		&add,
+		&add_rx,
+		&add_tx,
+		&rem_rx,
+		&rem_tx,
+		&rem,
+		NULL
+	};
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		fd = open_uevent_netlink();
+		TST_CHECKPOINT_WAKE(0);
+		wait_for_uevents(fd, uevents);
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+
+	generate_tun_uevents();
+
+	wait_for_pid(pid);
+}
+
+static struct tst_test test = {
+	.test_all = verify_uevent,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.needs_checkpoints = 1,
+	.needs_root = 1,
+	.needs_drivers = (const char *const []) {
+		"tun",
+		NULL
+	},
+};
Signature · 1 line
2.21.0

thanks
clemens

#8 lore raw

PATCH v2 3/5 kernel/uevent: Add uevent02

Hi!
Thanks for pointing out the obvious mistakes, will fix in v3.

Signature · 2 lines
Cyril Hrubis
[email protected]
#9 lore raw

PATCH v2 4/5 libs/libltpuinput: Add uinput library.

In reply to Cyril Hrubis

I to be used in the uevent03 test.

Also I will convert the uinput testcases to new library and make use of this library as well.

---
 include/tst_uinput.h           |  48 +++++++++++
 libs/libltpuinput/Makefile     |  12 +++
 libs/libltpuinput/tst_uinput.c | 143 +++++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+)
 create mode 100644 include/tst_uinput.h
 create mode 100644 libs/libltpuinput/Makefile
 create mode 100644 libs/libltpuinput/tst_uinput.c
include/tst_uinput.h new file +48 -0
new file mode 100644
index 000000000..dddbd9921
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Cyril Hrubis <[email protected]>
+ */
+
+#ifndef TST_UINPUT_H__
+#define TST_UINPUT_H__
+
+/**
+ * Tries to open the uinput device.
+ *
+ * Returns file descriptor on success, -1 on failure.
+ */
+int open_uinput(void);
+
+/**
+ * Creates virtual input device.
+ *
+ * @fd File descriptor returned by open_uinput().
+ */
+void create_input_device(int fd);
+
+/**
+ * Parses /proc/bus/input/devices and returns the handlers strings for our
+ * virtual device, which is list of input devices that receive events from the
+ * device separated by whitestpaces.
+ *
+ * Returns newly allocated string, list of handlers separated by whitespaces,
+ * or NULL in a case of failure.
+ */
+char *get_input_handlers(void);
+
+/**
+ * Sets up the virtual device to appear as a mouse, this must be called before
+ * the call to create_input_device().
+ *
+ * @fd File descriptor as returned by open_uinput().
+ */
+void setup_mouse_events(int fd);
+
+/**
+ * Destroys virtual input device.
+ *
+ * @fd File descriptor returned by open_uinput().
+ */
+void destroy_input_device(int fd);
+
+#endif	/* TST_UINPUT_H__ */
libs/libltpuinput/Makefile new file +12 -0
new file mode 100644
index 000000000..dd2a6c096
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) Cyril Hrubis <[email protected]>
+
+top_srcdir		?= ../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+LIB			:= libltpuinput.a
+
+include $(top_srcdir)/include/mk/lib.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
libs/libltpuinput/tst_uinput.c new file +143 -0

143 lines not on this page · raw message

Signature · 1 line
2.21.0
#10 lore raw

PATCH v2 4/5 libs/libltpuinput: Add uinput library.

Hi Cyril,

Quoted 187 lines · Cyril Hrubis

I to be used in the uevent03 test.

Also I will convert the uinput testcases to new library and make use of
this library as well.

---
 include/tst_uinput.h           |  48 +++++++++++
 libs/libltpuinput/Makefile     |  12 +++
 libs/libltpuinput/tst_uinput.c | 143
+++++++++++++++++++++++++++++++++
 3 files changed, 203 insertions(+)
 create mode 100644 include/tst_uinput.h
 create mode 100644 libs/libltpuinput/Makefile
 create mode 100644 libs/libltpuinput/tst_uinput.c
include/tst_uinput.h new file +24 -0
new file mode 100644
index 000000000..dddbd9921
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 Cyril Hrubis <[email protected]>
+ */
+
+#ifndef TST_UINPUT_H__
+#define TST_UINPUT_H__
+
+/**
+ * Tries to open the uinput device.
+ *
+ * Returns file descriptor on success, -1 on failure.
+ */
+int open_uinput(void);
+
+/**
+ * Creates virtual input device.
+ *
+ * @fd File descriptor returned by open_uinput().
+ */
+void create_input_device(int fd);
+
+/**
+ * Parses /proc/bus/input/devices and returns the handlers strings

for our
+ * virtual device, which is list of input devices that receive events from the
+ * device separated by whitestpaces.
+ *
+ * Returns newly allocated string, list of handlers separated by whitespaces,
+ * or NULL in a case of failure.
+ */

include/tst_uinput.h new file +4 -0
+char *get_input_handlers(void);
+
+/**
+ * Sets up the virtual device to appear as a mouse, this must be

called before
+ * the call to create_input_device().
+ *
+ * @fd File descriptor as returned by open_uinput().
+ */

include/tst_uinput.h new file +10 -0
+void setup_mouse_events(int fd);
+
+/**
+ * Destroys virtual input device.
+ *
+ * @fd File descriptor returned by open_uinput().
+ */
+void destroy_input_device(int fd);
+
+#endif	/* TST_UINPUT_H__ */
libs/libltpuinput/Makefile new file +12 -0
new file mode 100644
index 000000000..dd2a6c096
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) Cyril Hrubis <[email protected]>
+
+top_srcdir		?= ../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+
+LIB			:= libltpuinput.a
+
+include $(top_srcdir)/include/mk/lib.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
libs/libltpuinput/tst_uinput.c new file +33 -0
new file mode 100644
index 000000000..61d06138e
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2015 Cedric Hnyda <[email protected]>
+ * Copyright (c) 2019 Cyril Hrubis <[email protected]>
+ */
+
+#include <linux/input.h>
+#include <linux/uinput.h>
+#include <stdio.h>
+#include <string.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+
+#include "tst_uinput.h"
+
+#define VIRTUAL_DEVICE "virtual-device-ltp"
+
+static const char *uinput_paths[] = {
+	"/dev/input/uinput",
+	"/dev/uinput",
+};
+
+int open_uinput(void)
+{
+	unsigned int i;
+	int fd;
+
+	for (i = 0; i < ARRAY_SIZE(uinput_paths); i++) {
+		fd = open(uinput_paths[i], O_WRONLY | O_NONBLOCK);
+
+		if (fd > 0) {
+			tst_res(TINFO, "Found uinput dev at %s",

uinput_paths[i]);

libs/libltpuinput/tst_uinput.c new file +5 -0
+			return fd;
+		}
+
+		if (fd < 0 && errno != ENOENT) {
+			tst_brk(TBROK | TERRNO, "open(%s)",

uinput_paths[i]);

libs/libltpuinput/tst_uinput.c new file +13 -0
+		}
+	}
+
+	return -1;
+}
+
+#define HANDLERS_PREFIX "Handlers="
+
+static char *parse_handlers(char *line)
+{
+	char *handlers;
+
+	handlers = strstr(line, HANDLERS_PREFIX) +

sizeof(HANDLERS_PREFIX) - 1;

libs/libltpuinput/tst_uinput.c new file +33 -0
+
+	handlers[strlen(handlers) - 1] = 0;
+
+	return strdup(handlers);
+}
+
+char *get_input_handlers(void)
+{
+	FILE *file;
+	char line[1024];
+	int flag = 0;
+
+	file = fopen("/proc/bus/input/devices", "r");
+	if (!file)
+		return NULL;
+
+	while (fgets(line, sizeof(line), file)) {
+		if (strstr(line, "N: Name=\""VIRTUAL_DEVICE"\""))
+			flag = 1;
+
+		if (flag) {
+			if (line[0] == 'H')
+				return parse_handlers(line);
+
+			if (line[0] == '\n')
+				flag = 0;
+		}
+	}
+
+	fclose(file);
+	return NULL;
+}
+

libs/libltpuinput/tst_uinput.c line 94

Quoted 10 lines
libs/libltpuinput/tst_uinput.c new file +10 -0
+static int check_device(void)
+{
+	FILE *file;
+	char line[256];
+
+	file = fopen("/proc/bus/input/devices", "r");
+	if (!file)
+		return 0;
+
+	while (fgets(line, 256, file)) {

^maybe sizeof(line)

libs/libltpuinput/tst_uinput.c line 95

Quoted 1 line
libs/libltpuinput/tst_uinput.c new file +1 -0
+		if (strstr(line, VIRTUAL_DEVICE))

Could we get name clash and should check for Name="VIRTUAL_DEVICE" as in get_input_handlers() ?

Quoted 52 lines
libs/libltpuinput/tst_uinput.c new file +48 -0
+			return 1;
+	}
+
+	fclose(file);
+
+	return 0;
+}
+
+void setup_mouse_events(int fd)
+{
+	SAFE_IOCTL(fd, UI_SET_EVBIT, EV_KEY);
+	SAFE_IOCTL(fd, UI_SET_KEYBIT, BTN_LEFT);
+	SAFE_IOCTL(fd, UI_SET_EVBIT, EV_REL);
+	SAFE_IOCTL(fd, UI_SET_RELBIT, REL_X);
+	SAFE_IOCTL(fd, UI_SET_RELBIT, REL_Y);
+}
+
+void destroy_input_device(int fd)
+{
+	SAFE_IOCTL(fd, UI_DEV_DESTROY, NULL);
+	SAFE_CLOSE(fd);
+}
+
+void create_input_device(int fd)
+{
+	int nb;
+	struct uinput_user_dev uidev = {
+		.name = VIRTUAL_DEVICE,
+		.id = {
+			.bustype = BUS_USB,
+			.vendor = 0x1,
+			.product = 0x1,
+			.version = 1,
+		}
+	};
+
+	SAFE_WRITE(1, fd, &uidev, sizeof(uidev));
+	SAFE_IOCTL(fd, UI_DEV_CREATE, NULL);
+
+	for (nb = 100; nb > 0; nb--) {
+		if (check_device())
+			return;
+		usleep(10000);
+	}
+
+	destroy_input_device(fd);
+	tst_brk(TBROK, "Failed to create device");
+}
Signature · 1 line
2.21.0
#11 lore raw

PATCH v2 5/5 kernel/uevent: Add uevent03

In reply to Cyril Hrubis

This time we create a virtual input device, a mouse, and validate the result.

---
 runtest/uevent                      |   1 +
 testcases/kernel/uevents/.gitignore |   1 +
 testcases/kernel/uevents/Makefile   |   4 +
 testcases/kernel/uevents/uevent03.c | 240 ++++++++++++++++++++++++++++
 4 files changed, 246 insertions(+)
 create mode 100644 testcases/kernel/uevents/uevent03.c
runtest/uevent +1 -0
index 30b1114a4..0b59c8723 100644
@@ -1,2 +1,3 @@
 uevent01 uevent01
 uevent02 uevent02
+uevent03 uevent03
testcases/kernel/uevents/.gitignore +1 -0
index 0afc95534..7818f7308 100644
@@ -1,2 +1,3 @@
 uevent01
 uevent02
+uevent03
testcases/kernel/uevents/Makefile +4 -0
index cba769739..d5ceb0719 100644
@@ -2,5 +2,9 @@
 
 top_srcdir			?= ../../..
 
+LTPLIBS = ltpuinput
+
+uevent03: LDLIBS += -lltpuinput
+
 include $(top_srcdir)/include/mk/testcases.mk
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/uevents/uevent03.c new file +240 -0

240 lines not on this page · raw message

Signature · 1 line
2.21.0
#12 lore raw

PATCH v2 5/5 kernel/uevent: Add uevent03

Hi Cyril,
works nice, just minor thing and a question.

Quoted 229 lines · Cyril Hrubis

This time we create a virtual input device, a mouse, and validate the result.

---
 runtest/uevent                      |   1 +
 testcases/kernel/uevents/.gitignore |   1 +
 testcases/kernel/uevents/Makefile   |   4 +
 testcases/kernel/uevents/uevent03.c | 240
++++++++++++++++++++++++++++
 4 files changed, 246 insertions(+)
 create mode 100644 testcases/kernel/uevents/uevent03.c
runtest/uevent +1 -0
index 30b1114a4..0b59c8723 100644
@@ -1,2 +1,3 @@
 uevent01 uevent01
 uevent02 uevent02
+uevent03 uevent03
testcases/kernel/uevents/.gitignore +1 -0
index 0afc95534..7818f7308 100644
@@ -1,2 +1,3 @@
 uevent01
 uevent02
+uevent03
testcases/kernel/uevents/Makefile +4 -0
index cba769739..d5ceb0719 100644
@@ -2,5 +2,9 @@
 
 top_srcdir			?= ../../..
 
+LTPLIBS = ltpuinput
+
+uevent03: LDLIBS += -lltpuinput
+
 include $(top_srcdir)/include/mk/testcases.mk
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/uevents/uevent03.c new file +9 -0
new file mode 100644
index 000000000..26790967c
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2019 Cyril Hrubis <[email protected]>
+ */
+
+/*
+ * Very simple uevent netlink socket test.
+ *
+ * We fork a child that listens for a kernel events while parents

creates and
+ * removes a virtual mouse which produces add and remove event for the device
+ * itself and for two event handlers called eventX and mouseY.
+ */

testcases/kernel/uevents/uevent03.c new file +27 -0
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <sys/sysmacros.h>
+
+#include <linux/uinput.h>
+
+#include "tst_test.h"
+#include "tst_uinput.h"
+#include "uevent.h"
+
+static int mouse_fd;
+
+static void create_uinput_mouse(void)
+{
+	mouse_fd = open_uinput();
+	setup_mouse_events(mouse_fd);
+	create_input_device(mouse_fd);
+}
+
+static void destroy_uinput_mouse(void)
+{
+	destroy_input_device(mouse_fd);
+}
+
+static void get_minor_major(char *device, char *minor, char *major,

size_t buf_sizes)

testcases/kernel/uevents/uevent03.c new file +9 -0
+{
+	char path[1024];
+	struct stat stbuf;
+
+	snprintf(path, sizeof(path), "/dev/input/%s", device);
+
+	SAFE_STAT(path, &stbuf);
+
+	snprintf(major, buf_sizes, "MAJOR=%i",

major(stbuf.st_rdev));

testcases/kernel/uevents/uevent03.c new file +1 -0
+	snprintf(minor, buf_sizes, "MINOR=%i",

minor(stbuf.st_rdev));

testcases/kernel/uevents/uevent03.c new file +122 -0
+}
+
+#define MINOR_MAJOR_SIZE 32
+
+static void verify_uevent(void)
+{
+	int pid, fd;
+	char sysname[64];
+	char add_msg[1024];
+	char rem_msg[1024];
+	char dev_path[1024];
+	char add_msg_event1[1024];
+	char rem_msg_event1[1024];
+	char dev_path_event1[1024];
+	char add_msg_event2[1024];
+	char rem_msg_event2[1024];
+	char dev_path_event2[1024];
+	char dev_name1[1024];
+	char dev_name2[1024];
+
+	char minor_event1[MINOR_MAJOR_SIZE];
+	char minor_event2[MINOR_MAJOR_SIZE];
+	char major_event1[MINOR_MAJOR_SIZE];
+	char major_event2[MINOR_MAJOR_SIZE];
+
+	char *handlers, *handler1, *handler2;
+
+	struct uevent_desc add = {
+		.msg = add_msg,
+		.value_cnt = 7,
+		.values = (const char*[]) {
+			"ACTION=add",
+			dev_path,
+			"SUBSYSTEM=input",
+			"NAME=\"virtual-device-ltp\"",
+			"PROP=0",
+			"EV=7",
+			"REL=3",
+		}
+	};
+
+	struct uevent_desc add_event1 = {
+		.msg = add_msg_event1,
+		.value_cnt = 6,
+		.values = (const char*[]) {
+			"ACTION=add",
+			"SUBSYSTEM=input",
+			dev_name1,
+			dev_path_event1,
+			minor_event1,
+			major_event1,
+		}
+	};
+
+	struct uevent_desc add_event2 = {
+		.msg = add_msg_event2,
+		.value_cnt = 6,
+		.values = (const char*[]) {
+			"ACTION=add",
+			"SUBSYSTEM=input",
+			dev_name2,
+			dev_path_event2,
+			minor_event2,
+			major_event2,
+		}
+	};
+
+	struct uevent_desc rem_event1 = {
+		.msg = rem_msg_event1,
+		.value_cnt = 6,
+		.values = (const char*[]) {
+			"ACTION=remove",
+			"SUBSYSTEM=input",
+			dev_name1,
+			dev_path_event1,
+			minor_event1,
+			major_event1,
+		}
+	};
+
+	struct uevent_desc rem_event2 = {
+		.msg = rem_msg_event2,
+		.value_cnt = 6,
+		.values = (const char*[]) {
+			"ACTION=remove",
+			"SUBSYSTEM=input",
+			dev_name2,
+			dev_path_event2,
+			minor_event2,
+			major_event2,
+		}
+	};
+
+	struct uevent_desc rem = {
+		.msg = rem_msg,
+		.value_cnt = 7,
+		.values = (const char*[]) {
+			"ACTION=remove",
+			dev_path,
+			"SUBSYSTEM=input",
+			"NAME=\"virtual-device-ltp\"",
+			"PROP=0",
+			"EV=7",
+			"REL=3",
+		}
+	};
+
+	const struct uevent_desc *const uevents[] = {
+		&add,
+		&add_event1,
+		&add_event2,
+		&rem_event1,
+		&rem_event2,
+		&rem,
+		NULL
+	};
+
+	fd = open_uevent_netlink();
+
+	create_uinput_mouse();
+
+	SAFE_IOCTL(mouse_fd, UI_GET_SYSNAME(sizeof(sysname)),

sysname);

testcases/kernel/uevents/uevent03.c line 174

Quoted 3 lines
testcases/kernel/uevents/uevent03.c new file +3 -0
+	handlers = get_input_handlers();
+
+	handler1 = strtok(handlers, " ");

Not sure if it's possible that we will get none or only one handler?

testcases/kernel/uevents/uevent03.c line 183

Quoted 11 lines
testcases/kernel/uevents/uevent03.c new file +1 -0
+	get_minor_major(handler1, minor_event1, major_event1,

MINOR_MAJOR_SIZE);

testcases/kernel/uevents/uevent03.c new file +3 -0
+
+	handler2 = strtok(NULL, " ");
+	get_minor_major(handler2, minor_event2, major_event2,

MINOR_MAJOR_SIZE);

testcases/kernel/uevents/uevent03.c new file +5 -0
+
+	destroy_uinput_mouse();
+
+	tst_res(TINFO, "Sysname: %s", sysname);
+	tst_res(TINFO, "Handlers: %s", handlers);

Because of strtok(), we do not see all handlers here.

Quoted 67 lines
testcases/kernel/uevents/uevent03.c new file +13 -0
+
+	snprintf(add_msg, sizeof(add_msg),
+	         "add@/devices/virtual/input/%s", sysname);
+
+	snprintf(rem_msg, sizeof(rem_msg),
+	         "remove@/devices/virtual/input/%s", sysname);
+
+	snprintf(dev_path, sizeof(dev_path),
+	         "DEVPATH=/devices/virtual/input/%s", sysname);
+
+
+	snprintf(add_msg_event1, sizeof(add_msg_event1),
+	         "add@/devices/virtual/input/%s/%s", sysname,

handler1);

testcases/kernel/uevents/uevent03.c new file +3 -0
+
+	snprintf(rem_msg_event1, sizeof(rem_msg_event1),
+	         "remove@/devices/virtual/input/%s/%s", sysname,

handler1);

testcases/kernel/uevents/uevent03.c new file +3 -0
+
+	snprintf(dev_path_event1, sizeof(dev_path_event1),
+	         "DEVPATH=/devices/virtual/input/%s/%s", sysname,

handler1);

testcases/kernel/uevents/uevent03.c new file +7 -0
+
+	snprintf(dev_name1, sizeof(dev_name1),
+	         "DEVNAME=input/%s", handler1);
+
+
+	snprintf(add_msg_event2, sizeof(add_msg_event2),
+	         "add@/devices/virtual/input/%s/%s", sysname,

handler2);

testcases/kernel/uevents/uevent03.c new file +3 -0
+
+	snprintf(rem_msg_event2, sizeof(rem_msg_event2),
+	         "remove@/devices/virtual/input/%s/%s", sysname,

handler2);

testcases/kernel/uevents/uevent03.c new file +3 -0
+
+	snprintf(dev_path_event2, sizeof(dev_path_event2),
+	         "DEVPATH=/devices/virtual/input/%s/%s", sysname,

handler2);

testcases/kernel/uevents/uevent03.c new file +25 -0
+
+	snprintf(dev_name2, sizeof(dev_name2),
+	         "DEVNAME=input/%s", handler2);
+
+	free(handlers);
+
+	pid = SAFE_FORK();
+	if (!pid)
+		wait_for_uevents(fd, uevents);
+
+	SAFE_CLOSE(fd);
+	wait_for_pid(pid);
+}
+
+static struct tst_test test = {
+	.test_all = verify_uevent,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+	.needs_checkpoints = 1,
+	.needs_drivers = (const char *const[]) {
+		"uinput",
+		NULL
+	},
+	.needs_root = 1,
+};
Signature · 1 line
2.21.0
#13 lore raw

PATCH v2 5/5 kernel/uevent: Add uevent03

Hi!

Quoted 2 lines

testcases/kernel/uevents/uevent03.c line 174

Quoted 1 line
testcases/kernel/uevents/uevent03.c new file +1 -0
+	handler1 = strtok(handlers, " ");

Not sure if it's possible that we will get none or only one handler?

Well that would be a bug. As far as I can tell we have to get eventX handler and mouseY handler for each device that looks like a mouse.

I guess that checking the content of handler1 and handler2 wouldn't harm though.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +1 -0
+	get_minor_major(handler1, minor_event1, major_event1,

MINOR_MAJOR_SIZE);

(quoted diff) +2 -0
+
+	handler2 = strtok(NULL, " ");
Quoted 8 lines

testcases/kernel/uevents/uevent03.c line 183

Quoted 7 lines
(quoted diff) +1 -0
+	get_minor_major(handler2, minor_event2, major_event2,

MINOR_MAJOR_SIZE);

testcases/kernel/uevents/uevent03.c new file +5 -0
+
+	destroy_uinput_mouse();
+
+	tst_res(TINFO, "Sysname: %s", sysname);
+	tst_res(TINFO, "Handlers: %s", handlers);

Because of strtok(), we do not see all handlers here.

Ah my bad, we have to move that before the strtok() call.

Signature · 2 lines
Cyril Hrubis
[email protected]