| 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 |
// Darwin system calls. |
| 6 |
// This file is compiled as ordinary Go code, |
| 7 |
// but it is also input to mksyscall, |
| 8 |
// which parses the //sys lines and generates system call stubs. |
| 9 |
// Note that sometimes we use a lowercase //sys name and wrap |
| 10 |
// it in our own nicer implementation, either here or in |
| 11 |
// syscall_bsd.go or syscall_unix.go. |
| 12 |
|
| 13 |
package unix |
| 14 |
|
| 15 |
import ( |
| 16 |
"fmt" |
| 17 |
"syscall" |
| 18 |
"unsafe" |
| 19 |
) |
| 20 |
|
| 21 |
//sys closedir(dir uintptr) (err error) |
| 22 |
//sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno) |
| 23 |
|
| 24 |
func fdopendir(fd int) (dir uintptr, err error) { |
| 25 |
r0, _, e1 := syscall_syscallPtr(libc_fdopendir_trampoline_addr, uintptr(fd), 0, 0) |
| 26 |
dir = uintptr(r0) |
| 27 |
if e1 != 0 { |
| 28 |
err = errnoErr(e1) |
| 29 |
} |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
var libc_fdopendir_trampoline_addr uintptr |
| 34 |
|
| 35 |
//go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib" |
| 36 |
|
| 37 |
func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) { |
| 38 |
// Simulate Getdirentries using fdopendir/readdir_r/closedir. |
| 39 |
// We store the number of entries to skip in the seek |
| 40 |
// offset of fd. See issue #31368. |
| 41 |
// It's not the full required semantics, but should handle the case |
| 42 |
// of calling Getdirentries or ReadDirent repeatedly. |
| 43 |
// It won't handle assigning the results of lseek to *basep, or handle |
| 44 |
// the directory being edited underfoot. |
| 45 |
skip, err := Seek(fd, 0, 1 /* SEEK_CUR */) |
| 46 |
if err != nil { |
| 47 |
return 0, err |
| 48 |
} |
| 49 |
|
| 50 |
// We need to duplicate the incoming file descriptor |
| 51 |
// because the caller expects to retain control of it, but |
| 52 |
// fdopendir expects to take control of its argument. |
| 53 |
// Just Dup'ing the file descriptor is not enough, as the |
| 54 |
// result shares underlying state. Use Openat to make a really |
| 55 |
// new file descriptor referring to the same directory. |
| 56 |
fd2, err := Openat(fd, ".", O_RDONLY, 0) |
| 57 |
if err != nil { |
| 58 |
return 0, err |
| 59 |
} |
| 60 |
d, err := fdopendir(fd2) |
| 61 |
if err != nil { |
| 62 |
Close(fd2) |
| 63 |
return 0, err |
| 64 |
} |
| 65 |
defer closedir(d) |
| 66 |
|
| 67 |
var cnt int64 |
| 68 |
for { |
| 69 |
var entry Dirent |
| 70 |
var entryp *Dirent |
| 71 |
e := readdir_r(d, &entry, &entryp) |
| 72 |
if e != 0 { |
| 73 |
return n, errnoErr(e) |
| 74 |
} |
| 75 |
if entryp == nil { |
| 76 |
break |
| 77 |
} |
| 78 |
if skip > 0 { |
| 79 |
skip-- |
| 80 |
cnt++ |
| 81 |
continue |
| 82 |
} |
| 83 |
|
| 84 |
reclen := int(entry.Reclen) |
| 85 |
if reclen > len(buf) { |
| 86 |
// Not enough room. Return for now. |
| 87 |
// The counter will let us know where we should start up again. |
| 88 |
// Note: this strategy for suspending in the middle and |
| 89 |
// restarting is O(n^2) in the length of the directory. Oh well. |
| 90 |
break |
| 91 |
} |
| 92 |
|
| 93 |
// Copy entry into return buffer. |
| 94 |
s := unsafe.Slice((*byte)(unsafe.Pointer(&entry)), reclen) |
| 95 |
copy(buf, s) |
| 96 |
|
| 97 |
buf = buf[reclen:] |
| 98 |
n += reclen |
| 99 |
cnt++ |
| 100 |
} |
| 101 |
// Set the seek offset of the input fd to record |
| 102 |
// how many files we've already returned. |
| 103 |
_, err = Seek(fd, cnt, 0 /* SEEK_SET */) |
| 104 |
if err != nil { |
| 105 |
return n, err |
| 106 |
} |
| 107 |
|
| 108 |
return n, nil |
| 109 |
} |
| 110 |
|
| 111 |
// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets. |
| 112 |
type SockaddrDatalink struct { |
| 113 |
Len uint8 |
| 114 |
Family uint8 |
| 115 |
Index uint16 |
| 116 |
Type uint8 |
| 117 |
Nlen uint8 |
| 118 |
Alen uint8 |
| 119 |
Slen uint8 |
| 120 |
Data [12]int8 |
| 121 |
raw RawSockaddrDatalink |
| 122 |
} |
| 123 |
|
| 124 |
// SockaddrCtl implements the Sockaddr interface for AF_SYSTEM type sockets. |
| 125 |
type SockaddrCtl struct { |
| 126 |
ID uint32 |
| 127 |
Unit uint32 |
| 128 |
raw RawSockaddrCtl |
| 129 |
} |
| 130 |
|
| 131 |
func (sa *SockaddrCtl) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 132 |
sa.raw.Sc_len = SizeofSockaddrCtl |
| 133 |
sa.raw.Sc_family = AF_SYSTEM |
| 134 |
sa.raw.Ss_sysaddr = AF_SYS_CONTROL |
| 135 |
sa.raw.Sc_id = sa.ID |
| 136 |
sa.raw.Sc_unit = sa.Unit |
| 137 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrCtl, nil |
| 138 |
} |
| 139 |
|
| 140 |
// SockaddrVM implements the Sockaddr interface for AF_VSOCK type sockets. |
| 141 |
// SockaddrVM provides access to Darwin VM sockets: a mechanism that enables |
| 142 |
// bidirectional communication between a hypervisor and its guest virtual |
| 143 |
// machines. |
| 144 |
type SockaddrVM struct { |
| 145 |
// CID and Port specify a context ID and port address for a VM socket. |
| 146 |
// Guests have a unique CID, and hosts may have a well-known CID of: |
| 147 |
// - VMADDR_CID_HYPERVISOR: refers to the hypervisor process. |
| 148 |
// - VMADDR_CID_LOCAL: refers to local communication (loopback). |
| 149 |
// - VMADDR_CID_HOST: refers to other processes on the host. |
| 150 |
CID uint32 |
| 151 |
Port uint32 |
| 152 |
raw RawSockaddrVM |
| 153 |
} |
| 154 |
|
| 155 |
func (sa *SockaddrVM) sockaddr() (unsafe.Pointer, _Socklen, error) { |
| 156 |
sa.raw.Len = SizeofSockaddrVM |
| 157 |
sa.raw.Family = AF_VSOCK |
| 158 |
sa.raw.Port = sa.Port |
| 159 |
sa.raw.Cid = sa.CID |
| 160 |
|
| 161 |
return unsafe.Pointer(&sa.raw), SizeofSockaddrVM, nil |
| 162 |
} |
| 163 |
|
| 164 |
func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { |
| 165 |
switch rsa.Addr.Family { |
| 166 |
case AF_SYSTEM: |
| 167 |
pp := (*RawSockaddrCtl)(unsafe.Pointer(rsa)) |
| 168 |
if pp.Ss_sysaddr == AF_SYS_CONTROL { |
| 169 |
sa := new(SockaddrCtl) |
| 170 |
sa.ID = pp.Sc_id |
| 171 |
sa.Unit = pp.Sc_unit |
| 172 |
return sa, nil |
| 173 |
} |
| 174 |
case AF_VSOCK: |
| 175 |
pp := (*RawSockaddrVM)(unsafe.Pointer(rsa)) |
| 176 |
sa := &SockaddrVM{ |
| 177 |
CID: pp.Cid, |
| 178 |
Port: pp.Port, |
| 179 |
} |
| 180 |
return sa, nil |
| 181 |
} |
| 182 |
return nil, EAFNOSUPPORT |
| 183 |
} |
| 184 |
|
| 185 |
// Some external packages rely on SYS___SYSCTL being defined to implement their |
| 186 |
// own sysctl wrappers. Provide it here, even though direct syscalls are no |
| 187 |
// longer supported on darwin. |
| 188 |
const SYS___SYSCTL = SYS_SYSCTL |
| 189 |
|
| 190 |
// Translate "kern.hostname" to []_C_int{0,1,2,3}. |
| 191 |
func nametomib(name string) (mib []_C_int, err error) { |
| 192 |
const siz = unsafe.Sizeof(mib[0]) |
| 193 |
|
| 194 |
// NOTE(rsc): It seems strange to set the buffer to have |
| 195 |
// size CTL_MAXNAME+2 but use only CTL_MAXNAME |
| 196 |
// as the size. I don't know why the +2 is here, but the |
| 197 |
// kernel uses +2 for its own implementation of this function. |
| 198 |
// I am scared that if we don't include the +2 here, the kernel |
| 199 |
// will silently write 2 words farther than we specify |
| 200 |
// and we'll get memory corruption. |
| 201 |
var buf [CTL_MAXNAME + 2]_C_int |
| 202 |
n := uintptr(CTL_MAXNAME) * siz |
| 203 |
|
| 204 |
p := (*byte)(unsafe.Pointer(&buf[0])) |
| 205 |
bytes, err := ByteSliceFromString(name) |
| 206 |
if err != nil { |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
|
| 210 |
// Magic sysctl: "setting" 0.3 to a string name |
| 211 |
// lets you read back the array of integers form. |
| 212 |
if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil { |
| 213 |
return nil, err |
| 214 |
} |
| 215 |
return buf[0 : n/siz], nil |
| 216 |
} |
| 217 |
|
| 218 |
func direntIno(buf []byte) (uint64, bool) { |
| 219 |
return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino)) |
| 220 |
} |
| 221 |
|
| 222 |
func direntReclen(buf []byte) (uint64, bool) { |
| 223 |
return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen)) |
| 224 |
} |
| 225 |
|
| 226 |
func direntNamlen(buf []byte) (uint64, bool) { |
| 227 |
return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen)) |
| 228 |
} |
| 229 |
|
| 230 |
func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) } |
| 231 |
func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) } |
| 232 |
func PtraceDenyAttach() (err error) { return ptrace(PT_DENY_ATTACH, 0, 0, 0) } |
| 233 |
|
| 234 |
//sysnb pipe(p *[2]int32) (err error) |
| 235 |
|
| 236 |
func Pipe(p []int) (err error) { |
| 237 |
if len(p) != 2 { |
| 238 |
return EINVAL |
| 239 |
} |
| 240 |
var x [2]int32 |
| 241 |
err = pipe(&x) |
| 242 |
if err == nil { |
| 243 |
p[0] = int(x[0]) |
| 244 |
p[1] = int(x[1]) |
| 245 |
} |
| 246 |
return |
| 247 |
} |
| 248 |
|
| 249 |
func Getfsstat(buf []Statfs_t, flags int) (n int, err error) { |
| 250 |
var _p0 unsafe.Pointer |
| 251 |
var bufsize uintptr |
| 252 |
if len(buf) > 0 { |
| 253 |
_p0 = unsafe.Pointer(&buf[0]) |
| 254 |
bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf)) |
| 255 |
} |
| 256 |
return getfsstat(_p0, bufsize, flags) |
| 257 |
} |
| 258 |
|
| 259 |
func xattrPointer(dest []byte) *byte { |
| 260 |
// It's only when dest is set to NULL that the OS X implementations of |
| 261 |
// getxattr() and listxattr() return the current sizes of the named attributes. |
| 262 |
// An empty byte array is not sufficient. To maintain the same behaviour as the |
| 263 |
// linux implementation, we wrap around the system calls and pass in NULL when |
| 264 |
// dest is empty. |
| 265 |
var destp *byte |
| 266 |
if len(dest) > 0 { |
| 267 |
destp = &dest[0] |
| 268 |
} |
| 269 |
return destp |
| 270 |
} |
| 271 |
|
| 272 |
//sys getxattr(path string, attr string, dest *byte, size int, position uint32, options int) (sz int, err error) |
| 273 |
|
| 274 |
func Getxattr(path string, attr string, dest []byte) (sz int, err error) { |
| 275 |
return getxattr(path, attr, xattrPointer(dest), len(dest), 0, 0) |
| 276 |
} |
| 277 |
|
| 278 |
func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) { |
| 279 |
return getxattr(link, attr, xattrPointer(dest), len(dest), 0, XATTR_NOFOLLOW) |
| 280 |
} |
| 281 |
|
| 282 |
//sys fgetxattr(fd int, attr string, dest *byte, size int, position uint32, options int) (sz int, err error) |
| 283 |
|
| 284 |
func Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) { |
| 285 |
return fgetxattr(fd, attr, xattrPointer(dest), len(dest), 0, 0) |
| 286 |
} |
| 287 |
|
| 288 |
//sys setxattr(path string, attr string, data *byte, size int, position uint32, options int) (err error) |
| 289 |
|
| 290 |
func Setxattr(path string, attr string, data []byte, flags int) (err error) { |
| 291 |
// The parameters for the OS X implementation vary slightly compared to the |
| 292 |
// linux system call, specifically the position parameter: |
| 293 |
// |
| 294 |
// linux: |
| 295 |
// int setxattr( |
| 296 |
// const char *path, |
| 297 |
// const char *name, |
| 298 |
// const void *value, |
| 299 |
// size_t size, |
| 300 |
// int flags |
| 301 |
// ); |
| 302 |
// |
| 303 |
// darwin: |
| 304 |
// int setxattr( |
| 305 |
// const char *path, |
| 306 |
// const char *name, |
| 307 |
// void *value, |
| 308 |
// size_t size, |
| 309 |
// u_int32_t position, |
| 310 |
// int options |
| 311 |
// ); |
| 312 |
// |
| 313 |
// position specifies the offset within the extended attribute. In the |
| 314 |
// current implementation, only the resource fork extended attribute makes |
| 315 |
// use of this argument. For all others, position is reserved. We simply |
| 316 |
// default to setting it to zero. |
| 317 |
return setxattr(path, attr, xattrPointer(data), len(data), 0, flags) |
| 318 |
} |
| 319 |
|
| 320 |
func Lsetxattr(link string, attr string, data []byte, flags int) (err error) { |
| 321 |
return setxattr(link, attr, xattrPointer(data), len(data), 0, flags|XATTR_NOFOLLOW) |
| 322 |
} |
| 323 |
|
| 324 |
//sys fsetxattr(fd int, attr string, data *byte, size int, position uint32, options int) (err error) |
| 325 |
|
| 326 |
func Fsetxattr(fd int, attr string, data []byte, flags int) (err error) { |
| 327 |
return fsetxattr(fd, attr, xattrPointer(data), len(data), 0, 0) |
| 328 |
} |
| 329 |
|
| 330 |
//sys removexattr(path string, attr string, options int) (err error) |
| 331 |
|
| 332 |
func Removexattr(path string, attr string) (err error) { |
| 333 |
// We wrap around and explicitly zero out the options provided to the OS X |
| 334 |
// implementation of removexattr, we do so for interoperability with the |
| 335 |
// linux variant. |
| 336 |
return removexattr(path, attr, 0) |
| 337 |
} |
| 338 |
|
| 339 |
func Lremovexattr(link string, attr string) (err error) { |
| 340 |
return removexattr(link, attr, XATTR_NOFOLLOW) |
| 341 |
} |
| 342 |
|
| 343 |
//sys fremovexattr(fd int, attr string, options int) (err error) |
| 344 |
|
| 345 |
func Fremovexattr(fd int, attr string) (err error) { |
| 346 |
return fremovexattr(fd, attr, 0) |
| 347 |
} |
| 348 |
|
| 349 |
//sys listxattr(path string, dest *byte, size int, options int) (sz int, err error) |
| 350 |
|
| 351 |
func Listxattr(path string, dest []byte) (sz int, err error) { |
| 352 |
return listxattr(path, xattrPointer(dest), len(dest), 0) |
| 353 |
} |
| 354 |
|
| 355 |
func Llistxattr(link string, dest []byte) (sz int, err error) { |
| 356 |
return listxattr(link, xattrPointer(dest), len(dest), XATTR_NOFOLLOW) |
| 357 |
} |
| 358 |
|
| 359 |
//sys flistxattr(fd int, dest *byte, size int, options int) (sz int, err error) |
| 360 |
|
| 361 |
func Flistxattr(fd int, dest []byte) (sz int, err error) { |
| 362 |
return flistxattr(fd, xattrPointer(dest), len(dest), 0) |
| 363 |
} |
| 364 |
|
| 365 |
//sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) |
| 366 |
|
| 367 |
/* |
| 368 |
* Wrapped |
| 369 |
*/ |
| 370 |
|
| 371 |
//sys fcntl(fd int, cmd int, arg int) (val int, err error) |
| 372 |
|
| 373 |
//sys kill(pid int, signum int, posix int) (err error) |
| 374 |
|
| 375 |
func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(signum), 1) } |
| 376 |
|
| 377 |
//sys ioctl(fd int, req uint, arg uintptr) (err error) |
| 378 |
//sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL |
| 379 |
|
| 380 |
func IoctlCtlInfo(fd int, ctlInfo *CtlInfo) error { |
| 381 |
return ioctlPtr(fd, CTLIOCGINFO, unsafe.Pointer(ctlInfo)) |
| 382 |
} |
| 383 |
|
| 384 |
// IfreqMTU is struct ifreq used to get or set a network device's MTU. |
| 385 |
type IfreqMTU struct { |
| 386 |
Name [IFNAMSIZ]byte |
| 387 |
MTU int32 |
| 388 |
} |
| 389 |
|
| 390 |
// IoctlGetIfreqMTU performs the SIOCGIFMTU ioctl operation on fd to get the MTU |
| 391 |
// of the network device specified by ifname. |
| 392 |
func IoctlGetIfreqMTU(fd int, ifname string) (*IfreqMTU, error) { |
| 393 |
var ifreq IfreqMTU |
| 394 |
copy(ifreq.Name[:], ifname) |
| 395 |
err := ioctlPtr(fd, SIOCGIFMTU, unsafe.Pointer(&ifreq)) |
| 396 |
return &ifreq, err |
| 397 |
} |
| 398 |
|
| 399 |
// IoctlSetIfreqMTU performs the SIOCSIFMTU ioctl operation on fd to set the MTU |
| 400 |
// of the network device specified by ifreq.Name. |
| 401 |
func IoctlSetIfreqMTU(fd int, ifreq *IfreqMTU) error { |
| 402 |
return ioctlPtr(fd, SIOCSIFMTU, unsafe.Pointer(ifreq)) |
| 403 |
} |
| 404 |
|
| 405 |
//sys renamexNp(from string, to string, flag uint32) (err error) |
| 406 |
|
| 407 |
func RenamexNp(from string, to string, flag uint32) (err error) { |
| 408 |
return renamexNp(from, to, flag) |
| 409 |
} |
| 410 |
|
| 411 |
//sys renameatxNp(fromfd int, from string, tofd int, to string, flag uint32) (err error) |
| 412 |
|
| 413 |
func RenameatxNp(fromfd int, from string, tofd int, to string, flag uint32) (err error) { |
| 414 |
return renameatxNp(fromfd, from, tofd, to, flag) |
| 415 |
} |
| 416 |
|
| 417 |
//sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS_SYSCTL |
| 418 |
|
| 419 |
func Uname(uname *Utsname) error { |
| 420 |
mib := []_C_int{CTL_KERN, KERN_OSTYPE} |
| 421 |
n := unsafe.Sizeof(uname.Sysname) |
| 422 |
if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil { |
| 423 |
return err |
| 424 |
} |
| 425 |
|
| 426 |
mib = []_C_int{CTL_KERN, KERN_HOSTNAME} |
| 427 |
n = unsafe.Sizeof(uname.Nodename) |
| 428 |
if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil { |
| 429 |
return err |
| 430 |
} |
| 431 |
|
| 432 |
mib = []_C_int{CTL_KERN, KERN_OSRELEASE} |
| 433 |
n = unsafe.Sizeof(uname.Release) |
| 434 |
if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil { |
| 435 |
return err |
| 436 |
} |
| 437 |
|
| 438 |
mib = []_C_int{CTL_KERN, KERN_VERSION} |
| 439 |
n = unsafe.Sizeof(uname.Version) |
| 440 |
if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil { |
| 441 |
return err |
| 442 |
} |
| 443 |
|
| 444 |
// The version might have newlines or tabs in it, convert them to |
| 445 |
// spaces. |
| 446 |
for i, b := range uname.Version { |
| 447 |
if b == '\n' || b == '\t' { |
| 448 |
if i == len(uname.Version)-1 { |
| 449 |
uname.Version[i] = 0 |
| 450 |
} else { |
| 451 |
uname.Version[i] = ' ' |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
mib = []_C_int{CTL_HW, HW_MACHINE} |
| 457 |
n = unsafe.Sizeof(uname.Machine) |
| 458 |
if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil { |
| 459 |
return err |
| 460 |
} |
| 461 |
|
| 462 |
return nil |
| 463 |
} |
| 464 |
|
| 465 |
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { |
| 466 |
if raceenabled { |
| 467 |
raceReleaseMerge(unsafe.Pointer(&ioSync)) |
| 468 |
} |
| 469 |
var length = int64(count) |
| 470 |
err = sendfile(infd, outfd, *offset, &length, nil, 0) |
| 471 |
written = int(length) |
| 472 |
return |
| 473 |
} |
| 474 |
|
| 475 |
func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) { |
| 476 |
var value IPMreqn |
| 477 |
vallen := _Socklen(SizeofIPMreqn) |
| 478 |
errno := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) |
| 479 |
return &value, errno |
| 480 |
} |
| 481 |
|
| 482 |
func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) { |
| 483 |
return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq)) |
| 484 |
} |
| 485 |
|
| 486 |
// GetsockoptXucred is a getsockopt wrapper that returns an Xucred struct. |
| 487 |
// The usual level and opt are SOL_LOCAL and LOCAL_PEERCRED, respectively. |
| 488 |
func GetsockoptXucred(fd, level, opt int) (*Xucred, error) { |
| 489 |
x := new(Xucred) |
| 490 |
vallen := _Socklen(SizeofXucred) |
| 491 |
err := getsockopt(fd, level, opt, unsafe.Pointer(x), &vallen) |
| 492 |
return x, err |
| 493 |
} |
| 494 |
|
| 495 |
func GetsockoptTCPConnectionInfo(fd, level, opt int) (*TCPConnectionInfo, error) { |
| 496 |
var value TCPConnectionInfo |
| 497 |
vallen := _Socklen(SizeofTCPConnectionInfo) |
| 498 |
err := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) |
| 499 |
return &value, err |
| 500 |
} |
| 501 |
|
| 502 |
func SysctlKinfoProc(name string, args ...int) (*KinfoProc, error) { |
| 503 |
mib, err := sysctlmib(name, args...) |
| 504 |
if err != nil { |
| 505 |
return nil, err |
| 506 |
} |
| 507 |
|
| 508 |
var kinfo KinfoProc |
| 509 |
n := uintptr(SizeofKinfoProc) |
| 510 |
if err := sysctl(mib, (*byte)(unsafe.Pointer(&kinfo)), &n, nil, 0); err != nil { |
| 511 |
return nil, err |
| 512 |
} |
| 513 |
if n != SizeofKinfoProc { |
| 514 |
return nil, EIO |
| 515 |
} |
| 516 |
return &kinfo, nil |
| 517 |
} |
| 518 |
|
| 519 |
func SysctlKinfoProcSlice(name string, args ...int) ([]KinfoProc, error) { |
| 520 |
mib, err := sysctlmib(name, args...) |
| 521 |
if err != nil { |
| 522 |
return nil, err |
| 523 |
} |
| 524 |
|
| 525 |
for { |
| 526 |
// Find size. |
| 527 |
n := uintptr(0) |
| 528 |
if err := sysctl(mib, nil, &n, nil, 0); err != nil { |
| 529 |
return nil, err |
| 530 |
} |
| 531 |
if n == 0 { |
| 532 |
return nil, nil |
| 533 |
} |
| 534 |
if n%SizeofKinfoProc != 0 { |
| 535 |
return nil, fmt.Errorf("sysctl() returned a size of %d, which is not a multiple of %d", n, SizeofKinfoProc) |
| 536 |
} |
| 537 |
|
| 538 |
// Read into buffer of that size. |
| 539 |
buf := make([]KinfoProc, n/SizeofKinfoProc) |
| 540 |
if err := sysctl(mib, (*byte)(unsafe.Pointer(&buf[0])), &n, nil, 0); err != nil { |
| 541 |
if err == ENOMEM { |
| 542 |
// Process table grew. Try again. |
| 543 |
continue |
| 544 |
} |
| 545 |
return nil, err |
| 546 |
} |
| 547 |
if n%SizeofKinfoProc != 0 { |
| 548 |
return nil, fmt.Errorf("sysctl() returned a size of %d, which is not a multiple of %d", n, SizeofKinfoProc) |
| 549 |
} |
| 550 |
|
| 551 |
// The actual call may return less than the original reported required |
| 552 |
// size so ensure we deal with that. |
| 553 |
return buf[:n/SizeofKinfoProc], nil |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
//sys pthread_chdir_np(path string) (err error) |
| 558 |
|
| 559 |
func PthreadChdir(path string) (err error) { |
| 560 |
return pthread_chdir_np(path) |
| 561 |
} |
| 562 |
|
| 563 |
//sys pthread_fchdir_np(fd int) (err error) |
| 564 |
|
| 565 |
func PthreadFchdir(fd int) (err error) { |
| 566 |
return pthread_fchdir_np(fd) |
| 567 |
} |
| 568 |
|
| 569 |
// Connectx calls connectx(2) to initiate a connection on a socket. |
| 570 |
// |
| 571 |
// srcIf, srcAddr, and dstAddr are filled into a [SaEndpoints] struct and passed as the endpoints argument. |
| 572 |
// |
| 573 |
// - srcIf is the optional source interface index. 0 means unspecified. |
| 574 |
// - srcAddr is the optional source address. nil means unspecified. |
| 575 |
// - dstAddr is the destination address. |
| 576 |
// |
| 577 |
// On success, Connectx returns the number of bytes enqueued for transmission. |
| 578 |
func Connectx(fd int, srcIf uint32, srcAddr, dstAddr Sockaddr, associd SaeAssocID, flags uint32, iov []Iovec, connid *SaeConnID) (n uintptr, err error) { |
| 579 |
endpoints := SaEndpoints{ |
| 580 |
Srcif: srcIf, |
| 581 |
} |
| 582 |
|
| 583 |
if srcAddr != nil { |
| 584 |
addrp, addrlen, err := srcAddr.sockaddr() |
| 585 |
if err != nil { |
| 586 |
return 0, err |
| 587 |
} |
| 588 |
endpoints.Srcaddr = (*RawSockaddr)(addrp) |
| 589 |
endpoints.Srcaddrlen = uint32(addrlen) |
| 590 |
} |
| 591 |
|
| 592 |
if dstAddr != nil { |
| 593 |
addrp, addrlen, err := dstAddr.sockaddr() |
| 594 |
if err != nil { |
| 595 |
return 0, err |
| 596 |
} |
| 597 |
endpoints.Dstaddr = (*RawSockaddr)(addrp) |
| 598 |
endpoints.Dstaddrlen = uint32(addrlen) |
| 599 |
} |
| 600 |
|
| 601 |
err = connectx(fd, &endpoints, associd, flags, iov, &n, connid) |
| 602 |
return |
| 603 |
} |
| 604 |
|
| 605 |
//sys connectx(fd int, endpoints *SaEndpoints, associd SaeAssocID, flags uint32, iov []Iovec, n *uintptr, connid *SaeConnID) (err error) |
| 606 |
//sys sendfile(infd int, outfd int, offset int64, len *int64, hdtr unsafe.Pointer, flags int) (err error) |
| 607 |
|
| 608 |
//sys shmat(id int, addr uintptr, flag int) (ret uintptr, err error) |
| 609 |
//sys shmctl(id int, cmd int, buf *SysvShmDesc) (result int, err error) |
| 610 |
//sys shmdt(addr uintptr) (err error) |
| 611 |
//sys shmget(key int, size int, flag int) (id int, err error) |
| 612 |
|
| 613 |
/* |
| 614 |
* Exposed directly |
| 615 |
*/ |
| 616 |
//sys Access(path string, mode uint32) (err error) |
| 617 |
//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error) |
| 618 |
//sys Chdir(path string) (err error) |
| 619 |
//sys Chflags(path string, flags int) (err error) |
| 620 |
//sys Chmod(path string, mode uint32) (err error) |
| 621 |
//sys Chown(path string, uid int, gid int) (err error) |
| 622 |
//sys Chroot(path string) (err error) |
| 623 |
//sys ClockGettime(clockid int32, time *Timespec) (err error) |
| 624 |
//sys Close(fd int) (err error) |
| 625 |
//sys Clonefile(src string, dst string, flags int) (err error) |
| 626 |
//sys Clonefileat(srcDirfd int, src string, dstDirfd int, dst string, flags int) (err error) |
| 627 |
//sys Dup(fd int) (nfd int, err error) |
| 628 |
//sys Dup2(from int, to int) (err error) |
| 629 |
//sys Exchangedata(path1 string, path2 string, options int) (err error) |
| 630 |
//sys Exit(code int) |
| 631 |
//sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error) |
| 632 |
//sys Fchdir(fd int) (err error) |
| 633 |
//sys Fchflags(fd int, flags int) (err error) |
| 634 |
//sys Fchmod(fd int, mode uint32) (err error) |
| 635 |
//sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) |
| 636 |
//sys Fchown(fd int, uid int, gid int) (err error) |
| 637 |
//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) |
| 638 |
//sys Fclonefileat(srcDirfd int, dstDirfd int, dst string, flags int) (err error) |
| 639 |
//sys Flock(fd int, how int) (err error) |
| 640 |
//sys Fpathconf(fd int, name int) (val int, err error) |
| 641 |
//sys Fsync(fd int) (err error) |
| 642 |
//sys Ftruncate(fd int, length int64) (err error) |
| 643 |
//sys Getcwd(buf []byte) (n int, err error) |
| 644 |
//sys Getdtablesize() (size int) |
| 645 |
//sysnb Getegid() (egid int) |
| 646 |
//sysnb Geteuid() (uid int) |
| 647 |
//sysnb Getgid() (gid int) |
| 648 |
//sysnb Getpgid(pid int) (pgid int, err error) |
| 649 |
//sysnb Getpgrp() (pgrp int) |
| 650 |
//sysnb Getpid() (pid int) |
| 651 |
//sysnb Getppid() (ppid int) |
| 652 |
//sys Getpriority(which int, who int) (prio int, err error) |
| 653 |
//sysnb Getrlimit(which int, lim *Rlimit) (err error) |
| 654 |
//sysnb Getrusage(who int, rusage *Rusage) (err error) |
| 655 |
//sysnb Getsid(pid int) (sid int, err error) |
| 656 |
//sysnb Gettimeofday(tp *Timeval) (err error) |
| 657 |
//sysnb Getuid() (uid int) |
| 658 |
//sysnb Issetugid() (tainted bool) |
| 659 |
//sys Kqueue() (fd int, err error) |
| 660 |
//sys Lchown(path string, uid int, gid int) (err error) |
| 661 |
//sys Link(path string, link string) (err error) |
| 662 |
//sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error) |
| 663 |
//sys Listen(s int, backlog int) (err error) |
| 664 |
//sys Mkdir(path string, mode uint32) (err error) |
| 665 |
//sys Mkdirat(dirfd int, path string, mode uint32) (err error) |
| 666 |
//sys Mkfifo(path string, mode uint32) (err error) |
| 667 |
//sys Mknod(path string, mode uint32, dev int) (err error) |
| 668 |
//sys Mount(fsType string, dir string, flags int, data unsafe.Pointer) (err error) |
| 669 |
//sys Open(path string, mode int, perm uint32) (fd int, err error) |
| 670 |
//sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) |
| 671 |
//sys Pathconf(path string, name int) (val int, err error) |
| 672 |
//sys pread(fd int, p []byte, offset int64) (n int, err error) |
| 673 |
//sys pwrite(fd int, p []byte, offset int64) (n int, err error) |
| 674 |
//sys read(fd int, p []byte) (n int, err error) |
| 675 |
//sys Readlink(path string, buf []byte) (n int, err error) |
| 676 |
//sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error) |
| 677 |
//sys Rename(from string, to string) (err error) |
| 678 |
//sys Renameat(fromfd int, from string, tofd int, to string) (err error) |
| 679 |
//sys Revoke(path string) (err error) |
| 680 |
//sys Rmdir(path string) (err error) |
| 681 |
//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK |
| 682 |
//sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) |
| 683 |
//sys Setattrlist(path string, attrlist *Attrlist, attrBuf []byte, options int) (err error) |
| 684 |
//sys Setegid(egid int) (err error) |
| 685 |
//sysnb Seteuid(euid int) (err error) |
| 686 |
//sysnb Setgid(gid int) (err error) |
| 687 |
//sys Setlogin(name string) (err error) |
| 688 |
//sysnb Setpgid(pid int, pgid int) (err error) |
| 689 |
//sys Setpriority(which int, who int, prio int) (err error) |
| 690 |
//sys Setprivexec(flag int) (err error) |
| 691 |
//sysnb Setregid(rgid int, egid int) (err error) |
| 692 |
//sysnb Setreuid(ruid int, euid int) (err error) |
| 693 |
//sysnb Setsid() (pid int, err error) |
| 694 |
//sysnb Settimeofday(tp *Timeval) (err error) |
| 695 |
//sysnb Setuid(uid int) (err error) |
| 696 |
//sys Symlink(path string, link string) (err error) |
| 697 |
//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) |
| 698 |
//sys Sync() (err error) |
| 699 |
//sys Truncate(path string, length int64) (err error) |
| 700 |
//sys Umask(newmask int) (oldmask int) |
| 701 |
//sys Undelete(path string) (err error) |
| 702 |
//sys Unlink(path string) (err error) |
| 703 |
//sys Unlinkat(dirfd int, path string, flags int) (err error) |
| 704 |
//sys Unmount(path string, flags int) (err error) |
| 705 |
//sys write(fd int, p []byte) (n int, err error) |
| 706 |
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) |
| 707 |
//sys munmap(addr uintptr, length uintptr) (err error) |
| 708 |
//sys readv(fd int, iovecs []Iovec) (n int, err error) |
| 709 |
//sys preadv(fd int, iovecs []Iovec, offset int64) (n int, err error) |
| 710 |
//sys writev(fd int, iovecs []Iovec) (n int, err error) |
| 711 |
//sys pwritev(fd int, iovecs []Iovec, offset int64) (n int, err error) |