unix: add Uname on FreeBSD
FreeBSD doesn't have a uname syscall but the information can be
retrieved using sysctls the same way as on Darwin.
Change-Id: I824c42490d1feed3f1ad3823427c01dd3e5ea3c1
Reviewed-on: https://go-review.googlesource.com/79918
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/unix/syscall_freebsd.go b/unix/syscall_freebsd.go
index a82ce12..bcf9812 100644
--- a/unix/syscall_freebsd.go
+++ b/unix/syscall_freebsd.go
@@ -396,6 +396,52 @@
return &value, err
}
+func Uname(uname *Utsname) error {
+ mib := []_C_int{CTL_KERN, KERN_OSTYPE}
+ n := unsafe.Sizeof(uname.Sysname)
+ if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
+ return err
+ }
+
+ mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
+ n = unsafe.Sizeof(uname.Nodename)
+ if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
+ return err
+ }
+
+ mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
+ n = unsafe.Sizeof(uname.Release)
+ if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
+ return err
+ }
+
+ mib = []_C_int{CTL_KERN, KERN_VERSION}
+ n = unsafe.Sizeof(uname.Version)
+ if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
+ return err
+ }
+
+ // The version might have newlines or tabs in it, convert them to
+ // spaces.
+ for i, b := range uname.Version {
+ if b == '\n' || b == '\t' {
+ if i == len(uname.Version)-1 {
+ uname.Version[i] = 0
+ } else {
+ uname.Version[i] = ' '
+ }
+ }
+ }
+
+ mib = []_C_int{CTL_HW, HW_MACHINE}
+ n = unsafe.Sizeof(uname.Machine)
+ if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
+ return err
+ }
+
+ return nil
+}
+
/*
* Exposed directly
*/
diff --git a/unix/syscall_freebsd_test.go b/unix/syscall_freebsd_test.go
index 654439e..170a043 100644
--- a/unix/syscall_freebsd_test.go
+++ b/unix/syscall_freebsd_test.go
@@ -295,3 +295,13 @@
t.Fatalf("Wrong rights set")
}
}
+
+func TestUname(t *testing.T) {
+ var utsname unix.Utsname
+ err := unix.Uname(&utsname)
+ if err != nil {
+ t.Fatalf("Uname: %v", err)
+ }
+
+ t.Logf("OS: %s/%s %s", utsname.Sysname[:], utsname.Machine[:], utsname.Release[:])
+}
diff --git a/unix/types_freebsd.go b/unix/types_freebsd.go
index 7340004..8ba8b1d 100644
--- a/unix/types_freebsd.go
+++ b/unix/types_freebsd.go
@@ -36,6 +36,7 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
+#include <sys/utsname.h>
#include <sys/wait.h>
#include <net/bpf.h>
#include <net/if.h>
@@ -389,3 +390,7 @@
// Capabilities
type CapRights C.struct_cap_rights
+
+// Uname
+
+type Utsname C.struct_utsname
diff --git a/unix/zerrors_freebsd_386.go b/unix/zerrors_freebsd_386.go
index adf5eef..a8b0587 100644
--- a/unix/zerrors_freebsd_386.go
+++ b/unix/zerrors_freebsd_386.go
@@ -351,6 +351,8 @@
CSTOP = 0x13
CSTOPB = 0x400
CSUSP = 0x1a
+ CTL_HW = 0x6
+ CTL_KERN = 0x1
CTL_MAXNAME = 0x18
CTL_NET = 0x4
DLT_A429 = 0xb8
@@ -608,6 +610,7 @@
F_UNLCKSYS = 0x4
F_WRLCK = 0x3
HUPCL = 0x4000
+ HW_MACHINE = 0x1
ICANON = 0x100
ICMP6_FILTER = 0x12
ICRNL = 0x100
@@ -944,6 +947,10 @@
IXANY = 0x800
IXOFF = 0x400
IXON = 0x200
+ KERN_HOSTNAME = 0xa
+ KERN_OSRELEASE = 0x2
+ KERN_OSTYPE = 0x1
+ KERN_VERSION = 0x4
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
diff --git a/unix/zerrors_freebsd_amd64.go b/unix/zerrors_freebsd_amd64.go
index 360caff..cf5f012 100644
--- a/unix/zerrors_freebsd_amd64.go
+++ b/unix/zerrors_freebsd_amd64.go
@@ -351,6 +351,8 @@
CSTOP = 0x13
CSTOPB = 0x400
CSUSP = 0x1a
+ CTL_HW = 0x6
+ CTL_KERN = 0x1
CTL_MAXNAME = 0x18
CTL_NET = 0x4
DLT_A429 = 0xb8
@@ -608,6 +610,7 @@
F_UNLCKSYS = 0x4
F_WRLCK = 0x3
HUPCL = 0x4000
+ HW_MACHINE = 0x1
ICANON = 0x100
ICMP6_FILTER = 0x12
ICRNL = 0x100
@@ -944,6 +947,10 @@
IXANY = 0x800
IXOFF = 0x400
IXON = 0x200
+ KERN_HOSTNAME = 0xa
+ KERN_OSRELEASE = 0x2
+ KERN_OSTYPE = 0x1
+ KERN_VERSION = 0x4
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
diff --git a/unix/zerrors_freebsd_arm.go b/unix/zerrors_freebsd_arm.go
index 87deda9..9bbb90a 100644
--- a/unix/zerrors_freebsd_arm.go
+++ b/unix/zerrors_freebsd_arm.go
@@ -351,6 +351,8 @@
CSTOP = 0x13
CSTOPB = 0x400
CSUSP = 0x1a
+ CTL_HW = 0x6
+ CTL_KERN = 0x1
CTL_MAXNAME = 0x18
CTL_NET = 0x4
DLT_A429 = 0xb8
@@ -615,6 +617,7 @@
F_UNLCKSYS = 0x4
F_WRLCK = 0x3
HUPCL = 0x4000
+ HW_MACHINE = 0x1
ICANON = 0x100
ICMP6_FILTER = 0x12
ICRNL = 0x100
@@ -951,6 +954,10 @@
IXANY = 0x800
IXOFF = 0x400
IXON = 0x200
+ KERN_HOSTNAME = 0xa
+ KERN_OSRELEASE = 0x2
+ KERN_OSTYPE = 0x1
+ KERN_VERSION = 0x4
LOCK_EX = 0x2
LOCK_NB = 0x4
LOCK_SH = 0x1
diff --git a/unix/ztypes_freebsd_386.go b/unix/ztypes_freebsd_386.go
index 18f7816..f11c787 100644
--- a/unix/ztypes_freebsd_386.go
+++ b/unix/ztypes_freebsd_386.go
@@ -539,3 +539,11 @@
type CapRights struct {
Rights [2]uint64
}
+
+type Utsname struct {
+ Sysname [256]byte
+ Nodename [256]byte
+ Release [256]byte
+ Version [256]byte
+ Machine [256]byte
+}
diff --git a/unix/ztypes_freebsd_amd64.go b/unix/ztypes_freebsd_amd64.go
index dd0db2a..6580947 100644
--- a/unix/ztypes_freebsd_amd64.go
+++ b/unix/ztypes_freebsd_amd64.go
@@ -542,3 +542,11 @@
type CapRights struct {
Rights [2]uint64
}
+
+type Utsname struct {
+ Sysname [256]byte
+ Nodename [256]byte
+ Release [256]byte
+ Version [256]byte
+ Machine [256]byte
+}
diff --git a/unix/ztypes_freebsd_arm.go b/unix/ztypes_freebsd_arm.go
index 473d3dc..5a73ab3 100644
--- a/unix/ztypes_freebsd_arm.go
+++ b/unix/ztypes_freebsd_arm.go
@@ -542,3 +542,11 @@
type CapRights struct {
Rights [2]uint64
}
+
+type Utsname struct {
+ Sysname [256]byte
+ Nodename [256]byte
+ Release [256]byte
+ Version [256]byte
+ Machine [256]byte
+}