-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/internal/osinfo: stop importing golang.org/x/sys/unix
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
1 parent
060bd25
commit 555ef55
Showing
6 changed files
with
167 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |