aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h4
-rw-r--r--miscutils/less.c2
-rw-r--r--miscutils/watchdog.c2
-rw-r--r--networking/tcpudp.c2
-rw-r--r--procps/top.c48
5 files changed, 34 insertions, 24 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 654643743..d059ac9de 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -278,7 +278,7 @@ char *xmalloc_follow_symlinks(const char *path);
278 278
279 279
280enum { 280enum {
281 /* bb_signals(BB_SIGS_FATAL, handler) catches all signals which 281 /* bb_signals(BB_FATAL_SIGS, handler) catches all signals which
282 * otherwise would kill us, except for those resulting from bugs: 282 * otherwise would kill us, except for those resulting from bugs:
283 * SIGSEGV, SIGILL, SIGFPE. 283 * SIGSEGV, SIGILL, SIGFPE.
284 * Other fatal signals not included (TODO?): 284 * Other fatal signals not included (TODO?):
@@ -288,7 +288,7 @@ enum {
288 * SIGSYS Bad argument to routine 288 * SIGSYS Bad argument to routine
289 * SIGTRAP Trace/breakpoint trap 289 * SIGTRAP Trace/breakpoint trap
290 */ 290 */
291 BB_SIGS_FATAL = 0 291 BB_FATAL_SIGS = 0
292 + (1 << SIGHUP) 292 + (1 << SIGHUP)
293 + (1 << SIGINT) 293 + (1 << SIGINT)
294 + (1 << SIGTERM) 294 + (1 << SIGTERM)
diff --git a/miscutils/less.c b/miscutils/less.c
index 3a9dea039..1a67ca7ce 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -1400,7 +1400,7 @@ int less_main(int argc, char **argv)
1400 term_less.c_cc[VTIME] = 0; 1400 term_less.c_cc[VTIME] = 0;
1401 1401
1402 /* We want to restore term_orig on exit */ 1402 /* We want to restore term_orig on exit */
1403 bb_signals(BB_SIGS_FATAL, sig_catcher); 1403 bb_signals(BB_FATAL_SIGS, sig_catcher);
1404 1404
1405 reinitialize(); 1405 reinitialize();
1406 while (1) { 1406 while (1) {
diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c
index 7fb16b861..a5061f5d7 100644
--- a/miscutils/watchdog.c
+++ b/miscutils/watchdog.c
@@ -46,7 +46,7 @@ int watchdog_main(int argc, char **argv)
46 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); 46 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
47 } 47 }
48 48
49 bb_signals(BB_SIGS_FATAL, watchdog_shutdown); 49 bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
50 50
51 /* Use known fd # - avoid needing global 'int fd' */ 51 /* Use known fd # - avoid needing global 'int fd' */
52 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3); 52 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
diff --git a/networking/tcpudp.c b/networking/tcpudp.c
index f97787497..5da4de505 100644
--- a/networking/tcpudp.c
+++ b/networking/tcpudp.c
@@ -264,7 +264,7 @@ int tcpudpsvd_main(int argc ATTRIBUTE_UNUSED, char **argv)
264 264
265 sig_block(SIGCHLD); 265 sig_block(SIGCHLD);
266 signal(SIGCHLD, sig_child_handler); 266 signal(SIGCHLD, sig_child_handler);
267 bb_signals(BB_SIGS_FATAL, sig_term_handler); 267 bb_signals(BB_FATAL_SIGS, sig_term_handler);
268 signal(SIGPIPE, SIG_IGN); 268 signal(SIGPIPE, SIG_IGN);
269 269
270 if (max_per_host) 270 if (max_per_host)
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}