From a3d9b6e9b18e0d9466c7a5874a276c137e54d60a Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 16 Dec 2025 11:12:40 +0100 Subject: [PATCH] initctl: fix remaining lingering artifacts in 'top' output Signed-off-by: Joachim Wiberg --- src/cgutil.c | 19 +++++++++++++++++-- src/initctl.c | 2 +- src/util.c | 5 ++++- src/util.h | 4 ++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/cgutil.c b/src/cgutil.c index 02525dce..edbb263c 100644 --- a/src/cgutil.c +++ b/src/cgutil.c @@ -728,13 +728,24 @@ int show_cgps(char *arg) static void cgtop(uev_t *w, void *arg, int events) { + static int first = 1; int lines; (void)w; (void)events; - /* Move cursor to home instead of clearing screen to avoid flicker */ - fputs("\e[H", stdout); + /* Re-probe screen size in case terminal was resized */ + ttinit(1); + + /* Clear screen on first run to remove any artifacts */ + if (first) { + fputs("\e[?7l\e[2J\e[H", stdout); /* Disable line wrap, clear, home */ + first = 0; + } else { + /* Move cursor to home instead of clearing screen to avoid flicker */ + fputs("\e[H", stdout); + } + lines = 0; if (heading) { print_header(" VmSIZE RSS VmLIB %%MEM %%CPU GROUP"); @@ -749,6 +760,7 @@ static void cgtop(uev_t *w, void *arg, int events) static void cleanup(void) { + fputs("\e[?7h", stdout); /* Re-enable line wrap */ ttcooked(); showcursor(); puts(""); @@ -799,6 +811,9 @@ int show_cgtop(char *arg) if (!hcreate(ttrows + 25)) ERR(70, "failed creating hash table"); + /* Ensure we have correct terminal size before starting */ + ttinit(1); + sysinfo(&si); total_ram = si.totalram * si.mem_unit; diff --git a/src/initctl.c b/src/initctl.c index b9bcb8fc..7096d62e 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -1724,7 +1724,7 @@ int main(int argc, char *argv[]) } if (interactive) - ttinit(); + ttinit(0); return cmd_parse(argc - optind, &argv[optind], command); } diff --git a/src/util.c b/src/util.c index c9191212..1188f021 100644 --- a/src/util.c +++ b/src/util.c @@ -687,7 +687,7 @@ int fistmpfs(char *dir) * Called by initctl, and by finit at boot and shutdown, to * (re)initialize the screen size for print() et al. */ -int ttinit(void) +int ttinit(int probe_size) { struct pollfd fd = { STDIN_FILENO, POLLIN, 0 }; struct winsize ws = { 0 }; @@ -707,6 +707,9 @@ int ttinit(void) tcsetattr(STDERR_FILENO, TCSANOW, &tc); } + if (probe_size) + goto fallback; + /* 1. Try TIOCWINSZ to query window size from kernel */ if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)) { ttrows = ws.ws_row; diff --git a/src/util.h b/src/util.h index 260bd9f5..7e8df0b2 100644 --- a/src/util.h +++ b/src/util.h @@ -95,11 +95,11 @@ int fismnt (char *dir); int fistmpfs (char *dir); #ifdef HAVE_TERMIOS_H -int ttinit (void); +int ttinit (int probe_size); int ttraw (void); int ttcooked (void); #else -#define ttinit() ttcols +#define ttinit(v) ttcols #define ttraw() 0 #define ttcooked() 0 #endif