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.
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.
21 const ImplementsGetwd = true
23 func Getwd() (string, error) {
24 buf := make([]byte, 2048)
25 attrs, err := getAttrList(".", attrList{CommonAttr: attrCmnFullpath}, buf, 0)
26 if err == nil && len(attrs) == 1 && len(attrs[0]) >= 2 {
27 wd := string(attrs[0])
28 // Sanity check that it's an absolute path and ends
29 // in a null byte, which we then strip.
30 if wd[0] == '/' && wd[len(wd)-1] == 0 {
31 return wd[:len(wd)-1], nil
34 // If pkg/os/getwd.go gets ENOTSUP, it will fall back to the
39 // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
40 type SockaddrDatalink struct {
49 raw RawSockaddrDatalink
52 // Translate "kern.hostname" to []_C_int{0,1,2,3}.
53 func nametomib(name string) (mib []_C_int, err error) {
54 const siz = unsafe.Sizeof(mib[0])
56 // NOTE(rsc): It seems strange to set the buffer to have
57 // size CTL_MAXNAME+2 but use only CTL_MAXNAME
58 // as the size. I don't know why the +2 is here, but the
59 // kernel uses +2 for its own implementation of this function.
60 // I am scared that if we don't include the +2 here, the kernel
61 // will silently write 2 words farther than we specify
62 // and we'll get memory corruption.
63 var buf [CTL_MAXNAME + 2]_C_int
64 n := uintptr(CTL_MAXNAME) * siz
66 p := (*byte)(unsafe.Pointer(&buf[0]))
67 bytes, err := ByteSliceFromString(name)
72 // Magic sysctl: "setting" 0.3 to a string name
73 // lets you read back the array of integers form.
74 if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
77 return buf[0 : n/siz], nil
80 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
81 func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
82 func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
86 attrCmnFullpath = 0x08000000
89 type attrList struct {
99 func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
100 if len(attrBuf) < 4 {
101 return nil, errors.New("attrBuf too small")
103 attrList.bitmapCount = attrBitMapCount
106 _p0, err = BytePtrFromString(path)
111 if err := getattrlist(_p0, unsafe.Pointer(&attrList), unsafe.Pointer(&attrBuf[0]), uintptr(len(attrBuf)), int(options)); err != nil {
114 size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
116 // dat is the section of attrBuf that contains valid data,
117 // without the 4 byte length header. All attribute offsets
118 // are relative to dat.
120 if int(size) < len(attrBuf) {
123 dat = dat[4:] // remove length prefix
125 for i := uint32(0); int(i) < len(dat); {
128 return attrs, errors.New("truncated attribute header")
130 datOff := *(*int32)(unsafe.Pointer(&header[0]))
131 attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
132 if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
133 return attrs, errors.New("truncated results; attrBuf too small")
135 end := uint32(datOff) + attrLen
136 attrs = append(attrs, dat[datOff:end])
138 if r := i % 4; r != 0 {
145 //sys getattrlist(path *byte, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error)
147 func SysctlClockinfo(name string) (*Clockinfo, error) {
148 mib, err := sysctlmib(name)
153 n := uintptr(SizeofClockinfo)
155 if err := sysctl(mib, (*byte)(unsafe.Pointer(&ci)), &n, nil, 0); err != nil {
158 if n != SizeofClockinfo {
164 //sysnb pipe() (r int, w int, err error)
166 func Pipe(p []int) (err error) {
170 p[0], p[1], err = pipe()
174 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
175 var _p0 unsafe.Pointer
178 _p0 = unsafe.Pointer(&buf[0])
179 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
181 return getfsstat(_p0, bufsize, flags)
184 func xattrPointer(dest []byte) *byte {
185 // It's only when dest is set to NULL that the OS X implementations of
186 // getxattr() and listxattr() return the current sizes of the named attributes.
187 // An empty byte array is not sufficient. To maintain the same behaviour as the
188 // linux implementation, we wrap around the system calls and pass in NULL when
197 //sys getxattr(path string, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
199 func Getxattr(path string, attr string, dest []byte) (sz int, err error) {
200 return getxattr(path, attr, xattrPointer(dest), len(dest), 0, 0)
203 func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) {
204 return getxattr(link, attr, xattrPointer(dest), len(dest), 0, XATTR_NOFOLLOW)
207 //sys fgetxattr(fd int, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
209 func Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) {
210 return fgetxattr(fd, attr, xattrPointer(dest), len(dest), 0, 0)
213 //sys setxattr(path string, attr string, data *byte, size int, position uint32, options int) (err error)
215 func Setxattr(path string, attr string, data []byte, flags int) (err error) {
216 // The parameters for the OS X implementation vary slightly compared to the
217 // linux system call, specifically the position parameter:
223 // const void *value,
234 // u_int32_t position,
238 // position specifies the offset within the extended attribute. In the
239 // current implementation, only the resource fork extended attribute makes
240 // use of this argument. For all others, position is reserved. We simply
241 // default to setting it to zero.
242 return setxattr(path, attr, xattrPointer(data), len(data), 0, flags)
245 func Lsetxattr(link string, attr string, data []byte, flags int) (err error) {
246 return setxattr(link, attr, xattrPointer(data), len(data), 0, flags|XATTR_NOFOLLOW)
249 //sys fsetxattr(fd int, attr string, data *byte, size int, position uint32, options int) (err error)
251 func Fsetxattr(fd int, attr string, data []byte, flags int) (err error) {
252 return fsetxattr(fd, attr, xattrPointer(data), len(data), 0, 0)
255 //sys removexattr(path string, attr string, options int) (err error)
257 func Removexattr(path string, attr string) (err error) {
258 // We wrap around and explicitly zero out the options provided to the OS X
259 // implementation of removexattr, we do so for interoperability with the
261 return removexattr(path, attr, 0)
264 func Lremovexattr(link string, attr string) (err error) {
265 return removexattr(link, attr, XATTR_NOFOLLOW)
268 //sys fremovexattr(fd int, attr string, options int) (err error)
270 func Fremovexattr(fd int, attr string) (err error) {
271 return fremovexattr(fd, attr, 0)
274 //sys listxattr(path string, dest *byte, size int, options int) (sz int, err error)
276 func Listxattr(path string, dest []byte) (sz int, err error) {
277 return listxattr(path, xattrPointer(dest), len(dest), 0)
280 func Llistxattr(link string, dest []byte) (sz int, err error) {
281 return listxattr(link, xattrPointer(dest), len(dest), XATTR_NOFOLLOW)
284 //sys flistxattr(fd int, dest *byte, size int, options int) (sz int, err error)
286 func Flistxattr(fd int, dest []byte) (sz int, err error) {
287 return flistxattr(fd, xattrPointer(dest), len(dest), 0)
290 func setattrlistTimes(path string, times []Timespec, flags int) error {
291 _p0, err := BytePtrFromString(path)
296 var attrList attrList
297 attrList.bitmapCount = ATTR_BIT_MAP_COUNT
298 attrList.CommonAttr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME
300 // order is mtime, atime: the opposite of Chtimes
301 attributes := [2]Timespec{times[1], times[0]}
303 if flags&AT_SYMLINK_NOFOLLOW != 0 {
304 options |= FSOPT_NOFOLLOW
308 unsafe.Pointer(&attrList),
309 unsafe.Pointer(&attributes),
310 unsafe.Sizeof(attributes),
314 //sys setattrlist(path *byte, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error)
316 func utimensat(dirfd int, path string, times *[2]Timespec, flags int) error {
317 // Darwin doesn't support SYS_UTIMENSAT
325 //sys kill(pid int, signum int, posix int) (err error)
327 func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(signum), 1) }
329 //sys ioctl(fd int, req uint, arg uintptr) (err error)
331 // ioctl itself should not be exposed directly, but additional get/set
332 // functions for specific types are permissible.
334 // IoctlSetInt performs an ioctl operation which sets an integer value
335 // on fd, using the specified request number.
336 func IoctlSetInt(fd int, req uint, value int) error {
337 return ioctl(fd, req, uintptr(value))
340 func ioctlSetWinsize(fd int, req uint, value *Winsize) error {
341 return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
344 func ioctlSetTermios(fd int, req uint, value *Termios) error {
345 return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
348 // IoctlGetInt performs an ioctl operation which gets an integer value
349 // from fd, using the specified request number.
350 func IoctlGetInt(fd int, req uint) (int, error) {
352 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
356 func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
358 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
362 func IoctlGetTermios(fd int, req uint) (*Termios, error) {
364 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
368 func Uname(uname *Utsname) error {
369 mib := []_C_int{CTL_KERN, KERN_OSTYPE}
370 n := unsafe.Sizeof(uname.Sysname)
371 if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
375 mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
376 n = unsafe.Sizeof(uname.Nodename)
377 if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
381 mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
382 n = unsafe.Sizeof(uname.Release)
383 if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
387 mib = []_C_int{CTL_KERN, KERN_VERSION}
388 n = unsafe.Sizeof(uname.Version)
389 if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
393 // The version might have newlines or tabs in it, convert them to
395 for i, b := range uname.Version {
396 if b == '\n' || b == '\t' {
397 if i == len(uname.Version)-1 {
400 uname.Version[i] = ' '
405 mib = []_C_int{CTL_HW, HW_MACHINE}
406 n = unsafe.Sizeof(uname.Machine)
407 if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
414 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
416 raceReleaseMerge(unsafe.Pointer(&ioSync))
418 var length = int64(count)
419 err = sendfile(infd, outfd, *offset, &length, nil, 0)
420 written = int(length)
424 //sys sendfile(infd int, outfd int, offset int64, len *int64, hdtr unsafe.Pointer, flags int) (err error)
429 //sys Access(path string, mode uint32) (err error)
430 //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
431 //sys Chdir(path string) (err error)
432 //sys Chflags(path string, flags int) (err error)
433 //sys Chmod(path string, mode uint32) (err error)
434 //sys Chown(path string, uid int, gid int) (err error)
435 //sys Chroot(path string) (err error)
436 //sys ClockGettime(clockid int32, time *Timespec) (err error)
437 //sys Close(fd int) (err error)
438 //sys Dup(fd int) (nfd int, err error)
439 //sys Dup2(from int, to int) (err error)
440 //sys Exchangedata(path1 string, path2 string, options int) (err error)
442 //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
443 //sys Fchdir(fd int) (err error)
444 //sys Fchflags(fd int, flags int) (err error)
445 //sys Fchmod(fd int, mode uint32) (err error)
446 //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
447 //sys Fchown(fd int, uid int, gid int) (err error)
448 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
449 //sys Flock(fd int, how int) (err error)
450 //sys Fpathconf(fd int, name int) (val int, err error)
451 //sys Fsync(fd int) (err error)
452 //sys Ftruncate(fd int, length int64) (err error)
453 //sys Getdtablesize() (size int)
454 //sysnb Getegid() (egid int)
455 //sysnb Geteuid() (uid int)
456 //sysnb Getgid() (gid int)
457 //sysnb Getpgid(pid int) (pgid int, err error)
458 //sysnb Getpgrp() (pgrp int)
459 //sysnb Getpid() (pid int)
460 //sysnb Getppid() (ppid int)
461 //sys Getpriority(which int, who int) (prio int, err error)
462 //sysnb Getrlimit(which int, lim *Rlimit) (err error)
463 //sysnb Getrusage(who int, rusage *Rusage) (err error)
464 //sysnb Getsid(pid int) (sid int, err error)
465 //sysnb Getuid() (uid int)
466 //sysnb Issetugid() (tainted bool)
467 //sys Kqueue() (fd int, err error)
468 //sys Lchown(path string, uid int, gid int) (err error)
469 //sys Link(path string, link string) (err error)
470 //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
471 //sys Listen(s int, backlog int) (err error)
472 //sys Mkdir(path string, mode uint32) (err error)
473 //sys Mkdirat(dirfd int, path string, mode uint32) (err error)
474 //sys Mkfifo(path string, mode uint32) (err error)
475 //sys Mknod(path string, mode uint32, dev int) (err error)
476 //sys Open(path string, mode int, perm uint32) (fd int, err error)
477 //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
478 //sys Pathconf(path string, name int) (val int, err error)
479 //sys Pread(fd int, p []byte, offset int64) (n int, err error)
480 //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
481 //sys read(fd int, p []byte) (n int, err error)
482 //sys Readlink(path string, buf []byte) (n int, err error)
483 //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
484 //sys Rename(from string, to string) (err error)
485 //sys Renameat(fromfd int, from string, tofd int, to string) (err error)
486 //sys Revoke(path string) (err error)
487 //sys Rmdir(path string) (err error)
488 //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
489 //sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
490 //sys Setegid(egid int) (err error)
491 //sysnb Seteuid(euid int) (err error)
492 //sysnb Setgid(gid int) (err error)
493 //sys Setlogin(name string) (err error)
494 //sysnb Setpgid(pid int, pgid int) (err error)
495 //sys Setpriority(which int, who int, prio int) (err error)
496 //sys Setprivexec(flag int) (err error)
497 //sysnb Setregid(rgid int, egid int) (err error)
498 //sysnb Setreuid(ruid int, euid int) (err error)
499 //sysnb Setrlimit(which int, lim *Rlimit) (err error)
500 //sysnb Setsid() (pid int, err error)
501 //sysnb Settimeofday(tp *Timeval) (err error)
502 //sysnb Setuid(uid int) (err error)
503 //sys Symlink(path string, link string) (err error)
504 //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
505 //sys Sync() (err error)
506 //sys Truncate(path string, length int64) (err error)
507 //sys Umask(newmask int) (oldmask int)
508 //sys Undelete(path string) (err error)
509 //sys Unlink(path string) (err error)
510 //sys Unlinkat(dirfd int, path string, flags int) (err error)
511 //sys Unmount(path string, flags int) (err error)
512 //sys write(fd int, p []byte) (n int, err error)
513 //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
514 //sys munmap(addr uintptr, length uintptr) (err error)
515 //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
516 //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
551 // Kqueue_from_portset_np
610 // Shared_region_check_np
611 // Shared_region_map_np
612 // __pthread_mutex_destroy
613 // __pthread_mutex_init
614 // __pthread_mutex_lock
615 // __pthread_mutex_trylock
616 // __pthread_mutex_unlock
617 // __pthread_cond_init
618 // __pthread_cond_destroy
619 // __pthread_cond_broadcast
620 // __pthread_cond_signal
622 // __pthread_cond_timedwait
631 // __pthread_cond_wait
636 // __disable_threadsignal
637 // __pthread_markcancel
638 // __pthread_canceled
657 // Bsdthread_terminate
659 // Bsdthread_register
690 // Sigsuspend_nocancel
701 // Aio_suspend_nocancel
702 // __sigwait_nocancel
703 // __semwait_signal_nocancel