test: move tty disabling closer to the actual running of the tests
authorPeter Hutterer <peter.hutterer@who-t.net>
Wed, 16 Oct 2024 01:02:21 +0000 (11:02 +1000)
committerMarge Bot <emma+marge@anholt.net>
Wed, 30 Oct 2024 23:20:42 +0000 (23:20 +0000)
And move the litest-specific condition out into the caller so
disable_tty() only cares about the TTY, not any surrounding conditions.

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1067>

test/litest.c

index fa2c0ce9c0527dc87a6561cae395f820a3e4ecd9..9a36909cd09220c4a76cca5d6a48fc74f4d80cf3 100644 (file)
@@ -1068,10 +1068,53 @@ out:
        return lock_fd;
 }
 
+static int
+disable_tty(void)
+{
+       int tty_mode = -1;
+
+       if (isatty(STDIN_FILENO) && ioctl(STDIN_FILENO, KDGKBMODE, &tty_mode) == 0) {
+#ifdef __linux__
+               ioctl(STDIN_FILENO, KDSKBMODE, K_OFF);
+#elif __FreeBSD__
+               ioctl(STDIN_FILENO, KDSKBMODE, K_RAW);
+
+               /* Put the tty into raw mode */
+               struct termios tios;
+               if (tcgetattr(STDIN_FILENO, &tios))
+                               fprintf(stderr, "Failed to get terminal attribute: %d - %s\n", errno, strerror(errno));
+               cfmakeraw(&tios);
+               if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
+                               fprintf(stderr, "Failed to set terminal attribute: %d - %s\n", errno, strerror(errno));
+#endif
+       }
+
+       return tty_mode;
+}
+
+static void
+restore_tty(int tty_mode)
+{
+       if (tty_mode != -1) {
+               ioctl(STDIN_FILENO, KDSKBMODE, tty_mode);
+#ifdef __FreeBSD__
+               /* Put the tty into "sane" mode */
+               struct termios tios;
+               if (tcgetattr(STDIN_FILENO, &tios))
+                               fprintf(stderr, "Failed to get terminal attribute: %d - %s\n", errno, strerror(errno));
+               cfmakesane(&tios);
+               if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
+                               fprintf(stderr, "Failed to set terminal attribute: %d - %s\n", errno, strerror(errno));
+#endif
+       }
+}
+
+
 static inline enum litest_runner_result
 litest_run(struct list *suites)
 {
        int inhibit_lock_fd;
+       int tty_mode = -1;
 
        list_init(&created_files_list);
 
@@ -1089,6 +1132,13 @@ litest_run(struct list *suites)
                litest_setup_quirks(&created_files_list, mode);
        }
 
+       /* If we're running 'normally' on the VT, disable the keyboard to
+        * avoid messing up our host. But if we're inside gdb or running
+        * without forking, leave it as-is.
+        */
+       if (!run_deviceless && jobs > 1 && !in_debugger)
+               tty_mode = disable_tty();
+
        inhibit_lock_fd = inhibit();
 
        const char *data_path = getenv("LIBINPUT_QUIRKS_DIR");
@@ -1107,6 +1157,8 @@ litest_run(struct list *suites)
 
        close(inhibit_lock_fd);
 
+       restore_tty(tty_mode);
+
        litest_remove_udev_rules(&created_files_list);
 
        return result;
@@ -4742,55 +4794,6 @@ check_device_access(void)
        return 0;
 }
 
-static int
-disable_tty(void)
-{
-       int tty_mode = -1;
-
-       /* If we're running 'normally' on the VT, disable the keyboard to
-        * avoid messing up our host. But if we're inside gdb or running
-        * without forking, leave it as-is.
-        */
-       if (!run_deviceless &&
-           jobs > 1 &&
-           !in_debugger &&
-           isatty(STDIN_FILENO) &&
-           ioctl(STDIN_FILENO, KDGKBMODE, &tty_mode) == 0) {
-#ifdef __linux__
-               ioctl(STDIN_FILENO, KDSKBMODE, K_OFF);
-#elif __FreeBSD__
-               ioctl(STDIN_FILENO, KDSKBMODE, K_RAW);
-
-               /* Put the tty into raw mode */
-               struct termios tios;
-               if (tcgetattr(STDIN_FILENO, &tios))
-                               fprintf(stderr, "Failed to get terminal attribute: %d - %s\n", errno, strerror(errno));
-               cfmakeraw(&tios);
-               if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
-                               fprintf(stderr, "Failed to set terminal attribute: %d - %s\n", errno, strerror(errno));
-#endif
-       }
-
-       return tty_mode;
-}
-
-static void
-restore_tty(int tty_mode)
-{
-       if (tty_mode != -1) {
-               ioctl(STDIN_FILENO, KDSKBMODE, tty_mode);
-#ifdef __FreeBSD__
-               /* Put the tty into "sane" mode */
-               struct termios tios;
-               if (tcgetattr(STDIN_FILENO, &tios))
-                               fprintf(stderr, "Failed to get terminal attribute: %d - %s\n", errno, strerror(errno));
-               cfmakesane(&tios);
-               if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
-                               fprintf(stderr, "Failed to set terminal attribute: %d - %s\n", errno, strerror(errno));
-#endif
-       }
-}
-
 static void
 litest_free_test_list(struct list *tests)
 {
@@ -4817,7 +4820,6 @@ main(int argc, char **argv)
 {
        const struct rlimit corelimit = { 0, 0 };
        enum litest_mode mode;
-       int tty_mode = -1;
        int rc;
        const char *meson_testthreads;
 
@@ -4861,14 +4863,10 @@ main(int argc, char **argv)
        if (setrlimit(RLIMIT_CORE, &corelimit) != 0)
                perror("WARNING: Core dumps not disabled");
 
-       tty_mode = disable_tty();
-
        enum litest_runner_result result = litest_run(&all_test_suites);
 
        litest_free_test_list(&all_test_suites);
 
-       restore_tty(tty_mode);
-
        switch (result) {
                case LITEST_PASS:
                        return EXIT_SUCCESS;