[PATCH libinput 2/4] test: always install our own udev rule/hwdb files for tests

Peter Hutterer peter.hutterer at who-t.net
Thu Jun 4 23:41:30 PDT 2015


We can't rely on the system having these files installed, at least not in the
latest version that we'd like.
Copy them over from the source directory into the /run/udev/ directories for
each test and update udev and the hwdb. This ensures the tags we set in the
hwdb file are always set, regardless of the system configuration.

Note that the /run/udev/* files need to have a different filename to the ones
we ship to avoid getting overridden by local configuration.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 test/Makefile.am |  6 +++--
 test/litest.c    | 79 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/test/Makefile.am b/test/Makefile.am
index fc05ff6..4c2d3d5 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -38,7 +38,9 @@ liblitest_la_SOURCES = \
 	litest-vmware-virtual-usb-mouse.c \
 	litest.c
 liblitest_la_LIBADD = $(top_builddir)/src/libinput-util.la
-liblitest_la_CFLAGS = $(AM_CFLAGS)
+liblitest_la_CFLAGS = $(AM_CFLAGS) \
+	      -DLIBINPUT_UDEV_RULES_FILE="\"$(abs_top_srcdir)/udev/90-libinput-model-quirks.rules\"" \
+	      -DLIBINPUT_UDEV_HWDB_FILE="\"$(abs_top_srcdir)/udev/90-libinput-model-quirks.hwdb\""
 if HAVE_LIBUNWIND
 liblitest_la_LIBADD += $(LIBUNWIND_LIBS) -ldl
 liblitest_la_CFLAGS += $(LIBUNWIND_CFLAGS)
@@ -110,7 +112,7 @@ test_device_LDADD = $(TEST_LIBS)
 test_device_LDFLAGS = -no-install
 
 test_litest_selftest_SOURCES = litest-selftest.c litest.c litest-int.h litest.h
-test_litest_selftest_CFLAGS = -DLITEST_DISABLE_BACKTRACE_LOGGING -DLITEST_NO_MAIN
+test_litest_selftest_CFLAGS = -DLITEST_DISABLE_BACKTRACE_LOGGING -DLITEST_NO_MAIN $(liblitest_la_CFLAGS)
 test_litest_selftest_LDADD = $(TEST_LIBS)
 test_litest_selftest_LDFLAGS = -no-install
 
diff --git a/test/litest.c b/test/litest.c
index 0e2cb96..a786362 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -40,6 +40,7 @@
 #include <unistd.h>
 #include "linux/input.h"
 #include <sys/ptrace.h>
+#include <sys/sendfile.h>
 #include <sys/timerfd.h>
 #include <sys/wait.h>
 
@@ -49,6 +50,9 @@
 
 #define UDEV_RULES_D "/run/udev/rules.d"
 #define UDEV_RULE_PREFIX "99-litest-"
+#define UDEV_HWDB_D "/run/udev/hwdb.d"
+#define UDEV_COMMON_RULE_FILE UDEV_RULES_D "/91-litest-model-quirks.rules"
+#define UDEV_COMMON_HWDB_FILE UDEV_RULES_D "/91-litest-model-quirks.hwdb"
 
 static int in_debugger = -1;
 static int verbose = 0;
@@ -56,6 +60,8 @@ const char *filter_test = NULL;
 const char *filter_device = NULL;
 const char *filter_group = NULL;
 
+static inline void litest_remove_model_quirks(void);
+
 /* defined for the litest selftest */
 #ifndef LITEST_DISABLE_BACKTRACE_LOGGING
 #define litest_log(...) fprintf(stderr, __VA_ARGS__)
@@ -374,22 +380,34 @@ struct litest_test_device* devices[] = {
 
 static struct list all_tests;
 
-static void
-litest_reload_udev_rules(void)
+static inline void
+litest_system(const char *command)
 {
-	int ret = system("udevadm control --reload-rules");
+	int ret;
+
+	ret = system(command);
+
 	if (ret == -1) {
-		litest_abort_msg("Failed to execute: udevadm");
+		litest_abort_msg("Failed to execute: %s", command);
 	} else if (WIFEXITED(ret)) {
 		if (WEXITSTATUS(ret))
-			litest_abort_msg("udevadm failed with %d",
+			litest_abort_msg("'%s' failed with %d",
+					 command,
 					 WEXITSTATUS(ret));
 	} else if (WIFSIGNALED(ret)) {
-		litest_abort_msg("udevadm terminated with signal %d",
+		litest_abort_msg("'%s' terminated with signal %d",
+				 command,
 				 WTERMSIG(ret));
 	}
 }
 
+static void
+litest_reload_udev_rules(void)
+{
+	litest_system("udevadm control --reload-rules");
+	litest_system("udevadm hwdb --update");
+}
+
 static int
 litest_udev_rule_filter(const struct dirent *entry)
 {
@@ -430,6 +448,7 @@ litest_drop_udev_rules(void)
 	}
 	free(entries);
 
+	litest_remove_model_quirks();
 	litest_reload_udev_rules();
 }
 
@@ -873,21 +892,58 @@ merge_events(const int *orig, const int *override)
 	return events;
 }
 
+static inline void
+litest_copy_file(const char *dest, const char *src)
+{
+	int in, out;
+
+	out = open(dest, O_CREAT|O_WRONLY, 0644);
+	litest_assert_int_gt(out, -1);
+	in = open(src, O_RDONLY);
+	litest_assert_int_gt(in, -1);
+	/* lazy, just check for error and empty file copy */
+	litest_assert_int_gt(sendfile(out, in, NULL, 4096), 0);
+	close(out);
+	close(in);
+}
+
+static inline void
+litest_install_model_quirks(void)
+{
+	litest_copy_file(UDEV_COMMON_RULE_FILE, LIBINPUT_UDEV_RULES_FILE);
+	litest_copy_file(UDEV_COMMON_HWDB_FILE, LIBINPUT_UDEV_HWDB_FILE);
+}
+
+static inline void
+litest_remove_model_quirks(void)
+{
+	unlink(UDEV_COMMON_RULE_FILE);
+	unlink(UDEV_COMMON_HWDB_FILE);
+}
+
 static char *
 litest_init_udev_rules(struct litest_test_device *dev)
 {
 	int rc;
 	FILE *f;
-	char *path;
-
-	if (!dev->udev_rule)
-		return NULL;
+	char *path = NULL;
 
 	rc = mkdir(UDEV_RULES_D, 0755);
 	if (rc == -1 && errno != EEXIST)
 		ck_abort_msg("Failed to create udev rules directory (%s)\n",
 			     strerror(errno));
 
+	rc = mkdir(UDEV_HWDB_D, 0755);
+	if (rc == -1 && errno != EEXIST)
+		ck_abort_msg("Failed to create udev hwdb directory (%s)\n",
+			     strerror(errno));
+
+	litest_install_model_quirks();
+
+	/* device-specific udev rules */
+	if (!dev->udev_rule)
+		goto out;
+
 	rc = xasprintf(&path,
 		      "%s/%s%s.rules",
 		      UDEV_RULES_D,
@@ -903,6 +959,7 @@ litest_init_udev_rules(struct litest_test_device *dev)
 	litest_assert_int_ge(fputs(dev->udev_rule, f), 0);
 	fclose(f);
 
+out:
 	litest_reload_udev_rules();
 
 	return path;
@@ -942,6 +999,7 @@ litest_create(enum litest_device_type which,
 	if ((*dev)->create) {
 		(*dev)->create(d);
 		if (abs_override || events_override) {
+			litest_remove_model_quirks();
 			if (udev_file)
 				unlink(udev_file);
 			litest_abort_msg("Custom create cannot be overridden");
@@ -1120,6 +1178,7 @@ litest_delete_device(struct litest_device *d)
 		return;
 
 	if (d->udev_rule_file) {
+		litest_remove_model_quirks();
 		unlink(d->udev_rule_file);
 		free(d->udev_rule_file);
 		d->udev_rule_file = NULL;
-- 
2.4.1



More information about the wayland-devel mailing list