blob: f026a56d8b2934e50979f743c1f4fe48cf6662b1 [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
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]))
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,
79// appending the names to names. It returns the number
80// 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))
199 n = int(r0)
200 if e1 != 0 {
201 err = e1
202 }
203 return
204}
205
Russ Cox602a4462009-06-01 22:14:57 -0700206/*
Russ Cox602a4462009-06-01 22:14:57 -0700207 * Wrapped
208 */
209
Russ Coxc017a822011-11-13 22:44:52 -0500210//sys kill(pid int, signum int, posix int) (err error)
Russ Coxf2317d32010-02-04 02:06:08 -0800211
Russ Cox35586f72012-02-13 13:52:37 -0500212func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
Russ Coxf2317d32010-02-04 02:06:08 -0800213
Russ Cox602a4462009-06-01 22:14:57 -0700214/*
215 * Exposed directly
216 */
Russ Coxc017a822011-11-13 22:44:52 -0500217//sys Access(path string, mode uint32) (err error)
218//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
219//sys Chdir(path string) (err error)
220//sys Chflags(path string, flags int) (err error)
221//sys Chmod(path string, mode uint32) (err error)
222//sys Chown(path string, uid int, gid int) (err error)
223//sys Chroot(path string) (err error)
224//sys Close(fd int) (err error)
225//sysnb Dup(fd int) (nfd int, err error)
226//sysnb Dup2(from int, to int) (err error)
227//sys Exchangedata(path1 string, path2 string, options int) (err error)
Russ Cox602a4462009-06-01 22:14:57 -0700228//sys Exit(code int)
Russ Coxc017a822011-11-13 22:44:52 -0500229//sys Fchdir(fd int) (err error)
Shenghou Ma6de184b2013-05-07 05:20:00 +0800230//sys Fchflags(fd int, flags int) (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500231//sys Fchmod(fd int, mode uint32) (err error)
232//sys Fchown(fd int, uid int, gid int) (err error)
233//sys Flock(fd int, how int) (err error)
234//sys Fpathconf(fd int, name int) (val int, err error)
235//sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
236//sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
237//sys Fsync(fd int) (err error)
238//sys Ftruncate(fd int, length int64) (err error)
239//sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
Russ Cox602a4462009-06-01 22:14:57 -0700240//sys Getdtablesize() (size int)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700241//sysnb Getegid() (egid int)
242//sysnb Geteuid() (uid int)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700243//sysnb Getgid() (gid int)
Russ Coxc017a822011-11-13 22:44:52 -0500244//sysnb Getpgid(pid int) (pgid int, err error)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700245//sysnb Getpgrp() (pgrp int)
246//sysnb Getpid() (pid int)
247//sysnb Getppid() (ppid int)
Russ Coxc017a822011-11-13 22:44:52 -0500248//sys Getpriority(which int, who int) (prio int, err error)
249//sysnb Getrlimit(which int, lim *Rlimit) (err error)
250//sysnb Getrusage(who int, rusage *Rusage) (err error)
251//sysnb Getsid(pid int) (sid int, err error)
Ian Lance Taylor4fd41e42011-03-16 19:03:01 -0700252//sysnb Getuid() (uid int)
253//sysnb Issetugid() (tainted bool)
Russ Coxc017a822011-11-13 22:44:52 -0500254//sys Kqueue() (fd int, err error)
255//sys Lchown(path string, uid int, gid int) (err error)
256//sys Link(path string, link string) (err error)
257//sys Listen(s int, backlog int) (err error)
258//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
259//sys Mkdir(path string, mode uint32) (err error)
260//sys Mkfifo(path string, mode uint32) (err error)
261//sys Mknod(path string, mode uint32, dev int) (err error)
William Orr08706f12014-02-27 10:13:41 -0800262//sys Mlock(b []byte) (err error)
263//sys Mlockall(flags int) (err error)
264//sys Mprotect(b []byte, prot int) (err error)
265//sys Munlock(b []byte) (err error)
266//sys Munlockall() (err error)
Russ Coxc017a822011-11-13 22:44:52 -0500267//sys Open(path string, mode int, perm uint32) (fd int, err error)
268//sys Pathconf(path string, name int) (val int, err error)
269//sys Pread(fd int, p []byte, offset int64) (n int, err error)
270//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400271//sys read(fd int, p []byte) (n int, err error)
Russ Coxc017a822011-11-13 22:44:52 -0500272//sys Readlink(path string, buf []byte) (n int, err error)
273//sys Rename(from string, to string) (err error)
274//sys Revoke(path string) (err error)
275//sys Rmdir(path string) (err error)
276//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
277//sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
278//sys Setegid(egid int) (err error)
279//sysnb Seteuid(euid int) (err error)
280//sysnb Setgid(gid int) (err error)
281//sys Setlogin(name string) (err error)
282//sysnb Setpgid(pid int, pgid int) (err error)
283//sys Setpriority(which int, who int, prio int) (err error)
284//sys Setprivexec(flag int) (err error)
285//sysnb Setregid(rgid int, egid int) (err error)
286//sysnb Setreuid(ruid int, euid int) (err error)
287//sysnb Setrlimit(which int, lim *Rlimit) (err error)
288//sysnb Setsid() (pid int, err error)
289//sysnb Settimeofday(tp *Timeval) (err error)
290//sysnb Setuid(uid int) (err error)
291//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
292//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
293//sys Symlink(path string, link string) (err error)
294//sys Sync() (err error)
295//sys Truncate(path string, length int64) (err error)
Giles Lean5aa3a8d2010-02-16 11:43:25 -0800296//sys Umask(newmask int) (oldmask int)
Russ Coxc017a822011-11-13 22:44:52 -0500297//sys Undelete(path string) (err error)
298//sys Unlink(path string) (err error)
299//sys Unmount(path string, flags int) (err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400300//sys write(fd int, p []byte) (n int, err error)
Russ Coxc017a822011-11-13 22:44:52 -0500301//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
302//sys munmap(addr uintptr, length uintptr) (err error)
Dmitriy Vyukovcffbfae2012-10-09 20:51:58 +0400303//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
304//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
Russ Cox602a4462009-06-01 22:14:57 -0700305
Russ Cox602a4462009-06-01 22:14:57 -0700306/*
307 * Unimplemented
308 */
309// Profil
310// Sigaction
311// Sigprocmask
312// Getlogin
313// Sigpending
314// Sigaltstack
315// Ioctl
316// Reboot
317// Execve
318// Vfork
319// Sbrk
320// Sstk
321// Ovadvise
322// Mincore
323// Setitimer
324// Swapon
325// Select
326// Sigsuspend
327// Readv
328// Writev
329// Nfssvc
330// Getfh
331// Quotactl
332// Mount
333// Csops
334// Waitid
335// Add_profil
336// Kdebug_trace
337// Sigreturn
338// Mmap
Russ Cox602a4462009-06-01 22:14:57 -0700339// Mlock
340// Munlock
341// Atsocket
342// Kqueue_from_portset_np
343// Kqueue_portset
344// Getattrlist
345// Setattrlist
346// Getdirentriesattr
347// Searchfs
348// Delete
349// Copyfile
350// Poll
351// Watchevent
352// Waitevent
353// Modwatch
354// Getxattr
355// Fgetxattr
356// Setxattr
357// Fsetxattr
358// Removexattr
359// Fremovexattr
360// Listxattr
361// Flistxattr
362// Fsctl
363// Initgroups
364// Posix_spawn
365// Nfsclnt
366// Fhopen
367// Minherit
368// Semsys
369// Msgsys
370// Shmsys
371// Semctl
372// Semget
373// Semop
374// Msgctl
375// Msgget
376// Msgsnd
377// Msgrcv
378// Shmat
379// Shmctl
380// Shmdt
381// Shmget
382// Shm_open
383// Shm_unlink
384// Sem_open
385// Sem_close
386// Sem_unlink
387// Sem_wait
388// Sem_trywait
389// Sem_post
390// Sem_getvalue
391// Sem_init
392// Sem_destroy
393// Open_extended
394// Umask_extended
395// Stat_extended
396// Lstat_extended
397// Fstat_extended
398// Chmod_extended
399// Fchmod_extended
400// Access_extended
401// Settid
402// Gettid
403// Setsgroups
404// Getsgroups
405// Setwgroups
406// Getwgroups
407// Mkfifo_extended
408// Mkdir_extended
409// Identitysvc
410// Shared_region_check_np
411// Shared_region_map_np
412// __pthread_mutex_destroy
413// __pthread_mutex_init
414// __pthread_mutex_lock
415// __pthread_mutex_trylock
416// __pthread_mutex_unlock
417// __pthread_cond_init
418// __pthread_cond_destroy
419// __pthread_cond_broadcast
420// __pthread_cond_signal
421// Setsid_with_pid
422// __pthread_cond_timedwait
423// Aio_fsync
424// Aio_return
425// Aio_suspend
426// Aio_cancel
427// Aio_error
428// Aio_read
429// Aio_write
430// Lio_listio
431// __pthread_cond_wait
432// Iopolicysys
433// Mlockall
434// Munlockall
435// __pthread_kill
436// __pthread_sigmask
437// __sigwait
438// __disable_threadsignal
439// __pthread_markcancel
440// __pthread_canceled
441// __semwait_signal
442// Proc_info
Dmitriy Vyukovc242aa32012-10-29 23:15:06 +0400443// sendfile
Russ Cox602a4462009-06-01 22:14:57 -0700444// Stat64_extended
445// Lstat64_extended
446// Fstat64_extended
447// __pthread_chdir
448// __pthread_fchdir
449// Audit
450// Auditon
451// Getauid
452// Setauid
453// Getaudit
454// Setaudit
455// Getaudit_addr
456// Setaudit_addr
457// Auditctl
458// Bsdthread_create
459// Bsdthread_terminate
460// Stack_snapshot
461// Bsdthread_register
462// Workq_open
463// Workq_ops
464// __mac_execve
465// __mac_syscall
466// __mac_get_file
467// __mac_set_file
468// __mac_get_link
469// __mac_set_link
470// __mac_get_proc
471// __mac_set_proc
472// __mac_get_fd
473// __mac_set_fd
474// __mac_get_pid
475// __mac_get_lcid
476// __mac_get_lctx
477// __mac_set_lctx
478// Setlcid
479// Read_nocancel
480// Write_nocancel
481// Open_nocancel
482// Close_nocancel
483// Wait4_nocancel
484// Recvmsg_nocancel
485// Sendmsg_nocancel
486// Recvfrom_nocancel
487// Accept_nocancel
488// Msync_nocancel
489// Fcntl_nocancel
490// Select_nocancel
491// Fsync_nocancel
492// Connect_nocancel
493// Sigsuspend_nocancel
494// Readv_nocancel
495// Writev_nocancel
496// Sendto_nocancel
497// Pread_nocancel
498// Pwrite_nocancel
499// Waitid_nocancel
500// Poll_nocancel
501// Msgsnd_nocancel
502// Msgrcv_nocancel
503// Sem_wait_nocancel
504// Aio_suspend_nocancel
505// __sigwait_nocancel
506// __semwait_signal_nocancel
507// __mac_mount
508// __mac_get_mount
509// __mac_getfsstat