Source file src/syscall/syscall_darwin.go

     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 syscall
    14  
    15  import (
    16  	"internal/abi"
    17  	"unsafe"
    18  )
    19  
    20  type SockaddrDatalink struct {
    21  	Len    uint8
    22  	Family uint8
    23  	Index  uint16
    24  	Type   uint8
    25  	Nlen   uint8
    26  	Alen   uint8
    27  	Slen   uint8
    28  	Data   [12]int8
    29  	raw    RawSockaddrDatalink
    30  }
    31  
    32  // Translate "kern.hostname" to []_C_int{0,1,2,3}.
    33  func nametomib(name string) (mib []_C_int, err error) {
    34  	const siz = unsafe.Sizeof(mib[0])
    35  
    36  	// NOTE(rsc): It seems strange to set the buffer to have
    37  	// size CTL_MAXNAME+2 but use only CTL_MAXNAME
    38  	// as the size. I don't know why the +2 is here, but the
    39  	// kernel uses +2 for its own implementation of this function.
    40  	// I am scared that if we don't include the +2 here, the kernel
    41  	// will silently write 2 words farther than we specify
    42  	// and we'll get memory corruption.
    43  	var buf [CTL_MAXNAME + 2]_C_int
    44  	n := uintptr(CTL_MAXNAME) * siz
    45  
    46  	p := (*byte)(unsafe.Pointer(&buf[0]))
    47  	bytes, err := ByteSliceFromString(name)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// Magic sysctl: "setting" 0.3 to a string name
    53  	// lets you read back the array of integers form.
    54  	if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
    55  		return nil, err
    56  	}
    57  	return buf[0 : n/siz], nil
    58  }
    59  
    60  func direntIno(buf []byte) (uint64, bool) {
    61  	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
    62  }
    63  
    64  func direntReclen(buf []byte) (uint64, bool) {
    65  	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
    66  }
    67  
    68  func direntNamlen(buf []byte) (uint64, bool) {
    69  	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
    70  }
    71  
    72  func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
    73  func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
    74  
    75  const (
    76  	attrBitMapCount = 5
    77  	attrCmnModtime  = 0x00000400
    78  	attrCmnAcctime  = 0x00001000
    79  )
    80  
    81  type attrList struct {
    82  	bitmapCount uint16
    83  	_           uint16
    84  	CommonAttr  uint32
    85  	VolAttr     uint32
    86  	DirAttr     uint32
    87  	FileAttr    uint32
    88  	Forkattr    uint32
    89  }
    90  
    91  //sysnb pipe(p *[2]int32) (err error)
    92  
    93  func Pipe(p []int) (err error) {
    94  	if len(p) != 2 {
    95  		return EINVAL
    96  	}
    97  	var q [2]int32
    98  	err = pipe(&q)
    99  	p[0] = int(q[0])
   100  	p[1] = int(q[1])
   101  	return
   102  }
   103  
   104  func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
   105  	var _p0 unsafe.Pointer
   106  	var bufsize uintptr
   107  	if len(buf) > 0 {
   108  		_p0 = unsafe.Pointer(&buf[0])
   109  		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
   110  	}
   111  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_getfsstat_trampoline), uintptr(_p0), bufsize, uintptr(flags))
   112  	n = int(r0)
   113  	if e1 != 0 {
   114  		err = e1
   115  	}
   116  	return
   117  }
   118  
   119  func libc_getfsstat_trampoline()
   120  
   121  //go:cgo_import_dynamic libc_getfsstat getfsstat "/usr/lib/libSystem.B.dylib"
   122  
   123  func setattrlistTimes(path string, times []Timespec) error {
   124  	_p0, err := BytePtrFromString(path)
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	var attrList attrList
   130  	attrList.bitmapCount = attrBitMapCount
   131  	attrList.CommonAttr = attrCmnModtime | attrCmnAcctime
   132  
   133  	// order is mtime, atime: the opposite of Chtimes
   134  	attributes := [2]Timespec{times[1], times[0]}
   135  	const options = 0
   136  	_, _, e1 := syscall6(
   137  		abi.FuncPCABI0(libc_setattrlist_trampoline),
   138  		uintptr(unsafe.Pointer(_p0)),
   139  		uintptr(unsafe.Pointer(&attrList)),
   140  		uintptr(unsafe.Pointer(&attributes)),
   141  		uintptr(unsafe.Sizeof(attributes)),
   142  		uintptr(options),
   143  		0,
   144  	)
   145  	if e1 != 0 {
   146  		return e1
   147  	}
   148  	return nil
   149  }
   150  
   151  func libc_setattrlist_trampoline()
   152  
   153  //go:cgo_import_dynamic libc_setattrlist setattrlist "/usr/lib/libSystem.B.dylib"
   154  
   155  func utimensat(dirfd int, path string, times *[2]Timespec, flag int) error {
   156  	// Darwin doesn't support SYS_UTIMENSAT
   157  	return ENOSYS
   158  }
   159  
   160  /*
   161   * Wrapped
   162   */
   163  
   164  //sys	kill(pid int, signum int, posix int) (err error)
   165  
   166  func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
   167  
   168  /*
   169   * Exposed directly
   170   */
   171  //sys	Access(path string, mode uint32) (err error)
   172  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   173  //sys	Chdir(path string) (err error)
   174  //sys	Chflags(path string, flags int) (err error)
   175  //sys	Chmod(path string, mode uint32) (err error)
   176  //sys	Chown(path string, uid int, gid int) (err error)
   177  //sys	Chroot(path string) (err error)
   178  //sys	Close(fd int) (err error)
   179  //sys	closedir(dir uintptr) (err error)
   180  //sys	Dup(fd int) (nfd int, err error)
   181  //sys	Dup2(from int, to int) (err error)
   182  //sys	Exchangedata(path1 string, path2 string, options int) (err error)
   183  //sys	Fchdir(fd int) (err error)
   184  //sys	Fchflags(fd int, flags int) (err error)
   185  //sys	Fchmod(fd int, mode uint32) (err error)
   186  //sys	Fchown(fd int, uid int, gid int) (err error)
   187  //sys	Flock(fd int, how int) (err error)
   188  //sys	Fpathconf(fd int, name int) (val int, err error)
   189  //sys	Fsync(fd int) (err error)
   190  //  Fsync is not called for os.File.Sync(). Please see internal/poll/fd_fsync_darwin.go
   191  //sys	Ftruncate(fd int, length int64) (err error)
   192  //sys	Getdtablesize() (size int)
   193  //sysnb	Getegid() (egid int)
   194  //sysnb	Geteuid() (uid int)
   195  //sysnb	Getgid() (gid int)
   196  //sysnb	Getpgid(pid int) (pgid int, err error)
   197  //sysnb	Getpgrp() (pgrp int)
   198  //sysnb	Getpid() (pid int)
   199  //sysnb	Getppid() (ppid int)
   200  //sys	Getpriority(which int, who int) (prio int, err error)
   201  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   202  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   203  //sysnb	Getsid(pid int) (sid int, err error)
   204  //sysnb	Getuid() (uid int)
   205  //sysnb	Issetugid() (tainted bool)
   206  //sys	Kqueue() (fd int, err error)
   207  //sys	Lchown(path string, uid int, gid int) (err error)
   208  //sys	Link(path string, link string) (err error)
   209  //sys	Listen(s int, backlog int) (err error)
   210  //sys	Mkdir(path string, mode uint32) (err error)
   211  //sys	Mkfifo(path string, mode uint32) (err error)
   212  //sys	Mknod(path string, mode uint32, dev int) (err error)
   213  //sys	Mlock(b []byte) (err error)
   214  //sys	Mlockall(flags int) (err error)
   215  //sys	Mprotect(b []byte, prot int) (err error)
   216  //sys	Munlock(b []byte) (err error)
   217  //sys	Munlockall() (err error)
   218  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   219  //sys	Pathconf(path string, name int) (val int, err error)
   220  //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
   221  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
   222  //sys	read(fd int, p []byte) (n int, err error)
   223  //sys	readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
   224  //sys	Readlink(path string, buf []byte) (n int, err error)
   225  //sys	Rename(from string, to string) (err error)
   226  //sys	Revoke(path string) (err error)
   227  //sys	Rmdir(path string) (err error)
   228  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_lseek
   229  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   230  //sys	Setegid(egid int) (err error)
   231  //sysnb	Seteuid(euid int) (err error)
   232  //sysnb	Setgid(gid int) (err error)
   233  //sys	Setlogin(name string) (err error)
   234  //sysnb	Setpgid(pid int, pgid int) (err error)
   235  //sys	Setpriority(which int, who int, prio int) (err error)
   236  //sys	Setprivexec(flag int) (err error)
   237  //sysnb	Setregid(rgid int, egid int) (err error)
   238  //sysnb	Setreuid(ruid int, euid int) (err error)
   239  //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
   240  //sysnb	Setsid() (pid int, err error)
   241  //sysnb	Settimeofday(tp *Timeval) (err error)
   242  //sysnb	Setuid(uid int) (err error)
   243  //sys	Symlink(path string, link string) (err error)
   244  //sys	Sync() (err error)
   245  //sys	Truncate(path string, length int64) (err error)
   246  //sys	Umask(newmask int) (oldmask int)
   247  //sys	Undelete(path string) (err error)
   248  //sys	Unlink(path string) (err error)
   249  //sys	Unmount(path string, flags int) (err error)
   250  //sys	write(fd int, p []byte) (n int, err error)
   251  //sys	writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
   252  //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   253  //sys   munmap(addr uintptr, length uintptr) (err error)
   254  //sysnb fork() (pid int, err error)
   255  //sysnb ioctl(fd int, req int, arg int) (err error)
   256  //sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
   257  //sysnb execve(path *byte, argv **byte, envp **byte) (err error)
   258  //sysnb exit(res int) (err error)
   259  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
   260  //sys	fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
   261  //sys   unlinkat(fd int, path string, flags int) (err error)
   262  //sys   openat(fd int, path string, flags int, perm uint32) (fdret int, err error)
   263  //sys	getcwd(buf []byte) (n int, err error)
   264  
   265  func init() {
   266  	execveDarwin = execve
   267  }
   268  
   269  func fdopendir(fd int) (dir uintptr, err error) {
   270  	r0, _, e1 := syscallPtr(abi.FuncPCABI0(libc_fdopendir_trampoline), uintptr(fd), 0, 0)
   271  	dir = uintptr(r0)
   272  	if e1 != 0 {
   273  		err = errnoErr(e1)
   274  	}
   275  	return
   276  }
   277  
   278  func libc_fdopendir_trampoline()
   279  
   280  //go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"
   281  
   282  func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
   283  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_read_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
   284  	n = int(r0)
   285  	if e1 != 0 {
   286  		err = errnoErr(e1)
   287  	}
   288  	return
   289  }
   290  
   291  func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
   292  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
   293  	n = int(r0)
   294  	if e1 != 0 {
   295  		err = errnoErr(e1)
   296  	}
   297  	return
   298  }
   299  
   300  func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
   301  	// Simulate Getdirentries using fdopendir/readdir_r/closedir.
   302  	// We store the number of entries to skip in the seek
   303  	// offset of fd. See issue #31368.
   304  	// It's not the full required semantics, but should handle the case
   305  	// of calling Getdirentries or ReadDirent repeatedly.
   306  	// It won't handle assigning the results of lseek to *basep, or handle
   307  	// the directory being edited underfoot.
   308  	skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
   309  	if err != nil {
   310  		return 0, err
   311  	}
   312  
   313  	// We need to duplicate the incoming file descriptor
   314  	// because the caller expects to retain control of it, but
   315  	// fdopendir expects to take control of its argument.
   316  	// Just Dup'ing the file descriptor is not enough, as the
   317  	// result shares underlying state. Use openat to make a really
   318  	// new file descriptor referring to the same directory.
   319  	fd2, err := openat(fd, ".", O_RDONLY, 0)
   320  	if err != nil {
   321  		return 0, err
   322  	}
   323  	d, err := fdopendir(fd2)
   324  	if err != nil {
   325  		Close(fd2)
   326  		return 0, err
   327  	}
   328  	defer closedir(d)
   329  
   330  	var cnt int64
   331  	for {
   332  		var entry Dirent
   333  		var entryp *Dirent
   334  		e := readdir_r(d, &entry, &entryp)
   335  		if e != 0 {
   336  			return n, errnoErr(e)
   337  		}
   338  		if entryp == nil {
   339  			break
   340  		}
   341  		if skip > 0 {
   342  			skip--
   343  			cnt++
   344  			continue
   345  		}
   346  		reclen := int(entry.Reclen)
   347  		if reclen > len(buf) {
   348  			// Not enough room. Return for now.
   349  			// The counter will let us know where we should start up again.
   350  			// Note: this strategy for suspending in the middle and
   351  			// restarting is O(n^2) in the length of the directory. Oh well.
   352  			break
   353  		}
   354  		// Copy entry into return buffer.
   355  		s := struct {
   356  			ptr unsafe.Pointer
   357  			siz int
   358  			cap int
   359  		}{ptr: unsafe.Pointer(&entry), siz: reclen, cap: reclen}
   360  		copy(buf, *(*[]byte)(unsafe.Pointer(&s)))
   361  		buf = buf[reclen:]
   362  		n += reclen
   363  		cnt++
   364  	}
   365  	// Set the seek offset of the input fd to record
   366  	// how many files we've already returned.
   367  	_, err = Seek(fd, cnt, 0 /* SEEK_SET */)
   368  	if err != nil {
   369  		return n, err
   370  	}
   371  
   372  	return n, nil
   373  }
   374  
   375  // Implemented in the runtime package (runtime/sys_darwin.go)
   376  func syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   377  func syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
   378  func syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
   379  func rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   380  func rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
   381  func syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   382  

View as plain text