Skip to content

Commit

Permalink
cmd/internal/osinfo: stop importing golang.org/x/sys/unix
Browse files Browse the repository at this point in the history
This is the only non-vendored file that imports x/sys/unix.
Switch to fetching the information in this package.

Change-Id: I4e54c2cd8b4953066e2bee42922f35c387fb43e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/623435
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Oct 30, 2024
1 parent 060bd25 commit 555ef55
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 24 deletions.
36 changes: 36 additions & 0 deletions src/cmd/internal/osinfo/os_solaris.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Supporting definitions for os_uname.go on Solaris.

package osinfo

import (
"syscall"
"unsafe"
)

type utsname struct {
Sysname [257]byte
Nodename [257]byte
Release [257]byte
Version [257]byte
Machine [257]byte
}

//go:cgo_import_dynamic libc_uname uname "libc.so"
//go:linkname procUname libc_uname

var procUname uintptr

//go:linkname rawsysvicall6 runtime.syscall_rawsysvicall6
func rawsysvicall6(fn, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err syscall.Errno)

func uname(buf *utsname) error {
_, _, errno := rawsysvicall6(uintptr(unsafe.Pointer(&procUname)), 1, uintptr(unsafe.Pointer(buf)), 0, 0, 0, 0, 0)
if errno != 0 {
return errno
}
return nil
}
17 changes: 17 additions & 0 deletions src/cmd/internal/osinfo/os_syscall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || linux

// Supporting definitions for os_uname.go on AIX and Linux.

package osinfo

import "syscall"

type utsname = syscall.Utsname

func uname(buf *utsname) error {
return syscall.Uname(buf)
}
41 changes: 41 additions & 0 deletions src/cmd/internal/osinfo/os_sysctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin || dragonfly || freebsd || netbsd || openbsd

package osinfo

import (
"strings"
"syscall"
)

// Version returns the OS version name/number.
func Version() (string, error) {
sysname, err := syscall.Sysctl("kern.ostype")
if err != nil {
return "", err
}
release, err := syscall.Sysctl("kern.osrelease")
if err != nil {
return "", err
}
version, err := syscall.Sysctl("kern.version")
if err != nil {
return "", err
}

// The version might have newlines or tabs; convert to spaces.
version = strings.ReplaceAll(version, "\n", " ")
version = strings.ReplaceAll(version, "\t", " ")
version = strings.TrimSpace(version)

machine, err := syscall.Sysctl("hw.machine")
if err != nil {
return "", err
}

ret := sysname + " " + release + " " + version + " " + machine
return ret, nil
}
47 changes: 47 additions & 0 deletions src/cmd/internal/osinfo/os_uname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || linux || solaris

package osinfo

import (
"bytes"
"strings"
"unsafe"
)

// Version returns the OS version name/number.
func Version() (string, error) {
var uts utsname
if err := uname(&uts); err != nil {
return "", err
}

var sb strings.Builder

writeCStr := func(b []byte) {
if i := bytes.IndexByte(b, '\000'); i >= 0 {
b = b[:i]
}
sb.Write(b)
}

// We need some absurd conversions because syscall.Utsname
// sometimes uses []uint8 and sometimes []int8.

s := uts.Sysname[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
sb.WriteByte(' ')
s = uts.Release[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
sb.WriteByte(' ')
s = uts.Version[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))
sb.WriteByte(' ')
s = uts.Machine[:]
writeCStr(*(*[]byte)(unsafe.Pointer(&s)))

return sb.String(), nil
}
24 changes: 0 additions & 24 deletions src/cmd/internal/osinfo/os_unix.go

This file was deleted.

26 changes: 26 additions & 0 deletions src/cmd/internal/osinfo/version_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build unix

package osinfo

import (
"strings"
"testing"
)

func TestVersion(t *testing.T) {
v, err := Version()
if err != nil {
t.Fatal(err)
}

t.Logf("%q", v)

fields := strings.Fields(v)
if len(fields) < 4 {
t.Errorf("wanted at least 4 fields in %q, got %d", v, len(fields))
}
}

0 comments on commit 555ef55

Please sign in to comment.