[procps] [PATCH] 'slabtop -o' with stdin not a tty would complain

  • From: Derek Fawcus <dfawcus@xxxxxxxxxxx>
  • To: "procps@xxxxxxxxxxxxx" <procps@xxxxxxxxxxxxx>
  • Date: Fri, 16 Jan 2015 09:53:38 +0000

Hello,h 
  When using 'slabtop -o' plumbed in to a pipeline,  an unfortunate error
message is produced.  The attached is a proposed patch which I find
resolves the issue.

 (I've not yet got my MUA properly hooked in to git,  and so have attached
this rather than mess up the whitespace).

Thanks

DF


From 0092468b0000e79f39f4033c566bf0a424a8977c Mon Sep 17 00:00:00 2001
From: Derek Fawcus <dfawcus@xxxxxxxxxxx>
Date: Thu, 15 Jan 2015 13:14:53 +0000
Subject: [PATCH] 'slabtop -o' with stdin not a tty would complain

When the command is executed in one shot mode (-o) with stdin
being something other than a terminal,  the tcgetattr() call
would fail,  and generate an error message. e.g.:

slabtop: terminal setting retrieval: Inappropriate ioctl for device
 Active / Total Objects (% used)    : 905319 / 915886 (98.8%)
---
 slabtop.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/slabtop.c b/slabtop.c
index 4be35b9..ec7515d 100644
--- a/slabtop.c
+++ b/slabtop.c
@@ -286,7 +286,7 @@ static void parse_input(char c)
 #define print_line(fmt, ...) if (run_once) printf(fmt, __VA_ARGS__); else 
printw(fmt, __VA_ARGS__)
 int main(int argc, char *argv[])
 {
-       int o;
+       int is_tty, o;
        unsigned short old_rows;
        struct slab_info *slab_list = NULL;
        int run_once = 0, retval = EXIT_SUCCESS;
@@ -337,7 +337,8 @@ int main(int argc, char *argv[])
                }
        }
 
-       if (tcgetattr(STDIN_FILENO, &saved_tty) == -1)
+       is_tty = isatty(STDIN_FILENO);
+       if (is_tty && tcgetattr(STDIN_FILENO, &saved_tty) == -1)
                xwarn(_("terminal setting retrieval"));
 
        old_rows = rows;
@@ -429,7 +430,8 @@ int main(int argc, char *argv[])
                }
        } while (delay);
 
-       tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tty);
+       if (is_tty)
+               tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tty);
        free_slabinfo(slab_list);
        if (!run_once)
                endwin();
-- 
1.9.1

Other related posts: