blob: 4604c2a5b7d67be15ad48dd30c557c96a300b9dd [file] [log] [blame]
Brad Bishop26bdd442019-08-16 17:08:17 -04001From 43fb9cec1749b337bfa252fc2c1b0288847e8fa7 Mon Sep 17 00:00:00 2001
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08002From: Andrea Adami <andrea.adami@gmail.com>
3Date: Wed, 18 Apr 2018 02:21:30 +0200
4Subject: [PATCH] crashdump-elf.c: work around for _SC_NPROCESSORS_CONF
5
6klibc sysconf lacks this so the implementation
7of Linus Torvalds was taken (simplified):
8
9https://sourceware.org/ml/libc-alpha/2011-06/msg00079.html
10
11Have fun reding the thread!
12
13Fix
14
15 crashdump-elf.c:117:21: error: '_SC_NPROCESSORS_CONF' undeclared
16
17Upstream-Status: Inappropriate [klibc specific]
18Signed-off-by: Andrea Adami <andrea.adami@gmail.com>
19
20---
Brad Bishop26bdd442019-08-16 17:08:17 -040021 kexec/crashdump-elf.c | 92 +++++++++++++++++++++++++++++++++++++++++++
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080022 1 file changed, 92 insertions(+)
23
24diff --git a/kexec/crashdump-elf.c b/kexec/crashdump-elf.c
25index b8bb686..7e6767c 100644
26--- a/kexec/crashdump-elf.c
27+++ b/kexec/crashdump-elf.c
28@@ -25,6 +25,94 @@ do { \
29 } while(0)
30 #endif
31
32+#ifdef __KLIBC__
33+#ifndef KLIBC_SYSFS_CPU_H
34+#define KLIBC_SYSFS_CPU_H
35+
36+
37+static int __get_sysfs_cpus(const char *path);
38+int __get_nprocs (void);
39+int __get_nprocs_conf (void);
40+
41+
42+
43+static int __get_sysfs_cpus(const char *path)
44+{
45+ FILE *file;
46+ int nr_cpus = 0;
47+ int prev = -1;
48+ char *p;
49+ char line[10];
50+
51+
52+ file = fopen(path, "r");
53+ if (!file)
54+ return -1;
55+ for (;;) {
56+ char sep;
57+ int cpu;
58+ int n;
59+
60+ /* int n = fscanf(file, "%u%c", &cpu, &sep); */
61+ p = fgets(line, sizeof(line), file);
62+ if (p == NULL)
63+ return -1;
64+ else
65+ n = sscanf(line, "%u%c", &cpu, &sep);
66+
67+ if (n <= 0)
68+ break;
69+
70+ /* EOF == EOLN */
71+ if (n == 1)
72+ sep = '\n';
73+
74+ /* Was the previous CPU a range? */
75+ if (prev >= 0) {
76+ nr_cpus += cpu - prev + 1;
77+ prev = -1;
78+ } else if (sep == '-')
79+ prev = cpu;
80+ else
81+ nr_cpus++;
82+
83+ if (sep == '\n')
84+ break;
85+ }
86+ fclose(file);
87+ return nr_cpus;
88+}
89+
90+int __get_nprocs ()
91+{
92+ long ret;
93+ static int cached = -1;
94+
95+ ret = cached;
96+ if (ret < 0)
97+ {
98+ ret = __get_sysfs_cpus("/sys/devices/system/cpu/online");
99+ cached = ret;
100+ }
101+ return ret;
102+}
103+
104+int __get_nprocs_conf ()
105+{
106+ long ret;
107+ static int cached = -1;
108+
109+ ret = cached;
110+ if (ret < 0)
111+ {
112+ ret = __get_sysfs_cpus("/sys/devices/system/cpu/possible");
113+ cached = ret;
114+ }
115+ return ret;
116+}
117+#endif
118+#endif
119+
120 /* Prepares the crash memory headers and stores in supplied buffer. */
121 int FUNC(struct kexec_info *info,
122 struct crash_elf_info *elf_info,
123@@ -46,7 +134,11 @@ int FUNC(struct kexec_info *info,
124 if (xen_present())
125 nr_cpus = xen_get_nr_phys_cpus();
126 else
127+#ifndef __KLIBC__
128 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
129+#else
130+ nr_cpus = __get_nprocs_conf();
131+#endif
132
133 if (nr_cpus < 0) {
134 return -1;