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  // utimensat should be an internal detail,
   117  // but widely used packages access it using linkname.
   118  // Notable members of the hall of shame include:
   119  //   - github.com/tetratelabs/wazero
   120  //
   121  // See go.dev/issue/67401.
   122  //
   123  //go:linkname utimensat
   124  
   125  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
   126  
   127  /*
   128   * Wrapped
   129   */
   130  
   131  //sys	kill(pid int, signum int, posix int) (err error)
   132  
   133  func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
   134  
   135  /*
   136   * Exposed directly
   137   */
   138  //sys	Access(path string, mode uint32) (err error)
   139  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   140  //sys	Chdir(path string) (err error)
   141  //sys	Chflags(path string, flags int) (err error)
   142  //sys	Chmod(path string, mode uint32) (err error)
   143  //sys	Chown(path string, uid int, gid int) (err error)
   144  //sys	Chroot(path string) (err error)
   145  //sys	Close(fd int) (err error)
   146  //sys	closedir(dir uintptr) (err error)
   147  //sys	Dup(fd int) (nfd int, err error)
   148  //sys	Dup2(from int, to int) (err error)
   149  //sys	Exchangedata(path1 string, path2 string, options int) (err error)
   150  //sys	Fchdir(fd int) (err error)
   151  //sys	Fchflags(fd int, flags int) (err error)
   152  //sys	Fchmod(fd int, mode uint32) (err error)
   153  //sys	Fchown(fd int, uid int, gid int) (err error)
   154  //sys	Flock(fd int, how int) (err error)
   155  //sys	Fpathconf(fd int, name int) (val int, err error)
   156  //sys	Fsync(fd int) (err error)
   157  //  Fsync is not called for os.File.Sync(). Please see internal/poll/fd_fsync_darwin.go
   158  //sys	Ftruncate(fd int, length int64) (err error)
   159  //sys	Getdtablesize() (size int)
   160  //sysnb	Getegid() (egid int)
   161  //sysnb	Geteuid() (uid int)
   162  //sysnb	Getgid() (gid int)
   163  //sysnb	Getpgid(pid int) (pgid int, err error)
   164  //sysnb	Getpgrp() (pgrp int)
   165  //sysnb	Getpid() (pid int)
   166  //sysnb	Getppid() (ppid int)
   167  //sys	Getpriority(which int, who int) (prio int, err error)
   168  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   169  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   170  //sysnb	Getsid(pid int) (sid int, err error)
   171  //sysnb	Getuid() (uid int)
   172  //sysnb	Issetugid() (tainted bool)
   173  //sys	Kqueue() (fd int, err error)
   174  //sys	Lchown(path string, uid int, gid int) (err error)
   175  //sys	Link(path string, link string) (err error)
   176  //sys	Listen(s int, backlog int) (err error)
   177  //sys	Mkdir(path string, mode uint32) (err error)
   178  //sys	Mkfifo(path string, mode uint32) (err error)
   179  //sys	Mknod(path string, mode uint32, dev int) (err error)
   180  //sys	Mlock(b []byte) (err error)
   181  //sys	Mlockall(flags int) (err error)
   182  //sys	Mprotect(b []byte, prot int) (err error)
   183  //sys	msync(b []byte, flags int) (err error)
   184  //sys	Munlock(b []byte) (err error)
   185  //sys	Munlockall() (err error)
   186  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   187  //sys	Pathconf(path string, name int) (val int, err error)
   188  //sys	pread(fd int, p []byte, offset int64) (n int, err error)
   189  //sys	pwrite(fd int, p []byte, offset int64) (n int, err error)
   190  //sys	read(fd int, p []byte) (n int, err error)
   191  //sys	readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
   192  //sys	Readlink(path string, buf []byte) (n int, err error)
   193  //sys	Rename(from string, to string) (err error)
   194  //sys	Revoke(path string) (err error)
   195  //sys	Rmdir(path string) (err error)
   196  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_lseek
   197  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   198  //sys	Setegid(egid int) (err error)
   199  //sysnb	Seteuid(euid int) (err error)
   200  //sysnb	Setgid(gid int) (err error)
   201  //sys	Setlogin(name string) (err error)
   202  //sysnb	Setpgid(pid int, pgid int) (err error)
   203  //sys	Setpriority(which int, who int, prio int) (err error)
   204  //sys	Setprivexec(flag int) (err error)
   205  //sysnb	Setregid(rgid int, egid int) (err error)
   206  //sysnb	Setreuid(ruid int, euid int) (err error)
   207  //sysnb	setrlimit(which int, lim *Rlimit) (err error)
   208  //sysnb	Setsid() (pid int, err error)
   209  //sysnb	Settimeofday(tp *Timeval) (err error)
   210  //sysnb	Setuid(uid int) (err error)
   211  //sys	Symlink(path string, link string) (err error)
   212  //sys	Sync() (err error)
   213  //sys	Truncate(path string, length int64) (err error)
   214  //sys	Umask(newmask int) (oldmask int)
   215  //sys	Undelete(path string) (err error)
   216  //sys	Unlink(path string) (err error)
   217  //sys	Unmount(path string, flags int) (err error)
   218  //sys	write(fd int, p []byte) (n int, err error)
   219  //sys	writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
   220  //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   221  //sys   munmap(addr uintptr, length uintptr) (err error)
   222  //sysnb fork() (pid int, err error)
   223  //sysnb execve(path *byte, argv **byte, envp **byte) (err error)
   224  //sysnb exit(res int) (err error)
   225  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
   226  //sys   unlinkat(fd int, path string, flags int) (err error)
   227  //sys   openat(fd int, path string, flags int, perm uint32) (fdret int, err error)
   228  //sys	getcwd(buf []byte) (n int, err error)
   229  
   230  func init() {
   231  	execveDarwin = execve
   232  }
   233  
   234  func fdopendir(fd int) (dir uintptr, err error) {
   235  	r0, _, e1 := syscallPtr(abi.FuncPCABI0(libc_fdopendir_trampoline), uintptr(fd), 0, 0)
   236  	dir = r0
   237  	if e1 != 0 {
   238  		err = errnoErr(e1)
   239  	}
   240  	return
   241  }
   242  
   243  func libc_fdopendir_trampoline()
   244  
   245  //go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"
   246  
   247  func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
   248  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_read_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
   249  	n = int(r0)
   250  	if e1 != 0 {
   251  		err = errnoErr(e1)
   252  	}
   253  	return
   254  }
   255  
   256  func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
   257  	// Simulate Getdirentries using fdopendir/readdir_r/closedir.
   258  	// We store the number of entries to skip in the seek
   259  	// offset of fd. See issue #31368.
   260  	// It's not the full required semantics, but should handle the case
   261  	// of calling Getdirentries or ReadDirent repeatedly.
   262  	// It won't handle assigning the results of lseek to *basep, or handle
   263  	// the directory being edited underfoot.
   264  	skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
   265  	if err != nil {
   266  		return 0, err
   267  	}
   268  
   269  	// We need to duplicate the incoming file descriptor
   270  	// because the caller expects to retain control of it, but
   271  	// fdopendir expects to take control of its argument.
   272  	// Just Dup'ing the file descriptor is not enough, as the
   273  	// result shares underlying state. Use openat to make a really
   274  	// new file descriptor referring to the same directory.
   275  	fd2, err := openat(fd, ".", O_RDONLY, 0)
   276  	if err != nil {
   277  		return 0, err
   278  	}
   279  	d, err := fdopendir(fd2)
   280  	if err != nil {
   281  		Close(fd2)
   282  		return 0, err
   283  	}
   284  	defer closedir(d)
   285  
   286  	var cnt int64
   287  	for {
   288  		var entry Dirent
   289  		var entryp *Dirent
   290  		e := readdir_r(d, &entry, &entryp)
   291  		if e != 0 {
   292  			return n, errnoErr(e)
   293  		}
   294  		if entryp == nil {
   295  			break
   296  		}
   297  		if skip > 0 {
   298  			skip--
   299  			cnt++
   300  			continue
   301  		}
   302  		reclen := int(entry.Reclen)
   303  		if reclen > len(buf) {
   304  			// Not enough room. Return for now.
   305  			// The counter will let us know where we should start up again.
   306  			// Note: this strategy for suspending in the middle and
   307  			// restarting is O(n^2) in the length of the directory. Oh well.
   308  			break
   309  		}
   310  		// Copy entry into return buffer.
   311  		copy(buf, unsafe.Slice((*byte)(unsafe.Pointer(&entry)), reclen))
   312  		buf = buf[reclen:]
   313  		n += reclen
   314  		cnt++
   315  	}
   316  	// Set the seek offset of the input fd to record
   317  	// how many files we've already returned.
   318  	_, err = Seek(fd, cnt, 0 /* SEEK_SET */)
   319  	if err != nil {
   320  		return n, err
   321  	}
   322  
   323  	return n, nil
   324  }
   325  
   326  // Implemented in the runtime package (runtime/sys_darwin.go)
   327  func syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   328  func syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
   329  func syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
   330  func rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   331  func rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
   332  func syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
   333  

View as plain text