Debian Codegraph @

libgo/go/syscall/syscall_netbsd.go

raw
1 // Copyright 2009,2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package syscall
6
7 import "unsafe"
8
9 func direntIno(buf []byte) (uint64, bool) {
10 return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno))
11 }
12
13 func direntReclen(buf []byte) (uint64, bool) {
14 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
15 }
16
17 func direntNamlen(buf []byte) (uint64, bool) {
18 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
19 }
20
21 //sys Getdents(fd int, buf []byte) (n int, err error)
22 //getdents(fd _C_int, buf *byte, nbytes uintptr) _C_int
23
24 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
25 n, err = Getdents(fd, buf)
26 if err != nil || basep == nil {
27 return
28 }
29
30 var off int64
31 off, err = Seek(fd, 0, 1 /* SEEK_CUR */)
32 if err != nil {
33 *basep = ^uintptr(0)
34 return
35 }
36 *basep = uintptr(off)
37 if unsafe.Sizeof(*basep) == 8 {
38 return
39 }
40 if off>>32 != 0 {
41 // We can't stuff the offset back into a uintptr, so any
42 // future calls would be suspect. Generate an error.
43 // EIO is allowed by getdirentries.
44 err = EIO
45 }
46 return
47 }
48
49 func sysctlNodes(mib []_C_int) (nodes []Sysctlnode, err error) {
50 var olen uintptr
51
52 // Get a list of all sysctl nodes below the given MIB by performing
53 // a sysctl for the given MIB with CTL_QUERY appended.
54 mib = append(mib, CTL_QUERY)
55 qnode := Sysctlnode{Flags: SYSCTL_VERS_1}
56 qp := (*byte)(unsafe.Pointer(&qnode))
57 sz := unsafe.Sizeof(qnode)
58 if err = sysctl(mib, nil, &olen, qp, sz); err != nil {
59 return nil, err
60 }
61
62 // Now that we know the size, get the actual nodes.
63 nodes = make([]Sysctlnode, olen/sz)
64 np := (*byte)(unsafe.Pointer(&nodes[0]))
65 if err = sysctl(mib, np, &olen, qp, sz); err != nil {
66 return nil, err
67 }
68
69 return nodes, nil
70 }
71
72 func nametomib(name string) (mib []_C_int, err error) {
73 // Split name into components.
74 var parts []string
75 last := 0
76 for i := 0; i < len(name); i++ {
77 if name[i] == '.' {
78 parts = append(parts, name[last:i])
79 last = i + 1
80 }
81 }
82 parts = append(parts, name[last:])
83
84 // Discover the nodes and construct the MIB OID.
85 for partno, part := range parts {
86 nodes, err := sysctlNodes(mib)
87 if err != nil {
88 return nil, err
89 }
90 for _, node := range nodes {
91 n := make([]byte, 0)
92 for i := range node.Name {
93 if node.Name[i] != 0 {
94 n = append(n, byte(node.Name[i]))
95 }
96 }
97 if string(n) == part {
98 mib = append(mib, _C_int(node.Num))
99 break
100 }
101 }
102 if len(mib) != partno+1 {
103 return nil, EINVAL
104 }
105 }
106
107 return mib, nil
108 }