blob: 380be70fad0eb9c2307fa18a83db7d5777d30402 [file] [log] [blame]
Giles Leanad73de22010-03-26 13:23:54 -07001// Copyright 2009,2010 The Go Authors. All rights reserved.
Russ Cox602a4462009-06-01 22:14:57 -07002// 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.
Giles Leanad73de22010-03-26 13:23:54 -07009// 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.
Russ Cox602a4462009-06-01 22:14:57 -070012
13package syscall
14
Brad Fitzpatrick7963ba62013-08-05 12:26:05 -070015import (
16 errorspkg "errors"
17 "unsafe"
18)
19
20const ImplementsGetwd = true
21
22func 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}
Mikio Harac54ed662011-04-07 12:03:45 -070037
Mikio Hara9c97af92011-02-11 14:34:00 -050038type 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
Joel Sing773a9212011-11-17 23:13:49 +110050// Translate "kern.hostname" to []_C_int{0,1,2,3}.
51func 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
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000056 // as the size. I don't know why the +2 is here, but the
Joel Sing773a9212011-11-17 23:13:49 +110057 // 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]))
Alexey Borzenkova1083692012-08-05 17:24:32 -040065 bytes, err := ByteSliceFromString(name)
66 if err != nil {
67 return nil, err
68 }
Joel Sing773a9212011-11-17 23:13:49 +110069
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
Mikio Harac54ed662011-04-07 12:03:45 -070078// ParseDirent parses up to max directory entries in buf,
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000079// appending the names to names. It returns the number
Mikio Harac54ed662011-04-07 12:03:45 -070080// bytes consumed from buf, the number of entries added
81// to names, and the new names slice.
82func 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
Russ Coxc017a822011-11-13 22:44:52 -0500106//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
107func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
108func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
Jeff Hodges0ce57a72011-06-14 12:56:46 -0400109
Brad Fitzpatrick7963ba62013-08-05 12:26:05 -0700110const (
111 attrBitMapCount = 5
112 attrCmnFullpath = 0x08000000
113)
114
115type attrList struct {
116 bitmapCount uint16
117 _ uint16
118 CommonAttr uint32
119 VolAttr uint32
120 DirAttr uint32
121 FileAttr uint32
122 Forkattr uint32
123}
124
125func 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 )
Russ Coxcf622d72014-09-08 16:59:59 -0400146 use(unsafe.Pointer(_p0))
Brad Fitzpatrick7963ba62013-08-05 12:26:05 -0700147 if e1 != 0 {
148 return nil, e1
149 }
150 size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
151
152 // dat is the section of attrBuf that contains valid data,
153 // without the 4 byte length header. All attribute offsets
154 // are relative to dat.
155 dat := attrBuf
156 if int(size) < len(attrBuf) {
157 dat = dat[:size]
158 }
159 dat = dat[4:] // remove length prefix
160
161 for i := uint32(0); int(i) < len(dat); {
162 header := dat[i:]
163 if len(header) < 8 {
164 return attrs, errorspkg.New("truncated attribute header")
165 }
166 datOff := *(*int32)(unsafe.Pointer(&header[0]))
167 attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
168 if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
169 return attrs, errorspkg.New("truncated results; attrBuf too small")
170 }
171 end := uint32(datOff) + attrLen
172 attrs = append(attrs, dat[datOff:end])
173 i = end
174 if r := i % 4; r != 0 {
175 i += (4 - r)
176 }
177 }
178 return
179}
180
Russ Coxc017a822011-11-13 22:44:52 -0500181//sysnb pipe() (r int, w int, err error)
Joel Singeb2b3f52011-07-29 13:47:20 -0400182
Russ Coxc017a822011-11-13 22:44:52 -0500183func Pipe(p []int) (err error) {
Joel Singeb2b3f52011-07-29 13:47:20 -0400184 if len(p) != 2 {
185 return EINVAL
186 }
Russ Coxc017a822011-11-13 22:44:52 -0500187 p[0], p[1], err = pipe()
Joel Singeb2b3f52011-07-29 13:47:20 -0400188 return
189}
190
Preetam Jinka4abbd4a2014-04-10 13:58:03 +1000191func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
192 var _p0 unsafe.Pointer
193 var bufsize uintptr
194 if len(buf) > 0 {
195 _p0 = unsafe.Pointer(&buf[0])
196 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
197 }
198 r0, _, e1 := Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags))
Mikio Hara878e0022016-07-04 08:45:10 +0900199 use(unsafe.Pointer(_p0))
Preetam Jinka4abbd4a2014-04-10 13:58:03 +1000200 n = int(r0)
201 if e1 != 0 {
202 err = e1
203 }
204 return
205}
206
Russ Cox602a4462009-06-01 22:14:57 -0700207/*
Russ Cox602a4462009-06-01 22:14:57 -0700208 * Wrapped
209 */
210
Russ Coxc017a822011-11-13 22:44:52 -0500211//sys kill(pid int, signum int, posix int) (err error)
Russ Coxf2317d32010-02-04 02:06:08 -0800212
Russ Cox35586f72012-02-13 13:52:37 -0500213func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
Russ Coxf2317d32010-02-04 02:06:08 -0800214
Russ Cox602a4462009-06-01 22:14:57 -0700215/*
216 * Exposed directly
217 */
Russ Coxc017a822011-11-13 22:44:52 -0500218//sys Access(path string, mode uint32) (err error)
219//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
220//sys Chdir(path string) (err error)
221//sys Chflags(path string, flags int) (err error)
222//sys Chmod(path string, mode uint32) (err error)
223//sys Chown(path string, uid int, gid int) (err error)
224//sys Chroot(path string) (err error)
225//sys Close(fd int) (err error)
Ian Lance Taylor28074d52015-03-26 08:02:16 -0700226//sys Dup(fd int) (nfd int, err error)
227//sys Dup2(from int, to int) (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500228//sys Exchangedata(path1 string, path2 string, options int) (err error)
Russ Cox602a4462009-06-01 22:14:57 -0700229//sys Exit(code int)
Russ Coxc017a822011-11-13 22:44:52 -0500230//sys Fchdir(fd int) (err error)
Shenghou Ma6de184b2013-05-07 05:20:00 +0800231//sys Fchflags(fd int, flags int) (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500232//sys Fchmod(fd int, mode uint32) (err error)
233//sys Fchown(fd int, uid int, gid int) (err error)
234//sys Flock(fd int, how int) (err error)
235//sys Fpathconf(fd int, name int) (val int, err error)
236//sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
237//sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
238//sys Fsync(fd int) (err error)
239//sys Ftruncate(fd int, length int64) (err error)
240//sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
Russ Cox602a4462009-06-01 22:14:57 -0700241//sys Getdtablesize() (size int)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700242//sysnb Getegid() (egid int)
243//sysnb Geteuid() (uid int)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700244//sysnb Getgid() (gid int)
Russ Coxc017a822011-11-13 22:44:52 -0500245//sysnb Getpgid(pid int) (pgid int, err error)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700246//sysnb Getpgrp() (pgrp int)
247//sysnb Getpid() (pid int)
248//sysnb Getppid() (ppid int)
Russ Coxc017a822011-11-13 22:44:52 -0500249//sys Getpriority(which int, who int) (prio int, err error)
250//sysnb Getrlimit(which int, lim *Rlimit) (err error)
251//sysnb Getrusage(who int, rusage *Rusage) (err error)
252//sysnb Getsid(pid int) (sid int, err error)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700253//sysnb Getuid() (uid int)
254//sysnb Issetugid() (tainted bool)
Russ Coxc017a822011-11-13 22:44:52 -0500255//sys Kqueue() (fd int, err error)
256//sys Lchown(path string, uid int, gid int) (err error)
257//sys Link(path string, link string) (err error)
258//sys Listen(s int, backlog int) (err error)
259//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
260//sys Mkdir(path string, mode uint32) (err error)
261//sys Mkfifo(path string, mode uint32) (err error)
262//sys Mknod(path string, mode uint32, dev int) (err error)
William Orr08706f12014-02-27 10:13:41 -0800263//sys Mlock(b []byte) (err error)
264//sys Mlockall(flags int) (err error)
265//sys Mprotect(b []byte, prot int) (err error)
266//sys Munlock(b []byte) (err error)
267//sys Munlockall() (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500268//sys Open(path string, mode int, perm uint32) (fd int, err error)
269//sys Pathconf(path string, name int) (val int, err error)
270//sys Pread(fd int, p []byte, offset int64) (n int, err error)
271//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400272//sys read(fd int, p []byte) (n int, err error)
Russ Coxc017a822011-11-13 22:44:52 -0500273//sys Readlink(path string, buf []byte) (n int, err error)
274//sys Rename(from string, to string) (err error)
275//sys Revoke(path string) (err error)
276//sys Rmdir(path string) (err error)
277//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
278//sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
279//sys Setegid(egid int) (err error)
280//sysnb Seteuid(euid int) (err error)
281//sysnb Setgid(gid int) (err error)
282//sys Setlogin(name string) (err error)
283//sysnb Setpgid(pid int, pgid int) (err error)
284//sys Setpriority(which int, who int, prio int) (err error)
285//sys Setprivexec(flag int) (err error)
286//sysnb Setregid(rgid int, egid int) (err error)
287//sysnb Setreuid(ruid int, euid int) (err error)
288//sysnb Setrlimit(which int, lim *Rlimit) (err error)
289//sysnb Setsid() (pid int, err error)
290//sysnb Settimeofday(tp *Timeval) (err error)
291//sysnb Setuid(uid int) (err error)
292//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
293//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
294//sys Symlink(path string, link string) (err error)
295//sys Sync() (err error)
296//sys Truncate(path string, length int64) (err error)
Giles Lean5aa3a8d2010-02-16 11:43:25 -0800297//sys Umask(newmask int) (oldmask int)
Russ Coxc017a822011-11-13 22:44:52 -0500298//sys Undelete(path string) (err error)
299//sys Unlink(path string) (err error)
300//sys Unmount(path string, flags int) (err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400301//sys write(fd int, p []byte) (n int, err error)
Russ Coxc017a822011-11-13 22:44:52 -0500302//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
303//sys munmap(addr uintptr, length uintptr) (err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400304//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
305//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
Russ Cox602a4462009-06-01 22:14:57 -0700306
Russ Cox602a4462009-06-01 22:14:57 -0700307/*
308 * Unimplemented
309 */
310// Profil
311// Sigaction
312// Sigprocmask
313// Getlogin
314// Sigpending
315// Sigaltstack
316// Ioctl
317// Reboot
318// Execve
319// Vfork
320// Sbrk
321// Sstk
322// Ovadvise
323// Mincore
324// Setitimer
325// Swapon
326// Select
327// Sigsuspend
328// Readv
329// Writev
330// Nfssvc
331// Getfh
332// Quotactl
333// Mount
334// Csops
335// Waitid
336// Add_profil
337// Kdebug_trace
338// Sigreturn
339// Mmap
Russ Cox602a4462009-06-01 22:14:57 -0700340// Mlock
341// Munlock
342// Atsocket
343// Kqueue_from_portset_np
344// Kqueue_portset
345// Getattrlist
346// Setattrlist
347// Getdirentriesattr
348// Searchfs
349// Delete
350// Copyfile
351// Poll
352// Watchevent
353// Waitevent
354// Modwatch
355// Getxattr
356// Fgetxattr
357// Setxattr
358// Fsetxattr
359// Removexattr
360// Fremovexattr
361// Listxattr
362// Flistxattr
363// Fsctl
364// Initgroups
365// Posix_spawn
366// Nfsclnt
367// Fhopen
368// Minherit
369// Semsys
370// Msgsys
371// Shmsys
372// Semctl
373// Semget
374// Semop
375// Msgctl
376// Msgget
377// Msgsnd
378// Msgrcv
379// Shmat
380// Shmctl
381// Shmdt
382// Shmget
383// Shm_open
384// Shm_unlink
385// Sem_open
386// Sem_close
387// Sem_unlink
388// Sem_wait
389// Sem_trywait
390// Sem_post
391// Sem_getvalue
392// Sem_init
393// Sem_destroy
394// Open_extended
395// Umask_extended
396// Stat_extended
397// Lstat_extended
398// Fstat_extended
399// Chmod_extended
400// Fchmod_extended
401// Access_extended
402// Settid
403// Gettid
404// Setsgroups
405// Getsgroups
406// Setwgroups
407// Getwgroups
408// Mkfifo_extended
409// Mkdir_extended
410// Identitysvc
411// Shared_region_check_np
412// Shared_region_map_np
413// __pthread_mutex_destroy
414// __pthread_mutex_init
415// __pthread_mutex_lock
416// __pthread_mutex_trylock
417// __pthread_mutex_unlock
418// __pthread_cond_init
419// __pthread_cond_destroy
420// __pthread_cond_broadcast
421// __pthread_cond_signal
422// Setsid_with_pid
423// __pthread_cond_timedwait
424// Aio_fsync
425// Aio_return
426// Aio_suspend
427// Aio_cancel
428// Aio_error
429// Aio_read
430// Aio_write
431// Lio_listio
432// __pthread_cond_wait
433// Iopolicysys
434// Mlockall
435// Munlockall
436// __pthread_kill
437// __pthread_sigmask
438// __sigwait
439// __disable_threadsignal
440// __pthread_markcancel
441// __pthread_canceled
442// __semwait_signal
443// Proc_info
Dmitriy Vyukovc242aa32012-10-29 23:15:06 +0400444// sendfile
Russ Cox602a4462009-06-01 22:14:57 -0700445// Stat64_extended
446// Lstat64_extended
447// Fstat64_extended
448// __pthread_chdir
449// __pthread_fchdir
450// Audit
451// Auditon
452// Getauid
453// Setauid
454// Getaudit
455// Setaudit
456// Getaudit_addr
457// Setaudit_addr
458// Auditctl
459// Bsdthread_create
460// Bsdthread_terminate
461// Stack_snapshot
462// Bsdthread_register
463// Workq_open
464// Workq_ops
465// __mac_execve
466// __mac_syscall
467// __mac_get_file
468// __mac_set_file
469// __mac_get_link
470// __mac_set_link
471// __mac_get_proc
472// __mac_set_proc
473// __mac_get_fd
474// __mac_set_fd
475// __mac_get_pid
476// __mac_get_lcid
477// __mac_get_lctx
478// __mac_set_lctx
479// Setlcid
480// Read_nocancel
481// Write_nocancel
482// Open_nocancel
483// Close_nocancel
484// Wait4_nocancel
485// Recvmsg_nocancel
486// Sendmsg_nocancel
487// Recvfrom_nocancel
488// Accept_nocancel
489// Msync_nocancel
490// Fcntl_nocancel
491// Select_nocancel
492// Fsync_nocancel
493// Connect_nocancel
494// Sigsuspend_nocancel
495// Readv_nocancel
496// Writev_nocancel
497// Sendto_nocancel
498// Pread_nocancel
499// Pwrite_nocancel
500// Waitid_nocancel
501// Poll_nocancel
502// Msgsnd_nocancel
503// Msgrcv_nocancel
504// Sem_wait_nocancel
505// Aio_suspend_nocancel
506// __sigwait_nocancel
507// __semwait_signal_nocancel
508// __mac_mount
509// __mac_get_mount
510// __mac_getfsstat