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

View as plain text