OLD | NEW |
1 // Copyright 2009,2010 The Go Authors. All rights reserved. | 1 // Copyright 2009,2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Darwin system calls. | 5 // Darwin system calls. |
6 // This file is compiled as ordinary Go code, | 6 // This file is compiled as ordinary Go code, |
7 // but it is also input to mksyscall, | 7 // but it is also input to mksyscall, |
8 // which parses the //sys lines and generates system call stubs. | 8 // which parses the //sys lines and generates system call stubs. |
9 // Note that sometimes we use a lowercase //sys name and wrap | 9 // Note that sometimes we use a lowercase //sys name and wrap |
10 // it in our own nicer implementation, either here or in | 10 // it in our own nicer implementation, either here or in |
11 // syscall_bsd.go or syscall_unix.go. | 11 // syscall_bsd.go or syscall_unix.go. |
12 | 12 |
13 package syscall | 13 package syscall |
14 | 14 |
15 import "unsafe" | 15 import ( |
| 16 » errorspkg "errors" |
| 17 » "unsafe" |
| 18 ) |
| 19 |
| 20 const ImplementsGetwd = true |
| 21 |
| 22 func Getwd() (string, error) { |
| 23 » buf := make([]byte, 2048) |
| 24 » attrs, err := getAttrList(".", attrList{CommonAttr: attrCmnFullpath}, bu
f, 0) |
| 25 » if err == nil && len(attrs) == 1 && len(attrs[0]) >= 2 { |
| 26 » » wd := string(attrs[0]) |
| 27 » » // Sanity check that it's an absolute path and ends |
| 28 » » // in a null byte, which we then strip. |
| 29 » » if wd[0] == '/' && wd[len(wd)-1] == 0 { |
| 30 » » » return wd[:len(wd)-1], nil |
| 31 » » } |
| 32 » } |
| 33 » // If pkg/os/getwd.go gets ENOTSUP, it will fall back to the |
| 34 » // slow algorithm. |
| 35 » return "", ENOTSUP |
| 36 } |
16 | 37 |
17 type SockaddrDatalink struct { | 38 type SockaddrDatalink struct { |
18 Len uint8 | 39 Len uint8 |
19 Family uint8 | 40 Family uint8 |
20 Index uint16 | 41 Index uint16 |
21 Type uint8 | 42 Type uint8 |
22 Nlen uint8 | 43 Nlen uint8 |
23 Alen uint8 | 44 Alen uint8 |
24 Slen uint8 | 45 Slen uint8 |
25 Data [12]int8 | 46 Data [12]int8 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 count++ | 100 count++ |
80 names = append(names, name) | 101 names = append(names, name) |
81 } | 102 } |
82 return origlen - len(buf), count, names | 103 return origlen - len(buf), count, names |
83 } | 104 } |
84 | 105 |
85 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) | 106 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error) |
86 func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) } | 107 func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) } |
87 func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) } | 108 func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) } |
88 | 109 |
| 110 const ( |
| 111 attrBitMapCount = 5 |
| 112 attrCmnFullpath = 0x08000000 |
| 113 ) |
| 114 |
| 115 type attrList struct { |
| 116 bitmapCount uint16 |
| 117 _ uint16 |
| 118 CommonAttr uint32 |
| 119 VolAttr uint32 |
| 120 DirAttr uint32 |
| 121 FileAttr uint32 |
| 122 Forkattr uint32 |
| 123 } |
| 124 |
| 125 func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (
attrs [][]byte, err error) { |
| 126 if len(attrBuf) < 4 { |
| 127 return nil, errorspkg.New("attrBuf too small") |
| 128 } |
| 129 attrList.bitmapCount = attrBitMapCount |
| 130 |
| 131 var _p0 *byte |
| 132 _p0, err = BytePtrFromString(path) |
| 133 if err != nil { |
| 134 return nil, err |
| 135 } |
| 136 |
| 137 _, _, e1 := Syscall6( |
| 138 SYS_GETATTRLIST, |
| 139 uintptr(unsafe.Pointer(_p0)), |
| 140 uintptr(unsafe.Pointer(&attrList)), |
| 141 uintptr(unsafe.Pointer(&attrBuf[0])), |
| 142 uintptr(len(attrBuf)), |
| 143 uintptr(options), |
| 144 0, |
| 145 ) |
| 146 if e1 != 0 { |
| 147 return nil, e1 |
| 148 } |
| 149 size := *(*uint32)(unsafe.Pointer(&attrBuf[0])) |
| 150 |
| 151 // dat is the section of attrBuf that contains valid data, |
| 152 // without the 4 byte length header. All attribute offsets |
| 153 // are relative to dat. |
| 154 dat := attrBuf |
| 155 if int(size) < len(attrBuf) { |
| 156 dat = dat[:size] |
| 157 } |
| 158 dat = dat[4:] // remove length prefix |
| 159 |
| 160 for i := uint32(0); int(i) < len(dat); { |
| 161 header := dat[i:] |
| 162 if len(header) < 8 { |
| 163 return attrs, errorspkg.New("truncated attribute header"
) |
| 164 } |
| 165 datOff := *(*int32)(unsafe.Pointer(&header[0])) |
| 166 attrLen := *(*uint32)(unsafe.Pointer(&header[4])) |
| 167 if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) { |
| 168 return attrs, errorspkg.New("truncated results; attrBuf
too small") |
| 169 } |
| 170 end := uint32(datOff) + attrLen |
| 171 attrs = append(attrs, dat[datOff:end]) |
| 172 i = end |
| 173 if r := i % 4; r != 0 { |
| 174 i += (4 - r) |
| 175 } |
| 176 } |
| 177 return |
| 178 } |
| 179 |
89 //sysnb pipe() (r int, w int, err error) | 180 //sysnb pipe() (r int, w int, err error) |
90 | 181 |
91 func Pipe(p []int) (err error) { | 182 func Pipe(p []int) (err error) { |
92 if len(p) != 2 { | 183 if len(p) != 2 { |
93 return EINVAL | 184 return EINVAL |
94 } | 185 } |
95 p[0], p[1], err = pipe() | 186 p[0], p[1], err = pipe() |
96 return | 187 return |
97 } | 188 } |
98 | 189 |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 // Poll_nocancel | 480 // Poll_nocancel |
390 // Msgsnd_nocancel | 481 // Msgsnd_nocancel |
391 // Msgrcv_nocancel | 482 // Msgrcv_nocancel |
392 // Sem_wait_nocancel | 483 // Sem_wait_nocancel |
393 // Aio_suspend_nocancel | 484 // Aio_suspend_nocancel |
394 // __sigwait_nocancel | 485 // __sigwait_nocancel |
395 // __semwait_signal_nocancel | 486 // __semwait_signal_nocancel |
396 // __mac_mount | 487 // __mac_mount |
397 // __mac_get_mount | 488 // __mac_get_mount |
398 // __mac_getfsstat | 489 // __mac_getfsstat |
OLD | NEW |