]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: implement nametomib() on netbsd
authorJoel Sing <jsing@google.com>
Tue, 22 May 2012 15:33:48 +0000 (01:33 +1000)
committerJoel Sing <jsing@google.com>
Tue, 22 May 2012 15:33:48 +0000 (01:33 +1000)
Implement nametomib() on NetBSD using the CTL_QUERY node discovery
mechanism.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6211071

src/pkg/syscall/mkerrors.sh
src/pkg/syscall/syscall_netbsd.go
src/pkg/syscall/types_netbsd.go
src/pkg/syscall/zerrors_netbsd_386.go
src/pkg/syscall/zerrors_netbsd_amd64.go
src/pkg/syscall/ztypes_netbsd_386.go
src/pkg/syscall/ztypes_netbsd_amd64.go

index 7e3c339bad427235b4d2d26403620b2d63a89e41..16738c5b66be288413f03e2f8eb5f1194396e912 100755 (executable)
@@ -106,7 +106,7 @@ includes_NetBSD='
 #include <netinet/if_ether.h>
 
 // Needed since <sys/param.h> 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)_/ ||
index d36aa0c528002b7c6166d8c358b481fb4f8dfe00..3534c0277be2f4c024dd70b5a0b896a9655c894d 100644 (file)
@@ -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,
index 82b6d14aaf3301985563c5ccc7361e8dcf37cc96..5d3018ee59a51fbc3e4604e33c2ccef6b22e8ba7 100644 (file)
@@ -31,6 +31,7 @@ package syscall
 #include <sys/signal.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/sysctl.h>
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <sys/un.h>
@@ -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
index d7cc7c944a2845565a97080bf49a96337838392f..78c1685ec4b448bcc21d1cfc0e75f1764df42b15 100644 (file)
@@ -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
index 10967bed3b6c43f8e1740dadb691d26b11f4d2a6..dd99b50db6588bf882eaa674f5e9398b963ad77d 100644 (file)
@@ -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
index 0374adf221d32d2a37cfe1f525453bb51644fa90..69ed13939d2330e7195ebc1487de36efa6f7defc 100644 (file)
@@ -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
+}
index 5a5b4e46f5b9807c292d14c355180dce682aee4a..b6795ed5f06f7642b22f285a01938c90a884f76c 100644 (file)
@@ -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
+}