syscall_freebsd.go 15 KB

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