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

K. Eugene Carlson kvngncrlsn at gmail.com
Thu Feb 17 12:06:22 UTC 2022


tools/intel_gpu_top: restore original terminal attributes upon quit or SIGINT.

Signed-off-by: K. Eugene Carlson <kvngncrlsn at gmail.com>
---
 tools/intel_gpu_top.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index 0404a588..36a2787d 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,12 @@ print_engines(struct engines *engines, double t, int lines, int w, int h)
 	return lines;
 }
 
+static void restore_term(void)
+{
+	tcsetattr(STDIN_FILENO, TCSANOW, &termios_orig);
+	printf("\n");
+}
+
 static bool stop_top;
 
 static void sigint_handler(int  sig)
@@ -1538,14 +1546,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 +1700,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