]>
Commit | Line | Data |
---|---|---|
c2047754 ILT |
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 | } | |
2ab1fc7a NB |
20 | |
21 | func sysctlNodes(mib []_C_int) (nodes []Sysctlnode, err error) { | |
22 | var olen uintptr | |
23 | ||
24 | // Get a list of all sysctl nodes below the given MIB by performing | |
25 | // a sysctl for the given MIB with CTL_QUERY appended. | |
26 | mib = append(mib, CTL_QUERY) | |
27 | qnode := Sysctlnode{Flags: SYSCTL_VERS_1} | |
28 | qp := (*byte)(unsafe.Pointer(&qnode)) | |
29 | sz := unsafe.Sizeof(qnode) | |
30 | if err = sysctl(mib, nil, &olen, qp, sz); err != nil { | |
31 | return nil, err | |
32 | } | |
33 | ||
34 | // Now that we know the size, get the actual nodes. | |
35 | nodes = make([]Sysctlnode, olen/sz) | |
36 | np := (*byte)(unsafe.Pointer(&nodes[0])) | |
37 | if err = sysctl(mib, np, &olen, qp, sz); err != nil { | |
38 | return nil, err | |
39 | } | |
40 | ||
41 | return nodes, nil | |
42 | } | |
43 | ||
44 | func nametomib(name string) (mib []_C_int, err error) { | |
45 | // Split name into components. | |
46 | var parts []string | |
47 | last := 0 | |
48 | for i := 0; i < len(name); i++ { | |
49 | if name[i] == '.' { | |
50 | parts = append(parts, name[last:i]) | |
51 | last = i + 1 | |
52 | } | |
53 | } | |
54 | parts = append(parts, name[last:]) | |
55 | ||
56 | // Discover the nodes and construct the MIB OID. | |
57 | for partno, part := range parts { | |
58 | nodes, err := sysctlNodes(mib) | |
59 | if err != nil { | |
60 | return nil, err | |
61 | } | |
62 | for _, node := range nodes { | |
63 | n := make([]byte, 0) | |
64 | for i := range node.Name { | |
65 | if node.Name[i] != 0 { | |
66 | n = append(n, byte(node.Name[i])) | |
67 | } | |
68 | } | |
69 | if string(n) == part { | |
70 | mib = append(mib, _C_int(node.Num)) | |
71 | break | |
72 | } | |
73 | } | |
74 | if len(mib) != partno+1 { | |
75 | return nil, EINVAL | |
76 | } | |
77 | } | |
78 | ||
79 | return mib, nil | |
80 | } |