1
0
mirror of https://github.com/golang/go synced 2024-11-20 04:24:51 -07:00

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
This commit is contained in:
Joel Sing 2012-05-23 01:33:48 +10:00
parent 5c6162cdd1
commit 495a9dc2b3
7 changed files with 105 additions and 4 deletions

View File

@ -106,7 +106,7 @@ includes_NetBSD='
#include <netinet/if_ether.h> #include <netinet/if_ether.h>
// Needed since <sys/param.h> refers to it... // Needed since <sys/param.h> refers to it...
const int schedppq = 1; #define schedppq 1
' '
includes_OpenBSD=' includes_OpenBSD='
@ -199,8 +199,8 @@ ccflags="$@"
$2 == "SOMAXCONN" || $2 == "SOMAXCONN" ||
$2 == "NAME_MAX" || $2 == "NAME_MAX" ||
$2 == "IFNAMSIZ" || $2 == "IFNAMSIZ" ||
$2 == "CTL_NET" || $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
$2 == "CTL_MAXNAME" || $2 ~ /^SYSCTL_VERS/ ||
$2 ~ /^(MS|MNT)_/ || $2 ~ /^(MS|MNT)_/ ||
$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ || $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
$2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ || $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||

View 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 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) { func nametomib(name string) (mib []_C_int, err error) {
// 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 nil, EINVAL
}
}
return mib, nil
} }
// ParseDirent parses up to max directory entries in buf, // ParseDirent parses up to max directory entries in buf,

View File

@ -31,6 +31,7 @@ package syscall
#include <sys/signal.h> #include <sys/signal.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/un.h> #include <sys/un.h>
@ -210,3 +211,7 @@ type BpfInsn C.struct_bpf_insn
type BpfHdr C.struct_bpf_hdr type BpfHdr C.struct_bpf_hdr
type BpfTimeval C.struct_bpf_timeval type BpfTimeval C.struct_bpf_timeval
// Sysctl
type Sysctlnode C.struct_sysctlnode

View File

@ -159,6 +159,7 @@ const (
CSUSP = 0x1a CSUSP = 0x1a
CTL_MAXNAME = 0xc CTL_MAXNAME = 0xc
CTL_NET = 0x4 CTL_NET = 0x4
CTL_QUERY = -0x2
DIOCBSFLUSH = 0x20006478 DIOCBSFLUSH = 0x20006478
DLT_AIRONET_HEADER = 0x78 DLT_AIRONET_HEADER = 0x78
DLT_APPLE_IP_OVER_IEEE1394 = 0x8a DLT_APPLE_IP_OVER_IEEE1394 = 0x8a
@ -984,6 +985,7 @@ const (
PARMRK = 0x8 PARMRK = 0x8
PARODD = 0x2000 PARODD = 0x2000
PENDIN = 0x20000000 PENDIN = 0x20000000
PRI_IOFLUSH = 0x7c
RLIMIT_AS = 0xa RLIMIT_AS = 0xa
RLIMIT_CORE = 0x4 RLIMIT_CORE = 0x4
RLIMIT_CPU = 0x0 RLIMIT_CPU = 0x0
@ -1163,6 +1165,10 @@ const (
SO_TIMESTAMP = 0x2000 SO_TIMESTAMP = 0x2000
SO_TYPE = 0x1008 SO_TYPE = 0x1008
SO_USELOOPBACK = 0x40 SO_USELOOPBACK = 0x40
SYSCTL_VERSION = 0x1000000
SYSCTL_VERS_0 = 0x0
SYSCTL_VERS_1 = 0x1000000
SYSCTL_VERS_MASK = 0xff000000
S_ARCH1 = 0x10000 S_ARCH1 = 0x10000
S_ARCH2 = 0x20000 S_ARCH2 = 0x20000
S_BLKSIZE = 0x200 S_BLKSIZE = 0x200

View File

@ -159,6 +159,7 @@ const (
CSUSP = 0x1a CSUSP = 0x1a
CTL_MAXNAME = 0xc CTL_MAXNAME = 0xc
CTL_NET = 0x4 CTL_NET = 0x4
CTL_QUERY = -0x2
DIOCBSFLUSH = 0x20006478 DIOCBSFLUSH = 0x20006478
DLT_AIRONET_HEADER = 0x78 DLT_AIRONET_HEADER = 0x78
DLT_APPLE_IP_OVER_IEEE1394 = 0x8a DLT_APPLE_IP_OVER_IEEE1394 = 0x8a
@ -974,6 +975,7 @@ const (
PARMRK = 0x8 PARMRK = 0x8
PARODD = 0x2000 PARODD = 0x2000
PENDIN = 0x20000000 PENDIN = 0x20000000
PRI_IOFLUSH = 0x7c
RLIMIT_AS = 0xa RLIMIT_AS = 0xa
RLIMIT_CORE = 0x4 RLIMIT_CORE = 0x4
RLIMIT_CPU = 0x0 RLIMIT_CPU = 0x0
@ -1153,6 +1155,10 @@ const (
SO_TIMESTAMP = 0x2000 SO_TIMESTAMP = 0x2000
SO_TYPE = 0x1008 SO_TYPE = 0x1008
SO_USELOOPBACK = 0x40 SO_USELOOPBACK = 0x40
SYSCTL_VERSION = 0x1000000
SYSCTL_VERS_0 = 0x0
SYSCTL_VERS_1 = 0x1000000
SYSCTL_VERS_MASK = 0xff000000
S_ARCH1 = 0x10000 S_ARCH1 = 0x10000
S_ARCH2 = 0x20000 S_ARCH2 = 0x20000
S_BLKSIZE = 0x200 S_BLKSIZE = 0x200

View File

@ -348,3 +348,16 @@ type BpfTimeval struct {
Sec int32 Sec int32
Usec 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
}

View File

@ -355,3 +355,16 @@ type BpfTimeval struct {
Sec int64 Sec int64
Usec 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
}