aboutsummaryrefslogtreecommitdiff
path: root/procps/top.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-19 19:38:46 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-19 19:38:46 +0000
commitcf7cf622046b0e1a2817e1da4aa8bc6f513b0153 (patch)
tree30bc88b5a107bfe6d5f751ca722ed5a49a0b5fd8 /procps/top.c
parent0764a7f72d7e5490b85dd0816bef8e56f6216d92 (diff)
downloadbusybox-w32-cf7cf622046b0e1a2817e1da4aa8bc6f513b0153.tar.gz
busybox-w32-cf7cf622046b0e1a2817e1da4aa8bc6f513b0153.tar.bz2
busybox-w32-cf7cf622046b0e1a2817e1da4aa8bc6f513b0153.zip
*: s/BB_SIGS_FATAL/BB_FATAL_SIGS/ (latter proved easier to remember)
top: fix "top </dev/null" case (by Cristian Ionescu-Idbohrn)
Diffstat (limited to 'procps/top.c')
-rw-r--r--procps/top.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/procps/top.c b/procps/top.c
index fdd7584c8..85ceaccf7 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -108,8 +108,13 @@ enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) };
108#define total_pcpu (G.total_pcpu ) 108#define total_pcpu (G.total_pcpu )
109#define line_buf (G.line_buf ) 109#define line_buf (G.line_buf )
110 110
111 111enum {
112#define OPT_BATCH_MODE (option_mask32 & 0x4) 112 OPT_d = (1 << 0),
113 OPT_n = (1 << 1),
114 OPT_b = (1 << 2),
115 OPT_EOF = (1 << 3), /* pseudo: "we saw EOF in stdin" */
116};
117#define OPT_BATCH_MODE (option_mask32 & OPT_b)
113 118
114 119
115#if ENABLE_FEATURE_USE_TERMIOS 120#if ENABLE_FEATURE_USE_TERMIOS
@@ -165,7 +170,7 @@ static void get_jiffy_counts(void)
165 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld", 170 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
166 &jif.usr,&jif.nic,&jif.sys,&jif.idle, 171 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
167 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) { 172 &jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
168 bb_error_msg_and_die("failed to read /proc/stat"); 173 bb_error_msg_and_die("can't read /proc/stat");
169 } 174 }
170 fclose(fp); 175 fclose(fp);
171 jif.total = jif.usr + jif.nic + jif.sys + jif.idle 176 jif.total = jif.usr + jif.nic + jif.sys + jif.idle
@@ -506,7 +511,7 @@ static void clearmems(void)
506 511
507static void reset_term(void) 512static void reset_term(void)
508{ 513{
509 tcsetattr(0, TCSANOW, (void *) &initial_settings); 514 tcsetattr(0, TCSANOW, &initial_settings);
510 if (ENABLE_FEATURE_CLEAN_UP) { 515 if (ENABLE_FEATURE_CLEAN_UP) {
511 clearmems(); 516 clearmems();
512#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE 517#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
@@ -753,13 +758,13 @@ int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
753 758
754 INIT_G(); 759 INIT_G();
755 760
756 interval = 5; /* default update rate is 5 seconds */ 761 interval = 5; /* default update interval is 5 seconds */
757 iterations = 0; /* infinite */ 762 iterations = 0; /* infinite */
758 763
759 /* do normal option parsing */ 764 /* all args are options; -n NUM */
760 opt_complementary = "-:n+"; 765 opt_complementary = "-:n+";
761 getopt32(argv, "d:n:b", &sinterval, &iterations); 766 getopt32(argv, "d:n:b", &sinterval, &iterations);
762 if (option_mask32 & 0x1) { 767 if (option_mask32 & OPT_d) {
763 /* Need to limit it to not overflow poll timeout */ 768 /* Need to limit it to not overflow poll timeout */
764 interval = xatou16(sinterval); // -d 769 interval = xatou16(sinterval); // -d
765 } 770 }
@@ -772,12 +777,8 @@ int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
772 /* unbuffered input, turn off echo */ 777 /* unbuffered input, turn off echo */
773 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); 778 new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
774 779
775 bb_signals(0 780 bb_signals(BB_FATAL_SIGS, sig_catcher);
776 + (1 << SIGTERM)
777 + (1 << SIGINT)
778 , sig_catcher);
779 tcsetattr(0, TCSANOW, (void *) &new_settings); 781 tcsetattr(0, TCSANOW, (void *) &new_settings);
780 atexit(reset_term);
781#endif /* FEATURE_USE_TERMIOS */ 782#endif /* FEATURE_USE_TERMIOS */
782 783
783#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE 784#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
@@ -794,7 +795,8 @@ int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
794 lines = 24; /* default */ 795 lines = 24; /* default */
795 col = 79; 796 col = 79;
796#if ENABLE_FEATURE_USE_TERMIOS 797#if ENABLE_FEATURE_USE_TERMIOS
797 get_terminal_width_height(0, &col, &lines); 798 /* We output to stdout, we need size of stdout (not stdin)! */
799 get_terminal_width_height(STDOUT_FILENO, &col, &lines);
798 if (lines < 5 || col < 10) { 800 if (lines < 5 || col < 10) {
799 sleep(interval); 801 sleep(interval);
800 continue; 802 continue;
@@ -839,9 +841,10 @@ int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
839 topmem[n].stack = p->stack; 841 topmem[n].stack = p->stack;
840#endif 842#endif
841 } 843 }
842 } 844 } /* end of "while we read /proc" */
843 if (ntop == 0) { 845 if (ntop == 0) {
844 bb_error_msg_and_die("no process info in /proc"); 846 bb_error_msg("no process info in /proc");
847 break;
845 } 848 }
846 849
847 if (scan_mask == TOP_MASK) { 850 if (scan_mask == TOP_MASK) {
@@ -875,9 +878,14 @@ int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
875#if !ENABLE_FEATURE_USE_TERMIOS 878#if !ENABLE_FEATURE_USE_TERMIOS
876 sleep(interval); 879 sleep(interval);
877#else 880#else
878 if (safe_poll(pfd, 1, interval * 1000) > 0) { 881 if (option_mask32 & (OPT_b|OPT_EOF))
879 if (read(0, &c, 1) != 1) /* signal */ 882 /* batch mode, or EOF on stdin ("top </dev/null") */
880 break; 883 sleep(interval);
884 else if (safe_poll(pfd, 1, interval * 1000) > 0) {
885 if (safe_read(0, &c, 1) != 1) { /* error/EOF? */
886 option_mask32 |= OPT_EOF;
887 continue;
888 }
881 if (c == initial_settings.c_cc[VINTR]) 889 if (c == initial_settings.c_cc[VINTR])
882 break; 890 break;
883 c |= 0x20; /* lowercase */ 891 c |= 0x20; /* lowercase */
@@ -922,7 +930,9 @@ int top_main(int argc ATTRIBUTE_UNUSED, char **argv)
922#endif 930#endif
923 } 931 }
924#endif /* FEATURE_USE_TERMIOS */ 932#endif /* FEATURE_USE_TERMIOS */
925 } 933 } /* end of "while (1)" */
934
926 bb_putchar('\n'); 935 bb_putchar('\n');
936 reset_term();
927 return EXIT_SUCCESS; 937 return EXIT_SUCCESS;
928} 938}