Browse Source

unix: add Uname on Darwin

Darwin doesn't have a uname syscall but the same information can be
retrieved using sysctls. Use the raw sysctl numbers instead of doing the
roundtrip via SysctlString in order to void the additional sysctl calls
in nametomib and SysctlRaw.

This implementation follows the one in
https://opensource.apple.com/source/Libc/Libc-1244.1.7/gen/uname.c

Change-Id: I19541e2a6700f84ced0549a8fb86faa8cb5e6fbe
Reviewed-on: https://go-review.googlesource.com/77270
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Tobias Klauser 8 years ago
parent
commit
665f6529cc

+ 4 - 1
unix/mkerrors.sh

@@ -48,6 +48,7 @@ includes_Darwin='
 #include <sys/sysctl.h>
 #include <sys/sysctl.h>
 #include <sys/mman.h>
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/mount.h>
+#include <sys/utsname.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
 #include <net/bpf.h>
 #include <net/bpf.h>
 #include <net/if.h>
 #include <net/if.h>
@@ -386,7 +387,9 @@ ccflags="$@"
 		$2 == "SOMAXCONN" ||
 		$2 == "SOMAXCONN" ||
 		$2 == "NAME_MAX" ||
 		$2 == "NAME_MAX" ||
 		$2 == "IFNAMSIZ" ||
 		$2 == "IFNAMSIZ" ||
-		$2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
+		$2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
+		$2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
+		$2 ~ /^HW_MACHINE$/ ||
 		$2 ~ /^SYSCTL_VERS/ ||
 		$2 ~ /^SYSCTL_VERS/ ||
 		$2 ~ /^(MS|MNT|UMOUNT)_/ ||
 		$2 ~ /^(MS|MNT|UMOUNT)_/ ||
 		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
 		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||

+ 46 - 0
unix/syscall_darwin.go

@@ -270,6 +270,52 @@ func IoctlGetTermios(fd int, req uint) (*Termios, error) {
 	return &value, err
 	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
  * Exposed directly
  */
  */

+ 5 - 0
unix/types_darwin.go

@@ -39,6 +39,7 @@ package unix
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/uio.h>
 #include <sys/uio.h>
 #include <sys/un.h>
 #include <sys/un.h>
+#include <sys/utsname.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
 #include <net/bpf.h>
 #include <net/bpf.h>
 #include <net/if.h>
 #include <net/if.h>
@@ -270,3 +271,7 @@ const (
 	POLLWRBAND = C.POLLWRBAND
 	POLLWRBAND = C.POLLWRBAND
 	POLLWRNORM = C.POLLWRNORM
 	POLLWRNORM = C.POLLWRNORM
 )
 )
+
+// uname
+
+type Utsname C.struct_utsname

+ 7 - 0
unix/zerrors_darwin_386.go

@@ -249,6 +249,8 @@ const (
 	CSTOP                             = 0x13
 	CSTOP                             = 0x13
 	CSTOPB                            = 0x400
 	CSTOPB                            = 0x400
 	CSUSP                             = 0x1a
 	CSUSP                             = 0x1a
+	CTL_HW                            = 0x6
+	CTL_KERN                          = 0x1
 	CTL_MAXNAME                       = 0xc
 	CTL_MAXNAME                       = 0xc
 	CTL_NET                           = 0x4
 	CTL_NET                           = 0x4
 	DLT_A429                          = 0xb8
 	DLT_A429                          = 0xb8
@@ -532,6 +534,7 @@ const (
 	F_VOLPOSMODE                      = 0x4
 	F_VOLPOSMODE                      = 0x4
 	F_WRLCK                           = 0x3
 	F_WRLCK                           = 0x3
 	HUPCL                             = 0x4000
 	HUPCL                             = 0x4000
+	HW_MACHINE                        = 0x1
 	ICANON                            = 0x100
 	ICANON                            = 0x100
 	ICMP6_FILTER                      = 0x12
 	ICMP6_FILTER                      = 0x12
 	ICRNL                             = 0x100
 	ICRNL                             = 0x100
@@ -878,6 +881,10 @@ const (
 	IXANY                             = 0x800
 	IXANY                             = 0x800
 	IXOFF                             = 0x400
 	IXOFF                             = 0x400
 	IXON                              = 0x200
 	IXON                              = 0x200
+	KERN_HOSTNAME                     = 0xa
+	KERN_OSRELEASE                    = 0x2
+	KERN_OSTYPE                       = 0x1
+	KERN_VERSION                      = 0x4
 	LOCK_EX                           = 0x2
 	LOCK_EX                           = 0x2
 	LOCK_NB                           = 0x4
 	LOCK_NB                           = 0x4
 	LOCK_SH                           = 0x1
 	LOCK_SH                           = 0x1

+ 7 - 0
unix/zerrors_darwin_amd64.go

@@ -249,6 +249,8 @@ const (
 	CSTOP                             = 0x13
 	CSTOP                             = 0x13
 	CSTOPB                            = 0x400
 	CSTOPB                            = 0x400
 	CSUSP                             = 0x1a
 	CSUSP                             = 0x1a
+	CTL_HW                            = 0x6
+	CTL_KERN                          = 0x1
 	CTL_MAXNAME                       = 0xc
 	CTL_MAXNAME                       = 0xc
 	CTL_NET                           = 0x4
 	CTL_NET                           = 0x4
 	DLT_A429                          = 0xb8
 	DLT_A429                          = 0xb8
@@ -532,6 +534,7 @@ const (
 	F_VOLPOSMODE                      = 0x4
 	F_VOLPOSMODE                      = 0x4
 	F_WRLCK                           = 0x3
 	F_WRLCK                           = 0x3
 	HUPCL                             = 0x4000
 	HUPCL                             = 0x4000
+	HW_MACHINE                        = 0x1
 	ICANON                            = 0x100
 	ICANON                            = 0x100
 	ICMP6_FILTER                      = 0x12
 	ICMP6_FILTER                      = 0x12
 	ICRNL                             = 0x100
 	ICRNL                             = 0x100
@@ -878,6 +881,10 @@ const (
 	IXANY                             = 0x800
 	IXANY                             = 0x800
 	IXOFF                             = 0x400
 	IXOFF                             = 0x400
 	IXON                              = 0x200
 	IXON                              = 0x200
+	KERN_HOSTNAME                     = 0xa
+	KERN_OSRELEASE                    = 0x2
+	KERN_OSTYPE                       = 0x1
+	KERN_VERSION                      = 0x4
 	LOCK_EX                           = 0x2
 	LOCK_EX                           = 0x2
 	LOCK_NB                           = 0x4
 	LOCK_NB                           = 0x4
 	LOCK_SH                           = 0x1
 	LOCK_SH                           = 0x1

+ 7 - 0
unix/zerrors_darwin_arm.go

@@ -249,6 +249,8 @@ const (
 	CSTOP                             = 0x13
 	CSTOP                             = 0x13
 	CSTOPB                            = 0x400
 	CSTOPB                            = 0x400
 	CSUSP                             = 0x1a
 	CSUSP                             = 0x1a
+	CTL_HW                            = 0x6
+	CTL_KERN                          = 0x1
 	CTL_MAXNAME                       = 0xc
 	CTL_MAXNAME                       = 0xc
 	CTL_NET                           = 0x4
 	CTL_NET                           = 0x4
 	DLT_A429                          = 0xb8
 	DLT_A429                          = 0xb8
@@ -532,6 +534,7 @@ const (
 	F_VOLPOSMODE                      = 0x4
 	F_VOLPOSMODE                      = 0x4
 	F_WRLCK                           = 0x3
 	F_WRLCK                           = 0x3
 	HUPCL                             = 0x4000
 	HUPCL                             = 0x4000
+	HW_MACHINE                        = 0x1
 	ICANON                            = 0x100
 	ICANON                            = 0x100
 	ICMP6_FILTER                      = 0x12
 	ICMP6_FILTER                      = 0x12
 	ICRNL                             = 0x100
 	ICRNL                             = 0x100
@@ -878,6 +881,10 @@ const (
 	IXANY                             = 0x800
 	IXANY                             = 0x800
 	IXOFF                             = 0x400
 	IXOFF                             = 0x400
 	IXON                              = 0x200
 	IXON                              = 0x200
+	KERN_HOSTNAME                     = 0xa
+	KERN_OSRELEASE                    = 0x2
+	KERN_OSTYPE                       = 0x1
+	KERN_VERSION                      = 0x4
 	LOCK_EX                           = 0x2
 	LOCK_EX                           = 0x2
 	LOCK_NB                           = 0x4
 	LOCK_NB                           = 0x4
 	LOCK_SH                           = 0x1
 	LOCK_SH                           = 0x1

+ 7 - 0
unix/zerrors_darwin_arm64.go

@@ -249,6 +249,8 @@ const (
 	CSTOP                             = 0x13
 	CSTOP                             = 0x13
 	CSTOPB                            = 0x400
 	CSTOPB                            = 0x400
 	CSUSP                             = 0x1a
 	CSUSP                             = 0x1a
+	CTL_HW                            = 0x6
+	CTL_KERN                          = 0x1
 	CTL_MAXNAME                       = 0xc
 	CTL_MAXNAME                       = 0xc
 	CTL_NET                           = 0x4
 	CTL_NET                           = 0x4
 	DLT_A429                          = 0xb8
 	DLT_A429                          = 0xb8
@@ -532,6 +534,7 @@ const (
 	F_VOLPOSMODE                      = 0x4
 	F_VOLPOSMODE                      = 0x4
 	F_WRLCK                           = 0x3
 	F_WRLCK                           = 0x3
 	HUPCL                             = 0x4000
 	HUPCL                             = 0x4000
+	HW_MACHINE                        = 0x1
 	ICANON                            = 0x100
 	ICANON                            = 0x100
 	ICMP6_FILTER                      = 0x12
 	ICMP6_FILTER                      = 0x12
 	ICRNL                             = 0x100
 	ICRNL                             = 0x100
@@ -878,6 +881,10 @@ const (
 	IXANY                             = 0x800
 	IXANY                             = 0x800
 	IXOFF                             = 0x400
 	IXOFF                             = 0x400
 	IXON                              = 0x200
 	IXON                              = 0x200
+	KERN_HOSTNAME                     = 0xa
+	KERN_OSRELEASE                    = 0x2
+	KERN_OSTYPE                       = 0x1
+	KERN_VERSION                      = 0x4
 	LOCK_EX                           = 0x2
 	LOCK_EX                           = 0x2
 	LOCK_NB                           = 0x4
 	LOCK_NB                           = 0x4
 	LOCK_SH                           = 0x1
 	LOCK_SH                           = 0x1

+ 8 - 0
unix/ztypes_darwin_386.go

@@ -479,3 +479,11 @@ const (
 	POLLWRBAND = 0x100
 	POLLWRBAND = 0x100
 	POLLWRNORM = 0x4
 	POLLWRNORM = 0x4
 )
 )
+
+type Utsname struct {
+	Sysname  [256]byte
+	Nodename [256]byte
+	Release  [256]byte
+	Version  [256]byte
+	Machine  [256]byte
+}

+ 8 - 0
unix/ztypes_darwin_amd64.go

@@ -489,3 +489,11 @@ const (
 	POLLWRBAND = 0x100
 	POLLWRBAND = 0x100
 	POLLWRNORM = 0x4
 	POLLWRNORM = 0x4
 )
 )
+
+type Utsname struct {
+	Sysname  [256]byte
+	Nodename [256]byte
+	Release  [256]byte
+	Version  [256]byte
+	Machine  [256]byte
+}

+ 8 - 0
unix/ztypes_darwin_arm.go

@@ -480,3 +480,11 @@ const (
 	POLLWRBAND = 0x100
 	POLLWRBAND = 0x100
 	POLLWRNORM = 0x4
 	POLLWRNORM = 0x4
 )
 )
+
+type Utsname struct {
+	Sysname  [256]byte
+	Nodename [256]byte
+	Release  [256]byte
+	Version  [256]byte
+	Machine  [256]byte
+}

+ 8 - 0
unix/ztypes_darwin_arm64.go

@@ -489,3 +489,11 @@ const (
 	POLLWRBAND = 0x100
 	POLLWRBAND = 0x100
 	POLLWRNORM = 0x4
 	POLLWRNORM = 0x4
 )
 )
+
+type Utsname struct {
+	Sysname  [256]byte
+	Nodename [256]byte
+	Release  [256]byte
+	Version  [256]byte
+	Machine  [256]byte
+}