1 diff -up irqbalance-0.55/irqbalance-0.55/cputree.c.orig irqbalance-0.55/irqbalance-0.55/cputree.c
2 --- irqbalance-0.55/irqbalance-0.55/cputree.c.orig 2006-12-10 15:04:59.000000000 -0500
3 +++ irqbalance-0.55/irqbalance-0.55/cputree.c 2007-09-28 12:43:35.000000000 -0400
13 @@ -131,34 +133,30 @@ static void fill_cache_domain(void)
17 -static void do_one_cpu(char *path)
18 +static void do_one_cpu(int dfd, char *d_name)
22 char new_path[PATH_MAX];
24 /* skip offline cpus */
25 - snprintf(new_path, PATH_MAX, "%s/online", path);
26 - file = fopen(new_path, "r");
30 - if (getline(&line, &size, file)==0)
31 + snprintf(new_path, PATH_MAX, "%s/online", d_name);
32 + int fd = openat(dfd, new_path, O_RDONLY);
35 + ssize_t n = read(fd, buf, sizeof(buf));
37 + if (n != sizeof(buf))
40 - if (line && line[0]=='0') {
48 - cpu = malloc(sizeof(struct cpu_core));
49 + cpu = calloc(1, sizeof(struct cpu_core));
52 - memset(cpu, 0, sizeof(struct cpu_core));
54 - cpu->number = strtoul(&path[27], NULL, 10);
55 + cpu->number = strtoul(&d_name[3], NULL, 10);
57 cpu_set(cpu->number, cpu->mask);
59 @@ -170,43 +168,45 @@ static void do_one_cpu(char *path)
66 /* try to read the package mask; if it doesn't exist assume solitary */
67 - snprintf(new_path, PATH_MAX, "%s/topology/core_siblings", path);
68 - file = fopen(new_path, "r");
69 + snprintf(new_path, PATH_MAX, "%s/topology/core_siblings", d_name);
70 + fd = openat(dfd, new_path, O_RDONLY);
71 + file = fd == -1 ? NULL : fdopen(fd, "r");
72 cpu_set(cpu->number, cpu->package_mask);
76 if (getline(&line, &size, file))
77 cpumask_parse_user(line, strlen(line), cpu->package_mask);
81 + } else if (fd != -1)
84 /* try to read the cache mask; if it doesn't exist assume solitary */
85 /* We want the deepest cache level available so try index1 first, then index2 */
86 cpu_set(cpu->number, cpu->cache_mask);
87 - snprintf(new_path, PATH_MAX, "%s/cache/index1/shared_cpu_map", path);
88 - file = fopen(new_path, "r");
89 + snprintf(new_path, PATH_MAX, "%s/cache/index1/shared_cpu_map", d_name);
90 + fd = openat(dfd, new_path, O_RDONLY);
91 + file = fd == -1 ? NULL : fdopen(fd, "r");
95 if (getline(&line, &size, file))
96 cpumask_parse_user(line, strlen(line), cpu->cache_mask);
100 - snprintf(new_path, PATH_MAX, "%s/cache/index2/shared_cpu_map", path);
101 - file = fopen(new_path, "r");
102 + } else if (fd != -1)
105 + snprintf(new_path, PATH_MAX, "%s/cache/index2/shared_cpu_map", d_name);
106 + fd = openat(dfd, new_path, O_RDONLY);
107 + file = fd == -1 ? NULL : fdopen(fd, "r");
111 if (getline(&line, &size, file))
112 cpumask_parse_user(line, strlen(line), cpu->cache_mask);
116 + } else if (fd != -1)
122 blank out the banned cpus from the various masks so that interrupts
123 @@ -311,18 +311,19 @@ void parse_cpu_tree(void)
126 struct dirent *entry;
129 cpus_complement(unbanned_cpus, banned_cpus);
131 dir = opendir("/sys/devices/system/cpu");
136 entry = readdir(dir);
137 - if (entry && strlen(entry->d_name)>3 && strstr(entry->d_name,"cpu")) {
138 - char new_path[PATH_MAX];
139 - sprintf(new_path, "/sys/devices/system/cpu/%s", entry->d_name);
140 - do_one_cpu(new_path);
141 + if (entry && strlen(entry->d_name)>3 && memcmp(entry->d_name,"cpu",3) == 0
142 + && isdigit(entry->d_name[3])) {
143 + do_one_cpu(dfd, entry->d_name);