github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/syscall/syscall_darwin.go (about)

     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  	errorspkg "errors"
    17  	"unsafe"
    18  )
    19  
    20  const ImplementsGetwd = true
    21  
    22  func Getwd() (string, error) {
    23  	buf := make([]byte, 2048)
    24  	attrs, err := getAttrList(".", attrList{CommonAttr: attrCmnFullpath}, buf, 0)
    25  	if err == nil && len(attrs) == 1 && len(attrs[0]) >= 2 {
    26  		wd := string(attrs[0])
    27  		// Sanity check that it's an absolute path and ends
    28  		// in a null byte, which we then strip.
    29  		if wd[0] == '/' && wd[len(wd)-1] == 0 {
    30  			return wd[:len(wd)-1], nil
    31  		}
    32  	}
    33  	// If pkg/os/getwd.go gets ENOTSUP, it will fall back to the
    34  	// slow algorithm.
    35  	return "", ENOTSUP
    36  }
    37  
    38  type SockaddrDatalink struct {
    39  	Len    uint8
    40  	Family uint8
    41  	Index  uint16
    42  	Type   uint8
    43  	Nlen   uint8
    44  	Alen   uint8
    45  	Slen   uint8
    46  	Data   [12]int8
    47  	raw    RawSockaddrDatalink
    48  }
    49  
    50  // Translate "kern.hostname" to []_C_int{0,1,2,3}.
    51  func nametomib(name string) (mib []_C_int, err error) {
    52  	const siz = unsafe.Sizeof(mib[0])
    53  
    54  	// NOTE(rsc): It seems strange to set the buffer to have
    55  	// size CTL_MAXNAME+2 but use only CTL_MAXNAME
    56  	// as the size.  I don't know why the +2 is here, but the
    57  	// kernel uses +2 for its own implementation of this function.
    58  	// I am scared that if we don't include the +2 here, the kernel
    59  	// will silently write 2 words farther than we specify
    60  	// and we'll get memory corruption.
    61  	var buf [CTL_MAXNAME + 2]_C_int
    62  	n := uintptr(CTL_MAXNAME) * siz
    63  
    64  	p := (*byte)(unsafe.Pointer(&buf[0]))
    65  	bytes, err := ByteSliceFromString(name)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	// Magic sysctl: "setting" 0.3 to a string name
    71  	// lets you read back the array of integers form.
    72  	if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
    73  		return nil, err
    74  	}
    75  	return buf[0 : n/siz], nil
    76  }
    77  
    78  // ParseDirent parses up to max directory entries in buf,
    79  // appending the names to names.  It returns the number
    80  // bytes consumed from buf, the number of entries added
    81  // to names, and the new names slice.
    82  func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
    83  	origlen := len(buf)
    84  	for max != 0 && len(buf) > 0 {
    85  		dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
    86  		if dirent.Reclen == 0 {
    87  			buf = nil
    88  			break
    89  		}
    90  		buf = buf[dirent.Reclen:]
    91  		if dirent.Ino == 0 { // File absent in directory.
    92  			continue
    93  		}
    94  		bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
    95  		var name = string(bytes[0:dirent.Namlen])
    96  		if name == "." || name == ".." { // Useless names
    97  			continue
    98  		}
    99  		max--
   100  		count++
   101  		names = append(names, name)
   102  	}
   103  	return origlen - len(buf), count, names
   104  }
   105  
   106  //sys   ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
   107  func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
   108  func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
   109  
   110  const (
   111  	attrBitMapCount = 5
   112  	attrCmnFullpath = 0x08000000
   113  )
   114  
   115  type attrList struct {
   116  	bitmapCount uint16
   117  	_           uint16
   118  	CommonAttr  uint32
   119  	VolAttr     uint32
   120  	DirAttr     uint32
   121  	FileAttr    uint32
   122  	Forkattr    uint32
   123  }
   124  
   125  func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
   126  	if len(attrBuf) < 4 {
   127  		return nil, errorspkg.New("attrBuf too small")
   128  	}
   129  	attrList.bitmapCount = attrBitMapCount
   130  
   131  	var _p0 *byte
   132  	_p0, err = BytePtrFromString(path)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	_, _, e1 := Syscall6(
   138  		SYS_GETATTRLIST,
   139  		uintptr(unsafe.Pointer(_p0)),
   140  		uintptr(unsafe.Pointer(&attrList)),
   141  		uintptr(unsafe.Pointer(&attrBuf[0])),
   142  		uintptr(len(attrBuf)),
   143  		uintptr(options),
   144  		0,
   145  	)
   146  	if e1 != 0 {
   147  		return nil, e1
   148  	}
   149  	size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
   150  
   151  	// dat is the section of attrBuf that contains valid data,
   152  	// without the 4 byte length header. All attribute offsets
   153  	// are relative to dat.
   154  	dat := attrBuf
   155  	if int(size) < len(attrBuf) {
   156  		dat = dat[:size]
   157  	}
   158  	dat = dat[4:] // remove length prefix
   159  
   160  	for i := uint32(0); int(i) < len(dat); {
   161  		header := dat[i:]
   162  		if len(header) < 8 {
   163  			return attrs, errorspkg.New("truncated attribute header")
   164  		}
   165  		datOff := *(*int32)(unsafe.Pointer(&header[0]))
   166  		attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
   167  		if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
   168  			return attrs, errorspkg.New("truncated results; attrBuf too small")
   169  		}
   170  		end := uint32(datOff) + attrLen
   171  		attrs = append(attrs, dat[datOff:end])
   172  		i = end
   173  		if r := i % 4; r != 0 {
   174  			i += (4 - r)
   175  		}
   176  	}
   177  	return
   178  }
   179  
   180  //sysnb pipe() (r int, w int, err error)
   181  
   182  func Pipe(p []int) (err error) {
   183  	if len(p) != 2 {
   184  		return EINVAL
   185  	}
   186  	p[0], p[1], err = pipe()
   187  	return
   188  }
   189  
   190  /*
   191   * Wrapped
   192   */
   193  
   194  //sys	kill(pid int, signum int, posix int) (err error)
   195  
   196  func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
   197  
   198  /*
   199   * Exposed directly
   200   */
   201  //sys	Access(path string, mode uint32) (err error)
   202  //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
   203  //sys	Chdir(path string) (err error)
   204  //sys	Chflags(path string, flags int) (err error)
   205  //sys	Chmod(path string, mode uint32) (err error)
   206  //sys	Chown(path string, uid int, gid int) (err error)
   207  //sys	Chroot(path string) (err error)
   208  //sys	Close(fd int) (err error)
   209  //sysnb	Dup(fd int) (nfd int, err error)
   210  //sysnb	Dup2(from int, to int) (err error)
   211  //sys	Exchangedata(path1 string, path2 string, options int) (err error)
   212  //sys	Exit(code int)
   213  //sys	Fchdir(fd int) (err error)
   214  //sys	Fchflags(fd int, flags int) (err error)
   215  //sys	Fchmod(fd int, mode uint32) (err error)
   216  //sys	Fchown(fd int, uid int, gid int) (err error)
   217  //sys	Flock(fd int, how int) (err error)
   218  //sys	Fpathconf(fd int, name int) (val int, err error)
   219  //sys	Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
   220  //sys	Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
   221  //sys	Fsync(fd int) (err error)
   222  //sys	Ftruncate(fd int, length int64) (err error)
   223  //sys	Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
   224  //sys	Getdtablesize() (size int)
   225  //sysnb	Getegid() (egid int)
   226  //sysnb	Geteuid() (uid int)
   227  //sys	Getfsstat(buf []Statfs_t, flags int) (n int, err error) = SYS_GETFSSTAT64
   228  //sysnb	Getgid() (gid int)
   229  //sysnb	Getpgid(pid int) (pgid int, err error)
   230  //sysnb	Getpgrp() (pgrp int)
   231  //sysnb	Getpid() (pid int)
   232  //sysnb	Getppid() (ppid int)
   233  //sys	Getpriority(which int, who int) (prio int, err error)
   234  //sysnb	Getrlimit(which int, lim *Rlimit) (err error)
   235  //sysnb	Getrusage(who int, rusage *Rusage) (err error)
   236  //sysnb	Getsid(pid int) (sid int, err error)
   237  //sysnb	Getuid() (uid int)
   238  //sysnb	Issetugid() (tainted bool)
   239  //sys	Kqueue() (fd int, err error)
   240  //sys	Lchown(path string, uid int, gid int) (err error)
   241  //sys	Link(path string, link string) (err error)
   242  //sys	Listen(s int, backlog int) (err error)
   243  //sys	Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
   244  //sys	Mkdir(path string, mode uint32) (err error)
   245  //sys	Mkfifo(path string, mode uint32) (err error)
   246  //sys	Mknod(path string, mode uint32, dev int) (err error)
   247  //sys	Open(path string, mode int, perm uint32) (fd int, err error)
   248  //sys	Pathconf(path string, name int) (val int, err error)
   249  //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
   250  //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
   251  //sys	read(fd int, p []byte) (n int, err error)
   252  //sys	Readlink(path string, buf []byte) (n int, err error)
   253  //sys	Rename(from string, to string) (err error)
   254  //sys	Revoke(path string) (err error)
   255  //sys	Rmdir(path string) (err error)
   256  //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
   257  //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
   258  //sys	Setegid(egid int) (err error)
   259  //sysnb	Seteuid(euid int) (err error)
   260  //sysnb	Setgid(gid int) (err error)
   261  //sys	Setlogin(name string) (err error)
   262  //sysnb	Setpgid(pid int, pgid int) (err error)
   263  //sys	Setpriority(which int, who int, prio int) (err error)
   264  //sys	Setprivexec(flag int) (err error)
   265  //sysnb	Setregid(rgid int, egid int) (err error)
   266  //sysnb	Setreuid(ruid int, euid int) (err error)
   267  //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
   268  //sysnb	Setsid() (pid int, err error)
   269  //sysnb	Settimeofday(tp *Timeval) (err error)
   270  //sysnb	Setuid(uid int) (err error)
   271  //sys	Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
   272  //sys	Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
   273  //sys	Symlink(path string, link string) (err error)
   274  //sys	Sync() (err error)
   275  //sys	Truncate(path string, length int64) (err error)
   276  //sys	Umask(newmask int) (oldmask int)
   277  //sys	Undelete(path string) (err error)
   278  //sys	Unlink(path string) (err error)
   279  //sys	Unmount(path string, flags int) (err error)
   280  //sys	write(fd int, p []byte) (n int, err error)
   281  //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
   282  //sys   munmap(addr uintptr, length uintptr) (err error)
   283  //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
   284  //sys	writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
   285  
   286  /*
   287   * Unimplemented
   288   */
   289  // Profil
   290  // Sigaction
   291  // Sigprocmask
   292  // Getlogin
   293  // Sigpending
   294  // Sigaltstack
   295  // Ioctl
   296  // Reboot
   297  // Execve
   298  // Vfork
   299  // Sbrk
   300  // Sstk
   301  // Ovadvise
   302  // Mincore
   303  // Setitimer
   304  // Swapon
   305  // Select
   306  // Sigsuspend
   307  // Readv
   308  // Writev
   309  // Nfssvc
   310  // Getfh
   311  // Quotactl
   312  // Mount
   313  // Csops
   314  // Waitid
   315  // Add_profil
   316  // Kdebug_trace
   317  // Sigreturn
   318  // Mmap
   319  // Mlock
   320  // Munlock
   321  // Atsocket
   322  // Kqueue_from_portset_np
   323  // Kqueue_portset
   324  // Getattrlist
   325  // Setattrlist
   326  // Getdirentriesattr
   327  // Searchfs
   328  // Delete
   329  // Copyfile
   330  // Poll
   331  // Watchevent
   332  // Waitevent
   333  // Modwatch
   334  // Getxattr
   335  // Fgetxattr
   336  // Setxattr
   337  // Fsetxattr
   338  // Removexattr
   339  // Fremovexattr
   340  // Listxattr
   341  // Flistxattr
   342  // Fsctl
   343  // Initgroups
   344  // Posix_spawn
   345  // Nfsclnt
   346  // Fhopen
   347  // Minherit
   348  // Semsys
   349  // Msgsys
   350  // Shmsys
   351  // Semctl
   352  // Semget
   353  // Semop
   354  // Msgctl
   355  // Msgget
   356  // Msgsnd
   357  // Msgrcv
   358  // Shmat
   359  // Shmctl
   360  // Shmdt
   361  // Shmget
   362  // Shm_open
   363  // Shm_unlink
   364  // Sem_open
   365  // Sem_close
   366  // Sem_unlink
   367  // Sem_wait
   368  // Sem_trywait
   369  // Sem_post
   370  // Sem_getvalue
   371  // Sem_init
   372  // Sem_destroy
   373  // Open_extended
   374  // Umask_extended
   375  // Stat_extended
   376  // Lstat_extended
   377  // Fstat_extended
   378  // Chmod_extended
   379  // Fchmod_extended
   380  // Access_extended
   381  // Settid
   382  // Gettid
   383  // Setsgroups
   384  // Getsgroups
   385  // Setwgroups
   386  // Getwgroups
   387  // Mkfifo_extended
   388  // Mkdir_extended
   389  // Identitysvc
   390  // Shared_region_check_np
   391  // Shared_region_map_np
   392  // __pthread_mutex_destroy
   393  // __pthread_mutex_init
   394  // __pthread_mutex_lock
   395  // __pthread_mutex_trylock
   396  // __pthread_mutex_unlock
   397  // __pthread_cond_init
   398  // __pthread_cond_destroy
   399  // __pthread_cond_broadcast
   400  // __pthread_cond_signal
   401  // Setsid_with_pid
   402  // __pthread_cond_timedwait
   403  // Aio_fsync
   404  // Aio_return
   405  // Aio_suspend
   406  // Aio_cancel
   407  // Aio_error
   408  // Aio_read
   409  // Aio_write
   410  // Lio_listio
   411  // __pthread_cond_wait
   412  // Iopolicysys
   413  // Mlockall
   414  // Munlockall
   415  // __pthread_kill
   416  // __pthread_sigmask
   417  // __sigwait
   418  // __disable_threadsignal
   419  // __pthread_markcancel
   420  // __pthread_canceled
   421  // __semwait_signal
   422  // Proc_info
   423  // sendfile
   424  // Stat64_extended
   425  // Lstat64_extended
   426  // Fstat64_extended
   427  // __pthread_chdir
   428  // __pthread_fchdir
   429  // Audit
   430  // Auditon
   431  // Getauid
   432  // Setauid
   433  // Getaudit
   434  // Setaudit
   435  // Getaudit_addr
   436  // Setaudit_addr
   437  // Auditctl
   438  // Bsdthread_create
   439  // Bsdthread_terminate
   440  // Stack_snapshot
   441  // Bsdthread_register
   442  // Workq_open
   443  // Workq_ops
   444  // __mac_execve
   445  // __mac_syscall
   446  // __mac_get_file
   447  // __mac_set_file
   448  // __mac_get_link
   449  // __mac_set_link
   450  // __mac_get_proc
   451  // __mac_set_proc
   452  // __mac_get_fd
   453  // __mac_set_fd
   454  // __mac_get_pid
   455  // __mac_get_lcid
   456  // __mac_get_lctx
   457  // __mac_set_lctx
   458  // Setlcid
   459  // Read_nocancel
   460  // Write_nocancel
   461  // Open_nocancel
   462  // Close_nocancel
   463  // Wait4_nocancel
   464  // Recvmsg_nocancel
   465  // Sendmsg_nocancel
   466  // Recvfrom_nocancel
   467  // Accept_nocancel
   468  // Msync_nocancel
   469  // Fcntl_nocancel
   470  // Select_nocancel
   471  // Fsync_nocancel
   472  // Connect_nocancel
   473  // Sigsuspend_nocancel
   474  // Readv_nocancel
   475  // Writev_nocancel
   476  // Sendto_nocancel
   477  // Pread_nocancel
   478  // Pwrite_nocancel
   479  // Waitid_nocancel
   480  // Poll_nocancel
   481  // Msgsnd_nocancel
   482  // Msgrcv_nocancel
   483  // Sem_wait_nocancel
   484  // Aio_suspend_nocancel
   485  // __sigwait_nocancel
   486  // __semwait_signal_nocancel
   487  // __mac_mount
   488  // __mac_get_mount
   489  // __mac_getfsstat