From 9a9c35b386727a05abb0192344eae2f394d4469c Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Wed, 28 May 2025 21:36:31 +0300 Subject: [PATCH] syscall, internal/routebsd: use hw.supported_arches on FreeBSD 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. --- src/internal/routebsd/sys_freebsd.go | 24 ++++------------------- src/syscall/route_freebsd.go | 29 +++++++++++----------------- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/src/internal/routebsd/sys_freebsd.go b/src/internal/routebsd/sys_freebsd.go index 5d5f49e42e01ea..be110ae86b3136 100644 --- a/src/internal/routebsd/sys_freebsd.go +++ b/src/internal/routebsd/sys_freebsd.go @@ -5,6 +5,7 @@ package routebsd import ( + "internal/stringslite" "syscall" "unsafe" ) @@ -25,26 +26,9 @@ func probeRoutingStack() (int, map[int]*wireFormat) { // to know the underlying kernel's architecture because the // alignment for routing facilities are set at the build time // of the kernel. - conf, _ := syscall.Sysctl("kern.conftxt") - for i, j := 0, 0; j < len(conf); j++ { - if conf[j] != '\n' { - continue - } - s := conf[i:j] - i = j + 1 - if len(s) > len("machine") && s[:len("machine")] == "machine" { - s = s[len("machine"):] - for k := 0; k < len(s); k++ { - if s[k] == ' ' || s[k] == '\t' { - s = s[1:] - } - break - } - if s == "amd64" { - align = 8 - } - break - } + arches, _ := syscall.Sysctl("hw.supported_archs") + if stringslite.Index(arches, "amd64") >= 0 { + align = 8 } ifm := &wireFormat{extOff: 16, bodyOff: sizeofIfMsghdr} ifm.parse = ifm.parseInterfaceMessage diff --git a/src/syscall/route_freebsd.go b/src/syscall/route_freebsd.go index 2b47faff429845..8d3b69b143aa73 100644 --- a/src/syscall/route_freebsd.go +++ b/src/syscall/route_freebsd.go @@ -4,28 +4,21 @@ package syscall -import "unsafe" +import ( + "internal/stringslite" + "unsafe" +) func init() { - conf, _ := Sysctl("kern.conftxt") - for i, j := 0, 0; j < len(conf); j++ { - if conf[j] != '\n' { - continue - } - s := conf[i:j] - i = j + 1 - if len(s) > len("machine") && s[:len("machine")] == "machine" { - s = s[len("machine"):] - for k := 0; k < len(s); k++ { - if s[k] == ' ' || s[k] == '\t' { - s = s[1:] - } - break - } - freebsdConfArch = s - break + machine, _ := Sysctl("hw.machine") + if machine == "i386" { + arches, _ := Sysctl("hw.supported_archs") + amd64 := "amd64" + if stringslite.Index(arches, amd64) >= 0 { + machine = amd64 } } + freebsdConfArch = machine } func (any *anyMessage) toRoutingMessage(b []byte) RoutingMessage {