From: Joel Sing Date: Tue, 22 May 2012 15:33:48 +0000 (+1000) Subject: syscall: implement nametomib() on netbsd X-Git-Tag: go1.1rc2~3160 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=495a9dc2b3ac76004e1324ca38761efad848ad96;p=gostls13.git syscall: implement nametomib() on netbsd Implement nametomib() on NetBSD using the CTL_QUERY node discovery mechanism. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6211071 --- diff --git a/src/pkg/syscall/mkerrors.sh b/src/pkg/syscall/mkerrors.sh index 7e3c339bad..16738c5b66 100755 --- a/src/pkg/syscall/mkerrors.sh +++ b/src/pkg/syscall/mkerrors.sh @@ -106,7 +106,7 @@ includes_NetBSD=' #include // Needed since refers to it... -const int schedppq = 1; +#define schedppq 1 ' includes_OpenBSD=' @@ -199,8 +199,8 @@ ccflags="$@" $2 == "SOMAXCONN" || $2 == "NAME_MAX" || $2 == "IFNAMSIZ" || - $2 == "CTL_NET" || - $2 == "CTL_MAXNAME" || + $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ || + $2 ~ /^SYSCTL_VERS/ || $2 ~ /^(MS|MNT)_/ || $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ || $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ || diff --git a/src/pkg/syscall/syscall_netbsd.go b/src/pkg/syscall/syscall_netbsd.go index d36aa0c528..3534c0277b 100644 --- a/src/pkg/syscall/syscall_netbsd.go +++ b/src/pkg/syscall/syscall_netbsd.go @@ -28,8 +28,66 @@ type SockaddrDatalink struct { func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err Errno) +func sysctlNodes(mib []_C_int) (nodes []Sysctlnode, err error) { + var olen uintptr + + // Get a list of all sysctl nodes below the given MIB by performing + // a sysctl for the given MIB with CTL_QUERY appended. + mib = append(mib, CTL_QUERY) + qnode := Sysctlnode{Flags: SYSCTL_VERS_1} + qp := (*byte)(unsafe.Pointer(&qnode)) + sz := unsafe.Sizeof(qnode) + if err = sysctl(mib, nil, &olen, qp, sz); err != nil { + return nil, err + } + + // Now that we know the size, get the actual nodes. + nodes = make([]Sysctlnode, olen/sz) + np := (*byte)(unsafe.Pointer(&nodes[0])) + if err = sysctl(mib, np, &olen, qp, sz); err != nil { + return nil, err + } + + return nodes, nil +} + func nametomib(name string) (mib []_C_int, err error) { - return nil, EINVAL + + // Split name into components. + var parts []string + last := 0 + for i := 0; i < len(name); i++ { + if name[i] == '.' { + parts = append(parts, name[last:i]) + last = i + 1 + } + } + parts = append(parts, name[last:]) + + // Discover the nodes and construct the MIB OID. + for partno, part := range parts { + nodes, err := sysctlNodes(mib) + if err != nil { + return nil, err + } + for _, node := range nodes { + n := make([]byte, 0) + for i := range node.Name { + if node.Name[i] != 0 { + n = append(n, byte(node.Name[i])) + } + } + if string(n) == part { + mib = append(mib, _C_int(node.Num)) + break + } + } + if len(mib) != partno+1 { + return nil, EINVAL + } + } + + return mib, nil } // ParseDirent parses up to max directory entries in buf, diff --git a/src/pkg/syscall/types_netbsd.go b/src/pkg/syscall/types_netbsd.go index 82b6d14aaf..5d3018ee59 100644 --- a/src/pkg/syscall/types_netbsd.go +++ b/src/pkg/syscall/types_netbsd.go @@ -31,6 +31,7 @@ package syscall #include #include #include +#include #include #include #include @@ -210,3 +211,7 @@ type BpfInsn C.struct_bpf_insn type BpfHdr C.struct_bpf_hdr type BpfTimeval C.struct_bpf_timeval + +// Sysctl + +type Sysctlnode C.struct_sysctlnode diff --git a/src/pkg/syscall/zerrors_netbsd_386.go b/src/pkg/syscall/zerrors_netbsd_386.go index d7cc7c944a..78c1685ec4 100644 --- a/src/pkg/syscall/zerrors_netbsd_386.go +++ b/src/pkg/syscall/zerrors_netbsd_386.go @@ -159,6 +159,7 @@ const ( CSUSP = 0x1a CTL_MAXNAME = 0xc CTL_NET = 0x4 + CTL_QUERY = -0x2 DIOCBSFLUSH = 0x20006478 DLT_AIRONET_HEADER = 0x78 DLT_APPLE_IP_OVER_IEEE1394 = 0x8a @@ -984,6 +985,7 @@ const ( PARMRK = 0x8 PARODD = 0x2000 PENDIN = 0x20000000 + PRI_IOFLUSH = 0x7c RLIMIT_AS = 0xa RLIMIT_CORE = 0x4 RLIMIT_CPU = 0x0 @@ -1163,6 +1165,10 @@ const ( SO_TIMESTAMP = 0x2000 SO_TYPE = 0x1008 SO_USELOOPBACK = 0x40 + SYSCTL_VERSION = 0x1000000 + SYSCTL_VERS_0 = 0x0 + SYSCTL_VERS_1 = 0x1000000 + SYSCTL_VERS_MASK = 0xff000000 S_ARCH1 = 0x10000 S_ARCH2 = 0x20000 S_BLKSIZE = 0x200 diff --git a/src/pkg/syscall/zerrors_netbsd_amd64.go b/src/pkg/syscall/zerrors_netbsd_amd64.go index 10967bed3b..dd99b50db6 100644 --- a/src/pkg/syscall/zerrors_netbsd_amd64.go +++ b/src/pkg/syscall/zerrors_netbsd_amd64.go @@ -159,6 +159,7 @@ const ( CSUSP = 0x1a CTL_MAXNAME = 0xc CTL_NET = 0x4 + CTL_QUERY = -0x2 DIOCBSFLUSH = 0x20006478 DLT_AIRONET_HEADER = 0x78 DLT_APPLE_IP_OVER_IEEE1394 = 0x8a @@ -974,6 +975,7 @@ const ( PARMRK = 0x8 PARODD = 0x2000 PENDIN = 0x20000000 + PRI_IOFLUSH = 0x7c RLIMIT_AS = 0xa RLIMIT_CORE = 0x4 RLIMIT_CPU = 0x0 @@ -1153,6 +1155,10 @@ const ( SO_TIMESTAMP = 0x2000 SO_TYPE = 0x1008 SO_USELOOPBACK = 0x40 + SYSCTL_VERSION = 0x1000000 + SYSCTL_VERS_0 = 0x0 + SYSCTL_VERS_1 = 0x1000000 + SYSCTL_VERS_MASK = 0xff000000 S_ARCH1 = 0x10000 S_ARCH2 = 0x20000 S_BLKSIZE = 0x200 diff --git a/src/pkg/syscall/ztypes_netbsd_386.go b/src/pkg/syscall/ztypes_netbsd_386.go index 0374adf221..69ed13939d 100644 --- a/src/pkg/syscall/ztypes_netbsd_386.go +++ b/src/pkg/syscall/ztypes_netbsd_386.go @@ -348,3 +348,16 @@ type BpfTimeval struct { Sec int32 Usec int32 } + +type Sysctlnode struct { + Flags uint32 + Num int32 + Name [32]int8 + Ver uint32 + X__rsvd uint32 + Un [16]byte + X_sysctl_size [8]byte + X_sysctl_func [8]byte + X_sysctl_parent [8]byte + X_sysctl_desc [8]byte +} diff --git a/src/pkg/syscall/ztypes_netbsd_amd64.go b/src/pkg/syscall/ztypes_netbsd_amd64.go index 5a5b4e46f5..b6795ed5f0 100644 --- a/src/pkg/syscall/ztypes_netbsd_amd64.go +++ b/src/pkg/syscall/ztypes_netbsd_amd64.go @@ -355,3 +355,16 @@ type BpfTimeval struct { Sec int64 Usec int64 } + +type Sysctlnode struct { + Flags uint32 + Num int32 + Name [32]int8 + Ver uint32 + X__rsvd uint32 + Un [16]byte + X_sysctl_size [8]byte + X_sysctl_func [8]byte + X_sysctl_parent [8]byte + X_sysctl_desc [8]byte +}