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 // FreeBSD 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 13 package unix 14 15 import ( 16 "errors" 17 "sync" 18 "unsafe" 19 ) 20 21 // See https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html. 22 var ( 23 osreldateOnce sync.Once 24 osreldate uint32 25 ) 26 27 func supportsABI(ver uint32) bool { 28 osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") }) 29 return osreldate >= ver 30 } 31 32 // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets. 33 type SockaddrDatalink struct { 34 Len uint8 35 Family uint8 36 Index uint16 37 Type uint8 38 Nlen uint8 39 Alen uint8 40 Slen uint8 41 Data [46]int8 42 raw RawSockaddrDatalink 43 } 44 45 func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { 46 return nil, EAFNOSUPPORT 47 } 48 49 // Translate "kern.hostname" to []_C_int{0,1,2,3}. 50 func nametomib(name string) (mib []_C_int, err error) { 51 const siz = unsafe.Sizeof(mib[0]) 52 53 // NOTE(rsc): It seems strange to set the buffer to have 54 // size CTL_MAXNAME+2 but use only CTL_MAXNAME 55 // as the size. I don't know why the +2 is here, but the 56 // kernel uses +2 for its own implementation of this function. 57 // I am scared that if we don't include the +2 here, the kernel 58 // will silently write 2 words farther than we specify 59 // and we'll get memory corruption. 60 var buf [CTL_MAXNAME + 2]_C_int 61 n := uintptr(CTL_MAXNAME) * siz 62 63 p := (*byte)(unsafe.Pointer(&buf[0])) 64 bytes, err := ByteSliceFromString(name) 65 if err != nil { 66 return nil, err 67 } 68 69 // Magic sysctl: "setting" 0.3 to a string name 70 // lets you read back the array of integers form. 71 if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil { 72 return nil, err 73 } 74 return buf[0 : n/siz], nil 75 } 76 77 func direntIno(buf []byte) (uint64, bool) { 78 return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno)) 79 } 80 81 func direntReclen(buf []byte) (uint64, bool) { 82 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen)) 83 } 84 85 func direntNamlen(buf []byte) (uint64, bool) { 86 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen)) 87 } 88 89 func Pipe(p []int) (err error) { 90 return Pipe2(p, 0) 91 } 92 93 //sysnb pipe2(p *[2]_C_int, flags int) (err error) 94 95 func Pipe2(p []int, flags int) error { 96 if len(p) != 2 { 97 return EINVAL 98 } 99 var pp [2]_C_int 100 err := pipe2(&pp, flags) 101 if err == nil { 102 p[0] = int(pp[0]) 103 p[1] = int(pp[1]) 104 } 105 return err 106 } 107 108 func GetsockoptIPMreqn(fd, level, opt int) (*IPMreqn, error) { 109 var value IPMreqn 110 vallen := _Socklen(SizeofIPMreqn) 111 errno := getsockopt(fd, level, opt, unsafe.Pointer(&value), &vallen) 112 return &value, errno 113 } 114 115 func SetsockoptIPMreqn(fd, level, opt int, mreq *IPMreqn) (err error) { 116 return setsockopt(fd, level, opt, unsafe.Pointer(mreq), unsafe.Sizeof(*mreq)) 117 } 118 119 // GetsockoptXucred is a getsockopt wrapper that returns an Xucred struct. 120 // The usual level and opt are SOL_LOCAL and LOCAL_PEERCRED, respectively. 121 func GetsockoptXucred(fd, level, opt int) (*Xucred, error) { 122 x := new(Xucred) 123 vallen := _Socklen(SizeofXucred) 124 err := getsockopt(fd, level, opt, unsafe.Pointer(x), &vallen) 125 return x, err 126 } 127 128 func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) { 129 var rsa RawSockaddrAny 130 var len _Socklen = SizeofSockaddrAny 131 nfd, err = accept4(fd, &rsa, &len, flags) 132 if err != nil { 133 return 134 } 135 if len > SizeofSockaddrAny { 136 panic("RawSockaddrAny too small") 137 } 138 sa, err = anyToSockaddr(fd, &rsa) 139 if err != nil { 140 Close(nfd) 141 nfd = 0 142 } 143 return 144 } 145 146 //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD 147 148 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) { 149 var ( 150 _p0 unsafe.Pointer 151 bufsize uintptr 152 ) 153 if len(buf) > 0 { 154 _p0 = unsafe.Pointer(&buf[0]) 155 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf)) 156 } 157 r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags)) 158 n = int(r0) 159 if e1 != 0 { 160 err = e1 161 } 162 return 163 } 164 165 //sys ioctl(fd int, req uint, arg uintptr) (err error) = SYS_IOCTL 166 //sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL 167 168 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL 169 170 func Uname(uname *Utsname) error { 171 mib := []_C_int{CTL_KERN, KERN_OSTYPE} 172 n := unsafe.Sizeof(uname.Sysname) 173 // Suppress ENOMEM errors to be compatible with the C library __xuname() implementation. 174 if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil && !errors.Is(err, ENOMEM) { 175 return err 176 } 177 178 mib = []_C_int{CTL_KERN, KERN_HOSTNAME} 179 n = unsafe.Sizeof(uname.Nodename) 180 if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil && !errors.Is(err, ENOMEM) { 181 return err 182 } 183 184 mib = []_C_int{CTL_KERN, KERN_OSRELEASE} 185 n = unsafe.Sizeof(uname.Release) 186 if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil && !errors.Is(err, ENOMEM) { 187 return err 188 } 189 190 mib = []_C_int{CTL_KERN, KERN_VERSION} 191 n = unsafe.Sizeof(uname.Version) 192 if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil && !errors.Is(err, ENOMEM) { 193 return err 194 } 195 196 // The version might have newlines or tabs in it, convert them to 197 // spaces. 198 for i, b := range uname.Version { 199 if b == '\n' || b == '\t' { 200 if i == len(uname.Version)-1 { 201 uname.Version[i] = 0 202 } else { 203 uname.Version[i] = ' ' 204 } 205 } 206 } 207 208 mib = []_C_int{CTL_HW, HW_MACHINE} 209 n = unsafe.Sizeof(uname.Machine) 210 if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil && !errors.Is(err, ENOMEM) { 211 return err 212 } 213 214 return nil 215 } 216 217 func Stat(path string, st *Stat_t) (err error) { 218 return Fstatat(AT_FDCWD, path, st, 0) 219 } 220 221 func Lstat(path string, st *Stat_t) (err error) { 222 return Fstatat(AT_FDCWD, path, st, AT_SYMLINK_NOFOLLOW) 223 } 224 225 func Getdents(fd int, buf []byte) (n int, err error) { 226 return Getdirentries(fd, buf, nil) 227 } 228 229 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) { 230 if basep == nil || unsafe.Sizeof(*basep) == 8 { 231 return getdirentries(fd, buf, (*uint64)(unsafe.Pointer(basep))) 232 } 233 // The syscall needs a 64-bit base. On 32-bit machines 234 // we can't just use the basep passed in. See #32498. 235 var base uint64 = uint64(*basep) 236 n, err = getdirentries(fd, buf, &base) 237 *basep = uintptr(base) 238 if base>>32 != 0 { 239 // We can't stuff the base back into a uintptr, so any 240 // future calls would be suspect. Generate an error. 241 // EIO is allowed by getdirentries. 242 err = EIO 243 } 244 return 245 } 246 247 func Mknod(path string, mode uint32, dev uint64) (err error) { 248 return Mknodat(AT_FDCWD, path, mode, dev) 249 } 250 251 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { 252 if raceenabled { 253 raceReleaseMerge(unsafe.Pointer(&ioSync)) 254 } 255 return sendfile(outfd, infd, offset, count) 256 } 257 258 //sys ptrace(request int, pid int, addr uintptr, data int) (err error) 259 //sys ptracePtr(request int, pid int, addr unsafe.Pointer, data int) (err error) = SYS_PTRACE 260 261 func PtraceAttach(pid int) (err error) { 262 return ptrace(PT_ATTACH, pid, 0, 0) 263 } 264 265 func PtraceCont(pid int, signal int) (err error) { 266 return ptrace(PT_CONTINUE, pid, 1, signal) 267 } 268 269 func PtraceDetach(pid int) (err error) { 270 return ptrace(PT_DETACH, pid, 1, 0) 271 } 272 273 func PtraceGetFpRegs(pid int, fpregsout *FpReg) (err error) { 274 return ptracePtr(PT_GETFPREGS, pid, unsafe.Pointer(fpregsout), 0) 275 } 276 277 func PtraceGetRegs(pid int, regsout *Reg) (err error) { 278 return ptracePtr(PT_GETREGS, pid, unsafe.Pointer(regsout), 0) 279 } 280 281 func PtraceIO(req int, pid int, offs uintptr, out []byte, countin int) (count int, err error) { 282 ioDesc := PtraceIoDesc{ 283 Op: int32(req), 284 Offs: offs, 285 } 286 if countin > 0 { 287 _ = out[:countin] // check bounds 288 ioDesc.Addr = &out[0] 289 } else if out != nil { 290 ioDesc.Addr = (*byte)(unsafe.Pointer(&_zero)) 291 } 292 ioDesc.SetLen(countin) 293 294 err = ptracePtr(PT_IO, pid, unsafe.Pointer(&ioDesc), 0) 295 return int(ioDesc.Len), err 296 } 297 298 func PtraceLwpEvents(pid int, enable int) (err error) { 299 return ptrace(PT_LWP_EVENTS, pid, 0, enable) 300 } 301 302 func PtraceLwpInfo(pid int, info *PtraceLwpInfoStruct) (err error) { 303 return ptracePtr(PT_LWPINFO, pid, unsafe.Pointer(info), int(unsafe.Sizeof(*info))) 304 } 305 306 func PtracePeekData(pid int, addr uintptr, out []byte) (count int, err error) { 307 return PtraceIO(PIOD_READ_D, pid, addr, out, SizeofLong) 308 } 309 310 func PtracePeekText(pid int, addr uintptr, out []byte) (count int, err error) { 311 return PtraceIO(PIOD_READ_I, pid, addr, out, SizeofLong) 312 } 313 314 func PtracePokeData(pid int, addr uintptr, data []byte) (count int, err error) { 315 return PtraceIO(PIOD_WRITE_D, pid, addr, data, SizeofLong) 316 } 317 318 func PtracePokeText(pid int, addr uintptr, data []byte) (count int, err error) { 319 return PtraceIO(PIOD_WRITE_I, pid, addr, data, SizeofLong) 320 } 321 322 func PtraceSetRegs(pid int, regs *Reg) (err error) { 323 return ptracePtr(PT_SETREGS, pid, unsafe.Pointer(regs), 0) 324 } 325 326 func PtraceSingleStep(pid int) (err error) { 327 return ptrace(PT_STEP, pid, 1, 0) 328 } 329 330 func Dup3(oldfd, newfd, flags int) error { 331 if oldfd == newfd || flags&^O_CLOEXEC != 0 { 332 return EINVAL 333 } 334 how := F_DUP2FD 335 if flags&O_CLOEXEC != 0 { 336 how = F_DUP2FD_CLOEXEC 337 } 338 _, err := fcntl(oldfd, how, newfd) 339 return err 340 } 341 342 /* 343 * Exposed directly 344 */ 345 //sys Access(path string, mode uint32) (err error) 346 //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error) 347 //sys CapEnter() (err error) 348 //sys capRightsGet(version int, fd int, rightsp *CapRights) (err error) = SYS___CAP_RIGHTS_GET 349 //sys capRightsLimit(fd int, rightsp *CapRights) (err error) 350 //sys Chdir(path string) (err error) 351 //sys Chflags(path string, flags int) (err error) 352 //sys Chmod(path string, mode uint32) (err error) 353 //sys Chown(path string, uid int, gid int) (err error) 354 //sys Chroot(path string) (err error) 355 //sys ClockGettime(clockid int32, time *Timespec) (err error) 356 //sys Close(fd int) (err error) 357 //sys Dup(fd int) (nfd int, err error) 358 //sys Dup2(from int, to int) (err error) 359 //sys Exit(code int) 360 //sys ExtattrGetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 361 //sys ExtattrSetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 362 //sys ExtattrDeleteFd(fd int, attrnamespace int, attrname string) (err error) 363 //sys ExtattrListFd(fd int, attrnamespace int, data uintptr, nbytes int) (ret int, err error) 364 //sys ExtattrGetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 365 //sys ExtattrSetFile(file string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 366 //sys ExtattrDeleteFile(file string, attrnamespace int, attrname string) (err error) 367 //sys ExtattrListFile(file string, attrnamespace int, data uintptr, nbytes int) (ret int, err error) 368 //sys ExtattrGetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 369 //sys ExtattrSetLink(link string, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error) 370 //sys ExtattrDeleteLink(link string, attrnamespace int, attrname string) (err error) 371 //sys ExtattrListLink(link string, attrnamespace int, data uintptr, nbytes int) (ret int, err error) 372 //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_POSIX_FADVISE 373 //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error) 374 //sys Fchdir(fd int) (err error) 375 //sys Fchflags(fd int, flags int) (err error) 376 //sys Fchmod(fd int, mode uint32) (err error) 377 //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) 378 //sys Fchown(fd int, uid int, gid int) (err error) 379 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) 380 //sys Flock(fd int, how int) (err error) 381 //sys Fpathconf(fd int, name int) (val int, err error) 382 //sys Fstat(fd int, stat *Stat_t) (err error) 383 //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error) 384 //sys Fstatfs(fd int, stat *Statfs_t) (err error) 385 //sys Fsync(fd int) (err error) 386 //sys Ftruncate(fd int, length int64) (err error) 387 //sys getdirentries(fd int, buf []byte, basep *uint64) (n int, err error) 388 //sys Getdtablesize() (size int) 389 //sysnb Getegid() (egid int) 390 //sysnb Geteuid() (uid int) 391 //sysnb Getgid() (gid int) 392 //sysnb Getpgid(pid int) (pgid int, err error) 393 //sysnb Getpgrp() (pgrp int) 394 //sysnb Getpid() (pid int) 395 //sysnb Getppid() (ppid int) 396 //sys Getpriority(which int, who int) (prio int, err error) 397 //sysnb Getrlimit(which int, lim *Rlimit) (err error) 398 //sysnb Getrusage(who int, rusage *Rusage) (err error) 399 //sysnb Getsid(pid int) (sid int, err error) 400 //sysnb Gettimeofday(tv *Timeval) (err error) 401 //sysnb Getuid() (uid int) 402 //sys Issetugid() (tainted bool) 403 //sys Kill(pid int, signum syscall.Signal) (err error) 404 //sys Kqueue() (fd int, err error) 405 //sys Lchown(path string, uid int, gid int) (err error) 406 //sys Link(path string, link string) (err error) 407 //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error) 408 //sys Listen(s int, backlog int) (err error) 409 //sys Mkdir(path string, mode uint32) (err error) 410 //sys Mkdirat(dirfd int, path string, mode uint32) (err error) 411 //sys Mkfifo(path string, mode uint32) (err error) 412 //sys Mknodat(fd int, path string, mode uint32, dev uint64) (err error) 413 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error) 414 //sys Open(path string, mode int, perm uint32) (fd int, err error) 415 //sys Openat(fdat int, path string, mode int, perm uint32) (fd int, err error) 416 //sys Pathconf(path string, name int) (val int, err error) 417 //sys pread(fd int, p []byte, offset int64) (n int, err error) 418 //sys pwrite(fd int, p []byte, offset int64) (n int, err error) 419 //sys read(fd int, p []byte) (n int, err error) 420 //sys Readlink(path string, buf []byte) (n int, err error) 421 //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error) 422 //sys Rename(from string, to string) (err error) 423 //sys Renameat(fromfd int, from string, tofd int, to string) (err error) 424 //sys Revoke(path string) (err error) 425 //sys Rmdir(path string) (err error) 426 //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK 427 //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) 428 //sysnb Setegid(egid int) (err error) 429 //sysnb Seteuid(euid int) (err error) 430 //sysnb Setgid(gid int) (err error) 431 //sys Setlogin(name string) (err error) 432 //sysnb Setpgid(pid int, pgid int) (err error) 433 //sys Setpriority(which int, who int, prio int) (err error) 434 //sysnb Setregid(rgid int, egid int) (err error) 435 //sysnb Setreuid(ruid int, euid int) (err error) 436 //sysnb Setresgid(rgid int, egid int, sgid int) (err error) 437 //sysnb Setresuid(ruid int, euid int, suid int) (err error) 438 //sysnb Setsid() (pid int, err error) 439 //sysnb Settimeofday(tp *Timeval) (err error) 440 //sysnb Setuid(uid int) (err error) 441 //sys Statfs(path string, stat *Statfs_t) (err error) 442 //sys Symlink(path string, link string) (err error) 443 //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) 444 //sys Sync() (err error) 445 //sys Truncate(path string, length int64) (err error) 446 //sys Umask(newmask int) (oldmask int) 447 //sys Undelete(path string) (err error) 448 //sys Unlink(path string) (err error) 449 //sys Unlinkat(dirfd int, path string, flags int) (err error) 450 //sys Unmount(path string, flags int) (err error) 451 //sys write(fd int, p []byte) (n int, err error) 452 //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) 453 //sys munmap(addr uintptr, length uintptr) (err error) 454 //sys accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error) 455 //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) 456