From be41407610c622f4a00a39123126e656c69c72ec Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 12 May 2019 14:59:30 -0700 Subject: [PATCH] Make have_proc_stat an ordinary function Removes a mutable global variable. --- src/builtin_jobs.cpp | 4 ++-- src/env.cpp | 4 ---- src/proc.cpp | 9 +++++++-- src/proc.h | 2 +- src/reader.cpp | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/builtin_jobs.cpp b/src/builtin_jobs.cpp index 905a7db8d..e3be3f63d 100644 --- a/src/builtin_jobs.cpp +++ b/src/builtin_jobs.cpp @@ -54,7 +54,7 @@ static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_ if (header) { // Print table header before first job. streams.out.append(_(L"Job\tGroup\t")); - if (have_proc_stat) { + if (have_proc_stat()) { streams.out.append(_(L"CPU\t")); } streams.out.append(_(L"State\tCommand\n")); @@ -62,7 +62,7 @@ static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_ streams.out.append_format(L"%d\t%d\t", j->job_id, j->pgid); - if (have_proc_stat) { + if (have_proc_stat()) { streams.out.append_format(L"%d%%\t", cpu_use(j)); } diff --git a/src/env.cpp b/src/env.cpp index bee66a820..9deefb77b 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -219,10 +219,6 @@ void misc_init() { fflush(stdout); setvbuf(stdout, NULL, _IONBF, 0); } - // Check for /proc/self/stat to see if we are running with Linux-style procfs - if (access("/proc/self/stat", R_OK) == 0) { - have_proc_stat = true; - } } /// Ensure the content of the magic path env vars is reasonable. Specifically, that empty path diff --git a/src/proc.cpp b/src/proc.cpp index c38f06898..8dc66a0e1 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -59,7 +59,12 @@ bool is_breakpoint = false; bool is_login = false; int is_event = 0; int no_exec = 0; -bool have_proc_stat = false; + +bool have_proc_stat() { + // Check for /proc/self/stat to see if we are running with Linux-style procfs. + static const bool s_result = (access("/proc/self/stat", R_OK) == 0); + return s_result; +} static relaxed_atomic_t job_control_mode{job_control_t::interactive}; @@ -623,7 +628,7 @@ bool job_reap(parser_t &parser, bool allow_interactive) { /// Get the CPU time for the specified process. unsigned long proc_get_jiffies(process_t *p) { - if (!have_proc_stat) return 0; + if (!have_proc_stat()) return 0; if (p->pid <= 0) return 0; wchar_t fn[FN_SIZE]; diff --git a/src/proc.h b/src/proc.h index 7126e6ee4..8097f96e9 100644 --- a/src/proc.h +++ b/src/proc.h @@ -517,6 +517,6 @@ void add_disowned_pgid(pid_t pgid); /// function enum { INVALID_PID = -2 }; -extern bool have_proc_stat; +bool have_proc_stat(); #endif diff --git a/src/reader.cpp b/src/reader.cpp index 222b17c32..e6998bc25 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1963,7 +1963,7 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) { // For compatibility with fish 2.0's $_, now replaced with `status current-command` parser.vars().set_one(L"_", ENV_GLOBAL, program_name); - if (have_proc_stat) { + if (have_proc_stat()) { proc_update_jiffies(parser); } }