aboutsummaryrefslogtreecommitdiffstats
path: root/tools/power/cpupower/utils/helpers
diff options
context:
space:
mode:
authorPalmer Cox <p@lmercox.com>2012-11-27 07:17:45 -0500
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>2012-11-27 17:07:19 -0500
commit53d2000ebe0618219f73ac866701533237180044 (patch)
tree2e18236f6a0a7b42732c76cb78e962d23c0d9dd8 /tools/power/cpupower/utils/helpers
parentfb8eaeb7ab96b09c910e36abf7df7f9ecbb0fb60 (diff)
cpupower tools: Fix issues with sysfs_topology_read_file
Fix a variety of issues with sysfs_topology_read_file: * The return value of sysfs_topology_read_file function was not properly being checked for failure. * The function was reading int valued sysfs variables and then returning their value. So, even if a function was trying to check the return value of this function, a caller would not be able to tell an failure code apart from reading a negative value. This also conflicted with the comment on the function which said that a return value of 0 indicated success. * The function was parsing int valued sysfs values with strtoul instead of strtol. * The function was non-static even though it was only used in the file it was declared in. Signed-off-by: Palmer Cox <p@lmercox.com> Signed-off-by: Thomas Renninger <trenn@suse.de> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Diffstat (limited to 'tools/power/cpupower/utils/helpers')
-rw-r--r--tools/power/cpupower/utils/helpers/topology.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/tools/power/cpupower/utils/helpers/topology.c b/tools/power/cpupower/utils/helpers/topology.c
index 4eae2c47ba48..216f3e3466ce 100644
--- a/tools/power/cpupower/utils/helpers/topology.c
+++ b/tools/power/cpupower/utils/helpers/topology.c
@@ -20,9 +20,8 @@
20#include <helpers/sysfs.h> 20#include <helpers/sysfs.h>
21 21
22/* returns -1 on failure, 0 on success */ 22/* returns -1 on failure, 0 on success */
23int sysfs_topology_read_file(unsigned int cpu, const char *fname) 23static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
24{ 24{
25 unsigned long value;
26 char linebuf[MAX_LINE_LEN]; 25 char linebuf[MAX_LINE_LEN];
27 char *endp; 26 char *endp;
28 char path[SYSFS_PATH_MAX]; 27 char path[SYSFS_PATH_MAX];
@@ -31,10 +30,10 @@ int sysfs_topology_read_file(unsigned int cpu, const char *fname)
31 cpu, fname); 30 cpu, fname);
32 if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) 31 if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0)
33 return -1; 32 return -1;
34 value = strtoul(linebuf, &endp, 0); 33 *result = strtol(linebuf, &endp, 0);
35 if (endp == linebuf || errno == ERANGE) 34 if (endp == linebuf || errno == ERANGE)
36 return -1; 35 return -1;
37 return value; 36 return 0;
38} 37}
39 38
40struct cpuid_core_info { 39struct cpuid_core_info {
@@ -82,13 +81,19 @@ int get_cpu_topology(struct cpupower_topology *cpu_top)
82 for (cpu = 0; cpu < cpus; cpu++) { 81 for (cpu = 0; cpu < cpus; cpu++) {
83 cpu_top->core_info[cpu].cpu = cpu; 82 cpu_top->core_info[cpu].cpu = cpu;
84 cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu); 83 cpu_top->core_info[cpu].is_online = sysfs_is_cpu_online(cpu);
85 cpu_top->core_info[cpu].pkg = 84 if(sysfs_topology_read_file(
86 sysfs_topology_read_file(cpu, "physical_package_id"); 85 cpu,
86 "physical_package_id",
87 &(cpu_top->core_info[cpu].pkg)) < 0)
88 return -1;
87 if ((int)cpu_top->core_info[cpu].pkg != -1 && 89 if ((int)cpu_top->core_info[cpu].pkg != -1 &&
88 cpu_top->core_info[cpu].pkg > cpu_top->pkgs) 90 cpu_top->core_info[cpu].pkg > cpu_top->pkgs)
89 cpu_top->pkgs = cpu_top->core_info[cpu].pkg; 91 cpu_top->pkgs = cpu_top->core_info[cpu].pkg;
90 cpu_top->core_info[cpu].core = 92 if(sysfs_topology_read_file(
91 sysfs_topology_read_file(cpu, "core_id"); 93 cpu,
94 "core_id",
95 &(cpu_top->core_info[cpu].core)) < 0)
96 return -1;
92 } 97 }
93 cpu_top->pkgs++; 98 cpu_top->pkgs++;
94 99