blob: c5018a385c87909076c32cabfb6cd513e7956642 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001// 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
Don Newton7577f072020-01-06 12:41:11 -050080func 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
Don Newton98fd8812019-09-23 15:15:02 -040092func 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
Don Newton7577f072020-01-06 12:41:11 -0500158func SysctlClockinfo(name string) (*Clockinfo, error) {
159 mib, err := sysctlmib(name)
160 if err != nil {
161 return nil, err
162 }
163
164 n := uintptr(SizeofClockinfo)
165 var ci Clockinfo
166 if err := sysctl(mib, (*byte)(unsafe.Pointer(&ci)), &n, nil, 0); err != nil {
167 return nil, err
168 }
169 if n != SizeofClockinfo {
170 return nil, EIO
171 }
172 return &ci, nil
173}
174
Don Newton98fd8812019-09-23 15:15:02 -0400175//sysnb pipe() (r int, w int, err error)
176
177func Pipe(p []int) (err error) {
178 if len(p) != 2 {
179 return EINVAL
180 }
181 p[0], p[1], err = pipe()
182 return
183}
184
185func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
186 var _p0 unsafe.Pointer
187 var bufsize uintptr
188 if len(buf) > 0 {
189 _p0 = unsafe.Pointer(&buf[0])
190 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
191 }
192 return getfsstat(_p0, bufsize, flags)
193}
194
195func xattrPointer(dest []byte) *byte {
196 // It's only when dest is set to NULL that the OS X implementations of
197 // getxattr() and listxattr() return the current sizes of the named attributes.
198 // An empty byte array is not sufficient. To maintain the same behaviour as the
199 // linux implementation, we wrap around the system calls and pass in NULL when
200 // dest is empty.
201 var destp *byte
202 if len(dest) > 0 {
203 destp = &dest[0]
204 }
205 return destp
206}
207
208//sys getxattr(path string, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
209
210func Getxattr(path string, attr string, dest []byte) (sz int, err error) {
211 return getxattr(path, attr, xattrPointer(dest), len(dest), 0, 0)
212}
213
214func Lgetxattr(link string, attr string, dest []byte) (sz int, err error) {
215 return getxattr(link, attr, xattrPointer(dest), len(dest), 0, XATTR_NOFOLLOW)
216}
217
218//sys fgetxattr(fd int, attr string, dest *byte, size int, position uint32, options int) (sz int, err error)
219
220func Fgetxattr(fd int, attr string, dest []byte) (sz int, err error) {
221 return fgetxattr(fd, attr, xattrPointer(dest), len(dest), 0, 0)
222}
223
224//sys setxattr(path string, attr string, data *byte, size int, position uint32, options int) (err error)
225
226func Setxattr(path string, attr string, data []byte, flags int) (err error) {
227 // The parameters for the OS X implementation vary slightly compared to the
228 // linux system call, specifically the position parameter:
229 //
230 // linux:
231 // int setxattr(
232 // const char *path,
233 // const char *name,
234 // const void *value,
235 // size_t size,
236 // int flags
237 // );
238 //
239 // darwin:
240 // int setxattr(
241 // const char *path,
242 // const char *name,
243 // void *value,
244 // size_t size,
245 // u_int32_t position,
246 // int options
247 // );
248 //
249 // position specifies the offset within the extended attribute. In the
250 // current implementation, only the resource fork extended attribute makes
251 // use of this argument. For all others, position is reserved. We simply
252 // default to setting it to zero.
253 return setxattr(path, attr, xattrPointer(data), len(data), 0, flags)
254}
255
256func Lsetxattr(link string, attr string, data []byte, flags int) (err error) {
257 return setxattr(link, attr, xattrPointer(data), len(data), 0, flags|XATTR_NOFOLLOW)
258}
259
260//sys fsetxattr(fd int, attr string, data *byte, size int, position uint32, options int) (err error)
261
262func Fsetxattr(fd int, attr string, data []byte, flags int) (err error) {
263 return fsetxattr(fd, attr, xattrPointer(data), len(data), 0, 0)
264}
265
266//sys removexattr(path string, attr string, options int) (err error)
267
268func Removexattr(path string, attr string) (err error) {
269 // We wrap around and explicitly zero out the options provided to the OS X
270 // implementation of removexattr, we do so for interoperability with the
271 // linux variant.
272 return removexattr(path, attr, 0)
273}
274
275func Lremovexattr(link string, attr string) (err error) {
276 return removexattr(link, attr, XATTR_NOFOLLOW)
277}
278
279//sys fremovexattr(fd int, attr string, options int) (err error)
280
281func Fremovexattr(fd int, attr string) (err error) {
282 return fremovexattr(fd, attr, 0)
283}
284
285//sys listxattr(path string, dest *byte, size int, options int) (sz int, err error)
286
287func Listxattr(path string, dest []byte) (sz int, err error) {
288 return listxattr(path, xattrPointer(dest), len(dest), 0)
289}
290
291func Llistxattr(link string, dest []byte) (sz int, err error) {
292 return listxattr(link, xattrPointer(dest), len(dest), XATTR_NOFOLLOW)
293}
294
295//sys flistxattr(fd int, dest *byte, size int, options int) (sz int, err error)
296
297func Flistxattr(fd int, dest []byte) (sz int, err error) {
298 return flistxattr(fd, xattrPointer(dest), len(dest), 0)
299}
300
301func setattrlistTimes(path string, times []Timespec, flags int) error {
302 _p0, err := BytePtrFromString(path)
303 if err != nil {
304 return err
305 }
306
307 var attrList attrList
308 attrList.bitmapCount = ATTR_BIT_MAP_COUNT
309 attrList.CommonAttr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME
310
311 // order is mtime, atime: the opposite of Chtimes
312 attributes := [2]Timespec{times[1], times[0]}
313 options := 0
314 if flags&AT_SYMLINK_NOFOLLOW != 0 {
315 options |= FSOPT_NOFOLLOW
316 }
317 return setattrlist(
318 _p0,
319 unsafe.Pointer(&attrList),
320 unsafe.Pointer(&attributes),
321 unsafe.Sizeof(attributes),
322 options)
323}
324
325//sys setattrlist(path *byte, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error)
326
327func utimensat(dirfd int, path string, times *[2]Timespec, flags int) error {
328 // Darwin doesn't support SYS_UTIMENSAT
329 return ENOSYS
330}
331
332/*
333 * Wrapped
334 */
335
336//sys kill(pid int, signum int, posix int) (err error)
337
338func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(signum), 1) }
339
340//sys ioctl(fd int, req uint, arg uintptr) (err error)
341
Don Newton98fd8812019-09-23 15:15:02 -0400342func Uname(uname *Utsname) error {
343 mib := []_C_int{CTL_KERN, KERN_OSTYPE}
344 n := unsafe.Sizeof(uname.Sysname)
345 if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil {
346 return err
347 }
348
349 mib = []_C_int{CTL_KERN, KERN_HOSTNAME}
350 n = unsafe.Sizeof(uname.Nodename)
351 if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil {
352 return err
353 }
354
355 mib = []_C_int{CTL_KERN, KERN_OSRELEASE}
356 n = unsafe.Sizeof(uname.Release)
357 if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil {
358 return err
359 }
360
361 mib = []_C_int{CTL_KERN, KERN_VERSION}
362 n = unsafe.Sizeof(uname.Version)
363 if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil {
364 return err
365 }
366
367 // The version might have newlines or tabs in it, convert them to
368 // spaces.
369 for i, b := range uname.Version {
370 if b == '\n' || b == '\t' {
371 if i == len(uname.Version)-1 {
372 uname.Version[i] = 0
373 } else {
374 uname.Version[i] = ' '
375 }
376 }
377 }
378
379 mib = []_C_int{CTL_HW, HW_MACHINE}
380 n = unsafe.Sizeof(uname.Machine)
381 if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil {
382 return err
383 }
384
385 return nil
386}
387
388func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
389 if raceenabled {
390 raceReleaseMerge(unsafe.Pointer(&ioSync))
391 }
392 var length = int64(count)
393 err = sendfile(infd, outfd, *offset, &length, nil, 0)
394 written = int(length)
395 return
396}
397
398//sys sendfile(infd int, outfd int, offset int64, len *int64, hdtr unsafe.Pointer, flags int) (err error)
399
400/*
401 * Exposed directly
402 */
403//sys Access(path string, mode uint32) (err error)
404//sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
405//sys Chdir(path string) (err error)
406//sys Chflags(path string, flags int) (err error)
407//sys Chmod(path string, mode uint32) (err error)
408//sys Chown(path string, uid int, gid int) (err error)
409//sys Chroot(path string) (err error)
410//sys ClockGettime(clockid int32, time *Timespec) (err error)
411//sys Close(fd int) (err error)
412//sys Dup(fd int) (nfd int, err error)
413//sys Dup2(from int, to int) (err error)
414//sys Exchangedata(path1 string, path2 string, options int) (err error)
415//sys Exit(code int)
416//sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error)
417//sys Fchdir(fd int) (err error)
418//sys Fchflags(fd int, flags int) (err error)
419//sys Fchmod(fd int, mode uint32) (err error)
420//sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error)
421//sys Fchown(fd int, uid int, gid int) (err error)
422//sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error)
423//sys Flock(fd int, how int) (err error)
424//sys Fpathconf(fd int, name int) (val int, err error)
425//sys Fsync(fd int) (err error)
426//sys Ftruncate(fd int, length int64) (err error)
427//sys Getdtablesize() (size int)
428//sysnb Getegid() (egid int)
429//sysnb Geteuid() (uid int)
430//sysnb Getgid() (gid int)
431//sysnb Getpgid(pid int) (pgid int, err error)
432//sysnb Getpgrp() (pgrp int)
433//sysnb Getpid() (pid int)
434//sysnb Getppid() (ppid int)
435//sys Getpriority(which int, who int) (prio int, err error)
436//sysnb Getrlimit(which int, lim *Rlimit) (err error)
437//sysnb Getrusage(who int, rusage *Rusage) (err error)
438//sysnb Getsid(pid int) (sid int, err error)
439//sysnb Getuid() (uid int)
440//sysnb Issetugid() (tainted bool)
441//sys Kqueue() (fd int, err error)
442//sys Lchown(path string, uid int, gid int) (err error)
443//sys Link(path string, link string) (err error)
444//sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error)
445//sys Listen(s int, backlog int) (err error)
446//sys Mkdir(path string, mode uint32) (err error)
447//sys Mkdirat(dirfd int, path string, mode uint32) (err error)
448//sys Mkfifo(path string, mode uint32) (err error)
449//sys Mknod(path string, mode uint32, dev int) (err error)
450//sys Open(path string, mode int, perm uint32) (fd int, err error)
451//sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error)
452//sys Pathconf(path string, name int) (val int, err error)
453//sys Pread(fd int, p []byte, offset int64) (n int, err error)
454//sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
455//sys read(fd int, p []byte) (n int, err error)
456//sys Readlink(path string, buf []byte) (n int, err error)
457//sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error)
458//sys Rename(from string, to string) (err error)
459//sys Renameat(fromfd int, from string, tofd int, to string) (err error)
460//sys Revoke(path string) (err error)
461//sys Rmdir(path string) (err error)
462//sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK
Don Newton7577f072020-01-06 12:41:11 -0500463//sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
Don Newton98fd8812019-09-23 15:15:02 -0400464//sys Setegid(egid int) (err error)
465//sysnb Seteuid(euid int) (err error)
466//sysnb Setgid(gid int) (err error)
467//sys Setlogin(name string) (err error)
468//sysnb Setpgid(pid int, pgid int) (err error)
469//sys Setpriority(which int, who int, prio int) (err error)
470//sys Setprivexec(flag int) (err error)
471//sysnb Setregid(rgid int, egid int) (err error)
472//sysnb Setreuid(ruid int, euid int) (err error)
473//sysnb Setrlimit(which int, lim *Rlimit) (err error)
474//sysnb Setsid() (pid int, err error)
475//sysnb Settimeofday(tp *Timeval) (err error)
476//sysnb Setuid(uid int) (err error)
477//sys Symlink(path string, link string) (err error)
478//sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error)
479//sys Sync() (err error)
480//sys Truncate(path string, length int64) (err error)
481//sys Umask(newmask int) (oldmask int)
482//sys Undelete(path string) (err error)
483//sys Unlink(path string) (err error)
484//sys Unlinkat(dirfd int, path string, flags int) (err error)
485//sys Unmount(path string, flags int) (err error)
486//sys write(fd int, p []byte) (n int, err error)
487//sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
488//sys munmap(addr uintptr, length uintptr) (err error)
489//sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ
490//sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE
491
492/*
493 * Unimplemented
494 */
495// Profil
496// Sigaction
497// Sigprocmask
498// Getlogin
499// Sigpending
500// Sigaltstack
501// Ioctl
502// Reboot
503// Execve
504// Vfork
505// Sbrk
506// Sstk
507// Ovadvise
508// Mincore
509// Setitimer
510// Swapon
511// Select
512// Sigsuspend
513// Readv
514// Writev
515// Nfssvc
516// Getfh
517// Quotactl
518// Mount
519// Csops
520// Waitid
521// Add_profil
522// Kdebug_trace
523// Sigreturn
524// Atsocket
525// Kqueue_from_portset_np
526// Kqueue_portset
527// Getattrlist
528// Setattrlist
529// Getdirentriesattr
530// Searchfs
531// Delete
532// Copyfile
533// Watchevent
534// Waitevent
535// Modwatch
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