Switch to runtime check for /proc/self/stat

Removes a compile-time check that may have affected cross-compilation.

Work on #1067.
This commit is contained in:
David Adam 2019-04-29 21:29:21 +08:00
parent e8fd83ca25
commit 665ae3787a
8 changed files with 18 additions and 30 deletions

View file

@ -128,9 +128,6 @@ IF(STRUCT_WINSIZE GREATER -1 AND HAVE_TIOCGWINSZ EQUAL 1)
ENDIF() ENDIF()
CMAKE_POP_CHECK_STATE() CMAKE_POP_CHECK_STATE()
IF(EXISTS "/proc/self/stat")
SET(HAVE__PROC_SELF_STAT 1)
ENDIF()
CHECK_TYPE_SIZE("wchar_t[8]" WCHAR_T_BITS LANGUAGE CXX) CHECK_TYPE_SIZE("wchar_t[8]" WCHAR_T_BITS LANGUAGE CXX)
# Solaris, NetBSD and X/Open-conforming systems have a fixed-args tparm # Solaris, NetBSD and X/Open-conforming systems have a fixed-args tparm

View file

@ -127,9 +127,6 @@
/* Define to 1 if the _nl_msg_cat_cntr symbol is exported. */ /* Define to 1 if the _nl_msg_cat_cntr symbol is exported. */
#cmakedefine HAVE__NL_MSG_CAT_CNTR 1 #cmakedefine HAVE__NL_MSG_CAT_CNTR 1
/* Define to 1 if you have the file `/proc/self/stat'. */
#cmakedefine HAVE__PROC_SELF_STAT 1
/* Define to 1 if the _sys_errs array is available. */ /* Define to 1 if the _sys_errs array is available. */
#cmakedefine HAVE__SYS__ERRS 1 #cmakedefine HAVE__SYS__ERRS 1

View file

@ -3,9 +3,6 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#ifdef HAVE__PROC_SELF_STAT
#include <sys/time.h>
#endif
#include "builtin.h" #include "builtin.h"
#include "common.h" #include "common.h"

View file

@ -3,9 +3,7 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#ifdef HAVE__PROC_SELF_STAT
#include <sys/time.h> #include <sys/time.h>
#endif
#include "builtin.h" #include "builtin.h"
#include "common.h" #include "common.h"
@ -26,7 +24,6 @@ enum {
JOBS_PRINT_NOTHING, // print nothing (exit status only) JOBS_PRINT_NOTHING, // print nothing (exit status only)
}; };
#ifdef HAVE__PROC_SELF_STAT
/// Calculates the cpu usage (in percent) of the specified job. /// Calculates the cpu usage (in percent) of the specified job.
static int cpu_use(const job_t *j) { static int cpu_use(const job_t *j) {
double u = 0; double u = 0;
@ -45,7 +42,6 @@ static int cpu_use(const job_t *j) {
} }
return u * 1000000; return u * 1000000;
} }
#endif
/// Print information about the specified job. /// Print information about the specified job.
static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_t &streams) { static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_t &streams) {
@ -57,17 +53,18 @@ static void builtin_jobs_print(const job_t *j, int mode, int header, io_streams_
if (header) { if (header) {
// Print table header before first job. // Print table header before first job.
streams.out.append(_(L"Job\tGroup\t")); streams.out.append(_(L"Job\tGroup\t"));
#ifdef HAVE__PROC_SELF_STAT if (have_proc_stat) {
streams.out.append(_(L"CPU\t")); streams.out.append(_(L"CPU\t"));
#endif }
streams.out.append(_(L"State\tCommand\n")); streams.out.append(_(L"State\tCommand\n"));
} }
streams.out.append_format(L"%d\t%d\t", j->job_id, j->pgid); streams.out.append_format(L"%d\t%d\t", j->job_id, j->pgid);
#ifdef HAVE__PROC_SELF_STAT if (have_proc_stat) {
streams.out.append_format(L"%d%%\t", cpu_use(j)); streams.out.append_format(L"%d%%\t", cpu_use(j));
#endif }
streams.out.append(j->is_stopped() ? _(L"stopped") : _(L"running")); streams.out.append(j->is_stopped() ? _(L"stopped") : _(L"running"));
streams.out.append(L"\t"); streams.out.append(L"\t");
streams.out.append(j->command_wcstr()); streams.out.append(j->command_wcstr());

View file

@ -560,6 +560,10 @@ void misc_init() {
fflush(stdout); fflush(stdout);
setvbuf(stdout, NULL, _IONBF, 0); 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 /// Ensure the content of the magic path env vars is reasonable. Specifically, that empty path

View file

@ -72,6 +72,7 @@ bool is_login = false;
int is_event = 0; int is_event = 0;
job_control_t job_control_mode = job_control_t::interactive; job_control_t job_control_mode = job_control_t::interactive;
int no_exec = 0; int no_exec = 0;
bool have_proc_stat = false;
static int is_interactive = -1; static int is_interactive = -1;
@ -617,13 +618,12 @@ bool job_reap(bool allow_interactive) {
return found; return found;
} }
#ifdef HAVE__PROC_SELF_STAT
/// Maximum length of a /proc/[PID]/stat filename. /// Maximum length of a /proc/[PID]/stat filename.
#define FN_SIZE 256 #define FN_SIZE 256
/// Get the CPU time for the specified process. /// Get the CPU time for the specified process.
unsigned long proc_get_jiffies(process_t *p) { unsigned long proc_get_jiffies(process_t *p) {
if (! have_proc_stat) return 0;
if (p->pid <= 0) return 0; if (p->pid <= 0) return 0;
wchar_t fn[FN_SIZE]; wchar_t fn[FN_SIZE];
@ -666,8 +666,6 @@ void proc_update_jiffies() {
} }
} }
#endif
// Return control of the terminal to a job's process group. restore_attrs is true if we are restoring // Return control of the terminal to a job's process group. restore_attrs is true if we are restoring
// a previously-stopped job, in which case we need to restore terminal attributes. // a previously-stopped job, in which case we need to restore terminal attributes.
bool terminal_give_to_job(const job_t *j, bool restore_attrs) { bool terminal_give_to_job(const job_t *j, bool restore_attrs) {

View file

@ -228,12 +228,10 @@ class process_t {
bool stopped{false}; bool stopped{false};
/// Reported status value. /// Reported status value.
proc_status_t status{}; proc_status_t status{};
#ifdef HAVE__PROC_SELF_STAT
/// Last time of cpu time check. /// Last time of cpu time check.
struct timeval last_time {}; struct timeval last_time {};
/// Number of jiffies spent in process at last cpu time check. /// Number of jiffies spent in process at last cpu time check.
unsigned long last_jiffies{0}; unsigned long last_jiffies{0};
#endif
}; };
typedef std::unique_ptr<process_t> process_ptr_t; typedef std::unique_ptr<process_t> process_ptr_t;
@ -492,7 +490,6 @@ bool job_reap(bool interactive);
/// Mark a process as failed to execute (and therefore completed). /// Mark a process as failed to execute (and therefore completed).
void job_mark_process_as_failed(const std::shared_ptr<job_t> &job, const process_t *p); void job_mark_process_as_failed(const std::shared_ptr<job_t> &job, const process_t *p);
#ifdef HAVE__PROC_SELF_STAT
/// Use the procfs filesystem to look up how many jiffies of cpu time was used by this process. This /// Use the procfs filesystem to look up how many jiffies of cpu time was used by this process. This
/// function is only available on systems with the procfs file entry 'stat', i.e. Linux. /// function is only available on systems with the procfs file entry 'stat', i.e. Linux.
unsigned long proc_get_jiffies(process_t *p); unsigned long proc_get_jiffies(process_t *p);
@ -500,7 +497,6 @@ unsigned long proc_get_jiffies(process_t *p);
/// Update process time usage for all processes by calling the proc_get_jiffies function for every /// Update process time usage for all processes by calling the proc_get_jiffies function for every
/// process of every job. /// process of every job.
void proc_update_jiffies(); void proc_update_jiffies();
#endif
/// Perform a set of simple sanity checks on the job list. This includes making sure that only one /// Perform a set of simple sanity checks on the job list. This includes making sure that only one
/// job is in the foreground, that every process is in a valid state, etc. /// job is in the foreground, that every process is in a valid state, etc.
@ -555,4 +551,6 @@ void add_disowned_pgid(pid_t pgid);
/// function /// function
enum { INVALID_PID = -2 }; enum { INVALID_PID = -2 };
extern bool have_proc_stat;
#endif #endif

View file

@ -1976,9 +1976,9 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) {
// For compatibility with fish 2.0's $_, now replaced with `status current-command` // For compatibility with fish 2.0's $_, now replaced with `status current-command`
parser.vars().set_one(L"_", ENV_GLOBAL, program_name); parser.vars().set_one(L"_", ENV_GLOBAL, program_name);
#ifdef HAVE__PROC_SELF_STAT if (have_proc_stat) {
proc_update_jiffies(); proc_update_jiffies();
#endif }
} }
parser_test_error_bits_t reader_shell_test(const wcstring &b) { parser_test_error_bits_t reader_shell_test(const wcstring &b) {