Skip to content

Commit 38c2474

Browse files
committed
FreeBSD: use proper method to detect host architecture
Reading textual representation of the kernel config file to search for the "machine" config clause is awful, not to mention that kern.conftxt might be disabled at all. Use sysctl hw.supported_archs to detect 32bit binary running on amd64 host.
1 parent c61e5e7 commit 38c2474

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

src/internal/routebsd/sys_freebsd.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,15 @@ func probeRoutingStack() (int, map[int]*wireFormat) {
2525
// to know the underlying kernel's architecture because the
2626
// alignment for routing facilities are set at the build time
2727
// of the kernel.
28-
conf, _ := syscall.Sysctl("kern.conftxt")
29-
for i, j := 0, 0; j < len(conf); j++ {
30-
if conf[j] != '\n' {
31-
continue
28+
arches, _ := syscall.Sysctl("hw.supported_archs")
29+
amd64 := "amd64"
30+
for i := 0; i < len(arches); i++ {
31+
s := arches[i:i + len(amd64)]
32+
if len(s) < len(amd64) {
33+
break
3234
}
33-
s := conf[i:j]
34-
i = j + 1
35-
if len(s) > len("machine") && s[:len("machine")] == "machine" {
36-
s = s[len("machine"):]
37-
for k := 0; k < len(s); k++ {
38-
if s[k] == ' ' || s[k] == '\t' {
39-
s = s[1:]
40-
}
41-
break
42-
}
43-
if s == "amd64" {
44-
align = 8
45-
}
35+
if s == amd64 {
36+
align = 8
4637
break
4738
}
4839
}

src/syscall/route_freebsd.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@ package syscall
77
import "unsafe"
88

99
func init() {
10-
conf, _ := Sysctl("kern.conftxt")
11-
for i, j := 0, 0; j < len(conf); j++ {
12-
if conf[j] != '\n' {
13-
continue
14-
}
15-
s := conf[i:j]
16-
i = j + 1
17-
if len(s) > len("machine") && s[:len("machine")] == "machine" {
18-
s = s[len("machine"):]
19-
for k := 0; k < len(s); k++ {
20-
if s[k] == ' ' || s[k] == '\t' {
21-
s = s[1:]
22-
}
10+
machine, _ := Sysctl("hw.machine")
11+
if machine == "i386" {
12+
arches, _ := Sysctl("hw.supported_archs")
13+
amd64 := "amd64"
14+
for i := 0; i < len(arches); i++ {
15+
s := arches[i:i + len(amd64)]
16+
if len(s) < len(amd64) {
17+
break
18+
}
19+
if s == amd64 {
20+
machine = "amd64"
2321
break
2422
}
25-
freebsdConfArch = s
26-
break
2723
}
2824
}
25+
freebsdConfArch = machine
2926
}
3027

3128
func (any *anyMessage) toRoutingMessage(b []byte) RoutingMessage {

0 commit comments

Comments
 (0)