1// 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 "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
39// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
40type SockaddrDatalink struct {
41 Len uint8
42 Family uint8
43 Index uint16
44 Type uint8
45 Nlen uint8
46 Alen uint8
47 Slen uint8
48 Data [12]int8
49 raw RawSockaddrDatalink
50}
51
52// Translate "kern.hostname" to []_C_int{0,1,2,3}.
53func nametomib(name string) (mib []_C_int, err error) {
54 const siz = unsafe.Sizeof(mib[0])
55
56 // NOTE(rsc): It seems strange to set the buffer to have
57 // size CTL_MAXNAME+2 but use only CTL_MAXNAME
58 // as the size. I don't know why the +2 is here, but the
59 // kernel uses +2 for its own implementation of this function.
60 // I am scared that if we don't include the +2 here, the kernel
61 // will silently write 2 words farther than we specify
62 // and we'll get memory corruption.
63 var buf [CTL_MAXNAME + 2]_C_int
64 n := uintptr(CTL_MAXNAME) * siz
65
66 p := (*byte)(unsafe.Pointer(&buf[0]))
67 bytes, err := ByteSliceFromString(name)
68 if err != nil {
69 return nil, err
70 }
71
72 // Magic sysctl: "setting" 0.3 to a string name
73 // lets you read back the array of integers form.
74 if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil {
75 return nil, err
76 }
77 return buf[0 : n/siz], nil
78}
79
80//sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
81func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
82func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
83
84const (
85 attrBitMapCount = 5
86 attrCmnFullpath = 0x08000000
87)
88
89type attrList struct {
90 bitmapCount uint16
91 _ uint16
92 CommonAttr uint32
93 VolAttr uint32
94 DirAttr uint32
95 FileAttr uint32
96 Forkattr uint32
97}
98
99func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
100 if len(attrBuf) < 4 {
101 return nil, errors.New("attrBuf too small")
102 }
103 attrList.bitmapCount = attrBitMapCount
104
105 var _p0 *byte
106 _p0, err = BytePtrFromString(path)
107 if err != nil {
108 return nil, err
109 }
110
111 _, _, e1 := Syscall6(
112 SYS_GETATTRLIST,
113 uintptr(unsafe.Pointer(_p0)),
114 uintptr(unsafe.Pointer(&attrList)),
115 uintptr(unsafe.Pointer(&attrBuf[0])),
116 uintptr(len(attrBuf)),
117 uintptr(options),
118 0,
119 )
120 if e1 != 0 {
121 return nil, e1
122 }
123 size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
124
125 // dat is the section of attrBuf that contains valid data,
126 // without the 4 byte length header. All attribute offsets
127 // are relative to dat.
128 dat := attrBuf
129 if int(size) < len(attrBuf) {
130 dat = dat[:size]
131 }
132 dat = dat[4:] // remove length prefix
133
134 for i := uint32(0); int(i) < len(dat); {
135 header := dat[i:]
136 if len(header) < 8 {
137 return attrs, errors.New("truncated attribute header")
138 }
139 datOff := *(*int32)(unsafe.Pointer(&header[0]))
140 attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
141 if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
142 return attrs, errors.New("truncated results; attrBuf too small")
143 }
144 end := uint32(datOff) + attrLen
145 attrs = append(attrs, dat[datOff:end])
146 i = end
147 if r := i % 4; r != 0 {
148 i += (4 - r)
149 }
150 }
151 return
152}
153
154//sysnb pipe() (r int, w int, err error)
155
156func Pipe(p []int) (err error) {
157 if len(p) != 2 {
158 return EINVAL
159 }
160 p[0], p[1], err = pipe()
161 return
162}
163
164func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
165 var _p0 unsafe.Pointer
166 var bufsize uintptr
167 if len(buf) > 0 {
168 _p0 = unsafe.Pointer(&buf[0])
169 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
170 }
171 r0, _, e1 := Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags))
172 n = int(r0)
173 if e1 != 0 {
174 err = e1
175 }
176 return
177}
178
179func xattrPointer(dest []byte) *byte {
180 // It's only when dest is set to NULL that the OS X implementations of
181 // getxattr() and listxattr() return the current sizes of the named attributes.
182 // An empty byte array is not sufficient. To maintain the same behaviour as the
183 // linux implementation, we wrap around the system calls and pass in NULL when
184 // dest is empty.
185 var destp *byte
186 if len(dest) > 0 {
187 destp = &dest[0]
188 }
189 return destp
190}
191
192//sys getxattr(path string, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
193
194func Getxattr(path string, attr string, dest []byte) (sz int, err error) {
195 return getxattr(path, attr, xattrPointer(dest), len(dest), 0, 0)
196}
197
198func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) {
199 return getxattr(link, attr, xattrPointer(dest), len(dest), 0, XATTR_NOFOLLOW)
200}
201
202//sys setxattr(path string, attr string, data *byte, size int, position uint32, options int) (err error)
203
204func Setxattr(path string, attr string, data []byte, flags int) (err error) {
205 // The parameters for the OS X implementation vary slightly compared to the
206 // linux system call, specifically the position parameter:
207 //
208 // linux:
209 // int setxattr(
210 // const char *path,
211 // const char *name,
212 // const void *value,
213 // size_t size,
214 // int flags
215 // );
216 //
217 // darwin:
218 // int setxattr(
219 // const char *path,
220 // const char *name,
221 // void *value,
222 // size_t size,
223 // u_int32_t position,
224 // int options
225 // );
226 //
227 // position specifies the offset within the extended attribute. In the
228 // current implementation, only the resource fork extended attribute makes
229 // use of this argument. For all others, position is reserved. We simply
230 // default to setting it to zero.
231 return setxattr(path, attr, xattrPointer(data), len(data), 0, flags)
232}
233
234func Lsetxattr(link string, attr string, data []byte, flags int) (err error) {
235 return setxattr(link, attr, xattrPointer(data), len(data), 0, flags|XATTR_NOFOLLOW)
236}
237
238//sys removexattr(path string, attr string, options int) (err error)
239
240func Removexattr(path string, attr string) (err error) {
241 // We wrap around and explicitly zero out the options provided to the OS X
242 // implementation of removexattr, we do so for interoperability with the
243 // linux variant.
244 return removexattr(path, attr, 0)
245}
246
247func Lremovexattr(link string, attr string) (err error) {
248 return removexattr(link, attr, XATTR_NOFOLLOW)
249}
250
251//sys listxattr(path string, dest *byte, size int, options int) (sz int, err error)
252
253func Listxattr(path string, dest []byte) (sz int, err error) {
254 return listxattr(path, xattrPointer(dest), len(dest), 0)
255}
256
257func Llistxattr(link string, dest []byte) (sz int, err error) {
258 return listxattr(link, xattrPointer(dest), len(dest), XATTR_NOFOLLOW)
259}
260
261func setattrlistTimes(path string, times []Timespec, flags int) error {
262 _p0, err := BytePtrFromString(path)
263 if err != nil {
264 return err
265 }
266
267 var attrList attrList
268 attrList.bitmapCount = ATTR_BIT_MAP_COUNT
269 attrList.CommonAttr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME
270
271 // order is mtime, atime: the opposite of Chtimes
272 attributes := [2]Timespec{times[1], times[0]}
273 options := 0
274 if flags&AT_SYMLINK_NOFOLLOW != 0 {
275 options |= FSOPT_NOFOLLOW
276 }
277 _, _, e1 := Syscall6(
278 SYS_SETATTRLIST,
279 uintptr(unsafe.Pointer(_p0)),
280 uintptr(unsafe.Pointer(&attrList)),
281 uintptr(unsafe.Pointer(&attributes)),
282 uintptr(unsafe.Sizeof(attributes)),
283 uintptr(options),
284 0,
285 )
286 if e1 != 0 {
287 return e1
288 }
289 return nil
290}
291
292func utimensat(dirfd int, path string, times *[2]Timespec, flags int) error {
293 // Darwin doesn't support SYS_UTIMENSAT
294 return ENOSYS
295}
296
297/*
298 * Wrapped
299 */
300
301//sys kill(pid int, signum int, posix int) (err error)
302
303func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(signum), 1) }
304
305//sys ioctl(fd int, req uint, arg uintptr) (err error)
306
307// ioctl itself should not be exposed directly, but additional get/set
308// functions for specific types are permissible.
309
310// IoctlSetInt performs an ioctl operation which sets an integer value
311// on fd, using the specified request number.
312func IoctlSetInt(fd int, req uint, value int) error {
313 return ioctl(fd, req, uintptr(value))
314}
315
316func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
317 return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
318}
319
320func IoctlSetTermios(fd int, req uint, value *Termios) error {
321 return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
322}
323
324// IoctlGetInt performs an ioctl operation which gets an integer value
325// from fd, using the specified request number.
326func IoctlGetInt(fd int, req uint) (int, error) {
327 var value int
328 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
329 return value, err
330}
331
332func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
333 var value Winsize
334 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
335 return &value, err
336}
337
338func IoctlGetTermios(fd int, req uint) (*Termios, error) {
339 var value Termios
340 err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
341 return &value, err
342}
343
344func Uname(uname *Utsname) error {
345 mib := []_C_int{CTL_KERN, KERN_OSTYPE}
346 n := unsafe.Sizeof(uname.Sysname)
347 if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
348 return err
349 }
350
351 mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
352 n = unsafe.Sizeof(uname.Nodename)
353 if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
354 return err
355 }
356
357 mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
358 n = unsafe.Sizeof(uname.Release)
359 if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
360 return err
361 }
362
363 mib = []_C_int{CTL_KERN, KERN_VERSION}
364 n = unsafe.Sizeof(uname.Version)
365 if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
366 return err
367 }
368
369 // The version might have newlines or tabs in it, convert them to
370 // spaces.
371 for i, b := range uname.Version {
372 if b == '\n' || b == '\t' {
373 if i == len(uname.Version)-1 {
374 uname.Version[i] = 0
375 } else {
376 uname.Version[i] = ' '
377 }
378 }
379 }
380
381 mib = []_C_int{CTL_HW, HW_MACHINE}
382 n = unsafe.Sizeof(uname.Machine)
383 if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
384 return err
385 }
386
387 return nil
388}
389
390/*
391 * Exposed directly
392 */
393//sys Access(path string, mode uint32) (err error)
394//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
395//sys Chdir(path string) (err error)
396//sys Chflags(path string, flags int) (err error)
397//sys Chmod(path string, mode uint32) (err error)
398//sys Chown(path string, uid int, gid int) (err error)
399//sys Chroot(path string) (err error)
400//sys Close(fd int) (err error)
401//sys Dup(fd int) (nfd int, err error)
402//sys Dup2(from int, to int) (err error)
403//sys Exchangedata(path1 string, path2 string, options int) (err error)
404//sys Exit(code int)
405//sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
406//sys Fchdir(fd int) (err error)
407//sys Fchflags(fd int, flags int) (err error)
408//sys Fchmod(fd int, mode uint32) (err error)
409//sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
410//sys Fchown(fd int, uid int, gid int) (err error)
411//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
412//sys Flock(fd int, how int) (err error)
413//sys Fpathconf(fd int, name int) (val int, err error)
414//sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
415//sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error) = SYS_FSTATAT64
416//sys Fstatfs(fd int, stat *Statfs_t) (err error) = SYS_FSTATFS64
417//sys Fsync(fd int) (err error)
418//sys Ftruncate(fd int, length int64) (err error)
419//sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) = SYS_GETDIRENTRIES64
420//sys Getdtablesize() (size int)
421//sysnb Getegid() (egid int)
422//sysnb Geteuid() (uid int)
423//sysnb Getgid() (gid int)
424//sysnb Getpgid(pid int) (pgid int, err error)
425//sysnb Getpgrp() (pgrp int)
426//sysnb Getpid() (pid int)
427//sysnb Getppid() (ppid int)
428//sys Getpriority(which int, who int) (prio int, err error)
429//sysnb Getrlimit(which int, lim *Rlimit) (err error)
430//sysnb Getrusage(who int, rusage *Rusage) (err error)
431//sysnb Getsid(pid int) (sid int, err error)
432//sysnb Getuid() (uid int)
433//sysnb Issetugid() (tainted bool)
434//sys Kqueue() (fd int, err error)
435//sys Lchown(path string, uid int, gid int) (err error)
436//sys Link(path string, link string) (err error)
437//sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
438//sys Listen(s int, backlog int) (err error)
439//sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
440//sys Mkdir(path string, mode uint32) (err error)
441//sys Mkdirat(dirfd int, path string, mode uint32) (err error)
442//sys Mkfifo(path string, mode uint32) (err error)
443//sys Mknod(path string, mode uint32, dev int) (err error)
444//sys Open(path string, mode int, perm uint32) (fd int, err error)
445//sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
446//sys Pathconf(path string, name int) (val int, err error)
447//sys Pread(fd int, p []byte, offset int64) (n int, err error)
448//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
449//sys read(fd int, p []byte) (n int, err error)
450//sys Readlink(path string, buf []byte) (n int, err error)
451//sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
452//sys Rename(from string, to string) (err error)
453//sys Renameat(fromfd int, from string, tofd int, to string) (err error)
454//sys Revoke(path string) (err error)
455//sys Rmdir(path string) (err error)
456//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
457//sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
458//sys Setegid(egid int) (err error)
459//sysnb Seteuid(euid int) (err error)
460//sysnb Setgid(gid int) (err error)
461//sys Setlogin(name string) (err error)
462//sysnb Setpgid(pid int, pgid int) (err error)
463//sys Setpriority(which int, who int, prio int) (err error)
464//sys Setprivexec(flag int) (err error)
465//sysnb Setregid(rgid int, egid int) (err error)
466//sysnb Setreuid(ruid int, euid int) (err error)
467//sysnb Setrlimit(which int, lim *Rlimit) (err error)
468//sysnb Setsid() (pid int, err error)
469//sysnb Settimeofday(tp *Timeval) (err error)
470//sysnb Setuid(uid int) (err error)
471//sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
472//sys Statfs(path string, stat *Statfs_t) (err error) = SYS_STATFS64
473//sys Symlink(path string, link string) (err error)
474//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
475//sys Sync() (err error)
476//sys Truncate(path string, length int64) (err error)
477//sys Umask(newmask int) (oldmask int)
478//sys Undelete(path string) (err error)
479//sys Unlink(path string) (err error)
480//sys Unlinkat(dirfd int, path string, flags int) (err error)
481//sys Unmount(path string, flags int) (err error)
482//sys write(fd int, p []byte) (n int, err error)
483//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
484//sys munmap(addr uintptr, length uintptr) (err error)
485//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
486//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
487
488/*
489 * Unimplemented
490 */
491// Profil
492// Sigaction
493// Sigprocmask
494// Getlogin
495// Sigpending
496// Sigaltstack
497// Ioctl
498// Reboot
499// Execve
500// Vfork
501// Sbrk
502// Sstk
503// Ovadvise
504// Mincore
505// Setitimer
506// Swapon
507// Select
508// Sigsuspend
509// Readv
510// Writev
511// Nfssvc
512// Getfh
513// Quotactl
514// Mount
515// Csops
516// Waitid
517// Add_profil
518// Kdebug_trace
519// Sigreturn
520// Atsocket
521// Kqueue_from_portset_np
522// Kqueue_portset
523// Getattrlist
524// Setattrlist
525// Getdirentriesattr
526// Searchfs
527// Delete
528// Copyfile
529// Watchevent
530// Waitevent
531// Modwatch
532// Fgetxattr
533// Fsetxattr
534// Fremovexattr
535// Flistxattr
536// Fsctl
537// Initgroups
538// Posix_spawn
539// Nfsclnt
540// Fhopen
541// Minherit
542// Semsys
543// Msgsys
544// Shmsys
545// Semctl
546// Semget
547// Semop
548// Msgctl
549// Msgget
550// Msgsnd
551// Msgrcv
552// Shmat
553// Shmctl
554// Shmdt
555// Shmget
556// Shm_open
557// Shm_unlink
558// Sem_open
559// Sem_close
560// Sem_unlink
561// Sem_wait
562// Sem_trywait
563// Sem_post
564// Sem_getvalue
565// Sem_init
566// Sem_destroy
567// Open_extended
568// Umask_extended
569// Stat_extended
570// Lstat_extended
571// Fstat_extended
572// Chmod_extended
573// Fchmod_extended
574// Access_extended
575// Settid
576// Gettid
577// Setsgroups
578// Getsgroups
579// Setwgroups
580// Getwgroups
581// Mkfifo_extended
582// Mkdir_extended
583// Identitysvc
584// Shared_region_check_np
585// Shared_region_map_np
586// __pthread_mutex_destroy
587// __pthread_mutex_init
588// __pthread_mutex_lock
589// __pthread_mutex_trylock
590// __pthread_mutex_unlock
591// __pthread_cond_init
592// __pthread_cond_destroy
593// __pthread_cond_broadcast
594// __pthread_cond_signal
595// Setsid_with_pid
596// __pthread_cond_timedwait
597// Aio_fsync
598// Aio_return
599// Aio_suspend
600// Aio_cancel
601// Aio_error
602// Aio_read
603// Aio_write
604// Lio_listio
605// __pthread_cond_wait
606// Iopolicysys
607// __pthread_kill
608// __pthread_sigmask
609// __sigwait
610// __disable_threadsignal
611// __pthread_markcancel
612// __pthread_canceled
613// __semwait_signal
614// Proc_info
615// sendfile
616// Stat64_extended
617// Lstat64_extended
618// Fstat64_extended
619// __pthread_chdir
620// __pthread_fchdir
621// Audit
622// Auditon
623// Getauid
624// Setauid
625// Getaudit
626// Setaudit
627// Getaudit_addr
628// Setaudit_addr
629// Auditctl
630// Bsdthread_create
631// Bsdthread_terminate
632// Stack_snapshot
633// Bsdthread_register
634// Workq_open
635// Workq_ops
636// __mac_execve
637// __mac_syscall
638// __mac_get_file
639// __mac_set_file
640// __mac_get_link
641// __mac_set_link
642// __mac_get_proc
643// __mac_set_proc
644// __mac_get_fd
645// __mac_set_fd
646// __mac_get_pid
647// __mac_get_lcid
648// __mac_get_lctx
649// __mac_set_lctx
650// Setlcid
651// Read_nocancel
652// Write_nocancel
653// Open_nocancel
654// Close_nocancel
655// Wait4_nocancel
656// Recvmsg_nocancel
657// Sendmsg_nocancel
658// Recvfrom_nocancel
659// Accept_nocancel
660// Fcntl_nocancel
661// Select_nocancel
662// Fsync_nocancel
663// Connect_nocancel
664// Sigsuspend_nocancel
665// Readv_nocancel
666// Writev_nocancel
667// Sendto_nocancel
668// Pread_nocancel
669// Pwrite_nocancel
670// Waitid_nocancel
671// Poll_nocancel
672// Msgsnd_nocancel
673// Msgrcv_nocancel
674// Sem_wait_nocancel
675// Aio_suspend_nocancel
676// __sigwait_nocancel
677// __semwait_signal_nocancel
678// __mac_mount
679// __mac_get_mount
680// __mac_getfsstat