blob: cf21ec46886709b18f77324e2a92186e2ea4a9ee [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
Damien Neilf5f7d6e2016-06-03 15:04:53 -070078func direntIno(buf []byte) (uint64, bool) {
79 return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
80}
81
82func direntReclen(buf []byte) (uint64, bool) {
83 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
84}
85
86func direntNamlen(buf []byte) (uint64, bool) {
87 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
Mikio Harac54ed662011-04-07 12:03:45 -070088}
89
Russ Coxc017a822011-11-13 22:44:52 -050090//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
91func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
92func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
Jeff Hodges0ce57a72011-06-14 12:56:46 -040093
Brad Fitzpatrick7963ba62013-08-05 12:26:05 -070094const (
95 attrBitMapCount = 5
Evan Joneseb2b0ed2017-11-01 12:44:14 -040096 attrCmnModtime = 0x00000400
97 attrCmnAcctime = 0x00001000
Brad Fitzpatrick7963ba62013-08-05 12:26:05 -070098 attrCmnFullpath = 0x08000000
99)
100
101type attrList struct {
102 bitmapCount uint16
103 _ uint16
104 CommonAttr uint32
105 VolAttr uint32
106 DirAttr uint32
107 FileAttr uint32
108 Forkattr uint32
109}
110
111func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
112 if len(attrBuf) < 4 {
113 return nil, errorspkg.New("attrBuf too small")
114 }
115 attrList.bitmapCount = attrBitMapCount
116
117 var _p0 *byte
118 _p0, err = BytePtrFromString(path)
119 if err != nil {
120 return nil, err
121 }
122
123 _, _, e1 := Syscall6(
124 SYS_GETATTRLIST,
125 uintptr(unsafe.Pointer(_p0)),
126 uintptr(unsafe.Pointer(&attrList)),
127 uintptr(unsafe.Pointer(&attrBuf[0])),
128 uintptr(len(attrBuf)),
129 uintptr(options),
130 0,
131 )
132 if e1 != 0 {
133 return nil, e1
134 }
135 size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
136
137 // dat is the section of attrBuf that contains valid data,
138 // without the 4 byte length header. All attribute offsets
139 // are relative to dat.
140 dat := attrBuf
141 if int(size) < len(attrBuf) {
142 dat = dat[:size]
143 }
144 dat = dat[4:] // remove length prefix
145
146 for i := uint32(0); int(i) < len(dat); {
147 header := dat[i:]
148 if len(header) < 8 {
149 return attrs, errorspkg.New("truncated attribute header")
150 }
151 datOff := *(*int32)(unsafe.Pointer(&header[0]))
152 attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
153 if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
154 return attrs, errorspkg.New("truncated results; attrBuf too small")
155 }
156 end := uint32(datOff) + attrLen
157 attrs = append(attrs, dat[datOff:end])
158 i = end
159 if r := i % 4; r != 0 {
160 i += (4 - r)
161 }
162 }
163 return
164}
165
Russ Coxc017a822011-11-13 22:44:52 -0500166//sysnb pipe() (r int, w int, err error)
Joel Singeb2b3f52011-07-29 13:47:20 -0400167
Russ Coxc017a822011-11-13 22:44:52 -0500168func Pipe(p []int) (err error) {
Joel Singeb2b3f52011-07-29 13:47:20 -0400169 if len(p) != 2 {
170 return EINVAL
171 }
Russ Coxc017a822011-11-13 22:44:52 -0500172 p[0], p[1], err = pipe()
Joel Singeb2b3f52011-07-29 13:47:20 -0400173 return
174}
175
Preetam Jinka4abbd4a2014-04-10 13:58:03 +1000176func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
177 var _p0 unsafe.Pointer
178 var bufsize uintptr
179 if len(buf) > 0 {
180 _p0 = unsafe.Pointer(&buf[0])
181 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
182 }
183 r0, _, e1 := Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags))
184 n = int(r0)
185 if e1 != 0 {
186 err = e1
187 }
188 return
189}
190
Evan Joneseb2b0ed2017-11-01 12:44:14 -0400191func setattrlistTimes(path string, times []Timespec) error {
192 _p0, err := BytePtrFromString(path)
193 if err != nil {
194 return err
195 }
196
197 var attrList attrList
198 attrList.bitmapCount = attrBitMapCount
199 attrList.CommonAttr = attrCmnModtime | attrCmnAcctime
200
201 // order is mtime, atime: the opposite of Chtimes
202 attributes := [2]Timespec{times[1], times[0]}
203 const options = 0
204 _, _, e1 := Syscall6(
205 SYS_SETATTRLIST,
206 uintptr(unsafe.Pointer(_p0)),
207 uintptr(unsafe.Pointer(&attrList)),
208 uintptr(unsafe.Pointer(&attributes)),
209 uintptr(unsafe.Sizeof(attributes)),
210 uintptr(options),
211 0,
212 )
213 if e1 != 0 {
214 return e1
215 }
216 return nil
217}
218
Yuval Pavel Zholkover0b069292017-08-14 19:05:56 +0300219func utimensat(dirfd int, path string, times *[2]Timespec, flag int) error {
Tobias Klauser67c36052017-08-11 16:49:01 +0200220 // Darwin doesn't support SYS_UTIMENSAT
221 return ENOSYS
222}
223
Russ Cox602a4462009-06-01 22:14:57 -0700224/*
Russ Cox602a4462009-06-01 22:14:57 -0700225 * Wrapped
226 */
227
Russ Coxc017a822011-11-13 22:44:52 -0500228//sys kill(pid int, signum int, posix int) (err error)
Russ Coxf2317d32010-02-04 02:06:08 -0800229
Russ Cox35586f72012-02-13 13:52:37 -0500230func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
Russ Coxf2317d32010-02-04 02:06:08 -0800231
Russ Cox602a4462009-06-01 22:14:57 -0700232/*
233 * Exposed directly
234 */
Russ Coxc017a822011-11-13 22:44:52 -0500235//sys Access(path string, mode uint32) (err error)
236//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
237//sys Chdir(path string) (err error)
238//sys Chflags(path string, flags int) (err error)
239//sys Chmod(path string, mode uint32) (err error)
240//sys Chown(path string, uid int, gid int) (err error)
241//sys Chroot(path string) (err error)
242//sys Close(fd int) (err error)
Ian Lance Taylor28074d52015-03-26 08:02:16 -0700243//sys Dup(fd int) (nfd int, err error)
244//sys Dup2(from int, to int) (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500245//sys Exchangedata(path1 string, path2 string, options int) (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500246//sys Fchdir(fd int) (err error)
Shenghou Ma6de184b2013-05-07 05:20:00 +0800247//sys Fchflags(fd int, flags int) (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500248//sys Fchmod(fd int, mode uint32) (err error)
249//sys Fchown(fd int, uid int, gid int) (err error)
250//sys Flock(fd int, how int) (err error)
251//sys Fpathconf(fd int, name int) (val int, err error)
252//sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
253//sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
254//sys Fsync(fd int) (err error)
255//sys Ftruncate(fd int, length int64) (err error)
256//sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
Russ Cox602a4462009-06-01 22:14:57 -0700257//sys Getdtablesize() (size int)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700258//sysnb Getegid() (egid int)
259//sysnb Geteuid() (uid int)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700260//sysnb Getgid() (gid int)
Russ Coxc017a822011-11-13 22:44:52 -0500261//sysnb Getpgid(pid int) (pgid int, err error)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700262//sysnb Getpgrp() (pgrp int)
263//sysnb Getpid() (pid int)
264//sysnb Getppid() (ppid int)
Russ Coxc017a822011-11-13 22:44:52 -0500265//sys Getpriority(which int, who int) (prio int, err error)
266//sysnb Getrlimit(which int, lim *Rlimit) (err error)
267//sysnb Getrusage(who int, rusage *Rusage) (err error)
268//sysnb Getsid(pid int) (sid int, err error)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700269//sysnb Getuid() (uid int)
270//sysnb Issetugid() (tainted bool)
Russ Coxc017a822011-11-13 22:44:52 -0500271//sys Kqueue() (fd int, err error)
272//sys Lchown(path string, uid int, gid int) (err error)
273//sys Link(path string, link string) (err error)
274//sys Listen(s int, backlog int) (err error)
275//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
276//sys Mkdir(path string, mode uint32) (err error)
277//sys Mkfifo(path string, mode uint32) (err error)
278//sys Mknod(path string, mode uint32, dev int) (err error)
William Orr08706f12014-02-27 10:13:41 -0800279//sys Mlock(b []byte) (err error)
280//sys Mlockall(flags int) (err error)
281//sys Mprotect(b []byte, prot int) (err error)
282//sys Munlock(b []byte) (err error)
283//sys Munlockall() (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500284//sys Open(path string, mode int, perm uint32) (fd int, err error)
285//sys Pathconf(path string, name int) (val int, err error)
286//sys Pread(fd int, p []byte, offset int64) (n int, err error)
287//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400288//sys read(fd int, p []byte) (n int, err error)
Russ Coxc017a822011-11-13 22:44:52 -0500289//sys Readlink(path string, buf []byte) (n int, err error)
290//sys Rename(from string, to string) (err error)
291//sys Revoke(path string) (err error)
292//sys Rmdir(path string) (err error)
293//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
294//sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
295//sys Setegid(egid int) (err error)
296//sysnb Seteuid(euid int) (err error)
297//sysnb Setgid(gid int) (err error)
298//sys Setlogin(name string) (err error)
299//sysnb Setpgid(pid int, pgid int) (err error)
300//sys Setpriority(which int, who int, prio int) (err error)
301//sys Setprivexec(flag int) (err error)
302//sysnb Setregid(rgid int, egid int) (err error)
303//sysnb Setreuid(ruid int, euid int) (err error)
304//sysnb Setrlimit(which int, lim *Rlimit) (err error)
305//sysnb Setsid() (pid int, err error)
306//sysnb Settimeofday(tp *Timeval) (err error)
307//sysnb Setuid(uid int) (err error)
308//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
309//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
310//sys Symlink(path string, link string) (err error)
311//sys Sync() (err error)
312//sys Truncate(path string, length int64) (err error)
Giles Lean5aa3a8d2010-02-16 11:43:25 -0800313//sys Umask(newmask int) (oldmask int)
Russ Coxc017a822011-11-13 22:44:52 -0500314//sys Undelete(path string) (err error)
315//sys Unlink(path string) (err error)
316//sys Unmount(path string, flags int) (err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400317//sys write(fd int, p []byte) (n int, err error)
Russ Coxc017a822011-11-13 22:44:52 -0500318//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
319//sys munmap(addr uintptr, length uintptr) (err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400320//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
321//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
Russ Cox602a4462009-06-01 22:14:57 -0700322
Russ Cox602a4462009-06-01 22:14:57 -0700323/*
324 * Unimplemented
325 */
326// Profil
327// Sigaction
328// Sigprocmask
329// Getlogin
330// Sigpending
331// Sigaltstack
332// Ioctl
333// Reboot
334// Execve
335// Vfork
336// Sbrk
337// Sstk
338// Ovadvise
339// Mincore
340// Setitimer
341// Swapon
342// Select
343// Sigsuspend
344// Readv
345// Writev
346// Nfssvc
347// Getfh
348// Quotactl
349// Mount
350// Csops
351// Waitid
352// Add_profil
353// Kdebug_trace
354// Sigreturn
355// Mmap
Russ Cox602a4462009-06-01 22:14:57 -0700356// Mlock
357// Munlock
358// Atsocket
359// Kqueue_from_portset_np
360// Kqueue_portset
361// Getattrlist
362// Setattrlist
363// Getdirentriesattr
364// Searchfs
365// Delete
366// Copyfile
367// Poll
368// Watchevent
369// Waitevent
370// Modwatch
371// Getxattr
372// Fgetxattr
373// Setxattr
374// Fsetxattr
375// Removexattr
376// Fremovexattr
377// Listxattr
378// Flistxattr
379// Fsctl
380// Initgroups
381// Posix_spawn
382// Nfsclnt
383// Fhopen
384// Minherit
385// Semsys
386// Msgsys
387// Shmsys
388// Semctl
389// Semget
390// Semop
391// Msgctl
392// Msgget
393// Msgsnd
394// Msgrcv
395// Shmat
396// Shmctl
397// Shmdt
398// Shmget
399// Shm_open
400// Shm_unlink
401// Sem_open
402// Sem_close
403// Sem_unlink
404// Sem_wait
405// Sem_trywait
406// Sem_post
407// Sem_getvalue
408// Sem_init
409// Sem_destroy
410// Open_extended
411// Umask_extended
412// Stat_extended
413// Lstat_extended
414// Fstat_extended
415// Chmod_extended
416// Fchmod_extended
417// Access_extended
418// Settid
419// Gettid
420// Setsgroups
421// Getsgroups
422// Setwgroups
423// Getwgroups
424// Mkfifo_extended
425// Mkdir_extended
426// Identitysvc
427// Shared_region_check_np
428// Shared_region_map_np
429// __pthread_mutex_destroy
430// __pthread_mutex_init
431// __pthread_mutex_lock
432// __pthread_mutex_trylock
433// __pthread_mutex_unlock
434// __pthread_cond_init
435// __pthread_cond_destroy
436// __pthread_cond_broadcast
437// __pthread_cond_signal
438// Setsid_with_pid
439// __pthread_cond_timedwait
440// Aio_fsync
441// Aio_return
442// Aio_suspend
443// Aio_cancel
444// Aio_error
445// Aio_read
446// Aio_write
447// Lio_listio
448// __pthread_cond_wait
449// Iopolicysys
450// Mlockall
451// Munlockall
452// __pthread_kill
453// __pthread_sigmask
454// __sigwait
455// __disable_threadsignal
456// __pthread_markcancel
457// __pthread_canceled
458// __semwait_signal
459// Proc_info
Dmitriy Vyukovc242aa32012-10-29 23:15:06 +0400460// sendfile
Russ Cox602a4462009-06-01 22:14:57 -0700461// Stat64_extended
462// Lstat64_extended
463// Fstat64_extended
464// __pthread_chdir
465// __pthread_fchdir
466// Audit
467// Auditon
468// Getauid
469// Setauid
470// Getaudit
471// Setaudit
472// Getaudit_addr
473// Setaudit_addr
474// Auditctl
475// Bsdthread_create
476// Bsdthread_terminate
477// Stack_snapshot
478// Bsdthread_register
479// Workq_open
480// Workq_ops
481// __mac_execve
482// __mac_syscall
483// __mac_get_file
484// __mac_set_file
485// __mac_get_link
486// __mac_set_link
487// __mac_get_proc
488// __mac_set_proc
489// __mac_get_fd
490// __mac_set_fd
491// __mac_get_pid
492// __mac_get_lcid
493// __mac_get_lctx
494// __mac_set_lctx
495// Setlcid
496// Read_nocancel
497// Write_nocancel
498// Open_nocancel
499// Close_nocancel
500// Wait4_nocancel
501// Recvmsg_nocancel
502// Sendmsg_nocancel
503// Recvfrom_nocancel
504// Accept_nocancel
505// Msync_nocancel
506// Fcntl_nocancel
507// Select_nocancel
508// Fsync_nocancel
509// Connect_nocancel
510// Sigsuspend_nocancel
511// Readv_nocancel
512// Writev_nocancel
513// Sendto_nocancel
514// Pread_nocancel
515// Pwrite_nocancel
516// Waitid_nocancel
517// Poll_nocancel
518// Msgsnd_nocancel
519// Msgrcv_nocancel
520// Sem_wait_nocancel
521// Aio_suspend_nocancel
522// __sigwait_nocancel
523// __semwait_signal_nocancel
524// __mac_mount
525// __mac_get_mount
526// __mac_getfsstat