aboutsummaryrefslogtreecommitdiffstats
path: root/meta-initramfs/recipes-kernel/kexec/kexec-tools-klibc/0005-crashdump-elf.c-work-around-for-sysconf-_SC_NPROCESS.patch
blob: 4f7506d44c8e281834c59081ac1f050024f9f49d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
From 1cfee75d9662c3382bb7cb8f1c952333837598ae Mon Sep 17 00:00:00 2001
From: Andrea Adami <andrea.adami@gmail.com>
Date: Wed, 18 Apr 2018 02:21:30 +0200
Subject: [PATCH] crashdump-elf.c: work around for _SC_NPROCESSORS_CONF

klibc sysconf lacks this so the implementation
of Linus Torvalds was taken (simplified):

https://sourceware.org/ml/libc-alpha/2011-06/msg00079.html

Have fun reding the thread!

Fix

 crashdump-elf.c:117:21: error: '_SC_NPROCESSORS_CONF' undeclared

Upstream-Status: Inappropriate [klibc specific]
Signed-off-by: Andrea Adami <andrea.adami@gmail.com>

---
 kexec/crashdump-elf.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/kexec/crashdump-elf.c b/kexec/crashdump-elf.c
index b8bb686..7e6767c 100644
--- a/kexec/crashdump-elf.c
+++ b/kexec/crashdump-elf.c
@@ -25,6 +25,94 @@ do {									\
 } while(0)
 #endif
 
+#ifdef __KLIBC__
+#ifndef KLIBC_SYSFS_CPU_H
+#define KLIBC_SYSFS_CPU_H
+
+
+static int __get_sysfs_cpus(const char *path);
+int __get_nprocs (void);
+int __get_nprocs_conf (void);
+
+
+
+static int __get_sysfs_cpus(const char *path)
+{
+	FILE *file;
+	int nr_cpus = 0;
+	int prev = -1;
+	char *p;
+	char line[10];
+
+
+	file = fopen(path, "r");
+	if (!file)
+		return -1;
+	for (;;) {
+		char sep;
+		int cpu;
+		int n;
+
+		/* int n = fscanf(file, "%u%c", &cpu, &sep); */
+		p = fgets(line, sizeof(line), file);
+		if (p == NULL)
+			return -1;
+		else
+			n = sscanf(line, "%u%c", &cpu, &sep);
+
+		if (n <= 0)
+			break;
+
+		/* EOF == EOLN */
+		if (n == 1)
+			sep = '\n';
+
+		/* Was the previous CPU a range? */
+		if (prev >= 0) {
+			nr_cpus += cpu - prev + 1;
+			prev = -1;
+		} else if (sep == '-')
+			prev = cpu;
+		else
+		nr_cpus++;
+
+		if (sep == '\n')
+			break;
+	}
+	fclose(file);
+	return nr_cpus;
+}
+
+int __get_nprocs ()
+{
+	long ret;
+	static int cached = -1;
+
+	ret = cached;
+	if (ret < 0)
+	{
+		ret = __get_sysfs_cpus("/sys/devices/system/cpu/online");
+		cached = ret;
+	}
+	return ret;
+}
+
+int __get_nprocs_conf ()
+{
+	long ret;
+	static int cached = -1;
+
+	ret = cached;
+	if (ret < 0)
+	{
+		ret = __get_sysfs_cpus("/sys/devices/system/cpu/possible");
+		cached = ret;
+	}
+	return ret;
+}
+#endif
+#endif
+
 /* Prepares the crash memory headers and stores in supplied buffer. */
 int FUNC(struct kexec_info *info,
 	 struct crash_elf_info *elf_info,
@@ -46,7 +134,11 @@ int FUNC(struct kexec_info *info,
 	if (xen_present())
 		nr_cpus = xen_get_nr_phys_cpus();
 	else
+#ifndef __KLIBC__
 		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
+#else
+		nr_cpus = __get_nprocs_conf();
+#endif
 
 	if (nr_cpus < 0) {
 		return -1;