[1/2] lib: Add support for test child safe exit handlers

Submitted by Janusz Krzysztofik on June 7, 2019, 10:17 a.m.

Details

Message ID 20190607101703.31433-2-janusz.krzysztofik@linux.intel.com
State New
Headers show
Series "tests/gem_ringfill: Fix GPU wedged on test break" ( rev: 2 ) in IGT - Trybot

Not browsing as part of any series.

Commit Message

Janusz Krzysztofik June 7, 2019, 10:17 a.m.
Signed-off-by: Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com>
---
 lib/igt_core.c | 40 ++++++++++++++++++++++++++++++++++------
 lib/igt_core.h |  2 ++
 2 files changed, 36 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/lib/igt_core.c b/lib/igt_core.c
index 6b9f0425..011a9daa 100644
--- a/lib/igt_core.c
+++ b/lib/igt_core.c
@@ -255,6 +255,7 @@ 
  */
 
 static unsigned int exit_handler_count;
+static bool exit_handler_disabled;
 const char *igt_interactive_debug;
 
 /* subtests helpers */
@@ -1768,7 +1769,7 @@  bool __igt_fork(void)
 		igt_assert(0);
 	case 0:
 		test_child = true;
-		exit_handler_count = 0;
+		exit_handler_disabled = true;
 		reset_helper_process_list();
 		oom_adjust_for_doom();
 		igt_unshare_spins();
@@ -1898,8 +1899,11 @@  static struct {
 	bool installed;
 } orig_sig[MAX_SIGNALS];
 
-static igt_exit_handler_t exit_handler_fn[MAX_EXIT_HANDLERS];
-static bool exit_handler_disabled;
+static struct {
+	igt_exit_handler_t fn;
+	bool child_safe;
+} exit_handler[MAX_EXIT_HANDLERS];
+
 static const struct {
 	int number;
 	const char *name;
@@ -1961,7 +1965,9 @@  static void call_exit_handlers(int sig)
 	}
 
 	for (i = exit_handler_count - 1; i >= 0; i--)
-		exit_handler_fn[i](sig);
+		if (!test_child || exit_handler[i].child_safe) {
+			exit_handler[i].fn(sig);
+		}
 
 	/* ensure we don't get called twice */
 	exit_handler_count = 0;
@@ -2065,12 +2071,13 @@  void igt_install_exit_handler(igt_exit_handler_t fn)
 	int i;
 
 	for (i = 0; i < exit_handler_count; i++)
-		if (exit_handler_fn[i] == fn)
+		if (exit_handler[i].fn == fn)
 			return;
 
 	igt_assert(exit_handler_count < MAX_EXIT_HANDLERS);
 
-	exit_handler_fn[exit_handler_count] = fn;
+	exit_handler[exit_handler_count].fn = fn;
+	exit_handler[exit_handler_count].child_safe = test_child;
 	exit_handler_count++;
 
 	if (exit_handler_count > 1)
@@ -2093,6 +2100,27 @@  err:
 	igt_assert_f(0, "failed to install the signal handler\n");
 }
 
+/**
+ * igt_set_exit_handler_child_safe:
+ * @fn: exit handler function
+ *
+ * Mark an already installed exit handler function as safe to be called
+ * from a test child.
+ */
+void igt_set_exit_handler_child_safe(igt_exit_handler_t fn)
+{
+	int i;
+
+	for (i = 0; i < exit_handler_count; i++)
+		if (exit_handler[i].fn == fn)
+			break;
+
+	igt_assert(i < exit_handler_count);
+
+	igt_debug("marking exit handler #%d child safe\n", i);
+	exit_handler[i].child_safe = true;
+}
+
 /* simulation enviroment support */
 
 /**
diff --git a/lib/igt_core.h b/lib/igt_core.h
index 88a95ec2..0fc09f06 100644
--- a/lib/igt_core.h
+++ b/lib/igt_core.h
@@ -782,6 +782,7 @@  static inline void igt_ignore_warn(bool value)
 } while (0)
 
 /* fork support code */
+extern bool test_child;
 bool __igt_fork(void);
 
 /**
@@ -861,6 +862,7 @@  typedef void (*igt_exit_handler_t)(int sig);
 
 /* reliable atexit helpers, also work when killed by a signal (if possible) */
 void igt_install_exit_handler(igt_exit_handler_t fn);
+void igt_set_exit_handler_child_safe(igt_exit_handler_t fn);
 
 /* helpers to automatically reduce test runtime in simulation */
 bool igt_run_in_simulation(void);