Home | History | Annotate | Download | only in syscall
      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 func direntIno(buf []byte) (uint64, bool) {
     79 	return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
     80 }
     81 
     82 func direntReclen(buf []byte) (uint64, bool) {
     83 	return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
     84 }
     85 
     86 func direntNamlen(buf []byte) (uint64, bool) {
     87 	return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
     88 }
     89 
     90 //sys   ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
     91 func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
     92 func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
     93 
     94 const (
     95 	attrBitMapCount = 5
     96 	attrCmnFullpath = 0x08000000
     97 )
     98 
     99 type attrList struct {
    100 	bitmapCount uint16
    101 	_           uint16
    102 	CommonAttr  uint32
    103 	VolAttr     uint32
    104 	DirAttr     uint32
    105 	FileAttr    uint32
    106 	Forkattr    uint32
    107 }
    108 
    109 func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
    110 	if len(attrBuf) < 4 {
    111 		return nil, errorspkg.New("attrBuf too small")
    112 	}
    113 	attrList.bitmapCount = attrBitMapCount
    114 
    115 	var _p0 *byte
    116 	_p0, err = BytePtrFromString(path)
    117 	if err != nil {
    118 		return nil, err
    119 	}
    120 
    121 	_, _, e1 := Syscall6(
    122 		SYS_GETATTRLIST,
    123 		uintptr(unsafe.Pointer(_p0)),
    124 		uintptr(unsafe.Pointer(&attrList)),
    125 		uintptr(unsafe.Pointer(&attrBuf[0])),
    126 		uintptr(len(attrBuf)),
    127 		uintptr(options),
    128 		0,
    129 	)
    130 	use(unsafe.Pointer(_p0))
    131 	if e1 != 0 {
    132 		return nil, e1
    133 	}
    134 	size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
    135 
    136 	// dat is the section of attrBuf that contains valid data,
    137 	// without the 4 byte length header. All attribute offsets
    138 	// are relative to dat.
    139 	dat := attrBuf
    140 	if int(size) < len(attrBuf) {
    141 		dat = dat[:size]
    142 	}
    143 	dat = dat[4:] // remove length prefix
    144 
    145 	for i := uint32(0); int(i) < len(dat); {
    146 		header := dat[i:]
    147 		if len(header) < 8 {
    148 			return attrs, errorspkg.New("truncated attribute header")
    149 		}
    150 		datOff := *(*int32)(unsafe.Pointer(&header[0]))
    151 		attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
    152 		if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
    153 			return attrs, errorspkg.New("truncated results; attrBuf too small")
    154 		}
    155 		end := uint32(datOff) + attrLen
    156 		attrs = append(attrs, dat[datOff:end])
    157 		i = end
    158 		if r := i % 4; r != 0 {
    159 			i += (4 - r)
    160 		}
    161 	}
    162 	return
    163 }
    164 
    165 //sysnb pipe() (r int, w int, err error)
    166 
    167 func Pipe(p []int) (err error) {
    168 	if len(p) != 2 {
    169 		return EINVAL
    170 	}
    171 	p[0], p[1], err = pipe()
    172 	return
    173 }
    174 
    175 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
    176 	var _p0 unsafe.Pointer
    177 	var bufsize uintptr
    178 	if len(buf) > 0 {
    179 		_p0 = unsafe.Pointer(&buf[0])
    180 		bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
    181 	}
    182 	r0, _, e1 := Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags))
    183 	use(unsafe.Pointer(_p0))
    184 	n = int(r0)
    185 	if e1 != 0 {
    186 		err = e1
    187 	}
    188 	return
    189 }
    190 
    191 /*
    192  * Wrapped
    193  */
    194 
    195 //sys	kill(pid int, signum int, posix int) (err error)
    196 
    197 func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
    198 
    199 /*
    200  * Exposed directly
    201  */
    202 //sys	Access(path string, mode uint32) (err error)
    203 //sys	Adjtime(delta *Timeval, olddelta *Timeval) (err error)
    204 //sys	Chdir(path string) (err error)
    205 //sys	Chflags(path string, flags int) (err error)
    206 //sys	Chmod(path string, mode uint32) (err error)
    207 //sys	Chown(path string, uid int, gid int) (err error)
    208 //sys	Chroot(path string) (err error)
    209 //sys	Close(fd int) (err error)
    210 //sys	Dup(fd int) (nfd int, err error)
    211 //sys	Dup2(from int, to int) (err error)
    212 //sys	Exchangedata(path1 string, path2 string, options int) (err error)
    213 //sys	Exit(code int)
    214 //sys	Fchdir(fd int) (err error)
    215 //sys	Fchflags(fd int, flags int) (err error)
    216 //sys	Fchmod(fd int, mode uint32) (err error)
    217 //sys	Fchown(fd int, uid int, gid int) (err error)
    218 //sys	Flock(fd int, how int) (err error)
    219 //sys	Fpathconf(fd int, name int) (val int, err error)
    220 //sys	Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
    221 //sys	Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
    222 //sys	Fsync(fd int) (err error)
    223 //sys	Ftruncate(fd int, length int64) (err error)
    224 //sys	Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
    225 //sys	Getdtablesize() (size int)
    226 //sysnb	Getegid() (egid int)
    227 //sysnb	Geteuid() (uid int)
    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	Mlock(b []byte) (err error)
    248 //sys	Mlockall(flags int) (err error)
    249 //sys	Mprotect(b []byte, prot int) (err error)
    250 //sys	Munlock(b []byte) (err error)
    251 //sys	Munlockall() (err error)
    252 //sys	Open(path string, mode int, perm uint32) (fd int, err error)
    253 //sys	Pathconf(path string, name int) (val int, err error)
    254 //sys	Pread(fd int, p []byte, offset int64) (n int, err error)
    255 //sys	Pwrite(fd int, p []byte, offset int64) (n int, err error)
    256 //sys	read(fd int, p []byte) (n int, err error)
    257 //sys	Readlink(path string, buf []byte) (n int, err error)
    258 //sys	Rename(from string, to string) (err error)
    259 //sys	Revoke(path string) (err error)
    260 //sys	Rmdir(path string) (err error)
    261 //sys	Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
    262 //sys	Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
    263 //sys	Setegid(egid int) (err error)
    264 //sysnb	Seteuid(euid int) (err error)
    265 //sysnb	Setgid(gid int) (err error)
    266 //sys	Setlogin(name string) (err error)
    267 //sysnb	Setpgid(pid int, pgid int) (err error)
    268 //sys	Setpriority(which int, who int, prio int) (err error)
    269 //sys	Setprivexec(flag int) (err error)
    270 //sysnb	Setregid(rgid int, egid int) (err error)
    271 //sysnb	Setreuid(ruid int, euid int) (err error)
    272 //sysnb	Setrlimit(which int, lim *Rlimit) (err error)
    273 //sysnb	Setsid() (pid int, err error)
    274 //sysnb	Settimeofday(tp *Timeval) (err error)
    275 //sysnb	Setuid(uid int) (err error)
    276 //sys	Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
    277 //sys	Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
    278 //sys	Symlink(path string, link string) (err error)
    279 //sys	Sync() (err error)
    280 //sys	Truncate(path string, length int64) (err error)
    281 //sys	Umask(newmask int) (oldmask int)
    282 //sys	Undelete(path string) (err error)
    283 //sys	Unlink(path string) (err error)
    284 //sys	Unmount(path string, flags int) (err error)
    285 //sys	write(fd int, p []byte) (n int, err error)
    286 //sys   mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
    287 //sys   munmap(addr uintptr, length uintptr) (err error)
    288 //sys	readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
    289 //sys	writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
    290 
    291 /*
    292  * Unimplemented
    293  */
    294 // Profil
    295 // Sigaction
    296 // Sigprocmask
    297 // Getlogin
    298 // Sigpending
    299 // Sigaltstack
    300 // Ioctl
    301 // Reboot
    302 // Execve
    303 // Vfork
    304 // Sbrk
    305 // Sstk
    306 // Ovadvise
    307 // Mincore
    308 // Setitimer
    309 // Swapon
    310 // Select
    311 // Sigsuspend
    312 // Readv
    313 // Writev
    314 // Nfssvc
    315 // Getfh
    316 // Quotactl
    317 // Mount
    318 // Csops
    319 // Waitid
    320 // Add_profil
    321 // Kdebug_trace
    322 // Sigreturn
    323 // Mmap
    324 // Mlock
    325 // Munlock
    326 // Atsocket
    327 // Kqueue_from_portset_np
    328 // Kqueue_portset
    329 // Getattrlist
    330 // Setattrlist
    331 // Getdirentriesattr
    332 // Searchfs
    333 // Delete
    334 // Copyfile
    335 // Poll
    336 // Watchevent
    337 // Waitevent
    338 // Modwatch
    339 // Getxattr
    340 // Fgetxattr
    341 // Setxattr
    342 // Fsetxattr
    343 // Removexattr
    344 // Fremovexattr
    345 // Listxattr
    346 // Flistxattr
    347 // Fsctl
    348 // Initgroups
    349 // Posix_spawn
    350 // Nfsclnt
    351 // Fhopen
    352 // Minherit
    353 // Semsys
    354 // Msgsys
    355 // Shmsys
    356 // Semctl
    357 // Semget
    358 // Semop
    359 // Msgctl
    360 // Msgget
    361 // Msgsnd
    362 // Msgrcv
    363 // Shmat
    364 // Shmctl
    365 // Shmdt
    366 // Shmget
    367 // Shm_open
    368 // Shm_unlink
    369 // Sem_open
    370 // Sem_close
    371 // Sem_unlink
    372 // Sem_wait
    373 // Sem_trywait
    374 // Sem_post
    375 // Sem_getvalue
    376 // Sem_init
    377 // Sem_destroy
    378 // Open_extended
    379 // Umask_extended
    380 // Stat_extended
    381 // Lstat_extended
    382 // Fstat_extended
    383 // Chmod_extended
    384 // Fchmod_extended
    385 // Access_extended
    386 // Settid
    387 // Gettid
    388 // Setsgroups
    389 // Getsgroups
    390 // Setwgroups
    391 // Getwgroups
    392 // Mkfifo_extended
    393 // Mkdir_extended
    394 // Identitysvc
    395 // Shared_region_check_np
    396 // Shared_region_map_np
    397 // __pthread_mutex_destroy
    398 // __pthread_mutex_init
    399 // __pthread_mutex_lock
    400 // __pthread_mutex_trylock
    401 // __pthread_mutex_unlock
    402 // __pthread_cond_init
    403 // __pthread_cond_destroy
    404 // __pthread_cond_broadcast
    405 // __pthread_cond_signal
    406 // Setsid_with_pid
    407 // __pthread_cond_timedwait
    408 // Aio_fsync
    409 // Aio_return
    410 // Aio_suspend
    411 // Aio_cancel
    412 // Aio_error
    413 // Aio_read
    414 // Aio_write
    415 // Lio_listio
    416 // __pthread_cond_wait
    417 // Iopolicysys
    418 // Mlockall
    419 // Munlockall
    420 // __pthread_kill
    421 // __pthread_sigmask
    422 // __sigwait
    423 // __disable_threadsignal
    424 // __pthread_markcancel
    425 // __pthread_canceled
    426 // __semwait_signal
    427 // Proc_info
    428 // sendfile
    429 // Stat64_extended
    430 // Lstat64_extended
    431 // Fstat64_extended
    432 // __pthread_chdir
    433 // __pthread_fchdir
    434 // Audit
    435 // Auditon
    436 // Getauid
    437 // Setauid
    438 // Getaudit
    439 // Setaudit
    440 // Getaudit_addr
    441 // Setaudit_addr
    442 // Auditctl
    443 // Bsdthread_create
    444 // Bsdthread_terminate
    445 // Stack_snapshot
    446 // Bsdthread_register
    447 // Workq_open
    448 // Workq_ops
    449 // __mac_execve
    450 // __mac_syscall
    451 // __mac_get_file
    452 // __mac_set_file
    453 // __mac_get_link
    454 // __mac_set_link
    455 // __mac_get_proc
    456 // __mac_set_proc
    457 // __mac_get_fd
    458 // __mac_set_fd
    459 // __mac_get_pid
    460 // __mac_get_lcid
    461 // __mac_get_lctx
    462 // __mac_set_lctx
    463 // Setlcid
    464 // Read_nocancel
    465 // Write_nocancel
    466 // Open_nocancel
    467 // Close_nocancel
    468 // Wait4_nocancel
    469 // Recvmsg_nocancel
    470 // Sendmsg_nocancel
    471 // Recvfrom_nocancel
    472 // Accept_nocancel
    473 // Msync_nocancel
    474 // Fcntl_nocancel
    475 // Select_nocancel
    476 // Fsync_nocancel
    477 // Connect_nocancel
    478 // Sigsuspend_nocancel
    479 // Readv_nocancel
    480 // Writev_nocancel
    481 // Sendto_nocancel
    482 // Pread_nocancel
    483 // Pwrite_nocancel
    484 // Waitid_nocancel
    485 // Poll_nocancel
    486 // Msgsnd_nocancel
    487 // Msgrcv_nocancel
    488 // Sem_wait_nocancel
    489 // Aio_suspend_nocancel
    490 // __sigwait_nocancel
    491 // __semwait_signal_nocancel
    492 // __mac_mount
    493 // __mac_get_mount
    494 // __mac_getfsstat
    495