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