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