syscall_dragonfly.go (14674B)
1 // Copyright 2009 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 // DragonFly BSD 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 "sync" 17 "unsafe" 18 ) 19 20 // See version list in https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/sys/sys/param.h 21 var ( 22 osreldateOnce sync.Once 23 osreldate uint32 24 ) 25 26 // First __DragonFly_version after September 2019 ABI changes 27 // http://lists.dragonflybsd.org/pipermail/users/2019-September/358280.html 28 const _dragonflyABIChangeVersion = 500705 29 30 func supportsABI(ver uint32) bool { 31 osreldateOnce.Do(func() { osreldate, _ = SysctlUint32("kern.osreldate") }) 32 return osreldate >= ver 33 } 34 35 // SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets. 36 type SockaddrDatalink struct { 37 Len uint8 38 Family uint8 39 Index uint16 40 Type uint8 41 Nlen uint8 42 Alen uint8 43 Slen uint8 44 Data [12]int8 45 Rcf uint16 46 Route [16]uint16 47 raw RawSockaddrDatalink 48 } 49 50 func anyToSockaddrGOOS(fd int, rsa *RawSockaddrAny) (Sockaddr, error) { 51 return nil, EAFNOSUPPORT 52 } 53 54 // Translate "kern.hostname" to []_C_int{0,1,2,3}. 55 func nametomib(name string) (mib []_C_int, err error) { 56 const siz = unsafe.Sizeof(mib[0]) 57 58 // NOTE(rsc): It seems strange to set the buffer to have 59 // size CTL_MAXNAME+2 but use only CTL_MAXNAME 60 // as the size. I don't know why the +2 is here, but the 61 // kernel uses +2 for its own implementation of this function. 62 // I am scared that if we don't include the +2 here, the kernel 63 // will silently write 2 words farther than we specify 64 // and we'll get memory corruption. 65 var buf [CTL_MAXNAME + 2]_C_int 66 n := uintptr(CTL_MAXNAME) * siz 67 68 p := (*byte)(unsafe.Pointer(&buf[0])) 69 bytes, err := ByteSliceFromString(name) 70 if err != nil { 71 return nil, err 72 } 73 74 // Magic sysctl: "setting" 0.3 to a string name 75 // lets you read back the array of integers form. 76 if err = sysctl([]_C_int{0, 3}, p, &n, &bytes[0], uintptr(len(name))); err != nil { 77 return nil, err 78 } 79 return buf[0 : n/siz], nil 80 } 81 82 func direntIno(buf []byte) (uint64, bool) { 83 return readInt(buf, unsafe.Offsetof(Dirent{}.Fileno), unsafe.Sizeof(Dirent{}.Fileno)) 84 } 85 86 func direntReclen(buf []byte) (uint64, bool) { 87 namlen, ok := direntNamlen(buf) 88 if !ok { 89 return 0, false 90 } 91 return (16 + namlen + 1 + 7) &^ 7, true 92 } 93 94 func direntNamlen(buf []byte) (uint64, bool) { 95 return readInt(buf, unsafe.Offsetof(Dirent{}.Namlen), unsafe.Sizeof(Dirent{}.Namlen)) 96 } 97 98 //sysnb pipe() (r int, w int, err error) 99 100 func Pipe(p []int) (err error) { 101 if len(p) != 2 { 102 return EINVAL 103 } 104 r, w, err := pipe() 105 if err == nil { 106 p[0], p[1] = r, w 107 } 108 return 109 } 110 111 //sysnb pipe2(p *[2]_C_int, flags int) (r int, w int, err error) 112 113 func Pipe2(p []int, flags int) (err error) { 114 if len(p) != 2 { 115 return EINVAL 116 } 117 var pp [2]_C_int 118 // pipe2 on dragonfly takes an fds array as an argument, but still 119 // returns the file descriptors. 120 r, w, err := pipe2(&pp, flags) 121 if err == nil { 122 p[0], p[1] = r, w 123 } 124 return err 125 } 126 127 //sys extpread(fd int, p []byte, flags int, offset int64) (n int, err error) 128 129 func pread(fd int, p []byte, offset int64) (n int, err error) { 130 return extpread(fd, p, 0, offset) 131 } 132 133 //sys extpwrite(fd int, p []byte, flags int, offset int64) (n int, err error) 134 135 func pwrite(fd int, p []byte, offset int64) (n int, err error) { 136 return extpwrite(fd, p, 0, offset) 137 } 138 139 func Accept4(fd, flags int) (nfd int, sa Sockaddr, err error) { 140 var rsa RawSockaddrAny 141 var len _Socklen = SizeofSockaddrAny 142 nfd, err = accept4(fd, &rsa, &len, flags) 143 if err != nil { 144 return 145 } 146 if len > SizeofSockaddrAny { 147 panic("RawSockaddrAny too small") 148 } 149 sa, err = anyToSockaddr(fd, &rsa) 150 if err != nil { 151 Close(nfd) 152 nfd = 0 153 } 154 return 155 } 156 157 //sys Getcwd(buf []byte) (n int, err error) = SYS___GETCWD 158 159 func Getfsstat(buf []Statfs_t, flags int) (n int, err error) { 160 var _p0 unsafe.Pointer 161 var bufsize uintptr 162 if len(buf) > 0 { 163 _p0 = unsafe.Pointer(&buf[0]) 164 bufsize = unsafe.Sizeof(Statfs_t{}) * uintptr(len(buf)) 165 } 166 r0, _, e1 := Syscall(SYS_GETFSSTAT, uintptr(_p0), bufsize, uintptr(flags)) 167 n = int(r0) 168 if e1 != 0 { 169 err = e1 170 } 171 return 172 } 173 174 //sys ioctl(fd int, req uint, arg uintptr) (err error) 175 //sys ioctlPtr(fd int, req uint, arg unsafe.Pointer) (err error) = SYS_IOCTL 176 177 //sys sysctl(mib []_C_int, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) = SYS___SYSCTL 178 179 func sysctlUname(mib []_C_int, old *byte, oldlen *uintptr) error { 180 err := sysctl(mib, old, oldlen, nil, 0) 181 if err != nil { 182 // Utsname members on Dragonfly are only 32 bytes and 183 // the syscall returns ENOMEM in case the actual value 184 // is longer. 185 if err == ENOMEM { 186 err = nil 187 } 188 } 189 return err 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 := sysctlUname(mib, &uname.Sysname[0], &n); err != nil { 196 return err 197 } 198 uname.Sysname[unsafe.Sizeof(uname.Sysname)-1] = 0 199 200 mib = []_C_int{CTL_KERN, KERN_HOSTNAME} 201 n = unsafe.Sizeof(uname.Nodename) 202 if err := sysctlUname(mib, &uname.Nodename[0], &n); err != nil { 203 return err 204 } 205 uname.Nodename[unsafe.Sizeof(uname.Nodename)-1] = 0 206 207 mib = []_C_int{CTL_KERN, KERN_OSRELEASE} 208 n = unsafe.Sizeof(uname.Release) 209 if err := sysctlUname(mib, &uname.Release[0], &n); err != nil { 210 return err 211 } 212 uname.Release[unsafe.Sizeof(uname.Release)-1] = 0 213 214 mib = []_C_int{CTL_KERN, KERN_VERSION} 215 n = unsafe.Sizeof(uname.Version) 216 if err := sysctlUname(mib, &uname.Version[0], &n); err != nil { 217 return err 218 } 219 220 // The version might have newlines or tabs in it, convert them to 221 // spaces. 222 for i, b := range uname.Version { 223 if b == '\n' || b == '\t' { 224 if i == len(uname.Version)-1 { 225 uname.Version[i] = 0 226 } else { 227 uname.Version[i] = ' ' 228 } 229 } 230 } 231 232 mib = []_C_int{CTL_HW, HW_MACHINE} 233 n = unsafe.Sizeof(uname.Machine) 234 if err := sysctlUname(mib, &uname.Machine[0], &n); err != nil { 235 return err 236 } 237 uname.Machine[unsafe.Sizeof(uname.Machine)-1] = 0 238 239 return nil 240 } 241 242 func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) { 243 if raceenabled { 244 raceReleaseMerge(unsafe.Pointer(&ioSync)) 245 } 246 return sendfile(outfd, infd, offset, count) 247 } 248 249 /* 250 * Exposed directly 251 */ 252 //sys Access(path string, mode uint32) (err error) 253 //sys Adjtime(delta *Timeval, olddelta *Timeval) (err error) 254 //sys Chdir(path string) (err error) 255 //sys Chflags(path string, flags int) (err error) 256 //sys Chmod(path string, mode uint32) (err error) 257 //sys Chown(path string, uid int, gid int) (err error) 258 //sys Chroot(path string) (err error) 259 //sys ClockGettime(clockid int32, time *Timespec) (err error) 260 //sys Close(fd int) (err error) 261 //sys Dup(fd int) (nfd int, err error) 262 //sys Dup2(from int, to int) (err error) 263 //sys Exit(code int) 264 //sys Faccessat(dirfd int, path string, mode uint32, flags 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 Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) 269 //sys Fchown(fd int, uid int, gid int) (err error) 270 //sys Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) 271 //sys Flock(fd int, how int) (err error) 272 //sys Fpathconf(fd int, name int) (val int, err error) 273 //sys Fstat(fd int, stat *Stat_t) (err error) 274 //sys Fstatat(fd int, path string, stat *Stat_t, flags int) (err error) 275 //sys Fstatfs(fd int, stat *Statfs_t) (err error) 276 //sys Fsync(fd int) (err error) 277 //sys Ftruncate(fd int, length int64) (err error) 278 //sys Getdents(fd int, buf []byte) (n int, err error) 279 //sys Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) 280 //sys Getdtablesize() (size int) 281 //sysnb Getegid() (egid int) 282 //sysnb Geteuid() (uid int) 283 //sysnb Getgid() (gid int) 284 //sysnb Getpgid(pid int) (pgid int, err error) 285 //sysnb Getpgrp() (pgrp int) 286 //sysnb Getpid() (pid int) 287 //sysnb Getppid() (ppid int) 288 //sys Getpriority(which int, who int) (prio int, err error) 289 //sysnb Getrlimit(which int, lim *Rlimit) (err error) 290 //sysnb Getrusage(who int, rusage *Rusage) (err error) 291 //sysnb Getsid(pid int) (sid int, err error) 292 //sysnb Gettimeofday(tv *Timeval) (err error) 293 //sysnb Getuid() (uid int) 294 //sys Issetugid() (tainted bool) 295 //sys Kill(pid int, signum syscall.Signal) (err error) 296 //sys Kqueue() (fd int, err error) 297 //sys Lchown(path string, uid int, gid int) (err error) 298 //sys Link(path string, link string) (err error) 299 //sys Linkat(pathfd int, path string, linkfd int, link string, flags int) (err error) 300 //sys Listen(s int, backlog int) (err error) 301 //sys Lstat(path string, stat *Stat_t) (err error) 302 //sys Mkdir(path string, mode uint32) (err error) 303 //sys Mkdirat(dirfd int, path string, mode uint32) (err error) 304 //sys Mkfifo(path string, mode uint32) (err error) 305 //sys Mknod(path string, mode uint32, dev int) (err error) 306 //sys Mknodat(fd int, path string, mode uint32, dev int) (err error) 307 //sys Nanosleep(time *Timespec, leftover *Timespec) (err error) 308 //sys Open(path string, mode int, perm uint32) (fd int, err error) 309 //sys Openat(dirfd int, path string, mode int, perm uint32) (fd int, err error) 310 //sys Pathconf(path string, name int) (val int, err error) 311 //sys read(fd int, p []byte) (n int, err error) 312 //sys Readlink(path string, buf []byte) (n int, err error) 313 //sys Rename(from string, to string) (err error) 314 //sys Renameat(fromfd int, from string, tofd int, to string) (err error) 315 //sys Revoke(path string) (err error) 316 //sys Rmdir(path string) (err error) 317 //sys Seek(fd int, offset int64, whence int) (newoffset int64, err error) = SYS_LSEEK 318 //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) 319 //sysnb Setegid(egid int) (err error) 320 //sysnb Seteuid(euid int) (err error) 321 //sysnb Setgid(gid int) (err error) 322 //sys Setlogin(name string) (err error) 323 //sysnb Setpgid(pid int, pgid int) (err error) 324 //sys Setpriority(which int, who int, prio int) (err error) 325 //sysnb Setregid(rgid int, egid int) (err error) 326 //sysnb Setreuid(ruid int, euid int) (err error) 327 //sysnb Setresgid(rgid int, egid int, sgid int) (err error) 328 //sysnb Setresuid(ruid int, euid int, suid int) (err error) 329 //sysnb Setsid() (pid int, err error) 330 //sysnb Settimeofday(tp *Timeval) (err error) 331 //sysnb Setuid(uid int) (err error) 332 //sys Stat(path string, stat *Stat_t) (err error) 333 //sys Statfs(path string, stat *Statfs_t) (err error) 334 //sys Symlink(path string, link string) (err error) 335 //sys Symlinkat(oldpath string, newdirfd int, newpath string) (err error) 336 //sys Sync() (err error) 337 //sys Truncate(path string, length int64) (err error) 338 //sys Umask(newmask int) (oldmask int) 339 //sys Undelete(path string) (err error) 340 //sys Unlink(path string) (err error) 341 //sys Unlinkat(dirfd int, path string, flags int) (err error) 342 //sys Unmount(path string, flags int) (err error) 343 //sys write(fd int, p []byte) (n int, err error) 344 //sys mmap(addr uintptr, length uintptr, prot int, flag int, fd int, pos int64) (ret uintptr, err error) 345 //sys munmap(addr uintptr, length uintptr) (err error) 346 //sys readlen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_READ 347 //sys writelen(fd int, buf *byte, nbuf int) (n int, err error) = SYS_WRITE 348 //sys accept4(fd int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (nfd int, err error) 349 //sys utimensat(dirfd int, path string, times *[2]Timespec, flags int) (err error) 350 351 /* 352 * Unimplemented 353 * TODO(jsing): Update this list for DragonFly. 354 */ 355 // Profil 356 // Sigaction 357 // Sigprocmask 358 // Getlogin 359 // Sigpending 360 // Sigaltstack 361 // Reboot 362 // Execve 363 // Vfork 364 // Sbrk 365 // Sstk 366 // Ovadvise 367 // Mincore 368 // Setitimer 369 // Swapon 370 // Select 371 // Sigsuspend 372 // Readv 373 // Writev 374 // Nfssvc 375 // Getfh 376 // Quotactl 377 // Mount 378 // Csops 379 // Waitid 380 // Add_profil 381 // Kdebug_trace 382 // Sigreturn 383 // Atsocket 384 // Kqueue_from_portset_np 385 // Kqueue_portset 386 // Getattrlist 387 // Setattrlist 388 // Getdirentriesattr 389 // Searchfs 390 // Delete 391 // Copyfile 392 // Watchevent 393 // Waitevent 394 // Modwatch 395 // Getxattr 396 // Fgetxattr 397 // Setxattr 398 // Fsetxattr 399 // Removexattr 400 // Fremovexattr 401 // Listxattr 402 // Flistxattr 403 // Fsctl 404 // Initgroups 405 // Posix_spawn 406 // Nfsclnt 407 // Fhopen 408 // Minherit 409 // Semsys 410 // Msgsys 411 // Shmsys 412 // Semctl 413 // Semget 414 // Semop 415 // Msgctl 416 // Msgget 417 // Msgsnd 418 // Msgrcv 419 // Shmat 420 // Shmctl 421 // Shmdt 422 // Shmget 423 // Shm_open 424 // Shm_unlink 425 // Sem_open 426 // Sem_close 427 // Sem_unlink 428 // Sem_wait 429 // Sem_trywait 430 // Sem_post 431 // Sem_getvalue 432 // Sem_init 433 // Sem_destroy 434 // Open_extended 435 // Umask_extended 436 // Stat_extended 437 // Lstat_extended 438 // Fstat_extended 439 // Chmod_extended 440 // Fchmod_extended 441 // Access_extended 442 // Settid 443 // Gettid 444 // Setsgroups 445 // Getsgroups 446 // Setwgroups 447 // Getwgroups 448 // Mkfifo_extended 449 // Mkdir_extended 450 // Identitysvc 451 // Shared_region_check_np 452 // Shared_region_map_np 453 // __pthread_mutex_destroy 454 // __pthread_mutex_init 455 // __pthread_mutex_lock 456 // __pthread_mutex_trylock 457 // __pthread_mutex_unlock 458 // __pthread_cond_init 459 // __pthread_cond_destroy 460 // __pthread_cond_broadcast 461 // __pthread_cond_signal 462 // Setsid_with_pid 463 // __pthread_cond_timedwait 464 // Aio_fsync 465 // Aio_return 466 // Aio_suspend 467 // Aio_cancel 468 // Aio_error 469 // Aio_read 470 // Aio_write 471 // Lio_listio 472 // __pthread_cond_wait 473 // Iopolicysys 474 // __pthread_kill 475 // __pthread_sigmask 476 // __sigwait 477 // __disable_threadsignal 478 // __pthread_markcancel 479 // __pthread_canceled 480 // __semwait_signal 481 // Proc_info 482 // Stat64_extended 483 // Lstat64_extended 484 // Fstat64_extended 485 // __pthread_chdir 486 // __pthread_fchdir 487 // Audit 488 // Auditon 489 // Getauid 490 // Setauid 491 // Getaudit 492 // Setaudit 493 // Getaudit_addr 494 // Setaudit_addr 495 // Auditctl 496 // Bsdthread_create 497 // Bsdthread_terminate 498 // Stack_snapshot 499 // Bsdthread_register 500 // Workq_open 501 // Workq_ops 502 // __mac_execve 503 // __mac_syscall 504 // __mac_get_file 505 // __mac_set_file 506 // __mac_get_link 507 // __mac_set_link 508 // __mac_get_proc 509 // __mac_set_proc 510 // __mac_get_fd 511 // __mac_set_fd 512 // __mac_get_pid 513 // __mac_get_lcid 514 // __mac_get_lctx 515 // __mac_set_lctx 516 // Setlcid 517 // Read_nocancel 518 // Write_nocancel 519 // Open_nocancel 520 // Close_nocancel 521 // Wait4_nocancel 522 // Recvmsg_nocancel 523 // Sendmsg_nocancel 524 // Recvfrom_nocancel 525 // Accept_nocancel 526 // Fcntl_nocancel 527 // Select_nocancel 528 // Fsync_nocancel 529 // Connect_nocancel 530 // Sigsuspend_nocancel 531 // Readv_nocancel 532 // Writev_nocancel 533 // Sendto_nocancel 534 // Pread_nocancel 535 // Pwrite_nocancel 536 // Waitid_nocancel 537 // Msgsnd_nocancel 538 // Msgrcv_nocancel 539 // Sem_wait_nocancel 540 // Aio_suspend_nocancel 541 // __sigwait_nocancel 542 // __semwait_signal_nocancel 543 // __mac_mount 544 // __mac_get_mount 545 // __mac_getfsstat