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.
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.
20 const ImplementsGetwd = true
22 func 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
33 // If pkg/os/getwd.go gets ENOTSUP, it will fall back to the
38 type SockaddrDatalink struct {
47 raw RawSockaddrDatalink
50 // Translate "kern.hostname" to []_C_int{0,1,2,3}.
51 func nametomib(name string) (mib []_C_int, err error) {
52 const siz = unsafe.Sizeof(mib[0])
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
64 p := (*byte)(unsafe.Pointer(&buf[0]))
65 bytes, err := ByteSliceFromString(name)
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 {
75 return buf[0 : n/siz], nil
78 func direntIno(buf []byte) (uint64, bool) {
79 return readInt(buf, unsafe.Offsetof(Dirent{}.Ino), unsafe.Sizeof(Dirent{}.Ino))
82 func direntReclen(buf []byte) (uint64, bool) {
83 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen))
86 func direntNamlen(buf []byte) (uint64, bool) {
87 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen))
90 //sys ptrace(request int, pid int, addr uintptr, data uintptr) (err error)
91 func PtraceAttach(pid int) (err error) { return ptrace(PT_ATTACH, pid, 0, 0) }
92 func PtraceDetach(pid int) (err error) { return ptrace(PT_DETACH, pid, 0, 0) }
96 attrCmnModtime = 0x00000400
97 attrCmnAcctime = 0x00001000
98 attrCmnFullpath = 0x08000000
101 type attrList struct {
111 func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) {
112 if len(attrBuf) < 4 {
113 return nil, errorspkg.New("attrBuf too small")
115 attrList.bitmapCount = attrBitMapCount
118 _p0, err = BytePtrFromString(path)
123 _, _, e1 := syscall6(
124 funcPC(libc_getattrlist_trampoline),
125 uintptr(unsafe.Pointer(_p0)),
126 uintptr(unsafe.Pointer(&attrList)),
127 uintptr(unsafe.Pointer(&attrBuf[0])),
128 uintptr(len(attrBuf)),
135 size := *(*uint32)(unsafe.Pointer(&attrBuf[0]))
137 // dat is the section of attrBuf that contains valid data,
138 // without the 4 byte length header. All attribute offsets
139 // are relative to dat.
141 if int(size) < len(attrBuf) {
144 dat = dat[4:] // remove length prefix
146 for i := uint32(0); int(i) < len(dat); {
149 return attrs, errorspkg.New("truncated attribute header")
151 datOff := *(*int32)(unsafe.Pointer(&header[0]))
152 attrLen := *(*uint32)(unsafe.Pointer(&header[4]))
153 if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) {
154 return attrs, errorspkg.New("truncated results; attrBuf too small")
156 end := uint32(datOff) + attrLen
157 attrs = append(attrs, dat[datOff:end])
159 if r := i % 4; r != 0 {
166 func libc_getattrlist_trampoline()
168 //go:linkname libc_getattrlist libc_getattrlist
169 //go:cgo_import_dynamic libc_getattrlist getattrlist "/usr/lib/libSystem.B.dylib"
171 //sysnb pipe(p *[2]int32) (err error)
173 func Pipe(p []int) (err error) {
184 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
185 var _p0 unsafe.Pointer
188 _p0 = unsafe.Pointer(&buf[0])
189 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf))
191 r0, _, e1 := syscall(funcPC(libc_getfsstat64_trampoline), uintptr(_p0), bufsize, uintptr(flags))
199 func libc_getfsstat64_trampoline()
201 //go:linkname libc_getfsstat64 libc_getfsstat64
202 //go:cgo_import_dynamic libc_getfsstat64 getfsstat64 "/usr/lib/libSystem.B.dylib"
204 func setattrlistTimes(path string, times []Timespec) error {
205 _p0, err := BytePtrFromString(path)
210 var attrList attrList
211 attrList.bitmapCount = attrBitMapCount
212 attrList.CommonAttr = attrCmnModtime | attrCmnAcctime
214 // order is mtime, atime: the opposite of Chtimes
215 attributes := [2]Timespec{times[1], times[0]}
217 _, _, e1 := syscall6(
218 funcPC(libc_setattrlist_trampoline),
219 uintptr(unsafe.Pointer(_p0)),
220 uintptr(unsafe.Pointer(&attrList)),
221 uintptr(unsafe.Pointer(&attributes)),
222 uintptr(unsafe.Sizeof(attributes)),
232 func libc_setattrlist_trampoline()
234 //go:linkname libc_setattrlist libc_setattrlist
235 //go:cgo_import_dynamic libc_setattrlist setattrlist "/usr/lib/libSystem.B.dylib"
237 func utimensat(dirfd int, path string, times *[2]Timespec, flag int) error {
238 // Darwin doesn't support SYS_UTIMENSAT
246 //sys kill(pid int, signum int, posix int) (err error)
248 func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) }
253 //sys Access(path string, mode uint32) (err error)
254 //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error)
255 //sys Chdir(path string) (err error)
256 //sys Chflags(path string, flags int) (err error)
257 //sys Chmod(path string, mode uint32) (err error)
258 //sys Chown(path string, uid int, gid int) (err error)
259 //sys Chroot(path string) (err error)
260 //sys Close(fd int) (err error)
261 //sys closedir(dir uintptr) (err error)
262 //sys Dup(fd int) (nfd int, err error)
263 //sys Dup2(from int, to int) (err error)
264 //sys Exchangedata(path1 string, path2 string, options int) (err error)
265 //sys Fchdir(fd int) (err error)
266 //sys Fchflags(fd int, flags int) (err error)
267 //sys Fchmod(fd int, mode uint32) (err error)
268 //sys Fchown(fd int, uid int, gid int) (err error)
269 //sys Flock(fd int, how int) (err error)
270 //sys Fpathconf(fd int, name int) (val int, err error)
271 //sys Fsync(fd int) (err error)
272 // Fsync is not called for os.File.Sync(). Please see internal/poll/fd_fsync_darwin.go
273 //sys Ftruncate(fd int, length int64) (err error)
274 //sys Getdtablesize() (size int)
275 //sysnb Getegid() (egid int)
276 //sysnb Geteuid() (uid int)
277 //sysnb Getgid() (gid int)
278 //sysnb Getpgid(pid int) (pgid int, err error)
279 //sysnb Getpgrp() (pgrp int)
280 //sysnb Getpid() (pid int)
281 //sysnb Getppid() (ppid int)
282 //sys Getpriority(which int, who int) (prio int, err error)
283 //sysnb Getrlimit(which int, lim *Rlimit) (err error)
284 //sysnb Getrusage(who int, rusage *Rusage) (err error)
285 //sysnb Getsid(pid int) (sid int, err error)
286 //sysnb Getuid() (uid int)
287 //sysnb Issetugid() (tainted bool)
288 //sys Kqueue() (fd int, err error)
289 //sys Lchown(path string, uid int, gid int) (err error)
290 //sys Link(path string, link string) (err error)
291 //sys Listen(s int, backlog int) (err error)
292 //sys Mkdir(path string, mode uint32) (err error)
293 //sys Mkfifo(path string, mode uint32) (err error)
294 //sys Mknod(path string, mode uint32, dev int) (err error)
295 //sys Mlock(b []byte) (err error)
296 //sys Mlockall(flags int) (err error)
297 //sys Mprotect(b []byte, prot int) (err error)
298 //sys Munlock(b []byte) (err error)
299 //sys Munlockall() (err error)
300 //sys Open(path string, mode int, perm uint32) (fd int, err error)
301 //sys Pathconf(path string, name int) (val int, err error)
302 //sys Pread(fd int, p []byte, offset int64) (n int, err error)
303 //sys Pwrite(fd int, p []byte, offset int64) (n int, err error)
304 //sys read(fd int, p []byte) (n int, err error)
305 //sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
306 //sys Readlink(path string, buf []byte) (n int, err error)
307 //sys Rename(from string, to string) (err error)
308 //sys Revoke(path string) (err error)
309 //sys Rmdir(path string) (err error)
310 //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_lseek
311 //sys Select(n int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (err error)
312 //sys Setegid(egid int) (err error)
313 //sysnb Seteuid(euid int) (err error)
314 //sysnb Setgid(gid int) (err error)
315 //sys Setlogin(name string) (err error)
316 //sysnb Setpgid(pid int, pgid int) (err error)
317 //sys Setpriority(which int, who int, prio int) (err error)
318 //sys Setprivexec(flag int) (err error)
319 //sysnb Setregid(rgid int, egid int) (err error)
320 //sysnb Setreuid(ruid int, euid int) (err error)
321 //sysnb Setrlimit(which int, lim *Rlimit) (err error)
322 //sysnb Setsid() (pid int, err error)
323 //sysnb Settimeofday(tp *Timeval) (err error)
324 //sysnb Setuid(uid int) (err error)
325 //sys Symlink(path string, link string) (err error)
326 //sys Sync() (err error)
327 //sys Truncate(path string, length int64) (err error)
328 //sys Umask(newmask int) (oldmask int)
329 //sys Undelete(path string) (err error)
330 //sys Unlink(path string) (err error)
331 //sys Unmount(path string, flags int) (err error)
332 //sys write(fd int, p []byte) (n int, err error)
333 //sys writev(fd int, iovecs []Iovec) (cnt uintptr, err error)
334 //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error)
335 //sys munmap(addr uintptr, length uintptr) (err error)
336 //sysnb fork() (pid int, err error)
337 //sysnb ioctl(fd int, req int, arg int) (err error)
338 //sysnb ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_ioctl
339 //sysnb execve(path *byte, argv **byte, envp **byte) (err error)
340 //sysnb exit(res int) (err error)
341 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error)
342 //sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl
343 //sys unlinkat(fd int, path string, flags int) (err error)
344 //sys openat(fd int, path string, flags int, perm uint32) (fdret int, err error)
347 execveDarwin = execve
350 func readlen(fd int, buf *byte, nbuf int) (n int, err error) {
351 r0, _, e1 := syscall(funcPC(libc_read_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
359 func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
360 r0, _, e1 := syscall(funcPC(libc_write_trampoline), uintptr(fd), uintptr(unsafe.Pointer(buf)), uintptr(nbuf))
368 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
369 // Simulate Getdirentries using fdopendir/readdir_r/closedir.
370 const ptrSize = unsafe.Sizeof(uintptr(0))
372 // We store the number of entries to skip in the seek
373 // offset of fd. See issue #31368.
374 // It's not the full required semantics, but should handle the case
375 // of calling Getdirentries or ReadDirent repeatedly.
376 // It won't handle assigning the results of lseek to *basep, or handle
377 // the directory being edited underfoot.
378 skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
383 // We need to duplicate the incoming file descriptor
384 // because the caller expects to retain control of it, but
385 // fdopendir expects to take control of its argument.
386 // Just Dup'ing the file descriptor is not enough, as the
387 // result shares underlying state. Use openat to make a really
388 // new file descriptor referring to the same directory.
389 fd2, err := openat(fd, ".", O_RDONLY, 0)
393 d, err := fdopendir(fd2)
404 e := readdir_r(d, &entry, &entryp)
406 return n, errnoErr(e)
416 reclen := int(entry.Reclen)
417 if reclen > len(buf) {
418 // Not enough room. Return for now.
419 // The counter will let us know where we should start up again.
420 // Note: this strategy for suspending in the middle and
421 // restarting is O(n^2) in the length of the directory. Oh well.
424 // Copy entry into return buffer.
429 }{ptr: unsafe.Pointer(&entry), siz: reclen, cap: reclen}
430 copy(buf, *(*[]byte)(unsafe.Pointer(&s)))
435 // Set the seek offset of the input fd to record
436 // how many files we've already returned.
437 _, err = Seek(fd, cnt, 0 /* SEEK_SET */)
445 // Implemented in the runtime package (runtime/sys_darwin.go)
446 func syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
447 func syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
448 func syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
449 func rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
450 func rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
451 func syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
453 // Find the entry point for f. See comments in runtime/proc.go for the
454 // function of the same name.
456 func funcPC(f func()) uintptr {
457 return **(**uintptr)(unsafe.Pointer(&f))