linux: make uv_cpu_info() handle absent procfs
Return an error when reading from /proc files fails because the procfs
isn't mounted.
This is a back-port of commit 7e59f9b from the master branch.
This commit is contained in:
parent
23b93474b1
commit
b0c5e62035
@ -64,9 +64,9 @@ static struct {
|
|||||||
size_t len;
|
size_t len;
|
||||||
} process_title;
|
} process_title;
|
||||||
|
|
||||||
static void read_models(unsigned int numcpus, uv_cpu_info_t* ci);
|
static int read_models(unsigned int numcpus, uv_cpu_info_t* ci);
|
||||||
|
static int read_times(unsigned int numcpus, uv_cpu_info_t* ci);
|
||||||
static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci);
|
static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci);
|
||||||
static void read_times(unsigned int numcpus, uv_cpu_info_t* ci);
|
|
||||||
static unsigned long read_cpufreq(unsigned int cpunum);
|
static unsigned long read_cpufreq(unsigned int cpunum);
|
||||||
|
|
||||||
|
|
||||||
@ -325,10 +325,19 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
|
|||||||
if (ci == NULL)
|
if (ci == NULL)
|
||||||
return uv__new_sys_error(ENOMEM);
|
return uv__new_sys_error(ENOMEM);
|
||||||
|
|
||||||
read_models(numcpus, ci);
|
if (read_models(numcpus, ci)) {
|
||||||
read_times(numcpus, ci);
|
SAVE_ERRNO(free(ci));
|
||||||
|
return uv__new_sys_error(errno);
|
||||||
|
}
|
||||||
|
|
||||||
/* read_models() on x86 also reads the CPU speed from /proc/cpuinfo */
|
if (read_times(numcpus, ci)) {
|
||||||
|
SAVE_ERRNO(free(ci));
|
||||||
|
return uv__new_sys_error(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* read_models() on x86 also reads the CPU speed from /proc/cpuinfo.
|
||||||
|
* We don't check for errors here. Worst case, the field is left zero.
|
||||||
|
*/
|
||||||
if (ci[0].speed == 0)
|
if (ci[0].speed == 0)
|
||||||
read_speeds(numcpus, ci);
|
read_speeds(numcpus, ci);
|
||||||
|
|
||||||
@ -350,7 +359,7 @@ static void read_speeds(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|||||||
/* Also reads the CPU frequency on x86. The other architectures only have
|
/* Also reads the CPU frequency on x86. The other architectures only have
|
||||||
* a BogoMIPS field, which may not be very accurate.
|
* a BogoMIPS field, which may not be very accurate.
|
||||||
*/
|
*/
|
||||||
static void read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
|
static int read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
|
||||||
#if defined(__i386__) || defined(__x86_64__)
|
#if defined(__i386__) || defined(__x86_64__)
|
||||||
static const char model_marker[] = "model name\t: ";
|
static const char model_marker[] = "model name\t: ";
|
||||||
static const char speed_marker[] = "cpu MHz\t\t: ";
|
static const char speed_marker[] = "cpu MHz\t\t: ";
|
||||||
@ -373,7 +382,7 @@ static void read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|||||||
|
|
||||||
fp = fopen("/proc/cpuinfo", "r");
|
fp = fopen("/proc/cpuinfo", "r");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
return;
|
return -1;
|
||||||
|
|
||||||
model_idx = 0;
|
model_idx = 0;
|
||||||
speed_idx = 0;
|
speed_idx = 0;
|
||||||
@ -398,10 +407,12 @@ static void read_models(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void read_times(unsigned int numcpus, uv_cpu_info_t* ci) {
|
static int read_times(unsigned int numcpus, uv_cpu_info_t* ci) {
|
||||||
unsigned long clock_ticks;
|
unsigned long clock_ticks;
|
||||||
struct uv_cpu_times_s ts;
|
struct uv_cpu_times_s ts;
|
||||||
unsigned long user;
|
unsigned long user;
|
||||||
@ -421,7 +432,7 @@ static void read_times(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|||||||
|
|
||||||
fp = fopen("/proc/stat", "r");
|
fp = fopen("/proc/stat", "r");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
return;
|
return -1;
|
||||||
|
|
||||||
if (!fgets(buf, sizeof(buf), fp))
|
if (!fgets(buf, sizeof(buf), fp))
|
||||||
abort();
|
abort();
|
||||||
@ -466,6 +477,8 @@ static void read_times(unsigned int numcpus, uv_cpu_info_t* ci) {
|
|||||||
ci[num++].cpu_times = ts;
|
ci[num++].cpu_times = ts;
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user