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  //sysnb pipe(p *[2]int32) (err error)
    76  
    77  func Pipe(p []int) (err error) {
    78  	if len(p) != 2 {
    79  		return EINVAL
    80  	}
    81  	var q [2]int32
    82  	err = pipe(&q)
    83  	if err == nil {
    84  		p[0] = int(q[0])
    85  		p[1] = int(q[1])
    86  	}
    87  	return
    88  }
    89  
    90  func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
    91  	var _p0 unsafe.Pointer
    92  	var bufsize uintptr
    93  	if len(buf) > 0 {
    94  		_p0 = unsafe.Pointer(&buf[0])
    95  		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
    96  	}
    97  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_getfsstat_trampoline), uintptr(_p0), bufsize, uintptr(flags))
    98  	n = int(r0)
    99  	if e1 != 0 {
   100  		err = e1
   101  	}
   102  	return
   103  }
   104  
   105  func libc_getfsstat_trampoline()
   106  
   107  //go:cgo_import_dynamic libc_getfsstat getfsstat "/usr/lib/libSystem.B.dylib"
   108  
   109  //sys	utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error)
   110  
   111  /*
   112   * Wrapped
   113   */
   114  
   115  //sys	kill(pid int, signum int, posix int) (err error)
   116  
   117  func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
   118  
   119  /*
   120   * Exposed directly
   121   */
   122  //sys	Access(path string, mode uint32) (err error)
   123  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   124  //sys	Chdir(path string) (err error)
   125  //sys	Chflags(path string, flags int) (err error)
   126  //sys	Chmod(path string, mode uint32) (err error)
   127  //sys	Chown(path string, uid int, gid int) (err error)
   128  //sys	Chroot(path string) (err error)
   129  //sys	Close(fd int) (err error)
   130  //sys	closedir(dir uintptr) (err error)
   131  //sys	Dup(fd int) (nfd int, err error)
   132  //sys	Dup2(from int, to int) (err error)
   133  //sys	Exchangedata(path1 string, path2 string, options int) (err error)
   134  //sys	Fchdir(fd int) (err error)
   135  //sys	Fchflags(fd int, flags int) (err error)
   136  //sys	Fchmod(fd int, mode uint32) (err error)
   137  //sys	Fchown(fd int, uid int, gid int) (err error)
   138  //sys	Flock(fd int, how int) (err error)
   139  //sys	Fpathconf(fd int, name int) (val int, err error)
   140  //sys	Fsync(fd int) (err error)
   141  //  Fsync is not called for os.File.Sync(). Please see internal/poll/fd_fsync_darwin.go
   142  //sys	Ftruncate(fd int, length int64) (err error)
   143  //sys	Getdtablesize() (size int)
   144  //sysnb	Getegid() (egid int)
   145  //sysnb	Geteuid() (uid int)
   146  //sysnb	Getgid() (gid int)
   147  //sysnb	Getpgid(pid int) (pgid int, err error)
   148  //sysnb	Getpgrp() (pgrp int)
   149  //sysnb	Getpid() (pid int)
   150  //sysnb	Getppid() (ppid int)
   151  //sys	Getpriority(which int, who int) (prio int, err error)
   152  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   153  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   154  //sysnb	Getsid(pid int) (sid int, err error)
   155  //sysnb	Getuid() (uid int)
   156  //sysnb	Issetugid() (tainted bool)
   157  //sys	Kqueue() (fd int, err error)
   158  //sys	Lchown(path string, uid int, gid int) (err error)
   159  //sys	Link(path string, link string) (err error)
   160  //sys	Listen(s int, backlog int) (err error)
   161  //sys	Mkdir(path string, mode uint32) (err error)
   162  //sys	Mkfifo(path string, mode uint32) (err error)
   163  //sys	Mknod(path string, mode uint32, dev int) (err error)
   164  //sys	Mlock(b []byte) (err error)
   165  //sys	Mlockall(flags int) (err error)
   166  //sys	Mprotect(b []byte, prot int) (err error)
   167  //sys	Munlock(b []byte) (err error)
   168  //sys	Munlockall() (err error)
   169  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   170  //sys	Pathconf(path string, name int) (val int, err error)
   171  //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
   172  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
   173  //sys	read(fd int, p []byte) (n int, err error)
   174  //sys	readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
   175  //sys	Readlink(path string, buf []byte) (n int, err error)
   176  //sys	Rename(from string, to string) (err error)
   177  //sys	Revoke(path string) (err error)
   178  //sys	Rmdir(path string) (err error)
   179  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_lseek
   180  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   181  //sys	Setegid(egid int) (err error)
   182  //sysnb	Seteuid(euid int) (err error)
   183  //sysnb	Setgid(gid int) (err error)
   184  //sys	Setlogin(name string) (err error)
   185  //sysnb	Setpgid(pid int, pgid int) (err error)
   186  //sys	Setpriority(which int, who int, prio int) (err error)
   187  //sys	Setprivexec(flag int) (err error)
   188  //sysnb	Setregid(rgid int, egid int) (err error)
   189  //sysnb	Setreuid(ruid int, euid int) (err error)
   190  //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
   191  //sysnb	Setsid() (pid int, err error)
   192  //sysnb	Settimeofday(tp *Timeval) (err error)
   193  //sysnb	Setuid(uid int) (err error)
   194  //sys	Symlink(path string, link string) (err error)
   195  //sys	Sync() (err error)
   196  //sys	Truncate(path string, length int64) (err error)
   197  //sys	Umask(newmask int) (oldmask int)
   198  //sys	Undelete(path string) (err error)
   199  //sys	Unlink(path string) (err error)
   200  //sys	Unmount(path string, flags int) (err error)
   201  //sys	write(fd int, p []byte) (n int, err error)
   202  //sys	writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
   203  //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   204  //sys   munmap(addr uintptr, length uintptr) (err error)
   205  //sysnb fork() (pid int, err error)
   206  //sysnb ioctl(fd int, req int, arg int) (err error)
   207  //sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
   208  //sysnb execve(path *byte, argv **byte, envp **byte) (err error)
   209  //sysnb exit(res int) (err error)
   210  //sys	sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
   211  //sys	fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
   212  //sys   unlinkat(fd int, path string, flags int) (err error)
   213  //sys   openat(fd int, path string, flags int, perm uint32) (fdret int, err error)
   214  //sys	getcwd(buf []byte) (n int, err error)
   215  
   216  func init() {
   217  	execveDarwin = execve
   218  }
   219  
   220  func fdopendir(fd int) (dir uintptr, err error) {
   221  	r0, _, e1 := syscallPtr(abi.FuncPCABI0(libc_fdopendir_trampoline), uintptr(fd), 0, 0)
   222  	dir = uintptr(r0)
   223  	if e1 != 0 {
   224  		err = errnoErr(e1)
   225  	}
   226  	return
   227  }
   228  
   229  func libc_fdopendir_trampoline()
   230  
   231  //go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"
   232  
   233  func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
   234  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_read_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
   235  	n = int(r0)
   236  	if e1 != 0 {
   237  		err = errnoErr(e1)
   238  	}
   239  	return
   240  }
   241  
   242  func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
   243  	r0, _, e1 := syscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
   244  	n = int(r0)
   245  	if e1 != 0 {
   246  		err = errnoErr(e1)
   247  	}
   248  	return
   249  }
   250  
   251  func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
   252  	// Simulate Getdirentries using fdopendir/readdir_r/closedir.
   253  	// We store the number of entries to skip in the seek
   254  	// offset of fd. See issue #31368.
   255  	// It's not the full required semantics, but should handle the case
   256  	// of calling Getdirentries or ReadDirent repeatedly.
   257  	// It won't handle assigning the results of lseek to *basep, or handle
   258  	// the directory being edited underfoot.
   259  	skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
   260  	if err != nil {
   261  		return 0, err
   262  	}
   263  
   264  	// We need to duplicate the incoming file descriptor
   265  	// because the caller expects to retain control of it, but
   266  	// fdopendir expects to take control of its argument.
   267  	// Just Dup'ing the file descriptor is not enough, as the
   268  	// result shares underlying state. Use openat to make a really
   269  	// new file descriptor referring to the same directory.
   270  	fd2, err := openat(fd, ".", O_RDONLY, 0)
   271  	if err != nil {
   272  		return 0, err
   273  	}
   274  	d, err := fdopendir(fd2)
   275  	if err != nil {
   276  		Close(fd2)
   277  		return 0, err
   278  	}
   279  	defer closedir(d)
   280  
   281  	var cnt int64
   282  	for {
   283  		var entry Dirent
   284  		var entryp *Dirent
   285  		e := readdir_r(d, &entry, &entryp)
   286  		if e != 0 {
   287  			return n, errnoErr(e)
   288  		}
   289  		if entryp == nil {
   290  			break
   291  		}
   292  		if skip > 0 {
   293  			skip--
   294  			cnt++
   295  			continue
   296  		}
   297  		reclen := int(entry.Reclen)
   298  		if reclen > len(buf) {
   299  			// Not enough room. Return for now.
   300  			// The counter will let us know where we should start up again.
   301  			// Note: this strategy for suspending in the middle and
   302  			// restarting is O(n^2) in the length of the directory. Oh well.
   303  			break
   304  		}
   305  		// Copy entry into return buffer.
   306  		s := struct {
   307  			ptr unsafe.Pointer
   308  			siz int
   309  			cap int
   310  		}{ptr: unsafe.Pointer(&entry), siz: reclen, cap: reclen}
   311  		copy(buf, *(*[]byte)(unsafe.Pointer(&s)))
   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