From aac26b549eba148c296a28f9945f91bab43c99f8 Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Sat, 30 Sep 2023 14:24:23 -0400 Subject: [PATCH] Fix long/short cpu stat init Fixes #1630. --- src/core.cc | 12 ++++-------- src/linux.cc | 9 +++++++-- src/linux.h | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/core.cc b/src/core.cc index 99bbffb25e..b5661afc55 100644 --- a/src/core.cc +++ b/src/core.cc @@ -747,15 +747,14 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, END OBJ(diskiograph_write, &update_diskio) parse_diskiograph_arg(obj, arg); obj->callbacks.graphval = &diskiographval_write; #endif /* BUILD_GUI */ - END OBJ(color, nullptr) - if (false + END OBJ(color, nullptr) if (false #ifdef BUILD_GUI - || out_to_gui(*state) + || out_to_gui(*state) #endif /* BUILD_GUI */ #ifdef BUILD_NCURSES - || out_to_ncurses.get(*state) + || out_to_ncurses.get(*state) #endif /* BUILD_NCURSES */ -) { + ) { Colour c = arg != nullptr ? parse_color(arg) : default_color.get(*state); obj->data.l = c.to_argb32(); set_current_text_color(c); @@ -1058,9 +1057,6 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, * nothing else than just that, using an ugly switch(). */ if (strncmp(s, "top", 3) == EQUAL) { if (parse_top_args(s, arg, obj) != 0) { -#ifdef __linux__ - determine_longstat_file(); -#endif obj->cb_handle = create_cb_handle(update_top); } else { free(obj); diff --git a/src/linux.cc b/src/linux.cc index 4e6609d05c..ed921ef56f 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -903,6 +903,10 @@ void determine_longstat_file(void) { FILE *stat_fp; static int reported = 0; char buf[MAX_PROCSTAT_LINELEN + 1]; + static int stat_initialized = 0; + + /* only execute once */ + if (stat_initialized) return; if (!(stat_fp = open_file("/proc/stat", &reported))) return; while (!feof(stat_fp) && @@ -913,6 +917,7 @@ void determine_longstat_file(void) { } } fclose(stat_fp); + stat_initialized = 1; } void get_cpu_count(void) { @@ -1000,6 +1005,7 @@ int update_stat(void) { } if (!stat_template) { + determine_longstat_file(); stat_template = KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT; } @@ -1216,8 +1222,7 @@ static void get_dev_path(const char *dir, const char *dev, char *out_buf) { if (name_fd < 0) { snprintf(path, 512, "%s%s/device/name", dir, namelist[i]->d_name); name_fd = open(path, O_RDONLY); - if (name_fd < 0) - continue; + if (name_fd < 0) continue; } size = read(name_fd, name, strlen(dev)); if (size < strlen(dev)) { diff --git a/src/linux.h b/src/linux.h index 0d1fc72f80..1e71657e11 100644 --- a/src/linux.h +++ b/src/linux.h @@ -58,8 +58,6 @@ int update_stat(void); void print_distribution(struct text_object *, char *, unsigned int); -void determine_longstat_file(void); - extern char e_iface[64]; extern char interfaces_arr[MAX_NET_INTERFACES][64];