2010-03-26 14:23:54 -06:00
|
|
|
// Copyright 2009,2010 The Go Authors. All rights reserved.
|
2009-06-01 23:14:57 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Darwin system calls.
|
|
|
|
// This file is compiled as ordinary Go code,
|
|
|
|
// but it is also input to mksyscall,
|
|
|
|
// which parses the //sys lines and generates system call stubs.
|
2010-03-26 14:23:54 -06:00
|
|
|
// Note that sometimes we use a lowercase //sys name and wrap
|
|
|
|
// it in our own nicer implementation, either here or in
|
|
|
|
// syscall_bsd.go or syscall_unix.go.
|
2009-06-01 23:14:57 -06:00
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
2011-04-07 13:03:45 -06:00
|
|
|
import "unsafe"
|
|
|
|
|
2011-02-11 12:34:00 -07:00
|
|
|
type SockaddrDatalink struct {
|
|
|
|
Len uint8
|
|
|
|
Family uint8
|
|
|
|
Index uint16
|
|
|
|
Type uint8
|
|
|
|
Nlen uint8
|
|
|
|
Alen uint8
|
|
|
|
Slen uint8
|
|
|
|
Data [12]int8
|
|
|
|
raw RawSockaddrDatalink
|
|
|
|
}
|
|
|
|
|
2011-11-17 05:13:49 -07:00
|
|
|
// Translate "kern.hostname" to []_C_int{0,1,2,3}.
|
|
|
|
func nametomib(name string) (mib []_C_int, err error) {
|
|
|
|
const siz = unsafe.Sizeof(mib[0])
|
|
|
|
|
|
|
|
// NOTE(rsc): It seems strange to set the buffer to have
|
|
|
|
// size CTL_MAXNAME+2 but use only CTL_MAXNAME
|
|
|
|
// as the size. I don't know why the +2 is here, but the
|
|
|
|
// kernel uses +2 for its own implementation of this function.
|
|
|
|
// I am scared that if we don't include the +2 here, the kernel
|
|
|
|
// will silently write 2 words farther than we specify
|
|
|
|
// and we'll get memory corruption.
|
|
|
|
var buf [CTL_MAXNAME + 2]_C_int
|
|
|
|
n := uintptr(CTL_MAXNAME) * siz
|
|
|
|
|
|
|
|
p := (*byte)(unsafe.Pointer(&buf[0]))
|
syscall: return EINVAL when string arguments have NUL characters
Since NUL usually terminates strings in underlying syscalls, allowing
it when converting string arguments is a security risk, especially
when dealing with filenames. For example, a program might reason that
filename like "/root/..\x00/" is a subdirectory or "/root/" and allow
access to it, while underlying syscall will treat "\x00" as an end of
that string and the actual filename will be "/root/..", which might
be unexpected. Returning EINVAL when string arguments have NUL in
them makes sure this attack vector is unusable.
R=golang-dev, r, bradfitz, fullung, rsc, minux.ma
CC=golang-dev
https://golang.org/cl/6458050
2012-08-05 15:24:32 -06:00
|
|
|
bytes, err := ByteSliceFromString(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-11-17 05:13:49 -07:00
|
|
|
|
|
|
|
// Magic sysctl: "setting" 0.3 to a string name
|
|
|
|
// lets you read back the array of integers form.
|
|
|
|
if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf[0 : n/siz], nil
|
|
|
|
}
|
|
|
|
|
2011-04-07 13:03:45 -06:00
|
|
|
// ParseDirent parses up to max directory entries in buf,
|
|
|
|
// appending the names to names. It returns the number
|
|
|
|
// bytes consumed from buf, the number of entries added
|
|
|
|
// to names, and the new names slice.
|
|
|
|
func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string) {
|
|
|
|
origlen := len(buf)
|
|
|
|
for max != 0 && len(buf) > 0 {
|
|
|
|
dirent := (*Dirent)(unsafe.Pointer(&buf[0]))
|
|
|
|
if dirent.Reclen == 0 {
|
|
|
|
buf = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buf = buf[dirent.Reclen:]
|
|
|
|
if dirent.Ino == 0 { // File absent in directory.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
|
|
|
|
var name = string(bytes[0:dirent.Namlen])
|
|
|
|
if name == "." || name == ".." { // Useless names
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
max--
|
|
|
|
count++
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return origlen - len(buf), count, names
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
|
|
|
|
func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
|
|
|
|
func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
|
2011-06-14 10:56:46 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sysnb pipe() (r int, w int, err error)
|
2011-07-29 11:47:20 -06:00
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
func Pipe(p []int) (err error) {
|
2011-07-29 11:47:20 -06:00
|
|
|
if len(p) != 2 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
p[0], p[1], err = pipe()
|
2011-07-29 11:47:20 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-05-20 12:51:31 -06:00
|
|
|
// TODO
|
2011-11-13 20:44:52 -07:00
|
|
|
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
|
2011-05-20 12:51:31 -06:00
|
|
|
return -1, ENOSYS
|
|
|
|
}
|
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
/*
|
|
|
|
* Wrapped
|
|
|
|
*/
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys kill(pid int, signum int, posix int) (err error)
|
2010-02-04 03:06:08 -07:00
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
|
2010-02-04 03:06:08 -07:00
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
/*
|
|
|
|
* Exposed directly
|
|
|
|
*/
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Access(path string, mode uint32) (err error)
|
|
|
|
//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
|
|
|
|
//sys Chdir(path string) (err error)
|
|
|
|
//sys Chflags(path string, flags int) (err error)
|
|
|
|
//sys Chmod(path string, mode uint32) (err error)
|
|
|
|
//sys Chown(path string, uid int, gid int) (err error)
|
|
|
|
//sys Chroot(path string) (err error)
|
|
|
|
//sys Close(fd int) (err error)
|
|
|
|
//sysnb Dup(fd int) (nfd int, err error)
|
|
|
|
//sysnb Dup2(from int, to int) (err error)
|
|
|
|
//sys Exchangedata(path1 string, path2 string, options int) (err error)
|
2009-06-01 23:14:57 -06:00
|
|
|
//sys Exit(code int)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Fchdir(fd int) (err error)
|
|
|
|
//sys Fchflags(path string, flags int) (err error)
|
|
|
|
//sys Fchmod(fd int, mode uint32) (err error)
|
|
|
|
//sys Fchown(fd int, uid int, gid int) (err error)
|
|
|
|
//sys Flock(fd int, how int) (err error)
|
|
|
|
//sys Fpathconf(fd int, name int) (val int, err error)
|
|
|
|
//sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
|
|
|
|
//sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
|
|
|
|
//sys Fsync(fd int) (err error)
|
|
|
|
//sys Ftruncate(fd int, length int64) (err error)
|
|
|
|
//sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
|
2009-06-01 23:14:57 -06:00
|
|
|
//sys Getdtablesize() (size int)
|
2011-03-16 20:03:01 -06:00
|
|
|
//sysnb Getegid() (egid int)
|
|
|
|
//sysnb Geteuid() (uid int)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Getfsstat(buf []Statfs_t, flags int) (n int, err error) = SYS_GETFSSTAT64
|
2011-03-16 20:03:01 -06:00
|
|
|
//sysnb Getgid() (gid int)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sysnb Getpgid(pid int) (pgid int, err error)
|
2011-03-16 20:03:01 -06:00
|
|
|
//sysnb Getpgrp() (pgrp int)
|
|
|
|
//sysnb Getpid() (pid int)
|
|
|
|
//sysnb Getppid() (ppid int)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Getpriority(which int, who int) (prio int, err error)
|
|
|
|
//sysnb Getrlimit(which int, lim *Rlimit) (err error)
|
|
|
|
//sysnb Getrusage(who int, rusage *Rusage) (err error)
|
|
|
|
//sysnb Getsid(pid int) (sid int, err error)
|
2011-03-16 20:03:01 -06:00
|
|
|
//sysnb Getuid() (uid int)
|
|
|
|
//sysnb Issetugid() (tainted bool)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Kqueue() (fd int, err error)
|
|
|
|
//sys Lchown(path string, uid int, gid int) (err error)
|
|
|
|
//sys Link(path string, link string) (err error)
|
|
|
|
//sys Listen(s int, backlog int) (err error)
|
|
|
|
//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
|
|
|
|
//sys Mkdir(path string, mode uint32) (err error)
|
|
|
|
//sys Mkfifo(path string, mode uint32) (err error)
|
|
|
|
//sys Mknod(path string, mode uint32, dev int) (err error)
|
|
|
|
//sys Open(path string, mode int, perm uint32) (fd int, err error)
|
|
|
|
//sys Pathconf(path string, name int) (val int, err error)
|
|
|
|
//sys Pread(fd int, p []byte, offset int64) (n int, err error)
|
|
|
|
//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
|
2012-10-09 10:51:58 -06:00
|
|
|
//sys read(fd int, p []byte) (n int, err error)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Readlink(path string, buf []byte) (n int, err error)
|
|
|
|
//sys Rename(from string, to string) (err error)
|
|
|
|
//sys Revoke(path string) (err error)
|
|
|
|
//sys Rmdir(path string) (err error)
|
|
|
|
//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
|
|
|
|
//sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
|
|
|
|
//sys Setegid(egid int) (err error)
|
|
|
|
//sysnb Seteuid(euid int) (err error)
|
|
|
|
//sysnb Setgid(gid int) (err error)
|
|
|
|
//sys Setlogin(name string) (err error)
|
|
|
|
//sysnb Setpgid(pid int, pgid int) (err error)
|
|
|
|
//sys Setpriority(which int, who int, prio int) (err error)
|
|
|
|
//sys Setprivexec(flag int) (err error)
|
|
|
|
//sysnb Setregid(rgid int, egid int) (err error)
|
|
|
|
//sysnb Setreuid(ruid int, euid int) (err error)
|
|
|
|
//sysnb Setrlimit(which int, lim *Rlimit) (err error)
|
|
|
|
//sysnb Setsid() (pid int, err error)
|
|
|
|
//sysnb Settimeofday(tp *Timeval) (err error)
|
|
|
|
//sysnb Setuid(uid int) (err error)
|
|
|
|
//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
|
|
|
|
//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
|
|
|
|
//sys Symlink(path string, link string) (err error)
|
|
|
|
//sys Sync() (err error)
|
|
|
|
//sys Truncate(path string, length int64) (err error)
|
2010-02-16 12:43:25 -07:00
|
|
|
//sys Umask(newmask int) (oldmask int)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys Undelete(path string) (err error)
|
|
|
|
//sys Unlink(path string) (err error)
|
|
|
|
//sys Unmount(path string, flags int) (err error)
|
2012-10-09 10:51:58 -06:00
|
|
|
//sys write(fd int, p []byte) (n int, err error)
|
2011-11-13 20:44:52 -07:00
|
|
|
//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
|
|
|
|
//sys munmap(addr uintptr, length uintptr) (err error)
|
2012-10-09 10:51:58 -06:00
|
|
|
//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
|
|
|
|
//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
|
2009-06-01 23:14:57 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unimplemented
|
|
|
|
*/
|
|
|
|
// Profil
|
|
|
|
// Sigaction
|
|
|
|
// Sigprocmask
|
|
|
|
// Getlogin
|
|
|
|
// Sigpending
|
|
|
|
// Sigaltstack
|
|
|
|
// Ioctl
|
|
|
|
// Reboot
|
|
|
|
// Execve
|
|
|
|
// Vfork
|
|
|
|
// Sbrk
|
|
|
|
// Sstk
|
|
|
|
// Ovadvise
|
|
|
|
// Mincore
|
|
|
|
// Setitimer
|
|
|
|
// Swapon
|
|
|
|
// Select
|
|
|
|
// Sigsuspend
|
|
|
|
// Readv
|
|
|
|
// Writev
|
|
|
|
// Nfssvc
|
|
|
|
// Getfh
|
|
|
|
// Quotactl
|
|
|
|
// Mount
|
|
|
|
// Csops
|
|
|
|
// Waitid
|
|
|
|
// Add_profil
|
|
|
|
// Kdebug_trace
|
|
|
|
// Sigreturn
|
|
|
|
// Mmap
|
|
|
|
// Mlock
|
|
|
|
// Munlock
|
|
|
|
// Atsocket
|
|
|
|
// Kqueue_from_portset_np
|
|
|
|
// Kqueue_portset
|
|
|
|
// Getattrlist
|
|
|
|
// Setattrlist
|
|
|
|
// Getdirentriesattr
|
|
|
|
// Searchfs
|
|
|
|
// Delete
|
|
|
|
// Copyfile
|
|
|
|
// Poll
|
|
|
|
// Watchevent
|
|
|
|
// Waitevent
|
|
|
|
// Modwatch
|
|
|
|
// Getxattr
|
|
|
|
// Fgetxattr
|
|
|
|
// Setxattr
|
|
|
|
// Fsetxattr
|
|
|
|
// Removexattr
|
|
|
|
// Fremovexattr
|
|
|
|
// Listxattr
|
|
|
|
// Flistxattr
|
|
|
|
// Fsctl
|
|
|
|
// Initgroups
|
|
|
|
// Posix_spawn
|
|
|
|
// Nfsclnt
|
|
|
|
// Fhopen
|
|
|
|
// Minherit
|
|
|
|
// Semsys
|
|
|
|
// Msgsys
|
|
|
|
// Shmsys
|
|
|
|
// Semctl
|
|
|
|
// Semget
|
|
|
|
// Semop
|
|
|
|
// Msgctl
|
|
|
|
// Msgget
|
|
|
|
// Msgsnd
|
|
|
|
// Msgrcv
|
|
|
|
// Shmat
|
|
|
|
// Shmctl
|
|
|
|
// Shmdt
|
|
|
|
// Shmget
|
|
|
|
// Shm_open
|
|
|
|
// Shm_unlink
|
|
|
|
// Sem_open
|
|
|
|
// Sem_close
|
|
|
|
// Sem_unlink
|
|
|
|
// Sem_wait
|
|
|
|
// Sem_trywait
|
|
|
|
// Sem_post
|
|
|
|
// Sem_getvalue
|
|
|
|
// Sem_init
|
|
|
|
// Sem_destroy
|
|
|
|
// Open_extended
|
|
|
|
// Umask_extended
|
|
|
|
// Stat_extended
|
|
|
|
// Lstat_extended
|
|
|
|
// Fstat_extended
|
|
|
|
// Chmod_extended
|
|
|
|
// Fchmod_extended
|
|
|
|
// Access_extended
|
|
|
|
// Settid
|
|
|
|
// Gettid
|
|
|
|
// Setsgroups
|
|
|
|
// Getsgroups
|
|
|
|
// Setwgroups
|
|
|
|
// Getwgroups
|
|
|
|
// Mkfifo_extended
|
|
|
|
// Mkdir_extended
|
|
|
|
// Identitysvc
|
|
|
|
// Shared_region_check_np
|
|
|
|
// Shared_region_map_np
|
|
|
|
// __pthread_mutex_destroy
|
|
|
|
// __pthread_mutex_init
|
|
|
|
// __pthread_mutex_lock
|
|
|
|
// __pthread_mutex_trylock
|
|
|
|
// __pthread_mutex_unlock
|
|
|
|
// __pthread_cond_init
|
|
|
|
// __pthread_cond_destroy
|
|
|
|
// __pthread_cond_broadcast
|
|
|
|
// __pthread_cond_signal
|
|
|
|
// Setsid_with_pid
|
|
|
|
// __pthread_cond_timedwait
|
|
|
|
// Aio_fsync
|
|
|
|
// Aio_return
|
|
|
|
// Aio_suspend
|
|
|
|
// Aio_cancel
|
|
|
|
// Aio_error
|
|
|
|
// Aio_read
|
|
|
|
// Aio_write
|
|
|
|
// Lio_listio
|
|
|
|
// __pthread_cond_wait
|
|
|
|
// Iopolicysys
|
|
|
|
// Mlockall
|
|
|
|
// Munlockall
|
|
|
|
// __pthread_kill
|
|
|
|
// __pthread_sigmask
|
|
|
|
// __sigwait
|
|
|
|
// __disable_threadsignal
|
|
|
|
// __pthread_markcancel
|
|
|
|
// __pthread_canceled
|
|
|
|
// __semwait_signal
|
|
|
|
// Proc_info
|
|
|
|
// Sendfile
|
|
|
|
// Stat64_extended
|
|
|
|
// Lstat64_extended
|
|
|
|
// Fstat64_extended
|
|
|
|
// __pthread_chdir
|
|
|
|
// __pthread_fchdir
|
|
|
|
// Audit
|
|
|
|
// Auditon
|
|
|
|
// Getauid
|
|
|
|
// Setauid
|
|
|
|
// Getaudit
|
|
|
|
// Setaudit
|
|
|
|
// Getaudit_addr
|
|
|
|
// Setaudit_addr
|
|
|
|
// Auditctl
|
|
|
|
// Bsdthread_create
|
|
|
|
// Bsdthread_terminate
|
|
|
|
// Stack_snapshot
|
|
|
|
// Bsdthread_register
|
|
|
|
// Workq_open
|
|
|
|
// Workq_ops
|
|
|
|
// __mac_execve
|
|
|
|
// __mac_syscall
|
|
|
|
// __mac_get_file
|
|
|
|
// __mac_set_file
|
|
|
|
// __mac_get_link
|
|
|
|
// __mac_set_link
|
|
|
|
// __mac_get_proc
|
|
|
|
// __mac_set_proc
|
|
|
|
// __mac_get_fd
|
|
|
|
// __mac_set_fd
|
|
|
|
// __mac_get_pid
|
|
|
|
// __mac_get_lcid
|
|
|
|
// __mac_get_lctx
|
|
|
|
// __mac_set_lctx
|
|
|
|
// Setlcid
|
|
|
|
// Read_nocancel
|
|
|
|
// Write_nocancel
|
|
|
|
// Open_nocancel
|
|
|
|
// Close_nocancel
|
|
|
|
// Wait4_nocancel
|
|
|
|
// Recvmsg_nocancel
|
|
|
|
// Sendmsg_nocancel
|
|
|
|
// Recvfrom_nocancel
|
|
|
|
// Accept_nocancel
|
|
|
|
// Msync_nocancel
|
|
|
|
// Fcntl_nocancel
|
|
|
|
// Select_nocancel
|
|
|
|
// Fsync_nocancel
|
|
|
|
// Connect_nocancel
|
|
|
|
// Sigsuspend_nocancel
|
|
|
|
// Readv_nocancel
|
|
|
|
// Writev_nocancel
|
|
|
|
// Sendto_nocancel
|
|
|
|
// Pread_nocancel
|
|
|
|
// Pwrite_nocancel
|
|
|
|
// Waitid_nocancel
|
|
|
|
// Poll_nocancel
|
|
|
|
// Msgsnd_nocancel
|
|
|
|
// Msgrcv_nocancel
|
|
|
|
// Sem_wait_nocancel
|
|
|
|
// Aio_suspend_nocancel
|
|
|
|
// __sigwait_nocancel
|
|
|
|
// __semwait_signal_nocancel
|
|
|
|
// __mac_mount
|
|
|
|
// __mac_get_mount
|
|
|
|
// __mac_getfsstat
|