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