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 // OpenBSD 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 "sort" 17 "syscall" 18 "unsafe" 19 ) 20 21 // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets. 22 type SockaddrDatalink struct { 23 Len uint8 24 Family uint8 25 Index uint16 26 Type uint8 27 Nlen uint8 28 Alen uint8 29 Slen uint8 30 Data [24]int8 31 raw RawSockaddrDatalink 32 } 33 34 func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { 35 return nil, EAFNOSUPPORT 36 } 37 38 func Syscall9(trap, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno) 39 40 func nametomib(name string) (mib []_C_int, err error) { 41 i := sort.Search(len(sysctlMib), func(i int) bool { 42 return sysctlMib[i].ctlname >= name 43 }) 44 if i < len(sysctlMib) && sysctlMib[i].ctlname == name { 45 return sysctlMib[i].ctloid, nil 46 } 47 return nil, EINVAL 48 } 49 50 func direntIno(buf []byte) (uint64, bool) { 51 return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno)) 52 } 53 54 func direntReclen(buf []byte) (uint64, bool) { 55 return readInt(buf, unsafe.Offsetof(Dirent{}.Reclen), unsafe.Sizeof(Dirent{}.Reclen)) 56 } 57 58 func direntNamlen(buf []byte) (uint64, bool) { 59 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen)) 60 } 61 62 func SysctlUvmexp(name string) (*Uvmexp, error) { 63 mib, err := sysctlmib(name) 64 if err != nil { 65 return nil, err 66 } 67 68 n := uintptr(SizeofUvmexp) 69 var u Uvmexp 70 if err := sysctl(mib, (*byte)(unsafe.Pointer(&u)), &n, nil, 0); err != nil { 71 return nil, err 72 } 73 if n != SizeofUvmexp { 74 return nil, EIO 75 } 76 return &u, nil 77 } 78 79 func Pipe(p []int) (err error) { 80 return Pipe2(p, 0) 81 } 82 83 //sysnb pipe2(p *[2]_C_int, flags int) (err error) 84 85 func Pipe2(p []int, flags int) error { 86 if len(p) != 2 { 87 return EINVAL 88 } 89 var pp [2]_C_int 90 err := pipe2(&pp, flags) 91 if err == nil { 92 p[0] = int(pp[0]) 93 p[1] = int(pp[1]) 94 } 95 return err 96 } 97 98 //sys Getdents(fd int, buf []byte) (n int, err error) 99 100 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) { 101 n, err = Getdents(fd, buf) 102 if err != nil || basep == nil { 103 return 104 } 105 106 var off int64 107 off, err = Seek(fd, 0, 1 /* SEEK_CUR */) 108 if err != nil { 109 *basep = ^uintptr(0) 110 return 111 } 112 *basep = uintptr(off) 113 if unsafe.Sizeof(*basep) == 8 { 114 return 115 } 116 if off>>32 != 0 { 117 // We can't stuff the offset back into a uintptr, so any 118 // future calls would be suspect. Generate an error. 119 // EIO was allowed by getdirentries. 120 err = EIO 121 } 122 return 123 } 124 125 //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD 126 127 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { 128 if raceenabled { 129 raceReleaseMerge(unsafe.Pointer(&ioSync)) 130 } 131 return sendfile(outfd, infd, offset, count) 132 } 133 134 // TODO 135 func sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { 136 return -1, ENOSYS 137 } 138 139 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) { 140 var bufptr *Statfs_t 141 var bufsize uintptr 142 if len(buf) > 0 { 143 bufptr = &buf[0] 144 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf)) 145 } 146 return getfsstat(bufptr, bufsize, flags) 147 } 148 149 //sysnb getresuid(ruid *_C_int, euid *_C_int, suid *_C_int) 150 //sysnb getresgid(rgid *_C_int, egid *_C_int, sgid *_C_int) 151 152 func Getresuid() (ruid, euid, suid int) { 153 var r, e, s _C_int 154 getresuid(&r, &e, &s) 155 return int(r), int(e), int(s) 156 } 157 158 func Getresgid() (rgid, egid, sgid int) { 159 var r, e, s _C_int 160 getresgid(&r, &e, &s) 161 return int(r), int(e), int(s) 162 } 163 164 //sys ioctl(fd int, req uint, arg uintptr) (err error) 165 //sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL 166 167 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL 168 169 //sys fcntl(fd int, cmd int, arg int) (n int, err error) 170 //sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (n int, err error) = SYS_FCNTL 171 172 // FcntlInt performs a fcntl syscall on fd with the provided command and argument. 173 func FcntlInt(fd uintptr, cmd, arg int) (int, error) { 174 return fcntl(int(fd), cmd, arg) 175 } 176 177 // FcntlFlock performs a fcntl syscall for the F_GETLK, F_SETLK or F_SETLKW command. 178 func FcntlFlock(fd uintptr, cmd int, lk *Flock_t) error { 179 _, err := fcntlPtr(int(fd), cmd, unsafe.Pointer(lk)) 180 return err 181 } 182 183 //sys ppoll(fds *PollFd, nfds int, timeout *Timespec, sigmask *Sigset_t) (n int, err error) 184 185 func Ppoll(fds []PollFd, timeout *Timespec, sigmask *Sigset_t) (n int, err error) { 186 if len(fds) == 0 { 187 return ppoll(nil, 0, timeout, sigmask) 188 } 189 return ppoll(&fds[0], len(fds), timeout, sigmask) 190 } 191 192 func Uname(uname *Utsname) error { 193 mib := []_C_int{CTL_KERN, KERN_OSTYPE} 194 n := unsafe.Sizeof(uname.Sysname) 195 if err := sysctl(mib, &uname.Sysname[0], &n, nil, 0); err != nil { 196 return err 197 } 198 199 mib = []_C_int{CTL_KERN, KERN_HOSTNAME} 200 n = unsafe.Sizeof(uname.Nodename) 201 if err := sysctl(mib, &uname.Nodename[0], &n, nil, 0); err != nil { 202 return err 203 } 204 205 mib = []_C_int{CTL_KERN, KERN_OSRELEASE} 206 n = unsafe.Sizeof(uname.Release) 207 if err := sysctl(mib, &uname.Release[0], &n, nil, 0); err != nil { 208 return err 209 } 210 211 mib = []_C_int{CTL_KERN, KERN_VERSION} 212 n = unsafe.Sizeof(uname.Version) 213 if err := sysctl(mib, &uname.Version[0], &n, nil, 0); err != nil { 214 return err 215 } 216 217 // The version might have newlines or tabs in it, convert them to 218 // spaces. 219 for i, b := range uname.Version { 220 if b == '\n' || b == '\t' { 221 if i == len(uname.Version)-1 { 222 uname.Version[i] = 0 223 } else { 224 uname.Version[i] = ' ' 225 } 226 } 227 } 228 229 mib = []_C_int{CTL_HW, HW_MACHINE} 230 n = unsafe.Sizeof(uname.Machine) 231 if err := sysctl(mib, &uname.Machine[0], &n, nil, 0); err != nil { 232 return err 233 } 234 235 return nil 236 } 237 238 /* 239 * Exposed directly 240 */ 241 //sys Access(path string, mode uint32) (err error) 242 //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error) 243 //sys Chdir(path string) (err error) 244 //sys Chflags(path string, flags int) (err error) 245 //sys Chmod(path string, mode uint32) (err error) 246 //sys Chown(path string, uid int, gid int) (err error) 247 //sys Chroot(path string) (err error) 248 //sys ClockGettime(clockid int32, time *Timespec) (err error) 249 //sys Close(fd int) (err error) 250 //sys Dup(fd int) (nfd int, err error) 251 //sys Dup2(from int, to int) (err error) 252 //sys Dup3(from int, to int, flags int) (err error) 253 //sys Exit(code int) 254 //sys Faccessat(dirfd int, path string, mode uint32, flags int) (err error) 255 //sys Fchdir(fd int) (err error) 256 //sys Fchflags(fd int, flags int) (err error) 257 //sys Fchmod(fd int, mode uint32) (err error) 258 //sys Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) 259 //sys Fchown(fd int, uid int, gid int) (err error) 260 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) 261 //sys Flock(fd int, how int) (err error) 262 //sys Fpathconf(fd int, name int) (val int, err error) 263 //sys Fstat(fd int, stat *Stat_t) (err error) 264 //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error) 265 //sys Fstatfs(fd int, stat *Statfs_t) (err error) 266 //sys Fsync(fd int) (err error) 267 //sys Ftruncate(fd int, length int64) (err error) 268 //sysnb Getegid() (egid int) 269 //sysnb Geteuid() (uid int) 270 //sysnb Getgid() (gid int) 271 //sysnb Getpgid(pid int) (pgid int, err error) 272 //sysnb Getpgrp() (pgrp int) 273 //sysnb Getpid() (pid int) 274 //sysnb Getppid() (ppid int) 275 //sys Getpriority(which int, who int) (prio int, err error) 276 //sysnb Getrlimit(which int, lim *Rlimit) (err error) 277 //sysnb Getrtable() (rtable int, err error) 278 //sysnb Getrusage(who int, rusage *Rusage) (err error) 279 //sysnb Getsid(pid int) (sid int, err error) 280 //sysnb Gettimeofday(tv *Timeval) (err error) 281 //sysnb Getuid() (uid int) 282 //sys Issetugid() (tainted bool) 283 //sys Kill(pid int, signum syscall.Signal) (err error) 284 //sys Kqueue() (fd int, err error) 285 //sys Lchown(path string, uid int, gid int) (err error) 286 //sys Link(path string, link string) (err error) 287 //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error) 288 //sys Listen(s int, backlog int) (err error) 289 //sys Lstat(path string, stat *Stat_t) (err error) 290 //sys Mkdir(path string, mode uint32) (err error) 291 //sys Mkdirat(dirfd int, path string, mode uint32) (err error) 292 //sys Mkfifo(path string, mode uint32) (err error) 293 //sys Mkfifoat(dirfd int, path string, mode uint32) (err error) 294 //sys Mknod(path string, mode uint32, dev int) (err error) 295 //sys Mknodat(dirfd int, path string, mode uint32, dev int) (err error) 296 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error) 297 //sys Open(path string, mode int, perm uint32) (fd int, err error) 298 //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) 299 //sys Pathconf(path string, name int) (val int, err error) 300 //sys pread(fd int, p []byte, offset int64) (n int, err error) 301 //sys pwrite(fd int, p []byte, offset int64) (n int, err error) 302 //sys read(fd int, p []byte) (n int, err error) 303 //sys Readlink(path string, buf []byte) (n int, err error) 304 //sys Readlinkat(dirfd int, path string, buf []byte) (n int, err error) 305 //sys Rename(from string, to string) (err error) 306 //sys Renameat(fromfd int, from string, tofd int, to string) (err error) 307 //sys Revoke(path string) (err error) 308 //sys Rmdir(path string) (err error) 309 //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK 310 //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) 311 //sysnb Setegid(egid int) (err error) 312 //sysnb Seteuid(euid int) (err error) 313 //sysnb Setgid(gid int) (err error) 314 //sys Setlogin(name string) (err error) 315 //sysnb Setpgid(pid int, pgid int) (err error) 316 //sys Setpriority(which int, who int, prio int) (err error) 317 //sysnb Setregid(rgid int, egid int) (err error) 318 //sysnb Setreuid(ruid int, euid int) (err error) 319 //sysnb Setresgid(rgid int, egid int, sgid int) (err error) 320 //sysnb Setresuid(ruid int, euid int, suid int) (err error) 321 //sysnb Setrtable(rtable int) (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 Stat(path string, stat *Stat_t) (err error) 326 //sys Statfs(path string, stat *Statfs_t) (err error) 327 //sys Symlink(path string, link string) (err error) 328 //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) 329 //sys Sync() (err error) 330 //sys Truncate(path string, length int64) (err error) 331 //sys Umask(newmask int) (oldmask int) 332 //sys Unlink(path string) (err error) 333 //sys Unlinkat(dirfd int, path string, flags int) (err error) 334 //sys Unmount(path string, flags int) (err error) 335 //sys write(fd int, p []byte) (n int, err error) 336 //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) 337 //sys munmap(addr uintptr, length uintptr) (err error) 338 //sys getfsstat(stat *Statfs_t, bufsize uintptr, flags int) (n int, err error) 339 //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) 340 //sys pledge(promises *byte, execpromises *byte) (err error) 341 //sys unveil(path *byte, flags *byte) (err error) 342