[igt-dev] [PATCH i-g-t] tools/intel_gpu_top: Restore terminal attributes

K. Eugene Carlson kvngncrlsn at gmail.com
Thu Feb 17 10:49:23 UTC 2022


---
 tools/intel_gpu_top.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index 0404a588..1b2a82b9 100644
--- a/tools/intel_gpu_top.c
+++ b/tools/intel_gpu_top.c
@@ -119,6 +119,8 @@ struct engines {
 
 };
 
+static struct termios termios_orig = { };
+
 __attribute__((format(scanf,3,4)))
 static int igt_sysfs_scanf(int dir, const char *attr, const char *fmt, ...)
 {
@@ -1499,6 +1501,15 @@ print_engines(struct engines *engines, double t, int lines, int w, int h)
 	return lines;
 }
 
+static void restore_term(void)
+{
+	int ret;
+
+	ret = tcsetattr(STDIN_FILENO, TCSANOW, &termios_orig);
+	assert(ret == 0);
+	printf("\n");
+}
+
 static bool stop_top;
 
 static void sigint_handler(int  sig)
@@ -1538,14 +1549,17 @@ static void interactive_stdin(void)
 	struct termios termios = { };
 	int ret;
 
+	ret = tcgetattr(0, &termios);
+	assert(ret == 0);
+
+	memcpy(&termios_orig, &termios, sizeof(struct termios));
+	atexit(restore_term);
+
 	ret = fcntl(0, F_GETFL, NULL);
 	ret |= O_NONBLOCK;
 	ret = fcntl(0, F_SETFL, ret);
 	assert(ret == 0);
 
-	ret = tcgetattr(0, &termios);
-	assert(ret == 0);
-
 	termios.c_lflag &= ~ICANON;
 	termios.c_cc[VMIN] = 1;
 	termios.c_cc[VTIME] = 0; /* Deciseconds only - we'll use poll. */
@@ -1689,12 +1703,8 @@ int main(int argc, char **argv)
 		out = stdout;
 	}
 
-	if (output_mode != INTERACTIVE) {
-		sighandler_t sig = signal(SIGINT, sigint_handler);
-
-		if (sig == SIG_ERR)
-			fprintf(stderr, "Failed to install signal handler!\n");
-	}
+	if (signal(SIGINT, sigint_handler) == SIG_ERR)
+		fprintf(stderr, "Failed to install signal handler!\n");
 
 	switch (output_mode) {
 	case INTERACTIVE:
-- 
2.35.1



More information about the igt-dev mailing list