summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorK. Eugene Carlson <kvngncrlsn@gmail.com>2022-02-17 21:06:22 +0900
committerTvrtko Ursulin <tvrtko.ursulin@intel.com>2022-02-21 10:16:54 +0000
commit0513032006f385f34fcd094c810b93f31cbed09d (patch)
tree772a02d70341db2fa79b70532f160f1ff87c25c0 /tools
parent9cb64a757d2ff1e180b1648e611439d94afd697d (diff)
tools/intel_gpu_top: Restore terminal attributes
Save initial terminal attributes when in interactive mode, and reset when the program quits or receives SIGINT. Prevents bad text entry in some password prompts (e.g., su, passwd). [tursulin: Added commit text from earlier posting. Drop static initializer.] Signed-off-by: K. Eugene Carlson <kvngncrlsn@gmail.com> Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/intel_gpu_top.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index 0404a588..bc11fce2 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: