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